using System; using System.Threading; using UnityEditor; using UnityEngine; using System.IO; using System.Diagnostics; [InitializeOnLoad] public static class CustomPlayModeHandler { static DateTime m_lastModified; static bool m_enterPlayAfterServerReady; static double m_waitServerStartTime; const double WAIT_SERVER_TIMEOUT_SECONDS = 20.0; static CustomPlayModeHandler() { string strLastTime = PlayerPrefs.GetString("LastModifiedTime"); if (strLastTime != null && strLastTime != "") { m_lastModified = DateTime.Parse(strLastTime); //Debug.Log($"LastEditTime : {strLastTime}"); } EditorApplication.playModeStateChanged += OnPlayModeStateChanged; } //[MenuItem("Tools/Run External Exe")] public static void RunExe() { System.Diagnostics.Process pro; string exePath = "../XDevNode/XNode.exe"; // �滻Ϊ���exe�ļ�·�� string curPath = System.IO.Directory.GetCurrentDirectory(); string runPath = $"{curPath}/{exePath}";//@"D:\UD\XWorld\XWorldClient\60s\..\XDevNode\XNode.exe"; System.Diagnostics.ProcessStartInfo info = new System.Diagnostics.ProcessStartInfo(); info.FileName = runPath; info.Arguments = "xid3278136192177 param2=488";//XID info.WorkingDirectory = $"{curPath}/../XDevNode"; try { //Thread.Sleep(500); pro = System.Diagnostics.Process.Start(info); pro.EnableRaisingEvents = true; //pro.Exited += new EventHandler(myProcess_Exited); } catch (System.ComponentModel.Win32Exception ex) { Console.WriteLine("ϵͳ�Ҳ���ָ�����ļ���/r{0}", ex.ToString()); return; } } static bool IsProcessRunning(string processName) { // ��ȡ����ͬ������ Process[] processes = Process.GetProcessesByName(processName); // ����ҵ�����һ�����̣��򷵻� true return processes.Length > 0; } static void StartProcess(string executablePath) { RunExe(); } private static void OnPlayModeStateChanged(PlayModeStateChange state) { if (state == PlayModeStateChange.ExitingEditMode) { if (!m_enterPlayAfterServerReady && !XWorldUtil.IsPcMiniGameGatewayReady()) { UnityEngine.Debug.Log("PC MiniGame Gateway is not ready. Starting it before entering Play Mode..."); EditorApplication.isPlaying = false; m_enterPlayAfterServerReady = true; m_waitServerStartTime = EditorApplication.timeSinceStartup; XWorldUtil.OpenLocalServerAll(); EditorApplication.update -= WaitForServerThenEnterPlay; EditorApplication.update += WaitForServerThenEnterPlay; return; } m_enterPlayAfterServerReady = false; EditorApplication.update -= WaitForServerThenEnterPlay; XWorldUtil.OpenLocalServerAll(); //���lua�Ƿ��б仯������server lua /*if (IsPathChange()) { string srcPath = "../Lua/server"; string dstPath = "../XDevNode/Data/XWorld/Script"; Packager.CopyPath(srcPath, dstPath); //Thread.Sleep(1000); PlayerPrefs.SetString("LastModifiedTime", m_lastModified.ToString()); }//*/ //Debug.Log("��������ģʽ��ִ���Զ����߼���"); UnityEngine.Debug.Log("PC MiniGame Gateway checked. Legacy XNode auto-start is disabled for this flow."); string processName = "XNode"; string executablePath = "../XDevNode/XNode.exe"; if (false && !IsProcessRunning(processName)) { StartProcess(executablePath); } else { UnityEngine.Debug.Log("Legacy XNode server startup skipped."); //Console.WriteLine($"{processName} �Ѿ������С�"); } } else if (state == PlayModeStateChange.ExitingPlayMode) { UnityEngine.Debug.Log("Exist Playing"); } } private static void WaitForServerThenEnterPlay() { if (XWorldUtil.IsPcMiniGameGatewayReady()) { EditorApplication.update -= WaitForServerThenEnterPlay; UnityEngine.Debug.Log("PC MiniGame Gateway is ready. Entering Play Mode."); EditorApplication.EnterPlaymode(); return; } if (EditorApplication.timeSinceStartup - m_waitServerStartTime > WAIT_SERVER_TIMEOUT_SECONDS) { EditorApplication.update -= WaitForServerThenEnterPlay; m_enterPlayAfterServerReady = false; UnityEngine.Debug.LogError("Timed out waiting for PC MiniGame Gateway. Check XWorld/Server/Open Latest Log."); } } public static bool IsPathChange() { if (m_lastModified == null) { m_lastModified = DateTime.Now; UnityEngine.Debug.Log($"Now Time"); return true; } else { DateTime lastModified = m_lastModified; lastModified = RecursiveLastEditTime("../Lua/server", lastModified); if ((lastModified - m_lastModified).TotalSeconds > 1) { m_lastModified = lastModified; UnityEngine.Debug.Log($"LastEditTime Change: {m_lastModified}"); return true; } } return false; } static DateTime RecursiveLastEditTime(string path, DateTime lastTime) { string[] names = Directory.GetFiles(path); string[] dirs = Directory.GetDirectories(path); foreach (string filename in names) { DateTime lastModified = File.GetLastWriteTime(filename); if (lastModified > lastTime) lastTime = lastModified; } foreach (string dir in dirs) { if (dir.IndexOf(".svn") >= 0) { continue; } //paths.Add(dir.Replace('\\', '/')); lastTime = RecursiveLastEditTime(dir, lastTime); } return lastTime; } }