Example #1
0
        private void ModcontextMenuStrip1_Opening(object sender, System.ComponentModel.CancelEventArgs e)
        {
            installToolStripMenuItem.Visible   = false;
            uninstallToolStripMenuItem.Visible = false;
            updateToolStripMenuItem.Visible    = false;
            revertToolStripMenuItem.Visible    = false;
            wwwToolStripMenuItem1.Visible      = false;

            var modInfo = selectedMod;

            if (!modInfo)
            {
                e.Cancel = true;
                return;
            }

            if (modInfo.Status == ModStatus.Installed)
            {
                uninstallToolStripMenuItem.Visible = true;

                Version inRepository = new Version();
                if (repositories.ContainsKey(selectedGame))
                {
                    var release = repositories[selectedGame].FirstOrDefault(x => x.Id == modInfo.Id);
                    if (release != null && !string.IsNullOrEmpty(release.DownloadUrl) && !string.IsNullOrEmpty(release.Version))
                    {
                        var ver = Utils.ParseVersion(release.Version);
                        if (modInfo.ParsedVersion < ver)
                        {
                            inRepository = ver;
                            updateToolStripMenuItem.Text    = $"Update to {release.Version}";
                            updateToolStripMenuItem.Visible = true;
                        }
                    }
                }

                var newest = modInfo.AvailableVersions.Keys.Max(x => x);
                if (newest != null && newest > modInfo.ParsedVersion && inRepository <= newest)
                {
                    updateToolStripMenuItem.Text    = $"Update to {newest}";
                    updateToolStripMenuItem.Visible = true;
                }
                var previous = modInfo.AvailableVersions.Keys.Where(x => x < modInfo.ParsedVersion).Max(x => x);
                if (previous != null)
                {
                    revertToolStripMenuItem.Text    = $"Revert to {previous}";
                    revertToolStripMenuItem.Visible = true;
                }
            }
            else if (modInfo.Status == ModStatus.NotInstalled)
            {
                installToolStripMenuItem.Visible = true;
            }

            if (!string.IsNullOrEmpty(modInfo.HomePage))
            {
                wwwToolStripMenuItem1.Visible = true;
            }
        }
Example #2
0
        public bool IsValid()
        {
            if (string.IsNullOrEmpty(Id))
            {
                return(false);
            }
            if (string.IsNullOrEmpty(DisplayName))
            {
                DisplayName = Id;
            }
            if (ParsedVersion == null)
            {
                ParsedVersion = Utils.ParseVersion(Version);
            }

            return(true);
        }
        private void LastVersion_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            if (e.Error != null)
            {
                Log.Print(e.Error.Message);
                return;
            }

            if (e.Cancelled || string.IsNullOrEmpty(e.Result))
            {
                return;
            }
            try
            {
                var repository = JsonConvert.DeserializeObject <UnityModManager.Repository>(e.Result);
                if (repository?.Releases == null || repository.Releases.Length == 0)
                {
                    return;
                }

                var release = repository.Releases.FirstOrDefault(x => x.Id == nameof(UnityModManager));
                if (release == null || string.IsNullOrEmpty(release.Version))
                {
                    return;
                }

                var ver = Utils.ParseVersion(release.Version);
                if (version < ver)
                {
                    btnDownloadUpdate.Text = $@"下载V{release.Version}";
                    Log.Print($"有可用的新版本。");
                }
                else
                {
                    Log.Print($"没有可用的新版本。");
                }
            }
            catch (Exception ex)
            {
                Log.Print(ex.Message);
                Log.Print($"检查MOD新版本时出错!");
            }
        }
Example #4
0
        private void LastVersion_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            if (e.Error != null)
            {
                Log.Print(e.Error.Message);
                return;
            }

            if (!e.Cancelled && !string.IsNullOrEmpty(e.Result))
            {
                try
                {
                    var repository = JsonConvert.DeserializeObject <UnityModManager.Repository>(e.Result);
                    if (repository == null || repository.Releases == null || repository.Releases.Length == 0)
                    {
                        return;
                    }

                    var release = repository.Releases.FirstOrDefault(x => x.Id == nameof(UnityModManager));
                    if (release != null && !string.IsNullOrEmpty(release.Version))
                    {
                        var ver = Utils.ParseVersion(release.Version);
                        if (version < ver)
                        {
                            //btnDownloadUpdate.Visible = true;
                            btnDownloadUpdate.Text = $"Download {release.Version}";
                            Log.Print($"Update is available.");
                        }
                        else
                        {
                            Log.Print($"No updates.");
                        }
                    }
                }
                catch (Exception ex)
                {
                    Log.Print(ex.Message);
                    Log.Print($"Error checking update.");
                }
            }
        }
Example #5
0
        private void Init()
        {
            FormBorderStyle = FormBorderStyle.FixedDialog;
            instance        = this;

            Log.Init();

            var modManagerType = typeof(UnityModManager);
            var modManagerDef  = ModuleDefMD.Load(modManagerType.Module);
            var modManager     = modManagerDef.Types.First(x => x.Name == modManagerType.Name);
            var versionString  = modManager.Fields.First(x => x.Name == nameof(UnityModManager.version)).Constant.Value.ToString();

            currentVersion.Text = versionString;
            version             = Utils.ParseVersion(versionString);

            config = Config.Load();
            param  = Param.Load();

            if (config != null && config.GameInfo != null && config.GameInfo.Length > 0)
            {
                config.GameInfo = config.GameInfo.OrderBy(x => x.Name).ToArray();
                gameList.Items.AddRange(config.GameInfo);

                GameInfo selected = null;
                if (!string.IsNullOrEmpty(param.LastSelectedGame))
                {
                    selected = config.GameInfo.FirstOrDefault(x => x.Name == param.LastSelectedGame);
                }
                gameList.SelectedItem = selected ?? config.GameInfo.First();
            }
            else
            {
                InactiveForm();
                Log.Print($"Error parsing file '{Config.filename}'.");
                return;
            }

            CheckLastVersion();
        }
Example #6
0
        private void CheckState()
        {
            if (!IsValid(selectedGame))
            {
                InactiveForm();
                return;
            }

            btnInstall.Text = "安装";

            currentGamePath = "";
            if (!param.ExtractGamePath(selectedGame.Name, out var result) || !Directory.Exists(result))
            {
                result = FindGameFolder(selectedGame.Folder);
                if (string.IsNullOrEmpty(result))
                {
                    InactiveForm();
                    btnOpenFolder.ForeColor          = System.Drawing.Color.FromArgb(192, 0, 0);
                    btnOpenFolder.Text               = "选择游戏安装目录";
                    folderBrowserDialog.SelectedPath = null;
                    Log.Print($"游戏安装目录 '{selectedGame.Folder}' 无法找到.");
                    return;
                }
                Log.Print($"游戏安装目录 被检测为 '{result}'.");
                param.SaveGamePath(selectedGame.Name, result);
            }
            else
            {
                Log.Print($"游戏安装目录 被设置为 '{result}'.");
            }
            currentGamePath                  = result;
            btnOpenFolder.ForeColor          = System.Drawing.Color.Black;
            btnOpenFolder.Text               = new DirectoryInfo(result).Name;
            folderBrowserDialog.SelectedPath = currentGamePath;
            currentManagedPath               = FindManagedFolder(currentGamePath);

            var assemblyPath = Path.Combine(currentManagedPath, selectedGame.AssemblyName);

            ModuleDefMD assembly = null;

            if (File.Exists(assemblyPath))
            {
                try
                {
                    if (this.selectedGame.Name == "The Scroll Of Taiwu")
                    {
                        string de_ass = assemblyPath + ".de.dll";
                        decodeass(assemblyPath, de_ass);
                        assembly = ModuleDefMD.Load(File.ReadAllBytes(de_ass));
                    }
                    else
                    {
                        assembly = ModuleDefMD.Load(File.ReadAllBytes(assemblyPath));
                    }
                }
                catch (Exception e)
                {
                    InactiveForm();
                    Log.Print(e.Message);
                    return;
                }
            }
            else
            {
                InactiveForm();
                Log.Print($"'{selectedGame.AssemblyName}' not found.");
                return;
            }

            tabControl.TabPages[1].Enabled = true;

            var modManagerType        = typeof(UnityModManager);
            var modManagerDefInjected = assembly.Types.FirstOrDefault(x => x.Name == modManagerType.Name);

            if (modManagerDefInjected != null)
            {
                btnInstall.Text    = "更新";
                btnInstall.Enabled = false;
                btnRemove.Enabled  = true;

                var versionString = modManagerDefInjected.Fields.First(x => x.Name == nameof(UnityModManager.version)).Constant.Value.ToString();
                var version2      = Utils.ParseVersion(versionString);
                installedVersion.Text = versionString;
                if (version != version2)
                {
                    btnInstall.Enabled = true;
                }
            }
            else
            {
                installedVersion.Text = "-";
                btnInstall.Enabled    = true;
                btnRemove.Enabled     = false;
            }

            if (this.selectedGame.Name == "The Scroll Of Taiwu")
            {
                string de_ass = assemblyPath + ".de.dll";
                File.Delete(de_ass);
            }
        }
Example #7
0
        private void RefreshForm()
        {
            if (!IsValid(selectedGame))
            {
                InactiveForm();
                return;
            }

            btnInstall.Text    = "Install";
            btnRestore.Enabled = false;

            gamePath = "";
            if (string.IsNullOrEmpty(selectedGameParams.Path) || !Directory.Exists(selectedGameParams.Path))
            {
                var result = FindGameFolder(selectedGame.Folder);
                if (string.IsNullOrEmpty(result))
                {
                    InactiveForm();
                    btnOpenFolder.ForeColor          = System.Drawing.Color.FromArgb(192, 0, 0);
                    btnOpenFolder.Text               = "Select Game Folder";
                    folderBrowserDialog.SelectedPath = null;
                    Log.Print($"Game folder '{selectedGame.Folder}' not found.");
                    return;
                }
                Log.Print($"Game folder detected as '{result}'.");
                selectedGameParams.Path = result;
            }

            Utils.TryParseEntryPoint(selectedGame.EntryPoint, out var assemblyName);

            gamePath = selectedGameParams.Path;
            btnOpenFolder.ForeColor          = System.Drawing.Color.Black;
            btnOpenFolder.Text               = new DirectoryInfo(gamePath).Name;
            folderBrowserDialog.SelectedPath = gamePath;
            managedPath               = FindManagedFolder(gamePath);
            managerPath               = Path.Combine(managedPath, nameof(UnityModManager));
            entryAssemblyPath         = Path.Combine(managedPath, assemblyName);
            injectedEntryAssemblyPath = entryAssemblyPath;
            managerAssemblyPath       = Path.Combine(managerPath, typeof(UnityModManager).Module.Name);
            entryPoint          = selectedGame.EntryPoint;
            injectedEntryPoint  = selectedGame.EntryPoint;
            assemblyDef         = null;
            injectedAssemblyDef = null;
            managerDef          = null;

            gameExePath = !string.IsNullOrEmpty(selectedGame.GameExe) ? Path.Combine(gamePath, selectedGame.GameExe) : string.Empty;

            doorstopPath       = Path.Combine(gamePath, doorstopFilename);
            doorstopConfigPath = Path.Combine(gamePath, doorstopConfigFilename);

            libraryPaths = new string[libraryFiles.Length];
            for (int i = 0; i < libraryFiles.Length; i++)
            {
                libraryPaths[i] = Path.Combine(managerPath, libraryFiles[i]);
            }

            var parent = new DirectoryInfo(Application.StartupPath).Parent;

            for (int i = 0; i < 3; i++)
            {
                if (parent == null)
                {
                    break;
                }

                if (parent.FullName == gamePath)
                {
                    InactiveForm();
                    Log.Print("UMM Installer should not be located in the game folder.");
                    return;
                }
                parent = parent.Parent;
            }

            //machineConfigPath = string.Empty;
            //machineDoc = null;

            //if (!string.IsNullOrEmpty(selectedGame.MachineConfig))
            //{
            //    machineConfigPath = Path.Combine(gamePath, selectedGame.MachineConfig);
            //    try
            //    {
            //        machineDoc = XDocument.Load(machineConfigPath);
            //    }
            //    catch (Exception e)
            //    {
            //        InactiveForm();
            //        Log.Print(e.ToString());
            //        return;
            //    }
            //}

            try
            {
                assemblyDef = ModuleDefMD.Load(File.ReadAllBytes(entryAssemblyPath));
            }
            catch (Exception e)
            {
                InactiveForm();
                Log.Print(e.ToString());
                return;
            }

            var useOldPatchTarget = false;

            GameInfo.filepathInGame = Path.Combine(managerPath, "Config.xml");
            if (File.Exists(GameInfo.filepathInGame))
            {
                var gameConfig = GameInfo.ImportFromGame();
                if (gameConfig == null || !Utils.TryParseEntryPoint(gameConfig.EntryPoint, out assemblyName))
                {
                    InactiveForm();
                    return;
                }
                injectedEntryPoint        = gameConfig.EntryPoint;
                injectedEntryAssemblyPath = Path.Combine(managedPath, assemblyName);
            }
            else if (!string.IsNullOrEmpty(selectedGame.OldPatchTarget))
            {
                if (!Utils.TryParseEntryPoint(selectedGame.OldPatchTarget, out assemblyName))
                {
                    InactiveForm();
                    return;
                }
                useOldPatchTarget         = true;
                injectedEntryPoint        = selectedGame.OldPatchTarget;
                injectedEntryAssemblyPath = Path.Combine(managedPath, assemblyName);
            }

            try
            {
                if (injectedEntryAssemblyPath == entryAssemblyPath)
                {
                    injectedAssemblyDef = assemblyDef;
                }
                else
                {
                    injectedAssemblyDef = ModuleDefMD.Load(File.ReadAllBytes(injectedEntryAssemblyPath));
                }
                if (File.Exists(managerAssemblyPath))
                {
                    managerDef = ModuleDefMD.Load(File.ReadAllBytes(managerAssemblyPath));
                }
            }
            catch (Exception e)
            {
                InactiveForm();
                Log.Print(e.ToString());
                return;
            }

            var disabledMethods    = new List <InstallType>();
            var unavailableMethods = new List <InstallType>();

            var managerType = typeof(UnityModManager);
            var starterType = typeof(Injection.UnityModManagerStarter);

Rescan:
            var v0_12_Installed = injectedAssemblyDef.Types.FirstOrDefault(x => x.Name == managerType.Name);
            var newWayInstalled     = injectedAssemblyDef.Types.FirstOrDefault(x => x.Name == starterType.Name);
            var hasInjectedAssembly = v0_12_Installed != null || newWayInstalled != null;

            if (useOldPatchTarget && !hasInjectedAssembly)
            {
                useOldPatchTarget         = false;
                injectedEntryPoint        = selectedGame.EntryPoint;
                injectedEntryAssemblyPath = entryAssemblyPath;
                injectedAssemblyDef       = assemblyDef;
                goto Rescan;
            }

            //if (machineDoc == null)
            //{
            //    unavailableMethods.Add(InstallType.Config);
            //    selectedGameParams.InstallType = InstallType.Assembly;
            //}
            //else if (hasInjectedAssembly)
            //{
            //    disabledMethods.Add(InstallType.Config);
            //    selectedGameParams.InstallType = InstallType.Assembly;
            //}
            //else if (machineDoc.Descendants("cryptoClass").Any(x => x.HasAttributes && x.FirstAttribute.Name.LocalName == "ummRngWrapper"))
            //{
            //    disabledMethods.Add(InstallType.Assembly);
            //    selectedGameParams.InstallType = InstallType.Config;
            //}

            if (Utils.IsUnixPlatform() || !File.Exists(gameExePath))
            {
                unavailableMethods.Add(InstallType.DoorstopProxy);
                selectedGameParams.InstallType = InstallType.Assembly;
            }
            else if (File.Exists(doorstopPath))
            {
                disabledMethods.Add(InstallType.Assembly);
                selectedGameParams.InstallType = InstallType.DoorstopProxy;
            }

            if (hasInjectedAssembly)
            {
                disabledMethods.Add(InstallType.DoorstopProxy);
                selectedGameParams.InstallType = InstallType.Assembly;
            }

            foreach (var ctrl in installTypeGroup.Controls)
            {
                if (ctrl is RadioButton btn)
                {
                    if (unavailableMethods.Exists(x => x.ToString() == btn.Name))
                    {
                        btn.Visible = false;
                        btn.Enabled = false;
                        continue;
                    }
                    if (disabledMethods.Exists(x => x.ToString() == btn.Name))
                    {
                        btn.Visible = true;
                        btn.Enabled = false;
                        continue;
                    }

                    btn.Visible = true;
                    btn.Enabled = true;
                    btn.Checked = btn.Name == selectedGameParams.InstallType.ToString();
                }
            }

            installTypeGroup.PerformLayout();

            //if (selectedGameParams.InstallType == InstallType.Config)
            //{
            //    btnRestore.Enabled = IsDirty(machineDoc) && File.Exists($"{machineConfigPath}.original_");
            //}

            if (selectedGameParams.InstallType == InstallType.Assembly)
            {
                btnRestore.Enabled = IsDirty(injectedAssemblyDef) && File.Exists($"{injectedEntryAssemblyPath}.original_");
            }

            tabControl.TabPages[1].Enabled = true;

            managerDef = managerDef ?? injectedAssemblyDef;

            var managerInstalled = managerDef.Types.FirstOrDefault(x => x.Name == managerType.Name);

            if (managerInstalled != null && (hasInjectedAssembly || selectedGameParams.InstallType == InstallType.DoorstopProxy))
            {
                btnInstall.Text    = "Update";
                btnInstall.Enabled = false;
                btnRemove.Enabled  = true;

                Version version2;
                if (v0_12_Installed != null)
                {
                    var versionString = managerInstalled.Fields.First(x => x.Name == nameof(UnityModManager.version)).Constant.Value.ToString();
                    version2 = Utils.ParseVersion(versionString);
                }
                else
                {
                    version2 = managerDef.Assembly.Version;
                }

                installedVersion.Text = version2.ToString();
                if (version > version2 && v0_12_Installed == null)
                {
                    btnInstall.Enabled = true;
                }
            }
            else
            {
                installedVersion.Text = "-";
                btnInstall.Enabled    = true;
                btnRemove.Enabled     = false;
            }
        }
Example #8
0
        private void updateToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var modInfo = selectedMod;

            if (modInfo)
            {
                if (repositories.ContainsKey(selectedGame))
                {
                    var release = repositories[selectedGame].FirstOrDefault(x => x.Id == modInfo.Id);
                    if (release != null && !string.IsNullOrEmpty(release.DownloadUrl) && !string.IsNullOrEmpty(release.Version) && modInfo.AvailableVersions.All(x => x.Key < Utils.ParseVersion(release.Version)))
                    {
                        var downloadForm = new DownloadMod(release);
                        var result       = downloadForm.ShowDialog();
                        if (result == DialogResult.OK)
                        {
                            SaveAndInstallZipFiles(new string[] { downloadForm.tempFilepath });
                            ReloadMods();
                            RefreshModList();
                        }
                        return;
                    }
                }
                installToolStripMenuItem_Click(sender, e);
            }
        }
Example #9
0
        private void RefreshModList()
        {
            listMods.Items.Clear();

            if (selectedGame == null || mods.Count == 0 || tabControl.SelectedIndex != 1)
            {
                return;
            }

            mods.Sort((x, y) => x.DisplayName.CompareTo(y.DisplayName));

            foreach (var modInfo in mods)
            {
                string status;
                if (modInfo.Status == ModStatus.Installed)
                {
                    var release = repositories.ContainsKey(selectedGame) ? repositories[selectedGame].FirstOrDefault(x => x.Id == modInfo.Id) : null;
                    var web     = !string.IsNullOrEmpty(release?.Version) ? Utils.ParseVersion(release.Version) : new Version();
                    var local   = modInfo.AvailableVersions.Keys.Max(x => x) ?? new Version();

                    if (local > modInfo.ParsedVersion && local >= web)
                    {
                        status = $"Update {local}";
                    }
                    else if (web > modInfo.ParsedVersion && web > local)
                    {
                        status = string.IsNullOrEmpty(release.DownloadUrl) ? $"Available {web}" : $"Download {web}";
                    }
                    else
                    {
                        status = "OK";
                    }
                }
                else if (modInfo.Status == ModStatus.NotInstalled)
                {
                    status = "";
                }
                else
                {
                    status = "";
                }

                var listItem = new ListViewItem(modInfo.DisplayName);
                if (modInfo.Status == ModStatus.NotInstalled)
                {
                    listItem.SubItems.Add(modInfo.AvailableVersions.Count > 0 ? modInfo.AvailableVersions.Keys.Max(x => x).ToString() : modInfo.Version);
                }
                else
                {
                    listItem.SubItems.Add(modInfo.Version);
                }
                if (!string.IsNullOrEmpty(modInfo.ManagerVersion))
                {
                    listItem.SubItems.Add(modInfo.ManagerVersion);
                    if (version < Utils.ParseVersion(modInfo.ManagerVersion))
                    {
                        listItem.ForeColor = System.Drawing.Color.FromArgb(192, 0, 0);
                        status             = "Need to update UMM";
                    }
                }
                else
                {
                    listItem.SubItems.Add("");
                }
                listItem.SubItems.Add(status);
                listMods.Items.Add(listItem);
            }
        }
Example #10
0
        private void RefreshForm()
        {
            if (!IsValid(selectedGame))
            {
                InactiveForm();
                return;
            }

            btnInstall.Text    = "安装MOD管理器模块到游戏";
            btnRestore.Enabled = false;
            gamePath           = "";

            if (string.IsNullOrEmpty(selectedGameParams.Path) || !Directory.Exists(selectedGameParams.Path))
            {
                var result = FindGameFolder(selectedGame.Folder);
                if (string.IsNullOrEmpty(result))
                {
                    InactiveForm();
                    btnOpenFolder.ForeColor          = Color.Red;
                    btnOpenFolder.Text               = "选择游戏主目录";
                    folderBrowserDialog.SelectedPath = null;
                    Log.Print($"游戏主目录“{selectedGame.Folder}”不存在!");
                    return;
                }
                Log.Print($"已检测到游戏主目录“{result}”。");
                selectedGameParams.Path = result;
            }

            if (!Utils.IsUnixPlatform() && !Directory.GetFiles(selectedGameParams.Path, "*.exe", SearchOption.TopDirectoryOnly).Any())
            {
                InactiveForm();
                Log.Print("请选择游戏可执行文件所在的目录。");
                return;
            }

            if (Utils.IsMacPlatform() && !selectedGameParams.Path.EndsWith(".app"))
            {
                InactiveForm();
                Log.Print("请选择游戏可执行文件(扩展名为.app)所在的目录。");
                return;
            }

            Utils.TryParseEntryPoint(selectedGame.EntryPoint, out var assemblyName);
            gamePath = selectedGameParams.Path;
            btnOpenFolder.ForeColor          = Color.Green;
            btnOpenFolder.Text               = new DirectoryInfo(gamePath).Name;
            folderBrowserDialog.SelectedPath = gamePath;
            managedPath               = FindManagedFolder(gamePath);
            managerPath               = Path.Combine(managedPath, nameof(UnityModManager));
            entryAssemblyPath         = Path.Combine(managedPath, assemblyName);
            injectedEntryAssemblyPath = entryAssemblyPath;
            managerAssemblyPath       = Path.Combine(managerPath, typeof(UnityModManager).Module.Name);
            entryPoint          = selectedGame.EntryPoint;
            injectedEntryPoint  = selectedGame.EntryPoint;
            assemblyDef         = null;
            injectedAssemblyDef = null;
            managerDef          = null;
            doorstopPath        = Path.Combine(gamePath, doorstopFilename);
            doorstopConfigPath  = Path.Combine(gamePath, doorstopConfigFilename);

            libraryPaths = new List <string>();
            var gameSupportVersion = !string.IsNullOrEmpty(selectedGame.MinimalManagerVersion) ? Utils.ParseVersion(selectedGame.MinimalManagerVersion) : VER_0_22;

            foreach (var item in libraryFiles.Where(item => (item.Value & LibIncParam.Minimal_lt_0_22) <= 0 || gameSupportVersion < VER_0_22))
            {
                libraryPaths.Add(Path.Combine(managerPath, item.Key));
            }

            if (!string.IsNullOrEmpty(selectedGame.GameExe))
            {
                if (selectedGame.GameExe.Contains('*'))
                {
                    foreach (var file in new DirectoryInfo(gamePath).GetFiles(selectedGame.GameExe, SearchOption.TopDirectoryOnly))
                    {
                        selectedGame.GameExe = file.Name;
                    }
                }
                gameExePath = Path.Combine(gamePath, selectedGame.GameExe);
            }
            else
            {
                gameExePath = string.Empty;
            }

            var path = new DirectoryInfo(Application.StartupPath).FullName;

            if (path.StartsWith(gamePath))
            {
                InactiveForm();
                Log.Print("DUMM目录不能放在游戏主目录及其子目录下,请先关闭DUMM,再将DUMM目录移动到单独的目录下再试!");
                return;
            }

            try
            {
                assemblyDef = ModuleDefMD.Load(File.ReadAllBytes(entryAssemblyPath));
            }
            catch (Exception e)
            {
                InactiveForm();
                Log.Print(e + Environment.NewLine + entryAssemblyPath);
                return;
            }

            var useOldPatchTarget = false;

            GameInfo.filepathInGame = Path.Combine(managerPath, "Config.xml");

            if (File.Exists(GameInfo.filepathInGame))
            {
                var gameConfig = GameInfo.ImportFromGame();
                if (gameConfig == null || !Utils.TryParseEntryPoint(gameConfig.EntryPoint, out assemblyName))
                {
                    InactiveForm();
                    return;
                }
                injectedEntryPoint        = gameConfig.EntryPoint;
                injectedEntryAssemblyPath = Path.Combine(managedPath, assemblyName);
            }
            else if (!string.IsNullOrEmpty(selectedGame.OldPatchTarget))
            {
                if (!Utils.TryParseEntryPoint(selectedGame.OldPatchTarget, out assemblyName))
                {
                    InactiveForm();
                    return;
                }
                useOldPatchTarget         = true;
                injectedEntryPoint        = selectedGame.OldPatchTarget;
                injectedEntryAssemblyPath = Path.Combine(managedPath, assemblyName);
            }

            try
            {
                injectedAssemblyDef = injectedEntryAssemblyPath == entryAssemblyPath ? assemblyDef : ModuleDefMD.Load(File.ReadAllBytes(injectedEntryAssemblyPath));
                if (File.Exists(managerAssemblyPath))
                {
                    managerDef = ModuleDefMD.Load(File.ReadAllBytes(managerAssemblyPath));
                }
            }
            catch (Exception e)
            {
                InactiveForm();
                Log.Print(e + Environment.NewLine + injectedEntryAssemblyPath + Environment.NewLine + managerAssemblyPath);
                return;
            }

            var disabledMethods    = new List <InstallType>();
            var unavailableMethods = new List <InstallType>();
            var managerType        = typeof(UnityModManager);
            var starterType        = typeof(UnityModManagerStarter);

Rescan:
            var v0_12_Installed = injectedAssemblyDef.Types.FirstOrDefault(x => x.Name == managerType.Name);
            var newWayInstalled     = injectedAssemblyDef.Types.FirstOrDefault(x => x.Name == starterType.Name);
            var hasInjectedAssembly = v0_12_Installed != null || newWayInstalled != null;

            if (useOldPatchTarget && !hasInjectedAssembly)
            {
                useOldPatchTarget         = false;
                injectedEntryPoint        = selectedGame.EntryPoint;
                injectedEntryAssemblyPath = entryAssemblyPath;
                injectedAssemblyDef       = assemblyDef;
                goto Rescan;
            }

            if (Utils.IsUnixPlatform() || !File.Exists(gameExePath))
            {
                unavailableMethods.Add(InstallType.DoorstopProxy);
                selectedGameParams.InstallType = InstallType.Assembly;
            }
            else if (File.Exists(doorstopPath))
            {
                disabledMethods.Add(InstallType.Assembly);
                selectedGameParams.InstallType = InstallType.DoorstopProxy;
            }

            if (hasInjectedAssembly)
            {
                disabledMethods.Add(InstallType.DoorstopProxy);
                selectedGameParams.InstallType = InstallType.Assembly;
            }

            foreach (var ctrl in installTypeGroup.Controls)
            {
                if (!(ctrl is RadioButton btn))
                {
                    continue;
                }
                if (unavailableMethods.Exists(x => x.ToString() == btn.Name))
                {
                    btn.Visible = false;
                    btn.Enabled = false;
                    continue;
                }
                if (disabledMethods.Exists(x => x.ToString() == btn.Name))
                {
                    btn.Visible = true;
                    btn.Enabled = false;
                    continue;
                }

                btn.Visible = true;
                btn.Enabled = true;
                btn.Checked = btn.Name == selectedGameParams.InstallType.ToString();
            }

            if (selectedGameParams.InstallType == InstallType.Assembly)
            {
                btnRestore.Enabled = IsDirty(injectedAssemblyDef) && File.Exists($"{injectedEntryAssemblyPath}{Utils.FileSuffixCache}");
            }

            tabControl.TabPages[1].Enabled = true;
            managerDef ??= injectedAssemblyDef;
            var managerInstalled = managerDef.Types.FirstOrDefault(x => x.Name == managerType.Name);

            if (managerInstalled != null && (hasInjectedAssembly || selectedGameParams.InstallType == InstallType.DoorstopProxy))
            {
                btnInstall.Text    = "更新MOD管理器模块";
                btnInstall.Enabled = false;
                btnRemove.Enabled  = true;
                Version version2;

                if (v0_12_Installed != null)
                {
                    var versionString = managerInstalled.Fields.First(x => x.Name == nameof(UnityModManager.version)).Constant.Value.ToString();
                    version2 = Utils.ParseVersion(versionString);
                }
                else
                {
                    version2 = managerDef.Assembly.Version;
                }

                installedVersion.Text = version2.ToString();

                if (version > version2 && v0_12_Installed == null)
                {
                    btnInstall.Enabled = true;
                }
            }
            else
            {
                installedVersion.Text = "-";
                btnInstall.Enabled    = true;
                btnRemove.Enabled     = false;
            }
        }
Example #11
0
        private void CheckState()
        {
            if (!IsValid(selectedGame))
            {
                InactiveForm();
                return;
            }

            btnInstall.Text = "Install";

            currentGamePath = "";
            if (!param.ExtractGamePath(selectedGame.Name, out var result) || !Directory.Exists(result))
            {
                result = FindGameFolder(selectedGame.Folder);
                if (string.IsNullOrEmpty(result))
                {
                    InactiveForm();
                    btnOpenFolder.ForeColor          = System.Drawing.Color.FromArgb(192, 0, 0);
                    btnOpenFolder.Text               = "Select Game Folder";
                    folderBrowserDialog.SelectedPath = null;
                    Log.Print($"Game folder '{selectedGame.Folder}' not found.");
                    return;
                }
                Log.Print($"Game folder detected as '{result}'.");
                param.SaveGamePath(selectedGame.Name, result);
            }
            else
            {
                Log.Print($"Game folder set as '{result}'.");
            }
            currentGamePath                  = result;
            btnOpenFolder.ForeColor          = System.Drawing.Color.Black;
            btnOpenFolder.Text               = new DirectoryInfo(result).Name;
            folderBrowserDialog.SelectedPath = currentGamePath;
            currentManagedPath               = FindManagedFolder(currentGamePath);

            var         assemblyPath = Path.Combine(currentManagedPath, selectedGame.AssemblyName);
            ModuleDefMD assembly     = null;

            var originalAssemblyPath = $"{assemblyPath}.original_";

            btnRestore.Enabled = File.Exists(originalAssemblyPath);

            if (File.Exists(assemblyPath))
            {
                try
                {
                    assembly = ModuleDefMD.Load(File.ReadAllBytes(assemblyPath));
                }
                catch (Exception e)
                {
                    InactiveForm();
                    Log.Print(e.Message);
                    return;
                }
            }
            else
            {
                InactiveForm();
                Log.Print($"'{selectedGame.AssemblyName}' not found.");
                return;
            }

            tabControl.TabPages[1].Enabled = true;

            var modManagerType        = typeof(UnityModManager);
            var modManagerDefInjected = assembly.Types.FirstOrDefault(x => x.Name == modManagerType.Name);

            if (modManagerDefInjected != null)
            {
                btnInstall.Text    = "Update";
                btnInstall.Enabled = false;
                btnRemove.Enabled  = true;

                var versionString = modManagerDefInjected.Fields.First(x => x.Name == nameof(UnityModManager.version)).Constant.Value.ToString();
                var version2      = Utils.ParseVersion(versionString);
                installedVersion.Text = versionString;
                if (version != version2)
                {
                    btnInstall.Enabled = true;
                }
            }
            else
            {
                installedVersion.Text = "-";
                btnInstall.Enabled    = true;
                btnRemove.Enabled     = false;
            }

            btnRestore.Enabled = IsDirty(assembly) && btnRestore.Enabled;
        }
Example #12
0
        private void RefreshModList()
        {
            listMods.Items.Clear();

            if (selectedGame == null || _mods.Count == 0 || tabControl.SelectedIndex != 1)
            {
                return;
            }

            _mods.Sort((x, y) => string.Compare(x.DisplayName, y.DisplayName, StringComparison.OrdinalIgnoreCase));

            foreach (var modInfo in _mods)
            {
                var status = "";

                if (ModStatus.Installed.Equals(modInfo.Status))
                {
                    var release = _repositories.ContainsKey(selectedGame) ? _repositories[selectedGame].FirstOrDefault(x => x.Id == modInfo.Id) : null;
                    var web     = !string.IsNullOrEmpty(release?.Version) ? Utils.ParseVersion(release.Version) : new Version();
                    var local   = modInfo.AvailableVersions.Keys.Max(x => x) ?? new Version();

                    if (local > modInfo.ParsedVersion && local >= web)
                    {
                        status = $"更新 {local}";
                    }
                    else if (web > modInfo.ParsedVersion && web > local)
                    {
                        status = string.IsNullOrEmpty(release?.DownloadUrl) ? $"可更新 {web}" : $"下载 {web}";
                    }
                    else
                    {
                        status = "就绪";
                    }
                }

                var listItem = new ListViewItem(modInfo.DisplayName);

                if (modInfo.Status == ModStatus.NotInstalled)
                {
                    listItem.SubItems.Add(modInfo.AvailableVersions.Count > 0 ? modInfo.AvailableVersions.Keys.Max(x => x).ToString() : modInfo.Version);
                }
                else
                {
                    listItem.SubItems.Add(modInfo.Version);
                }

                if (!string.IsNullOrEmpty(modInfo.ManagerVersion))
                {
                    listItem.SubItems.Add(modInfo.ManagerVersion);
                    if (version < Utils.ParseVersion(modInfo.ManagerVersion))
                    {
                        listItem.ForeColor = System.Drawing.Color.FromArgb(192, 0, 0);
                        status             = "需要更新MOD管理器";
                    }
                }
                else
                {
                    listItem.SubItems.Add("");
                }

                listItem.SubItems.Add(status);
                listMods.Items.Add(listItem);
            }
        }
Example #13
0
        private void RefreshForm()
        {
            if (!IsValid(selectedGame))
            {
                InactiveForm();
                return;
            }

            btnInstall.Text              = "安装MOD管理器模块到游戏";
            btnRestore.Enabled           = false;
            additionallyGroupBox.Visible = false;

            gamePath = "";
            if (string.IsNullOrEmpty(selectedGameParams.Path) || !Directory.Exists(selectedGameParams.Path))
            {
                var result = FindGameFolder(selectedGame.Folder);
                if (string.IsNullOrEmpty(result))
                {
                    InactiveForm();
                    btnOpenFolder.ForeColor          = Color.Red;
                    btnOpenFolder.Text               = "选择游戏主目录";
                    folderBrowserDialog.SelectedPath = null;
                    Log.Print($"游戏主目录“{selectedGame.Folder}”不存在!");
                    return;
                }
                Log.Print($"已检测到游戏主目录“{result}”。");
                selectedGameParams.Path = result;
            }

            if (!Utils.IsUnixPlatform() && !Directory.GetFiles(selectedGameParams.Path, "*.exe", SearchOption.TopDirectoryOnly).Any())
            {
                InactiveForm();
                Log.Print("请选择游戏可执行文件所在的目录。");
                return;
            }

            Utils.TryParseEntryPoint(selectedGame.EntryPoint, out var assemblyName);

            gamePath = selectedGameParams.Path;
            btnOpenFolder.ForeColor          = Color.Green;
            btnOpenFolder.Text               = new DirectoryInfo(gamePath).Name;
            folderBrowserDialog.SelectedPath = gamePath;
            managedPath               = FindManagedFolder(gamePath);
            managerPath               = Path.Combine(managedPath, nameof(UnityModManager));
            entryAssemblyPath         = Path.Combine(managedPath, assemblyName);
            injectedEntryAssemblyPath = entryAssemblyPath;
            managerAssemblyPath       = Path.Combine(managerPath, typeof(UnityModManager).Module.Name);
            entryPoint          = selectedGame.EntryPoint;
            injectedEntryPoint  = selectedGame.EntryPoint;
            assemblyDef         = null;
            injectedAssemblyDef = null;
            managerDef          = null;

            gameExePath = !string.IsNullOrEmpty(selectedGame.GameExe) ? Path.Combine(gamePath, selectedGame.GameExe) : string.Empty;

            doorstopPath       = Path.Combine(gamePath, doorstopFilename);
            doorstopConfigPath = Path.Combine(gamePath, doorstopConfigFilename);

            libraryPaths = new string[libraryFiles.Length];
            for (int i = 0; i < libraryFiles.Length; i++)
            {
                libraryPaths[i] = Path.Combine(managerPath, libraryFiles[i]);
            }

            var parent = new DirectoryInfo(Application.StartupPath).Parent;

            for (int i = 0; i < 3; i++)
            {
                if (parent == null)
                {
                    break;
                }

                if (parent.FullName == gamePath)
                {
                    InactiveForm();
                    Log.Print("Unity游戏MOD管理器不能定位游戏主目录!");
                    return;
                }
                parent = parent.Parent;
            }

            try
            {
                assemblyDef = ModuleDefMD.Load(File.ReadAllBytes(entryAssemblyPath));
            }
            catch (Exception e)
            {
                InactiveForm();
                Log.Print(e.ToString());
                return;
            }

            var useOldPatchTarget = false;

            GameInfo.filepathInGame = Path.Combine(managerPath, "Config.xml");
            if (File.Exists(GameInfo.filepathInGame))
            {
                var gameConfig = GameInfo.ImportFromGame();
                if (gameConfig == null || !Utils.TryParseEntryPoint(gameConfig.EntryPoint, out assemblyName))
                {
                    InactiveForm();
                    return;
                }
                injectedEntryPoint        = gameConfig.EntryPoint;
                injectedEntryAssemblyPath = Path.Combine(managedPath, assemblyName);
            }
            else if (!string.IsNullOrEmpty(selectedGame.OldPatchTarget))
            {
                if (!Utils.TryParseEntryPoint(selectedGame.OldPatchTarget, out assemblyName))
                {
                    InactiveForm();
                    return;
                }
                useOldPatchTarget         = true;
                injectedEntryPoint        = selectedGame.OldPatchTarget;
                injectedEntryAssemblyPath = Path.Combine(managedPath, assemblyName);
            }

            try
            {
                if (injectedEntryAssemblyPath == entryAssemblyPath)
                {
                    injectedAssemblyDef = assemblyDef;
                }
                else
                {
                    injectedAssemblyDef = ModuleDefMD.Load(File.ReadAllBytes(injectedEntryAssemblyPath));
                }
                if (File.Exists(managerAssemblyPath))
                {
                    managerDef = ModuleDefMD.Load(File.ReadAllBytes(managerAssemblyPath));
                }
            }
            catch (Exception e)
            {
                InactiveForm();
                Log.Print(e.ToString());
                return;
            }

            var disabledMethods    = new List <InstallType>();
            var unavailableMethods = new List <InstallType>();

            var managerType = typeof(UnityModManager);
            var starterType = typeof(Injection.UnityModManagerStarter);

Rescan:
            var v0_12_Installed = injectedAssemblyDef.Types.FirstOrDefault(x => x.Name == managerType.Name);
            var newWayInstalled     = injectedAssemblyDef.Types.FirstOrDefault(x => x.Name == starterType.Name);
            var hasInjectedAssembly = v0_12_Installed != null || newWayInstalled != null;

            if (useOldPatchTarget && !hasInjectedAssembly)
            {
                useOldPatchTarget         = false;
                injectedEntryPoint        = selectedGame.EntryPoint;
                injectedEntryAssemblyPath = entryAssemblyPath;
                injectedAssemblyDef       = assemblyDef;
                goto Rescan;
            }

            if (Utils.IsUnixPlatform() || !File.Exists(gameExePath))
            {
                unavailableMethods.Add(InstallType.DoorstopProxy);
                selectedGameParams.InstallType = InstallType.Assembly;
            }
            else if (File.Exists(doorstopPath))
            {
                disabledMethods.Add(InstallType.Assembly);
                selectedGameParams.InstallType = InstallType.DoorstopProxy;
            }

            if (hasInjectedAssembly)
            {
                disabledMethods.Add(InstallType.DoorstopProxy);
                selectedGameParams.InstallType = InstallType.Assembly;
            }

            foreach (var ctrl in installTypeGroup.Controls)
            {
                if (ctrl is RadioButton btn)
                {
                    if (unavailableMethods.Exists(x => x.ToString() == btn.Name))
                    {
                        btn.Visible = false;
                        btn.Enabled = false;
                        continue;
                    }
                    if (disabledMethods.Exists(x => x.ToString() == btn.Name))
                    {
                        btn.Visible = true;
                        btn.Enabled = false;
                        continue;
                    }

                    btn.Visible = true;
                    btn.Enabled = true;
                    btn.Checked = btn.Name == selectedGameParams.InstallType.ToString();
                }
            }

            if (selectedGameParams.InstallType == InstallType.Assembly)
            {
                btnRestore.Enabled = IsDirty(injectedAssemblyDef) && File.Exists($"{injectedEntryAssemblyPath}.original_");
            }

            tabControl.TabPages[1].Enabled = true;

            managerDef = managerDef ?? injectedAssemblyDef;

            var managerInstalled = managerDef.Types.FirstOrDefault(x => x.Name == managerType.Name);

            if (managerInstalled != null && (hasInjectedAssembly || selectedGameParams.InstallType == InstallType.DoorstopProxy))
            {
                btnInstall.Text    = "更新MOD管理器模块";
                btnInstall.Enabled = false;
                btnRemove.Enabled  = true;

                Version version2;
                if (v0_12_Installed != null)
                {
                    var versionString = managerInstalled.Fields.First(x => x.Name == nameof(UnityModManager.version)).Constant.Value.ToString();
                    version2 = Utils.ParseVersion(versionString);
                }
                else
                {
                    version2 = managerDef.Assembly.Version;
                }

                installedVersion.Text = version2.ToString();
                if (version > version2 && v0_12_Installed == null)
                {
                    btnInstall.Enabled = true;
                }
            }
            else
            {
                installedVersion.Text = "-";
                btnInstall.Enabled    = true;
                btnRemove.Enabled     = false;
            }

            if (!string.IsNullOrEmpty(selectedGame.Additionally))
            {
                notesTextBox.Text            = selectedGame.Additionally;
                additionallyGroupBox.Visible = true;
            }
        }