SanAndreasUnity/Assets/Scripts/Networking/SyncedServerData.cs

64 lines
1.7 KiB
C#
Raw Normal View History

2021-02-20 01:20:19 +00:00
using Mirror;
2021-05-03 00:59:57 +00:00
using SanAndreasUnity.Utilities;
2021-02-20 01:20:19 +00:00
using UnityEngine;
namespace SanAndreasUnity.Net
{
public class SyncedServerData : NetworkBehaviour
{
public static SyncedServerData Instance { get; private set; }
SyncedBag.StringSyncDictionary _syncDictionary = new SyncedBag.StringSyncDictionary();
public static SyncedBag Data { get; private set; } = new SyncedBag(new SyncedBag.StringSyncDictionary());
2021-05-03 00:59:57 +00:00
public static event System.Action onInitialSyncDataAvailable = delegate {};
2021-02-20 01:20:19 +00:00
private void Awake()
{
if (Instance != null)
{
Debug.LogError($"{nameof(SyncedServerData)} object already exists. There should only be 1.");
Destroy(this.gameObject);
return;
}
Instance = this;
var oldData = Data;
if (NetStatus.IsServer)
{
var newData = new SyncedBag(_syncDictionary);
newData.SetData(oldData);
newData.SetCallbacks(oldData);
Data = newData;
}
else
{
var newData = new SyncedBag(_syncDictionary);
newData.SetCallbacks(oldData);
Data = newData;
}
}
private void OnDisable()
{
// clear data for next server start
Data = new SyncedBag(new SyncedBag.StringSyncDictionary());
}
2021-05-03 00:59:57 +00:00
public override void OnStartClient()
{
if (NetStatus.IsServer)
return;
F.InvokeEventExceptionSafe(onInitialSyncDataAvailable);
}
2021-02-20 01:20:19 +00:00
}
}