2021-02-19 20:45:54 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Globalization;
|
|
|
|
|
using Mirror;
|
|
|
|
|
using SanAndreasUnity.Utilities;
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
|
|
namespace SanAndreasUnity.Net
|
|
|
|
|
{
|
|
|
|
|
public class SyncedBag
|
|
|
|
|
{
|
|
|
|
|
public class StringSyncDictionary : SyncDictionary<string, string>
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private readonly StringSyncDictionary m_syncDictionary;
|
|
|
|
|
private readonly Dictionary<string, List<System.Action<string>>> m_callbacks = new Dictionary<string, List<Action<string>>>();
|
|
|
|
|
|
2021-02-20 15:13:41 +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[] array;
|
|
|
|
|
|
|
|
|
|
public ArrayWrapper(T[] array)
|
|
|
|
|
{
|
|
|
|
|
this.array = array;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-19 20:45:54 +00:00
|
|
|
|
|
|
|
|
|
public SyncedBag(StringSyncDictionary syncDictionary)
|
|
|
|
|
{
|
|
|
|
|
m_syncDictionary = syncDictionary;
|
|
|
|
|
|
|
|
|
|
m_syncDictionary.Callback += DictionaryCallback;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void DictionaryCallback(StringSyncDictionary.Operation op, string key, string item)
|
|
|
|
|
{
|
|
|
|
|
if (NetUtils.IsServer)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
switch (op)
|
|
|
|
|
{
|
|
|
|
|
case StringSyncDictionary.Operation.OP_ADD:
|
|
|
|
|
case StringSyncDictionary.Operation.OP_SET:
|
|
|
|
|
case StringSyncDictionary.Operation.OP_DIRTY:
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
{
|
|
|
|
|
m_callbacks.Add(key, new List<Action<string>>{callback});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
2021-02-20 15:13:41 +00:00
|
|
|
|
return JsonUtility.FromJson<ArrayWrapper<string>>(str).array;
|
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;
|
2021-02-20 15:13:41 +00:00
|
|
|
|
return JsonUtility.FromJson<ArrayWrapper<float>>(str).array;
|
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
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|