113 lines
3.2 KiB
C#
Raw Permalink Normal View History

2024-12-18 16:20:06 +08:00
using System;
2025-01-08 17:05:35 +08:00
using System.Collections.Generic;
2024-12-18 16:20:06 +08:00
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<LoadResAction> mPool =
new SimpleObjectPool<LoadResAction>(() => new LoadResAction(), null, 10);
string fileName;
string type;
2025-01-08 17:05:35 +08:00
string index;
public static LoadResAction Allocate(string fileName, Dictionary<string, string> datas, System.Action OnFinished = null)
2024-12-18 16:20:06 +08:00
{
var retNode = mPool.Allocate();
retNode.ActionID = ActionKit.ID_GENERATOR++;
retNode.Deinited = false;
retNode.Reset();
retNode.fileName = fileName;
2025-01-08 17:05:35 +08:00
retNode.type = datas.ContainsKey("resType") ? datas["resType"] : string.Empty;
retNode.index = datas.ContainsKey("index") ? datas["index"] : string.Empty;
2024-12-18 16:20:06 +08:00
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<LocalTextRes>().text;
2024-12-18 16:20:06 +08:00
if (string.IsNullOrEmpty(xmlStr) == false)
{
// <20><><EFBFBD><EFBFBD>XML
XDocument doc = XDocument.Parse(xmlStr);
// <20><>ȡ<EFBFBD><C8A1>Ԫ<EFBFBD><D4AA>
XElement moduleXml = doc.Root;
2025-01-08 17:20:18 +08:00
if (string.IsNullOrEmpty(index))
{
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
XmlParser.LoadModule(moduleXml, Global.Instance.appData);
}
else
{
Global.moduleDict.Add(index, moduleXml);
}
2024-12-18 16:20:06 +08:00
}
}
});
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; }
}
}