Upload from upload_mods.ps1
This commit is contained in:
44
Scripts/MonoBehaviours/AutoRemove.cs
Normal file
44
Scripts/MonoBehaviours/AutoRemove.cs
Normal file
@@ -0,0 +1,44 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class AutoRemove : TrackedBehaviourBase
|
||||
{
|
||||
public float lifetime = -1;
|
||||
|
||||
protected override void Awake()
|
||||
{
|
||||
ExplosionValue value = CustomExplosionManager.LastInitializedComponent;
|
||||
lifetime = value.CurrentExplosionParams._explosionData.Duration;
|
||||
if(lifetime > 0)
|
||||
syncOnConnect = value.Component.SyncOnConnect;
|
||||
base.Awake();
|
||||
if (lifetime > 0)
|
||||
Destroy(gameObject, lifetime);
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
if(lifetime > 0)
|
||||
lifetime -= Time.deltaTime;
|
||||
}
|
||||
|
||||
protected override void OnDestroy()
|
||||
{
|
||||
base.OnDestroy();
|
||||
CustomExplosionManager.removeInitializedParticle(gameObject);
|
||||
}
|
||||
|
||||
protected override void OnClientConnected(PooledBinaryWriter _bw)
|
||||
{
|
||||
_bw.Write(lifetime);
|
||||
}
|
||||
|
||||
protected override void OnConnectedToServer(PooledBinaryReader _br)
|
||||
{
|
||||
lifetime = _br.ReadSingle();
|
||||
if (lifetime > 0)
|
||||
Destroy(gameObject, lifetime);
|
||||
}
|
||||
}
|
||||
|
||||
97
Scripts/MonoBehaviours/NetSyncHelper.cs
Normal file
97
Scripts/MonoBehaviours/NetSyncHelper.cs
Normal file
@@ -0,0 +1,97 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class NetSyncHelper : MonoBehaviour
|
||||
{
|
||||
public event Action<PooledBinaryWriter> ClientConnected
|
||||
{
|
||||
add { list_connect_server_handlers.Add(value); }
|
||||
remove { list_connect_server_handlers.Remove(value); }
|
||||
}
|
||||
public event Action<PooledBinaryReader> ConnectedToServer
|
||||
{
|
||||
add { list_connect_client_handlers.Add(value); }
|
||||
remove { list_connect_client_handlers.Remove(value); }
|
||||
}
|
||||
public event Action<PooledBinaryWriter> ExplosionServerInit
|
||||
{
|
||||
add { list_init_server_handlers.Add(value); }
|
||||
remove { list_init_server_handlers.Remove(value); }
|
||||
}
|
||||
public event Action<PooledBinaryReader> ExplosionClientInit
|
||||
{
|
||||
add { list_init_client_handlers.Add(value); }
|
||||
remove { list_init_client_handlers.Remove(value); }
|
||||
}
|
||||
private List<Action<PooledBinaryWriter>> list_connect_server_handlers = new List<Action<PooledBinaryWriter>>();
|
||||
private List<Action<PooledBinaryReader>> list_connect_client_handlers = new List<Action<PooledBinaryReader>>();
|
||||
private List<Action<PooledBinaryWriter>> list_init_server_handlers = new List<Action<PooledBinaryWriter>>();
|
||||
private List<Action<PooledBinaryReader>> list_init_client_handlers = new List<Action<PooledBinaryReader>>();
|
||||
public ExplosionParams explParams;
|
||||
public ItemValue explValue;
|
||||
public uint explId;
|
||||
private static Dictionary<uint, NetSyncHelper> hash_helpers = new Dictionary<uint, NetSyncHelper>();
|
||||
|
||||
static NetSyncHelper()
|
||||
{
|
||||
CustomExplosionManager.CleanUp += hash_helpers.Clear;
|
||||
}
|
||||
|
||||
void Awake()
|
||||
{
|
||||
hash_helpers.Add((explId = CustomExplosionManager.LastInitializedComponent.CurrentExplosionParams._explId), this);
|
||||
if (SingletonMonoBehaviour<ConnectionManager>.Instance.IsServer && CustomExplosionManager.LastInitializedComponent.Component.SyncOnConnect)
|
||||
{
|
||||
explParams = CustomExplosionManager.LastInitializedComponent.CurrentExplosionParams;
|
||||
explValue = CustomExplosionManager.LastInitializedComponent.CurrentItemValue?.Clone();
|
||||
CustomExplosionManager.ClientConnected += OnClientConnected;
|
||||
}
|
||||
}
|
||||
|
||||
public static bool TryGetValue(uint id, out NetSyncHelper helper)
|
||||
{
|
||||
return hash_helpers.TryGetValue(id, out helper);
|
||||
}
|
||||
|
||||
void OnDestroy()
|
||||
{
|
||||
CustomExplosionManager.ClientConnected -= OnClientConnected;
|
||||
hash_helpers.Remove(explId);
|
||||
}
|
||||
|
||||
void OnClientConnected(PooledBinaryWriter _bw)
|
||||
{
|
||||
explParams._worldPos = transform.position + Origin.position;
|
||||
explParams._rotation = transform.rotation;
|
||||
byte[] array = explParams.ToByteArray();
|
||||
_bw.Write((ushort)array.Length);
|
||||
_bw.Write(array);
|
||||
_bw.Write(explValue != null);
|
||||
if (explValue != null)
|
||||
{
|
||||
explValue.Write(_bw);
|
||||
}
|
||||
foreach (var handler in list_connect_server_handlers)
|
||||
handler(_bw);
|
||||
}
|
||||
|
||||
public void OnConnectedToServer(PooledBinaryReader _br)
|
||||
{
|
||||
foreach (var handler in list_connect_client_handlers)
|
||||
handler(_br);
|
||||
}
|
||||
|
||||
public void OnExplosionServerInit(PooledBinaryWriter _bw)
|
||||
{
|
||||
foreach (var handler in list_init_server_handlers)
|
||||
handler(_bw);
|
||||
}
|
||||
|
||||
public void OnExplosionClientInit(PooledBinaryReader _br)
|
||||
{
|
||||
foreach (var handler in list_init_client_handlers)
|
||||
handler(_br);
|
||||
}
|
||||
}
|
||||
|
||||
42
Scripts/MonoBehaviours/ReverseTrackedBehaviour.cs
Normal file
42
Scripts/MonoBehaviours/ReverseTrackedBehaviour.cs
Normal file
@@ -0,0 +1,42 @@
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class ReverseTrackedBehaviour<T> : TrackedBehaviourBase where T : ReverseTrackedBehaviour<T>
|
||||
{
|
||||
protected static Dictionary<object, Dictionary<uint, T>> hash_instances = new Dictionary<object, Dictionary<uint, T>>();
|
||||
|
||||
static ReverseTrackedBehaviour()
|
||||
{
|
||||
CustomExplosionManager.CleanUp += hash_instances.Clear;
|
||||
}
|
||||
protected override void Awake()
|
||||
{
|
||||
track = true;
|
||||
base.Awake();
|
||||
}
|
||||
protected override void addRef()
|
||||
{
|
||||
if (!hash_instances.ContainsKey(key))
|
||||
hash_instances.Add(key, new Dictionary<uint, T>());
|
||||
hash_instances[key].Add(explId, (T)this);
|
||||
}
|
||||
protected override void removeRef()
|
||||
{
|
||||
var dict = hash_instances[key];
|
||||
if (dict != null)
|
||||
{
|
||||
dict.Remove(explId);
|
||||
if (dict.Count <= 0)
|
||||
hash_instances.Remove(key);
|
||||
}
|
||||
}
|
||||
|
||||
public static bool TryGetValue(uint id, object key, out T controller)
|
||||
{
|
||||
controller = null;
|
||||
if (hash_instances.TryGetValue(key, out var dict))
|
||||
return dict.TryGetValue(id, out controller);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
124
Scripts/MonoBehaviours/Timer.cs
Normal file
124
Scripts/MonoBehaviours/Timer.cs
Normal file
@@ -0,0 +1,124 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.Events;
|
||||
|
||||
/// <summary>
|
||||
/// 计时器
|
||||
/// <para>ZhangYu 2018-04-08</para>
|
||||
/// <para>code from: https://segmentfault.com/a/1190000015325310 </para>
|
||||
/// </summary>
|
||||
public class Timer : MonoBehaviour
|
||||
{
|
||||
// delay in second
|
||||
public float delay = 0;
|
||||
// interval in second
|
||||
public float interval = 1;
|
||||
// repeat count
|
||||
public int repeatCount = 1;
|
||||
// automatically start
|
||||
public bool autoStart = false;
|
||||
// automatically destroy
|
||||
public bool autoDestory = true;
|
||||
// current time
|
||||
public float currentTime = 0;
|
||||
// current count
|
||||
public int currentCount = 0;
|
||||
// event when interval reached
|
||||
public UnityEvent onIntervalEvent;
|
||||
// event when timer completed
|
||||
public UnityEvent onCompleteEvent;
|
||||
// callback delegate
|
||||
public delegate void TimerCallback(Timer timer);
|
||||
// last interval time
|
||||
private float lastTime = 0;
|
||||
// interval callback
|
||||
private TimerCallback onIntervalCall;
|
||||
// complete callback
|
||||
private TimerCallback onCompleteCall;
|
||||
|
||||
private void Start()
|
||||
{
|
||||
enabled = autoStart;
|
||||
}
|
||||
|
||||
private void FixedUpdate()
|
||||
{
|
||||
if (!enabled) return;
|
||||
addInterval(Time.deltaTime);
|
||||
}
|
||||
|
||||
/// <summary> add interval </summary>
|
||||
private void addInterval(float deltaTime)
|
||||
{
|
||||
currentTime += deltaTime;
|
||||
if (currentTime < delay) return;
|
||||
if (currentTime - lastTime >= interval)
|
||||
{
|
||||
currentCount++;
|
||||
lastTime = currentTime;
|
||||
if (repeatCount <= 0)
|
||||
{
|
||||
// repeate forever
|
||||
if (currentCount == int.MaxValue) reset();
|
||||
if (onIntervalCall != null) onIntervalCall(this);
|
||||
if (onIntervalEvent != null) onIntervalEvent.Invoke();
|
||||
}
|
||||
else
|
||||
{
|
||||
if (currentCount < repeatCount)
|
||||
{
|
||||
if (onIntervalCall != null) onIntervalCall(this);
|
||||
if (onIntervalEvent != null) onIntervalEvent.Invoke();
|
||||
}
|
||||
else
|
||||
{
|
||||
stop();
|
||||
if (onCompleteCall != null) onCompleteCall(this);
|
||||
if (onCompleteEvent != null) onCompleteEvent.Invoke();
|
||||
if (autoDestory && !enabled) Destroy(this);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void start()
|
||||
{
|
||||
enabled = autoStart = true;
|
||||
}
|
||||
|
||||
public void start(float time, TimerCallback onComplete)
|
||||
{
|
||||
start(time, 1, null, onComplete);
|
||||
}
|
||||
|
||||
public void start(float interval, int repeatCount, TimerCallback onComplete)
|
||||
{
|
||||
start(interval, repeatCount, null, onComplete);
|
||||
}
|
||||
|
||||
public void start(float interval, int repeatCount, TimerCallback onInterval, TimerCallback onComplete)
|
||||
{
|
||||
this.interval = interval;
|
||||
this.repeatCount = repeatCount;
|
||||
onIntervalCall = onInterval;
|
||||
onCompleteCall = onComplete;
|
||||
reset();
|
||||
enabled = autoStart = true;
|
||||
}
|
||||
|
||||
public void stop()
|
||||
{
|
||||
enabled = autoStart = false;
|
||||
}
|
||||
|
||||
public void reset()
|
||||
{
|
||||
lastTime = currentTime = currentCount = 0;
|
||||
}
|
||||
|
||||
public void restart()
|
||||
{
|
||||
reset();
|
||||
start();
|
||||
}
|
||||
|
||||
}
|
||||
43
Scripts/MonoBehaviours/TrackedBehaviour.cs
Normal file
43
Scripts/MonoBehaviours/TrackedBehaviour.cs
Normal file
@@ -0,0 +1,43 @@
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class TrackedBehaviour<T> : TrackedBehaviourBase where T : TrackedBehaviour<T>
|
||||
{
|
||||
protected static Dictionary<uint, Dictionary<object, T>> hash_instances = new Dictionary<uint, Dictionary<object, T>>();
|
||||
|
||||
static TrackedBehaviour()
|
||||
{
|
||||
CustomExplosionManager.CleanUp += hash_instances.Clear;
|
||||
}
|
||||
|
||||
protected override void Awake()
|
||||
{
|
||||
track = true;
|
||||
base.Awake();
|
||||
}
|
||||
protected override void addRef()
|
||||
{
|
||||
if (!hash_instances.ContainsKey(explId))
|
||||
hash_instances.Add(explId, new Dictionary<object, T>());
|
||||
hash_instances[explId].Add(key, (T)this);
|
||||
}
|
||||
|
||||
protected override void removeRef()
|
||||
{
|
||||
var dict = hash_instances[explId];
|
||||
if (dict != null)
|
||||
{
|
||||
dict.Remove(key);
|
||||
if (dict.Count <= 0)
|
||||
hash_instances.Remove(explId);
|
||||
}
|
||||
}
|
||||
|
||||
public static bool TryGetValue(uint id, object key, out T controller)
|
||||
{
|
||||
controller = null;
|
||||
if (hash_instances.TryGetValue(id, out var dict))
|
||||
return dict.TryGetValue(key, out controller);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
96
Scripts/MonoBehaviours/TrackedBehaviourBase.cs
Normal file
96
Scripts/MonoBehaviours/TrackedBehaviourBase.cs
Normal file
@@ -0,0 +1,96 @@
|
||||
using UnityEngine;
|
||||
|
||||
public class TrackedBehaviourBase : MonoBehaviour
|
||||
{
|
||||
protected uint explId;
|
||||
protected bool isServer;
|
||||
protected object key = null;
|
||||
protected bool syncOnConnect = false;
|
||||
protected bool syncOnInit = false;
|
||||
protected bool track = false;
|
||||
protected bool handleClientInfo = false;
|
||||
protected NetSyncHelper helper = null;
|
||||
|
||||
protected virtual void Awake()
|
||||
{
|
||||
explId = CustomExplosionManager.LastInitializedComponent.CurrentExplosionParams._explId;
|
||||
if (track)
|
||||
{
|
||||
if (key == null)
|
||||
{
|
||||
Log.Error("TrackedBehaviourBase: key is not initialized before addRef!");
|
||||
return;
|
||||
}
|
||||
addRef();
|
||||
}
|
||||
bool flag = NetSyncHelper.TryGetValue(explId, out helper);
|
||||
isServer = SingletonMonoBehaviour<ConnectionManager>.Instance.IsServer;
|
||||
if (flag)
|
||||
{
|
||||
if (syncOnConnect)
|
||||
{
|
||||
if (isServer)
|
||||
helper.ClientConnected += OnClientConnected;
|
||||
else
|
||||
helper.ConnectedToServer += OnConnectedToServer;
|
||||
}
|
||||
if (syncOnInit)
|
||||
{
|
||||
if (isServer)
|
||||
helper.ExplosionServerInit += OnExplosionInitServer;
|
||||
else
|
||||
helper.ExplosionClientInit += OnExplosionInitClient;
|
||||
}
|
||||
}else
|
||||
Log.Error("NetSyncHelper not initialized: explId: " + explId);
|
||||
if (handleClientInfo)
|
||||
CustomExplosionManager.HandleClientInfo += OnHandleClientInfo;
|
||||
}
|
||||
|
||||
protected virtual void OnDestroy()
|
||||
{
|
||||
if (helper != null)
|
||||
{
|
||||
if (syncOnConnect)
|
||||
{
|
||||
if (isServer)
|
||||
helper.ClientConnected -= OnClientConnected;
|
||||
else
|
||||
helper.ConnectedToServer -= OnConnectedToServer;
|
||||
}
|
||||
if (syncOnInit)
|
||||
{
|
||||
if (isServer)
|
||||
helper.ExplosionServerInit -= OnExplosionInitServer;
|
||||
else
|
||||
helper.ExplosionClientInit -= OnExplosionInitClient;
|
||||
}
|
||||
}
|
||||
if (handleClientInfo)
|
||||
CustomExplosionManager.HandleClientInfo -= OnHandleClientInfo;
|
||||
if (track)
|
||||
removeRef();
|
||||
}
|
||||
protected virtual void addRef()
|
||||
{
|
||||
}
|
||||
protected virtual void removeRef()
|
||||
{
|
||||
}
|
||||
protected virtual void OnClientConnected(PooledBinaryWriter _bw)
|
||||
{
|
||||
}
|
||||
protected virtual void OnConnectedToServer(PooledBinaryReader _br)
|
||||
{
|
||||
}
|
||||
protected virtual void OnExplosionInitServer(PooledBinaryWriter _bw)
|
||||
{
|
||||
}
|
||||
protected virtual void OnExplosionInitClient(PooledBinaryReader _br)
|
||||
{
|
||||
}
|
||||
protected virtual void OnHandleClientInfo(ClientInfo info)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user