generated from Distractic/LethalCompanyTemplate
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add configuration synchronization (#28)
Release-as: 1.4.4
- Loading branch information
Showing
5 changed files
with
122 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
using GameNetcodeStuff; | ||
using HarmonyLib; | ||
|
||
namespace Hypick.Patches; | ||
|
||
[HarmonyPatch] | ||
public class SyncConfig | ||
{ | ||
[HarmonyPatch(typeof(PlayerControllerB), "ConnectClientToPlayerObject")] | ||
[HarmonyPostfix] | ||
public static void InitializeLocalPlayer() { | ||
if (Config.IsHost) { | ||
Config.MessageManager.RegisterNamedMessageHandler("BetterShotgun_OnRequestConfigSync", Config.OnRequestSync); | ||
Config.Synced = true; | ||
|
||
return; | ||
} | ||
|
||
Config.Synced = false; | ||
Config.MessageManager.RegisterNamedMessageHandler("BetterShotgun_OnReceiveConfigSync", Config.OnReceiveSync); | ||
Config.RequestSync(); | ||
} | ||
|
||
[HarmonyPostfix] | ||
[HarmonyPatch(typeof(GameNetworkManager), "StartDisconnect")] | ||
public static void PlayerLeave() { | ||
Config.RevertSync(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
using System; | ||
using System.IO; | ||
using System.Runtime.Serialization.Formatters.Binary; | ||
using Unity.Netcode; | ||
|
||
namespace Hypick; | ||
|
||
[Serializable] | ||
public class SyncedInstance<T> { | ||
internal static CustomMessagingManager MessageManager => NetworkManager.Singleton.CustomMessagingManager; | ||
internal static bool IsClient => NetworkManager.Singleton.IsClient; | ||
internal static bool IsHost => NetworkManager.Singleton.IsHost; | ||
|
||
[NonSerialized] | ||
protected static int IntSize = 4; | ||
|
||
public static T Default { get; private set; } | ||
public static T Instance { get; private set; } | ||
|
||
public static bool Synced { get; internal set; } | ||
|
||
protected void InitInstance(T instance) { | ||
Default = instance; | ||
Instance = instance; | ||
|
||
// Makes sure the size of an integer is correct for the current system. | ||
// We use 4 by default as that's the size of an int on 32 and 64 bit systems. | ||
IntSize = sizeof(int); | ||
} | ||
|
||
internal static void SyncInstance(byte[] data) { | ||
Instance = DeserializeFromBytes(data); | ||
Synced = true; | ||
} | ||
|
||
internal static void RevertSync() { | ||
Instance = Default; | ||
Synced = false; | ||
} | ||
|
||
public static byte[] SerializeToBytes(T val) { | ||
BinaryFormatter bf = new(); | ||
using MemoryStream stream = new(); | ||
|
||
try { | ||
bf.Serialize(stream, val); | ||
return stream.ToArray(); | ||
} | ||
catch (Exception e) { | ||
Plugin.Log.LogError($"Error serializing instance: {e}"); | ||
return null; | ||
} | ||
} | ||
|
||
public static T DeserializeFromBytes(byte[] data) { | ||
BinaryFormatter bf = new(); | ||
using MemoryStream stream = new(data); | ||
|
||
try { | ||
return (T) bf.Deserialize(stream); | ||
} catch (Exception e) { | ||
Plugin.Log.LogError($"Error deserializing instance: {e}"); | ||
return default; | ||
} | ||
} | ||
} |