mirror of https://github.com/hykilpikonna/AquaDX
parent
792dce6843
commit
e3b06b110f
|
@ -6,85 +6,128 @@ using HarmonyLib;
|
||||||
using MAI2.Util;
|
using MAI2.Util;
|
||||||
using Manager;
|
using Manager;
|
||||||
using Manager.UserDatas;
|
using Manager.UserDatas;
|
||||||
|
using MelonLoader;
|
||||||
|
using Net;
|
||||||
using Net.Packet;
|
using Net.Packet;
|
||||||
using Net.Packet.Mai2;
|
using Net.Packet.Mai2;
|
||||||
|
using Net.VO;
|
||||||
|
|
||||||
namespace AquaMai.Core.Helpers;
|
namespace AquaMai.Core.Helpers;
|
||||||
|
|
||||||
public static class Shim
|
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 delegate string GetAccessTokenMethod(int index);
|
||||||
public static readonly GetAccessTokenMethod GetAccessToken = new Func<GetAccessTokenMethod>(() => {
|
public static readonly GetAccessTokenMethod GetAccessToken = Iife<GetAccessTokenMethod>(() =>
|
||||||
var tOperationManager = Traverse.Create(Singleton<OperationManager>.Instance);
|
{
|
||||||
var tGetAccessToken = tOperationManager.Method("GetAccessToken", [typeof(int)]);
|
var tOperationManager = Traverse.Create(Singleton<OperationManager>.Instance);
|
||||||
if (!tGetAccessToken.MethodExists())
|
var tGetAccessToken = tOperationManager.Method("GetAccessToken", [typeof(int)]);
|
||||||
{
|
if (!tGetAccessToken.MethodExists())
|
||||||
return (index) => throw new MissingMethodException("No matching OperationManager.GetAccessToken() method found");
|
{
|
||||||
}
|
return (index) => throw new MissingMethodException("No matching OperationManager.GetAccessToken() method found");
|
||||||
return (index) => tGetAccessToken.GetValue<string>(index);
|
}
|
||||||
})();
|
return (index) => tGetAccessToken.GetValue<string>(index);
|
||||||
|
});
|
||||||
|
|
||||||
public delegate PacketUploadUserPlaylog PacketUploadUserPlaylogCreator(int index, UserData src, int trackNo, Action<int> onDone, Action<PacketStatus> onError = null);
|
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>(() => {
|
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) {
|
var type = typeof(PacketUploadUserPlaylog);
|
||||||
return (index, src, trackNo, onDone, onError) => {
|
if (type.GetConstructor([typeof(int), typeof(UserData), typeof(int), typeof(Action<int>), typeof(Action<PacketStatus>)]) is ConstructorInfo ctor1)
|
||||||
var args = new object[] {index, src, trackNo, onDone, onError};
|
{
|
||||||
return (PacketUploadUserPlaylog)ctor1.Invoke(args);
|
return (index, src, trackNo, onDone, onError) =>
|
||||||
};
|
{
|
||||||
}
|
var args = new object[] { index, src, trackNo, onDone, onError };
|
||||||
else if (type.GetConstructor([typeof(int), typeof(UserData), typeof(int), typeof(string), typeof(Action<int>), typeof(Action<PacketStatus>)]) is ConstructorInfo ctor2) {
|
return (PacketUploadUserPlaylog)ctor1.Invoke(args);
|
||||||
return (index, src, trackNo, onDone, onError) => {
|
};
|
||||||
var accessToken = GetAccessToken(index);
|
}
|
||||||
var args = new object[] {index, src, trackNo, accessToken, onDone, onError};
|
else if (type.GetConstructor([typeof(int), typeof(UserData), typeof(int), typeof(string), typeof(Action<int>), typeof(Action<PacketStatus>)]) is ConstructorInfo ctor2)
|
||||||
return (PacketUploadUserPlaylog)ctor2.Invoke(args);
|
{
|
||||||
};
|
return (index, src, trackNo, onDone, onError) =>
|
||||||
}
|
{
|
||||||
else
|
var accessToken = GetAccessToken(index);
|
||||||
{
|
var args = new object[] { index, src, trackNo, accessToken, onDone, onError };
|
||||||
throw new MissingMethodException("No matching PacketUploadUserPlaylog constructor found");
|
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 delegate PacketUpsertUserAll PacketUpsertUserAllCreator(int index, UserData src, Action<int> onDone, Action<PacketStatus> onError = null);
|
||||||
public static readonly PacketUpsertUserAllCreator CreatePacketUpsertUserAll = new Func<PacketUpsertUserAllCreator>(() => {
|
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) {
|
var type = typeof(PacketUpsertUserAll);
|
||||||
return (index, src, onDone, onError) => {
|
if (type.GetConstructor([typeof(int), typeof(UserData), typeof(Action<int>), typeof(Action<PacketStatus>)]) is ConstructorInfo ctor1)
|
||||||
var args = new object[] {index, src, onDone, onError};
|
{
|
||||||
return (PacketUpsertUserAll)ctor1.Invoke(args);
|
return (index, src, onDone, onError) =>
|
||||||
};
|
{
|
||||||
}
|
var args = new object[] { index, src, onDone, onError };
|
||||||
else if (type.GetConstructor([typeof(int), typeof(UserData), typeof(string), typeof(Action<int>), typeof(Action<PacketStatus>)]) is ConstructorInfo ctor2) {
|
return (PacketUpsertUserAll)ctor1.Invoke(args);
|
||||||
return (index, src, onDone, onError) => {
|
};
|
||||||
var accessToken = GetAccessToken(index);
|
}
|
||||||
var args = new object[] {index, src, accessToken, onDone, onError};
|
else if (type.GetConstructor([typeof(int), typeof(UserData), typeof(string), typeof(Action<int>), typeof(Action<PacketStatus>)]) is ConstructorInfo ctor2)
|
||||||
return (PacketUpsertUserAll)ctor2.Invoke(args);
|
{
|
||||||
};
|
return (index, src, onDone, onError) =>
|
||||||
}
|
{
|
||||||
else
|
var accessToken = GetAccessToken(index);
|
||||||
{
|
var args = new object[] { index, src, accessToken, onDone, onError };
|
||||||
throw new MissingMethodException("No matching PacketUpsertUserAll constructor found");
|
return (PacketUpsertUserAll)ctor2.Invoke(args);
|
||||||
}
|
};
|
||||||
})();
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
throw new MissingMethodException("No matching PacketUpsertUserAll constructor found");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
public static IEnumerable<UserScore>[] GetUserScoreList(UserData userData)
|
public static IEnumerable<UserScore>[] GetUserScoreList(UserData userData)
|
||||||
{
|
{
|
||||||
var tUserData = Traverse.Create(userData);
|
var tUserData = Traverse.Create(userData);
|
||||||
|
|
||||||
var tScoreList = tUserData.Property("ScoreList");
|
var tScoreList = tUserData.Property("ScoreList");
|
||||||
if (tScoreList.PropertyExists())
|
if (tScoreList.PropertyExists())
|
||||||
{
|
{
|
||||||
return tScoreList.GetValue<List<UserScore>[]>();
|
return tScoreList.GetValue<List<UserScore>[]>();
|
||||||
}
|
}
|
||||||
|
|
||||||
var tScoreDic = tUserData.Property("ScoreDic");
|
var tScoreDic = tUserData.Property("ScoreDic");
|
||||||
if (tScoreDic.PropertyExists())
|
if (tScoreDic.PropertyExists())
|
||||||
{
|
{
|
||||||
var scoreDic = tScoreDic.GetValue<Dictionary<int, UserScore>[]>();
|
var scoreDic = tScoreDic.GetValue<Dictionary<int, UserScore>[]>();
|
||||||
return scoreDic.Select(dic => dic.Values).ToArray();
|
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");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -50,6 +50,7 @@
|
||||||
<Reference Include="System" />
|
<Reference Include="System" />
|
||||||
<Reference Include="System.Configuration" />
|
<Reference Include="System.Configuration" />
|
||||||
<Reference Include="System.Core" />
|
<Reference Include="System.Core" />
|
||||||
|
<Reference Include="System.Runtime" />
|
||||||
<Reference Include="System.Security" />
|
<Reference Include="System.Security" />
|
||||||
<Reference Include="System.Xml" />
|
<Reference Include="System.Xml" />
|
||||||
<Reference Include="Unity.Analytics.DataPrivacy" />
|
<Reference Include="Unity.Analytics.DataPrivacy" />
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
using AquaMai.Config.Attributes;
|
using AquaMai.Config.Attributes;
|
||||||
|
using AquaMai.Core.Helpers;
|
||||||
using HarmonyLib;
|
using HarmonyLib;
|
||||||
using Net.Packet;
|
using Net.Packet;
|
||||||
|
|
||||||
|
@ -25,7 +26,7 @@ public class RemoveEncryption
|
||||||
[HarmonyPatch(typeof(Packet), "Obfuscator", typeof(string))]
|
[HarmonyPatch(typeof(Packet), "Obfuscator", typeof(string))]
|
||||||
public static bool PreObfuscator(string srcStr, ref string __result)
|
public static bool PreObfuscator(string srcStr, ref string __result)
|
||||||
{
|
{
|
||||||
__result = srcStr.Replace("MaimaiExp", "").Replace("MaimaiChn", "");
|
__result = Shim.RemoveApiSuffix(srcStr);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,188 @@
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.IO;
|
||||||
|
using System.Net;
|
||||||
|
using System.Runtime.CompilerServices;
|
||||||
|
using System.Text;
|
||||||
|
using Net;
|
||||||
|
using Net.Packet;
|
||||||
|
using MelonLoader;
|
||||||
|
using MelonLoader.TinyJSON;
|
||||||
|
using HarmonyLib;
|
||||||
|
using AquaMai.Core.Attributes;
|
||||||
|
using AquaMai.Config.Attributes;
|
||||||
|
using AquaMai.Core.Helpers;
|
||||||
|
|
||||||
|
namespace AquaMai.Mods.Utils;
|
||||||
|
|
||||||
|
[ConfigSection(
|
||||||
|
en: "Log network requests to the MelonLoader console.",
|
||||||
|
zh: "将网络请求输出到 MelonLoader 控制台")]
|
||||||
|
public class LogNetworkRequests
|
||||||
|
{
|
||||||
|
[ConfigEntry]
|
||||||
|
private static readonly bool url = true;
|
||||||
|
|
||||||
|
[ConfigEntry]
|
||||||
|
private static readonly bool request = true;
|
||||||
|
[ConfigEntry]
|
||||||
|
private static readonly string requestOmittedApis = "UploadUserPhotoApi,UploadUserPortraitApi";
|
||||||
|
|
||||||
|
[ConfigEntry]
|
||||||
|
private static readonly bool response = true;
|
||||||
|
[ConfigEntry]
|
||||||
|
private static readonly string responseOmittedApis = "GetGameEventApi";
|
||||||
|
[ConfigEntry(
|
||||||
|
en: "Only print error responses, without the successful ones.",
|
||||||
|
zh: "仅输出出错的响应,不输出成功的响应")]
|
||||||
|
private static readonly bool responseErrorOnly = false;
|
||||||
|
|
||||||
|
private static HashSet<string> requestOmittedApiList = [];
|
||||||
|
private static HashSet<string> responseOmittedApiList = [];
|
||||||
|
|
||||||
|
private static readonly ConditionalWeakTable<NetHttpClient, HttpWebResponse> errorResponse = new();
|
||||||
|
|
||||||
|
public static void OnBeforePatch()
|
||||||
|
{
|
||||||
|
requestOmittedApiList = [.. requestOmittedApis.Split(',')];
|
||||||
|
responseOmittedApiList = [.. responseOmittedApis.Split(',')];
|
||||||
|
|
||||||
|
if (responseErrorOnly && !response)
|
||||||
|
{
|
||||||
|
MelonLogger.Warning("[LogNetworkRequests] `responseErrorOnly` is enabled but `response` is disabled. Will not print any response.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static string GetApiName(INetQuery netQuery)
|
||||||
|
{
|
||||||
|
return Shim.RemoveApiSuffix(netQuery.Api);
|
||||||
|
}
|
||||||
|
|
||||||
|
[EnableIf(nameof(url))]
|
||||||
|
[HarmonyPostfix]
|
||||||
|
[HarmonyPatch(typeof(Packet), "Create")]
|
||||||
|
public static void PostCreate(Packet __instance)
|
||||||
|
{
|
||||||
|
MelonLogger.Msg($"[LogNetworkRequests] {GetApiName(__instance.Query)} URL: {MaybeGetNetPacketUrl(__instance)}");
|
||||||
|
}
|
||||||
|
|
||||||
|
private static string MaybeGetNetPacketUrl(Packet __instance)
|
||||||
|
{
|
||||||
|
if (Traverse.Create(__instance).Field("Client").GetValue() is not NetHttpClient client)
|
||||||
|
{
|
||||||
|
return "<NetHttpClient is null>";
|
||||||
|
}
|
||||||
|
if (Traverse.Create(client).Field("_request").GetValue() is not HttpWebRequest request)
|
||||||
|
{
|
||||||
|
return "<HttpWebRequest is null>";
|
||||||
|
}
|
||||||
|
return request.RequestUri.ToString();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Record the error responses of NetHttpClient to display. These responses could not be acquired in other ways.
|
||||||
|
[HarmonyPrefix]
|
||||||
|
[HarmonyPatch(typeof(NetHttpClient), "SetError")]
|
||||||
|
public static void PreSetError(NetHttpClient __instance, HttpWebResponse response)
|
||||||
|
{
|
||||||
|
if (response != null)
|
||||||
|
{
|
||||||
|
errorResponse.Add(__instance, response);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[HarmonyPrefix]
|
||||||
|
[HarmonyPatch(typeof(Packet), "ProcImpl")]
|
||||||
|
public static void PreProcImpl(Packet __instance)
|
||||||
|
{
|
||||||
|
if (request && __instance.State == PacketState.Ready)
|
||||||
|
{
|
||||||
|
var netQuery = __instance.Query;
|
||||||
|
var api = GetApiName(netQuery);
|
||||||
|
var displayRequest = InspectRequest(api, netQuery.GetRequest());
|
||||||
|
MelonLogger.Msg($"[LogNetworkRequests] {api} Request: {displayRequest}");
|
||||||
|
}
|
||||||
|
else if (
|
||||||
|
response &&
|
||||||
|
__instance.State == PacketState.Process &&
|
||||||
|
Traverse.Create(__instance).Field("Client").GetValue() is NetHttpClient client)
|
||||||
|
{
|
||||||
|
if (client.State == NetHttpClient.StateDone && !responseErrorOnly)
|
||||||
|
{
|
||||||
|
var netQuery = __instance.Query;
|
||||||
|
var api = GetApiName(netQuery);
|
||||||
|
var displayResponse = InspectResponse(api, client.GetResponse().ToArray());
|
||||||
|
MelonLogger.Msg($"[LogNetworkRequests] {api} Response: {displayResponse}");
|
||||||
|
}
|
||||||
|
else if (client.State == NetHttpClient.StateError)
|
||||||
|
{
|
||||||
|
var displayError = InspectError(client);
|
||||||
|
MelonLogger.Warning($"[LogNetworkRequests] {GetApiName(__instance.Query)} Error: {displayError}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static string InspectRequest(string api, string request) =>
|
||||||
|
requestOmittedApiList.Contains(api)
|
||||||
|
? $"<{request.Length} characters omitted>"
|
||||||
|
: (request == "" ? "<empty request>" : request);
|
||||||
|
|
||||||
|
private static string InspectResponse(string api, byte[] response)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var decoded = Encoding.UTF8.GetString(response);
|
||||||
|
if (responseOmittedApiList.Contains(api))
|
||||||
|
{
|
||||||
|
return $"<{decoded.Length} characters omitted>";
|
||||||
|
}
|
||||||
|
else if (decoded == "")
|
||||||
|
{
|
||||||
|
return "<empty response>";
|
||||||
|
}
|
||||||
|
else if (decoded.IndexOf("\n") != -1)
|
||||||
|
{
|
||||||
|
return JSON.Dump(decoded);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return decoded;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
// Always non-empty when decoding fails.
|
||||||
|
return $"<Failed to decode text ({JSON.Dump(e.Message)}): {response.Length} bytes " +
|
||||||
|
(responseOmittedApiList.Contains(api)
|
||||||
|
? "omitted"
|
||||||
|
: "[" + BitConverter.ToString(response).Replace("-", " ")) + "]" +
|
||||||
|
">";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static string InspectError(NetHttpClient client) =>
|
||||||
|
"<" +
|
||||||
|
$"WebExceptionStatus.{client.WebException}: " +
|
||||||
|
$"HttpStatus = {client.HttpStatus}, " +
|
||||||
|
$"Error = {JSON.Dump(client.Error)}, " +
|
||||||
|
$"Response = " +
|
||||||
|
(errorResponse.TryGetValue(client, out var response)
|
||||||
|
? InspectErrorResponse(response)
|
||||||
|
: "null") +
|
||||||
|
">";
|
||||||
|
|
||||||
|
private static string InspectErrorResponse(HttpWebResponse response)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var webConnectionStream = response.GetResponseStream();
|
||||||
|
var memoryStream = new MemoryStream();
|
||||||
|
webConnectionStream.CopyTo(memoryStream);
|
||||||
|
return InspectResponse(null, memoryStream.ToArray());
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
// The stream has alraedy been consumed?
|
||||||
|
return $"<Failed to read response stream ({JSON.Dump(e.Message)})>";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue