summaryrefslogtreecommitdiff
path: root/Client/Assets/Scripts/XUtliPoolLib/XEngineCommandMgr.cs
blob: 26723a6357466b483aae4a8f10b5d147672c7149 (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
using System;
using System.Collections.Generic;
using UnityEngine;

namespace XUtliPoolLib
{
	public class XEngineCommandMgr : XSingleton<XEngineCommandMgr>
	{
		private SimpleQueue m_currentQueue = null;

		private SimpleQueue m_commandQueue0 = new SimpleQueue();

		private SimpleQueue m_commandQueue1 = new SimpleQueue();

		private SimpleQueue m_commandQueue = new SimpleQueue();

		private SimpleQueue m_objAsyncDataQueue = new SimpleQueue();

		private Queue<GameObject> m_gameObjectQueue = new Queue<GameObject>();

		private SimpleQueue GetCurrentQueue()
		{
			bool flag = this.m_currentQueue == null;
			if (flag)
			{
				this.m_currentQueue = this.m_commandQueue0;
			}
			return this.m_currentQueue;
		}

		private SimpleQueue GeBackQueue(SimpleQueue queue)
		{
			bool flag = queue == this.m_commandQueue0;
			SimpleQueue result;
			if (flag)
			{
				result = this.m_commandQueue1;
			}
			else
			{
				result = this.m_commandQueue0;
			}
			return result;
		}

		private void SwapQueue()
		{
			bool flag = this.m_currentQueue != null;
			if (flag)
			{
				bool flag2 = this.m_currentQueue == this.m_commandQueue0;
				if (flag2)
				{
					this.m_currentQueue = this.m_commandQueue1;
				}
				else
				{
					this.m_currentQueue = this.m_commandQueue0;
				}
			}
		}

		public XEngineCommand CreateCommand(ExecuteCommandHandler handler, XGameObject gameObject, int commandID = -1)
		{
			bool hasData = this.m_commandQueue.HasData;
			XEngineCommand xengineCommand;
			if (hasData)
			{
				xengineCommand = this.m_commandQueue.Dequeue<XEngineCommand>();
			}
			else
			{
				xengineCommand = new XEngineCommand();
			}
			xengineCommand.gameObject = gameObject;
			xengineCommand.handler = handler;
			xengineCommand.objID = gameObject.objID;
			xengineCommand.commandID = commandID;
			xengineCommand.id = XEngineCommand.debugid++;
			return xengineCommand;
		}

		public void AppendCommand(XEngineCommand command)
		{
			bool flag = command.CanExecute();
			if (flag)
			{
				command.Execute();
				this.ReturnCommand(command);
			}
			else
			{
				bool flag2 = XEngineCommand.debug && command.debugHandler != null;
				if (flag2)
				{
					command.debugHandler(command.gameObject, command, "append", command.id);
				}
				SimpleQueue currentQueue = this.GetCurrentQueue();
				currentQueue.Enqueue(command);
			}
		}

		public XObjAsyncData GetObjAsyncData()
		{
			bool hasData = this.m_objAsyncDataQueue.HasData;
			XObjAsyncData result;
			if (hasData)
			{
				result = this.m_objAsyncDataQueue.Dequeue<XObjAsyncData>();
			}
			else
			{
				result = new XObjAsyncData();
			}
			return result;
		}

		public void ReturnCommand(XEngineCommand command)
		{
			bool flag = command.data != null;
			if (flag)
			{
				command.data.Reset();
				this.m_objAsyncDataQueue.Enqueue(command.data);
			}
			command.Reset();
			this.m_commandQueue.Enqueue(command);
		}

		public GameObject GetGameObject()
		{
			bool flag = this.m_gameObjectQueue.Count > 0;
			GameObject result;
			if (flag)
			{
				result = this.m_gameObjectQueue.Dequeue();
			}
			else
			{
				result = new GameObject("EmptyObject");
			}
			return result;
		}

		public void ReturnGameObject(GameObject go)
		{
			bool flag = go != null;
			if (flag)
			{
				Transform transform = go.transform;
				transform.parent = null;
				transform.position = XResourceLoaderMgr.Far_Far_Away;
				transform.rotation = Quaternion.identity;
				transform.localScale = Vector3.one;
				go.name = "EmptyObject";
				this.m_gameObjectQueue.Enqueue(go);
			}
		}

		public void Update()
		{
			SimpleQueue currentQueue = this.GetCurrentQueue();
			bool flag = currentQueue != null && currentQueue.HasData;
			if (flag)
			{
				SimpleQueue simpleQueue = this.GeBackQueue(currentQueue);
				while (currentQueue.HasData)
				{
					XEngineCommand xengineCommand = currentQueue.Dequeue<XEngineCommand>();
					bool flag2 = xengineCommand.IsValid();
					if (flag2)
					{
						bool flag3 = xengineCommand.CanExecute();
						if (flag3)
						{
							bool flag4 = XEngineCommand.debug && xengineCommand.debugHandler != null;
							if (flag4)
							{
								xengineCommand.debugHandler(xengineCommand.gameObject, xengineCommand, "execute", xengineCommand.id);
							}
							xengineCommand.Execute();
							this.ReturnCommand(xengineCommand);
						}
						else
						{
							simpleQueue.Enqueue(xengineCommand);
						}
					}
					else
					{
						bool flag5 = XEngineCommand.debug && xengineCommand.debugHandler != null;
						if (flag5)
						{
							xengineCommand.debugHandler(xengineCommand.gameObject, xengineCommand, "invalid", xengineCommand.id);
						}
						this.ReturnCommand(xengineCommand);
					}
				}
				this.SwapQueue();
			}
		}

		public void Clear()
		{
			while (this.m_commandQueue0.HasData)
			{
				XEngineCommand command = this.m_commandQueue0.Dequeue<XEngineCommand>();
				this.ReturnCommand(command);
			}
			while (this.m_commandQueue1.HasData)
			{
				XEngineCommand command2 = this.m_commandQueue1.Dequeue<XEngineCommand>();
				this.ReturnCommand(command2);
			}
			this.m_currentQueue = this.m_commandQueue0;
			this.m_gameObjectQueue.Clear();
			XEngineCommand.debugid = 0;
			bool debug = XEngineCommand.debug;
			if (debug)
			{
				XSingleton<XDebug>.singleton.AddWarningLog("[EngineCommand] Clear", null, null, null, null, null);
			}
		}
	}
}