using System.ComponentModel; using System.Windows; using System.Windows.Input; using ClassicAssist.Data; using ClassicAssist.Data.Macros.Commands; using ClassicAssist.Misc; using ClassicAssist.Shared; using ClassicAssist.Shared.Resources; using ClassicAssist.UI.Misc; using ClassicAssist.UO.Gumps; using Newtonsoft.Json.Linq; namespace ClassicAssist.UI.ViewModels { public class OptionsTabViewModel : BaseViewModel, ISettingProvider { private Options _currentOptions; private ICommand _setLanguageOverrideCommand; public Options CurrentOptions { get => _currentOptions; set => SetProperty( ref _currentOptions, value ); } public ICommand SetLanguageOverrideCommand => _setLanguageOverrideCommand ?? ( _setLanguageOverrideCommand = new RelayCommand( SetLanguageOverride ) ); public void Serialize( JObject json ) { if ( CurrentOptions == null ) { return; } JObject options = new JObject(); JObject useOnce = new JObject { ["Persist"] = CurrentOptions.PersistUseOnce }; if ( CurrentOptions.PersistUseOnce ) { JArray useOnceItems = new JArray(); foreach ( int serial in ActionCommands.UseOnceList ) { useOnceItems.Add( serial ); } useOnce.Add( "Items", useOnceItems ); } options.Add( "UseOnce", useOnce ); options.Add( "UseDeathScreenWhilstHidden", CurrentOptions.UseDeathScreenWhilstHidden ); options.Add( "CommandPrefix", CurrentOptions.CommandPrefix ); options.Add( "RangeCheckLastTarget", CurrentOptions.RangeCheckLastTarget ); options.Add( "RangeCheckLastTargetAmount", CurrentOptions.RangeCheckLastTargetAmount ); options.Add( "UseExperimentalFizzleDetection", CurrentOptions.UseExperimentalFizzleDetection ); options.Add( "UseObjectQueue", CurrentOptions.UseObjectQueue ); options.Add( "UseObjectQueueAmount", CurrentOptions.UseObjectQueueAmount ); options.Add( "QueueLastTarget", CurrentOptions.QueueLastTarget ); options.Add( "MaxTargetQueueLength", CurrentOptions.MaxTargetQueueLength ); options.Add( "SmartTargetOption", CurrentOptions.SmartTargetOption.ToString() ); options.Add( "LimitMouseWheelTrigger", CurrentOptions.LimitMouseWheelTrigger ); options.Add( "LimitMouseWheelTriggerMS", CurrentOptions.LimitMouseWheelTriggerMS ); options.Add( "AutoAcceptPartyInvite", CurrentOptions.AutoAcceptPartyInvite ); options.Add( "AutoAcceptPartyOnlyFromFriends", CurrentOptions.AutoAcceptPartyOnlyFromFriends ); options.Add( "PreventTargetingInnocentsInGuardzone", CurrentOptions.PreventTargetingInnocentsInGuardzone ); options.Add( "PreventAttackingInnocentsInGuardzone", CurrentOptions.PreventAttackingInnocentsInGuardzone ); options.Add( "LastTargetMessage", CurrentOptions.LastTargetMessage ); options.Add( "FriendTargetMessage", CurrentOptions.FriendTargetMessage ); options.Add( "EnemyTargetMessage", CurrentOptions.EnemyTargetMessage ); options.Add( "DefaultMacroQuietMode", CurrentOptions.DefaultMacroQuietMode ); options.Add( "GetFriendEnemyUsesIgnoreList", CurrentOptions.GetFriendEnemyUsesIgnoreList ); options.Add( "AbilitiesGump", CurrentOptions.AbilitiesGump ); options.Add( "AbilitiesGumpX", CurrentOptions.AbilitiesGumpX ); options.Add( "AbilitiesGumpY", CurrentOptions.AbilitiesGumpY ); options.Add( "ShowProfileNameWindowTitle", CurrentOptions.ShowProfileNameWindowTitle ); options.Add( "SortMacrosAlphabetical", CurrentOptions.SortMacrosAlphabetical ); options.Add( "ShowResurrectionWaypoints", CurrentOptions.ShowResurrectionWaypoints ); options.Add( "RehueFriends", CurrentOptions.RehueFriends ); options.Add( "RehueFriendsHue", CurrentOptions.RehueFriendsHue ); options.Add( "CheckHandsPotions", CurrentOptions.CheckHandsPotions ); options.Add( "MacrosGump", CurrentOptions.MacrosGump ); options.Add( "MacrosGumpX", CurrentOptions.MacrosGumpX ); options.Add( "MacrosGumpY", CurrentOptions.MacrosGumpY ); json?.Add( "Options", options ); } public void Deserialize( JObject json, Options options ) { CurrentOptions = options; CurrentOptions.PropertyChanged += OnOptionsChanged; ActionCommands.UseOnceList.Clear(); JToken config = json?["Options"]; if ( config?["UseOnce"] != null ) { CurrentOptions.PersistUseOnce = config["UseOnce"]["Persist"]?.ToObject() ?? false; if ( CurrentOptions.PersistUseOnce ) { foreach ( JToken token in config["UseOnce"]["Items"] ) { ActionCommands.UseOnceList.Add( token.ToObject() ); } } } CurrentOptions.UseDeathScreenWhilstHidden = config?["UseDeathScreenWhilstHidden"]?.ToObject() ?? false; CurrentOptions.CommandPrefix = config?["CommandPrefix"]?.ToObject() ?? '='; CurrentOptions.RangeCheckLastTarget = config?["RangeCheckLastTarget"]?.ToObject() ?? false; CurrentOptions.RangeCheckLastTargetAmount = config?["RangeCheckLastTargetAmount"]?.ToObject() ?? 11; CurrentOptions.UseExperimentalFizzleDetection = config?["UseExperimentalFizzleDetection"]?.ToObject() ?? false; CurrentOptions.UseObjectQueue = config?["UseObjectQueue"]?.ToObject() ?? false; CurrentOptions.UseObjectQueueAmount = config?["UseObjectQueueAmount"]?.ToObject() ?? 5; CurrentOptions.QueueLastTarget = config?["QueueLastTarget"]?.ToObject() ?? false; CurrentOptions.MaxTargetQueueLength = config?["MaxTargetQueueLength"]?.ToObject() ?? 1; CurrentOptions.SmartTargetOption = config?["SmartTargetOption"]?.ToObject() ?? SmartTargetOption.None; CurrentOptions.LimitMouseWheelTrigger = config?["LimitMouseWheelTrigger"]?.ToObject() ?? false; CurrentOptions.LimitMouseWheelTriggerMS = config?["LimitMouseWheelTriggerMS"]?.ToObject() ?? 25; CurrentOptions.AutoAcceptPartyInvite = config?["AutoAcceptPartyInvite"]?.ToObject() ?? false; CurrentOptions.AutoAcceptPartyOnlyFromFriends = config?["AutoAcceptPartyOnlyFromFriends"]?.ToObject() ?? false; CurrentOptions.PreventTargetingInnocentsInGuardzone = config?["PreventTargetingInnocentsInGuardzone"]?.ToObject() ?? false; CurrentOptions.PreventAttackingInnocentsInGuardzone = config?["PreventAttackingInnocentsInGuardzone"]?.ToObject() ?? false; CurrentOptions.LastTargetMessage = config?["LastTargetMessage"]?.ToObject() ?? "[Last Target]"; CurrentOptions.FriendTargetMessage = config?["FriendTargetMessage"]?.ToObject() ?? "[Friend]"; CurrentOptions.EnemyTargetMessage = config?["EnemyTargetMessage"]?.ToObject() ?? "[Enemy]"; CurrentOptions.DefaultMacroQuietMode = config?["DefaultMacroQuietMode"]?.ToObject() ?? false; CurrentOptions.GetFriendEnemyUsesIgnoreList = config?["GetFriendEnemyUsesIgnoreList"]?.ToObject() ?? false; CurrentOptions.AbilitiesGump = config?["AbilitiesGump"]?.ToObject() ?? true; CurrentOptions.AbilitiesGumpX = config?["AbilitiesGumpX"]?.ToObject() ?? 100; CurrentOptions.AbilitiesGumpY = config?["AbilitiesGumpY"]?.ToObject() ?? 100; CurrentOptions.ShowProfileNameWindowTitle = config?["ShowProfileNameWindowTitle"]?.ToObject() ?? false; CurrentOptions.SortMacrosAlphabetical = config?["SortMacrosAlphabetical"]?.ToObject() ?? false; CurrentOptions.ShowResurrectionWaypoints = config?["ShowResurrectionWaypoints"]?.ToObject() ?? true; CurrentOptions.RehueFriends = config?["RehueFriends"]?.ToObject() ?? false; CurrentOptions.RehueFriendsHue = config?["RehueFriendsHue"]?.ToObject() ?? 35; CurrentOptions.CheckHandsPotions = config?["CheckHandsPotions"]?.ToObject() ?? false; CurrentOptions.MacrosGump = config?["MacrosGump"]?.ToObject() ?? true; CurrentOptions.MacrosGumpX = config?["MacrosGumpX"]?.ToObject() ?? 100; CurrentOptions.MacrosGumpY = config?["MacrosGumpY"]?.ToObject() ?? 100; if ( CurrentOptions.AbilitiesGumpX < 0 ) { CurrentOptions.AbilitiesGumpX = 100; } if ( CurrentOptions.AbilitiesGumpY < 0 ) { CurrentOptions.AbilitiesGumpY = 100; } if ( CurrentOptions.MacrosGump ) { MacrosGump.Initialize(); } } // Replay CurrentOptions changes onto Options.CurrentOptions // TODO: Fix Options private void OnOptionsChanged( object sender, PropertyChangedEventArgs args ) { if ( args.PropertyName == "Name" ) { return; } object val = CurrentOptions.GetType().GetProperty( args.PropertyName )?.GetValue( CurrentOptions ); if ( val == null ) { return; } object oldVal = Options.CurrentOptions.GetType().GetProperty( args.PropertyName ) ?.GetValue( Options.CurrentOptions ); if ( !val.Equals( oldVal ) ) { Options.CurrentOptions.GetType().GetProperty( args.PropertyName ) ?.SetValue( Options.CurrentOptions, val ); } } private static async void SetLanguageOverride( object obj ) { if ( !( obj is Language language ) ) { return; } AssistantOptions.LanguageOverride = language; await Engine.MessageBoxProvider.Show( Strings.Restart_game_for_changes_to_take_effect___, Strings.Restart_game_for_changes_to_take_effect___ ); } } }