diff --git a/Wabbajack.Common/Extensions/DictionaryExt.cs b/Wabbajack.Common/Extensions/DictionaryExt.cs new file mode 100644 index 00000000..cd2ea840 --- /dev/null +++ b/Wabbajack.Common/Extensions/DictionaryExt.cs @@ -0,0 +1,25 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Wabbajack +{ + public static class DictionaryExt + { + public static V TryCreate(this IDictionary dict, K key) + where V : new() + { + return dict.TryCreate(key, () => new V()); + } + + public static V TryCreate(this IDictionary dict, K key, Func create) + { + if (dict.TryGetValue(key, out var val)) return val; + var ret = create(); + dict[key] = ret; + return ret; + } + } +} diff --git a/Wabbajack.Common/Extensions/RxExt.cs b/Wabbajack.Common/Extensions/RxExt.cs new file mode 100644 index 00000000..8bf082d7 --- /dev/null +++ b/Wabbajack.Common/Extensions/RxExt.cs @@ -0,0 +1,172 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Reactive; +using System.Reactive.Concurrency; +using System.Reactive.Disposables; +using System.Reactive.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Wabbajack +{ + public static class RxExt + { + /// + /// Convenience function that discards events that are null + /// + /// + /// + /// Source events that are not null + public static IObservable NotNull(this IObservable source) + where T : class + { + return source.Where(u => u != null); + } + + /// + /// Converts any observable to type Unit. Useful for when you care that a signal occurred, + /// but don't care about what its value is downstream. + /// + /// An observable that returns Unit anytime the source signal fires an event. + public static IObservable Unit(this IObservable source) + { + return source.Select(_ => System.Reactive.Unit.Default); + } + + /// + /// Convenience operator to subscribe to the source observable, only when a second "switch" observable is on. + /// When the switch is on, the source will be subscribed to, and its updates passed through. + /// When the switch is off, the subscription to the source observable will be stopped, and no signal will be published. + /// + /// Source observable to subscribe to if on + /// On/Off signal of whether to subscribe to source observable + /// Observable that publishes data from source, if the switch is on. + public static IObservable FilterSwitch(this IObservable source, IObservable filterSwitch) + { + return filterSwitch + .DistinctUntilChanged() + .Select(on => + { + if (on) + { + return source; + } + else + { + return Observable.Empty(); + } + }) + .Switch(); + } + + + /// Inspiration: + /// http://reactivex.io/documentation/operators/debounce.html + /// https://stackoverflow.com/questions/20034476/how-can-i-use-reactive-extensions-to-throttle-events-using-a-max-window-size + public static IObservable Debounce(this IObservable source, TimeSpan interval, IScheduler scheduler = null) + { + scheduler = scheduler ?? Scheduler.Default; + return Observable.Create(o => + { + var hasValue = false; + bool throttling = false; + T value = default; + + var dueTimeDisposable = new SerialDisposable(); + + void internalCallback() + { + if (hasValue) + { + // We have another value that came in to fire. + // Reregister for callback + dueTimeDisposable.Disposable = scheduler.Schedule(interval, internalCallback); + o.OnNext(value); + value = default; + hasValue = false; + } + else + { + // Nothing to do, throttle is complete. + throttling = false; + } + } + + return source.Subscribe( + onNext: (x) => + { + if (!throttling) + { + // Fire initial value + o.OnNext(x); + // Mark that we're throttling + throttling = true; + // Register for callback when throttle is complete + dueTimeDisposable.Disposable = scheduler.Schedule(interval, internalCallback); + } + else + { + // In the middle of throttle + // Save value and return + hasValue = true; + value = x; + } + }, + onError: o.OnError, + onCompleted: o.OnCompleted); + }); + } + + public static IObservable SelectTask(this IObservable source, Func task) + { + return source + .SelectMany(async i => + { + await task(i).ConfigureAwait(false); + return System.Reactive.Unit.Default; + }); + } + + public static IObservable SelectTask(this IObservable source, Func task) + { + return source + .SelectMany(async _ => + { + await task().ConfigureAwait(false); + return System.Reactive.Unit.Default; + }); + } + + public static IObservable SelectTask(this IObservable source, Func> task) + { + return source + .SelectMany(_ => task()); + } + + public static IObservable SelectTask(this IObservable source, Func> task) + { + return source + .SelectMany(x => task(x)); + } + + public static IObservable DoTask(this IObservable source, Func task) + { + return source + .SelectMany(async (x) => + { + await task(x).ConfigureAwait(false); + return x; + }); + } + + public static IObservable WhereCastable(this IObservable source) + where R : class + where T : class + { + return source + .Select(x => x as R) + .NotNull(); + } + } +} diff --git a/Wabbajack.Lib/Extensions/TaskExt.cs b/Wabbajack.Common/Extensions/TaskExt.cs similarity index 100% rename from Wabbajack.Lib/Extensions/TaskExt.cs rename to Wabbajack.Common/Extensions/TaskExt.cs diff --git a/Wabbajack.Common/Wabbajack.Common.csproj b/Wabbajack.Common/Wabbajack.Common.csproj index f8a4ba0f..9485a93e 100644 --- a/Wabbajack.Common/Wabbajack.Common.csproj +++ b/Wabbajack.Common/Wabbajack.Common.csproj @@ -92,7 +92,10 @@ + + + diff --git a/Wabbajack.Lib/UI/UIUtils.cs b/Wabbajack.Lib/UI/UIUtils.cs index 13471acb..a3fc6c24 100644 --- a/Wabbajack.Lib/UI/UIUtils.cs +++ b/Wabbajack.Lib/UI/UIUtils.cs @@ -92,10 +92,11 @@ namespace Wabbajack.Lib } } - public static string OpenFileDialog(string filter) + public static string OpenFileDialog(string filter, string initialDirectory = null) { OpenFileDialog ofd = new OpenFileDialog(); ofd.Filter = filter; + ofd.InitialDirectory = initialDirectory; if (ofd.ShowDialog() == DialogResult.OK) return ofd.FileName; return null; diff --git a/Wabbajack.Lib/Wabbajack.Lib.csproj b/Wabbajack.Lib/Wabbajack.Lib.csproj index 09184a29..433b08cd 100644 --- a/Wabbajack.Lib/Wabbajack.Lib.csproj +++ b/Wabbajack.Lib/Wabbajack.Lib.csproj @@ -116,7 +116,6 @@ - diff --git a/Wabbajack/Extensions/ReactiveUIExt.cs b/Wabbajack/Extensions/ReactiveUIExt.cs index 372794a8..878b564c 100644 --- a/Wabbajack/Extensions/ReactiveUIExt.cs +++ b/Wabbajack/Extensions/ReactiveUIExt.cs @@ -31,18 +31,6 @@ namespace Wabbajack return This.WhenAny(property1, selector: x => x.GetValue()); } - /// - /// Convenience function that discards events that are null - /// - /// - /// - /// Source events that are not null - public static IObservable NotNull(this IObservable source) - where T : class - { - return source.Where(u => u != null); - } - /// /// Convenience wrapper to observe following calls on the GUI thread. /// @@ -51,42 +39,6 @@ namespace Wabbajack return source.ObserveOn(RxApp.MainThreadScheduler); } - /// - /// Converts any observable to type Unit. Useful for when you care that a signal occurred, - /// but don't care about what its value is downstream. - /// - /// An observable that returns Unit anytime the source signal fires an event. - public static IObservable Unit(this IObservable source) - { - return source.Select(_ => System.Reactive.Unit.Default); - } - - /// - /// Convenience operator to subscribe to the source observable, only when a second "switch" observable is on. - /// When the switch is on, the source will be subscribed to, and its updates passed through. - /// When the switch is off, the subscription to the source observable will be stopped, and no signal will be published. - /// - /// Source observable to subscribe to if on - /// On/Off signal of whether to subscribe to source observable - /// Observable that publishes data from source, if the switch is on. - public static IObservable FilterSwitch(this IObservable source, IObservable filterSwitch) - { - return filterSwitch - .DistinctUntilChanged() - .Select(on => - { - if (on) - { - return source; - } - else - { - return Observable.Empty(); - } - }) - .Switch(); - } - public static IObservable StartingExecution(this IReactiveCommand cmd) { return cmd.IsExecuting @@ -95,114 +47,6 @@ namespace Wabbajack .Unit(); } - /// Inspiration: - /// http://reactivex.io/documentation/operators/debounce.html - /// https://stackoverflow.com/questions/20034476/how-can-i-use-reactive-extensions-to-throttle-events-using-a-max-window-size - public static IObservable Debounce(this IObservable source, TimeSpan interval, IScheduler scheduler = null) - { - scheduler = scheduler ?? Scheduler.Default; - return Observable.Create(o => - { - var hasValue = false; - bool throttling = false; - T value = default; - - var dueTimeDisposable = new SerialDisposable(); - - void internalCallback() - { - if (hasValue) - { - // We have another value that came in to fire. - // Reregister for callback - dueTimeDisposable.Disposable = scheduler.Schedule(interval, internalCallback); - o.OnNext(value); - value = default; - hasValue = false; - } - else - { - // Nothing to do, throttle is complete. - throttling = false; - } - } - - return source.Subscribe( - onNext: (x) => - { - if (!throttling) - { - // Fire initial value - o.OnNext(x); - // Mark that we're throttling - throttling = true; - // Register for callback when throttle is complete - dueTimeDisposable.Disposable = scheduler.Schedule(interval, internalCallback); - } - else - { - // In the middle of throttle - // Save value and return - hasValue = true; - value = x; - } - }, - onError: o.OnError, - onCompleted: o.OnCompleted); - }); - } - - public static IObservable SelectTask(this IObservable source, Func task) - { - return source - .SelectMany(async i => - { - await task(i).ConfigureAwait(false); - return System.Reactive.Unit.Default; - }); - } - - public static IObservable SelectTask(this IObservable source, Func task) - { - return source - .SelectMany(async _ => - { - await task().ConfigureAwait(false); - return System.Reactive.Unit.Default; - }); - } - - public static IObservable SelectTask(this IObservable source, Func> task) - { - return source - .SelectMany(_ => task()); - } - - public static IObservable SelectTask(this IObservable source, Func> task) - { - return source - .SelectMany(x => task(x)); - } - - public static IObservable DoTask(this IObservable source, Func task) - { - return source - .SelectMany(async (x) => - { - await task(x).ConfigureAwait(false); - return x; - }); - } - - public static IObservable WhereCastable(this IObservable source) - where R : class - where T : class - { - return source - .Select(x => x as R) - .NotNull(); - } - /// These snippets were provided by RolandPheasant (author of DynamicData) /// They'll be going into the official library at some point, but are here for now. #region Dynamic Data EnsureUniqueChanges diff --git a/Wabbajack/Settings.cs b/Wabbajack/Settings.cs new file mode 100644 index 00000000..34b2b1d7 --- /dev/null +++ b/Wabbajack/Settings.cs @@ -0,0 +1,70 @@ +using Newtonsoft.Json; +using ReactiveUI.Fody.Helpers; +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Reactive; +using System.Reactive.Linq; +using System.Reactive.Subjects; +using System.Reflection; +using System.Text; +using System.Threading.Tasks; + +namespace Wabbajack +{ + [JsonObject(MemberSerialization.OptOut)] + public class MainSettings + { + private static string Filename = "settings.json"; + + public double PosX { get; set; } + public double PosY { get; set; } + public double Height { get; set; } + public double Width { get; set; } + public string LastInstalledListLocation { get; set; } + public Dictionary InstallationSettings { get; } = new Dictionary(); + public string LastCompiledProfileLocation { get; set; } + public Dictionary CompilationSettings { get; } = new Dictionary(); + + [JsonIgnoreAttribute] + private Subject _saveSignal = new Subject(); + public IObservable SaveSignal => _saveSignal; + + public static MainSettings LoadSettings() + { + if (!File.Exists(Filename)) return new MainSettings(); + return JsonConvert.DeserializeObject(File.ReadAllText(Filename)); + } + + public static void SaveSettings(MainSettings settings) + { + settings._saveSignal.OnNext(Unit.Default); + + // Might add this if people are putting save work on other threads or other + // things that delay the operation. + //settings._saveSignal.OnCompleted(); + //await settings._saveSignal; + + File.WriteAllText(Filename, JsonConvert.SerializeObject(settings, Formatting.Indented)); + } + } + + public class InstallationSettings + { + public string InstallationLocation { get; set; } + public string DownloadLocation { get; set; } + } + + public class CompilationSettings + { + public string ModListName { get; set; } + public string Author { get; set; } + public string Description { get; set; } + public string Website { get; set; } + public string Readme { get; set; } + public string SplashScreen { get; set; } + public string Location { get; set; } + public string DownloadLocation { get; set; } + } +} diff --git a/Wabbajack/Themes/Styles.xaml b/Wabbajack/Themes/Styles.xaml index 14e242b2..689fc4ca 100644 --- a/Wabbajack/Themes/Styles.xaml +++ b/Wabbajack/Themes/Styles.xaml @@ -3,6 +3,7 @@ xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:Wabbajack" + xmlns:mahapps="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro" xmlns:darkBlendTheme="clr-namespace:DarkBlendTheme" mc:Ignorable="d"> @@ -838,49 +839,120 @@ -