From 6dbf0656dc6231caef46428ebfb0fff95235d5e2 Mon Sep 17 00:00:00 2001 From: strongleong Date: Sun, 6 Feb 2022 22:50:32 +1100 Subject: [PATCH] Added ability to toggle mods --- ScrapModLoader/ModsLauncher.cs | 19 ++++++++++---- ScrapModLoader/ScrapMod.cs | 47 +++++++++++++++++++++++++++++++++- 2 files changed, 60 insertions(+), 6 deletions(-) diff --git a/ScrapModLoader/ModsLauncher.cs b/ScrapModLoader/ModsLauncher.cs index 56a0c86..c06e2c3 100644 --- a/ScrapModLoader/ModsLauncher.cs +++ b/ScrapModLoader/ModsLauncher.cs @@ -2,8 +2,6 @@ using System.Collections.Generic; using System.IO; -using Ionic.Zip; - using Microsoft.Win32; namespace ScrapModLoader @@ -78,10 +76,21 @@ namespace ScrapModLoader return isFound; } - internal void LoadMods(String gamePath) + public void LoadMods(String gamePath) { - foreach (ScrapMod mod in Mods.FindAll(mod => mod.Checked)) - mod.LoadModToGame(gamePath); + foreach (ScrapMod mod in Mods) + { + if (mod.Checked) + { + if (!mod.IsEnabled(gamePath)) + mod.Enable(gamePath); + } + else + { + if (mod.IsEnabled(gamePath)) + mod.Disable(gamePath); + } + } } } } diff --git a/ScrapModLoader/ScrapMod.cs b/ScrapModLoader/ScrapMod.cs index 455f922..d6b7ffc 100644 --- a/ScrapModLoader/ScrapMod.cs +++ b/ScrapModLoader/ScrapMod.cs @@ -38,7 +38,52 @@ namespace ScrapModLoader LoadFromFile(path); } - public void LoadModToGame(String gamePath) + + public Boolean IsLoaded(String gamePath) => Directory.Exists(gamePath + @"Mods\" + Name); + + public Boolean IsEnabled(String gamePath) + { + if (IsLoaded(gamePath)) + { + foreach (String file in Directory.EnumerateFiles(gamePath + @"Mods\" + Name)) + { + if (Path.GetExtension(file) == ".disabled") + return false; + } + + return true; + } + return false; + } + + public void Enable(String gamePath) + { + if (!IsLoaded(gamePath)) + LoadModToGame(gamePath); + + if (IsEnabled(gamePath)) + return; + + foreach (String file in Directory.EnumerateFiles(gamePath + @"Mods\" + Name)) + { + if (Path.GetExtension(file) == ".disabled") + File.Move(file, Path.ChangeExtension(file, null)); + } + } + + public void Disable(String gamePath) + { + if (!IsEnabled(gamePath)) + return; + + foreach (String file in Directory.EnumerateFiles(gamePath + @"Mods\" + Name)) + { + if (Path.GetExtension(file) == ".packed") + File.Move(file, file + ".disabled"); + } + } + + private void LoadModToGame(String gamePath) { gamePath += @"Mods\" + Name; Directory.CreateDirectory(gamePath);