add SyncedServerData

This commit is contained in:
in0finite 2021-02-20 02:20:19 +01:00
parent 0b4794bb72
commit b5e308c135
9 changed files with 251 additions and 2 deletions

View file

@ -29,6 +29,7 @@ GameObject:
- component: {fileID: 6547606932669875539}
- component: {fileID: 8970227709208402880}
- component: {fileID: 3898620812402470502}
- component: {fileID: 5182370913446898971}
m_Layer: 0
m_Name: GameManager
m_TagString: Untagged
@ -378,3 +379,17 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: aabf0ed3019cb714e9079ac483b9a670, type: 3}
m_Name:
m_EditorClassIdentifier:
--- !u!114 &5182370913446898971
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1297494511425690}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 40386a7e7b5eb5f42b596291c487277f, type: 3}
m_Name:
m_EditorClassIdentifier:
m_syncedServerDataPrefab: {fileID: 1597674083562683622, guid: f38745bb026a81e4c9711b36a5a262ae,
type: 3}

View file

@ -69,6 +69,7 @@ MonoBehaviour:
- {fileID: 100010, guid: 4f8c6e508a3f69243ab7b8813398b7ed, type: 3}
- {fileID: 8038540346792760480, guid: 858b1226127ef9f4e8d16c9610e691b6, type: 3}
- {fileID: 2141754834917738923, guid: 14d22e60f4926f64cbd36f393fe11b9a, type: 3}
- {fileID: 1597674083562683622, guid: f38745bb026a81e4c9711b36a5a262ae, type: 3}
--- !u!114 &4862460140142645991
MonoBehaviour:
m_ObjectHideFlags: 0

View file

@ -0,0 +1,63 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!1 &1597674083562683622
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 7624085711834089393}
- component: {fileID: 234247405925470423}
- component: {fileID: 6555188705879957608}
m_Layer: 0
m_Name: SyncedServerData
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!4 &7624085711834089393
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1597674083562683622}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!114 &234247405925470423
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1597674083562683622}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 9b91ecbcc199f4492b9a91e820070131, type: 3}
m_Name:
m_EditorClassIdentifier:
serverOnly: 0
localPlayerAuthority: 0
m_AssetId: f38745bb026a81e4c9711b36a5a262ae
m_SceneId: 0
--- !u!114 &6555188705879957608
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1597674083562683622}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: e09317a94d2d54949a17300f40dd6d3d, type: 3}
m_Name:
m_EditorClassIdentifier:
syncInterval: 0.1

View file

@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: f38745bb026a81e4c9711b36a5a262ae
PrefabImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -16,6 +16,16 @@ namespace SanAndreasUnity.Net
private readonly StringSyncDictionary m_syncDictionary;
private readonly Dictionary<string, List<System.Action<string>>> m_callbacks = new Dictionary<string, List<Action<string>>>();
private class ArrayWrapper<T>
{
public T[] array;
public ArrayWrapper(T[] array)
{
this.array = array;
}
}
public SyncedBag(StringSyncDictionary syncDictionary)
{
@ -69,6 +79,30 @@ namespace SanAndreasUnity.Net
}
}
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);
}
}
public void SetString(string key, string value)
{
if (m_syncDictionary.TryGetValue(key, out string existingValue))
@ -123,12 +157,25 @@ namespace SanAndreasUnity.Net
string str = GetString(key);
if (str == null)
return null;
return JsonUtility.FromJson<string[]>(str);
return JsonUtility.FromJson<ArrayWrapper<string>>(str)?.array;
}
public void SetStringArray(string key, string[] array)
{
SetString(key, JsonUtility.ToJson(array));
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)?.array;
}
public void SetFloatArray(string key, float[] array)
{
SetString(key, JsonUtility.ToJson(new ArrayWrapper<float>(array)));
}
}
}

View file

@ -0,0 +1,54 @@
using Mirror;
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());
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)
{
_syncDictionary = new SyncedBag.StringSyncDictionary();
var newData = new SyncedBag(_syncDictionary);
newData.SetData(oldData);
newData.SetCallbacks(oldData);
Data = newData;
}
else
{
_syncDictionary = new SyncedBag.StringSyncDictionary();
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());
}
}
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: e09317a94d2d54949a17300f40dd6d3d
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,40 @@
using UnityEngine;
using UnityEngine.SceneManagement;
namespace SanAndreasUnity.Net
{
public class SyncedServerDataObjectSpawner : MonoBehaviour
{
[SerializeField] private GameObject m_syncedServerDataPrefab = null;
private void Start()
{
NetManager.Instance.onServerStatusChanged += OnServerStatusChanged;
SceneManager.activeSceneChanged += OnActiveSceneChanged;
}
private void OnServerStatusChanged()
{
// scene is not changed yet, so the object will get destroyed when it changes
//Spawn();
}
private void OnActiveSceneChanged(Scene arg0, Scene arg1)
{
Spawn();
}
private void Spawn()
{
if (!NetStatus.IsServer)
return;
if (null != SyncedServerData.Instance)
return;
var go = Instantiate(m_syncedServerDataPrefab);
NetManager.Spawn(go);
}
}
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 40386a7e7b5eb5f42b596291c487277f
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: