diff --git a/ScrapModLoader/ModsLauncher.cs b/ScrapModLoader/ModsLauncher.cs index 79993fe..73e9c42 100644 --- a/ScrapModLoader/ModsLauncher.cs +++ b/ScrapModLoader/ModsLauncher.cs @@ -32,7 +32,7 @@ namespace ScrapModLoader { String[] files = Directory.GetFiles(folder, "*.sm", SearchOption.AllDirectories); foreach (String file in files) - Mods.Add(new ScrapMod(file)); + Mods.Add(ScrapMod.LoadFromFile(file)); } } } @@ -96,7 +96,7 @@ namespace ScrapModLoader if (mod.Checked) if (!mod.IsEnabled(gamePath)) mod.Enable(gamePath, SelectedGameVersion); - else + else if (mod.IsEnabled(gamePath)) mod.Disable(gamePath); } diff --git a/ScrapModLoader/ScrapMod.cs b/ScrapModLoader/ScrapMod.cs index 5c458de..c25f920 100644 --- a/ScrapModLoader/ScrapMod.cs +++ b/ScrapModLoader/ScrapMod.cs @@ -12,41 +12,33 @@ namespace ScrapModLoader { public class ScrapMod { - public String Name { get; private set; } - public String Description { get; private set; } - public String ModPath { get; private set; } - public BitmapImage Icon { get; private set; } - public Boolean Checked { get; set; } - public String Category { get; private set; } - public String Version { get; private set; } - public String RequiredLauncher { get; private set; } - public List SupportedGameVersions { get; private set; } - public String SupportedGameVersionsDisplay { - get - { - String result = String.Empty; - foreach (String version in SupportedGameVersions) - result += version + ", "; - return result.TrimEnd(',', ' '); - } - } - public List Authors { get; private set; } - public Dictionary> Credits { get; private set; } + public String Name { get; private set; } = String.Empty; - public ScrapMod(String path) + public String Description { get; private set; } = String.Empty; + + public String ModPath { get; private set; } = String.Empty; + + public BitmapImage Icon { get; private set; } = new BitmapImage(); + + public Boolean Checked { get; set; } = false; + + public String Category { get; private set; } = String.Empty; + + public String Version { get; private set; } = String.Empty; + + public String RequiredLauncher { get; private set; } = String.Empty; + + public List SupportedGameVersions { get; private set; } = new List(); + + public String SupportedGameVersionsDisplay => String.Join(", ", SupportedGameVersions); + + public List Authors { get; private set; } = new List(); + + public Dictionary> Credits { get; private set; } = new Dictionary>(); + + private ScrapMod() { - ModPath = path; - Name = String.Empty; - Description = String.Empty; - Icon = new BitmapImage(); - Checked = false; - Category = String.Empty; - Version = String.Empty; - RequiredLauncher = String.Empty; - SupportedGameVersions = new List(); - Authors = new List(); - Credits = new Dictionary>(); - LoadFromFile(path); + } @@ -95,7 +87,7 @@ namespace ScrapModLoader gamePath += @"Mods\" + Name; Directory.CreateDirectory(gamePath); - using (ZipFile zipFile = ZipFile.Read(ModPath)) + using (var zipFile = ZipFile.Read(ModPath)) { foreach (ZipEntry zipEntry in zipFile) { @@ -108,71 +100,55 @@ namespace ScrapModLoader } } - private void LoadFromFile(String path) + public static ScrapMod LoadFromFile(String path) { - using (ZipFile zipFile = ZipFile.Read(path)) + using var zipFile = ZipFile.Read(path); + + Byte[] iconBuffer = Utils.ExtractFromZip(zipFile, "icon.png"); + Byte[] confBuffer = Utils.ExtractFromZip(zipFile, "config.toml"); + + var mod = new ScrapMod() { - Byte[] iconBuffer = ExtractFromZip(zipFile, "icon.png"); - Byte[] confBuffer = ExtractFromZip(zipFile, "config.toml"); - LoadIcon(iconBuffer); - LoadConfig(confBuffer); - } + ModPath = path, + Icon = Utils.LoadImage(iconBuffer) + }; + + LoadConfig(ref mod, confBuffer); + + return mod; } - private Byte[] ExtractFromZip(ZipFile zip, String entry_path) + + + private static void LoadConfig(ref ScrapMod mod, Byte[] buffer) { - ZipEntry? entry = zip[entry_path]; - if (entry == null) - throw new FileFormatException($"No '{entry_path}' in {Name} found"); + using var sourceStream = new MemoryStream(buffer); + using var reader = new StreamReader(sourceStream); - Byte[] buffer = new Byte[entry.UncompressedSize]; - using (MemoryStream zipStream = new MemoryStream(buffer)) - entry.Extract(zipStream); + TomlTable config = TOML.Parse(reader); - return buffer; - } + CheckConfig(config); - private void LoadIcon(Byte[] buffer) - { - using (MemoryStream sourceStream = new MemoryStream(buffer)) + mod.Name = config["title"]; + mod.Description = config["description"]; + mod.Category = config["category"]; + mod.Version = config["version"]; + mod.RequiredLauncher = config["requiredLauncher"]; + + foreach (TomlNode version in config["supportedGameVersions"]) + mod.SupportedGameVersions.Add(version); + + foreach (TomlNode author in config["authors"]) + mod.Authors.Add(author["name"]); + + foreach (TomlNode credit in config["credits"]) { - Icon.BeginInit(); - Icon.CacheOption = BitmapCacheOption.OnLoad; - Icon.StreamSource = sourceStream; - Icon.EndInit(); - } - } + List entries = new List(); - private void LoadConfig(Byte[] buffer) - { - using (MemoryStream sourceStream = new MemoryStream(buffer)) - using (StreamReader reader = new StreamReader(sourceStream)) - { - TomlTable config = TOML.Parse(reader); + foreach (TomlNode entry in credit["credits"]) + entries.Add(entry["name"]); - CheckConfig(config); - - Name = config["title"]; - Description = config["description"]; - Category = config["category"]; - Version = config["version"]; - RequiredLauncher = config["requiredLauncher"]; - - foreach (TomlNode version in config["supportedGameVersions"]) - SupportedGameVersions.Add(version); - - foreach (TomlNode author in config["authors"]) - Authors.Add(author["name"]); - - foreach (TomlNode credit in config["credits"]) - { - List entries = new List(); - - foreach (TomlNode entry in credit["credits"]) - entries.Add(entry["name"]); - - Credits.Add(credit["group"], entries); - } + mod.Credits.Add(credit["group"], entries); } } diff --git a/ScrapModLoader/Utils.cs b/ScrapModLoader/Utils.cs index 13fc653..61bd172 100644 --- a/ScrapModLoader/Utils.cs +++ b/ScrapModLoader/Utils.cs @@ -1,27 +1,59 @@ using System; using System.Collections.Generic; using System.Collections.Specialized; +using System.Windows.Media.Imaging; +using System.IO; -namespace ScrapModLoader +using Ionic.Zip; + + +namespace ScrapModLoader; + +internal static class Utils { - internal static class Utils + public static String GetFolderDialog() { - public static String GetFolderDialog() - { - using System.Windows.Forms.FolderBrowserDialog? dialog = new System.Windows.Forms.FolderBrowserDialog(); - System.Windows.Forms.DialogResult result = dialog.ShowDialog(); - return dialog.SelectedPath; - } + using System.Windows.Forms.FolderBrowserDialog? dialog = new System.Windows.Forms.FolderBrowserDialog(); + System.Windows.Forms.DialogResult result = dialog.ShowDialog(); + return dialog.SelectedPath; + } - public static List StringCollectionToList(StringCollection collection) + public static List StringCollectionToList(StringCollection collection) + { + List list = new List(); + foreach (String? item in collection) { - List list = new List(); - foreach (String? item in collection) - { - if (item != null) - list.Add(item); - } - return list; + if (item != null) + list.Add(item); } + return list; + } + + public static Byte[] ExtractFromZip(ZipFile zip, String entry_path) + { + ZipEntry? entry = zip[entry_path]; + if (entry == null) + throw new FileFormatException($"No '{entry_path}' in {zip.Name} found"); + + var buffer = new Byte[entry.UncompressedSize]; + using (var zipStream = new MemoryStream(buffer)) + entry.Extract(zipStream); + + return buffer; + } + + public static BitmapImage LoadImage(Byte[] buffer) + { + using var sourceStream = new MemoryStream(buffer); + + var image = new BitmapImage(); + + image.BeginInit(); + image.CacheOption = BitmapCacheOption.OnLoad; + image.StreamSource = sourceStream; + image.EndInit(); + + return image; } } +