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>
<ColumnDefinition Width="2*" />
<ColumnDefinition Width="5" />
<ColumnDefinition Width="0*" />
<ColumnDefinition Name="PreviewColumn" Width="0*" />
</Grid.ColumnDefinitions>
<ListView d:ItemsSource="{d:SampleData ItemCount=5}" Name="ModsList" Initialized="ModsList_Initialized" MouseDown="ModsList_MouseDown">
<ListView.View>
@ -28,7 +28,7 @@
<DataTemplate>
<StackPanel Orientation="Horizontal" VerticalAlignment="Stretch" HorizontalAlignment="Left">
<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" />
</StackPanel>
</DataTemplate>
@ -71,8 +71,8 @@
</StackPanel>
<StackPanel Grid.Column="2" Orientation="Horizontal" HorizontalAlignment="Left">
<ComboBox Name="ScraplandVersion" Margin="0,0,10,0" SelectionChanged="ScraplandVersion_SelectionChanged">
<ComboBoxItem Content="Original" IsEnabled="False" />
<ComboBoxItem Content="Remastered" />
<ComboBoxItem Name="OriginalVersionItem" Content="Original" IsEnabled="False" />
<ComboBoxItem Name ="RemasteredVersionItem" Content="Remastered" />
</ComboBox>
<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" />

View File

@ -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<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)
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();

View File

@ -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<ScrapMod> Mods { get; private set; }
public List<String> 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<ScrapMod> Mods { get; private set; } = new List<ScrapMod>();
public List<String> 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<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()
@ -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,18 @@ namespace ScrapModLoader
}
}
private String ModPath(ScrapMod mod) =>
Path.Combine(SelectedGamePath, "Mods", 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 +136,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 +145,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 +161,7 @@ namespace ScrapModLoader
continue;
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()
{
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;
}