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 _taskTemplates = new List(); public List 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 LoadTasksFromPack_Internal(string szPackPath) { long readBytes = 0; var tasks = new List(); using (var fs = new FileStream( szPackPath, FileMode.Open, FileAccess.Read, FileShare.Read)) { TASK_PACK_HEADER tph = AAssit.ReadFromBinaryOf(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(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; } } }