Exemplo n.º 1
0
        /// <summary>
        /// Load saved profiles off disk, and optionally apply the loaded profile.
        /// </summary>
        /// <param name="overwriteWithManualChanges">Should any manual IO changes from the user take priority over the last active save?</param>
        /// <param name="dontApplyLoadedProfile">Set to true if you are going to apply a profile yourself right after this, so no reason to reload the last saved on cancel.</param>
        /// <returns><see langword="true"/> if a profile was loaded, otherwise <see langword="false"/></returns>
        public static bool LoadProfiles(bool overwriteWithManualChanges = true, bool dontApplyLoadedProfile = false)
        {
            AllProfiles.Clear();
            SetChangesSinceSave(false);

            if (!File.Exists(PROFILE_SAVE_PATH))
            {
                return(false);
            }

            try
            {
                var json = JsonReader.ParseFile(PROFILE_SAVE_PATH);

                if (json[nameof(AllProfiles)].AsJsonArray is JsonArray profileArray)
                {
                    foreach (var entry in profileArray)
                    {
                        if (MefinoProfile.FromJson(entry.ToString()) is MefinoProfile profile)
                        {
                            if (AllProfiles.ContainsKey(profile.name))
                            {
                                continue;
                            }

                            AllProfiles.Add(profile.name, profile);
                        }
                    }
                }

                if (dontApplyLoadedProfile)
                {
                    return(true);
                }

                if (!string.IsNullOrEmpty(s_activeProfile) &&
                    AllProfiles.TryGetValue(s_activeProfile, out MefinoProfile active))
                {
                    if (overwriteWithManualChanges && DoManualChangesOverwrite(active))
                    {
                        //Console.WriteLine("overwrote with manual changes");
                        return(true);
                    }

                    return(SetActiveProfile(active, true));
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception loading Mefino Profiles!");
                Console.WriteLine(ex);
            }

            return(false);
        }