Ejemplo n.º 1
0
        /// <summary>
        /// Try to load the registry of an instance
        /// </summary>
        /// <param name="ksp">Game instance</param>
        /// <param name="render">Function that shows a loading message</param>
        /// <returns>
        /// True if successfully loaded, false if it's locked or the registry was corrupted, etc.
        /// </returns>
        public static bool TryGetInstance(KSP ksp, Action render)
        {
            bool retry;

            do
            {
                try {
                    retry = false;
                    // Show loading message
                    render();
                    // Try to get the lock; this will throw if another instance is in there
                    RegistryManager.Instance(ksp);
                } catch (RegistryInUseKraken k) {
                    ConsoleMessageDialog md = new ConsoleMessageDialog(
                        $"Lock file with live process ID found at {k.lockfilePath}\n\n"
                        + "This means that another instance of CKAN probably is accessing this instance."
                        + " You can delete the file to continue, but data corruption is very likely.\n\n"
                        + "Do you want to delete this lock file to force access?",
                        new List <string>()
                    {
                        "Cancel", "Force"
                    }
                        );
                    if (md.Run() == 1)
                    {
                        // Delete it
                        File.Delete(k.lockfilePath);
                        retry = true;
                    }
                    else
                    {
                        // User cancelled, return failure
                        return(false);
                    }
                } catch (NotKSPDirKraken k) {
                    ConsoleMessageDialog errd = new ConsoleMessageDialog(
                        $"Error loading {ksp.GameDir()}:\n{k.Message}",
                        new List <string>()
                    {
                        "OK"
                    }
                        );
                    errd.Run();
                    return(false);
                } catch (Exception e) {
                    ConsoleMessageDialog errd = new ConsoleMessageDialog(
                        $"Error loading {Path.Combine(ksp.CkanDir(), "registry.json")}:\n{e.Message}",
                        new List <string>()
                    {
                        "OK"
                    }
                        );
                    errd.Run();
                    return(false);
                }
            } while (retry);

            // If we got the lock, then return success
            return(true);
        }
Ejemplo n.º 2
0
 private static string FindDownloadsPath(KSP gameInst)
 {
     foreach (string p in downloadPaths) {
         if (!string.IsNullOrEmpty(p) && Directory.Exists(p)) {
             return p;
         }
     }
     return gameInst.GameDir();
 }
Ejemplo n.º 3
0
        /// <summary>
        /// Initialize the Screen
        /// </summary>
        /// <param name="mgr">KSP manager containing the instances</param>
        /// <param name="k">Instance to edit</param>
        public KSPEditScreen(KSPManager mgr, KSP k)
            : base(mgr, k.Name, k.GameDir())
        {
            ksp = k;
            try {
                // If we can't parse the registry, just leave the repo list blank
                registry = RegistryManager.Instance(ksp).registry;
            } catch { }

            // Show the repositories if we can
            if (registry != null)
            {
                // Need to edit a copy of the list so it doesn't save on cancel
                repoEditList = new SortedDictionary <string, Repository>();
                foreach (var kvp in registry.Repositories)
                {
                    repoEditList.Add(kvp.Key, new Repository(
                                         kvp.Value.name,
                                         kvp.Value.uri.ToString(),
                                         kvp.Value.priority
                                         ));
                }

                // Also edit copy of the compatible versions
                compatEditList = new List <KspVersion>(ksp.GetCompatibleVersions());

                // I'm not a huge fan of this layout, but I think it's better than just a label
                AddObject(new ConsoleDoubleFrame(
                              1, repoFrameTop, -1, compatFrameBottom, compatFrameTop,
                              () => $"Mod List Sources",
                              () => $"Additional Compatible Versions",
                              () => ConsoleTheme.Current.LabelFg
                              ));

                repoList = new ConsoleListBox <Repository>(
                    3, repoListTop, -3, repoListBottom,
                    new List <Repository>(repoEditList.Values),
                    new List <ConsoleListBoxColumn <Repository> >()
                {
                    new ConsoleListBoxColumn <Repository>()
                    {
                        Header   = "Index",
                        Renderer = r => r.priority.ToString(),
                        Width    = 7
                    }, new ConsoleListBoxColumn <Repository>()
                    {
                        Header   = "Name",
                        Renderer = r => r.name,
                        Width    = 16
                    }, new ConsoleListBoxColumn <Repository>()
                    {
                        Header   = "URL",
                        Renderer = r => r.uri.ToString(),
                        Width    = 50
                    }
                },
                    1, 0, ListSortDirection.Ascending
                    );
                AddObject(repoList);
                repoList.AddTip("A", "Add");
                repoList.AddBinding(Keys.A, (object sender) => {
                    LaunchSubScreen(new RepoAddScreen(repoEditList));
                    repoList.SetData(new List <Repository>(repoEditList.Values));
                    return(true);
                });
                repoList.AddTip("R", "Remove");
                repoList.AddBinding(Keys.R, (object sender) => {
                    int oldPrio = repoList.Selection.priority;
                    repoEditList.Remove(repoList.Selection.name);
                    // Reshuffle the priorities to fill
                    foreach (Repository r in repoEditList.Values)
                    {
                        if (r.priority > oldPrio)
                        {
                            --r.priority;
                        }
                    }
                    repoList.SetData(new List <Repository>(repoEditList.Values));
                    return(true);
                });
                repoList.AddTip("E", "Edit");
                repoList.AddBinding(Keys.E, (object sender) => {
                    LaunchSubScreen(new RepoEditScreen(repoEditList, repoList.Selection));
                    repoList.SetData(new List <Repository>(repoEditList.Values));
                    return(true);
                });
                repoList.AddTip("-", "Up");
                repoList.AddBinding(Keys.Minus, (object sender) => {
                    if (repoList.Selection.priority > 0)
                    {
                        Repository prev = SortedDictFind(repoEditList,
                                                         r => r.priority == repoList.Selection.priority - 1);
                        if (prev != null)
                        {
                            ++prev.priority;
                        }
                        --repoList.Selection.priority;
                        repoList.SetData(new List <Repository>(repoEditList.Values));
                    }
                    return(true);
                });
                repoList.AddTip("+", "Down");
                repoList.AddBinding(Keys.Plus, (object sender) => {
                    Repository next = SortedDictFind(repoEditList,
                                                     r => r.priority == repoList.Selection.priority + 1);
                    if (next != null)
                    {
                        --next.priority;
                    }
                    ++repoList.Selection.priority;
                    repoList.SetData(new List <Repository>(repoEditList.Values));
                    return(true);
                });

                compatList = new ConsoleListBox <KspVersion>(
                    3, compatListTop, -3, compatListBottom,
                    compatEditList,
                    new List <ConsoleListBoxColumn <KspVersion> >()
                {
                    new ConsoleListBoxColumn <KspVersion>()
                    {
                        Header   = "Version",
                        Width    = 10,
                        Renderer = v => v.ToString(),
                        Comparer = (a, b) => a.CompareTo(b)
                    }
                },
                    0, 0, ListSortDirection.Descending
                    );
                AddObject(compatList);

                compatList.AddTip("A", "Add");
                compatList.AddBinding(Keys.A, (object sender) => {
                    CompatibleVersionDialog vd = new CompatibleVersionDialog();
                    KspVersion newVersion      = vd.Run();
                    DrawBackground();
                    if (newVersion != null && !compatEditList.Contains(newVersion))
                    {
                        compatEditList.Add(newVersion);
                        compatList.SetData(compatEditList);
                    }
                    return(true);
                });
                compatList.AddTip("R", "Remove", () => compatList.Selection != null);
                compatList.AddBinding(Keys.R, (object sender) => {
                    compatEditList.Remove(compatList.Selection);
                    compatList.SetData(compatEditList);
                    return(true);
                });
            }
            else
            {
                // Notify the user that the registry doesn't parse
                AddObject(new ConsoleLabel(
                              1, repoFrameTop, -1,
                              () => $"Failed to extract mod list sources from {ksp.Name}."
                              ));
            }
        }
Ejemplo n.º 4
0
 private void SetInstanceKeysTo(int instanceIndex, string name, KSP ksp)
 {
     SetRegistryValue(@"KSPInstanceName_" + instanceIndex, name);
     SetRegistryValue(@"KSPInstancePath_" + instanceIndex, ksp.GameDir());
 }