Пример #1
0
        public bool LoadOptions()
        {
            if (!HasSavedOptions)
            {
                return(false);
            }

            try
            {
                Options = JsonUtility.FromJson <OptionsData>(File.ReadAllText(_optionsPath));
                if (Options != null)
                {
                    Debug.LogFormat("OptionsManager:LoadOptions - Options loaded : {0}", JsonUtility.ToJson(Options));
                    OptionsLoaded?.Invoke();
                }
                else
                {
                    Debug.Log("OptionsManager:LoadOptions - Failed to deserialize options data");
                }
            }
            catch (IOException e)
            {
                Options = null;
                Debug.LogErrorFormat("OptionsManager:LoadOptions - {0}", e.StackTrace);
            }

            return(Options != null);
        }
Пример #2
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);
        }