-
Notifications
You must be signed in to change notification settings - Fork 0
/
Multiplayer.cs
521 lines (457 loc) · 22.3 KB
/
Multiplayer.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
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
using SkaterXL.Multiplayer;
using SkaterXL.Core;
using UnityEngine;
using HarmonyLib;
using UnityModManagerNet;
using System.Collections.Generic;
using Photon.Realtime;
using Photon.Pun;
using ExitGames.Client.Photon;
using System.Collections;
using SkaterXL.Data;
using System.Linq;
namespace fro_mod
{
public class Multiplayer : MonoBehaviour
{
public class ClothWatcherObj
{
public ClothingGearObjet cgo;
public CharacterGearInfo characterGearInfo;
public CharacterCustomizer customizer;
public BoardGearObject bgo;
public BoardGearInfo gearInfo;
public ClothWatcherObj(ClothingGearObjet cgo, CharacterGearInfo characterGearInfo, CharacterCustomizer customizer)
{
this.cgo = cgo;
this.characterGearInfo = characterGearInfo;
this.customizer = customizer;
}
public ClothWatcherObj(BoardGearObject bgo, BoardGearInfo gearInfo, CharacterCustomizer customizer)
{
this.bgo = bgo;
this.gearInfo = gearInfo;
this.customizer = customizer;
}
}
public List<ClothWatcherObj> toCheck = new List<ClothWatcherObj>();
public List<ClothWatcherObj> toCheckBoardGear = new List<ClothWatcherObj>();
void Start()
{
last_visibility = Main.settings.show_colliders;
}
List<GameObject> players = new List<GameObject>();
List<GameObject> skates = new List<GameObject>();
string last_scene = "";
bool last_visibility = false;
bool destroyed = false;
Vector3 last_respawn_point = Vector3.zero;
double time = 0;
void FixedUpdate()
{
if (MultiplayerManager.Instance.InRoom)
{
bool installed = false;
GetMapFrom(PhotonNetwork.CurrentRoom.CustomProperties, out installed);
if (Time.unscaledTime - time >= 20f)
{
// Utils.Log("Updating custom scale");
CheckCustomData();
time = Time.unscaledTime;
}
foreach (ClothWatcherObj cwo in toCheck)
{
if (cwo.cgo.State != GearObject.GearObjectState.Loading)
{
if (cwo.cgo.State == GearObject.GearObjectState.Finished || cwo.cgo.State == GearObject.GearObjectState.Initialized)
{
cwo.customizer.EquipCharacterGear(cwo.characterGearInfo);
toCheck.Remove(cwo);
}
if (cwo.cgo.State == GearObject.GearObjectState.Failed || cwo.cgo.State == GearObject.GearObjectState.Canceled || cwo.cgo.State == GearObject.GearObjectState.Disposed)
{
toCheck.Remove(cwo);
}
}
foreach (ClothWatcherObj cwobg in toCheckBoardGear)
{
if (cwobg.bgo.State != GearObject.GearObjectState.Loading)
{
if (cwobg.bgo.State == GearObject.GearObjectState.Finished || cwobg.bgo.State == GearObject.GearObjectState.Initialized)
{
cwobg.customizer.EquipBoardGear(cwobg.gearInfo);
toCheckBoardGear.Remove(cwobg);
}
if (cwobg.bgo.State == GearObject.GearObjectState.Failed || cwobg.bgo.State == GearObject.GearObjectState.Canceled || cwobg.bgo.State == GearObject.GearObjectState.Disposed)
{
toCheckBoardGear.Remove(cwo);
}
}
}
}
}
if (MultiplayerManager.Instance.InRoom && Main.settings.multiplayer_collision)
{
destroyed = false;
if (PlayerController.Instance.IsRespawning)
{
last_respawn_point = PlayerController.Instance.boardController.boardMesh.position;
}
Vector3 board = PlayerController.Instance.boardController.boardMesh.position;
bool isRespawning = FastApproximately(last_respawn_point.x, board.x, 1.05f) && FastApproximately(last_respawn_point.y, board.y, 1.05f) && FastApproximately(last_respawn_point.z, board.z, 1.05f);
if (MultiplayerManager.Instance.GetMapOfCurrentRoom().name != last_scene)
{
last_scene = MultiplayerManager.Instance.GetMapOfCurrentRoom().name;
players = new List<GameObject>();
skates = new List<GameObject>();
}
foreach (KeyValuePair<int, NetworkPlayerController> entry in MultiplayerManager.Instance.networkPlayers)
{
if (entry.Value)
{
string id = entry.Value.UserId;
if (MultiplayerManager.Instance.localPlayer.UserId != id)
{
GameObject player = FindPlayer("collider_" + id);
if (player == null)
{
GameObject collider = GameObject.CreatePrimitive(PrimitiveType.Capsule);
collider.GetComponent<MeshRenderer>().material.shader = Shader.Find("HDRP/Lit");
collider.GetComponent<MeshRenderer>().enabled = Main.settings.show_colliders;
collider.transform.localScale = new Vector3(.5f, .5f, .5f);
collider.name = "collider_" + id;
players.Add(collider);
GameObject collider_skate = GameObject.CreatePrimitive(PrimitiveType.Cube);
collider_skate.GetComponent<MeshRenderer>().material.shader = Shader.Find("HDRP/Lit");
collider_skate.GetComponent<MeshRenderer>().enabled = Main.settings.show_colliders;
collider_skate.transform.localScale = new Vector3(.2f, .1f, .65f);
collider_skate.name = "collider_skate_" + id;
collider_skate.isStatic = false;
skates.Add(collider_skate);
}
else
{
GameObject skate = FindSkate("collider_skate_" + id);
if (skate != null)
{
Transform target_skate = entry.Value.GetSkateboard();
skate.transform.position = target_skate.transform.position;
skate.transform.rotation = target_skate.transform.rotation;
}
Transform body = entry.Value.GetBody();
player.transform.rotation = body.transform.rotation;
player.transform.position = body.transform.position;
if (last_visibility != Main.settings.show_colliders)
{
player.GetComponent<MeshRenderer>().enabled = Main.settings.show_colliders;
skate.GetComponent<MeshRenderer>().enabled = Main.settings.show_colliders;
}
Collider collider = player.GetComponent<Collider>();
if (isRespawning || entry.Value.transformSyncer.currentStateEnum != NetworkPlayerStateEnum.GamePlay)
{
collider.enabled = false;
skate.GetComponent<Collider>().enabled = false;
}
else if (!collider.enabled)
{
collider.enabled = true;
skate.GetComponent<Collider>().enabled = true;
}
}
}
}
}
last_visibility = Main.settings.show_colliders;
}
else
{
if (!Main.settings.multiplayer_collision && !destroyed)
{
foreach (GameObject player in players)
{
Destroy(player);
}
foreach (GameObject skate in skates)
{
Destroy(skate);
}
players = new List<GameObject>();
skates = new List<GameObject>();
destroyed = true;
}
}
}
void CheckCustomData()
{
foreach (KeyValuePair<int, NetworkPlayerController> entry in MultiplayerManager.Instance.networkPlayers)
{
if (entry.Value)
{
if (entry.Value.GetPlayer().CustomProperties["scale"] != null)
{
entry.Value.GetBody().transform.localScale = (Vector3)entry.Value.GetPlayer().CustomProperties["scale"];
}
if (entry.Value.GetPlayer().CustomProperties["bpscale"] != null)
{
float[] scale = (float[])entry.Value.GetPlayer().CustomProperties["bpscale"];
Transform joints = entry.Value.GetBody().transform.Find("Skater_Joints");
Transform head = joints.FindChildRecursively("Skater_Head");
if (head) head.localScale = new Vector3(scale[0], scale[0], scale[0]);
Transform lhand = joints.FindChildRecursively("Skater_hand_l");
if (lhand) lhand.localScale = new Vector3(scale[1], scale[1], scale[1]);
Transform rhand = joints.FindChildRecursively("Skater_hand_r");
if (rhand) rhand.localScale = new Vector3(scale[2], scale[2], scale[2]);
Transform lfoot = joints.FindChildRecursively("Skater_foot_l");
if (lfoot) lfoot.localScale = new Vector3(scale[3], scale[3], scale[3]);
Transform rfoot = joints.FindChildRecursively("Skater_foot_r");
if (rfoot) rfoot.localScale = new Vector3(scale[4], scale[4], scale[4]);
Transform pelvis = joints.FindChildRecursively("Skater_pelvis");
if (pelvis) pelvis.localScale = new Vector3(scale[5], scale[5], scale[5]);
Transform spine = joints.FindChildRecursively("Skater_Spine1");
if (spine) spine.localScale = new Vector3(scale[6], scale[6], scale[6]);
Transform spine2 = joints.FindChildRecursively("Skater_Spine2");
if (spine2) spine2.localScale = new Vector3(scale[7], scale[7], scale[7]);
Transform larm = joints.FindChildRecursively("Skater_Arm_l");
if (larm) larm.localScale = new Vector3(scale[8], scale[8], scale[8]);
Transform lfarm = joints.FindChildRecursively("Skater_ForeArm_l");
if (lfarm) lfarm.localScale = new Vector3(scale[9], scale[9], scale[9]);
Transform rarm = joints.FindChildRecursively("Skater_Arm_r");
if (rarm) rarm.localScale = new Vector3(scale[10], scale[10], scale[10]);
Transform rfarm = joints.FindChildRecursively("Skater_ForeArm_r");
if (rfarm) rfarm.localScale = new Vector3(scale[11], scale[11], scale[11]);
Transform lupleg = joints.FindChildRecursively("Skater_UpLeg_l");
if (lupleg) lupleg.localScale = new Vector3(scale[12], scale[12], scale[12]);
Transform lleg = joints.FindChildRecursively("Skater_Leg_l");
if (lleg) lleg.localScale = new Vector3(scale[13], scale[13], scale[13]);
Transform rupleg = joints.FindChildRecursively("Skater_UpLeg_r");
if (rupleg) rupleg.localScale = new Vector3(scale[14], scale[14], scale[14]);
Transform rleg = joints.FindChildRecursively("Skater_Leg_r");
if (rleg) rleg.localScale = new Vector3(scale[15], scale[15], scale[15]);
Transform neck = joints.FindChildRecursively("Skater_Neck");
if (neck) neck.localScale = new Vector3(scale[16], scale[16], scale[16]);
}
}
}
ExitGames.Client.Photon.Hashtable hashtable = new ExitGames.Client.Photon.Hashtable();
hashtable["allowSpectators"] = true;
hashtable["scale"] = Main.settings.custom_scale;
float[] scales = { Main.settings.custom_scale_head,
Main.settings.custom_scale_hand_l,
Main.settings.custom_scale_hand_r,
Main.settings.custom_scale_foot_l,
Main.settings.custom_scale_foot_r,
Main.settings.custom_scale_pelvis,
Main.settings.custom_scale_spine,
Main.settings.custom_scale_spine2,
Main.settings.custom_scale_arm_l,
Main.settings.custom_scale_forearm_l,
Main.settings.custom_scale_arm_r,
Main.settings.custom_scale_forearm_r,
Main.settings.custom_scale_upleg_l,
Main.settings.custom_scale_leg_l,
Main.settings.custom_scale_upleg_r,
Main.settings.custom_scale_leg_r,
Main.settings.custom_scale_neck };
hashtable["bpscale"] = scales;
PhotonNetwork.SetPlayerCustomProperties(hashtable);
}
public void LogGear()
{
foreach (KeyValuePair<int, NetworkPlayerController> entry in MultiplayerManager.Instance.networkPlayers)
{
if (entry.Value)
{
if (entry.Value.UserId != MultiplayerManager.Instance.localPlayer.UserId)
{
Dictionary<int, GearObject> gearCache = (Dictionary<int, GearObject>)Traverse.Create(entry.Value.customizationSyncer.customizer).Field("gearCache").GetValue();
List<GearInfo> gear = entry.Value.customizationSyncer.customizer.CurrentCustomizations.GetAllGearInfos();
Utils.Log(entry.Value.NickName);
foreach (var obj in gearCache)
{
Utils.Log(obj.Key + " " + obj.Value.gearInfo.ToString());
}
}
}
}
}
public bool FastApproximately(float a, float b, float threshold)
{
return ((a - b) < 0 ? ((a - b) * -1) : (a - b)) <= threshold;
}
GameObject FindPlayer(string name)
{
foreach (GameObject player in players)
{
if (player.name == name) return player;
}
return null;
}
public void KickPlayer(string kickid)
{
string index = kickid.Split(':')[1];
foreach (KeyValuePair<int, NetworkPlayerController> entry in MultiplayerManager.Instance.networkPlayers)
{
if (entry.Value)
{
string id = entry.Value.NickName;
if (id == index)
{
PhotonNetwork.CloseConnection(entry.Value.GetPlayer());
}
}
}
}
Player lastMaster;
public void setMeAsMaster()
{
foreach (KeyValuePair<int, NetworkPlayerController> entry in MultiplayerManager.Instance.networkPlayers)
{
if (entry.Value)
{
if (entry.Value.GetPlayer().IsMasterClient)
{
lastMaster = entry.Value.GetPlayer();
}
}
}
PhotonNetwork.SetMasterClient(MultiplayerManager.Instance.localPlayer.GetPlayer());
}
public void restoreMaster()
{
PhotonNetwork.SetMasterClient(lastMaster);
}
public void MasterPlayer(string kickid)
{
string index = kickid.Split(':')[1];
foreach (KeyValuePair<int, NetworkPlayerController> entry in MultiplayerManager.Instance.networkPlayers)
{
if (entry.Value)
{
string id = entry.Value.NickName;
if (id == index)
{
PhotonNetwork.SetMasterClient(entry.Value.GetPlayer());
}
}
}
}
public bool isMaster()
{
return PhotonNetwork.IsMasterClient;
}
GameObject FindSkate(string name)
{
foreach (GameObject skate in skates)
{
if (skate.name == name) return skate;
}
return null;
}
void FindAndDestroy(string id)
{
foreach (GameObject player in players)
{
if (player.name == "collider_" + id)
{
players.Remove(player);
Destroy(player);
}
}
foreach (GameObject skate in skates)
{
if (skate.name == "collider_skate_" + id)
{
skates.Remove(skate);
Destroy(skate);
}
}
}
public void CreateRoom()
{
MultiplayerManager.Instance.CreateRoom(false);
}
public void OnPlayerLeft(Player player)
{
FindAndDestroy(player.UserId);
}
public void UpdateMapList()
{
LevelManager.Instance.IdToLevelDict.Clear();
using (IEnumerator<LevelInfo> enumerator = LevelManager.Instance.Levels.Union(LevelManager.Instance.CommunityLevels).Union(LevelManager.Instance.ModLevels).GetEnumerator())
{
while (enumerator.MoveNext())
{
LevelInfo lvl = enumerator.Current;
if (lvl.id != null)
{
if (!LevelManager.Instance.IdToLevelDict.ContainsKey(lvl.id))
{
LevelManager.Instance.IdToLevelDict.Add(lvl.id, lvl);
}
else
{
Debug.Log("Duplicate Level: " + lvl.name + " has same hash as " + LevelManager.Instance.IdToLevelDict[lvl.id].name);
}
string key;
if (LevelManager.Instance.LevelIdMigration.TryGetValue(lvl.id, out key) && !LevelManager.Instance.IdToLevelDict.ContainsKey(key))
{
LevelManager.Instance.IdToLevelDict.Add(key, lvl);
}
foreach (KeyValuePair<string, string> keyValuePair in from kvp in LevelManager.Instance.LevelIdMigration
where kvp.Value == lvl.id
select kvp)
{
if (!LevelManager.Instance.IdToLevelDict.ContainsKey(keyValuePair.Key))
{
LevelManager.Instance.IdToLevelDict.Add(keyValuePair.Key, lvl);
}
}
}
}
}
bool installed = false;
GetMapFrom(PhotonNetwork.CurrentRoom.CustomProperties, out installed);
}
public LevelInfo GetMapFrom(ExitGames.Client.Photon.Hashtable roomProperties, out bool isInstalled)
{
isInstalled = false;
object obj;
if (!roomProperties.TryGetValue(MultiplayerManager.MAPID_PROP_KEY, out obj))
{
Utils.Log("Room Properties don't contain mapId: " + string.Join("\n", from kvp in roomProperties
select kvp.Key + " : " + kvp.Value));
return null;
}
string text = roomProperties[MultiplayerManager.MAPNAME_PROP_KEY] as string;
if (!(obj is string))
{
Utils.Log("MP Room Property MapID Property is set but not a string");
return null;
}
string text2 = (string)obj;
if (MonoBehaviourSingleton<LevelManager>.Instance.IsCurrentLevel(text2))
{
isInstalled = true;
return MonoBehaviourSingleton<LevelManager>.Instance.currentLevel;
}
if (MonoBehaviourSingleton<LevelManager>.Instance.IdToLevelDict.ContainsKey(text2))
{
isInstalled = true;
return MonoBehaviourSingleton<LevelManager>.Instance.IdToLevelDict[text2];
}
Utils.Log(string.Concat(new string[]
{
"Map: ",
text,
"(id: ",
text2,
") not installed!"
}));
return new LevelInfo
{
name = text,
id = text2
};
}
}
}