CHUNITHM-Patch-Finder/Program.cs

120 lines
3.5 KiB
C#

// See https://aka.ms/new-console-template for more information
using CHUNITHM_Patch_Finder.Converters;
using CHUNITHM_Patch_Finder.Models;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using Reloaded.Memory.Sigscan;
using Reloaded.Memory.Sigscan.Definitions.Structs;
using YamlDotNet.Serialization;
using YamlDotNet.Serialization.NamingConventions;
if (args.Length < 1)
{
Console.WriteLine($"Usage: {Environment.ProcessPath} <PATH TO EXE>");
Environment.Exit(22); // EINVAL
}
var deserializer = new DeserializerBuilder()
.WithNamingConvention(LowerCaseNamingConvention.Instance)
.Build();
var patches =
deserializer.Deserialize<Pattern[]>(
File.ReadAllText("patterns.yaml"));
var binary = File.ReadAllBytes(args[0]);
var scanner = new Scanner(binary);
var exportedPatches = new List<BemaniPatcherPatch>();
// Other patches
foreach (var patch in patches)
{
var signatures = patch.Patches.Select(p => p.Signature ?? patch.Signature).ToList();
if (signatures.Any(s => s == null))
{
Console.WriteLine($"[ERROR] No signature provided for patch {patch.Name}");
continue;
}
var matches = signatures
.Zip(scanner.FindPatternsCached(signatures)!)
.GroupBy(p => p.First)
.ToDictionary(g => g.Key, g => g.First().Second);
var nonMatches = matches.Where(p => !p.Value.Found).ToList();
if (nonMatches.Count > 0)
{
Console.WriteLine($"No offset found for patch {patch.Name}, signatures {string.Join(",", nonMatches.Select(p => p.Key))}");
continue;
}
var patchObject = new BemaniPatcherStandardPatch
{
Name = patch.Name,
Tooltip = patch.Tooltip,
Danger = patch.Danger,
Patches = patch.Patches.Select(p =>
{
var signature = (p.Signature ?? patch.Signature)!;
return new BemaniPatcherStandardPatchEntry
{
Offset = matches[signature].Offset + p.Offset,
Off = p.Off,
On = p.On,
};
}).ToList()
};
exportedPatches.Add(patchObject);
}
// max track count patch
if (Path.GetFileName(args[0]) == "chusanApp.exe")
{
var offset = scanner
.FindPatterns([
"E8 ?? ?? ?? ?? 8D ?? 78 8B F0", // SDHD 2.00-2.20
"E8 ?? ?? ?? ?? 8B 4C 24 24 8B F0", // SDGS 1.30
])
.FirstOrDefault(o => o.Found, new PatternScanResult(-1));
if (offset.Found)
{
var trackCountRelativeAddress = BitConverter.ToInt32(binary, offset.Offset + 1);
var trackCountAddress = offset.Offset + 5 + trackCountRelativeAddress;
Console.WriteLine($"Found track count function at {trackCountAddress:X}");
var patch = new BemaniPatcherNumberPatch
{
Name = "Maximum tracks",
Offset = trackCountAddress + 1,
Size = 4,
Min = 3,
Max = 12,
};
exportedPatches.Add(patch);
}
else
{
Console.WriteLine("Track count function not found");
}
}
var settings = new JsonSerializerSettings
{
ContractResolver = new DefaultContractResolver
{
NamingStrategy = new CamelCaseNamingStrategy(),
},
Converters = [new HexNumberJsonConverter()],
Formatting = Formatting.Indented,
};
File.WriteAllText(
"patches.json",
JsonConvert.SerializeObject(exportedPatches, settings));
Console.WriteLine("Wrote patches to patches.json");