From 25c3fd04255663fd68b522ed5e5446bcda7ace74 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: Wed, 16 Feb 2022 00:30:28 +1100 Subject: [PATCH 1/2] refactor: modsLauncer and MainWindow refactor some of code of this classes --- ScrapModLoader/MainWindow.xaml | 8 ++--- ScrapModLoader/MainWindow.xaml.cs | 55 +++++++++++++++-------------- ScrapModLoader/ModsLauncher.cs | 57 ++++++++++++++----------------- ScrapModLoader/Utils.cs | 2 +- 4 files changed, 59 insertions(+), 63 deletions(-) diff --git a/ScrapModLoader/MainWindow.xaml b/ScrapModLoader/MainWindow.xaml index f6dcd08..411486c 100644 --- a/ScrapModLoader/MainWindow.xaml +++ b/ScrapModLoader/MainWindow.xaml @@ -18,7 +18,7 @@ - + @@ -28,7 +28,7 @@ - + @@ -71,8 +71,8 @@ - - + + diff --git a/ScrapModLoader/MainWindow.xaml.cs b/ScrapModLoader/MainWindow.xaml.cs index e027765..b130e23 100644 --- a/ScrapModLoader/MainWindow.xaml.cs +++ b/ScrapModLoader/MainWindow.xaml.cs @@ -28,8 +28,7 @@ namespace ScrapModLoader { if (Settings.Default.ModsPathes.Count == 0) { - String path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) - + Path.DirectorySeparatorChar + "Scrapland mods"; + String path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "Scrapland mods"); Settings.Default.ModsPathes.Add(path); Directory.CreateDirectory(path); } @@ -43,7 +42,8 @@ namespace ScrapModLoader if (!isFoundScrapland) { ButtonRunScrapland.IsEnabled = false; - MessageBox.Show("Error: unable to find Scrapland instalation. Please, specify yours game installation folder in settings."); + MessageBox.Show("Unable to find Scrapland instalation. Please, specify yours game installation folder in settings.", + "Warning", MessageBoxButton.OK, MessageBoxImage.Warning); } } catch (KeyNotFoundException ex) @@ -52,8 +52,8 @@ namespace ScrapModLoader } } - ((ComboBoxItem)ScraplandVersion.Items[0]).IsEnabled = modsLauncher.ScraplandPath != String.Empty; - ((ComboBoxItem)ScraplandVersion.Items[1]).IsEnabled = modsLauncher.ScraplandRemasteredPath != String.Empty; + OriginalVersionItem.IsEnabled = modsLauncher.ScraplandPath != String.Empty; + RemasteredVersionItem.IsEnabled = modsLauncher.ScraplandRemasteredPath != String.Empty; ScraplandVersion.SelectedIndex = modsLauncher.ScraplandRemasteredPath != String.Empty ? 1 : 0; @@ -62,18 +62,18 @@ namespace ScrapModLoader private void ModsList_Initialized(Object sender, EventArgs e) => ModsList.ItemsSource = modsLauncher.Mods; - private void ModsList_MouseDown(Object sender, System.Windows.Input.MouseButtonEventArgs e) + private void ModsList_MouseDown(Object sender, MouseButtonEventArgs e) { - if (MainGrid.ColumnDefinitions[2].Width.Value != 0) + if (PreviewColumn.Width.Value != 0) { - gridLength = MainGrid.ColumnDefinitions[2].Width; - MainGrid.ColumnDefinitions[2].Width = new GridLength(0, GridUnitType.Star); + gridLength = PreviewColumn.Width; + PreviewColumn.Width = new GridLength(0, GridUnitType.Star); } } private void ListViewItem_PreviewMouseLeftButtonDown(Object sender, MouseButtonEventArgs e) { - MainGrid.ColumnDefinitions[2].Width = gridLength; + PreviewColumn.Width = gridLength; if (sender is ListViewItem item) { @@ -81,7 +81,7 @@ namespace ScrapModLoader if (selectedModName == null) throw new KeyNotFoundException(nameof(selectedModName)); - ScrapMod ? mod = modsLauncher.Mods.Find(mod => mod.Name == selectedModName); + ScrapMod? mod = modsLauncher.Mods.Find(mod => mod.Name == selectedModName); if (mod == null) throw new KeyNotFoundException(nameof(mod)); @@ -99,34 +99,35 @@ namespace ScrapModLoader private void WriteModInfo(ScrapMod mod) { ModInfo.Document.Blocks.Clear(); - Paragraph parahraph = new Paragraph(); + Paragraph paragraph = new Paragraph(); - parahraph.Inlines.Add(new Bold(new Run("Description:\n"))); - parahraph.Inlines.Add(new Run(mod.Description)); + paragraph.Inlines.Add(new Bold(new Run("Description:\n"))); + paragraph.Inlines.Add(new Run(mod.Description)); - parahraph.Inlines.Add(new Bold(new Run("\n\nAuthors:\n"))); + paragraph.Inlines.Add(new Bold(new Run("\n\nAuthors:\n"))); foreach (String autor in mod.Authors) - parahraph.Inlines.Add(new Run(autor + "\n")); + paragraph.Inlines.Add(new Run(autor + "\n")); - ModInfo.Document.Blocks.Add(parahraph); + ModInfo.Document.Blocks.Add(paragraph); - ModCreditsTab.Visibility = Visibility.Visible; if (mod.Credits.Count == 0) ModCreditsTab.Visibility = Visibility.Hidden; else { + ModCreditsTab.Visibility = Visibility.Visible; + ModCredits.Document.Blocks.Clear(); - parahraph = new Paragraph(); + paragraph = new Paragraph(); foreach (KeyValuePair> credit in mod.Credits) { - parahraph.Inlines.Add(new Bold(new Run(credit.Key + "\n"))); + paragraph.Inlines.Add(new Bold(new Run(credit.Key + "\n"))); foreach (String autor in credit.Value) - parahraph.Inlines.Add(new Run(autor + "\n")); - parahraph.Inlines.Add(new Run("\n")); + paragraph.Inlines.Add(new Run(autor + "\n")); + paragraph.Inlines.Add(new Run("\n")); } - ModCredits.Document.Blocks.Add(parahraph); + ModCredits.Document.Blocks.Add(paragraph); } } @@ -139,6 +140,8 @@ namespace ScrapModLoader throw new NullReferenceException(nameof(isChecked)); StackPanel parent = (StackPanel)checkbox.Parent; + // TODO: replace by find template + // https://docs.microsoft.com/ru-ru/dotnet/desktop/wpf/data/how-to-find-datatemplate-generated-elements?view=netframeworkdesktop-4.8 Label label = (Label)parent.Children[2]; String? selectedModName = label.Content.ToString(); @@ -162,8 +165,8 @@ namespace ScrapModLoader modsLauncher.ScanMods(); ModsList.Items.Refresh(); - ((ComboBoxItem)ScraplandVersion.Items[0]).IsEnabled = modsLauncher.ScraplandPath != String.Empty; - ((ComboBoxItem)ScraplandVersion.Items[1]).IsEnabled = modsLauncher.ScraplandRemasteredPath != String.Empty; + OriginalVersionItem.IsEnabled = modsLauncher.ScraplandPath != String.Empty; + RemasteredVersionItem.IsEnabled = modsLauncher.ScraplandRemasteredPath != String.Empty; ScraplandVersion.SelectedIndex = modsLauncher.ScraplandRemasteredPath != String.Empty ? 1 : 0; } @@ -178,7 +181,7 @@ namespace ScrapModLoader String gamePath = modsLauncher.SelectedGameVersion == "1.0" ? modsLauncher.ScraplandPath : modsLauncher.ScraplandRemasteredPath; - Process.Start(gamePath + @"\bin\Scrap.exe", args); + Process.Start(Path.Combine(gamePath, @"\bin\Scrap.exe"), args); if (CloseLauncher.IsChecked ?? false) Close(); diff --git a/ScrapModLoader/ModsLauncher.cs b/ScrapModLoader/ModsLauncher.cs index e966c78..35a444f 100644 --- a/ScrapModLoader/ModsLauncher.cs +++ b/ScrapModLoader/ModsLauncher.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Globalization; using System.IO; +using System.Linq; using Ionic.Zip; @@ -11,23 +12,17 @@ namespace ScrapModLoader { public class ModsLauncher { - public List Mods { get; private set; } - public List ModsPathes { get; set; } - public String ScraplandPath { get; set; } - public String ScraplandRemasteredPath { get; set; } - public String SelectedGameVersion { get; set; } - public String LauncherVersion { get; set; } - public String SelectedGamePath { get; set; } + public List Mods { get; private set; } = new List(); + public List ModsPathes { get; set; } = Utils.StringCollectionToList(Settings.Default.ModsPathes); + public String ScraplandPath { get; set; } = Settings.Default.ScraplandPath; + public String ScraplandRemasteredPath { get; set; } = Settings.Default.ScraplandRemasteredPath; + public String SelectedGameVersion { get; set; } = "0.0"; + public String LauncherVersion { get; set; } = "0.3"; + public String SelectedGamePath { get; set; } = String.Empty; public ModsLauncher() { - Mods = new List(); - ModsPathes = Utils.StringCollectionToList(Settings.Default.ModsPathes); - ScraplandPath = Settings.Default.ScraplandPath; - ScraplandRemasteredPath = Settings.Default.ScraplandRemasteredPath; - SelectedGameVersion = "0.0"; - LauncherVersion = "0.3"; - SelectedGamePath = String.Empty; + } public void ScanMods() @@ -60,20 +55,20 @@ namespace ScrapModLoader if (key == null) continue; - foreach (String subkey_name in key.GetSubKeyNames()) + foreach (String subKeyName in key.GetSubKeyNames()) { - using RegistryKey? subkey = key.OpenSubKey(subkey_name); - if (subkey == null) + using RegistryKey? subKey = key.OpenSubKey(subKeyName); + if (subKey == null) continue; - String? displayName = subkey.GetValue("DisplayName")?.ToString(); + String? displayName = subKey.GetValue("DisplayName")?.ToString(); if (displayName == null) continue; if (displayName == "Scrapland" || displayName == "American McGee presents Scrapland") { - ScraplandPath = subkey.GetValue("InstallLocation")?.ToString() ?? ""; - if (ScraplandPath == null || ScraplandPath == String.Empty) + ScraplandPath = subKey.GetValue("InstallLocation")?.ToString() ?? String.Empty; + if (String.IsNullOrEmpty(ScraplandPath)) throw new KeyNotFoundException("Installed Scrapland found, but unable to locate the instalation folder"); Settings.Default.ScraplandPath = ScraplandPath; @@ -82,8 +77,8 @@ namespace ScrapModLoader if (displayName == "Scrapland Remastered") { - ScraplandRemasteredPath = subkey.GetValue("InstallLocation")?.ToString() ?? ""; - if (ScraplandRemasteredPath == null || ScraplandRemasteredPath == String.Empty) + ScraplandRemasteredPath = subKey.GetValue("InstallLocation")?.ToString() ?? String.Empty; + if (String.IsNullOrEmpty(ScraplandRemasteredPath)) throw new KeyNotFoundException("Installed Scrapland Remastered found, but unable to locate the instalation folder"); Settings.Default.ScraplandRemasteredPath = ScraplandRemasteredPath; @@ -119,18 +114,17 @@ namespace ScrapModLoader } } + private String ModPath(ScrapMod mod) => Path.Combine(SelectedGamePath, "Mod", mod.Name); + public Boolean IsLoaded(ScrapMod mod) => - Directory.Exists(SelectedGamePath + @"Mods\" + mod.Name); + Directory.Exists(ModPath(mod)); 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; + return Directory.EnumerateFiles(ModPath(mod), "*.disabled", SearchOption.AllDirectories).FirstOrDefault() == null; } public void Enable(ScrapMod mod) @@ -141,7 +135,7 @@ namespace ScrapModLoader if (IsEnabled(mod)) return; - foreach (String file in Directory.EnumerateFiles(SelectedGamePath + @"Mods\" + mod.Name, "*.disabled", SearchOption.AllDirectories)) + foreach (String file in Directory.EnumerateFiles(ModPath(mod), "*.disabled", SearchOption.AllDirectories)) File.Move(file, Path.ChangeExtension(file, null)); } @@ -150,14 +144,13 @@ namespace ScrapModLoader if (!IsEnabled(mod)) return; - foreach (String file in Directory.EnumerateFiles(SelectedGamePath + @"Mods\" + mod.Name, "*.packed", SearchOption.AllDirectories)) + foreach (String file in Directory.EnumerateFiles(ModPath(mod), "*.packed", SearchOption.AllDirectories)) File.Move(file, file + ".disabled"); } private void LoadModToGame(ScrapMod mod) { - String modPath = SelectedGamePath + @"Mods\" + mod.Name; - Directory.CreateDirectory(modPath); + Directory.CreateDirectory(ModPath(mod)); using (ZipFile zipFile = ZipFile.Read(mod.ModPath)) { @@ -167,7 +160,7 @@ namespace ScrapModLoader continue; if (Path.GetExtension(zipEntry.FileName) == ".packed") - zipEntry.Extract(modPath); + zipEntry.Extract(ModPath(mod)); } } } diff --git a/ScrapModLoader/Utils.cs b/ScrapModLoader/Utils.cs index 43439e1..fdab93c 100644 --- a/ScrapModLoader/Utils.cs +++ b/ScrapModLoader/Utils.cs @@ -13,7 +13,7 @@ internal static class Utils { public static String GetFolderDialog() { - using System.Windows.Forms.FolderBrowserDialog? dialog = new System.Windows.Forms.FolderBrowserDialog(); + using System.Windows.Forms.FolderBrowserDialog dialog = new System.Windows.Forms.FolderBrowserDialog(); System.Windows.Forms.DialogResult result = dialog.ShowDialog(); return dialog.SelectedPath; } From deffd055817f1187d875640f6db4ccfb7ce6b44f Mon Sep 17 00:00:00 2001 From: strongleong Date: Wed, 16 Feb 2022 19:52:38 +1100 Subject: [PATCH 2/2] Bug fixes --- ScrapModLoader/MainWindow.xaml.cs | 2 +- ScrapModLoader/ModsLauncher.cs | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/ScrapModLoader/MainWindow.xaml.cs b/ScrapModLoader/MainWindow.xaml.cs index b130e23..d85297d 100644 --- a/ScrapModLoader/MainWindow.xaml.cs +++ b/ScrapModLoader/MainWindow.xaml.cs @@ -181,7 +181,7 @@ namespace ScrapModLoader String gamePath = modsLauncher.SelectedGameVersion == "1.0" ? modsLauncher.ScraplandPath : modsLauncher.ScraplandRemasteredPath; - Process.Start(Path.Combine(gamePath, @"\bin\Scrap.exe"), args); + Process.Start(Path.Combine(gamePath, @"bin\Scrap.exe"), args); if (CloseLauncher.IsChecked ?? false) Close(); diff --git a/ScrapModLoader/ModsLauncher.cs b/ScrapModLoader/ModsLauncher.cs index 35a444f..8afcaaf 100644 --- a/ScrapModLoader/ModsLauncher.cs +++ b/ScrapModLoader/ModsLauncher.cs @@ -114,7 +114,8 @@ namespace ScrapModLoader } } - private String ModPath(ScrapMod mod) => Path.Combine(SelectedGamePath, "Mod", mod.Name); + private String ModPath(ScrapMod mod) => + Path.Combine(SelectedGamePath, "Mods", mod.Name); public Boolean IsLoaded(ScrapMod mod) => Directory.Exists(ModPath(mod));