This commit is contained in:
VDH
2025-09-12 18:06:54 +07:00
parent 755f5bca26
commit 57f4db66f7
6 changed files with 171 additions and 8 deletions
+160
View File
@@ -0,0 +1,160 @@
// Assets/Editor/EditorLogTools.cs
#if UNITY_EDITOR
using System;
using System.IO;
using UnityEditor;
using UnityEngine;
public static class EditorLogTools
{
// ===== Public Menu =====
[MenuItem("Tools/Logs/Clear Editor.log")]
public static void ClearEditorLogMenu()
{
string path = GetEditorLogPath();
if (string.IsNullOrEmpty(path))
{
EditorUtility.DisplayDialog("Editor.log", "Không xác định được đường dẫn Editor.log trên hệ điều hành này.", "OK");
return;
}
if (!File.Exists(path))
{
EditorUtility.DisplayDialog("Editor.log", $"Không tìm thấy file:\n{path}", "OK");
return;
}
try
{
TruncateFile(path);
EditorUtility.DisplayDialog("Editor.log", "Đã xoá sạch nội dung Editor.log ✅", "OK");
}
catch (Exception e)
{
Debug.LogError($"[EditorLogTools] Clear failed: {e}");
EditorUtility.DisplayDialog("Editor.log", "Xoá thất bại. Xem Console để biết chi tiết.", "OK");
}
}
[MenuItem("Tools/Logs/Trim Editor.log…")]
public static void TrimEditorLogMenu()
{
string input = EditorUtility.DisplayDialogComplex("Trim Editor.log",
"Chọn kích thước còn lại của Editor.log sau khi cắt:",
"Giữ 256 KB", "Giữ 1 MB", "Tự nhập (KB)") switch
{
0 => "256",
1 => "1024",
_ => EditorUtility.DisplayDialog("Nhập dung lượng", "Nhập số KB muốn giữ lại (ví dụ 512):", "OK")
? "512" // fallback, Unity không có input prompt chuẩn; giữ default
: null
};
if (string.IsNullOrEmpty(input)) return;
if (!int.TryParse(input, out int keepKb) || keepKb < 1) keepKb = 512;
string path = GetEditorLogPath();
if (!File.Exists(path))
{
EditorUtility.DisplayDialog("Editor.log", $"Không tìm thấy file:\n{path}", "OK");
return;
}
try
{
TrimTail(path, keepKb * 1024);
EditorUtility.DisplayDialog("Editor.log", $"Đã cắt Editor.log, giữ lại ~{keepKb} KB cuối cùng ✅", "OK");
}
catch (Exception e)
{
Debug.LogError($"[EditorLogTools] Trim failed: {e}");
EditorUtility.DisplayDialog("Editor.log", "Trim thất bại. Xem Console để biết chi tiết.", "OK");
}
}
[MenuItem("Tools/Logs/Open Editor.log")]
public static void OpenEditorLogMenu()
{
string path = GetEditorLogPath();
if (File.Exists(path))
EditorUtility.OpenWithDefaultApp(path);
else
EditorUtility.DisplayDialog("Editor.log", $"Không tìm thấy file:\n{path}", "OK");
}
[MenuItem("Tools/Logs/Reveal log folder")]
public static void RevealLogFolder()
{
string path = GetEditorLogPath();
string dir = string.IsNullOrEmpty(path) ? null : Path.GetDirectoryName(path);
if (!string.IsNullOrEmpty(dir) && Directory.Exists(dir))
EditorUtility.RevealInFinder(dir);
else
EditorUtility.DisplayDialog("Editor.log", "Không mở được thư mục log.", "OK");
}
// ===== Core =====
private static void TruncateFile(string path)
{
// Cho phép Editor tiếp tục ghi khi ta truncate (FileShare.ReadWrite)
using var fs = new FileStream(path, FileMode.Open, FileAccess.Write, FileShare.ReadWrite);
fs.SetLength(0);
fs.Flush(true);
}
private static void TrimTail(string path, int keepBytes)
{
var fi = new FileInfo(path);
long size = fi.Length;
if (size <= keepBytes)
return; // không cần cắt
// đọc phần đuôi rồi ghi đè lại
byte[] buffer;
using (var fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
fs.Seek(size - keepBytes, SeekOrigin.Begin);
buffer = new byte[keepBytes];
int read = fs.Read(buffer, 0, keepBytes);
if (read < keepBytes)
{
Array.Resize(ref buffer, read);
}
}
using (var fs = new FileStream(path, FileMode.Open, FileAccess.Write, FileShare.ReadWrite))
{
fs.SetLength(0);
fs.Write(buffer, 0, buffer.Length);
fs.Flush(true);
}
}
// ===== OS Paths =====
private static string GetEditorLogPath()
{
// Tham chiếu đường dẫn theo Unity docs
switch (Application.platform)
{
case RuntimePlatform.WindowsEditor:
// C:\Users\<user>\AppData\Local\Unity\Editor\Editor.log
string localApp = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
return Path.Combine(localApp, "Unity", "Editor", "Editor.log");
case RuntimePlatform.OSXEditor:
// ~/Library/Logs/Unity/Editor.log
string home = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
return Path.Combine(home, "Library", "Logs", "Unity", "Editor.log");
case RuntimePlatform.LinuxEditor:
// ~/.config/unity3d/Editor.log (đường dẫn phổ biến cho Editor)
string homeLinux = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
return Path.Combine(homeLinux, ".config", "unity3d", "Editor.log");
default:
return null;
}
}
}
#endif
+2
View File
@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: b640a9ed9ecce724cb2cd5891d01b4ff
Binary file not shown.
@@ -104,6 +104,7 @@ namespace BrewMonster.Network
}
public static void EnterWorldAsync(RoleInfo roleInfo, Action callback = null)
{
Debug.Log("EnterWorldAsync !!!!! nay ");
Instance._gameSession.EnterWorldAsync(roleInfo, callback);
}
@@ -83,9 +83,8 @@ namespace BrewMonster.UI
private void OnSelectRoleComplete(RoleInfo roleInfo)
{
// now we have to enter the world
/*UnityGameSession.SendProtocol(
UnityGameSession.SendProtocol(
new enterworld()
{
Roleid = roleInfo.roleid,
@@ -96,9 +95,9 @@ namespace BrewMonster.UI
Settime = 0,
Timeout = 0
}
);*/
Debug.Log("OnSelectRoleComplete");
UnityGameSession.EnterWorldAsync(roleInfo, OnEnterWorldComplete);
);
/* Debug.Log("OnSelectRoleComplete");
UnityGameSession.EnterWorldAsync(roleInfo, OnEnterWorldComplete);*/
}
private async void OnEnterWorldComplete()
+4 -3
View File
@@ -182,6 +182,7 @@ public class CECHostPlayer : MonoBehaviour
}
public void ProcessMessage(in ECMSG Msg)
{
Debug.LogWarning("HoangDev : ProcessMessageProcessMessageProcessMessage");
switch ((int)Msg.dwMsg)
{
case int value when value == EC_MsgDef.MSG_HST_CORRECTPOS: OnMsgHstCorrectPos(Msg); break;
@@ -235,18 +236,18 @@ public class CECHostPlayer : MonoBehaviour
}
public void OnMsgHstCorrectPos(in ECMSG Msg)
{
Debug.Log("OnMsgHstCorrectPos");
Debug.LogWarning("HoangDev : OnMsgHstCorrectPos");
byte[] buf = (byte[])Msg.dwParam1; // chỗ bạn lưu pDataBuf
GCHandle handle = GCHandle.Alloc(buf, GCHandleType.Pinned);
cmd_host_correct_pos pCmd = (cmd_host_correct_pos)Marshal.PtrToStructure(
handle.AddrOfPinnedObject(), typeof(cmd_host_correct_pos));
handle.Free();
Debug.LogWarning("pCmd.pos " + pCmd.pos);
Debug.LogWarning("HoangDev :pCmd.pos " + pCmd.pos);
SetPos(pCmd.pos);
}
public void OnMsgHstGoto(in ECMSG Msg)
{
Debug.Log("OnMsgHstGoto");
Debug.Log("HoangDev :OnMsgHstGoto");
cmd_notify_hostpos pCmd = (cmd_notify_hostpos)Msg.dwParam1;
}