示例#1
0
        public static void Load()
        {
            Engine.PlayerInitializedEvent += PlayerInitialized;

            string configPath = Path.Combine(Engine.StartupPath ?? Environment.CurrentDirectory, "Assistant.json");

            if (!File.Exists(configPath))
            {
                LastProfile = Options.DEFAULT_SETTINGS_FILENAME;
                UserId      = Guid.NewGuid().ToString();
                SessionId   = Guid.NewGuid().ToString();
                return;
            }

            JObject json = JObject.Parse(File.ReadAllText(configPath));

            LanguageOverride            = json?["LanguageOverride"]?.ToObject <Language>() ?? Language.Default;
            LastProfile                 = json?["LastProfile"]?.ToObject <string>() ?? Options.DEFAULT_SETTINGS_FILENAME;
            UpdateGumpVersion           = json?["UpdateGumpVersion"]?.ToObject <Version>() ?? new Version();
            AutoBackupProfiles          = json?["AutoBackupProfiles"]?.ToObject <bool>() ?? true;
            AutoBackupProfilesDays      = json?["AutoBackupProfilesDays"]?.ToObject <int>() ?? 7;
            AutoBackupProfilesDirectory =
                json?["AutoBackupProfilesDirectory"]?.ToObject <string>() ?? DEFAULT_BACKUP_PATH;
            AutoBackupProfilesLast = json?["AutoBackupProfilesLast"]?.ToObject <DateTime>() ?? default;
            SavePasswords          = json?["SavePasswords"]?.ToObject <bool>() ?? false;
            SavePasswordsOnlyBlank = json?["SavePasswordsOnlyBlank"]?.ToObject <bool>() ?? false;
            UserId       = json?["UserId"]?.ToObject <string>() ?? Guid.NewGuid().ToString();
            WindowWidth  = json?["WindowWidth"]?.ToObject <int>() ?? 625;
            WindowHeight = json?["WindowHeight"]?.ToObject <int>() ?? 500;
            Assemblies   = json?["Assemblies"]?.ToObject <string[]>() ?? new string[0];
            SessionId    = Guid.NewGuid().ToString();

            if (json?["Profiles"] != null)
            {
                foreach (JToken token in json["Profiles"])
                {
                    _linkedProfiles.Add(token["Serial"].ToObject <int>(), token["Profile"].ToObject <string>());
                }
            }

            if (json?["SavedPasswords"] != null)
            {
                foreach (JToken token in json["SavedPasswords"])
                {
                    SavedPasswords.Add(token["Username"].ToObject <string>(),
                                       Crypter.Decrypt(token["Password"].ToObject <string>()));
                }

                OnPasswordsChanged();
            }

            SetLanguage(LanguageOverride);

            if (DateTime.Now - AutoBackupProfilesLast >= TimeSpan.FromDays(AutoBackupProfilesDays))
            {
                BackupProfiles();
            }

            foreach (string assembly in Assemblies)
            {
                try
                {
                    Assembly asm = Assembly.LoadFile(assembly);

                    IEnumerable <MethodInfo> initializeMethods = asm.GetTypes()
                                                                 .Where(e => e.IsClass && e.IsPublic && e.GetMethod("Initialize",
                                                                                                                    BindingFlags.Public | BindingFlags.Static, null, Type.EmptyTypes, null) !=
                                                                        null).Select(e => e.GetMethod("Initialize",
                                                                                                      BindingFlags.Public | BindingFlags.Static, null, Type.EmptyTypes, null));

                    foreach (MethodInfo initializeMethod in initializeMethods)
                    {
                        initializeMethod?.Invoke(null, null);
                    }
                }
                catch (Exception)
                {
                    // ignored
                }
            }

            OptionsLoaded?.Invoke(null, EventArgs.Empty);
        }
        public static void Save()
        {
            if (BackupOptions != null && BackupOptions.Enabled &&
                DateTime.Now - BackupOptions.LastBackup >= TimeSpan.FromDays(BackupOptions.Days))
            {
                BackupProfiles();
            }

            JObject json = new JObject
            {
                { "LanguageOverride", LanguageOverride.ToString() },
                { "LastProfile", LastProfile },
                { "UpdateGumpVersion", UpdateGumpVersion?.ToString() ?? "0.0.0.0" },
                { "SavePasswords", SavePasswords },
                { "SavePasswordsOnlyBlank", SavePasswordsOnlyBlank },
                { "UserId", UserId },
                { "UseCUOClilocLanguage", UseCUOClilocLanguage },
#if !DEVELOP
                { "WindowWidth", WindowWidth },
                { "WindowHeight", WindowHeight },
#endif
            };

            BackupOptions?.Serialize(json);

            JArray linkedProfilesArray = new JArray();

            foreach (JObject linkedObj in _linkedProfiles.Select(profile =>
                                                                 new JObject {
                { "Serial", profile.Key }, { "Profile", profile.Value }
            }))
            {
                linkedProfilesArray.Add(linkedObj);
            }

            json.Add("Profiles", linkedProfilesArray);

            JArray savedPasswordsArray = new JArray();

            foreach (KeyValuePair <string, string> kvp in SavedPasswords)
            {
                savedPasswordsArray.Add(new JObject
                {
                    { "Username", kvp.Key }, { "Password", Crypter.Encrypt(kvp.Value) }
                });
            }

            json.Add("SavedPasswords", savedPasswordsArray);

            JArray assembliesArray = new JArray();

            foreach (string assembly in Assemblies ?? new string[0])
            {
                assembliesArray.Add(assembly);
            }

            json.Add("Assemblies", assembliesArray);

            File.WriteAllText(Path.Combine(Engine.StartupPath ?? Environment.CurrentDirectory, "Assistant.json"),
                              json.ToString(Formatting.Indented));
        }