diff options
author | chai <chaifix@163.com> | 2021-01-25 14:28:30 +0800 |
---|---|---|
committer | chai <chaifix@163.com> | 2021-01-25 14:28:30 +0800 |
commit | 6eb915c129fc90c6f4c82ae097dd6ffad5239efc (patch) | |
tree | 7dd2be50edf41f36b60fac84696e731c13afe617 /Client/Assets/Scripts/XUtliPoolLib/XTableAsyncLoader.cs |
+scripts
Diffstat (limited to 'Client/Assets/Scripts/XUtliPoolLib/XTableAsyncLoader.cs')
-rw-r--r-- | Client/Assets/Scripts/XUtliPoolLib/XTableAsyncLoader.cs | 171 |
1 files changed, 171 insertions, 0 deletions
diff --git a/Client/Assets/Scripts/XUtliPoolLib/XTableAsyncLoader.cs b/Client/Assets/Scripts/XUtliPoolLib/XTableAsyncLoader.cs new file mode 100644 index 00000000..8573de96 --- /dev/null +++ b/Client/Assets/Scripts/XUtliPoolLib/XTableAsyncLoader.cs @@ -0,0 +1,171 @@ +using System;
+using System.Collections.Generic;
+using System.Threading;
+using UnityEngine;
+using XUpdater;
+
+namespace XUtliPoolLib
+{
+ public sealed class XTableAsyncLoader
+ {
+ public bool IsDone
+ {
+ get
+ {
+ bool flag = false;
+ bool flag2 = this.currentXfra == null;
+ if (flag2)
+ {
+ bool flag3 = XTableAsyncLoader.currentThreadCount < XTableAsyncLoader.AsyncPerTime;
+ if (flag3)
+ {
+ XTableAsyncLoader.currentThreadCount++;
+ }
+ flag = this.InnerExecute();
+ }
+ else
+ {
+ bool isDone = this.currentXfra.IsDone;
+ if (isDone)
+ {
+ bool flag4 = XTableAsyncLoader.currentThreadCount < XTableAsyncLoader.AsyncPerTime;
+ if (flag4)
+ {
+ XTableAsyncLoader.currentThreadCount++;
+ }
+ XBinaryReader.Return(this.currentXfra.Data, false);
+ this.currentXfra = null;
+ flag = this.InnerExecute();
+ }
+ }
+ bool flag5 = flag;
+ if (flag5)
+ {
+ bool flag6 = this._call_back != null;
+ if (flag6)
+ {
+ this._call_back();
+ this._call_back = null;
+ }
+ this._executing = false;
+ }
+ return flag;
+ }
+ }
+
+ private bool _executing = false;
+
+ private List<XFileReadAsync> _task_list = new List<XFileReadAsync>();
+
+ private OnLoadedCallback _call_back = null;
+
+ private XFileReadAsync currentXfra = null;
+
+ public static int currentThreadCount = 0;
+
+ public static readonly int AsyncPerTime = 2;
+
+ public void AddTask(string location, CVSReader reader, bool native = false)
+ {
+ INativePlugin nativePlugin = XSingleton<XUpdater.XUpdater>.singleton.XPlatform.GetNativePlugin();
+ bool flag = native && nativePlugin != null;
+ if (flag)
+ {
+ TextAsset sharedResource = XSingleton<XResourceLoaderMgr>.singleton.GetSharedResource<TextAsset>(location, ".bytes", true, false);
+ bool flag2 = sharedResource != null;
+ if (flag2)
+ {
+ nativePlugin.InputData(0, 0, sharedResource.bytes, sharedResource.bytes.Length);
+ XSingleton<XResourceLoaderMgr>.singleton.UnSafeDestroyShareResource(location, ".bytes", sharedResource, false);
+ }
+ }
+ else
+ {
+ XFileReadAsync xfileReadAsync = new XFileReadAsync();
+ xfileReadAsync.Location = location;
+ xfileReadAsync.Reader = reader;
+ this._task_list.Add(xfileReadAsync);
+ }
+ }
+
+ private bool InnerExecute()
+ {
+ bool flag = this._task_list.Count > 0;
+ bool result;
+ if (flag)
+ {
+ bool flag2 = XTableAsyncLoader.currentThreadCount <= 0;
+ if (flag2)
+ {
+ result = false;
+ }
+ else
+ {
+ XTableAsyncLoader.currentThreadCount--;
+ this.currentXfra = this._task_list[this._task_list.Count - 1];
+ this._task_list.RemoveAt(this._task_list.Count - 1);
+ this.ReadFileAsync(this.currentXfra);
+ result = false;
+ }
+ }
+ else
+ {
+ result = true;
+ }
+ return result;
+ }
+
+ public bool Execute(OnLoadedCallback callback = null)
+ {
+ bool executing = this._executing;
+ bool result;
+ if (executing)
+ {
+ result = false;
+ }
+ else
+ {
+ this._call_back = callback;
+ this._executing = true;
+ this.InnerExecute();
+ result = true;
+ }
+ return result;
+ }
+
+ private void ReadFileAsync(XFileReadAsync xfra)
+ {
+ xfra.Data = XBinaryReader.Get();
+ bool flag = XSingleton<XResourceLoaderMgr>.singleton.ReadText(xfra.Location, ".bytes", xfra.Data, false);
+ bool flag2 = !flag;
+ if (flag2)
+ {
+ XResourceLoaderMgr.LoadErrorLog(xfra.Location);
+ XBinaryReader.Return(xfra.Data, false);
+ xfra.Data = null;
+ xfra.IsDone = true;
+ this.currentXfra = null;
+ }
+ else
+ {
+ ThreadPool.QueueUserWorkItem(delegate(object state)
+ {
+ try
+ {
+ bool flag3 = xfra.Reader.ReadFile(xfra.Data);
+ bool flag4 = !flag3;
+ if (flag4)
+ {
+ XSingleton<XDebug>.singleton.AddErrorLog("in File: ", xfra.Location, xfra.Reader.error, null, null, null);
+ }
+ }
+ catch (Exception ex)
+ {
+ XSingleton<XDebug>.singleton.AddErrorLog(ex.Message, " in File: ", xfra.Location, xfra.Reader.error, null, null);
+ }
+ xfra.IsDone = true;
+ });
+ }
+ }
+ }
+}
|