예제 #1
0
        public static bool IsModLoaderInstalled(string gameFolderPath)
        {
            try
            {
                if (Directory.Exists(gameFolderPath) == false)
                {
                    return(false);
                }
                if (Directory.Exists(Shrek2Utils.GetGameSystemFolderPath(gameFolderPath)) == false)
                {
                    return(false);
                }
                if (File.Exists(Path.Combine(Shrek2Utils.GetGameSystemFolderPath(gameFolderPath), Shrek2Utils.SHREK2MM_MODLOADER_FILE_EXE)) == false)
                {
                    return(false);
                }
                if (File.Exists(Path.Combine(Shrek2Utils.GetGameSystemFolderPath(gameFolderPath), Shrek2Utils.SHREK2MM_MODLOADER_FILE_INT)) == false)
                {
                    return(false);
                }
                if (File.Exists(Path.Combine(Shrek2Utils.GetGameSystemFolderPath(gameFolderPath), Shrek2Utils.SHREK2MM_MODLOADER_FILE_DLL)) == false)
                {
                    return(false);
                }

                return(true);
            }
            catch (Exception ex)
            {
                Shrek2Utils.LogError(ex);
                return(false);
            }
        }
예제 #2
0
        public static bool DeleteModFromDataFolder(string?fileName)
        {
            try
            {
                if (fileName == null)
                {
                    return(false);
                }

                Shrek2Utils.EnsureAddedModsFolderExists();

                var filePath = Path.Combine(Shrek2Utils.GetAddedModsFolderPath(), fileName);

                if (File.Exists(filePath) == false)
                {
                    return(true);
                }

                File.Delete(filePath);

                return(true);
            }
            catch (Exception ex)
            {
                Shrek2Utils.LogError(ex);
                return(false);
            }
        }
        private void Play_Mods_Click(object sender, RoutedEventArgs e)
        {
            var settings = Shrek2MM.LoadSettings();

            if (settings == null || string.IsNullOrWhiteSpace(settings.GameFolderPath))
            {
                MessageBox.Show("Unable to 'Play' yet as you have not set up Settings correctly yet. Please ensure your Game Folder is selected before proceeding!");
                return;
            }

            if (ModItems == null || ModItems.Count <= 0)
            {
                MessageBox.Show("You currently have no added mods in the Shrek 2 Mod Manager so you cannot 'Play' yet.");
                return;
            }

            if (ModItems.Any(p => p.IsActive) == false)
            {
                MessageBox.Show("You have no Enabled Mods in the Shrek 2 Mod Manager. Ensure atleast 1 mod is enabled before proceeding.");
                return;
            }

            var installed = Shrek2MM.InstallModLoader(settings.GameFolderPath);

            if (installed == false)
            {
                MessageBox.Show("Failed to install Mod Loader into the selected Game Folder you chose in Settings. This could be a Read/write Permission issue. Check the error_log in 'Documents/Shrek 2 Mod Manager' for more details.");
                return;
            }

            if (Shrek2MM.ReinstallMods(settings.GameFolderPath, ModItems.Where(p => p.IsActive).ToList()) == false)
            {
                MessageBox.Show("Failed to install the enabled mods. This could be a Read/write Permission issue. Check the error_log in 'Documents/Shrek 2 Mod Manager' for more details.");
                return;
            }

            if (Shrek2MM.UpdateDefUserFile(settings.GameFolderPath, ModItems.Where(p => p.IsActive).ToList()) == false)
            {
                MessageBox.Show("Failed to update DefUser.ini file, this could be a Read/write Permission issue. Check the error_log in 'Documents/Shrek 2 Mod Manager' for more details.");
                return;
            }



            GameProcess = Process.Start(Shrek2Utils.GetModdedGameExeFilePath(settings.GameFolderPath), settings.DisplayMode == 0 ? "-windowed" : "");
            GameProcess.EnableRaisingEvents = true;

            Play_Button.IsEnabled = false;
            Play_Button.Content   = "Running";

            GameProcess.Exited += (sender, e) =>
            {
                Dispatcher.Invoke(() =>
                {
                    Play_Button.IsEnabled = true;
                    Play_Button.Content   = "Play";
                });
            };
        }
예제 #4
0
        public static bool UpdateDefUserFile(string gameFolderPath, List <Shrek2ModListItem> mods)
        {
            try
            {
                if (mods == null || mods.Count <= 0)
                {
                    return(false);
                }
                if (IsModLoaderInstalled(gameFolderPath) == false)
                {
                    return(false);
                }

                var backupDefUserFilePath = Path.Combine(Shrek2Utils.GetGameSystemFolderPath(gameFolderPath), Shrek2Utils.SHREK2MM_DEF_USER_BACKUP_FILE);
                var defUserFilePath       = Path.Combine(Shrek2Utils.GetGameSystemFolderPath(gameFolderPath), Shrek2Utils.SHREK2MM_DEF_USER_FILE);

                if (File.Exists(backupDefUserFilePath) == false)
                {
                    File.Copy(defUserFilePath, backupDefUserFilePath, true);
                }

                var lines = File.ReadAllLines(defUserFilePath);

                string execString = "";
                int    x          = 1;
                foreach (var installedMod in mods)
                {
                    if (x == mods.Count)
                    {
                        execString += "exec " + installedMod.UUID.Replace(" ", "");
                    }
                    else
                    {
                        execString += "exec " + installedMod.UUID.Replace(" ", "") + " | ";
                    }

                    x++;
                }

                for (int i = 0; i < lines.Length; i++)
                {
                    if (lines[i].Contains("F24="))
                    {
                        lines[i] = "F24=" + execString;
                    }
                }

                File.WriteAllLines(defUserFilePath, lines);
                return(true);
            }
            catch (Exception ex)
            {
                Shrek2Utils.LogError(ex);
                return(false);
            }
        }
예제 #5
0
        public static bool SaveSettings(Shrek2MMSettings settings)
        {
            try
            {
                Shrek2Utils.EnsureDataFolderExists();

                var json = SettingsToJSON(settings);
                File.WriteAllText(Path.Combine(Shrek2Utils.GetDataFolderPath(), Shrek2Utils.SHREK2MM_FILE_SETTINGS), json);
                return(true);
            }
            catch (Exception ex)
            {
                Shrek2Utils.LogError(ex);
                return(false);
            }
        }
예제 #6
0
        public static bool SaveMods(List <Shrek2ModListItem> items)
        {
            try
            {
                Shrek2Utils.EnsureDataFolderExists();

                var json = ListToJSON(items);
                File.WriteAllText(Path.Combine(Shrek2Utils.GetDataFolderPath(), Shrek2Utils.SHREK2MM_FILE_ADDED_MODS), json);
                return(true);
            }
            catch (Exception ex)
            {
                Shrek2Utils.LogError(ex);
                return(false);
            }
        }
예제 #7
0
        public static bool CopyModToDataFolder(string filePath, string uuid)
        {
            try
            {
                Shrek2Utils.EnsureAddedModsFolderExists();

                File.Copy(filePath, Path.Combine(Shrek2Utils.GetAddedModsFolderPath(), $"{uuid}{Path.GetExtension(filePath)}"));

                return(true);
            }
            catch (Exception ex)
            {
                Shrek2Utils.LogError(ex);
                return(false);
            }
        }
        private void Add_Mod_Button_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                OpenFileDialog ofd = new()
                {
                    Filter = "Shrek 2 Mod File (.dll)|*.dll|Shrek 2 Mod Zip (.zip)|*.zip"
                };
                ofd.ShowDialog();

                if (string.IsNullOrEmpty(ofd.FileName))
                {
                    return;
                }

                var modTitle  = System.IO.Path.GetFileNameWithoutExtension(ofd.FileName);
                var fileName  = System.IO.Path.GetFileName(ofd.FileName);
                var extension = System.IO.Path.GetExtension(ofd.FileName);
                var uuid      = Guid.NewGuid().ToString();

                var copied = Shrek2MM.CopyModToDataFolder(ofd.FileName, uuid);

                if (copied == false)
                {
                    MessageBox.Show("Couldn't add the selected mod. Please check that the program has permissions to read/write on disk, and if the issue still persists please contact the developer.");
                    return;
                }

                ModItems.Add(new Shrek2ModListItem(
                                 modTitle,
                                 extension == ".dll" ? Shrek2ModListItem.ModTypes.ModFile : Shrek2ModListItem.ModTypes.ModZip,
                                 true,
                                 uuid
                                 ));

                if (Shrek2MM.SaveMods(ModItems.ToList()) == false)
                {
                    MessageBox.Show("Failed to save the selected mod you wanted to add. We correctly copied the mod data but we couldn't save the metadata. Please check that the program has permissions to read/write on disk, and if the issue still persists please contact the developer.");
                    return;
                }
            }
            catch (Exception ex)
            {
                Shrek2Utils.LogError(ex);
                MessageBox.Show("Error occured when trying to add a mod. For more details check the Error Log in 'This PC/My Documents/Shrek 2 Mod Manager'.");
            }
        }
예제 #9
0
        public static List <Shrek2ModListItem>?LoadMods()
        {
            try
            {
                Shrek2Utils.EnsureDataFolderExists();

                if (File.Exists(Path.Combine(Shrek2Utils.GetDataFolderPath(), Shrek2Utils.SHREK2MM_FILE_ADDED_MODS)) == false)
                {
                    return(new List <Shrek2ModListItem>());
                }

                var json = File.ReadAllText(Path.Combine(Shrek2Utils.GetDataFolderPath(), Shrek2Utils.SHREK2MM_FILE_ADDED_MODS));
                return(JSONToList(json));
            }
            catch (Exception ex)
            {
                Shrek2Utils.LogError(ex);
                return(null);
            }
        }
예제 #10
0
        public static Shrek2MMSettings?LoadSettings()
        {
            try
            {
                Shrek2Utils.EnsureDataFolderExists();

                if (File.Exists(Path.Combine(Shrek2Utils.GetDataFolderPath(), Shrek2Utils.SHREK2MM_FILE_SETTINGS)) == false)
                {
                    return(new Shrek2MMSettings());
                }

                var json = File.ReadAllText(Path.Combine(Shrek2Utils.GetDataFolderPath(), Shrek2Utils.SHREK2MM_FILE_SETTINGS));
                return(JSONToSettings(json));
            }
            catch (Exception ex)
            {
                Shrek2Utils.LogError(ex);
                return(null);
            }
        }
예제 #11
0
        public static async Task <bool> ExtractModLoader()
        {
            try
            {
                Shrek2Utils.EnsureDataFolderExists();

                var assembly     = Assembly.GetExecutingAssembly();
                var resourceName = assembly.GetManifestResourceNames().Single(str => str.EndsWith(Shrek2Utils.SHREK2MM_FILE_MOD_LOADER_ZIP));

                if (string.IsNullOrEmpty(resourceName))
                {
                    return(false);
                }

                using (var resStream = assembly.GetManifestResourceStream(resourceName))
                {
                    var ms = new MemoryStream();

                    if (resStream != null)
                    {
                        await resStream.CopyToAsync(ms);

                        var bytes = ms.ToArray();

                        File.WriteAllBytes(Path.Combine(Shrek2Utils.GetDataFolderPath(), Shrek2Utils.SHREK2MM_FILE_MOD_LOADER_ZIP), bytes);

                        return(true);
                    }
                }

                return(false);
            }
            catch (Exception ex)
            {
                Shrek2Utils.LogError(ex);
                return(false);
            }
        }
예제 #12
0
        public static bool InstallModLoader(string gameFolderPath)
        {
            try
            {
                if (IsModLoaderInstalled(gameFolderPath))
                {
                    return(true);
                }

                if (Directory.Exists(gameFolderPath) == false)
                {
                    return(false);
                }
                if (Directory.Exists(Shrek2Utils.GetGameSystemFolderPath(gameFolderPath)) == false)
                {
                    return(false);
                }

                if (Directory.Exists(Shrek2Utils.GetGameSystemModsFolderPath(gameFolderPath)) == false)
                {
                    Directory.CreateDirectory(Shrek2Utils.GetGameSystemModsFolderPath(gameFolderPath));
                }

                var     zipFilePath = Shrek2Utils.GetModLoaderZipFilePath();
                var     targetDir   = Shrek2Utils.GetGameSystemFolderPath(gameFolderPath);
                FastZip fastZip     = new FastZip();

                fastZip.ExtractZip(zipFilePath, targetDir, null);

                return(true);
            }
            catch (Exception ex)
            {
                Shrek2Utils.LogError(ex);
                return(false);
            }
        }
예제 #13
0
        public static bool ReinstallMods(string gameFolderPath, List <Shrek2ModListItem> mods)
        {
            try
            {
                if (IsModLoaderInstalled(gameFolderPath) == false)
                {
                    return(false);
                }

                if (Directory.Exists(Shrek2Utils.GetGameSystemModsFolderPath(gameFolderPath)) == false)
                {
                    Directory.CreateDirectory(Shrek2Utils.GetGameSystemModsFolderPath(gameFolderPath));
                }
                else
                {
                    Directory.Delete(Shrek2Utils.GetGameSystemModsFolderPath(gameFolderPath), true);
                    Directory.CreateDirectory(Shrek2Utils.GetGameSystemModsFolderPath(gameFolderPath));
                }

                if (mods == null || mods.Count <= 0)
                {
                    return(false);
                }

                foreach (var mod in mods)
                {
                    Directory.CreateDirectory(Path.Combine(Shrek2Utils.GetGameSystemModsFolderPath(gameFolderPath), mod.UUID));
                    if (mod.ModType == Shrek2ModListItem.ModTypes.ModFile)
                    {
                        File.Copy(Path.Combine(Shrek2Utils.GetAddedModsFolderPath(), mod.FileName), Path.Combine(Shrek2Utils.GetGameSystemModsFolderPath(gameFolderPath), mod.UUID, mod.FileName));
                    }
                    else if (mod.ModType == Shrek2ModListItem.ModTypes.ModZip)
                    {
                        var     zipFilePath = Path.Combine(Shrek2Utils.GetAddedModsFolderPath(), mod.FileName);
                        var     targetDir   = Path.Combine(Shrek2Utils.GetGameSystemModsFolderPath(gameFolderPath), mod.UUID);
                        FastZip fastZip     = new FastZip();
                        fastZip.ExtractZip(zipFilePath, targetDir, null);

                        string dllFilePath = Path.Combine(Shrek2Utils.GetGameSystemModsFolderPath(gameFolderPath), mod.UUID, $"{mod.UUID}.dll");

                        var files = Directory.GetFiles(targetDir);

                        if (files.Length <= 0)
                        {
                            Shrek2Utils.LogError(new Exception($"Failed to install the mod '{mod.Title}' because it had no files inside of it's .zip file."));
                            return(false);
                        }

                        var zipDllName = files.FirstOrDefault(p => Path.GetExtension(p) == ".dll");

                        if (string.IsNullOrWhiteSpace(zipDllName))
                        {
                            Shrek2Utils.LogError(new Exception($"Failed to install the mod '{mod.Title}' a .dll file inside of the .zip file doesn't exist!"));
                            return(false);
                        }

                        if (File.Exists(zipDllName))
                        {
                            FileInfo fi = new FileInfo(zipDllName);

                            if (fi.Exists)
                            {
                                fi.MoveTo(dllFilePath);
                            }
                        }
                    }
                }

                return(true);
            }
            catch (Exception ex)
            {
                Shrek2Utils.LogError(ex);
                return(false);
            }
        }