This repository has been archived by the owner on Jan 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
CommonZones.cs
335 lines (322 loc) · 13.9 KB
/
CommonZones.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
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
using CommonZones.API;
using CommonZones.Providers;
using CommonZones.Zones;
using Rocket.Core;
using Rocket.Core.Plugins;
using SDG.Unturned;
using Steamworks;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using UnityEngine;
namespace CommonZones;
public partial class CommonZones : RocketPlugin<CommonZonesConfig>
{
internal static CommonZones I = null!;
internal bool IsLoaded = false;
internal static CultureInfo Locale = new CultureInfo("en-US");
public static IZoneProvider ZoneProvider = null!;
internal static string DataDirectory = null!;
private static AssemblyName AssemblyName = null!;
private bool HasLoaded = false;
// Library support checks.
internal bool HasNewtonsoft = false;
internal bool HasSysTextJson = false;
internal bool HasMySqlData = false;
internal bool HasMySqlConnector = false;
internal bool HasSysXml = false;
internal Type? ZoneProviderType = null;
/// <summary>Called after <see cref="Load"/> has been called. This is where <see cref="API.Tags.Tags.RegisterPluginTags"/> should be called.</summary>
/// <remarks>If you're looking for where to register plugin zones, look at <see cref="API.Zones.OnRegisterPluginZones"/>.</remarks>
public static event System.Action? OnLoaded;
/// <summary>Called after all zones have been read (after level load).</summary>
public static event System.Action? OnZonesLoaded;
/// <summary>Called just before the plugin is unloaded.</summary>
public static event System.Action? OnUnloading;
protected override void Load()
{
I = this;
AssemblyName = Assembly.GetName();
Translation.InitTranslations();
L.LoadColoredConsole();
L.Log("Loading " + AssemblyName.Name + " by BlazingFlame#0001: " + AssemblyName.Version, ConsoleColor.Magenta);
DataDirectory = Path.Combine(System.Environment.CurrentDirectory, "Plugins", AssemblyName.Name) + Path.DirectorySeparatorChar;
LibCheck();
if (ZoneProviderType == null) return;
SubscribeEvents();
Tags.TagManager.RegisterTagsFromAssembly(Assembly);
if (HasLoaded)
{
OnLevelLoaded(2); // rocket reload
}
IsLoaded = true;
HasLoaded = true;
OnLoaded?.Invoke();
}
private void LibCheck()
{
bool sTxtJson1 = false, newtonsoft1 = false, sqld1 = false, sqlc1 = false, xml1 = false;
// private Dictionary<AssemblyName, string> RocketPluginManager.libraries from R.Plugins
Dictionary<AssemblyName, string>? libs = (Dictionary<AssemblyName, string>?)typeof(RocketPluginManager)?.GetField("libraries", BindingFlags.NonPublic | BindingFlags.Instance)?.GetValue(R.Plugins);
bool v = libs != null;
if (v)
{
// Rocket doesn't load libraries until a type from them are needed,
// so we need to check all the discovered libraries that have yet to be loaded.
// If they aren't there then we dont check for the type.
foreach (KeyValuePair<AssemblyName, string> kvp in libs!)
{
string n = kvp.Key.FullName;
if (n.IndexOf("System.Text.Json", StringComparison.OrdinalIgnoreCase) != -1)
sTxtJson1 = true;
else if (n.IndexOf("Newtonsoft.Json", StringComparison.OrdinalIgnoreCase) != -1)
newtonsoft1 = true;
else if (n.IndexOf("MySql.Data", StringComparison.OrdinalIgnoreCase) != -1)
sqld1 = true;
else if (n.IndexOf("MySqlConnector", StringComparison.OrdinalIgnoreCase) != -1)
sqlc1 = true;
else if (n.IndexOf("System.Xml", StringComparison.OrdinalIgnoreCase) != -1)
xml1 = true;
}
// Also check if the library was already loaded by unturned or rocket's dependencies.
if (!sTxtJson1 || !newtonsoft1 || !sqld1 || !sqlc1 || !xml1)
{
Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
for (int i = 0; i < assemblies.Length; ++i)
{
string n = assemblies[i].FullName;
if (n.IndexOf("System.Text.Json", StringComparison.OrdinalIgnoreCase) != -1)
sTxtJson1 = true;
else if (n.IndexOf("Newtonsoft.Json", StringComparison.OrdinalIgnoreCase) != -1)
newtonsoft1 = true;
else if (n.IndexOf("MySql.Data", StringComparison.OrdinalIgnoreCase) != -1)
sqld1 = true;
else if (n.IndexOf("MySqlConnector", StringComparison.OrdinalIgnoreCase) != -1)
sqlc1 = true;
else if (n.IndexOf("System.Xml", StringComparison.OrdinalIgnoreCase) != -1)
xml1 = true;
}
}
}
// If the libraries exist, try to get some random types to make sure the library is properly loaded and what we're expecting.
Type? sysTextJson = !v || sTxtJson1 ? Type.GetType("System.Text.Json.Utf8JsonReader, System.Text.Json", false, false) : null;
Type? newtonsoft = !v || newtonsoft1 ? Type.GetType("Newtonsoft.Json.JsonReader, Newtonsoft.Json", false, false) : null;
Type? mySqlConn = (!v || sqld1 ? Type.GetType("MySql.Data.MySqlClient.MySqlConnection, MySql.Data", false, false) : null)
?? (!v || sqlc1 ? Type.GetType("MySqlConnector.MySqlClient.MySqlConnection, MySqlConnector", false, false) : null);
Type? xml = !v || xml1 ? Type.GetType("System.Xml.XmlReader, System.Xml", false, false) : null;
if (!v && (sysTextJson == null || newtonsoft == null || mySqlConn == null || xml == null))
L.Log("You can ignore any dependency errors above.", ConsoleColor.DarkGray);
HasSysTextJson = sysTextJson != null && sysTextJson.Assembly.GetName().FullName.IndexOf("System.Text.Json", StringComparison.OrdinalIgnoreCase) != -1;
HasNewtonsoft = newtonsoft != null && newtonsoft .Assembly.GetName().FullName.IndexOf("Newtonsoft.Json", StringComparison.OrdinalIgnoreCase) != -1;
HasSysXml = xml != null && xml .Assembly.GetName().FullName.IndexOf("System.Xml", StringComparison.OrdinalIgnoreCase) != -1;
if (mySqlConn == null)
{
HasMySqlData = false;
HasMySqlConnector = false;
}
else
{
HasMySqlData = mySqlConn.Assembly.GetName().FullName.IndexOf("MySql.Data", StringComparison.OrdinalIgnoreCase) != -1;
if (!HasMySqlData)
HasMySqlConnector = mySqlConn.Assembly.GetName().FullName.IndexOf("MySqlConnector", StringComparison.OrdinalIgnoreCase) != -1;
}
if (HasSysTextJson)
L.Log("Found System.Text.Json " + sysTextJson!.Assembly.GetName().Version, ConsoleColor.Magenta);
if (HasNewtonsoft)
L.Log("Found Newtonsoft.Json " + newtonsoft!.Assembly.GetName().Version, ConsoleColor.Magenta);
if (HasMySqlData)
L.Log("Found MySql.Data " + mySqlConn!.Assembly.GetName().Version, ConsoleColor.Magenta);
else if (HasMySqlConnector)
L.Log("Found MySqlConnector " + mySqlConn!.Assembly.GetName().Version, ConsoleColor.Magenta);
if (HasSysXml)
L.Log("Found System.Xml " + xml!.Assembly.GetName().Version, ConsoleColor.Magenta);
if (!Enum.TryParse(Configuration.Instance.StorageType, true, out EZoneStorageType zoneStorage))
{
goto badzone;
}
switch (zoneStorage)
{
case EZoneStorageType.JSON:
if (!HasSysTextJson)
{
if (!HasNewtonsoft)
{
ZoneProviderType = null;
L.LogError("To read zone data with JSON one of the following libraries must be available in Rocket\\Libraries: `Newtonsoft.Json` or `System.Text.Json`.");
throw new ZoneAPIException();
}
else
{
ZoneProviderType = typeof(NewtonsoftJsonZoneProvider);
}
}
else
{
ZoneProviderType = typeof(SysTextJsonZoneProvider);
}
break;
case EZoneStorageType.MYSQL:
if (!HasMySqlData)
{
if (!HasMySqlConnector)
{
ZoneProviderType = null;
L.LogError("To read zone data from MySQL one of the following libraries must be available in Rocket\\Libraries: `MySql.Data` or `MySqlConnector`.");
throw new ZoneAPIException();
}
else
{
ZoneProviderType = typeof(MySqlConnZoneProvider);
}
}
else
{
ZoneProviderType = typeof(MySqlDataZoneProvider);
}
break;
case EZoneStorageType.XML:
if (!HasSysXml)
{
ZoneProviderType = null;
L.LogError("To read zone data from XML the library `System.Xml` must be available in Rocket\\Libraries.");
throw new ZoneAPIException();
}
else
{
ZoneProviderType = typeof(XmlZoneProvider);
}
break;
default: goto badzone;
}
if (ZoneProviderType != null)
{
if (ZoneProviderType.GetInterface(nameof(IZoneProvider)) == null)
{
L.LogError("Type " + ZoneProviderType.Name + " does not implement " + nameof(IZoneProvider) + "!");
ZoneProviderType = null;
throw new ZoneAPIException();
}
else if (ZoneProviderType.GetConstructor(new Type[] { typeof(FileInfo) }) == null)
{
L.LogError("Type " + ZoneProviderType.Name + " does not have a matching constructor: " + ZoneProviderType.Name + "(FileInfo)");
ZoneProviderType = null;
throw new ZoneAPIException();
}
}
return;
badzone:
ZoneProviderType = null;
L.LogError("Invalid zone storage type in config, unloading plugin. Options: JSON, MYSQL, XML");
throw new ZoneAPIException();
}
protected override void Unload()
{
if (!IsLoaded) return;
OnUnloading?.Invoke();
IsLoaded = false;
if (ZoneProvider != null)
{
ZoneProvider.Dispose();
ZoneProvider = null!;
}
L.Log("Unloading CommonZones by BlazingFlame#0001.");
UnsubscribeEvents();
}
private void SubscribeEvents()
{
Level.onLevelLoaded += OnLevelLoaded;
//API.Zones.OnRegisterPluginZones += OnRegisterZones;
Provider.onServerConnected += OnPlayerConnected;
}
private void UnsubscribeEvents()
{
Level.onLevelLoaded -= OnLevelLoaded;
//API.Zones.OnRegisterPluginZones -= OnRegisterZones;
Provider.onServerConnected -= OnPlayerConnected;
}
private void OnLevelLoaded(int level)
{
if (ZoneProviderType == null)
{
L.LogError("Failed to load because a provider type was not selected.");
return;
}
Zone.OnLevelLoaded();
try
{
ZoneProvider = (IZoneProvider)Activator.CreateInstance(ZoneProviderType, new FileInfo(Path.Combine(DataDirectory, "zones.json")));
ZoneProvider.Reload();
}
catch (ZoneAPIException)
{
UnloadPlugin(Rocket.API.PluginState.Failure);
return;
}
ZonePlayerComponent.UIInit();
OnZonesLoaded?.Invoke();
}
private void OnPlayerConnected(CSteamID playerid)
{
Player? player = null;
for (int i = 0; i < Provider.clients.Count; ++i)
{
if (Provider.clients[i].playerID.steamID.m_SteamID == playerid.m_SteamID)
{
player = Provider.clients[i].player;
break;
}
}
if (player == null)
{
L.LogWarning("Failed to resolve " + playerid.ToString() + " as a steam64 id.");
return;
}
L.Log(player.name + " connected.", ConsoleColor.Cyan);
player.gameObject.AddComponent<ZonePlayerComponent>().Init(player);
}
private void OnRegisterZones(ZoneBuilderCollection zones)
{
ZoneBuilder zone = new ZoneBuilder()
{
Name = "O'Leary Prison",
ShortName = "Prison",
UseMapCoordinates = true,
X = 750,
Z = 1005,
Radius = 133
};
zones.Add(zone);
zone = new ZoneBuilder()
{
Name = "Summerside Military Base",
ShortName = "Summerside",
UseMapCoordinates = true,
X = 532,
Z = 292.5f
};
zone.WithRectSize(122, 123);
zones.Add(zone);
zone = new ZoneBuilder()
{
Name = "Montague",
UseMapCoordinates = true,
X = 1341,
Z = 1081,
Points = new Vector2[]
{
new Vector2(1345, 1045),
new Vector2(1345, 1100),
new Vector2(1264, 1100),
new Vector2(1264, 1117),
new Vector2(1418, 1117),
new Vector2(1418, 1100),
new Vector2(1362, 1100),
new Vector2(1362, 1045)
}
};
zones.Add(zone);
}
}