Added ability to toggle mods

This commit is contained in:
strongleong 2022-02-06 22:50:32 +11:00
parent cb4c8ac90d
commit 6dbf0656dc
2 changed files with 60 additions and 6 deletions

View file

@ -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);
}
}
}
}
}

View file

@ -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);