Skip to content

Commit 044bcf5

Browse files
authored
Merge pull request #127 from ModShardTeam/ImprovingPerformance
Improving performance
2 parents 2703478 + 57d9b76 commit 044bcf5

35 files changed

Lines changed: 623 additions & 1214 deletions

CodeResources.cs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Linq;
5+
using System.Reflection;
6+
using Serilog;
7+
8+
namespace ModShardLauncher
9+
{
10+
internal static class CodeResources
11+
{
12+
private static readonly Lazy<Dictionary<string, string>> _scripts = new(LoadAllScripts);
13+
public static string GetGML(string name)
14+
{
15+
if (_scripts.Value.TryGetValue(name, out var script)) return script;
16+
17+
throw new ArgumentException($"GML script '{name}' not found");
18+
}
19+
private static Dictionary<string, string> LoadAllScripts()
20+
{
21+
Dictionary<string, string> scripts = new();
22+
Assembly assembly = Assembly.GetExecutingAssembly();
23+
24+
// Load all .gml resources
25+
IEnumerable<string> resourceNames = assembly.GetManifestResourceNames()
26+
.Where(name => name.EndsWith(".gml"));
27+
foreach (string resourceName in resourceNames)
28+
{
29+
string? scriptName = Path.GetFileNameWithoutExtension(resourceName);
30+
31+
using Stream? stream =
32+
assembly.GetManifestResourceStream(resourceName) ??
33+
throw new FileNotFoundException($"GML script '{scriptName}' not found");
34+
using StreamReader reader = new(stream);
35+
scripts[scriptName.Split('.').Last()] = reader.ReadToEnd();
36+
}
37+
38+
return scripts;
39+
}
40+
}
41+
}

Controls/GeneralPage.xaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@
292292
</ComboBox>
293293
<Label Content="{DynamicResource SettingHeader_Log}" VerticalAlignment="Top" Margin="32,100,0,0"
294294
Style="{StaticResource xFont}" FontSize="12" Foreground="#95796A"/>
295-
<CheckBox HorizontalAlignment="Left" Margin="160,102,0,0" VerticalAlignment="Top" IsChecked="{Binding isEnabled, Mode=TwoWay}" Name="Logger"
295+
<CheckBox HorizontalAlignment="Left" Margin="160,102,0,0" VerticalAlignment="Top" IsChecked="{Binding Enabled, Mode=TwoWay}" Name="Logger"
296296
Checked="Logger_Checked" Unchecked="Logger_Checked">
297297
<CheckBox.Style>
298298
<Style TargetType="CheckBox">

Controls/ModBar.xaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
<GridSplitter Width="2" Height="48" HorizontalAlignment="Center" VerticalAlignment="Bottom"
5252
Margin="12" Background="#33323F"/>
5353
<CheckBox HorizontalAlignment="Right" Margin="0,32,128,0" Content="{DynamicResource Enable}"
54-
FontFamily="{StaticResource ssFont}" Foreground="#FFFFFF" IsChecked="{Binding isEnabled, Mode=TwoWay}">
54+
FontFamily="{StaticResource ssFont}" Foreground="#FFFFFF" IsChecked="{Binding Enabled, Mode=TwoWay}">
5555
<CheckBox.Style>
5656
<Style TargetType="CheckBox">
5757
<Setter Property="IsChecked" Value="False"/>

Controls/ModInfos.xaml.cs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Diagnostics;
34
using System.IO;
5+
using System.Linq;
46
using System.Threading.Tasks;
57
using System.Windows;
68
using System.Windows.Controls;
@@ -36,23 +38,26 @@ private async void Save_Click(object sender, EventArgs e)
3638

3739
bool patchSucess = false;
3840

39-
try
41+
Stopwatch watch = Stopwatch.StartNew();
42+
try
4043
{
4144
ModLoader.PatchFile();
45+
long elapsedMs = watch.ElapsedMilliseconds;
46+
Main.Instance.LogModListStatus();
47+
Log.Information("Patching lasts {{{0}}} ms", elapsedMs);
4248
Log.Information("Successfully patch vanilla");
4349
patchSucess = true;
44-
Main.Instance.LogModList();
4550
}
46-
catch(Exception ex)
51+
catch (Exception ex)
4752
{
48-
Main.Instance.LogModList();
53+
Main.Instance.LogModListStatus();
4954
Log.Error(ex, "Something went wrong");
5055
Log.Information("Failed patching vanilla");
5156
MessageBox.Show(ex.ToString(), Application.Current.FindResource("SaveDataWarning").ToString());
5257
}
5358

5459
// attempt to save the patched data
55-
if (patchSucess)
60+
if (patchSucess)
5661
{
5762
Task<bool> save = DataLoader.DoSaveDialog();
5863
await save;

Controls/ModSourceInfos.xaml

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,6 @@
1919
<ImageBrush x:Key="OverSource" ImageSource="/Resources/Sprites/open_over.png"/>
2020
</controls:MyToggleButton.Resources>
2121
</controls:MyToggleButton>
22-
<controls:MyToggleButton ImageSource="/Resources/Sprites/save.png" Click="Save_Click" ToolTip="Save Data File"
23-
Margin="64,0,0,0">
24-
<controls:MyToggleButton.Resources>
25-
<ImageBrush x:Key="DownSource" ImageSource="/Resources/Sprites/save_down.png"/>
26-
<ImageBrush x:Key="OverSource" ImageSource="/Resources/Sprites/save_over.png"/>
27-
</controls:MyToggleButton.Resources>
28-
</controls:MyToggleButton>
2922
<Border BorderBrush="#43424D" BorderThickness="2"/>
3023
</Grid>
3124
<controls:MyItemsControl MaxHeight="672" Height="672" x:Name="SourceList"

Controls/ModSourceInfos.xaml.cs

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -23,33 +23,5 @@ private async void Open_Click(object sender, EventArgs e)
2323
await DataLoader.DoOpenDialog();
2424
Main.Instance.Refresh();
2525
}
26-
private async void Save_Click(object sender, EventArgs e)
27-
{
28-
if (DataLoader.data.FORM == null)
29-
{
30-
MessageBox.Show(Application.Current.FindResource("LoadDataWarning").ToString());
31-
return;
32-
}
33-
34-
bool patchSucess = false;
35-
36-
try
37-
{
38-
ModLoader.PatchFile();
39-
Log.Information("Successfully patch vanilla");
40-
patchSucess = true;
41-
Main.Instance.LogModList();
42-
}
43-
catch(Exception ex)
44-
{
45-
Main.Instance.LogModList();
46-
Log.Error(ex, "Something went wrong");
47-
Log.Information("Failed patching vanilla");
48-
MessageBox.Show(ex.ToString(), Application.Current.FindResource("SaveDataWarning").ToString());
49-
}
50-
51-
if (patchSucess) await DataLoader.DoSaveDialog();
52-
Main.Instance.Refresh();
53-
}
5426
}
5527
}

Extensions/ModShard.cs

Lines changed: 0 additions & 67 deletions
This file was deleted.

FileReader.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public class ModSource
2424
{
2525
public string Name { get; set; }
2626
public string Path { get; set; }
27-
public bool isExisted => File.Exists(Path);
27+
public bool Existed => File.Exists(Path);
2828
public override string ToString()
2929
{
3030
return Name;
@@ -46,17 +46,17 @@ public class ModFile
4646
public FileStream Stream;
4747
public string Path;
4848
public Mod instance { get; set; }
49-
public bool isEnabled { get; set; }
49+
public bool Enabled { get; set; }
5050
public PatchStatus PatchStatus { get; set; } = PatchStatus.None;
51-
public bool isExisted => File.Exists(Path);
51+
public bool Existed => File.Exists(Path);
5252
public byte[] Icon { get; set; } = Array.Empty<byte>();
5353
public override string ToString()
5454
{
5555
return instance.ToString();
5656
}
5757
public byte[] GetFile(string fileName)
5858
{
59-
if(!isExisted)
59+
if(!Existed)
6060
{
6161
MessageBox.Show(Application.Current.FindResource("ModLostWarning").ToString() + " : " + Name);
6262
ModLoader.LoadFiles();

Main.xaml.cs

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -131,27 +131,31 @@ private void SetInitialSize()
131131
}
132132
}
133133

134-
public void LogModList()
134+
public void LogModListStatus()
135135
{
136-
foreach (ModFile modFile in ModPage.Mods.Where(x => x.isEnabled))
136+
foreach (ModFile modFile in ModPage.Mods.Where(x => x.Enabled))
137137
{
138-
string statusMessage = "";
139-
switch (modFile.PatchStatus)
140-
{
141-
case PatchStatus.Patching:
142-
statusMessage = "Patching failed";
143-
break;
138+
LogModStatus(modFile);
139+
}
140+
}
141+
public static void LogModStatus(ModFile modFile)
142+
{
143+
string statusMessage = "";
144+
switch (modFile.PatchStatus)
145+
{
146+
case PatchStatus.Patching:
147+
statusMessage = "Patching failed";
148+
break;
144149

145-
case PatchStatus.Success:
146-
statusMessage = "Patching succeeded";
147-
break;
150+
case PatchStatus.Success:
151+
statusMessage = "Patching succeeded";
152+
break;
148153

149-
case PatchStatus.None:
150-
statusMessage = "Waiting to be patched";
151-
break;
152-
}
153-
Log.Warning("Patching {{{2}}} for {{{0}}} {{{1}}}", modFile.Name, modFile.Version, statusMessage);
154+
case PatchStatus.None:
155+
statusMessage = "Waiting to be patched";
156+
break;
154157
}
158+
Log.Warning("Patching {{{2}}} for {{{0}}} {{{1}}}", modFile.Name, modFile.Version, statusMessage);
155159
}
156160
private void MyToggleButton_Checked(object sender, EventArgs e)
157161
{
@@ -219,7 +223,7 @@ public class UserSettings
219223
{
220224
public string Language = "English";
221225
public bool EnableLogger = true;
222-
public List<string> EnableMods = new();
226+
public List<string> EnabledMods = new();
223227
public static void LoadSettings()
224228
{
225229
// if no settings file, early stop
@@ -233,16 +237,14 @@ public static void LoadSettings()
233237
CheckLog(Main.Settings.EnableLogger);
234238

235239
// auto check active mods
236-
if (Main.Settings.EnableMods.Count > 0)
240+
if (Main.Settings.EnabledMods.Count > 0)
237241
{
238242
List<ModFile> listModFile = ModInfos.Instance.Mods;
239-
foreach (string i in Main.Settings.EnableMods)
243+
foreach (string i in Main.Settings.EnabledMods)
240244
{
241245
(int indexMod, ModFile? modFile) = listModFile.Enumerate().FirstOrDefault(t => t.Item2.Name == i);
242-
if (modFile != null)
243-
listModFile[indexMod].isEnabled = true;
244-
else
245-
Log.Warning($"Mod {i} not found");
246+
if (modFile != null) listModFile[indexMod].Enabled = true;
247+
else Log.Warning($"Mod {i} not found");
246248
}
247249
}
248250
}

0 commit comments

Comments
 (0)