From 1e25aa210a076a8daa5660e4b8ab37b74d854b1f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9D=D0=B8=D0=BA=D0=BE=D0=BD=D0=BE=D0=B2=20=D0=92=D0=B0?= =?UTF-8?q?=D1=81=D0=B8=D0=BB=D0=B8=D0=B9?= Date: Mon, 14 Feb 2022 01:15:13 +1100 Subject: [PATCH 1/2] Method of loading data from archive moved to Utils class. Re-write property supportedGameVersionsDisplay more shortly and optimized. Used var declaration where type is obvious. Property initialized in declaration. constructor ScrapMod change to private, but LoadFromFile change to public. LoadIcon moved to Utils and renamed to LoadImage. --- ScrapModLoader/ModsLauncher.cs | 4 +- ScrapModLoader/ScrapMod.cs | 152 ++++++++++++++------------------- ScrapModLoader/Utils.cs | 64 ++++++++++---- 3 files changed, 114 insertions(+), 106 deletions(-) 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; } } + From 90764c8636d9cc6a730e71d52c83815915735d94 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9D=D0=B8=D0=BA=D0=BE=D0=BD=D0=BE=D0=B2=20=D0=92=D0=B0?= =?UTF-8?q?=D1=81=D0=B8=D0=BB=D0=B8=D0=B9?= Date: Mon, 14 Feb 2022 09:46:59 +1100 Subject: [PATCH 2/2] do PR note --- ScrapModLoader/ModsLauncher.cs | 4 ++-- ScrapModLoader/ScrapMod.cs | 15 +++++++-------- ScrapModLoader/Utils.cs | 10 +++++----- 3 files changed, 14 insertions(+), 15 deletions(-) diff --git a/ScrapModLoader/ModsLauncher.cs b/ScrapModLoader/ModsLauncher.cs index 73e9c42..e4d6181 100644 --- a/ScrapModLoader/ModsLauncher.cs +++ b/ScrapModLoader/ModsLauncher.cs @@ -97,8 +97,8 @@ namespace ScrapModLoader if (!mod.IsEnabled(gamePath)) mod.Enable(gamePath, SelectedGameVersion); else - if (mod.IsEnabled(gamePath)) - mod.Disable(gamePath); + if (mod.IsEnabled(gamePath)) + mod.Disable(gamePath); } } } diff --git a/ScrapModLoader/ScrapMod.cs b/ScrapModLoader/ScrapMod.cs index c25f920..711d8cd 100644 --- a/ScrapModLoader/ScrapMod.cs +++ b/ScrapModLoader/ScrapMod.cs @@ -2,7 +2,6 @@ using System.Collections.Generic; using System.IO; using System.Windows.Media.Imaging; -using System.Xml; using Ionic.Zip; @@ -87,7 +86,7 @@ namespace ScrapModLoader gamePath += @"Mods\" + Name; Directory.CreateDirectory(gamePath); - using (var zipFile = ZipFile.Read(ModPath)) + using (ZipFile zipFile = ZipFile.Read(ModPath)) { foreach (ZipEntry zipEntry in zipFile) { @@ -102,28 +101,28 @@ namespace ScrapModLoader public static ScrapMod LoadFromFile(String path) { - using var zipFile = ZipFile.Read(path); + using ZipFile zipFile = ZipFile.Read(path); Byte[] iconBuffer = Utils.ExtractFromZip(zipFile, "icon.png"); Byte[] confBuffer = Utils.ExtractFromZip(zipFile, "config.toml"); - var mod = new ScrapMod() + ScrapMod mod = new ScrapMod() { ModPath = path, Icon = Utils.LoadImage(iconBuffer) }; - LoadConfig(ref mod, confBuffer); + LoadConfig(mod, confBuffer); return mod; } - private static void LoadConfig(ref ScrapMod mod, Byte[] buffer) + private static void LoadConfig(ScrapMod mod, Byte[] buffer) { - using var sourceStream = new MemoryStream(buffer); - using var reader = new StreamReader(sourceStream); + using MemoryStream sourceStream = new MemoryStream(buffer); + using StreamReader reader = new StreamReader(sourceStream); TomlTable config = TOML.Parse(reader); diff --git a/ScrapModLoader/Utils.cs b/ScrapModLoader/Utils.cs index 61bd172..43439e1 100644 --- a/ScrapModLoader/Utils.cs +++ b/ScrapModLoader/Utils.cs @@ -1,8 +1,8 @@ using System; using System.Collections.Generic; using System.Collections.Specialized; -using System.Windows.Media.Imaging; using System.IO; +using System.Windows.Media.Imaging; using Ionic.Zip; @@ -35,8 +35,8 @@ internal static class Utils 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)) + Byte[] buffer = new Byte[entry.UncompressedSize]; + using (MemoryStream zipStream = new MemoryStream(buffer)) entry.Extract(zipStream); return buffer; @@ -44,9 +44,9 @@ internal static class Utils public static BitmapImage LoadImage(Byte[] buffer) { - using var sourceStream = new MemoryStream(buffer); + using MemoryStream sourceStream = new MemoryStream(buffer); - var image = new BitmapImage(); + BitmapImage? image = new BitmapImage(); image.BeginInit(); image.CacheOption = BitmapCacheOption.OnLoad;