Skip to content

Commit

Permalink
Merge pull request #61
Browse files Browse the repository at this point in the history
* Merge targets

* Merge branch 'v1-dev' into fork/Menci/merge-targets

* [O] Move Shim to AquaMai.Helpers
  • Loading branch information
Menci authored Oct 13, 2024
1 parent e67b68a commit a3bc37f
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 66 deletions.
3 changes: 0 additions & 3 deletions AquaMai/AquaMai.sln
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,11 @@ EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Release|Any CPU = Release|Any CPU
SDGA145|Any CPU = SDGA145|Any CPU
Debug|Any CPU = Debug|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{788BC472-59F7-46F6-B760-65C18BA74389}.Release|Any CPU.ActiveCfg = Release|Any CPU
{788BC472-59F7-46F6-B760-65C18BA74389}.Release|Any CPU.Build.0 = Release|Any CPU
{788BC472-59F7-46F6-B760-65C18BA74389}.SDGA145|Any CPU.ActiveCfg = SDGA145|Any CPU
{788BC472-59F7-46F6-B760-65C18BA74389}.SDGA145|Any CPU.Build.0 = SDGA145|Any CPU
{788BC472-59F7-46F6-B760-65C18BA74389}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{788BC472-59F7-46F6-B760-65C18BA74389}.Debug|Any CPU.Build.0 = Debug|Any CPU
EndGlobalSection
Expand Down
69 changes: 22 additions & 47 deletions AquaMai/Fix/RemoveEncryption.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System.Linq;
using System.Reflection;
using HarmonyLib;
using MelonLoader;
using Net.Packet;

namespace AquaMai.Fix;
Expand All @@ -11,65 +10,41 @@ public class RemoveEncryption
{
[HarmonyPrefix]
[HarmonyPatch(typeof(Packet), "Obfuscator", typeof(string))]
private static bool PreObfuscator(string srcStr, ref string __result)
public static bool PreObfuscator(string srcStr, ref string __result)
{
__result = srcStr.Replace("MaimaiExp", "").Replace("MaimaiChn", "");
return false;
}

[HarmonyPatch]
public class Encrypt
public class EncryptDecrypt
{
public static IEnumerable<MethodBase> TargetMethods()
{
# if SDGA145
return [AccessTools.Method("Net.CipherAES:Encrypt", [typeof(byte[])])];
# else
return [AccessTools.TypeByName("Net.CipherAES").GetMethods().FirstOrDefault(it => it.Name == "Encrypt")];
# endif
var methods = AccessTools.TypeByName("Net.CipherAES").GetMethods();
return
[
methods.FirstOrDefault(it => it.Name == "Encrypt" && it.IsPublic),
methods.FirstOrDefault(it => it.Name == "Decrypt" && it.IsPublic)
];
}

# if SDGA145
public static bool Prefix(byte[] data, ref byte[] __result)
public static bool Prefix(object[] __args, ref object __result)
{
__result = data;
if (__args.Length == 1)
{
// public static byte[] Encrypt(byte[] data)
// public static byte[] Decrypt(byte[] encryptData)
__result = __args[0];
}
else if (__args.Length == 2)
{
// public static bool Encrypt(byte[] data, out byte[] encryptData)
// public static bool Decrypt(byte[] encryptData, out byte[] plainData)
__args[1] = __args[0];
__result = true;
}
return false;
}
# else
public static bool Prefix(byte[] data, out byte[] encryptData, ref bool __result)
{
encryptData = data;
__result = true;
return false;
}
# endif
}

[HarmonyPatch]
public class Decrypt
{
public static IEnumerable<MethodBase> TargetMethods()
{
# if SDGA145
return [AccessTools.Method("Net.CipherAES:Decrypt", [typeof(byte[])])];
# else
return [AccessTools.TypeByName("Net.CipherAES").GetMethods().FirstOrDefault(it => it.Name == "Decrypt")];
# endif
}

# if SDGA145
public static bool Prefix(byte[] encryptData, ref byte[] __result)
{
__result = encryptData;
return false;
}
# else
public static bool Prefix(byte[] encryptData, out byte[] plainData, ref bool __result)
{
plainData = encryptData;
__result = true;
return false;
}
# endif
}
}
67 changes: 67 additions & 0 deletions AquaMai/Helpers/Shim.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
using System;
using System.Reflection;
using HarmonyLib;
using MAI2.Util;
using Manager;
using Net.Packet;
using Net.Packet.Mai2;

namespace AquaMai.Helpers;

public static class Shim
{
public delegate string GetAccessTokenMethod(int index);
public static readonly GetAccessTokenMethod GetAccessToken = new Func<GetAccessTokenMethod>(() => {
var tOperationManager = Traverse.Create(Singleton<OperationManager>.Instance);
var tGetAccessToken = tOperationManager.Method("GetAccessToken", [typeof(int)]);
if (!tGetAccessToken.MethodExists())
{
return (index) => throw new MissingMethodException("No matching OperationManager.GetAccessToken() method found");
}
return (index) => tGetAccessToken.GetValue<string>(index);
})();

public delegate PacketUploadUserPlaylog PacketUploadUserPlaylogCreator(int index, UserData src, int trackNo, Action<int> onDone, Action<PacketStatus> onError = null);
public static readonly PacketUploadUserPlaylogCreator CreatePacketUploadUserPlaylog = new Func<PacketUploadUserPlaylogCreator>(() => {
var type = typeof(PacketUploadUserPlaylog);
if (type.GetConstructor([typeof(int), typeof(UserData), typeof(int), typeof(Action<int>), typeof(Action<PacketStatus>)]) is ConstructorInfo ctor1) {
return (index, src, trackNo, onDone, onError) => {
var args = new object[] {index, src, trackNo, "", onDone, onError};
return (PacketUploadUserPlaylog)ctor1.Invoke(args);
};
}
else if (type.GetConstructor([typeof(int), typeof(UserData), typeof(int), typeof(string), typeof(Action<int>), typeof(Action<PacketStatus>)]) is ConstructorInfo ctor2) {
return (index, src, trackNo, onDone, onError) => {
var accessToken = GetAccessToken(index);
var args = new object[] {index, src, trackNo, accessToken, onDone, onError};
return (PacketUploadUserPlaylog)ctor2.Invoke(args);
};
}
else
{
throw new MissingMethodException("No matching PacketUploadUserPlaylog constructor found");
}
})();

public delegate PacketUpsertUserAll PacketUpsertUserAllCreator(int index, UserData src, Action<int> onDone, Action<PacketStatus> onError = null);
public static readonly PacketUpsertUserAllCreator CreatePacketUpsertUserAll = new Func<PacketUpsertUserAllCreator>(() => {
var type = typeof(PacketUpsertUserAll);
if (type.GetConstructor([typeof(int), typeof(UserData), typeof(Action<int>), typeof(Action<PacketStatus>)]) is ConstructorInfo ctor1) {
return (index, src, onDone, onError) => {
var args = new object[] {index, src, "", onDone, onError};
return (PacketUpsertUserAll)ctor1.Invoke(args);
};
}
else if (type.GetConstructor([typeof(int), typeof(UserData), typeof(string), typeof(Action<int>), typeof(Action<PacketStatus>)]) is ConstructorInfo ctor2) {
return (index, src, onDone, onError) => {
var accessToken = GetAccessToken(index);
var args = new object[] {index, src, accessToken, onDone, onError};
return (PacketUpsertUserAll)ctor2.Invoke(args);
};
}
else
{
throw new MissingMethodException("No matching PacketUpsertUserAll constructor found");
}
})();
}
21 changes: 5 additions & 16 deletions AquaMai/UX/ImmediateSave.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
using System.Linq;
using AquaMai.Helpers;
using AquaMai.Resources;
using AquaMai.Utils;
using CriAtomDebugDetail;
using DB;
using HarmonyLib;
using MAI2.Util;
Expand Down Expand Up @@ -63,12 +65,7 @@ void CheckSaveDone()
}

SaveDataFix(userData);
# if SDGA145
PacketHelper.StartPacket(new PacketUploadUserPlaylog(i, userData, (int)GameManager.MusicTrackNumber - 1,
# else
var accessToken = Singleton<OperationManager>.Instance.GetAccessToken(i);
PacketHelper.StartPacket(new PacketUploadUserPlaylog(i, userData, (int)GameManager.MusicTrackNumber - 1, accessToken,
# endif
PacketHelper.StartPacket(Shim.CreatePacketUploadUserPlaylog(i, userData, (int)GameManager.MusicTrackNumber - 1,
delegate
{
MelonLogger.Msg("Playlog saved");
Expand All @@ -82,11 +79,7 @@ void CheckSaveDone()
MessageHelper.ShowMessage("Playlog save error");
CheckSaveDone();
}));
# if SDGA145
PacketHelper.StartPacket(new PacketUpsertUserAll(i, userData, delegate(int code)
# else
PacketHelper.StartPacket(new PacketUpsertUserAll(i, userData, accessToken, delegate(int code)
# endif
PacketHelper.StartPacket(Shim.CreatePacketUpsertUserAll(i, userData, delegate(int code)
{
if (code == 1)
{
Expand Down Expand Up @@ -222,11 +215,7 @@ private static void SaveDataFix(UserData userData)
userData.Detail.LastPlayMode = 2;
}

# if SDGA145
userData.Detail.LastGameId = "SDGA";
# else
userData.Detail.LastGameId = "SDEZ";
# endif
userData.Detail.LastGameId = ConstParameter.GameIDStr;
userData.Detail.LastRomVersion = Singleton<SystemConfig>.Instance.config.romVersionInfo.versionNo.versionString;
userData.Detail.LastDataVersion = Singleton<SystemConfig>.Instance.config.dataVersionInfo.versionNo.versionString;
}
Expand Down

0 comments on commit a3bc37f

Please sign in to comment.