-
Notifications
You must be signed in to change notification settings - Fork 0
/
ProtectionBlock.cs
236 lines (202 loc) · 7.9 KB
/
ProtectionBlock.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
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
using UnityEngine;
using System.Diagnostics;
using System;
using System.Linq;
using System.Collections.Generic;
public class ProtectionBlock : Machine
{
public string ID = "unassigned";
public bool visible;
public Material lineMat;
public GameObject connectionObject;
public string creationMethod = "built";
private List<string> userNames;
private StateManager stateManager;
private LineRenderer[] lines;
private Vector3[] vectors;
private Vector3 pos;
private bool init;
private int connectionAttempts;
//! Called by unity engine on start up to initialize variables.
public void Start()
{
stateManager = GameObject.Find("GameManager").GetComponent<StateManager>();
visible = true;
}
//! Called by MachineManager update coroutine.
public override void UpdateMachine()
{
if (stateManager.worldLoaded == true && ID != "unassigned" && init == false && creationMethod == "built")
{
PlayerController player = GameObject.Find("Player").GetComponent<PlayerController>();
float distance = Vector3.Distance(transform.position, player.transform.position);
string userName = PlayerPrefs.GetString("UserName");
if (distance <= 160 && userName != stateManager.worldName)
{
AddUser(userName);
init = true;
}
NetworkPlayer[] networkPlayers = FindObjectsOfType<NetworkPlayer>();
if (networkPlayers != null)
{
foreach (NetworkPlayer netWorkPlayer in networkPlayers)
{
if (netWorkPlayer != null)
{
float networkPlayerDistance = Vector3.Distance(netWorkPlayer.transform.position, transform.position);
if (networkPlayerDistance <= 160)
{
AddUser(netWorkPlayer.gameObject.name);
string[] commandLineOptions = Environment.GetCommandLineArgs();
if (commandLineOptions.Contains("-batchmode") && ID.Length > stateManager.worldName.Length)
{
string logID = ID.Substring(stateManager.worldName.Length);
UnityEngine.Debug.Log("Adding user " + netWorkPlayer.gameObject.name + " to " + logID);
}
init = true;
}
}
}
}
connectionAttempts++;
if (connectionAttempts >= 128)
{
Destroy(gameObject);
}
}
if (vectors == null)
{
vectors = new Vector3[8];
}
if (lines == null)
{
lines = new LineRenderer[12];
for (int i = 0; i < lines.Length; i++)
{
GameObject line = Instantiate(connectionObject, transform.position, transform.rotation);
line.transform.parent = transform;
line.SetActive(true);
lines[i] = line.AddComponent<LineRenderer>();
lines[i].startWidth = 0.2f;
lines[i].endWidth = 0.2f;
lines[i].material = lineMat;
lines[i].loop = true;
lines[i].enabled = true;
}
}
if (vectors != null && lines != null)
{
pos = transform.position;
vectors[0] = new Vector3(pos.x - 100, pos.y - 600, pos.z - 100);
vectors[1] = new Vector3(pos.x + 100, pos.y - 600, pos.z + 100);
vectors[2] = new Vector3(pos.x - 100, pos.y - 600, pos.z + 100);
vectors[3] = new Vector3(pos.x + 100, pos.y - 600, pos.z - 100);
vectors[4] = new Vector3(pos.x - 100, pos.y + 600, pos.z - 100);
vectors[5] = new Vector3(pos.x + 100, pos.y + 600, pos.z + 100);
vectors[6] = new Vector3(pos.x - 100, pos.y + 600, pos.z + 100);
vectors[7] = new Vector3(pos.x + 100, pos.y + 600, pos.z - 100);
lines[0].SetPosition(0, vectors[0]);
lines[0].SetPosition(1, vectors[2]);
lines[1].SetPosition(0, vectors[0]);
lines[1].SetPosition(1, vectors[3]);
lines[2].SetPosition(0, vectors[0]);
lines[2].SetPosition(1, vectors[4]);
lines[3].SetPosition(0, vectors[1]);
lines[3].SetPosition(1, vectors[2]);
lines[4].SetPosition(0, vectors[1]);
lines[4].SetPosition(1, vectors[3]);
lines[5].SetPosition(0, vectors[1]);
lines[5].SetPosition(1, vectors[5]);
lines[6].SetPosition(0, vectors[4]);
lines[6].SetPosition(1, vectors[6]);
lines[7].SetPosition(0, vectors[4]);
lines[7].SetPosition(1, vectors[7]);
lines[8].SetPosition(0, vectors[5]);
lines[8].SetPosition(1, vectors[6]);
lines[9].SetPosition(0, vectors[5]);
lines[9].SetPosition(1, vectors[7]);
lines[10].SetPosition(0, vectors[2]);
lines[10].SetPosition(1, vectors[6]);
lines[11].SetPosition(0, vectors[3]);
lines[11].SetPosition(1, vectors[7]);
for (int i = 0; i < lines.Length; i++)
{
lines[i].enabled = visible;
}
}
}
//! Adds a user to the list of authorized users for this protection block.
public void AddUser(string userName)
{
StackFrame frame = new StackFrame(1);
if (IsValidCaller(frame))
{
if (userNames == null)
{
userNames = new List<string>();
}
userNames.Add(userName);
}
}
//! Adds a user to the list of authorized users for this protection block.
public List<string> GetUserNames()
{
StackFrame frame = new StackFrame(1);
if (IsValidCaller(frame))
{
return userNames;
}
return null;
}
//! Adds a user to the list of authorized users for this protection block.
public void SetUserNames(List<string> names)
{
StackFrame frame = new StackFrame(1);
if (IsValidCaller(frame))
{
userNames = names;
}
}
//! Returns true if the player is an authorized user for this protection block.
public bool IsAuthorizedUser(string userName)
{
StackFrame frame = new StackFrame(1);
if (IsValidCaller(frame))
{
return userNames.Contains(userName);
}
return false;
}
//! Returns true if the method is called from a valid, authorized class.
private bool IsValidCaller(StackFrame frame)
{
string methodName = frame.GetMethod().Name;
string className = frame.GetMethod().DeclaringType.Name;
string assembly = frame.GetMethod().DeclaringType.Assembly.ToString().Split(',')[0];
if (methodName == "MoveNext" && className == "<LoadWorld>d__71" && assembly == "Assembly-CSharp")
{
return true;
}
if (methodName == "MoveNext" && className == "<SaveDataCoroutine>d__4" && assembly == "Assembly-CSharp")
{
return true;
}
if (methodName == "UpdateMachine" && className == "ProtectionBlock" && assembly == "Assembly-CSharp")
{
return true;
}
if (methodName == "CanBuild" && className == "BuildController" && assembly == "Assembly-CSharp")
{
return true;
}
if (methodName == "CanInteract" && className == "InteractionController" && assembly == "Assembly-CSharp")
{
return true;
}
if (methodName == "InteractWithProtectionBlock" && className == "MachineInteraction" && assembly == "Assembly-CSharp")
{
return true;
}
return false;
}
}