Files
test/Assets/PerfectWorld/Scripts/Task/TaskTemplContainerSO.cs
T
2025-12-17 11:23:10 +07:00

79 lines
2.7 KiB
C#

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 bool _loadAllTasksOnValidate = false;
[SerializeField] private List<ATaskTempl> _taskTemplates = new List<ATaskTempl>();
public List<ATaskTempl> TaskTemplates { get { return _taskTemplates; } }
public int TaskLoadedCount;
public bool LoadAllTasksOnValidate => _loadAllTasksOnValidate;
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;
}
}
}