From 0c9de325bea9cc50cad09cf2915429ab9a633a13 Mon Sep 17 00:00:00 2001 From: jvyden Date: Mon, 18 Oct 2021 10:11:09 -0400 Subject: [PATCH] Add CLI patcher --- UnionPatcher.Gui/MainForm.cs | 1 + UnionPatcher.Gui/UnionPatcher.Gui.csproj | 4 ++ UnionPatcher.sln | 13 ++++++ UnionPatcher/Patcher.cs | 51 ++++++++++++++++++++++++ UnionPatcher/Program.cs | 33 +++++++++++++++ UnionPatcher/UnionPatcher.csproj | 8 ++++ 6 files changed, 110 insertions(+) create mode 100644 UnionPatcher/Patcher.cs create mode 100644 UnionPatcher/Program.cs create mode 100644 UnionPatcher/UnionPatcher.csproj diff --git a/UnionPatcher.Gui/MainForm.cs b/UnionPatcher.Gui/MainForm.cs index ca5bfe6..8579d7c 100644 --- a/UnionPatcher.Gui/MainForm.cs +++ b/UnionPatcher.Gui/MainForm.cs @@ -13,6 +13,7 @@ namespace UnionPatcher.Gui { control.Click += delegate { Console.WriteLine("patch button clicked"); +// Program.Test(); }; return control; diff --git a/UnionPatcher.Gui/UnionPatcher.Gui.csproj b/UnionPatcher.Gui/UnionPatcher.Gui.csproj index c3c1b7b..c35739d 100644 --- a/UnionPatcher.Gui/UnionPatcher.Gui.csproj +++ b/UnionPatcher.Gui/UnionPatcher.Gui.csproj @@ -9,4 +9,8 @@ + + + + diff --git a/UnionPatcher.sln b/UnionPatcher.sln index 2c2e17c..ee2f48d 100644 --- a/UnionPatcher.sln +++ b/UnionPatcher.sln @@ -6,6 +6,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UnionPatcher.Gui.Windows", EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UnionPatcher.Gui.Linux", "UnionPatcher.Gui.Linux\UnionPatcher.Gui.Linux.csproj", "{55DFA962-3EC5-44E4-9F93-FACF3E7EEBB2}" EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Gui", "Gui", "{59194212-45BB-4A27-A08F-54DB6ADA26B3}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UnionPatcher", "UnionPatcher\UnionPatcher.csproj", "{1CEC657B-5C05-44FB-941B-3C41411A7BB8}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -24,5 +28,14 @@ Global {55DFA962-3EC5-44E4-9F93-FACF3E7EEBB2}.Debug|Any CPU.Build.0 = Debug|Any CPU {55DFA962-3EC5-44E4-9F93-FACF3E7EEBB2}.Release|Any CPU.ActiveCfg = Release|Any CPU {55DFA962-3EC5-44E4-9F93-FACF3E7EEBB2}.Release|Any CPU.Build.0 = Release|Any CPU + {1CEC657B-5C05-44FB-941B-3C41411A7BB8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {1CEC657B-5C05-44FB-941B-3C41411A7BB8}.Debug|Any CPU.Build.0 = Debug|Any CPU + {1CEC657B-5C05-44FB-941B-3C41411A7BB8}.Release|Any CPU.ActiveCfg = Release|Any CPU + {1CEC657B-5C05-44FB-941B-3C41411A7BB8}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(NestedProjects) = preSolution + {235B4DEC-11A0-42EF-9B8D-16529869ED1A} = {59194212-45BB-4A27-A08F-54DB6ADA26B3} + {55DFA962-3EC5-44E4-9F93-FACF3E7EEBB2} = {59194212-45BB-4A27-A08F-54DB6ADA26B3} + {5F8CDEA2-B483-40D1-B1FB-EF8EC667185A} = {59194212-45BB-4A27-A08F-54DB6ADA26B3} EndGlobalSection EndGlobal diff --git a/UnionPatcher/Patcher.cs b/UnionPatcher/Patcher.cs new file mode 100644 index 0000000..5a1436b --- /dev/null +++ b/UnionPatcher/Patcher.cs @@ -0,0 +1,51 @@ +using System; +using System.IO; +using System.Text; + +namespace UnionPatcher { + public static class Patcher { + private static readonly string[] ToBePatched = { + "https://littlebigplanetps3.online.scee.com:10061/LITTLEBIGPLANETPS3_XML", + "http://littlebigplanetps3.online.scee.com:10060/LITTLEBIGPLANETPS3_XML", + }; + + public static void PatchFile(string fileName, string serverUrl, string outputFileName) { + File.WriteAllBytes(outputFileName, PatchData(File.ReadAllBytes(fileName), serverUrl)); + } + + public static byte[] PatchData(byte[] data, string serverUrl) { + string dataAsString = Encoding.ASCII.GetString(data); + + using MemoryStream ms = new(data); + using BinaryWriter writer = new(ms); + + // using writer.Write(string) writes the length as a byte beforehand which is problematic + byte[] serverUrlAsBytes = Encoding.ASCII.GetBytes(serverUrl); + + foreach(string url in ToBePatched) { + if(serverUrl.Length > url.Length) { + throw new ArgumentOutOfRangeException(nameof(serverUrl), $"Server URL ({serverUrl.Length} characters long) is above maximum length {url.Length}"); + } + + int offset = dataAsString.IndexOf(url, StringComparison.Ordinal); + if(offset < 1) { + Console.WriteLine($"URL {url} not found!"); + continue; + } + + writer.BaseStream.Position = offset; + for(int i = 0; i < url.Length; i++) { + writer.Write((byte)0x00); // Zero out data + } + + writer.BaseStream.Position = offset; // Reset position to beginning + writer.Write(serverUrlAsBytes); + } + + writer.Flush(); + writer.Close(); + + return data; + } + } +} \ No newline at end of file diff --git a/UnionPatcher/Program.cs b/UnionPatcher/Program.cs new file mode 100644 index 0000000..9de0d40 --- /dev/null +++ b/UnionPatcher/Program.cs @@ -0,0 +1,33 @@ +using System; +using System.Diagnostics; +using System.IO; + +namespace UnionPatcher { + public static class Program { + public const string Version = "1.0"; + + private static string fileName; + + public static string FileName { + get { + if(fileName != null) return fileName; + + return fileName = Path.GetFileName(Process.GetCurrentProcess().MainModule?.FileName); + } + } + + public static void Main(string[] args) { + Patcher.PatchFile("EBOOT.elf", "https://lighthouse.lbpunion.com:10061/LITTLEBIGPLANETPS3_XML", "EBOOT.new.elf"); + if(args.Length < 3) { + PrintHelp(); + return; + } + + } + + public static void PrintHelp() { + Console.WriteLine($"UnionPatcher {Version}"); + Console.WriteLine($" Usage: {FileName} "); + } + } +} \ No newline at end of file diff --git a/UnionPatcher/UnionPatcher.csproj b/UnionPatcher/UnionPatcher.csproj new file mode 100644 index 0000000..9590466 --- /dev/null +++ b/UnionPatcher/UnionPatcher.csproj @@ -0,0 +1,8 @@ + + + + Exe + net5.0 + + +