cache TaskTempl Data into SO

This commit is contained in:
MinhHai
2025-12-17 10:49:19 +07:00
parent ff50d807a7
commit c15108fcf5
10 changed files with 3264902 additions and 1 deletions
@@ -15,6 +15,11 @@ MonoBehaviour:
m_GroupName: Default Local Group
m_GUID: 712e3991f28e549e7a56ee582a977810
m_SerializeEntries:
- m_GUID: 1b653230886be4009808803501ad7d7f
m_Address: Assets/PerfectWorld/SO/TaskTemplContainerSO.asset
m_ReadOnly: 0
m_SerializedLabels: []
FlaggedDuringContentUpdateRestriction: 0
- m_GUID: 1cfde61ea9d19614a8ea91cb1eeca97b
m_Address: "\u7A0B\u5E8F\u8054\u5165/\u89D2\u8272\u51FA\u73B0\u4EBA\u7C7B.gfx"
m_ReadOnly: 0
+8
View File
@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 5f3d86049520f4217970358d8c6e0fd1
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 1b653230886be4009808803501ad7d7f
NativeFormatImporter:
externalObjects: {}
mainObjectFileID: 11400000
userData:
assetBundleName:
assetBundleVariant:
+1 -1
View File
@@ -1,3 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:dacbdce679588961c84b8352291bd17e3716bc1ee2561e3dd140add411b495c6
oid sha256:0930e07b267dc93d54fbc4de6bfaac5a9bf1aa7ea5ff239a58e5a8a7a25cf6c7
size 10723
@@ -4,6 +4,7 @@ using BrewMonster.Scripts.Task;
namespace PerfectWorld.Scripts.Task
{
[Serializable]
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct ATaskTemplFixedData
{
@@ -8,6 +8,8 @@ using System.Threading;
using CSNetwork.GPDataType;
using Cysharp.Threading.Tasks;
using UnityEngine;
using UnityEngine.AddressableAssets;
using UnityEngine.ResourceManagement.AsyncOperations;
namespace BrewMonster.Scripts.Task
{
@@ -83,6 +85,15 @@ namespace BrewMonster.Scripts.Task
public async UniTask<bool> LoadTasksFromPack(string szPackPath, bool bLoadDescript, Action<float> onProgress, CancellationToken token)
{
bool wasLoaded = await LoadTaskTemplFromSO();
if (wasLoaded)
{
BMLogger.Log($" [ATaskTemplMan] Loaded task templates from ScriptableObject.");
onProgress?.Invoke(1f);
return true;
}
// //TaskInterface::WriteLog(0, 0, 2, "LoadPack begin");
// BMLogger.Log("[Dat]- szPackPath: " + szPackPath);
// if (!File.Exists(szPackPath))
@@ -1360,6 +1371,57 @@ namespace BrewMonster.Scripts.Task
return true;
}
public async UniTask<bool> LoadTaskTemplFromSO()
{
const string addressKey = "Assets/PerfectWorld/SO/TaskTemplContainerSO.asset"; // set this to the real address
AsyncOperationHandle<TaskTemplContainerSO> handle = default;
try
{
handle = Addressables.LoadAssetAsync<TaskTemplContainerSO>(addressKey);
await handle.Task;
if (handle.Status != AsyncOperationStatus.Succeeded)
{
BMLogger.LogError($"[ATaskTemplMan] LoadTaskTemplFromSO failed: {handle.OperationException}");
return false;
}
var config = handle.Result;
if (config == null)
{
BMLogger.LogError("[ATaskTemplMan] LoadTaskTemplFromSO returned null result");
return false;
}
var loadedTasks = config.TaskTemplates;
if (loadedTasks == null || loadedTasks.Count == 0)
{
BMLogger.LogError("[ATaskTemplMan] LoadTaskTemplFromSO, no task templates found in SO");
return false;
}
for (int i = 0; i < loadedTasks.Count; i++)
{
AddOneTaskTempl(loadedTasks[i]);
g_ulNewCount++;
}
Debug.Log($"Finished loading {m_TaskTemplMap.Count} task templates.");
return true;
}
catch (Exception e)
{
BMLogger.LogError($"[ATaskTemplMan] LoadTaskTemplFromSO exception: {e}");
return false;
}
finally
{
if (handle.IsValid())
Addressables.Release(handle);
}
}
}
[ StructLayout(LayoutKind.Sequential, Pack = 1) ]
@@ -1486,8 +1486,10 @@ namespace BrewMonster.Scripts.Task
/// 任务模板类 // Task Template Class
/// include
/// </summary>
[Serializable]
public partial class ATaskTempl
{
[SerializeField]
public ATaskTemplFixedData m_FixedData;
public bool is_in_zone(ZONE_VERT _min, ZONE_VERT _max, float[] pos)
@@ -0,0 +1,76 @@
using System;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
namespace BrewMonster.Scripts.Task
{
[ CreateAssetMenu(fileName = "TaskTemplContainerSO", menuName = "BrewMonster/Task/TaskTemplContainerSO")]
public class TaskTemplContainerSO : ScriptableObject
{
public const ulong TASK_PACK_MAGIC = 0x93858361;
public const ulong _task_templ_cur_version = 121;
[SerializeField] private List<ATaskTempl> _taskTemplates = new List<ATaskTempl>();
public List<ATaskTempl> TaskTemplates { get { return _taskTemplates; } }
public int TaskLoadedCount;
private void OnValidate()
{
TaskLoadedCount = _taskTemplates ==null ? 0 : _taskTemplates.Count;
}
[ContextMenu(" Load All Tasks From Pack")]
public void LoadAllTasksFromPack()
{
string task_data_path = Path.Combine(Application.streamingAssetsPath, "data/tasks.data");
_taskTemplates = LoadTasksFromPack_Internal(task_data_path);
Debug.Log($"[TaskTemplContainerSO] Loaded {_taskTemplates.Count} task templates from pack.");
}
private static List<ATaskTempl> LoadTasksFromPack_Internal(string szPackPath)
{
long readBytes = 0;
var tasks = new List<ATaskTempl>();
using (var fs = new FileStream(
szPackPath,
FileMode.Open,
FileAccess.Read,
FileShare.Read))
{
TASK_PACK_HEADER tph =
AAssit.ReadFromBinaryOf<TASK_PACK_HEADER>(fs, ref readBytes);
if (tph.magic != TASK_PACK_MAGIC ||
tph.version != _task_templ_cur_version)
throw new Exception("Invalid task pack header");
if (tph.item_count == 0)
return tasks;
uint[] pOffs =
AAssit.ReadArrayFromBinary<uint>(fs, (int)tph.item_count, ref readBytes);
const float TASK_LOAD_WEIGHT = 0.8f;
for (int i = 0; i < tph.item_count; i++)
{
fs.Seek(pOffs[i], SeekOrigin.Begin);
ATaskTempl templ = new ATaskTempl();
if (!templ.LoadFromBinFile(fs))
continue;
tasks.Add(templ);
}
}
return tasks;
}
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: da89386b7a1d4c75b768f686888bac2a
timeCreated: 1765890592