/// <summary> /// Is the provided profile different to the current enabled packages? This is generally NOT a safe check, only used right after loading a save and comparing /// it to the current packages. /// </summary> /// <param name="profile"></param> /// <returns><see langword="true"/> if different, otherwise <see langword="false"/></returns> internal static bool IsProfileDifferentToEnabledPackages(MefinoProfile profile) { if (profile == null) { return(true); } return(profile.packages.Count != LocalPackageManager.s_enabledPackages.Count || profile.packages.Any(it => !LocalPackageManager.s_enabledPackages.ContainsKey(it))); }
/// <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); }
/// <summary> /// True if there are manual changes and we overwrote the profile, otherwise false. /// If true, it will also set that as the active profile.</summary> private static bool DoManualChangesOverwrite(MefinoProfile active) { if (IsProfileDifferentToEnabledPackages(active)) { List <string> uninstalled = new List <string>(); foreach (var pkg in active.packages) { if (LocalPackageManager.TryGetInstalledPackage(pkg) == null) { uninstalled.Add(pkg); } } if (uninstalled.Any()) { var msg = ""; foreach (var entry in uninstalled) { msg += $"\n{entry}"; } var msgResult = MessageBox.Show($"The following packages from the profile '{active.name}' are no longer installed:" + $"\n{msg}" + $"\n\n" + $"Do you want to reinstall them?", "Missing packages!", MessageBoxButtons.YesNo); if (msgResult == DialogResult.Yes) { foreach (var entry in uninstalled) { LocalPackageManager.TryInstallWebPackage(entry, true); } } } active.packages.Clear(); active.packages.AddRange(LocalPackageManager.s_enabledPackages.Keys); SetChangesSinceSave(true); SetActiveProfile(active, true); return(true); } return(false); }
public static MefinoProfile FromJson(string jsonString) { MefinoProfile ret = null; try { var json = JsonReader.Parse(jsonString); ret = new MefinoProfile { name = json[nameof(name)].AsString }; if (json[nameof(packages)].AsJsonArray is JsonArray array) { ret.packages.AddRange(array.Select(it => it.AsString)); } // verify all GUIDs. for (int i = ret.packages.Count - 1; i >= 0; i--) { var guid = ret.packages[i]; if (!WebManifestManager.s_webManifests.ContainsKey(guid) && LocalPackageManager.TryGetInstalledPackage(guid) == null) { // GUID in package list does not exist, locally or online! ret.packages.RemoveAt(i); } } return(ret); } catch (Exception ex) { Console.WriteLine("Exception parsing Profile from json!"); Console.WriteLine(ex); return(ret); } }
/// <summary> /// Set the current profile to the provided profile. /// </summary> /// <param name="profile">The profile to activate</param> /// <param name="skipSavePrompt">Skip saving the current profile?</param> /// <returns><see langword="true"/> if successful, otherwise <see langword="false"/></returns> public static bool SetActiveProfile(MefinoProfile profile, bool skipSavePrompt) { if (!skipSavePrompt && !string.IsNullOrEmpty(s_activeProfile)) { SavePrompt(); } try { s_activeProfile = profile.name; profile.EnableProfile(); AppDataManager.SaveConfig(); LauncherPage.SendRebuildPackageList(); return(true); } catch (Exception ex) { Console.WriteLine(ex); return(false); } }