forked from notnotnotswipez/BonelabMultiplayerMockup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMelonLoaderMod.cs
245 lines (213 loc) · 9.4 KB
/
MelonLoaderMod.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
using System;
using System.Collections;
using System.Collections.Generic;
using System.Threading;
using BonelabMultiplayerMockup.NetworkData;
using BonelabMultiplayerMockup.Nodes;
using BonelabMultiplayerMockup.Object;
using BonelabMultiplayerMockup.Packets;
using BonelabMultiplayerMockup.Packets.Object;
using BonelabMultiplayerMockup.Packets.Player;
using BonelabMultiplayerMockup.Packets.Reset;
using BonelabMultiplayerMockup.Patches;
using BonelabMultiplayerMockup.Representations;
using BonelabMultiplayerMockup.Utils;
using BoneLib;
using MelonLoader;
using SLZ.Rig;
using UnityEngine;
namespace BonelabMultiplayerMockup
{
public static class BuildInfo
{
public const string Name = "BonelabMultiplayerMockup"; // Name of the Mod. (MUST BE SET)
public const string Author = "notnotnotswipez"; // Author of the Mod. (Set as null if none)
public const string Company = null; // Company that made the Mod. (Set as null if none)
public const string Version = "1.0.0"; // Version of the Mod. (MUST BE SET)
public const string DownloadLink = null; // Download Link for the Mod. (Set as null if none)
}
public class BonelabMultiplayerMockup : MelonMod
{
public static PlayerRepresentation debugRep = null;
public static Dictionary<byte, GameObject> boneDictionary = new Dictionary<byte, GameObject>();
private static byte currentBoneId = 0;
private int updateCount = 0;
private int desiredFrames = 2;
public static string sceneName = "";
public static bool debug = false;
public static void PopulateCurrentAvatarData()
{
boneDictionary.Clear();
currentBoneId = 0;
PopulateBoneDictionary(Player.GetRigManager().GetComponentInChildren<RigManager>().avatar.gameObject.transform);
}
private static void PopulateBoneDictionary(Transform parent)
{
var childCount = parent.childCount;
for (var i = 0; i < childCount; i++)
{
var child = parent.GetChild(i).gameObject;
boneDictionary.Add(currentBoneId++, child);
if (child.transform.childCount > 0) PopulateBoneDictionary(child.transform);
}
}
public override void OnApplicationStart()
{
GameSDK.LoadGameSDK();
PacketHandler.RegisterPackets();
DiscordIntegration.Init();
Client.StartClient();
DataDirectory.Initialize();
}
public override void OnSceneWasLoaded(int buildIndex, string sceneName)
{
if (DiscordIntegration.hasLobby)
{
if (Player.GetRigManager() != null)
{
GameObject rigManager = Player.GetRigManager();
if (rigManager.scene.name != BonelabMultiplayerMockup.sceneName)
{
DebugLogger.Msg("Cleaned data");
DebugLogger.Msg("Loaded scene with name: " + rigManager.scene.name);
SyncedObject.CleanData();
PopulateCurrentAvatarData();
BonelabMultiplayerMockup.sceneName = rigManager.scene.name;
MelonCoroutines.Start(PatchCoroutines.WaitForAvatarSwitch());
}
else
{
DebugLogger.Msg("Loaded a new scene zone!");
MelonCoroutines.Start(PatchCoroutines.WaitForAvatarSwitch());
if (DiscordIntegration.isHost)
{
var syncResetData = new SyncResetData()
{
// empty data
};
PacketByteBuf packetByteBuf =
PacketHandler.CompressMessage(NetworkMessageType.SyncResetPacket, syncResetData);
Node.activeNode.BroadcastMessage((byte)NetworkChannel.Transaction ,packetByteBuf.getBytes());
SyncedObject.CleanData(true);
if (BoneLib.Player.GetObjectInHand(BoneLib.Player.leftHand) != null)
{
SyncedObject.Sync(BoneLib.Player.GetObjectInHand(BoneLib.Player.leftHand));
}
if (BoneLib.Player.GetObjectInHand(BoneLib.Player.rightHand) != null)
{
SyncedObject.Sync(BoneLib.Player.GetObjectInHand(BoneLib.Player.rightHand));
}
}
}
}
}
}
public override void OnUpdate()
{
if (Input.GetKey(KeyCode.S))
{
Server.StartServer();
}
if (Input.GetKey(KeyCode.P))
{
MelonLogger.Msg("Server shutdown!");
Server.instance.Shutdown();
}
if (Input.GetKey(KeyCode.O))
{
if (DiscordIntegration.hasLobby)
{
if (DiscordIntegration.isHost)
{
var syncResetData = new SyncResetData()
{
// empty data
};
PacketByteBuf packetByteBuf =
PacketHandler.CompressMessage(NetworkMessageType.SyncResetPacket, syncResetData);
Node.activeNode.BroadcastMessage((byte)NetworkChannel.Transaction ,packetByteBuf.getBytes());
SyncedObject.CleanData(true);
}
}
}
updateCount++;
if (updateCount >= desiredFrames)
{
if (DiscordIntegration.hasLobby)
{
SendBones();
SyncedObject.UpdateSyncedNPCs();
}
updateCount = 0;
}
}
public override void OnFixedUpdate()
{
if (DiscordIntegration.hasLobby)
{
if (SyncedObject.syncedObjectIds.Count > 0)
{
foreach (SyncedObject syncedObject in SyncedObject.syncedObjectIds.Values) {
try
{
syncedObject.UpdatePos();
}
catch (Exception e)
{
// Ignore it, if something goes wrong we dont want everything in the list to break
}
}
if (SyncedObject.queuedObjectsToDelete.Count > 0)
{
for (int i = 0; i < SyncedObject.queuedObjectsToDelete.Count; i++)
{
List<SyncedObject> syncedObjects = SyncedObject.GetAllSyncables(
SyncedObject.relatedSyncedObjects[SyncedObject.queuedObjectsToDelete[i]][0].gameObject);
ushort lastId = 0;
foreach (var synced in syncedObjects)
{
lastId = synced.groupId;
synced.DestroySyncable(true);
}
ushort groupId = SyncedObject.queuedObjectsToDelete[i];
GroupDestroyData groupDestroyData = new GroupDestroyData()
{
groupId = groupId,
backupObjectId = lastId
};
PacketByteBuf message = PacketHandler.CompressMessage(NetworkMessageType.GroupDestroyPacket, groupDestroyData);
Node.activeNode.BroadcastMessage((byte)NetworkChannel.Object, message.getBytes());
}
SyncedObject.queuedObjectsToDelete.Clear();
}
}
}
}
private static void SendBones()
{
for (byte i = 0; i < boneDictionary.Count; i++)
{
GameObject bone = boneDictionary[i];
if (bone == null)
{
// Assume its all wrong
break;
}
var simplifiedTransform = new CompressedTransform(bone.transform.position,
Quaternion.Euler(bone.transform.eulerAngles));
PlayerBoneData playerBoneData = new PlayerBoneData()
{
userId = DiscordIntegration.currentUser.Id,
boneId = i,
transform = simplifiedTransform
};
PacketByteBuf message = PacketHandler.CompressMessage(NetworkMessageType.PlayerUpdatePacket, playerBoneData);
Node.activeNode.BroadcastMessage((byte)NetworkChannel.Unreliable, message.getBytes());
}
}
public override void OnLateUpdate()
{
DiscordIntegration.Tick();
}
}
}