Skip to content

Commit

Permalink
[+] LogNetworkRequests (#90)
Browse files Browse the repository at this point in the history
* It doesn't work...

* Implement

* rename
  • Loading branch information
Menci authored Nov 25, 2024
1 parent 792dce6 commit e3b06b1
Show file tree
Hide file tree
Showing 4 changed files with 296 additions and 63 deletions.
167 changes: 105 additions & 62 deletions AquaMai/AquaMai.Core/Helpers/Shim.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,85 +6,128 @@
using MAI2.Util;
using Manager;
using Manager.UserDatas;
using MelonLoader;
using Net;
using Net.Packet;
using Net.Packet.Mai2;
using Net.VO;

namespace AquaMai.Core.Helpers;

public static class Shim
{
private static T Iife<T>(Func<T> func) => func();

public static readonly string apiSuffix = Iife(() =>
{
try
{
var baseNetQueryConstructor = typeof(NetQuery<VOSerializer, VOSerializer>)
.GetConstructors()
.First();
return ((INetQuery)baseNetQueryConstructor.Invoke(
baseNetQueryConstructor
.GetParameters()
.Select((parameter, i) => i == 0 ? "" : parameter.DefaultValue)
.ToArray())).Api;
}
catch (Exception e)
{
MelonLogger.Error($"Failed to resolve the API suffix: {e}");
return null;
}
});

public static string RemoveApiSuffix(string api)
{
return !string.IsNullOrEmpty(apiSuffix) && api.EndsWith(apiSuffix)
? api.Substring(0, api.Length - apiSuffix.Length)
: api;
}

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 static readonly GetAccessTokenMethod GetAccessToken = Iife<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 static readonly PacketUploadUserPlaylogCreator CreatePacketUploadUserPlaylog = Iife<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");
}
})();
public static readonly PacketUpsertUserAllCreator CreatePacketUpsertUserAll = Iife<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");
}
});

public static IEnumerable<UserScore>[] GetUserScoreList(UserData userData)
{
var tUserData = Traverse.Create(userData);
var tUserData = Traverse.Create(userData);

var tScoreList = tUserData.Property("ScoreList");
if (tScoreList.PropertyExists())
{
return tScoreList.GetValue<List<UserScore>[]>();
}
var tScoreList = tUserData.Property("ScoreList");
if (tScoreList.PropertyExists())
{
return tScoreList.GetValue<List<UserScore>[]>();
}

var tScoreDic = tUserData.Property("ScoreDic");
if (tScoreDic.PropertyExists())
{
var scoreDic = tScoreDic.GetValue<Dictionary<int, UserScore>[]>();
return scoreDic.Select(dic => dic.Values).ToArray();
}
var tScoreDic = tUserData.Property("ScoreDic");
if (tScoreDic.PropertyExists())
{
var scoreDic = tScoreDic.GetValue<Dictionary<int, UserScore>[]>();
return scoreDic.Select(dic => dic.Values).ToArray();
}

throw new MissingFieldException("No matching UserData.ScoreList/ScoreDic found");
throw new MissingFieldException("No matching UserData.ScoreList/ScoreDic found");
}
}
1 change: 1 addition & 0 deletions AquaMai/AquaMai.Mods/AquaMai.Mods.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
<Reference Include="System" />
<Reference Include="System.Configuration" />
<Reference Include="System.Core" />
<Reference Include="System.Runtime" />
<Reference Include="System.Security" />
<Reference Include="System.Xml" />
<Reference Include="Unity.Analytics.DataPrivacy" />
Expand Down
3 changes: 2 additions & 1 deletion AquaMai/AquaMai.Mods/GameSystem/RemoveEncryption.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Linq;
using System.Reflection;
using AquaMai.Config.Attributes;
using AquaMai.Core.Helpers;
using HarmonyLib;
using Net.Packet;

Expand All @@ -25,7 +26,7 @@ public class RemoveEncryption
[HarmonyPatch(typeof(Packet), "Obfuscator", typeof(string))]
public static bool PreObfuscator(string srcStr, ref string __result)
{
__result = srcStr.Replace("MaimaiExp", "").Replace("MaimaiChn", "");
__result = Shim.RemoveApiSuffix(srcStr);
return false;
}

Expand Down
Loading

0 comments on commit e3b06b1

Please sign in to comment.