SanAndreasUnity/Assets/Scripts/Networking/SyncedBag.cs

177 lines
5 KiB
C#
Raw Permalink Normal View History

2021-02-19 20:45:54 +00:00
using System;
using System.Collections.Generic;
using System.Globalization;
using Mirror;
2022-09-30 12:39:50 +00:00
using UGameCore.Utilities;
2021-02-19 20:45:54 +00:00
using UnityEngine;
namespace SanAndreasUnity.Net
{
public class SyncedBag
{
Feature/mirror upgrade (#127) * Updated SyncDictionary usage in SyncedBag.cs - Removed the extra class that is no longer needed for SyncDictionary's - Removed OP codes (OP_DIRTY) switch case that no longer exists (superseded by OP_SET) Signed-off-by: Lukas Olson <lukasolson@greyblockgames.com> * Updated method names Signed-off-by: Lukas Olson <lukasolson@greyblockgames.com> * Updated more string sync dictionaries Ped_Networking.cs VehicleController.cs Player.cs SyncedServerData.cs Signed-off-by: Lukas Olson <lukasolson@greyblockgames.com> * Updated NetworkTime fields in NetStats.cs Signed-off-by: Lukas Olson <lukasolson@greyblockgames.com> * Updated syncData types Signed-off-by: Lukas Olson <lukasolson@greyblockgames.com> * Implemented conditional compilation to replace old isHeadless Signed-off-by: Lukas Olson <lukasolson@greyblockgames.com> * Updated hooks * A few more syncDictionary upgrades * Moved away from obsolete NetworkIdentity.spawned * Updated SetDirtyBit to SetSyncVarDirtyBit in DeadBody.cs * Updated ScriptingDefineSymbols for Mirror * Updated JoinGameWindow.cs * Use latest MirrorLite commit for submodule * Use latest MirrorLite commit for submodule * Reverted EditorSettings.asset to commit b1c9d38e3ac5113e899ac34ad07bcb8a2890d18b * Reverted JoinGameWindow.cs to commit b1c9d38e3ac5113e899ac34ad07bcb8a2890d18b * Changed method for headless mode in NetCmdLineHandler.cs Changed from compiler defs to SanAndreasUnity helpers for determining headless mode in NetCmdLineHandler.cs * Re-Added ConfigureHeadlessFrameRate override on CustomNetworkManager.cs * Started updating JoinGameWindow.cs - Commented out GUI errors - Updated type 'DiscoveryInfo' to 'ServerResponse' in method ConnectFromDiscovery() params. - Updated Connect() 'port' parameter to use type 'int' rather than 'ushort' as per Mirror conventions. Co-authored-by: Lukas Olson <lukasolson@greyblockgames.com>
2022-04-02 18:15:08 +00:00
private readonly SyncDictionary<string, string> m_syncDictionary;
2021-02-19 20:45:54 +00:00
Feature/mirror upgrade (#127) * Updated SyncDictionary usage in SyncedBag.cs - Removed the extra class that is no longer needed for SyncDictionary's - Removed OP codes (OP_DIRTY) switch case that no longer exists (superseded by OP_SET) Signed-off-by: Lukas Olson <lukasolson@greyblockgames.com> * Updated method names Signed-off-by: Lukas Olson <lukasolson@greyblockgames.com> * Updated more string sync dictionaries Ped_Networking.cs VehicleController.cs Player.cs SyncedServerData.cs Signed-off-by: Lukas Olson <lukasolson@greyblockgames.com> * Updated NetworkTime fields in NetStats.cs Signed-off-by: Lukas Olson <lukasolson@greyblockgames.com> * Updated syncData types Signed-off-by: Lukas Olson <lukasolson@greyblockgames.com> * Implemented conditional compilation to replace old isHeadless Signed-off-by: Lukas Olson <lukasolson@greyblockgames.com> * Updated hooks * A few more syncDictionary upgrades * Moved away from obsolete NetworkIdentity.spawned * Updated SetDirtyBit to SetSyncVarDirtyBit in DeadBody.cs * Updated ScriptingDefineSymbols for Mirror * Updated JoinGameWindow.cs * Use latest MirrorLite commit for submodule * Use latest MirrorLite commit for submodule * Reverted EditorSettings.asset to commit b1c9d38e3ac5113e899ac34ad07bcb8a2890d18b * Reverted JoinGameWindow.cs to commit b1c9d38e3ac5113e899ac34ad07bcb8a2890d18b * Changed method for headless mode in NetCmdLineHandler.cs Changed from compiler defs to SanAndreasUnity helpers for determining headless mode in NetCmdLineHandler.cs * Re-Added ConfigureHeadlessFrameRate override on CustomNetworkManager.cs * Started updating JoinGameWindow.cs - Commented out GUI errors - Updated type 'DiscoveryInfo' to 'ServerResponse' in method ConnectFromDiscovery() params. - Updated Connect() 'port' parameter to use type 'int' rather than 'ushort' as per Mirror conventions. Co-authored-by: Lukas Olson <lukasolson@greyblockgames.com>
2022-04-02 18:15:08 +00:00
private readonly Dictionary<string, List<System.Action<string>>> m_callbacks =
new Dictionary<string, List<Action<string>>>();
2021-02-19 20:45:54 +00:00
private struct ArrayWrapper<T> // use struct so that it doesn't allocate memory
2021-02-20 01:20:19 +00:00
{
public T[] a; // use short name because it will be a part of json
2021-02-20 01:20:19 +00:00
public ArrayWrapper(T[] a)
2021-02-20 01:20:19 +00:00
{
this.a = a;
2021-02-20 01:20:19 +00:00
}
}
2021-02-19 20:45:54 +00:00
Feature/mirror upgrade (#127) * Updated SyncDictionary usage in SyncedBag.cs - Removed the extra class that is no longer needed for SyncDictionary's - Removed OP codes (OP_DIRTY) switch case that no longer exists (superseded by OP_SET) Signed-off-by: Lukas Olson <lukasolson@greyblockgames.com> * Updated method names Signed-off-by: Lukas Olson <lukasolson@greyblockgames.com> * Updated more string sync dictionaries Ped_Networking.cs VehicleController.cs Player.cs SyncedServerData.cs Signed-off-by: Lukas Olson <lukasolson@greyblockgames.com> * Updated NetworkTime fields in NetStats.cs Signed-off-by: Lukas Olson <lukasolson@greyblockgames.com> * Updated syncData types Signed-off-by: Lukas Olson <lukasolson@greyblockgames.com> * Implemented conditional compilation to replace old isHeadless Signed-off-by: Lukas Olson <lukasolson@greyblockgames.com> * Updated hooks * A few more syncDictionary upgrades * Moved away from obsolete NetworkIdentity.spawned * Updated SetDirtyBit to SetSyncVarDirtyBit in DeadBody.cs * Updated ScriptingDefineSymbols for Mirror * Updated JoinGameWindow.cs * Use latest MirrorLite commit for submodule * Use latest MirrorLite commit for submodule * Reverted EditorSettings.asset to commit b1c9d38e3ac5113e899ac34ad07bcb8a2890d18b * Reverted JoinGameWindow.cs to commit b1c9d38e3ac5113e899ac34ad07bcb8a2890d18b * Changed method for headless mode in NetCmdLineHandler.cs Changed from compiler defs to SanAndreasUnity helpers for determining headless mode in NetCmdLineHandler.cs * Re-Added ConfigureHeadlessFrameRate override on CustomNetworkManager.cs * Started updating JoinGameWindow.cs - Commented out GUI errors - Updated type 'DiscoveryInfo' to 'ServerResponse' in method ConnectFromDiscovery() params. - Updated Connect() 'port' parameter to use type 'int' rather than 'ushort' as per Mirror conventions. Co-authored-by: Lukas Olson <lukasolson@greyblockgames.com>
2022-04-02 18:15:08 +00:00
public SyncedBag(SyncDictionary<string, string> syncDictionary)
2021-02-19 20:45:54 +00:00
{
m_syncDictionary = syncDictionary;
m_syncDictionary.Callback += DictionaryCallback;
}
Feature/mirror upgrade (#127) * Updated SyncDictionary usage in SyncedBag.cs - Removed the extra class that is no longer needed for SyncDictionary's - Removed OP codes (OP_DIRTY) switch case that no longer exists (superseded by OP_SET) Signed-off-by: Lukas Olson <lukasolson@greyblockgames.com> * Updated method names Signed-off-by: Lukas Olson <lukasolson@greyblockgames.com> * Updated more string sync dictionaries Ped_Networking.cs VehicleController.cs Player.cs SyncedServerData.cs Signed-off-by: Lukas Olson <lukasolson@greyblockgames.com> * Updated NetworkTime fields in NetStats.cs Signed-off-by: Lukas Olson <lukasolson@greyblockgames.com> * Updated syncData types Signed-off-by: Lukas Olson <lukasolson@greyblockgames.com> * Implemented conditional compilation to replace old isHeadless Signed-off-by: Lukas Olson <lukasolson@greyblockgames.com> * Updated hooks * A few more syncDictionary upgrades * Moved away from obsolete NetworkIdentity.spawned * Updated SetDirtyBit to SetSyncVarDirtyBit in DeadBody.cs * Updated ScriptingDefineSymbols for Mirror * Updated JoinGameWindow.cs * Use latest MirrorLite commit for submodule * Use latest MirrorLite commit for submodule * Reverted EditorSettings.asset to commit b1c9d38e3ac5113e899ac34ad07bcb8a2890d18b * Reverted JoinGameWindow.cs to commit b1c9d38e3ac5113e899ac34ad07bcb8a2890d18b * Changed method for headless mode in NetCmdLineHandler.cs Changed from compiler defs to SanAndreasUnity helpers for determining headless mode in NetCmdLineHandler.cs * Re-Added ConfigureHeadlessFrameRate override on CustomNetworkManager.cs * Started updating JoinGameWindow.cs - Commented out GUI errors - Updated type 'DiscoveryInfo' to 'ServerResponse' in method ConnectFromDiscovery() params. - Updated Connect() 'port' parameter to use type 'int' rather than 'ushort' as per Mirror conventions. Co-authored-by: Lukas Olson <lukasolson@greyblockgames.com>
2022-04-02 18:15:08 +00:00
private void DictionaryCallback(SyncDictionary<string, string>.Operation op, string key, string item)
2021-02-19 20:45:54 +00:00
{
if (NetUtils.IsServer)
return;
switch (op)
{
Feature/mirror upgrade (#127) * Updated SyncDictionary usage in SyncedBag.cs - Removed the extra class that is no longer needed for SyncDictionary's - Removed OP codes (OP_DIRTY) switch case that no longer exists (superseded by OP_SET) Signed-off-by: Lukas Olson <lukasolson@greyblockgames.com> * Updated method names Signed-off-by: Lukas Olson <lukasolson@greyblockgames.com> * Updated more string sync dictionaries Ped_Networking.cs VehicleController.cs Player.cs SyncedServerData.cs Signed-off-by: Lukas Olson <lukasolson@greyblockgames.com> * Updated NetworkTime fields in NetStats.cs Signed-off-by: Lukas Olson <lukasolson@greyblockgames.com> * Updated syncData types Signed-off-by: Lukas Olson <lukasolson@greyblockgames.com> * Implemented conditional compilation to replace old isHeadless Signed-off-by: Lukas Olson <lukasolson@greyblockgames.com> * Updated hooks * A few more syncDictionary upgrades * Moved away from obsolete NetworkIdentity.spawned * Updated SetDirtyBit to SetSyncVarDirtyBit in DeadBody.cs * Updated ScriptingDefineSymbols for Mirror * Updated JoinGameWindow.cs * Use latest MirrorLite commit for submodule * Use latest MirrorLite commit for submodule * Reverted EditorSettings.asset to commit b1c9d38e3ac5113e899ac34ad07bcb8a2890d18b * Reverted JoinGameWindow.cs to commit b1c9d38e3ac5113e899ac34ad07bcb8a2890d18b * Changed method for headless mode in NetCmdLineHandler.cs Changed from compiler defs to SanAndreasUnity helpers for determining headless mode in NetCmdLineHandler.cs * Re-Added ConfigureHeadlessFrameRate override on CustomNetworkManager.cs * Started updating JoinGameWindow.cs - Commented out GUI errors - Updated type 'DiscoveryInfo' to 'ServerResponse' in method ConnectFromDiscovery() params. - Updated Connect() 'port' parameter to use type 'int' rather than 'ushort' as per Mirror conventions. Co-authored-by: Lukas Olson <lukasolson@greyblockgames.com>
2022-04-02 18:15:08 +00:00
case SyncDictionary<string, string>.Operation.OP_ADD:
case SyncDictionary<string, string>.Operation.OP_SET:
2021-02-19 20:45:54 +00:00
if (m_callbacks.TryGetValue(key, out var list))
{
// don't leave garbage
for (int i = 0; i < list.Count; i++)
{
var callback = list[i];
F.RunExceptionSafe(() => callback(item));
}
}
break;
}
}
public void RegisterCallback(string key, System.Action<string> callback)
{
if (m_callbacks.TryGetValue(key, out var list))
{
list.Add(callback);
}
else
{
Feature/mirror upgrade (#127) * Updated SyncDictionary usage in SyncedBag.cs - Removed the extra class that is no longer needed for SyncDictionary's - Removed OP codes (OP_DIRTY) switch case that no longer exists (superseded by OP_SET) Signed-off-by: Lukas Olson <lukasolson@greyblockgames.com> * Updated method names Signed-off-by: Lukas Olson <lukasolson@greyblockgames.com> * Updated more string sync dictionaries Ped_Networking.cs VehicleController.cs Player.cs SyncedServerData.cs Signed-off-by: Lukas Olson <lukasolson@greyblockgames.com> * Updated NetworkTime fields in NetStats.cs Signed-off-by: Lukas Olson <lukasolson@greyblockgames.com> * Updated syncData types Signed-off-by: Lukas Olson <lukasolson@greyblockgames.com> * Implemented conditional compilation to replace old isHeadless Signed-off-by: Lukas Olson <lukasolson@greyblockgames.com> * Updated hooks * A few more syncDictionary upgrades * Moved away from obsolete NetworkIdentity.spawned * Updated SetDirtyBit to SetSyncVarDirtyBit in DeadBody.cs * Updated ScriptingDefineSymbols for Mirror * Updated JoinGameWindow.cs * Use latest MirrorLite commit for submodule * Use latest MirrorLite commit for submodule * Reverted EditorSettings.asset to commit b1c9d38e3ac5113e899ac34ad07bcb8a2890d18b * Reverted JoinGameWindow.cs to commit b1c9d38e3ac5113e899ac34ad07bcb8a2890d18b * Changed method for headless mode in NetCmdLineHandler.cs Changed from compiler defs to SanAndreasUnity helpers for determining headless mode in NetCmdLineHandler.cs * Re-Added ConfigureHeadlessFrameRate override on CustomNetworkManager.cs * Started updating JoinGameWindow.cs - Commented out GUI errors - Updated type 'DiscoveryInfo' to 'ServerResponse' in method ConnectFromDiscovery() params. - Updated Connect() 'port' parameter to use type 'int' rather than 'ushort' as per Mirror conventions. Co-authored-by: Lukas Olson <lukasolson@greyblockgames.com>
2022-04-02 18:15:08 +00:00
m_callbacks.Add(key, new List<Action<string>> { callback });
2021-02-19 20:45:54 +00:00
}
}
public void UnRegisterCallback(string key, System.Action<string> callback)
{
if (m_callbacks.TryGetValue(key, out var list))
{
list.Remove(callback);
}
}
2021-02-20 01:20:19 +00:00
public void SetCallbacks(SyncedBag other)
{
if (this == other)
return;
m_callbacks.Clear();
foreach (var callback in other.m_callbacks)
{
m_callbacks.Add(callback.Key, callback.Value);
}
}
public void SetData(SyncedBag other)
{
if (this == other)
return;
m_syncDictionary.Clear();
foreach (var pair in other.m_syncDictionary)
{
m_syncDictionary.Add(pair.Key, pair.Value);
}
}
2021-02-19 20:45:54 +00:00
public void SetString(string key, string value)
{
if (m_syncDictionary.TryGetValue(key, out string existingValue))
{
if (value != existingValue)
{
m_syncDictionary[key] = value;
}
}
m_syncDictionary[key] = value;
}
public string GetString(string key)
{
return m_syncDictionary.TryGetValue(key, out string value) ? value : null;
}
public bool HasValue(string key)
{
return m_syncDictionary.ContainsKey(key);
}
public int GetInt(string key)
{
string str = GetString(key);
if (str == null)
return 0;
return int.Parse(str, CultureInfo.InvariantCulture);
}
public void SetInt(string key, int value)
{
SetString(key, value.ToString(CultureInfo.InvariantCulture));
}
public float GetFloat(string key)
{
string str = GetString(key);
if (str == null)
return 0;
return float.Parse(str, CultureInfo.InvariantCulture);
}
public void SetFloat(string key, float value)
{
SetString(key, value.ToString(CultureInfo.InvariantCulture));
}
public string[] GetStringArray(string key)
{
string str = GetString(key);
if (str == null)
return null;
return JsonUtility.FromJson<ArrayWrapper<string>>(str).a;
2021-02-19 20:45:54 +00:00
}
public void SetStringArray(string key, string[] array)
{
2021-02-20 01:20:19 +00:00
SetString(key, JsonUtility.ToJson(new ArrayWrapper<string>(array)));
}
public float[] GetFloatArray(string key)
{
string str = GetString(key);
if (str == null)
return null;
return JsonUtility.FromJson<ArrayWrapper<float>>(str).a;
2021-02-20 01:20:19 +00:00
}
public void SetFloatArray(string key, float[] array)
{
SetString(key, JsonUtility.ToJson(new ArrayWrapper<float>(array)));
2021-02-19 20:45:54 +00:00
}
}
Feature/mirror upgrade (#127) * Updated SyncDictionary usage in SyncedBag.cs - Removed the extra class that is no longer needed for SyncDictionary's - Removed OP codes (OP_DIRTY) switch case that no longer exists (superseded by OP_SET) Signed-off-by: Lukas Olson <lukasolson@greyblockgames.com> * Updated method names Signed-off-by: Lukas Olson <lukasolson@greyblockgames.com> * Updated more string sync dictionaries Ped_Networking.cs VehicleController.cs Player.cs SyncedServerData.cs Signed-off-by: Lukas Olson <lukasolson@greyblockgames.com> * Updated NetworkTime fields in NetStats.cs Signed-off-by: Lukas Olson <lukasolson@greyblockgames.com> * Updated syncData types Signed-off-by: Lukas Olson <lukasolson@greyblockgames.com> * Implemented conditional compilation to replace old isHeadless Signed-off-by: Lukas Olson <lukasolson@greyblockgames.com> * Updated hooks * A few more syncDictionary upgrades * Moved away from obsolete NetworkIdentity.spawned * Updated SetDirtyBit to SetSyncVarDirtyBit in DeadBody.cs * Updated ScriptingDefineSymbols for Mirror * Updated JoinGameWindow.cs * Use latest MirrorLite commit for submodule * Use latest MirrorLite commit for submodule * Reverted EditorSettings.asset to commit b1c9d38e3ac5113e899ac34ad07bcb8a2890d18b * Reverted JoinGameWindow.cs to commit b1c9d38e3ac5113e899ac34ad07bcb8a2890d18b * Changed method for headless mode in NetCmdLineHandler.cs Changed from compiler defs to SanAndreasUnity helpers for determining headless mode in NetCmdLineHandler.cs * Re-Added ConfigureHeadlessFrameRate override on CustomNetworkManager.cs * Started updating JoinGameWindow.cs - Commented out GUI errors - Updated type 'DiscoveryInfo' to 'ServerResponse' in method ConnectFromDiscovery() params. - Updated Connect() 'port' parameter to use type 'int' rather than 'ushort' as per Mirror conventions. Co-authored-by: Lukas Olson <lukasolson@greyblockgames.com>
2022-04-02 18:15:08 +00:00
}