Skip to content

Commit

Permalink
Remove global userscript, create backup singleton class
Browse files Browse the repository at this point in the history
  • Loading branch information
Eisenmonoxid committed Dec 9, 2024
1 parent b7791f8 commit a9b0c20
Show file tree
Hide file tree
Showing 8 changed files with 112 additions and 90 deletions.
2 changes: 1 addition & 1 deletion Forms/mainFrm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ private void StartPatching(execID ID)
OpenFileDialog ofd = Helpers.CreateOFDialog();
if (ofd.ShowDialog() == DialogResult.OK && File.Exists(ofd.FileName))
{
if (Helpers.CreateBackup(ofd.FileName) == false)
if (Backup.Instance.CreateBackup(ofd.FileName) == false)
{
MessageBox.Show(Resources.ErrorBackup, "Error", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
Expand Down
12 changes: 3 additions & 9 deletions Forms/mainPatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -159,32 +159,26 @@ private void cbHighTextures_CheckedChanged(object sender, EventArgs e)
private void btnPatch_Click(object sender, EventArgs e)
{
SelectPatchVersion(GlobalID, ref GlobalStream);
DialogResult = DialogResult.OK; // Patching successfull

DialogResult = DialogResult.OK; // Patching successful
Close();
Dispose();
}
private void btnAbort_Click(object sender, EventArgs e)
{
DialogResult = DialogResult.Abort; // Abort

Close();
Dispose();
}
private void btnBackup_Click(object sender, EventArgs e)
{
string Path = GlobalStream.Name;
GlobalStream.Close();
GlobalStream.Dispose();

bool Result = Helpers.RestoreBackup(Path);
bool Result = Backup.Instance.RestoreBackup(ref GlobalStream);
if (Result == false)
{
DialogResult = DialogResult.Cancel; // Backup failed
}
else
{
DialogResult = DialogResult.Retry; // Backup successfull
DialogResult = DialogResult.Retry; // Backup successful
}

Close();
Expand Down
94 changes: 94 additions & 0 deletions Helpers/Backup.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Windows.Forms;
using static S6Patcher.Helpers;

namespace S6Patcher
{
public sealed class Backup
{
private static readonly Backup _instance = new Backup();
private Backup() {}
public static Backup Instance
{
get
{
return _instance;
}
}
public bool CreateBackup(string Filepath)
{
string FileName = Path.GetFileNameWithoutExtension(Filepath);
string DirectoryPath = Path.GetDirectoryName(Filepath);
string FinalPath = Path.Combine(DirectoryPath, FileName + "_BACKUP.exe");

if (File.Exists(FinalPath) == false)
{
try
{
File.Copy(Filepath, FinalPath, false);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
return false;
}
}
return true;
}
public void RemoveUserScriptFiles()
{
string[] ScriptFiles = { "UserScriptLocal.lua", "EMXBinData.s6patcher" };
List<string> Directories = GetUserScriptDirectories();
foreach (string Element in Directories)
{
string ScriptPath = Path.Combine(Element, "Script");
if (!Directory.Exists(ScriptPath))
{
continue;
}
foreach (string Entry in ScriptFiles)
{
try
{
File.Delete(Path.Combine(ScriptPath, Entry));
}
catch (Exception) // Errors here do not matter
{
continue;
}
}
}
}
public bool RestoreBackup(ref FileStream Stream)
{
RemoveUserScriptFiles(); // Delete Userscript from Folders

string FilePath = Stream.Name;
string FileName = Path.GetFileNameWithoutExtension(FilePath);
string DirectoryPath = Path.GetDirectoryName(FilePath);
string FinalPath = Path.Combine(DirectoryPath, FileName + "_BACKUP.exe");

if (File.Exists(FinalPath) == false)
{
return false;
}

Stream.Close();
Stream.Dispose();

try
{
File.Replace(FinalPath, FilePath, null);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
return false;
}

return true;
}
}
}
73 changes: 1 addition & 72 deletions Helpers/Helpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,26 +61,6 @@ public static OpenFileDialog CreateOFDialog()

return ofd;
}
public static bool CreateBackup(string Filepath)
{
string FileName = Path.GetFileNameWithoutExtension(Filepath);
string DirectoryPath = Path.GetDirectoryName(Filepath);
string FinalPath = Path.Combine(DirectoryPath, FileName + "_BACKUP.exe");

if (File.Exists(FinalPath) == false)
{
try
{
File.Copy(Filepath, FinalPath, false);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
return false;
}
}
return true;
}
public static bool CheckExecVersion(ref FileStream Reader, execID Identifier, Int64 Offset = 0x0)
{
string ExpectedVersion = "1, 71, 4289, 0";
Expand Down Expand Up @@ -144,56 +124,5 @@ public static List<string> GetUserScriptDirectories()

return Directories;
}
public static void RemoveUserScriptFiles()
{
string[] ScriptFiles = {"UserScriptLocal.lua", "EMXBinData.s6patcher"};
List<string> Directories = GetUserScriptDirectories();

string ScriptPath = String.Empty;
foreach (string Element in Directories)
{
ScriptPath = Path.Combine(Element, "Script");
if (!Directory.Exists(ScriptPath))
{
continue;
}
try
{
foreach (string Entry in ScriptFiles)
{
File.Delete(Path.Combine(ScriptPath, Entry));
}
}
catch (Exception) // Errors here do not matter
{
continue;
}
}
}
public static bool RestoreBackup(string filePath)
{
RemoveUserScriptFiles(); // Delete Userscript from Folders

string FileName = Path.GetFileNameWithoutExtension(filePath);
string DirectoryPath = Path.GetDirectoryName(filePath);
string FinalPath = Path.Combine(DirectoryPath, FileName + "_BACKUP.exe");

if (File.Exists(FinalPath) == false)
{
return false;
}

try
{
File.Replace(FinalPath, filePath, null);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
return false;
}

return true;
}
}
}
}
8 changes: 4 additions & 4 deletions Properties/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -151,16 +151,16 @@ Process was aborted ...</value>
- Can set the maximum zoom level in the game.
- Can activate the Development-Mode permanently without Registry-Key.
- Can set the Large Address Aware Flag for more usable memory.
- Can fix Bugs in Script&amp;Code (e.g. "Meldungsstau", "Entertainercrash").
-&gt; Original versions from Steam will need to be extracted with the tool "Steamless" first!</value>
- Can fix Bugs in Script&amp;Code (e.g. "Meldungsstau", "Entertainercrash", "2K &amp; 4K - Resolution").
-&gt; Original releases from Steam will need to be extracted with the tool "Steamless" first!</value>
</data>
<data name="btnPatchOV_GermanText" xml:space="preserve">
<value>- Kann den Textur-Slider im Menü reaktivieren, um hohe Texturen auswählen zu können.
- Kann die Qualität von Bodentexturen auf den höchsten Wert setzen.
- Kann den maximalen Zoomlevel setzen.
- Kann das Large Address Aware Flag setzen, um mehr adressierbaren Speicher zu haben.
- Kann den Development-Mode ohne Registry-Key dauerhaft aktivieren.
- Kann Script&amp;Code-Bugs fixen (z.B. "Meldungsstau", "Entertainercrash").
- Kann Script&amp;Code-Bugs fixen (z.B. "Meldungsstau", "Entertainercrash", "2K &amp; 4K - Auflösung").
-&gt; Originalversionen von Steam müssen zuerst mit dem Tool "Steamless" entpackt werden!</value>
</data>
<data name="btnPatch_EnglishText" xml:space="preserve">
Expand All @@ -172,7 +172,7 @@ Process was aborted ...</value>
- Activates the Development-Mode without Registry-Key.
- Enables walls and wall gates from all climate zones in all climate zones.
- Can place, select and move all entity types (Careful, some may crash).
- Can apply all texture types from all climate zones (and some hidden too).
- Can apply all texture types from all climate zones (and some hidden ones too).
- Can open map files that have been protected with the "S6Tools".
- Can set the Large Address Aware Flag for more usable memory.</value>
</data>
Expand Down
4 changes: 4 additions & 0 deletions Resources/UserScriptLocal.lua
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ end
S6Patcher.GlobalScriptOverridden = false;
S6Patcher.DefaultKnightNames = {"U_KnightSaraya", "U_KnightTrading", "U_KnightHealing", "U_KnightChivalry", "U_KnightWisdom", "U_KnightPlunder", "U_KnightSong"};
S6Patcher.OverrideGlobalScript = function()
if S6Patcher.SelectedKnight == nil then
return;
end

local Knight = Entities[S6Patcher.DefaultKnightNames[S6Patcher.SelectedKnight]];
Framework.SetOnGameStartLuaCommand(""); -- free memory

Expand Down
1 change: 1 addition & 0 deletions S6Patcher.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
<Compile Include="Forms\mainFrm.Designer.cs">
<DependentUpon>mainFrm.cs</DependentUpon>
</Compile>
<Compile Include="Helpers\Backup.cs" />
<Compile Include="Patcher\PatcherMappings.cs" />
<Compile Include="Patcher\Patcher.cs" />
<Compile Include="Helpers\Helpers.cs" />
Expand Down

0 comments on commit a9b0c20

Please sign in to comment.