using System; using System.Collections.Generic; using System.Xml.Linq; using UnityEngine; using XMLTool; namespace QFramework { internal class LoadResAction : IAction { public System.Action OnFinished { get; set; } private LoadResAction() { } private static readonly SimpleObjectPool mPool = new SimpleObjectPool(() => new LoadResAction(), null, 10); string fileName; string type; string index; public static LoadResAction Allocate(string fileName, Dictionary datas, System.Action OnFinished = null) { var retNode = mPool.Allocate(); retNode.ActionID = ActionKit.ID_GENERATOR++; retNode.Deinited = false; retNode.Reset(); retNode.fileName = fileName; retNode.type = datas.ContainsKey("resType") ? datas["resType"] : string.Empty; retNode.index = datas.ContainsKey("index") ? datas["index"] : string.Empty; retNode.OnFinished = OnFinished; return retNode; } public ulong ActionID { get; set; } public ActionStatus Status { get; set; } public void OnStart() { ResLoader resLoader = ResLoader.Allocate(); string path = Global.xmlPath + fileName; switch (type) { case "xml": resLoader.Add2Load(path.ToLocalTextResName(), (success, res) => { if (success) { string xmlStr = res.As().text; if (string.IsNullOrEmpty(xmlStr) == false) { // 加载XML XDocument doc = XDocument.Parse(xmlStr); // 获取根元素 XElement moduleXml = doc.Root; if (string.IsNullOrEmpty(index)) { // 无序加载 XmlParser.LoadModule(moduleXml, Global.Instance.appData); } else { Global.moduleDict.Add(index, moduleXml); } } } }); break; } resLoader.LoadAsync(() => { resLoader.Recycle2Cache(); this.Finish(); }); } public void OnExecute(float dt) { } public void OnFinish() { } public void Reset() { Status = ActionStatus.NotStart; Paused = false; } public bool Paused { get; set; } public void Deinit() { if (!Deinited) { OnFinished = null; Deinited = true; mPool.Recycle(this); } } public bool Deinited { get; set; } } }