summaryrefslogtreecommitdiff
path: root/Client/Assets/Scripts/XUtliPoolLib/LoadAsyncTask.cs
blob: f728914b44ec76967d67b196dcd47adbe319af98 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
using System;
using System.Collections.Generic;
using UnityEngine;
using XUpdater;

namespace XUtliPoolLib
{
	public class LoadAsyncTask
	{
		public EAsyncLoadState loadState = EAsyncLoadState.EFree;

		public Type loadType = null;

		public uint hash = 0u;

		public string location = "";

		public string ext = "";

		public bool isSharedRes = true;

		public UnityEngine.Object asset = null;

		public List<LoadInfo> loadCbList = new List<LoadInfo>();

		private AssetBundleInfo assetBundleInfo = null;

		public object cbObj = null;

		public void Clear()
		{
			this.loadState = EAsyncLoadState.EFree;
			this.location = "";
			this.hash = 0u;
			this.asset = null;
			this.loadCbList.Clear();
			this.loadType = null;
			this.assetBundleInfo = null;
			this.cbObj = null;
		}

		public bool Update()
		{
			bool result;
			switch (this.loadState)
			{
			case EAsyncLoadState.EFree:
				result = false;
				break;
			case EAsyncLoadState.EPreLoading:
			{
				bool flag = XSingleton<XUpdater.XUpdater>.singleton.ABManager != null;
				if (flag)
				{
					this.assetBundleInfo = XSingleton<XUpdater.XUpdater>.singleton.ABManager.LoadImm(this.hash, this.location, this.ext, null);
				}
				bool flag2 = this.assetBundleInfo == null;
				if (flag2)
				{
					this.asset = Resources.Load(this.location, this.loadType);
					XResourceLoaderMgr.resourceLoadCount++;
				}
				else
				{
					int num = this.location.LastIndexOf("/");
					bool flag3 = num > 0 && num + 1 < this.location.Length;
					if (flag3)
					{
						string text = this.location.Substring(num + 1);
					}
					else
					{
						string text = this.location;
					}
					this.asset = this.assetBundleInfo.mainObject;
					XResourceLoaderMgr.abLoadCount++;
				}
				this.loadState = EAsyncLoadState.ELoading;
				result = false;
				break;
			}
			case EAsyncLoadState.ELoading:
				this.LoadComplete();
				this.loadState = EAsyncLoadState.EFree;
				result = true;
				break;
			case EAsyncLoadState.EInstance:
			{
				bool flag4 = this.asset != null;
				if (flag4)
				{
					int i = 0;
					int count = this.loadCbList.Count;
					while (i < count)
					{
						LoadInfo loadInfo = this.loadCbList[i];
						bool flag5 = loadInfo.loadCb != null;
						if (flag5)
						{
							GameObject gameObject = XCommon.Instantiate<UnityEngine.Object>(this.asset) as GameObject;
							XResourceLoaderMgr.instanceCount++;
							XSingleton<XResourceLoaderMgr>.singleton.AssetsRefRetain(this.hash);
							XSingleton<XResourceLoaderMgr>.singleton.LogReverseID(gameObject, this.hash);
							loadInfo.loadCb(gameObject, this.cbObj);
						}
						i++;
					}
					this.loadCbList.Clear();
				}
				result = true;
				break;
			}
			default:
				result = false;
				break;
			}
			return result;
		}

		public void CancelLoad(LoadCallBack cb)
		{
			int count = this.loadCbList.Count;
			for (int i = count - 1; i >= 0; i--)
			{
				LoadInfo loadInfo = this.loadCbList[i];
				bool flag = loadInfo.loadCb == cb;
				if (flag)
				{
					this.loadCbList.RemoveAt(i);
				}
			}
		}

		private void ReturnNull()
		{
			int i = 0;
			int count = this.loadCbList.Count;
			while (i < count)
			{
				LoadInfo loadInfo = this.loadCbList[i];
				bool flag = loadInfo.loadCb != null;
				if (flag)
				{
					loadInfo.loadCb(null, this.cbObj);
				}
				i++;
			}
			this.loadCbList.Clear();
		}

		private void LoadComplete()
		{
			bool flag = this.asset == null;
			if (flag)
			{
				XResourceLoaderMgr.LoadErrorLog(this.location);
				this.ReturnNull();
			}
			else
			{
				bool useNewMgr = XSingleton<XResourceLoaderMgr>.singleton.useNewMgr;
				if (useNewMgr)
				{
					XResourceLoaderMgr.UniteObjectInfo uniteObjectInfo;
					bool uoiasync = XSingleton<XResourceLoaderMgr>.singleton.GetUOIAsync(this.location, this.hash, null, this.assetBundleInfo, this.isSharedRes, out uniteObjectInfo);
					bool flag2 = uniteObjectInfo != null;
					if (flag2)
					{
						int i = 0;
						int count = this.loadCbList.Count;
						while (i < count)
						{
							LoadInfo loadInfo = this.loadCbList[i];
							bool flag3 = loadInfo.loadCb != null;
							if (flag3)
							{
								UnityEngine.Object uoi = XSingleton<XResourceLoaderMgr>.singleton.GetUOI(uniteObjectInfo, this.isSharedRes, loadInfo.usePool);
								loadInfo.loadCb(uoi, this.cbObj);
							}
							i++;
						}
						this.loadCbList.Clear();
					}
				}
				else
				{
					XSingleton<XResourceLoaderMgr>.singleton.AddAssetInPool(this.asset, this.hash, this.assetBundleInfo);
					int j = 0;
					int count2 = this.loadCbList.Count;
					while (j < count2)
					{
						LoadInfo loadInfo2 = this.loadCbList[j];
						bool flag4 = loadInfo2.loadCb != null;
						if (flag4)
						{
							bool flag5 = this.isSharedRes;
							if (flag5)
							{
								this.GetSharedResourceCb(loadInfo2.loadCb, this.assetBundleInfo);
							}
							else
							{
								UnityEngine.Object obj = null;
								bool flag6 = loadInfo2.usePool && XSingleton<XResourceLoaderMgr>.singleton.GetInObjectPool(ref obj, this.hash);
								if (flag6)
								{
									loadInfo2.loadCb(obj, this.cbObj);
								}
								else
								{
									this.CreateFromPrefabCb(loadInfo2.loadCb, this.assetBundleInfo);
								}
							}
						}
						j++;
					}
					this.loadCbList.Clear();
				}
			}
		}

		private void GetSharedResourceCb(LoadCallBack loadCb, AssetBundleInfo info = null)
		{
			UnityEngine.Object assetInPool = XSingleton<XResourceLoaderMgr>.singleton.GetAssetInPool(this.hash);
			XSingleton<XResourceLoaderMgr>.singleton.AssetsRefRetain(this.hash);
			loadCb(assetInPool, this.cbObj);
		}

		private void CreateFromPrefabCb(LoadCallBack loadCb, AssetBundleInfo info = null)
		{
			UnityEngine.Object assetInPool = XSingleton<XResourceLoaderMgr>.singleton.GetAssetInPool(this.hash);
			GameObject gameObject = XCommon.Instantiate<UnityEngine.Object>(assetInPool) as GameObject;
			XSingleton<XResourceLoaderMgr>.singleton.AssetsRefRetain(this.hash);
			XSingleton<XResourceLoaderMgr>.singleton.LogReverseID(gameObject, this.hash);
			loadCb(gameObject, this.cbObj);
		}
	}
}