Skip to content

Commit

Permalink
GUI for patcher.
Browse files Browse the repository at this point in the history
  • Loading branch information
AdmiralCurtiss committed May 29, 2020
1 parent 2aa7e57 commit 9583573
Show file tree
Hide file tree
Showing 21 changed files with 1,720 additions and 0 deletions.
21 changes: 21 additions & 0 deletions SenLib/SenCommonPaths.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SenLib {
public static class SenCommonPaths {
public static readonly string Sen1SteamDir = @"c:\Program Files (x86)\Steam\steamapps\common\Trails of Cold Steel\";
public static readonly string Sen1GalaxyDir = @"c:\Program Files (x86)\GOG Galaxy\Games\The Legend of Heroes - Trails of Cold Steel\";
public static readonly string Sen1EnExePath = "ed8.exe";
public static readonly string Sen1JpExePath = "ed8jp.exe";

public static readonly string Sen2SteamDir = @"c:\Program Files (x86)\Steam\steamapps\common\Trails of Cold Steel II\";
public static readonly string Sen2GalaxyDir = @"c:\Program Files (x86)\GOG Galaxy\Games\The Legend of Heroes Trails of Cold Steel II\";
public static readonly string Sen2EnExePath = @"bin\Win32\ed8_2_PC_US.exe";
public static readonly string Sen2JpExePath = @"bin\Win32\ed8_2_PC_JP.exe";

public static readonly string BackupPostfix = ".senpatcher.bkp";
}
}
2 changes: 2 additions & 0 deletions SenLib/SenLib.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@
<Compile Include="Sen2\Sen2ExecutablePatchInterface.cs" />
<Compile Include="Sen2\Sen2Mapper.cs" />
<Compile Include="Sen2\Sen2SystemData.cs" />
<Compile Include="SenCommonPaths.cs" />
<Compile Include="SenVersionIdentifier.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\HyoutaUtils\HyoutaPluginBase\HyoutaPluginBase.csproj">
Expand Down
69 changes: 69 additions & 0 deletions SenLib/SenVersionIdentifier.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
using HyoutaUtils;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace SenLib {
public enum SenVersion {
Sen1_v1_6_En,
Sen1_v1_6_Jp,
Sen2_v1_4_1_En,
Sen2_v1_4_1_Jp,
Sen2_v1_4_2_En,
Sen2_v1_4_2_Jp,
}

public static class SenVersionIdentifier {
public static (Stream binary, SenVersion? version) OpenAndIdentifyGame(string path) {
if (!File.Exists(path)) {
return (null, null);
}

var try1 = TryIdentify(path);

if (try1.binary != null && try1.version != null) {
return try1;
}

string backuppath = path + SenCommonPaths.BackupPostfix;
if (File.Exists(backuppath)) {
// we have a backup file, try that one
return TryIdentify(backuppath);
}

return (null, null);
}

private static (Stream binary, SenVersion? version) TryIdentify(string p) {
using (var s = new FileStream(p, FileMode.Open, FileAccess.Read, FileShare.Read)) {
if (s.Length > 16 * 1024 * 1024) {
// way too large to be any CS game
return (null, null);
}

MemoryStream ms = s.CopyToMemory();
using (SHA1 sha1 = SHA1.Create()) {
byte[] hashbytes = sha1.ComputeHash(ms);
ms.Position = 0;
StringBuilder sb = new StringBuilder(28);
foreach (byte b in hashbytes) {
sb.Append(b.ToString("x2"));
}
switch (sb.ToString()) {
case "373c1d1b30001af360042365ed257e070bf40acc": return (ms, SenVersion.Sen1_v1_6_En);
case "1d56abf5aa02eeae334797c287ef2109c7a103fa": return (ms, SenVersion.Sen1_v1_6_Jp);
case "d5c333b4cd517d43e3868e159fbec37dba4122d6": return (ms, SenVersion.Sen2_v1_4_1_En);
case "b8158fb59e43c02e904f813150d841336d1a13e5": return (ms, SenVersion.Sen2_v1_4_1_Jp);
case "b08ece4ee38e6e3a99e58eb11cffb45e49704f86": return (ms, SenVersion.Sen2_v1_4_2_En);
case "7d1db7e0bb91ab77a3fd1eba53b0ed25806186c1": return (ms, SenVersion.Sen2_v1_4_2_Jp);
default: ms.Dispose(); return (null, null);
}
}
}
}
}
}
6 changes: 6 additions & 0 deletions SenPatcher.sln
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HyoutaUtils", "HyoutaUtils\
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SenPatcherCli", "SenPatcherCli\SenPatcherCli.csproj", "{E410E775-6F9A-48F2-B9E1-41BB2E4B1ECF}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SenPatcherGui", "SenPatcherGui\SenPatcherGui.csproj", "{1E458E0B-8F4B-4996-B26D-AE7949FA10FA}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -33,6 +35,10 @@ Global
{E410E775-6F9A-48F2-B9E1-41BB2E4B1ECF}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E410E775-6F9A-48F2-B9E1-41BB2E4B1ECF}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E410E775-6F9A-48F2-B9E1-41BB2E4B1ECF}.Release|Any CPU.Build.0 = Release|Any CPU
{1E458E0B-8F4B-4996-B26D-AE7949FA10FA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1E458E0B-8F4B-4996-B26D-AE7949FA10FA}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1E458E0B-8F4B-4996-B26D-AE7949FA10FA}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1E458E0B-8F4B-4996-B26D-AE7949FA10FA}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
6 changes: 6 additions & 0 deletions SenPatcherGui/App.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7" />
</startup>
</configuration>
174 changes: 174 additions & 0 deletions SenPatcherGui/MainForm.Designer.cs

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

93 changes: 93 additions & 0 deletions SenPatcherGui/MainForm.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
using SenLib;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SenPatcherGui {
public partial class MainForm : Form {
public MainForm() {
InitializeComponent();
}

private void buttonCs1SteamEn_Click(object sender, EventArgs e) {
OpenFileGui(Path.Combine(SenCommonPaths.Sen1SteamDir, SenCommonPaths.Sen1EnExePath), new List<SenVersion>() { SenVersion.Sen1_v1_6_En });
}

private void buttonCs1SteamJp_Click(object sender, EventArgs e) {
OpenFileGui(Path.Combine(SenCommonPaths.Sen1SteamDir, SenCommonPaths.Sen1JpExePath), new List<SenVersion>() { SenVersion.Sen1_v1_6_Jp });
}

private void buttonCs1GalaxyEn_Click(object sender, EventArgs e) {
OpenFileGui(Path.Combine(SenCommonPaths.Sen1GalaxyDir, SenCommonPaths.Sen1EnExePath), new List<SenVersion>() { SenVersion.Sen1_v1_6_En });
}

private void buttonCs1GalaxyJp_Click(object sender, EventArgs e) {
OpenFileGui(Path.Combine(SenCommonPaths.Sen1GalaxyDir, SenCommonPaths.Sen1JpExePath), new List<SenVersion>() { SenVersion.Sen1_v1_6_Jp });
}

private void buttonCs2SteamEn_Click(object sender, EventArgs e) {
OpenFileGui(Path.Combine(SenCommonPaths.Sen2SteamDir, SenCommonPaths.Sen2EnExePath), new List<SenVersion>() { SenVersion.Sen2_v1_4_1_En, SenVersion.Sen2_v1_4_2_En });
}

private void buttonCs2SteamJp_Click(object sender, EventArgs e) {
OpenFileGui(Path.Combine(SenCommonPaths.Sen2SteamDir, SenCommonPaths.Sen2JpExePath), new List<SenVersion>() { SenVersion.Sen2_v1_4_1_Jp, SenVersion.Sen2_v1_4_2_Jp });
}

private void buttonCs2GalaxyEn_Click(object sender, EventArgs e) {
OpenFileGui(Path.Combine(SenCommonPaths.Sen2GalaxyDir, SenCommonPaths.Sen2EnExePath), new List<SenVersion>() { SenVersion.Sen2_v1_4_1_En, SenVersion.Sen2_v1_4_2_En });
}

private void buttonCs2GalaxyJp_Click(object sender, EventArgs e) {
OpenFileGui(Path.Combine(SenCommonPaths.Sen2GalaxyDir, SenCommonPaths.Sen2JpExePath), new List<SenVersion>() { SenVersion.Sen2_v1_4_1_Jp, SenVersion.Sen2_v1_4_2_Jp });
}

private void buttonManuallySelect_Click(object sender, EventArgs e) {
using (OpenFileDialog d = new OpenFileDialog()) {
d.Filter = "Cold Steel executables (ed8*.exe)|ed8*.exe|All files (*.*)|*.*";
if (d.ShowDialog() == DialogResult.OK) {
OpenFileGui(d.FileName, null);
}
}
}

private void OpenFileGui(string path, List<SenVersion> expectedVersions) {
if (!File.Exists(path)) {
MessageBox.Show("No file found at " + path + ".");
return;
}

Stream binary = null;
SenVersion? actualVersion = null;
try {
(binary, actualVersion) = SenVersionIdentifier.OpenAndIdentifyGame(path);
} catch (Exception ex) {
MessageBox.Show("Error while identifying " + path + ": " + ex.ToString());
return;
}

if (binary == null || actualVersion == null) {
MessageBox.Show("Could not identify file at " + path + " as any supported Cold Steel executable.");
return;
}

if (actualVersion == SenVersion.Sen1_v1_6_En || actualVersion == SenVersion.Sen1_v1_6_Jp) {
new Sen1Form(path, binary, actualVersion.Value).ShowDialog();
return;
}

if (actualVersion == SenVersion.Sen2_v1_4_1_En || actualVersion == SenVersion.Sen2_v1_4_1_Jp || actualVersion == SenVersion.Sen2_v1_4_2_En || actualVersion == SenVersion.Sen2_v1_4_2_Jp) {
new Sen2Form(path, binary, actualVersion.Value).ShowDialog();
return;
}

MessageBox.Show("Internal error?");
}
}
}
Loading

0 comments on commit 9583573

Please sign in to comment.