//using AOT; //using System; //using System.Collections; //using System.Collections.Generic; //using System.Runtime.InteropServices; //using UnityEngine; //public class IPCClient : MonoBehaviour //{ // static IntPtr ipcHandle = IntPtr.Zero; // public static IPCClient Instance // { // get // { // return _i; // } // } // /// // /// 版本id // /// // int version = 1; // static IPCClient _i; // public string PreviewRoomCode = ""; // private void Awake() // { // _i = this; // } // /// // /// IPC是否连上 // /// // public bool IsConnect // { // get;private set; // } // public delegate void _OnDataReceived(string content, uint len, IntPtr ptr); // public delegate void _OnConnected(IntPtr ptr); // public delegate void _OnDisconnect(IntPtr ptr); // public delegate void _OnLog(string content, uint len, IntPtr ptr); // static Queue msgQueue = new Queue(); // private void Start() // { // string[] commandLineArgs = System.Environment.GetCommandLineArgs(); // CommandLineArgs = new Dictionary(); // for (int i = 0; i < commandLineArgs.Length; i++) // { // if (commandLineArgs[i] == "-c" || commandLineArgs[i] == "-pos" || commandLineArgs[i] == "-ipc") // { // CommandLineArgs[commandLineArgs[i]] = commandLineArgs[i + 1]; // } // } // foreach (var item in CommandLineArgs) // { // Debug.Log(item); // } // if (CommandLineArgs.TryGetValue("-c", out string str)) // { // PreviewRoomCode = str; // } // if (CommandLineArgs.TryGetValue("-ipc", out string str1)) // { // GlobalDefine.IPCPath = str1; // } // IsConnect = false; // if (!string.IsNullOrEmpty(GlobalDefine.IPCPath)) // { // Debug.Log("初始化IPC:" + Env.Instance.IPCPath); // ipcHandle = new IntPtr(GetHashCode()); // //设置回调 // IpcInterface.SetDataReceivedCallback(OnDataReceived, ipcHandle); // IpcInterface.SetConnectedCallback(OnConnected, ipcHandle); // IpcInterface.SetDisconnectCallback(OnDisconnect, ipcHandle); // IpcInterface.SetLogCallback(OnLog, ipcHandle); // int result = IpcInterface.InitIpc(GlobalDefine.IPCPath, (uint)GlobalDefine.IPCPath.Length, IpcInterface.IPC_CS_TYPE.CLIENT); // if (result == 0) // { // IsConnect = true; // EventMgr.FireEvent(TEventType.IPCStateChange); // } // else // { // Debug.LogError("IPC 初始化失败 result:" + result.ToString()); // } // } // } // private void Update() // { // lock (msgQueue) // { // if (msgQueue.Count > 0) // { // while (msgQueue.Count > 0) // { // ParseMsg(msgQueue.Dequeue()); // } // } // } // } // /// // /// 解析并下发消息 // /// // /// // void ParseMsg(string content) // { // Dictionary msg = MiniJSON.Deserialize(content) as Dictionary; // if (msg != null) // { // if (msg.TryGetValue("type", out var t)) // { // Debug.Log("type:" + t); // } // Dictionary data = null; // if (msg.TryGetValue("data", out var d)) // { // data = d as Dictionary; // } // switch (t) // { // case "SC_SET_CODE": // if (data != null && data.TryGetValue("code", out var code)) // { // Debug.Log("SC_SET_CODE code:" + code.ToString()); // string oldCode = PreviewRoomCode; // PreviewRoomCode = code.ToString(); // EventMgr.FireEvent(TEventType.PreviewRoomCodeChange, oldCode); // } // break; // case "SC_QUIT": // if (data != null && data.TryGetValue("message", out var message)) // { // Debug.Log("SC_QUIT msg:" + message); // } // Application.Quit(); // break; // default: // //Debug.Log("无效的类型:" + t); // break; // } // } // } // /// // /// 发送IPC消息 // /// // /// // public void SendMsg(string msgType, Dictionary msgData) // { // if (ipcHandle != IntPtr.Zero) // { // Dictionary data = new Dictionary() // { // { "type" ,msgType }, // { "data" , msgData }, // { "version" , version } // }; // string msg = MiniJSON.Serialize(data); // int result = IpcInterface.SendData(msg, (uint)msg.Length); // if (result == -1) // { // Debug.LogError("IPC消息发送失败!"); // } // } // else // { // Debug.LogError("IPC 初始化失败"); // } // } // /// // /// 收到数据 // /// // /// // /// // /// // [MonoPInvokeCallback(typeof(_OnDataReceived))] // static void OnDataReceived(IntPtr contentPtr, uint len, IntPtr ptr) // { // string content = Marshal.PtrToStringUTF8(contentPtr, (int)len); // if (string.IsNullOrEmpty(content)) // { // Debug.Log("ipc call func : OnDataReceived ptr:" + ptr.ToString() + " curPtr:" + ipcHandle.ToString() + " len" + len.ToString() + " content: is null or empty"); // return; // } // Debug.Log("ipc call func : OnDataReceived ptr:" + ptr.ToString() + " curPtr:" + ipcHandle.ToString() + " len" + len.ToString() + " content:" + content); // if (ptr == ipcHandle) // { // lock (msgQueue) // { // msgQueue.Enqueue(content); // } // } // } // /// // /// 连接 // /// // /// // [MonoPInvokeCallback(typeof(_OnConnected))] // static void OnConnected(IntPtr ptr) // { // Debug.Log("ipc call func : OnConnected ptr:" + ptr.ToString() + " curPtr:" + ipcHandle.ToString()); // if (ptr == ipcHandle) // { // Debug.Log("ipc conneted"); // Instance.IsConnect = true; // EventMgr.FireEvent(TEventType.IPCStateChange); // } // } // /// // /// 断开连接 // /// // /// // [MonoPInvokeCallback(typeof(_OnDisconnect))] // static void OnDisconnect(IntPtr ptr) // { // Debug.Log("ipc call func : OnDisconnect ptr:" + ptr.ToString() + " curPtr:" + ipcHandle.ToString()); // if (ptr == ipcHandle) // { // Instance.IsConnect = false; // EventMgr.FireEvent(TEventType.IPCStateChange); // Debug.Log(message: "ipc disconneted"); // //断开连接后自动关闭游戏 // Application.Quit(); // } // } // /// // /// 收到日志 // /// // /// // /// // /// // [MonoPInvokeCallback(typeof(_OnLog))] // static void OnLog(IntPtr contentPtr, uint len, IntPtr ptr) // { // string content = Marshal.PtrToStringUTF8(contentPtr, (int)len); // Debug.Log("ipc call func : OnLog ptr:" + ptr.ToString() + " curPtr:" + ipcHandle.ToString() + " len" + len.ToString() + " content:" + content); // //if (ptr == ipcHandle) // //{ // // Debug.Log("ipc log:" + content); // //} // } // /// // /// 释放ipc信息 // /// // public void ReleaseIPC() // { // if (ipcHandle != IntPtr.Zero) // { // ipcHandle = IntPtr.Zero; // IpcInterface.ReleaseIpc(); // } // } // private void OnDestroy() // { // ReleaseIPC(); // } //}