summaryrefslogtreecommitdiff
path: root/Client/Assets/Scripts/XMainClient/XGame.cs
blob: cd375fcfaedfcb999429e15803b151f3992ee834 (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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
using System;
using System.Collections;
using System.Collections.Generic;
using System.Threading;
using KKSG;
using ProtoBuf;
using UnityEngine;
using XMainClient.UI;
using XMainClient.UI.UICommon;
using XUpdater;
using XUtliPoolLib;

namespace XMainClient
{
	public class XGame : XSingleton<XGame>
	{
		public bool IsGMAccount
		{
			get
			{
				return this._isgmaccount;
			}
			set
			{
				this._isgmaccount = value;
			}
		}

		public bool ShowBuildLog
		{
			get
			{
				return this._show_build_log;
			}
			set
			{
				this._show_build_log = value;
			}
		}

		public ulong PlayerID
		{
			get
			{
				return this._player_id;
			}
			set
			{
				this._player_id = value;
			}
		}

		public bool CalcFps
		{
			get
			{
				return this._calc_fps;
			}
		}

		public float Fps
		{
			get
			{
				return this._fps;
			}
		}

		public float FpsAvg
		{
			get
			{
				return this._fpsAvg;
			}
		}

		public XStage CurrentStage
		{
			get
			{
				return this._stage;
			}
		}

		public bool StageReady
		{
			get
			{
				return this._stage != null && this._stage.IsEntered;
			}
		}

		internal XDocuments Doc
		{
			get
			{
				return this._doc;
			}
		}

		public int SyncModeValue
		{
			get
			{
				return this._nSyncMode;
			}
			set
			{
				this._nSyncMode = value;
				Physics.IgnoreLayerCollision(10, 10, this.SyncMode);
				Physics.IgnoreLayerCollision(21, 13, this.SyncMode);
			}
		}

		public bool SyncMode
		{
			get
			{
				return this._nSyncMode == 1;
			}
		}

		public static int RoleCount;

		private XStage _stage = null;

		private XStage _old_stage = null;

		private List<XBaseSingleton> _singletons = null;

		private XDocuments _doc = null;

		private int _nSyncMode = 0;

		private bool _show_build_log = true;

		private int _fps_count = 0;

		private float _fps = (float)XShell.TargetFrame;

		private float _fpsAvg = (float)XShell.TargetFrame;

		private float _fps_interval = 0.16667f;

		private bool _calc_fps = true;

		private float _fps_real_time = 0f;

		private int _fpsCalcCount = 0;

		private float _fpsAcc = 0f;

		private bool _isgmaccount = false;

		private ulong _player_id = 0UL;

		private int _millisecondsToWait = 0;

		private float _lastExitClickTime = 0f;

		private XTimerMgr.ElapsedEventHandler _fpsHandler = null;

		private XTimerMgr.ElapsedEventHandler _innerSwitchCb = null;

		public bool notLoadScene = true;

		public bool networkRun = true;

		public bool switchScene = false;

		public static bool EditorMode = false;

		public XGame()
		{
			this._singletons = new List<XBaseSingleton>();
			this._singletons.Add(XSingleton<BufferPoolMgr>.singleton);
			this._singletons.Add(XSingleton<ScriptCode>.singleton);
			XSingleton<XStringTable>.singleton.Uninit();
			this._singletons.Add(XSingleton<XStringTable>.singleton);
			this._singletons.Add(XSingleton<XInput>.singleton);
			this._singletons.Add(XSingleton<XServerTimeMgr>.singleton);
			this._singletons.Add(XSingleton<XGlobalConfig>.singleton);
			this._singletons.Add(XSingleton<XGameUI>.singleton);
			this._singletons.Add(XSingleton<XSceneMgr>.singleton);
			this._singletons.Add(XSingleton<XBuffTemplateManager>.singleton);
			this._singletons.Add(XSingleton<XAudioMgr>.singleton);
			this._singletons.Add(XSingleton<XSkillEffectMgr>.singleton);
			this._singletons.Add(XSingleton<XPostEffectMgr>.singleton);
			this._singletons.Add(XSingleton<XLevelFinishMgr>.singleton);
			this._singletons.Add(XSingleton<XCombat>.singleton);
			this._singletons.Add(XSingleton<XProfessionSkillMgr>.singleton);
			this._singletons.Add(XSingleton<XGameSysMgr>.singleton);
			this._singletons.Add(XSingleton<XOperationRecord>.singleton);
			this._singletons.Add(XSingleton<XLevelDoodadMgr>.singleton);
			this._singletons.Add(XSingleton<XTutorialMgr>.singleton);
			this._singletons.Add(XSingleton<XEntityMgr>.singleton);
			this._singletons.Add(XSingleton<XScene>.singleton);
			this._singletons.Add(XSingleton<XForbidWordMgr>.singleton);
			this._singletons.Add(XSingleton<XSkillFactory>.singleton);
			this._singletons.Add(XSingleton<XCutScene>.singleton);
			this._singletons.Add(XSingleton<XBulletMgr>.singleton);
			this._singletons.Add(XSingleton<XAttributeMgr>.singleton);
			this._singletons.Add(XSingleton<XEventMgr>.singleton);
			this._singletons.Add(XSingleton<XPowerPointCalculator>.singleton);
			this._singletons.Add(XSingleton<UIManager>.singleton);
			this._singletons.Add(XSingleton<XLevelSpawnMgr>.singleton);
			this._fpsHandler = new XTimerMgr.ElapsedEventHandler(this.CalculateFPS);
			this._innerSwitchCb = new XTimerMgr.ElapsedEventHandler(this.InnerSwitch);
			this._doc = new XDocuments();
		}

		public IEnumerator Awake()
		{
			XLinkTimeStamp.FetchBuildDateTime();
			this.TriggerFps();
			PtcRegister.RegistProtocol();
			XSingleton<XResourceLoaderMgr>.singleton.LoadServerCurve("Table/Curve");
			XSingleton<XClientNetwork>.singleton.Initialize();
			CVSReader.Init();
			int i = 0;
			int num;
			for (i = 0; i < this._singletons.Count; i = num + 1)
			{
				int tryCount = 0;
				while (!this._singletons[i].Init())
				{
					num = tryCount;
					tryCount = num + 1;
					bool flag = tryCount % 1000 == 0;
					if (flag)
					{
						Thread.Sleep(1);
					}
				}
				num = i;
			}
			GC.Collect();
			i = 0;
			XBagDocument.Execute(new OnLoadedCallback(XBagDocument.OnTableLoaded));
			while (!XBagDocument.AsyncLoader.IsDone)
			{
				num = i + 1;
				i = num;
				bool flag2 = num % 1000 == 0;
				if (flag2)
				{
					Thread.Sleep(1);
				}
			}
			this._doc.CtorLoad();
			this._doc.Initilize(0);
			Serializer.SetMultiThread(XSingleton<XGlobalConfig>.singleton.GetSettingEnum(ESettingConfig.EMultiThreadProtoBuf));
			Serializer.SetSkipProtoIgnore(XSingleton<XGlobalConfig>.singleton.GetSettingEnum(ESettingConfig.ESkipProtoIgnore));
			XAutoFade.MakeBlack(true);
			XOptionsDocument optDoc = XDocuments.GetSpecificDocument<XOptionsDocument>(XOptionsDocument.uuID);
			bool flag3 = optDoc != null;
			if (flag3)
			{
				int value = optDoc.GetValue(XOptionsDefine.OD_SMOOTH);
				XSingleton<XGameUI>.singleton.SetUIOptOption(false, value == 1, false, false);
			}
			bool settingEnum = XSingleton<XGlobalConfig>.singleton.GetSettingEnum(ESettingConfig.EApm);
			if (settingEnum)
			{
				XSingleton<XUpdater.XUpdater>.singleton.XPlatform.InitApm();
			}
			XResourceLoaderMgr.UseCurveTable = XSingleton<XGlobalConfig>.singleton.GetSettingEnum(ESettingConfig.ELoadCurveTable);
			XSingleton<XResourceLoaderMgr>.singleton.DelayLoad = XSingleton<XGlobalConfig>.singleton.GetSettingEnum(ESettingConfig.EDelayLoad);
			XFxMgr.FilterFarFx = XSingleton<XGlobalConfig>.singleton.GetSettingEnum(ESettingConfig.EFilterFarFx);
			yield return null;
			yield break;
		}

		public override bool Init()
		{
			XQualitySetting.InitResolution();
			bool flag = (int)Application.platform == 7 || (int)Application.platform == 0|| (int)Application.platform == 2;
			if (flag)
			{
				Application.targetFrameRate = 60;
			}
			else
			{
				Application.targetFrameRate = XShell.TargetFrame;
			}
			XAutoFade.FadeIn(0f, true);
			this.SwitchTo(EXStage.Login, 3u);
			return true;
		}

		public override void Uninit()
		{
			this.SwitchTo(EXStage.Null, 0u);
			this._doc.Uninitilize();
			for (int i = this._singletons.Count - 1; i >= 0; i--)
			{
				this._singletons[i].Uninit();
			}
			XSingleton<XClientNetwork>.singleton.Close(NetErrCode.Net_NoError);
			XSingleton<XInterfaceMgr>.singleton.Uninit();
			INativePlugin nativePlugin = XSingleton<XUpdater.XUpdater>.singleton.XPlatform.GetNativePlugin();
			bool flag = nativePlugin != null;
			if (flag)
			{
				nativePlugin.CloseHooks();
			}
		}

		public void UpdateNetwork()
		{
			bool flag = this.networkRun;
			if (flag)
			{
				XSingleton<XClientNetwork>.singleton.Update();
			}
		}

		public void PreUpdate(float fDeltaT)
		{
			bool flag = this.StageReady && this.notLoadScene;
			if (flag)
			{
				XSingleton<XInput>.singleton.Update();
				this._stage.PreUpdate(fDeltaT);
			}
		}

		public void Update(float fDeltaT)
		{
			bool flag = this.StageReady && this.notLoadScene;
			if (flag)
			{
				this._stage.Update(fDeltaT);
				this._doc.Update(fDeltaT);
				XSingleton<UIManager>.singleton.Update(fDeltaT);
			}
			bool calc_fps = this._calc_fps;
			if (calc_fps)
			{
				this._fps_count++;
			}
			bool key = Input.GetKey((KeyCode)284);
			if (key)
			{
				XSingleton<X3DTouchMgr>.singleton.OnProcess3DTouch("");
			}
		}

		private void CheckExit()
		{
			bool keyDown = Input.GetKeyDown((KeyCode)27);
			if (keyDown)
			{
				bool flag = Time.time - this._lastExitClickTime < float.Parse(XSingleton<XGlobalConfig>.singleton.GetValue("DoubleClickExitTime"));
				if (flag)
				{
					Application.Quit();
				}
				XSingleton<UiUtility>.singleton.ShowSystemTip(XSingleton<XStringTable>.singleton.GetString("EXIT_GAME_TIP"), "fece00");
				this._lastExitClickTime = Time.time;
			}
		}

		public void PostUpdate(float fDeltaT)
		{
			bool flag = this.StageReady && this.notLoadScene;
			if (flag)
			{
				this._stage.PostUpdate(fDeltaT);
				this._doc.PostUpdate(fDeltaT);
				XSingleton<UIManager>.singleton.PostUpdate(fDeltaT);
			}
		}

		public void SwitchTo(EXStage eStage, uint sceneID)
		{
			bool flag = sceneID == XSingleton<XScene>.singleton.SceneID && this.CurrentStage.Stage == eStage && sceneID != 3u && XSingleton<XScene>.singleton.SceneType != SceneType.SCENE_TOWER;
			if (flag)
			{
				XSingleton<XDebug>.singleton.AddLog("switch to the same scene id ", sceneID.ToString(), " with same stage.", null, null, null, XDebugColor.XDebug_None);
			}
			bool flag2 = eStage == EXStage.Null || sceneID == 0u;
			if (flag2)
			{
				this._old_stage = this._stage;
				this._old_stage.OnLeaveStage(EXStage.Null);
				this._old_stage.OnLeaveScene(false);
				XSingleton<XScene>.singleton.OnLeaveScene(false);
				this._old_stage = null;
			}
			else
			{
				int num = XFastEnumIntEqualityComparer<EXStage>.ToInt(eStage);
				uint num2 = (uint)(num << 24 | (int)sceneID);
				bool flag3 = this._stage == null || sceneID != XSingleton<XScene>.singleton.SceneID || XSingleton<XSceneMgr>.singleton.GetSceneSwitchToSelf(sceneID);
				if (flag3)
				{
					XSingleton<XScene>.singleton.UntriggerScene();
				}
				bool flag4 = (eStage == EXStage.World || eStage == EXStage.Hall) && XSingleton<XScene>.singleton.TurnToTransfer(sceneID);
				if (flag4)
				{
					XAutoFade.FadeOut(0.5f);
					XSingleton<XTimerMgr>.singleton.SetTimer(1f, this._innerSwitchCb, num2 | 2147483648u);
				}
				else
				{
					float sceneDelayTransfer = XSingleton<XSceneMgr>.singleton.GetSceneDelayTransfer(sceneID);
					bool flag5 = sceneDelayTransfer > 0f;
					if (flag5)
					{
						XAutoFade.FadeOut(sceneDelayTransfer);
						XSingleton<XTimerMgr>.singleton.SetTimer(sceneDelayTransfer + 0.03f, this._innerSwitchCb, num2);
					}
					else
					{
						this.InnerSwitch(num2);
					}
				}
			}
		}

		public XStage CreateSpecifiecStage(EXStage eStage)
		{
			XStage result;
			switch (eStage)
			{
			case EXStage.Login:
				result = XStage.CreateSpecificStage<XLoginStage>();
				break;
			case EXStage.SelectChar:
				result = XStage.CreateSpecificStage<XSelectcharStage>();
				break;
			case EXStage.World:
				result = XStage.CreateSpecificStage<XWorldStage>();
				break;
			case EXStage.Hall:
				result = XStage.CreateSpecificStage<XHallStage>();
				break;
			default:
				result = null;
				break;
			}
			return result;
		}

		public void TriggerFps()
		{
			bool calc_fps = this._calc_fps;
			if (calc_fps)
			{
				XSingleton<XTimerMgr>.singleton.SetGlobalTimer(this._fps_interval, this._fpsHandler, null);
			}
		}

		private void InnerSwitch(object o)
		{
			this.switchScene = true;
			uint num = (uint)o;
			bool flag = num >> 31 > 0u;
			uint num2 = num & 16777215u;
			EXStage exstage = (EXStage)(num >> 24 & 127u);
			bool flag2 = flag;
			if (flag2)
			{
				XAutoFade.MakeBlack(false);
			}
			bool flag3 = XSingleton<XScene>.singleton.SceneID == 100u;
			if (flag3)
			{
				XAutoFade.FadeIn(0.2f, false);
			}
			this._old_stage = this._stage;
			this._stage = ((this._old_stage != null && exstage == this._old_stage.Stage) ? this._old_stage : this.CreateSpecifiecStage(exstage));
			bool flag4 = this._old_stage != null;
			if (flag4)
			{
				this._old_stage.OnLeaveStage(this._stage.Stage);
				bool flag5 = num2 != XSingleton<XScene>.singleton.SceneID || XSingleton<XSceneMgr>.singleton.GetSceneSwitchToSelf(num2);
				if (flag5)
				{
					XSingleton<XGameUI>.singleton.m_uiTool.EnableUILoadingUpdate(false);
					this._old_stage.OnLeaveScene(flag);
					bool flag6 = XSingleton<XScene>.singleton.SceneID > 0u;
					if (flag6)
					{
						XSingleton<XScene>.singleton.OnLeaveScene(flag);
					}
					XSingleton<XScene>.singleton.LoadSceneAsync(num2, exstage, !flag, flag);
				}
				else
				{
					this.OnEnterStage();
					this.switchScene = false;
				}
			}
			else
			{
				XSingleton<XScene>.singleton.LoadSceneAsync(num2, exstage, true, false);
			}
		}

		private void LockFps(int goal)
		{
			float num = 1f / (float)goal - Time.smoothDeltaTime;
			bool flag = num > 0f;
			if (flag)
			{
				this._millisecondsToWait++;
			}
			else
			{
				this._millisecondsToWait--;
			}
			Thread.Sleep((int)Mathf.Clamp((float)this._millisecondsToWait, 0f, 1f / (float)goal * 1000f));
		}

		private void CalculateFPS(object o)
		{
			float realtimeSinceStartup = Time.realtimeSinceStartup;
			this._fps = (float)this._fps_count / (realtimeSinceStartup - this._fps_real_time);
			this._fpsCalcCount++;
			this._fpsAcc += this._fps;
			bool flag = this._fpsCalcCount >= 20;
			if (flag)
			{
				this._fpsAvg = this._fpsAcc / (float)this._fpsCalcCount;
				this._fpsCalcCount = 0;
				this._fpsAcc = 0f;
			}
			bool flag2 = DlgBase<XMainInterface, XMainInterfaceBehaviour>.singleton.MainDoc != null;
			if (flag2)
			{
				bool flag3 = DlgBase<XMainInterface, XMainInterfaceBehaviour>.singleton.MainDoc.GetFpsState == XMainInterfaceDocument.GetFps.start;
				if (flag3)
				{
					DlgBase<XMainInterface, XMainInterfaceBehaviour>.singleton.MainDoc.FpsStartTime = realtimeSinceStartup;
					DlgBase<XMainInterface, XMainInterfaceBehaviour>.singleton.MainDoc.PeakFps = this._fps;
					DlgBase<XMainInterface, XMainInterfaceBehaviour>.singleton.MainDoc.FpsCount = this._fps_count;
					DlgBase<XMainInterface, XMainInterfaceBehaviour>.singleton.MainDoc.GetFpsState = XMainInterfaceDocument.GetFps.running;
				}
				else
				{
					bool flag4 = DlgBase<XMainInterface, XMainInterfaceBehaviour>.singleton.MainDoc.GetFpsState == XMainInterfaceDocument.GetFps.running;
					if (flag4)
					{
						DlgBase<XMainInterface, XMainInterfaceBehaviour>.singleton.MainDoc.PeakFps = Math.Max(this._fps, DlgBase<XMainInterface, XMainInterfaceBehaviour>.singleton.MainDoc.PeakFps);
						DlgBase<XMainInterface, XMainInterfaceBehaviour>.singleton.MainDoc.FpsCount += this._fps_count;
					}
				}
			}
			this._fps_real_time = realtimeSinceStartup;
			this._fps_count = 0;
			XSingleton<XTimerMgr>.singleton.SetGlobalTimer(this._fps_interval, this._fpsHandler, null);
		}

		private void OnEnterStage()
		{
			this._stage.OnEnterStage((this._old_stage != null) ? this._old_stage.Stage : EXStage.Null);
		}

		public void OnEnterScene(uint sceneid, bool bTransfer = false)
		{
			bool bHall = XSingleton<XSceneMgr>.singleton.GetSceneType(sceneid) == SceneType.SCENE_HALL || XSingleton<XSceneMgr>.singleton.GetSceneType(sceneid) == SceneType.SCENE_GUILD_HALL || XSingleton<XSceneMgr>.singleton.GetSceneType(sceneid) == SceneType.SCENE_LEISURE;
			XSingleton<XScene>.singleton.GameCamera.PreInstall(GameObject.Find("Main Camera"), bHall);
			XSingleton<XScene>.singleton.OnEnterScene(sceneid, bTransfer);
			this._stage.OnEnterScene(sceneid, bTransfer);
			XSingleton<XScene>.singleton.GameCamera.Installed();
			XSingleton<XScene>.singleton.AssociatedCamera = XSingleton<XScene>.singleton.GameCamera.UnityCamera;
			XSingleton<XAIGeneralMgr>.singleton.InitAIMgr();
			this.OnEnterStage();
		}

		public string GetSyncModeString()
		{
			bool flag = this._nSyncMode == 0;
			string result;
			if (flag)
			{
				result = " Solo Mode ";
			}
			else
			{
				bool flag2 = this._nSyncMode == 1;
				if (flag2)
				{
					result = " OnLine Mode ";
				}
				else
				{
					bool flag3 = this._nSyncMode == 2;
					if (flag3)
					{
						result = " Mixed Mode ";
					}
					else
					{
						result = "Unknown";
					}
				}
			}
			return result;
		}

		public static void SetVisible(GameObject go, int layer)
		{
			go.layer = layer;
			for (int i = 0; i < go.transform.childCount; i++)
			{
				Transform child = go.transform.GetChild(i);
				XGame.SetVisible(child.gameObject, layer);
			}
		}
	}
}