forked from iiDk-the-actual/Console
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServerData.cs
More file actions
274 lines (216 loc) · 10.7 KB
/
ServerData.cs
File metadata and controls
274 lines (216 loc) · 10.7 KB
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
using GorillaNetworking;
using HarmonyLib;
using MonoMod.Utils;
using Photon.Pun;
using Photon.Realtime;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using UnityEngine;
using UnityEngine.Networking;
using Valve.Newtonsoft.Json;
using Valve.Newtonsoft.Json.Linq;
namespace Console
{
public class ServerData : MonoBehaviour
{
#region Configuration
public static readonly bool ServerDataEnabled = true; // Disables Console, telemetry, and admin panel
public static bool DisableTelemetry = false; // Disables telemetry data being sent to the server
// Use this to host your own servers. You can use a hosting service, or you can self-host. Know that self-hosting servers will use your ip address.
public const string ServerEndpoint = "put your server endpoint link here";
public static readonly string ServerDataEndpoint = $"{ServerEndpoint}/serverdata.json"; // I use jsons because its easier to pull data, but you can change this to your liking (Delete this when you implement console into your mod)
// The dictionary used to assign the admins only seen in your mod. Remove the two slashes and input the prompts down below.
public static readonly Dictionary<string, string> LocalAdmins = new Dictionary<string, string>()
{
// { "Placeholder Admin UserID", "Placeholder Admin Name" },
};
public static void SetupAdminPanel(string playerName) { } // Method used to spawn admin panel
#endregion
#region Server Data Code
private static ServerData instance;
private static readonly List<string> DetectedModsLabelled = new List<string>();
private static float DataLoadTime = -1f;
private static float ReloadTime = -1f;
private static int LoadAttempts;
private static bool GivenAdminMods;
public static bool OutdatedVersion;
public void Awake()
{
instance = this;
DataLoadTime = Time.time + 5f;
NetworkSystem.Instance.OnJoinedRoomEvent += OnJoinRoom;
NetworkSystem.Instance.OnPlayerJoined += UpdatePlayerCount;
NetworkSystem.Instance.OnPlayerLeft += UpdatePlayerCount;
}
public void Update()
{
if (DataLoadTime > 0f && Time.time > DataLoadTime && GorillaComputer.instance.isConnectedToMaster)
{
DataLoadTime = Time.time + 5f;
LoadAttempts++;
if (LoadAttempts >= 3)
{
Console.Log("Server data could not be loaded");
DataLoadTime = -1f;
return;
}
Console.Log("Attempting to load web data");
instance.StartCoroutine(LoadServerData());
}
if (ReloadTime > 0f)
{
if (Time.time > ReloadTime)
{
ReloadTime = Time.time + 60f;
instance.StartCoroutine(LoadServerData());
}
}
else
{
if (GorillaComputer.instance.isConnectedToMaster)
ReloadTime = Time.time + 5f;
}
if (Time.time > DataSyncDelay || !PhotonNetwork.InRoom)
{
if (PhotonNetwork.InRoom && PhotonNetwork.PlayerList.Length != PlayerCount)
instance.StartCoroutine(PlayerDataSync(PhotonNetwork.CurrentRoom.Name, PhotonNetwork.CloudRegion));
PlayerCount = PhotonNetwork.InRoom ? PhotonNetwork.PlayerList.Length : -1;
}
}
public static void OnJoinRoom() =>
instance.StartCoroutine(TelementryRequest(PhotonNetwork.CurrentRoom.Name, PhotonNetwork.NickName, PhotonNetwork.CloudRegion, PhotonNetwork.LocalPlayer.UserId, PhotonNetwork.CurrentRoom.IsVisible, PhotonNetwork.PlayerList.Length, NetworkSystem.Instance.GameModeString));
public static string CleanString(string input, int maxLength = 12)
{
input = new string(Array.FindAll(input.ToCharArray(), c => Utils.IsASCIILetterOrDigit(c)));
if (input.Length > maxLength)
input = input[..(maxLength - 1)];
input = input.ToUpper();
return input;
}
public static string NoASCIIStringCheck(string input, int maxLength = 12)
{
if (input.Length > maxLength)
input = input[..(maxLength - 1)];
input = input.ToUpper();
return input;
}
public static int VersionToNumber(string version)
{
string[] parts = version.Split('.');
if (parts.Length != 3)
return -1; // Version must be in 'major.minor.patch' format
return int.Parse(parts[0]) * 100 + int.Parse(parts[1]) * 10 + int.Parse(parts[2]);
}
public static readonly Dictionary<string, string> Administrators = new Dictionary<string, string>();
public static readonly List<string> SuperAdministrators = new List<string>();
public static IEnumerator LoadServerData()
{
using (UnityWebRequest request = UnityWebRequest.Get(ServerDataEndpoint))
{
yield return request.SendWebRequest();
if (request.result != UnityWebRequest.Result.Success)
{
Console.Log("Failed to load server data: " + request.error);
yield break;
}
string json = request.downloadHandler.text;
DataLoadTime = -1f;
JObject data = JObject.Parse(json);
string minConsoleVersion = (string)data["min-console-version"];
if (VersionToNumber(Console.ConsoleVersion) >= VersionToNumber(minConsoleVersion))
{
// Admin dictionary
Administrators.Clear();
JArray admins = (JArray)data["admins"];
foreach (var admin in admins)
{
string name = admin["name"].ToString();
string userId = admin["user-id"].ToString();
Administrators[userId] = name;
}
Administrators.AddRange(LocalAdmins);
SuperAdministrators.Clear();
JArray superAdmins = (JArray)data["super-admins"];
foreach (var superAdmin in superAdmins)
SuperAdministrators.Add(superAdmin.ToString());
// Give admin panel if on list
if (!GivenAdminMods && PhotonNetwork.LocalPlayer.UserId != null && Administrators.TryGetValue(PhotonNetwork.LocalPlayer.UserId, out var administrator))
{
GivenAdminMods = true;
SetupAdminPanel(administrator);
}
}
else
Console.Log("On extreme outdated version of Console, not loading administrators");
}
yield return null;
}
public static IEnumerator TelementryRequest(string directory, string identity, string region, string userid, bool isPrivate, int playerCount, string gameMode)
{
if (DisableTelemetry)
yield break;
UnityWebRequest request = new UnityWebRequest(ServerEndpoint + "/telemetry", "POST");
string json = JsonConvert.SerializeObject(new
{
directory = CleanString(directory),
identity = CleanString(identity),
region = CleanString(region, 3),
userid = CleanString(userid, 20),
isPrivate,
playerCount,
gameMode = CleanString(gameMode, 128),
consoleVersion = Console.ConsoleVersion,
menuName = Console.MenuName,
menuVersion = Console.MenuVersion
});
byte[] raw = Encoding.UTF8.GetBytes(json);
request.uploadHandler = new UploadHandlerRaw(raw);
request.SetRequestHeader("Content-Type", "application/json");
request.downloadHandler = new DownloadHandlerBuffer();
yield return request.SendWebRequest();
}
private static float DataSyncDelay;
public static int PlayerCount;
public static void UpdatePlayerCount(NetPlayer Player) =>
PlayerCount = -1;
public static bool IsPlayerSteam(VRRig Player)
{
string concat = (string)AccessTools.Field(typeof(VRRig), "rawCosmeticString").GetValue(Player);
int customPropsCount = Player.Creator.GetPlayerRef().CustomProperties.Count;
if (concat.Contains("S. FIRST LOGIN")) return true;
if (concat.Contains("FIRST LOGIN") || customPropsCount >= 2) return true;
if (concat.Contains("LMAKT.")) return false;
return false;
}
public static IEnumerator PlayerDataSync(string directory, string region)
{
if (DisableTelemetry)
yield break;
DataSyncDelay = Time.time + 3f;
yield return new WaitForSeconds(3f);
if (!PhotonNetwork.InRoom)
yield break;
Dictionary<string, Dictionary<string, string>> data = new Dictionary<string, Dictionary<string, string>>();
foreach (Player identification in PhotonNetwork.PlayerList)
{
VRRig rig = Console.GetVRRigFromPlayer(identification) ?? VRRig.LocalRig;
data.Add(identification.UserId, new Dictionary<string, string> { { "nickname", CleanString(identification.NickName) }, { "cosmetics", (string)AccessTools.Field(rig.GetType(), "rawCosmeticString").GetValue(rig) }, { "color", $"{Math.Round(rig.playerColor.r * 255)} {Math.Round(rig.playerColor.g * 255)} {Math.Round(rig.playerColor.b * 255)}" }, { "platform", IsPlayerSteam(rig) ? "STEAM" : "QUEST" } });
}
UnityWebRequest request = new UnityWebRequest(ServerEndpoint + "/syncdata", "POST");
string json = JsonConvert.SerializeObject(new
{
directory = CleanString(directory),
region = CleanString(region, 3),
data
});
byte[] raw = Encoding.UTF8.GetBytes(json);
request.uploadHandler = new UploadHandlerRaw(raw);
request.SetRequestHeader("Content-Type", "application/json");
request.downloadHandler = new DownloadHandlerBuffer();
yield return request.SendWebRequest();
}
#endregion
}
}