Refactored methods that do filesystem stuff from `ScrapMod` to `ModsLauncher`

This commit is contained in:
strongleong 2022-02-15 22:51:37 +11:00
parent ca870eb66b
commit 8da74ffa6d
2 changed files with 78 additions and 69 deletions

View File

@ -1,7 +1,10 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Globalization;
using System.IO; using System.IO;
using Ionic.Zip;
using Microsoft.Win32; using Microsoft.Win32;
namespace ScrapModLoader namespace ScrapModLoader
@ -9,18 +12,22 @@ namespace ScrapModLoader
public class ModsLauncher public class ModsLauncher
{ {
public List<ScrapMod> Mods { get; private set; } public List<ScrapMod> Mods { get; private set; }
public List<String> ModsPathes { get; set; }
public String ScraplandPath { get; set; } public String ScraplandPath { get; set; }
public String ScraplandRemasteredPath { get; set; } public String ScraplandRemasteredPath { get; set; }
public String SelectedGameVersion { get; set; } public String SelectedGameVersion { get; set; }
public String LauncherVersion { get; set; } public String LauncherVersion { get; set; }
public String SelectedGamePath { get; set; }
public ModsLauncher() public ModsLauncher()
{ {
Mods = new List<ScrapMod>(); Mods = new List<ScrapMod>();
ModsPathes = Utils.StringCollectionToList(Settings.Default.ModsPathes);
ScraplandPath = Settings.Default.ScraplandPath; ScraplandPath = Settings.Default.ScraplandPath;
ScraplandRemasteredPath = Settings.Default.ScraplandRemasteredPath; ScraplandRemasteredPath = Settings.Default.ScraplandRemasteredPath;
SelectedGameVersion = "0.0"; SelectedGameVersion = "0.0";
LauncherVersion = "0.3"; LauncherVersion = "0.3";
SelectedGamePath = String.Empty;
} }
public void ScanMods() public void ScanMods()
@ -63,9 +70,12 @@ namespace ScrapModLoader
if (displayName == null) if (displayName == null)
continue; continue;
if (displayName == "Scrapland") if (displayName == "Scrapland" || displayName == "American McGee presents Scrapland")
{ {
ScraplandPath = subkey.GetValue("InstallLocation")?.ToString() ?? ""; ScraplandPath = subkey.GetValue("InstallLocation")?.ToString() ?? "";
if (ScraplandPath == null || ScraplandPath == String.Empty)
throw new KeyNotFoundException("Installed Scrapland found, but unable to locate the instalation folder");
Settings.Default.ScraplandPath = ScraplandPath; Settings.Default.ScraplandPath = ScraplandPath;
isFound = true; isFound = true;
} }
@ -73,6 +83,9 @@ namespace ScrapModLoader
if (displayName == "Scrapland Remastered") if (displayName == "Scrapland Remastered")
{ {
ScraplandRemasteredPath = subkey.GetValue("InstallLocation")?.ToString() ?? ""; ScraplandRemasteredPath = subkey.GetValue("InstallLocation")?.ToString() ?? "";
if (ScraplandRemasteredPath == null || ScraplandRemasteredPath == String.Empty)
throw new KeyNotFoundException("Installed Scrapland Remastered found, but unable to locate the instalation folder");
Settings.Default.ScraplandRemasteredPath = ScraplandRemasteredPath; Settings.Default.ScraplandRemasteredPath = ScraplandRemasteredPath;
isFound = true; isFound = true;
} }
@ -84,21 +97,78 @@ namespace ScrapModLoader
public void LoadMods() public void LoadMods()
{ {
String gamePath = SelectedGameVersion == "1.0" ? ScraplandPath : ScraplandRemasteredPath; SelectedGamePath = SelectedGameVersion == "1.0" ? ScraplandPath : ScraplandRemasteredPath;
foreach (ScrapMod mod in Mods) foreach (ScrapMod mod in Mods)
{ {
// TODO: Warning about not loading mods that not supports selected version // TODO: Warning about not loading mods that not supports selected version
if (!mod.SupportedGameVersions.Contains(SelectedGameVersion) || if (!mod.SupportedGameVersions.Contains(SelectedGameVersion) ||
Single.Parse(mod.RequiredLauncher) < Single.Parse(LauncherVersion)) Single.Parse(mod.RequiredLauncher, CultureInfo.InvariantCulture) < Single.Parse(LauncherVersion, CultureInfo.InvariantCulture))
continue; continue;
if (mod.Checked) if (mod.Checked)
if (!mod.IsEnabled(gamePath)) {
mod.Enable(gamePath, SelectedGameVersion); if (!IsEnabled(mod))
else Enable(mod);
if (mod.IsEnabled(gamePath)) }
mod.Disable(gamePath); else
{
if (IsEnabled(mod))
Disable(mod);
}
}
}
public Boolean IsLoaded(ScrapMod mod) =>
Directory.Exists(SelectedGamePath + @"Mods\" + mod.Name);
public Boolean IsEnabled(ScrapMod mod)
{
if (!IsLoaded(mod))
return false;
foreach (String file in Directory.EnumerateFiles(SelectedGamePath + @"Mods\" + mod.Name, "*.disabled", SearchOption.AllDirectories))
return false;
return true;
}
public void Enable(ScrapMod mod)
{
if (!IsLoaded(mod))
LoadModToGame(mod);
if (IsEnabled(mod))
return;
foreach (String file in Directory.EnumerateFiles(SelectedGamePath + @"Mods\" + mod.Name, "*.disabled", SearchOption.AllDirectories))
File.Move(file, Path.ChangeExtension(file, null));
}
public void Disable(ScrapMod mod)
{
if (!IsEnabled(mod))
return;
foreach (String file in Directory.EnumerateFiles(SelectedGamePath + @"Mods\" + mod.Name, "*.packed", SearchOption.AllDirectories))
File.Move(file, file + ".disabled");
}
private void LoadModToGame(ScrapMod mod)
{
String modPath = SelectedGamePath + @"Mods\" + mod.Name;
Directory.CreateDirectory(modPath);
using (ZipFile zipFile = ZipFile.Read(mod.ModPath))
{
foreach (ZipEntry zipEntry in zipFile)
{
if (!Path.GetFullPath(zipEntry.FileName).Contains(SelectedGameVersion))
continue;
if (Path.GetExtension(zipEntry.FileName) == ".packed")
zipEntry.Extract(modPath);
}
} }
} }
} }

View File

@ -40,65 +40,6 @@ namespace ScrapModLoader
} }
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, String gameVersion)
{
if (!IsLoaded(gamePath))
LoadModToGame(gamePath, gameVersion);
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, String gameVersion)
{
gamePath += @"Mods\" + Name;
Directory.CreateDirectory(gamePath);
using (ZipFile zipFile = ZipFile.Read(ModPath))
{
foreach (ZipEntry zipEntry in zipFile)
{
if (!Path.GetFullPath(zipEntry.FileName).Contains(gameVersion))
continue;
if (Path.GetExtension(zipEntry.FileName) == ".packed")
zipEntry.Extract(gamePath);
}
}
}
public static ScrapMod LoadFromFile(String path) public static ScrapMod LoadFromFile(String path)
{ {
using ZipFile zipFile = ZipFile.Read(path); using ZipFile zipFile = ZipFile.Read(path);
@ -117,8 +58,6 @@ namespace ScrapModLoader
return mod; return mod;
} }
private static void LoadConfig(ScrapMod mod, Byte[] buffer) private static void LoadConfig(ScrapMod mod, Byte[] buffer)
{ {
using MemoryStream sourceStream = new MemoryStream(buffer); using MemoryStream sourceStream = new MemoryStream(buffer);