Files
test/Assets/PerfectWorld/Scripts/Task/TaskTest.cs
T
2025-12-16 16:53:01 +07:00

212 lines
6.8 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Threading;
using BrewMonster.Scripts.Task.UI;
using BrewMonster.Scripts.UI;
using ModelRenderer.Scripts.Common;
using PerfectWorld.Scripts.Task;
using UnityEngine;
namespace BrewMonster.Scripts.Task
{
public class TaskTest : MonoSingleton<TaskTest>
{
public static ATaskTemplMan m_pTaskMan; // use static to store loaded data across instances
public bool WasLoadTaskData = false;
public bool AutoLoadData;
[Header("Test Dlg Award Options")]
[SerializeField] private uint _awardItemID = 1001;
[SerializeField] private KeyCode _awardkey = KeyCode.A;
private CancellationTokenSource _cts;
protected override void Awake()
{
base.Awake();
_cts = new CancellationTokenSource();
}
private void OnValidate()
{
WasLoadTaskData = m_pTaskMan != null && m_pTaskMan.TaskLoadedCount > 0;
if (!WasLoadTaskData && AutoLoadData)
{
LoadTaskData();
}
}
private void OnDestroy()
{
_cts?.Cancel();
_cts?.Dispose();
}
private void OnApplicationQuit()
{
_cts?.Cancel();
}
private void Update()
{
#if UNITY_EDITOR
if (Input.GetKeyDown(KeyCode.Q))
{
var dlgTaskGO = CECUIManager.Instance.GetInGameUIMan().GetDialog("Win_Task");
var dlgTask = (dlgTaskGO) as DlgTask;
if (dlgTask && !dlgTask.gameObject.activeInHierarchy)
{
dlgTask.Show(true);
}
else
{
dlgTask.Show(false);
}
}
if (Input.GetKeyDown(_awardkey))
{
var pShow1 = CECUIManager.Instance.GetInGameUIMan().GetDialog("Win_Award");
CDlgAward pAward = (pShow1) as CDlgAward;
if (pAward && !pAward.gameObject.activeInHierarchy)
{
pAward.UpdateAwardItem((ushort)_awardItemID, true);
pAward.Show(true);
}
else
{
pAward.Show(false);
}
}
if (Input.GetKeyDown(KeyCode.E))
{
var dlg = CECUIManager.Instance.GetInGameUIMan().GetDialog("Win_SkillSubAction");
if (dlg && !dlg.gameObject.activeInHierarchy)
{
dlg.Show(true);
}
else
{
dlg.Show(false);
}
}
#endif
}
// [ContextMenu("Test Npc Service Log")]
// public void LogNpcService()
// {
// // Debug.Log($" idFun = {(ExpTypes.SERVICE_TYPE)(-2147483641)} | idService = {44515} ");
// }
// [ContextMenu("Show Task Name")]
// public void ShowTaskName()
// {
//
// var text01 = ByteToStringUtils.UshortArrayToUnicodeString(m_pTaskMan
// .GetTaskTemplByID((uint)TaskTemplConstants.TASK_SPECIAL_AWARD[0]).m_FixedData.m_szName);
// var text02 = ByteToStringUtils.UshortArrayToUnicodeString(m_pTaskMan
// .GetTaskTemplByID((uint)TaskTemplConstants.TASK_SPECIAL_AWARD[1]).m_FixedData.m_szName);
// Debug.Log($" Task Name 01: {text01} \n Task Name 02: {text02}");
// }
[ContextMenu("Size Of ZONE_VERT")]
public void SizeOfTest()
{
int size = Marshal.SizeOf(typeof(ZONE_VERT));
BMLogger.Log("Size of ATaskTemplFixedData: " + size);
}
[ContextMenu("Load Data")]
async void LoadTaskData()
{
if (m_pTaskMan == null)
{
m_pTaskMan = new ATaskTemplMan();
}
string path = Path.Combine(Application.streamingAssetsPath, "data/tasks.data");
WasLoadTaskData = await m_pTaskMan.LoadTasksFromPack(path, true,(x)=>{},_cts.Token);
}
[ContextMenu("Test Size")]
void TestSize()
{
var size = Marshal.SizeOf(typeof(ATaskTemplFixedData));
BMLogger.Log("Size of ATaskTemplFixedData: " + size);
}
[ContextMenu("🧾 Log Struct Field Sizes (ATaskTemplFixedData)")]
private void LogStructFieldSizes()
{
Type structType = typeof(ATaskTemplFixedData);
FieldInfo[] fields = structType.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
Debug.Log($"===== Struct: {structType.Name} =====");
foreach (var field in fields)
{
string name = field.Name;
Type fieldType = field.FieldType;
int sizeInBytes = -1;
string note = "";
// 🟢 1. Nếu field có [MarshalAs], lấy SizeConst
var marshalAttr = field.GetCustomAttribute<MarshalAsAttribute>();
if (marshalAttr != null && marshalAttr.SizeConst > 0)
{
int elemSize = GetElementSize(fieldType);
sizeInBytes = elemSize * marshalAttr.SizeConst;
note = $"(ByValArray × {marshalAttr.SizeConst})";
}
else
{
try
{
sizeInBytes = Marshal.SizeOf(fieldType);
}
catch
{
note = "(reference type / variable size)";
}
}
string sizeText = sizeInBytes >= 0 ? $"{sizeInBytes} bytes" : "unknown size";
Debug.Log($"{name,-40} | {fieldType.Name,-25} | {sizeText} {note}");
}
try
{
int total = Marshal.SizeOf(structType);
Debug.Log($"===== Total Struct Size: {total} bytes =====");
}
catch
{
Debug.Log($"⚠️ Struct {structType.Name} contains non-blittable fields; total size unknown.");
}
}
/// <summary>
/// Tính kích thước phần tử cơ bản của mảng blittable
/// </summary>
private int GetElementSize(Type type)
{
if (type.IsArray)
{
Type elemType = type.GetElementType();
if (elemType != null)
{
try { return Marshal.SizeOf(elemType); }
catch { return 1; } // fallback
}
}
return Marshal.SizeOf(type);
}
}
}