コード例 #1
0
ファイル: ModManager.cs プロジェクト: ManicJamie/ModInstaller
        private void OnEnableButtonClick(object sender, EventArgs e)
        {
            var      button  = (Button)sender;
            ModField entry   = _modEntries.First(f => f.EnableButton == button);
            Mod      mod     = _modsList.First(m => m.Name == entry.Name.Text);
            string   modname = mod.Name;

            if (entry.IsEnabled)
            {
                if (_modsList.Any(m => m.Name == modname))
                {
                    foreach (string s in _modsList.First(m => m.Name == modname)
                             .Files.Keys
                             .Where(f => Path.GetExtension(f) == ".dll"))
                    {
                        if (!File.Exists($"{Properties.Settings.Default.modFolder}/{s}"))
                        {
                            continue;
                        }
                        if (File.Exists($"{Properties.Settings.Default.modFolder}/Disabled/{s}"))
                        {
                            File.Delete($"{Properties.Settings.Default.modFolder}/Disabled/{s}");
                        }

                        File.Move
                        (
                            $"{Properties.Settings.Default.modFolder}/{s}",
                            $"{Properties.Settings.Default.modFolder}/Disabled/{s}"
                        );
                    }
                }
                else
                {
                    if (!File.Exists($"{Properties.Settings.Default.modFolder}/{modname}"))
                    {
                        return;
                    }
                    if (File.Exists($"{Properties.Settings.Default.modFolder}/Disabled/{modname}"))
                    {
                        File.Delete($"{Properties.Settings.Default.modFolder}/Disabled/{modname}");
                    }

                    File.Move
                    (
                        $"{Properties.Settings.Default.modFolder}/{modname}",
                        $"{Properties.Settings.Default.modFolder}/Disabled/{modname}"
                    );
                }
            }
            else
            {
                if (_modsList.Any(m => m.Name == modname))
                {
                    foreach (string s in _modsList.First(m => m.Name == modname)
                             .Files.Keys
                             .Where(f => Path.GetExtension(f) == ".dll"))
                    {
                        if (!File.Exists($"{Properties.Settings.Default.modFolder}/Disabled/{s}"))
                        {
                            continue;
                        }
                        if (File.Exists($"{Properties.Settings.Default.modFolder}/{s}"))
                        {
                            File.Delete($"{Properties.Settings.Default.modFolder}/{s}");
                        }

                        File.Move
                        (
                            $"{Properties.Settings.Default.modFolder}/Disabled/{s}",
                            $"{Properties.Settings.Default.modFolder}/{s}"
                        );
                    }
                }
                else
                {
                    if (!File.Exists($"{Properties.Settings.Default.modFolder}/Disabled/{modname}"))
                    {
                        return;
                    }
                    if (File.Exists($"{Properties.Settings.Default.modFolder}/{modname}"))
                    {
                        File.Delete($"{Properties.Settings.Default.modFolder}/{modname}");
                    }

                    File.Move
                    (
                        $"{Properties.Settings.Default.modFolder}/Disabled/{modname}",
                        $"{Properties.Settings.Default.modFolder}/{modname}"
                    );
                }
            }

            _modEntries.First(f => f.EnableButton == button).IsEnabled =
                !_modEntries.First(f => f.EnableButton == button).IsEnabled;
            _modEntries.First(f => f.EnableButton == button).EnableButton.Text = entry.IsEnabled ? "Disable" : "Enable";
        }
コード例 #2
0
ファイル: ModManager.cs プロジェクト: ManicJamie/ModInstaller
        private void GetInstalledFiles()
        {
            var modsFolder = new DirectoryInfo(Properties.Settings.Default.modFolder);

            FileInfo[] modsFiles = modsFolder.GetFiles("*.dll");

            if (!Directory.Exists($"{Properties.Settings.Default.modFolder}/Disabled"))
            {
                Directory.CreateDirectory($"{Properties.Settings.Default.modFolder}/Disabled");
            }

            var disabledFolder = new DirectoryInfo($"{Properties.Settings.Default.modFolder}/Disabled");

            FileInfo[] disabledFiles = disabledFolder.GetFiles("*.dll");

            foreach (FileInfo modsFile in modsFiles)
            {
                Mod mod;

                var entry = new ModField
                {
                    Name          = new Label(),
                    EnableButton  = new Button(),
                    InstallButton = new Button(),
                    ReadmeButton  = new Button(),
                    IsEnabled     = true,
                    IsInstalled   = true
                };

                panel.Controls.Add(entry.Name);
                panel.Controls.Add(entry.EnableButton);
                panel.Controls.Add(entry.InstallButton);
                panel.Controls.Add(entry.ReadmeButton);

                bool isGDriveMod = _modsList.Any(m => m.Files.Keys.Contains(Path.GetFileName(modsFile.Name)));

                if (isGDriveMod)
                {
                    mod = _modsList.First(m => m.Files.Keys.Contains(Path.GetFileName(modsFile.Name)));
                }
                else
                {
                    mod = new Mod
                    {
                        Name  = Path.GetFileNameWithoutExtension(modsFile.Name),
                        Files = new Dictionary <string, string>
                        {
                            [Path.GetFileName(modsFile.Name)] = GetSHA1(modsFile.FullName)
                        },
                        Link         = "",
                        Dependencies = new List <string>(),
                        Optional     = new List <string>()
                    };
                }

                if (string.IsNullOrEmpty(mod.Name) || _allMods.Contains(mod.Name))
                {
                    continue;
                }

                entry.Name.Text = mod.Name;
                _modEntries.Add(entry);
                _modsList.Add(mod);
                _allMods.Add(mod.Name);
                _installedMods.Add(mod.Name);
            }

            foreach (FileInfo file in disabledFiles)
            {
                Mod mod;

                var entry = new ModField
                {
                    Name          = new Label(),
                    EnableButton  = new Button(),
                    InstallButton = new Button(),
                    ReadmeButton  = new Button(),
                    IsEnabled     = false,
                    IsInstalled   = true
                };

                panel.Controls.Add(entry.Name);
                panel.Controls.Add(entry.EnableButton);
                panel.Controls.Add(entry.InstallButton);
                panel.Controls.Add(entry.ReadmeButton);

                bool isGDriveMod = _modsList.Any(m => m.Files.Keys.Contains(Path.GetFileName(file.Name)));

                if (isGDriveMod)
                {
                    mod = _modsList.First(m => m.Files.Keys.Contains(Path.GetFileName(file.Name)));
                }
                else
                {
                    mod = new Mod
                    {
                        Name  = Path.GetFileNameWithoutExtension(file.Name),
                        Files = new Dictionary <string, string> {
                            [Path.GetFileName(file.Name)] = GetSHA1(file.FullName)
                        },
                        Link         = "",
                        Dependencies = new List <string>(),
                        Optional     = new List <string>()
                    };
                }

                if (string.IsNullOrEmpty(mod.Name) || _allMods.Contains(mod.Name))
                {
                    continue;
                }

                entry.Name.Text = mod.Name;

                _modsList.Add(mod);
                _modEntries.Add(entry);
                _allMods.Add(mod.Name);
                _installedMods.Add(mod.Name);
            }

            foreach ((FileInfo file, bool enabled) in modsFiles.Select(x => (x, true)).Union(disabledFiles.Select(x => (x, false))))
            {
                // High-key hate having to do it again but i cba refactoring the entire method
                // If it's in the modlinks
                Mod?mod = _modsList.Cast <Mod?>().FirstOrDefault(x => x is Mod m && m.Files.Keys.Contains(Path.GetFileName(file.Name)));

                if (!(mod is Mod modlinksMod))
                {
                    continue;
                }

                if (!CheckModUpdated(file.FullName, modlinksMod, enabled))
                {
                    InstallDependencies(modlinksMod);
                }
            }
        }
コード例 #3
0
ファイル: ModManager.cs プロジェクト: ManicJamie/ModInstaller
        private void OnInstallButtonClick(object sender, EventArgs e)
        {
            var      button  = (Button)sender;
            ModField entry   = _modEntries.First(f => f.InstallButton == button);
            Mod      mod     = _modsList.First(m => m.Name == entry.Name.Text);
            string   modname = mod.Name;

            string readmeModPathNoExtension = $"{Properties.Settings.Default.installFolder}/README({modname})";
            string readmeModPathTxt         = $"{readmeModPathNoExtension}.txt";
            string readmeModPathMd          = $"{readmeModPathNoExtension}.md";

            if (entry.IsInstalled)
            {
                DialogResult result = MessageBox.Show
                                      (
                    $"Do you want to remove {modname} from your computer?",
                    "Confirm removal",
                    MessageBoxButtons.YesNo
                                      );

                if (result != DialogResult.Yes)
                {
                    return;
                }

                foreach (string s in mod.Files.Keys)
                {
                    if (File.Exists($"{Properties.Settings.Default.modFolder}/{s}"))
                    {
                        File.Delete($"{Properties.Settings.Default.modFolder}/{s}");
                    }
                }

                foreach (string directory in Directory.EnumerateDirectories(Properties.Settings.Default.modFolder))
                {
                    if (!Directory.EnumerateFileSystemEntries(directory).Any() && directory != "Disabled")
                    {
                        Directory.Delete(directory);
                    }
                }

                if (File.Exists(readmeModPathTxt))
                {
                    File.Delete(readmeModPathTxt);
                }
                else if (File.Exists(readmeModPathMd))
                {
                    File.Delete(readmeModPathMd);
                }

                MessageBox.Show($"{modname} successfully uninstalled!");
                _installedMods.Remove(modname);
            }
            else
            {
                if (_installedMods.Contains(modname))
                {
                    return;
                }

                DialogResult result = MessageBox.Show
                                      (
                    $"Do you want to install {modname}?",
                    "Confirm installation",
                    MessageBoxButtons.YesNo
                                      );

                if (result != DialogResult.Yes)
                {
                    return;
                }

                InstallDependencies(mod);

                if (mod.Optional.Any())
                {
                    // ReSharper disable once ForeachCanBePartlyConvertedToQueryUsingAnotherGetEnumerator
                    foreach (string optional in mod.Optional)
                    {
                        if (_installedMods.Contains(optional))
                        {
                            continue;
                        }

                        DialogResult depInstall = MessageBox.Show
                                                  (
                            $"The mod author suggests installing {optional} together with this mod.\nDo you want to install {optional}?",
                            "Confirm installation",
                            MessageBoxButtons.YesNo
                                                  );

                        if (depInstall != DialogResult.Yes)
                        {
                            continue;
                        }

                        Install(optional, false, true);

                        MessageBox.Show($"{optional} successfully installed!");
                    }
                }

                Install(modname, false, true);
            }

            entry.IsInstalled          = !entry.IsInstalled;
            entry.InstallButton.Text   = entry.IsInstalled ? "Uninstall" : "Install";
            entry.IsEnabled            = entry.IsInstalled;
            entry.EnableButton.Enabled = entry.IsInstalled;
            entry.EnableButton.Text    = entry.IsInstalled ? "Disable" : "Enable";
            entry.ReadmeButton.Enabled = entry.IsInstalled;
        }