Simple code to receive packets from AQ3D. I use this in Game's Update() void, but you can ideally use it anywhere you want as long as it's being rapidly called
Credits: Styx
Ready to paste code in Game class
Code:
using System;
using System.IO;
using System.Text;
using System.Reflection;
using System.Runtime.InteropServices;
using Newtonsoft.Json;
using UnityEngine;
public partial class Game : State
{
// Console Related
bool consoleBooted = false;
bool isSniffing = false;
byte[] payload;
public event Action<string> packetReceived;
public event Action<string> packetSent;
public event Action<Request> beforePacketSent;
static MethodInfo sendMessageMethod;
public string jsonPacket;
[DllImport("kernel32.dll")] static extern bool AllocConsole();
[DllImport("kernel32.dll")] static extern IntPtr GetStdHandle(int nStdHandle);
[DllImport("msvcrt.dll")] static extern int _open_osfhandle(IntPtr h, int flags);
[DllImport("kernel32.dll", SetLastError = true)] static extern bool SetConsoleTitle(string lpConsoleTitle);
void InitConsole()
{
if (consoleBooted) return;
if (!AllocConsole()) return;
IntPtr std = GetStdHandle(-11); // STD_OUTPUT_HANDLE
int safe = _open_osfhandle(std, 0x4000); // _O_TEXT
if (safe < 0) return;
var fs = new FileStream(new Microsoft.Win32.SafeHandles.SafeFileHandle(std, false), FileAccess.Write);
var writer = new StreamWriter(fs) { AutoFlush = true };
Console.SetOut(writer);
Console.SetError(writer);
SetConsoleTitle("AQ3D");
consoleBooted = true;
}
readonly JsonSerializerSettings _jsonCfg = new JsonSerializerSettings {
DefaultValueHandling = DefaultValueHandling.Ignore, NullValueHandling = NullValueHandling.Ignore
};
void beginSniffing()
{
if (sendMessageMethod == null) {
var aec = AEC.getInstance();
sendMessageMethod = aec.GetType().GetMethod("sendMessage", BindingFlags.NonPublic | BindingFlags.Instance);
}
AEC.getInstance().ResponseReceived += HandleResponse;
}
void endSniffing() => AEC.getInstance().ResponseReceived -= HandleResponse;
void Update()
{
if (!isReady) return;
InputManager.Update();
InitConsole();
if (Input.GetKeyDown(KeyCode.J)) {
isSniffing = !isSniffing;
if (isSniffing) {
packetReceived += logInbound;
beginSniffing();
Chat.Notify("Sniffer Started", "[00FF00]");
}
else {
packetReceived -= logInbound;
endSniffing();
Chat.Notify("Sniffer Stopped", "[FF0000]");
}
}
foreach (var npc in entities.NpcList)
npc.Update();
foreach (var player in entities.PlayerList)
player.Update();
if (!UICamera.inputHasFocus && entities.me != null)
ProcessShortcuts();
UpdateCursor();
UpdateDraggable();
}
void logInbound(string json)
{
Console.WriteLine($"[Received] {json}");
}
void HandleResponse(Response r)
{
string payload = JsonConve******rializeObject(r);
packetReceived?.Invoke(payload);
}
}
Also, I don't know why JsonConve******rializeObject is censored, but y'all know what it is.