From 823f3a7e67b5177ca1d6dd472c4e5399cd5821e8 Mon Sep 17 00:00:00 2001 From: sirjonasxx <36828922+sirjonasxx@users.noreply.github.com> Date: Thu, 10 Feb 2022 06:21:02 +0100 Subject: [PATCH] progress fixing unity --- .../unity_tools/UnityWebModifyer.java | 1 + .../services/unity_tools/WasmCodePatcher.java | 6 ++-- .../codepatcher/IncomingPacketPatcher.java | 7 ++++ .../codepatcher/OutgoingPacketPatcher.java | 3 ++ .../unity_tools/js_code/unity_code.js | 15 +++++---- .../unity_tools/js_code/unity_imports.js | 33 +++++++++++-------- 6 files changed, 42 insertions(+), 23 deletions(-) diff --git a/G-Earth/src/main/java/gearth/services/unity_tools/UnityWebModifyer.java b/G-Earth/src/main/java/gearth/services/unity_tools/UnityWebModifyer.java index 3b7f3fb..b9a7c8d 100644 --- a/G-Earth/src/main/java/gearth/services/unity_tools/UnityWebModifyer.java +++ b/G-Earth/src/main/java/gearth/services/unity_tools/UnityWebModifyer.java @@ -125,6 +125,7 @@ public class UnityWebModifyer { contents = contents .replace("var _free", "_free") .replace("var _malloc", "_malloc") + .replace("var Module", "Module") .replace("{{RevisionName}}", revision); BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new GZIPOutputStream(new FileOutputStream(frameworkFile)))); diff --git a/G-Earth/src/main/java/gearth/services/unity_tools/WasmCodePatcher.java b/G-Earth/src/main/java/gearth/services/unity_tools/WasmCodePatcher.java index ba2831b..9d725f3 100644 --- a/G-Earth/src/main/java/gearth/services/unity_tools/WasmCodePatcher.java +++ b/G-Earth/src/main/java/gearth/services/unity_tools/WasmCodePatcher.java @@ -19,12 +19,12 @@ public class WasmCodePatcher { } public void patch() throws IOException, InvalidOpCodeException { - Module module = new Module(file, Arrays.asList( - new SetKeyPatcher(), + Module module = new Module(file, true, Arrays.asList( +// new SetKeyPatcher(), new ReturnBytePatcher(), new OutgoingPacketPatcher(), new IncomingPacketPatcher() )); - module.assembleToFile(file); + module.assembleToFile(file, true); } } diff --git a/G-Earth/src/main/java/gearth/services/unity_tools/codepatcher/IncomingPacketPatcher.java b/G-Earth/src/main/java/gearth/services/unity_tools/codepatcher/IncomingPacketPatcher.java index b3482cd..227fc8a 100644 --- a/G-Earth/src/main/java/gearth/services/unity_tools/codepatcher/IncomingPacketPatcher.java +++ b/G-Earth/src/main/java/gearth/services/unity_tools/codepatcher/IncomingPacketPatcher.java @@ -2,6 +2,10 @@ package gearth.services.unity_tools.codepatcher; import wasm.disassembly.instructions.Instr; import wasm.disassembly.instructions.InstrType; +import wasm.disassembly.instructions.control.IfElseInstr; +import wasm.disassembly.instructions.memory.MemArg; +import wasm.disassembly.instructions.memory.MemInstr; +import wasm.disassembly.instructions.variable.LocalVariableInstr; import wasm.disassembly.modules.sections.code.Func; import wasm.disassembly.modules.sections.code.Locals; import wasm.disassembly.types.FuncType; @@ -52,6 +56,9 @@ public class IncomingPacketPatcher implements StreamReplacement { if (instr.getInstrType() != expectedExpr.get(j)) return false; } + if (((MemInstr)(code.getExpression().getInstructions().get(5))).getMemArg().getAlign() != 2 || + ((MemInstr)(code.getExpression().getInstructions().get(5))).getMemArg().getOffset() != 32) return false; + return true; } diff --git a/G-Earth/src/main/java/gearth/services/unity_tools/codepatcher/OutgoingPacketPatcher.java b/G-Earth/src/main/java/gearth/services/unity_tools/codepatcher/OutgoingPacketPatcher.java index 026fee3..2410f56 100644 --- a/G-Earth/src/main/java/gearth/services/unity_tools/codepatcher/OutgoingPacketPatcher.java +++ b/G-Earth/src/main/java/gearth/services/unity_tools/codepatcher/OutgoingPacketPatcher.java @@ -2,6 +2,7 @@ package gearth.services.unity_tools.codepatcher; import wasm.disassembly.instructions.Instr; import wasm.disassembly.instructions.InstrType; +import wasm.disassembly.instructions.variable.LocalVariableInstr; import wasm.disassembly.modules.sections.code.Func; import wasm.disassembly.types.FuncType; import wasm.disassembly.types.ResultType; @@ -45,6 +46,8 @@ public class OutgoingPacketPatcher implements StreamReplacement { if (expression.get(4).getInstrType() != InstrType.I32_CONST) return false; if (expression.get(5).getInstrType() != InstrType.CALL) return false; + if (((LocalVariableInstr)(expression.get(2))).getLocalIdx().getX() != 1) return false; + return true; } } diff --git a/G-Earth/src/main/resources/gearth/services/unity_tools/js_code/unity_code.js b/G-Earth/src/main/resources/gearth/services/unity_tools/js_code/unity_code.js index b7a8e6e..ce04a2f 100644 --- a/G-Earth/src/main/resources/gearth/services/unity_tools/js_code/unity_code.js +++ b/G-Earth/src/main/resources/gearth/services/unity_tools/js_code/unity_code.js @@ -4,7 +4,7 @@ let revision = "{{RevisionName}}"; let g_ws; -let chachas = []; +let chachas = [-1, -1]; let chachaClass = -1; let out_send_param1 = -1; @@ -20,6 +20,7 @@ let _gearth_incoming_copy; let _malloc; let _free; +let Module; var packetBuff = {"out": [], "in": []}; @@ -97,10 +98,10 @@ function inject_out(packet) { let inject_amount = Math.min(_g_packet_split, packet.length - i); let packet_location = _malloc(inject_amount + 16); - unityInstance.Module.HEAPU8.set(out_packet_objid, packet_location); - unityInstance.Module.HEAPU8.fill(0, packet_location + 4, packet_location + 12); - unityInstance.Module.HEAPU8.set(writeLittleEndian(inject_amount), packet_location + 12); - unityInstance.Module.HEAPU8.set(packet.slice(i, i + inject_amount), packet_location + 16); + Module.HEAPU8.set(out_packet_objid, packet_location); + Module.HEAPU8.fill(0, packet_location + 4, packet_location + 12); + Module.HEAPU8.set(writeLittleEndian(inject_amount), packet_location + 12); + Module.HEAPU8.set(packet.slice(i, i + inject_amount), packet_location + 16); _gearth_outgoing_copy(out_send_param1, packet_location, out_send_param3); _free(packet_location); @@ -128,8 +129,8 @@ function inject_in(packet) { let inject_amount = Math.min(_g_packet_split, packet.length - i); let packet_location = _malloc(inject_amount + 16); - unityInstance.Module.HEAPU8.set(in_packet_prefix, packet_location); - unityInstance.Module.HEAPU8.set(packet.slice(i, i + inject_amount), packet_location + 16); + Module.HEAPU8.set(in_packet_prefix, packet_location); + Module.HEAPU8.set(packet.slice(i, i + inject_amount), packet_location + 16); _gearth_incoming_copy(in_recv_param1, packet_location, 0, inject_amount, 0); _free(packet_location); diff --git a/G-Earth/src/main/resources/gearth/services/unity_tools/js_code/unity_imports.js b/G-Earth/src/main/resources/gearth/services/unity_tools/js_code/unity_imports.js index 221d93c..e602910 100644 --- a/G-Earth/src/main/resources/gearth/services/unity_tools/js_code/unity_imports.js +++ b/G-Earth/src/main/resources/gearth/services/unity_tools/js_code/unity_imports.js @@ -1,10 +1,10 @@ function g_outgoing_packet(param1, param2, param3) { out_send_param1 = param1; out_send_param3 = param3; - out_packet_objid = unityInstance.Module.HEAPU8.slice(param2, param2 + 4); + out_packet_objid = Module.HEAPU8.slice(param2, param2 + 4); - let length = readLittleEndian(unityInstance.Module.HEAPU8.subarray(param2 + 12, param2 + 12 + 4)); - let array = [].slice.call(unityInstance.Module.HEAPU8.subarray(param2 + 12 + 4, param2 + 12 + 4 + length)); + let length = readLittleEndian(Module.HEAPU8.subarray(param2 + 12, param2 + 12 + 4)); + let array = [].slice.call(Module.HEAPU8.subarray(param2 + 12 + 4, param2 + 12 + 4 + length)); packetBuff["out"] = packetBuff["out"].concat(array); @@ -15,9 +15,9 @@ function g_outgoing_packet(param1, param2, param3) { function g_incoming_packet(param1, param2, param3, param4, param5) { in_recv_param1 = param1; - in_packet_prefix = unityInstance.Module.HEAPU8.slice(param2, param2 + 16); + in_packet_prefix = Module.HEAPU8.slice(param2, param2 + 16); - let buffer = unityInstance.Module.HEAPU8.slice(param2 + 16, param2 + 16 + param4); + let buffer = Module.HEAPU8.slice(param2 + 16, param2 + 16 + param4); packetBuff["in"] = packetBuff["in"].concat([].slice.call(buffer)); let packets = collect_packets("in"); @@ -30,20 +30,27 @@ function g_incoming_packet(param1, param2, param3, param4, param5) { } } -function g_chacha_setkey(param1, param2, param3, param4) { - if (chachas.length === 2) { - chachas = []; - } - - chachas.push(param1); -} +// function g_chacha_setkey(param1, param2, param3, param4) { +// if (chachas.length === 2) { +// chachas = []; +// } +// +// chachas.push(param1); +// } function g_chacha_returnbyte(param1, param2, param3) { + console.log("hi"); chachaClass = param3; + if (chachas[0] === -1) { + chachas[0] = param1; + } + else if (chachas[1] === -1 && chachas[0] !== param1) { + chachas[1] = param1; + } return param2; } env["g_outgoing_packet"] = g_outgoing_packet; env["g_incoming_packet"] = g_incoming_packet; -env["g_chacha_setkey"] = g_chacha_setkey; +// env["g_chacha_setkey"] = g_chacha_setkey; env["g_chacha_returnbyte"] = g_chacha_returnbyte; \ No newline at end of file