Merge pull request #4 from Grumgog/refactor-modLauncher

refactor: modsLauncer and MainWindow
This commit is contained in:
Strongleong 2022-02-16 19:54:54 +11:00 committed by GitHub
commit 4f80b72603
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 60 additions and 63 deletions

View File

@ -18,7 +18,7 @@
<Grid.ColumnDefinitions> <Grid.ColumnDefinitions>
<ColumnDefinition Width="2*" /> <ColumnDefinition Width="2*" />
<ColumnDefinition Width="5" /> <ColumnDefinition Width="5" />
<ColumnDefinition Width="0*" /> <ColumnDefinition Name="PreviewColumn" Width="0*" />
</Grid.ColumnDefinitions> </Grid.ColumnDefinitions>
<ListView d:ItemsSource="{d:SampleData ItemCount=5}" Name="ModsList" Initialized="ModsList_Initialized" MouseDown="ModsList_MouseDown"> <ListView d:ItemsSource="{d:SampleData ItemCount=5}" Name="ModsList" Initialized="ModsList_Initialized" MouseDown="ModsList_MouseDown">
<ListView.View> <ListView.View>
@ -28,7 +28,7 @@
<DataTemplate> <DataTemplate>
<StackPanel Orientation="Horizontal" VerticalAlignment="Stretch" HorizontalAlignment="Left"> <StackPanel Orientation="Horizontal" VerticalAlignment="Stretch" HorizontalAlignment="Left">
<CheckBox IsChecked="{Binding Checked}" VerticalAlignment="Center" HorizontalAlignment="Center" Checked="CheckBox_Checked" /> <CheckBox IsChecked="{Binding Checked}" VerticalAlignment="Center" HorizontalAlignment="Center" Checked="CheckBox_Checked" />
<Image Source="{Binding Icon}" Stretch="Fill" Width="16" Height="16" /> <Image Source="{Binding Icon}" Stretch="Fill" Width="16" Height="16" />
<Label Content="{Binding Name}" HorizontalAlignment="Stretch" VerticalAlignment="Center" /> <Label Content="{Binding Name}" HorizontalAlignment="Stretch" VerticalAlignment="Center" />
</StackPanel> </StackPanel>
</DataTemplate> </DataTemplate>
@ -71,8 +71,8 @@
</StackPanel> </StackPanel>
<StackPanel Grid.Column="2" Orientation="Horizontal" HorizontalAlignment="Left"> <StackPanel Grid.Column="2" Orientation="Horizontal" HorizontalAlignment="Left">
<ComboBox Name="ScraplandVersion" Margin="0,0,10,0" SelectionChanged="ScraplandVersion_SelectionChanged"> <ComboBox Name="ScraplandVersion" Margin="0,0,10,0" SelectionChanged="ScraplandVersion_SelectionChanged">
<ComboBoxItem Content="Original" IsEnabled="False" /> <ComboBoxItem Name="OriginalVersionItem" Content="Original" IsEnabled="False" />
<ComboBoxItem Content="Remastered" /> <ComboBoxItem Name ="RemasteredVersionItem" Content="Remastered" />
</ComboBox> </ComboBox>
<CheckBox Name="Windowed" Content=" Windowed " Margin="0,0,10,0" HorizontalAlignment="Center" VerticalAlignment="Center" /> <CheckBox Name="Windowed" Content=" Windowed " Margin="0,0,10,0" HorizontalAlignment="Center" VerticalAlignment="Center" />
<CheckBox Name="CloseLauncher" Content=" Close launcher " Margin="0,0,10,0" HorizontalAlignment="Center" VerticalAlignment="Center" /> <CheckBox Name="CloseLauncher" Content=" Close launcher " Margin="0,0,10,0" HorizontalAlignment="Center" VerticalAlignment="Center" />

View File

@ -28,8 +28,7 @@ namespace ScrapModLoader
{ {
if (Settings.Default.ModsPathes.Count == 0) if (Settings.Default.ModsPathes.Count == 0)
{ {
String path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) String path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "Scrapland mods");
+ Path.DirectorySeparatorChar + "Scrapland mods";
Settings.Default.ModsPathes.Add(path); Settings.Default.ModsPathes.Add(path);
Directory.CreateDirectory(path); Directory.CreateDirectory(path);
} }
@ -43,7 +42,8 @@ namespace ScrapModLoader
if (!isFoundScrapland) if (!isFoundScrapland)
{ {
ButtonRunScrapland.IsEnabled = false; 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) catch (KeyNotFoundException ex)
@ -52,8 +52,8 @@ namespace ScrapModLoader
} }
} }
((ComboBoxItem)ScraplandVersion.Items[0]).IsEnabled = modsLauncher.ScraplandPath != String.Empty; OriginalVersionItem.IsEnabled = modsLauncher.ScraplandPath != String.Empty;
((ComboBoxItem)ScraplandVersion.Items[1]).IsEnabled = modsLauncher.ScraplandRemasteredPath != String.Empty; RemasteredVersionItem.IsEnabled = modsLauncher.ScraplandRemasteredPath != String.Empty;
ScraplandVersion.SelectedIndex = modsLauncher.ScraplandRemasteredPath != String.Empty ? 1 : 0; 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_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; gridLength = PreviewColumn.Width;
MainGrid.ColumnDefinitions[2].Width = new GridLength(0, GridUnitType.Star); PreviewColumn.Width = new GridLength(0, GridUnitType.Star);
} }
} }
private void ListViewItem_PreviewMouseLeftButtonDown(Object sender, MouseButtonEventArgs e) private void ListViewItem_PreviewMouseLeftButtonDown(Object sender, MouseButtonEventArgs e)
{ {
MainGrid.ColumnDefinitions[2].Width = gridLength; PreviewColumn.Width = gridLength;
if (sender is ListViewItem item) if (sender is ListViewItem item)
{ {
@ -81,7 +81,7 @@ namespace ScrapModLoader
if (selectedModName == null) if (selectedModName == null)
throw new KeyNotFoundException(nameof(selectedModName)); 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) if (mod == null)
throw new KeyNotFoundException(nameof(mod)); throw new KeyNotFoundException(nameof(mod));
@ -99,34 +99,35 @@ namespace ScrapModLoader
private void WriteModInfo(ScrapMod mod) private void WriteModInfo(ScrapMod mod)
{ {
ModInfo.Document.Blocks.Clear(); ModInfo.Document.Blocks.Clear();
Paragraph parahraph = new Paragraph(); Paragraph paragraph = new Paragraph();
parahraph.Inlines.Add(new Bold(new Run("Description:\n"))); paragraph.Inlines.Add(new Bold(new Run("Description:\n")));
parahraph.Inlines.Add(new Run(mod.Description)); 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) 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) if (mod.Credits.Count == 0)
ModCreditsTab.Visibility = Visibility.Hidden; ModCreditsTab.Visibility = Visibility.Hidden;
else else
{ {
ModCreditsTab.Visibility = Visibility.Visible;
ModCredits.Document.Blocks.Clear(); ModCredits.Document.Blocks.Clear();
parahraph = new Paragraph(); paragraph = new Paragraph();
foreach (KeyValuePair<String, List<String>> credit in mod.Credits) foreach (KeyValuePair<String, List<String>> 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) foreach (String autor in credit.Value)
parahraph.Inlines.Add(new Run(autor + "\n")); paragraph.Inlines.Add(new Run(autor + "\n"));
parahraph.Inlines.Add(new Run("\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)); throw new NullReferenceException(nameof(isChecked));
StackPanel parent = (StackPanel)checkbox.Parent; 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]; Label label = (Label)parent.Children[2];
String? selectedModName = label.Content.ToString(); String? selectedModName = label.Content.ToString();
@ -162,8 +165,8 @@ namespace ScrapModLoader
modsLauncher.ScanMods(); modsLauncher.ScanMods();
ModsList.Items.Refresh(); ModsList.Items.Refresh();
((ComboBoxItem)ScraplandVersion.Items[0]).IsEnabled = modsLauncher.ScraplandPath != String.Empty; OriginalVersionItem.IsEnabled = modsLauncher.ScraplandPath != String.Empty;
((ComboBoxItem)ScraplandVersion.Items[1]).IsEnabled = modsLauncher.ScraplandRemasteredPath != String.Empty; RemasteredVersionItem.IsEnabled = modsLauncher.ScraplandRemasteredPath != String.Empty;
ScraplandVersion.SelectedIndex = modsLauncher.ScraplandRemasteredPath != String.Empty ? 1 : 0; ScraplandVersion.SelectedIndex = modsLauncher.ScraplandRemasteredPath != String.Empty ? 1 : 0;
} }
@ -178,7 +181,7 @@ namespace ScrapModLoader
String gamePath = modsLauncher.SelectedGameVersion == "1.0" ? String gamePath = modsLauncher.SelectedGameVersion == "1.0" ?
modsLauncher.ScraplandPath : modsLauncher.ScraplandRemasteredPath; modsLauncher.ScraplandPath : modsLauncher.ScraplandRemasteredPath;
Process.Start(gamePath + @"\bin\Scrap.exe", args); Process.Start(Path.Combine(gamePath, @"bin\Scrap.exe"), args);
if (CloseLauncher.IsChecked ?? false) if (CloseLauncher.IsChecked ?? false)
Close(); Close();

View File

@ -2,6 +2,7 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Globalization; using System.Globalization;
using System.IO; using System.IO;
using System.Linq;
using Ionic.Zip; using Ionic.Zip;
@ -11,23 +12,17 @@ namespace ScrapModLoader
{ {
public class ModsLauncher public class ModsLauncher
{ {
public List<ScrapMod> Mods { get; private set; } public List<ScrapMod> Mods { get; private set; } = new List<ScrapMod>();
public List<String> ModsPathes { get; set; } public List<String> ModsPathes { get; set; } = Utils.StringCollectionToList(Settings.Default.ModsPathes);
public String ScraplandPath { get; set; } public String ScraplandPath { get; set; } = Settings.Default.ScraplandPath;
public String ScraplandRemasteredPath { get; set; } public String ScraplandRemasteredPath { get; set; } = Settings.Default.ScraplandRemasteredPath;
public String SelectedGameVersion { get; set; } public String SelectedGameVersion { get; set; } = "0.0";
public String LauncherVersion { get; set; } public String LauncherVersion { get; set; } = "0.3";
public String SelectedGamePath { get; set; } public String SelectedGamePath { get; set; } = String.Empty;
public ModsLauncher() public ModsLauncher()
{ {
Mods = new List<ScrapMod>();
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() public void ScanMods()
@ -60,20 +55,20 @@ namespace ScrapModLoader
if (key == null) if (key == null)
continue; continue;
foreach (String subkey_name in key.GetSubKeyNames()) foreach (String subKeyName in key.GetSubKeyNames())
{ {
using RegistryKey? subkey = key.OpenSubKey(subkey_name); using RegistryKey? subKey = key.OpenSubKey(subKeyName);
if (subkey == null) if (subKey == null)
continue; continue;
String? displayName = subkey.GetValue("DisplayName")?.ToString(); String? displayName = subKey.GetValue("DisplayName")?.ToString();
if (displayName == null) if (displayName == null)
continue; continue;
if (displayName == "Scrapland" || displayName == "American McGee presents Scrapland") if (displayName == "Scrapland" || displayName == "American McGee presents Scrapland")
{ {
ScraplandPath = subkey.GetValue("InstallLocation")?.ToString() ?? ""; ScraplandPath = subKey.GetValue("InstallLocation")?.ToString() ?? String.Empty;
if (ScraplandPath == null || ScraplandPath == String.Empty) if (String.IsNullOrEmpty(ScraplandPath))
throw new KeyNotFoundException("Installed Scrapland found, but unable to locate the instalation folder"); throw new KeyNotFoundException("Installed Scrapland found, but unable to locate the instalation folder");
Settings.Default.ScraplandPath = ScraplandPath; Settings.Default.ScraplandPath = ScraplandPath;
@ -82,8 +77,8 @@ namespace ScrapModLoader
if (displayName == "Scrapland Remastered") if (displayName == "Scrapland Remastered")
{ {
ScraplandRemasteredPath = subkey.GetValue("InstallLocation")?.ToString() ?? ""; ScraplandRemasteredPath = subKey.GetValue("InstallLocation")?.ToString() ?? String.Empty;
if (ScraplandRemasteredPath == null || ScraplandRemasteredPath == String.Empty) if (String.IsNullOrEmpty(ScraplandRemasteredPath))
throw new KeyNotFoundException("Installed Scrapland Remastered found, but unable to locate the instalation folder"); throw new KeyNotFoundException("Installed Scrapland Remastered found, but unable to locate the instalation folder");
Settings.Default.ScraplandRemasteredPath = ScraplandRemasteredPath; Settings.Default.ScraplandRemasteredPath = ScraplandRemasteredPath;
@ -119,18 +114,18 @@ namespace ScrapModLoader
} }
} }
private String ModPath(ScrapMod mod) =>
Path.Combine(SelectedGamePath, "Mods", mod.Name);
public Boolean IsLoaded(ScrapMod mod) => public Boolean IsLoaded(ScrapMod mod) =>
Directory.Exists(SelectedGamePath + @"Mods\" + mod.Name); Directory.Exists(ModPath(mod));
public Boolean IsEnabled(ScrapMod mod) public Boolean IsEnabled(ScrapMod mod)
{ {
if (!IsLoaded(mod)) if (!IsLoaded(mod))
return false; return false;
foreach (String file in Directory.EnumerateFiles(SelectedGamePath + @"Mods\" + mod.Name, "*.disabled", SearchOption.AllDirectories)) return Directory.EnumerateFiles(ModPath(mod), "*.disabled", SearchOption.AllDirectories).FirstOrDefault() == null;
return false;
return true;
} }
public void Enable(ScrapMod mod) public void Enable(ScrapMod mod)
@ -141,7 +136,7 @@ namespace ScrapModLoader
if (IsEnabled(mod)) if (IsEnabled(mod))
return; 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)); File.Move(file, Path.ChangeExtension(file, null));
} }
@ -150,14 +145,13 @@ namespace ScrapModLoader
if (!IsEnabled(mod)) if (!IsEnabled(mod))
return; 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"); File.Move(file, file + ".disabled");
} }
private void LoadModToGame(ScrapMod mod) private void LoadModToGame(ScrapMod mod)
{ {
String modPath = SelectedGamePath + @"Mods\" + mod.Name; Directory.CreateDirectory(ModPath(mod));
Directory.CreateDirectory(modPath);
using (ZipFile zipFile = ZipFile.Read(mod.ModPath)) using (ZipFile zipFile = ZipFile.Read(mod.ModPath))
{ {
@ -167,7 +161,7 @@ namespace ScrapModLoader
continue; continue;
if (Path.GetExtension(zipEntry.FileName) == ".packed") if (Path.GetExtension(zipEntry.FileName) == ".packed")
zipEntry.Extract(modPath); zipEntry.Extract(ModPath(mod));
} }
} }
} }

View File

@ -13,7 +13,7 @@ internal static class Utils
{ {
public static String GetFolderDialog() 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(); System.Windows.Forms.DialogResult result = dialog.ShowDialog();
return dialog.SelectedPath; return dialog.SelectedPath;
} }