ScrapModLoader/ScrapModLoader/ModsLauncher.cs

103 lines
3.4 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.IO;
using Microsoft.Win32;
namespace ScrapModLoader
{
public class ModsLauncher
{
public List<ScrapMod> Mods { get; private set; }
public String ScraplandPath { get; set; }
public String ScraplandRemasteredPath { get; set; }
2022-02-06 12:20:05 +00:00
public String SelectedGameVersion { get; set; }
public ModsLauncher()
{
Mods = new List<ScrapMod>();
ScraplandPath = Settings.Default.ScraplandPath;
ScraplandRemasteredPath = Settings.Default.ScraplandRemasteredPath;
2022-02-06 12:20:05 +00:00
SelectedGameVersion = "0.0";
}
public void ScanMods()
{
Mods.Clear();
foreach (String? folder in Settings.Default.ModsPathes)
{
if (folder != null)
{
String[] files = Directory.GetFiles(folder, "*.sm", SearchOption.AllDirectories);
foreach (String file in files)
Mods.Add(new ScrapMod(file));
}
}
}
public Boolean SearchForScrapland()
{
Boolean isFound = false;
String[] registryPathes =
{
@"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall",
@"SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall"
};
foreach (String registryPath in registryPathes)
{
RegistryKey? key = Registry.LocalMachine.OpenSubKey(registryPath);
if (key == null)
continue;
foreach (String subkey_name in key.GetSubKeyNames())
{
using RegistryKey? subkey = key.OpenSubKey(subkey_name);
if (subkey == null)
continue;
String? displayName = subkey.GetValue("DisplayName")?.ToString();
if (displayName == null)
continue;
if (displayName == "Scrapland")
{
ScraplandPath = subkey.GetValue("InstallLocation")?.ToString() ?? "";
2022-02-06 13:06:49 +00:00
Settings.Default.ScraplandPath = ScraplandPath;
isFound = true;
}
if (displayName == "Scrapland Remastered")
{
ScraplandRemasteredPath = subkey.GetValue("InstallLocation")?.ToString() ?? "";
2022-02-06 13:06:49 +00:00
Settings.Default.ScraplandRemasteredPath = ScraplandRemasteredPath;
isFound = true;
}
}
}
return isFound;
}
2022-02-06 10:59:08 +00:00
2022-02-06 12:20:05 +00:00
public void LoadMods()
2022-02-06 10:59:08 +00:00
{
2022-02-06 12:20:05 +00:00
String gamePath = SelectedGameVersion == "1.0" ? ScraplandPath : ScraplandRemasteredPath;
2022-02-06 11:50:32 +00:00
foreach (ScrapMod mod in Mods)
{
// TODO: Warning about not loading mods that not supports selected version
if (!mod.SupportedGameVersions.Contains(SelectedGameVersion))
2022-02-06 12:20:05 +00:00
continue;
2022-02-06 11:50:32 +00:00
if (mod.Checked)
if (!mod.IsEnabled(gamePath))
mod.Enable(gamePath, SelectedGameVersion);
2022-02-06 11:50:32 +00:00
else
if (mod.IsEnabled(gamePath))
mod.Disable(gamePath);
}
2022-02-06 10:59:08 +00:00
}
}
}