-
Notifications
You must be signed in to change notification settings - Fork 0
/
ASingleton.cs
180 lines (143 loc) · 4.48 KB
/
ASingleton.cs
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
// ------------------------------------------------------------------------------
// <autogenerated>
// This code was generated by a tool.
// Mono Runtime Version: 4.0.30319.1
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </autogenerated>
// ------------------------------------------------------------------------------
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
namespace RMX
{
public interface ISingleton {
string name { get; }
// ISingleton Singleton { get; set; }
bool Destroyed { get; }
GameObject gameObject { get; }
}
public static class Singletons {
// static bool _gameControllerInitialized = false;
public static bool GameControllerInitialized {
get {
return _gameController != null;//_gameControllerInitialized;
}
}
public interface IDebugHUD
{
}
static IGameController _gameController;
public static IGameController GameController {
get{
return _gameController;
}
}
public abstract class ASingleton<T> : RMXObject, ISingleton, EventListener
where T : RMXObject, EventListener, ISingleton
{
private static T _singleton = null;
private static bool _isInitialized = false;
protected virtual bool SetupComplete {
get{
return true;
}
}
public static bool IsInitialized {
get {
return _isInitialized && (_singleton as ASingleton<T>).SetupComplete;
}
}
protected virtual IGameController gameController {
get {
return _gameController;
}
}
public static T current {
get {
if (IsInitialized) {
return _singleton as T;
} else {
if (typeof(T) is IGameController)
throw new System.Exception(typeof(T).Name + "Should never be initialized with static Getter method 'current'");
if (typeof(T) is IDebugHUD)
throw new System.Exception("DebugHUD should not be accessed through 'ASingleton.current' as a Singleton");
return Initialize() as T;
}
}
}
const string tempName = "324329hrNhfeuwh9";
private bool _destroyed = false;
public bool Destroyed {
get {
return _destroyed;
}
}
public static T Initialize() {
if (typeof(T) is IGameController)
throw new System.Exception(typeof(T).Name + "Should never be initialized with static Getter method 'current'");
if (IsInitialized)
return _singleton;
else {
var go = new GameObject(tempName);
ISingleton aSingleton = go.AddComponent<T> ();
if (aSingleton == null || (aSingleton as ISingleton).Destroyed) {
return null;
}
aSingleton.gameObject.name = aSingleton.GetType ().Name;
if (!(aSingleton is IGameController) && Singletons.GameControllerInitialized) {
var parent = Singletons.GameController.gameObject;
aSingleton.gameObject.transform.SetParent (parent.transform);
}
return aSingleton as T;
}
}
private void MainInitCheck() {
if (this is IGameController) {
Singletons._gameController = this as IGameController;
_gameController.Patch();
}
else if (_gameController == null)
Debug.LogWarning ("GameController should be initialized before " + this.GetType().Name);
}
protected virtual bool WillInitialize {
get {
return true;
}
}
/// <summary>
/// Checks whether a singleton already exists. If so, object is destroyed.
/// Otherwise it checks whether the EventListener methods have been overriden. If so, the object is added to the global EventListeners.
/// </summary>
protected override void Awake() {
if (!WillInitialize) {
_destroyed = true;
return;
}
var message = "__new__ <color=lightblue>" + this.GetType().Name + "</color>()";
if (_singleton == null) {
DontDestroyOnLoad (gameObject);
_singleton = this as T;// as T;
MainInitCheck();
base.Awake();
}
else if (_singleton != this) {
if (gameObject.name == tempName) {// gameObject.name == this.GetType().Name &&
message += " -- <color=red> DELETING REDUNDANT " + this.GetType().Name + "</color>()";
_destroyed = true;
Destroy (gameObject);
Destroy (this);
} else {
message += " -- <color=orange> DELETING REDUNDANT ASingleton: </color> " + this.GetType().Name + "</color>()";
_destroyed = true;
Destroy(this);
}
}
if (Bugger.WillLog (RMXTests.Singletons, message))
Debug.Log (Bugger.Last);
_isInitialized = true;
}
}
}
}