| using System; |
| using System; |
| using System.Collections.Generic; |
| using System.Collections.Generic; |
| using System.Reflection; |
| using System.Reflection; |
| using SoftReferenceableAssets; |
| using SoftReferenceableAssets; |
| using UnityEngine; |
| using UnityEngine; |
| |
| |
| public class ZNetView : MonoBehaviour, IReferenceHolder |
| public class ZNetView : MonoBehaviour, IReferenceHolder |
| { |
| { |
| private void Awake() |
| private void Awake() |
| { |
| { |
| if (ZNetView.m_forceDisableInit || ZDOMan.instance == null) |
| if (ZNetView.m_forceDisableInit || ZDOMan.instance == null) |
| { |
| { |
| UnityEngine.Object.Destroy(this); |
| UnityEngine.Object.Destroy(this); |
| return; |
| return; |
| } |
| } |
| this.m_body = base.GetComponent<Rigidbody>(); |
| this.m_body = base.GetComponent<Rigidbody>(); |
| if (ZNetView.m_useInitZDO && ZNetView.m_initZDO == null) |
| if (ZNetView.m_useInitZDO && ZNetView.m_initZDO == null) |
| { |
| { |
| ZLog.LogWarning("Double ZNetview when initializing object " + base.gameObject.name); |
| ZLog.LogWarning("Double ZNetview when initializing object " + base.gameObject.name); |
| } |
| } |
| if (ZNetView.m_initZDO != null) |
| if (ZNetView.m_initZDO != null) |
| { |
| { |
| this.m_zdo = ZNetView.m_initZDO; |
| this.m_zdo = ZNetView.m_initZDO; |
| ZNetView.m_initZDO = null; |
| ZNetView.m_initZDO = null; |
| if (this.m_zdo.Type != this.m_type && this.m_zdo.IsOwner()) |
| if (this.m_zdo.Type != this.m_type && this.m_zdo.IsOwner()) |
| { |
| { |
| this.m_zdo.SetType(this.m_type); |
| this.m_zdo.SetType(this.m_type); |
| } |
| } |
| if (this.m_zdo.Distant != this.m_distant && this.m_zdo.IsOwner()) |
| if (this.m_zdo.Distant != this.m_distant && this.m_zdo.IsOwner()) |
| { |
| { |
| this.m_zdo.SetDistant(this.m_distant); |
| this.m_zdo.SetDistant(this.m_distant); |
| } |
| } |
| if (this.m_syncInitialScale) |
| if (this.m_syncInitialScale) |
| { |
| { |
| Vector3 vec = this.m_zdo.GetVec3(ZDOVars.s_scaleHash, Vector3.zero); |
| Vector3 vec = this.m_zdo.GetVec3(ZDOVars.s_scaleHash, Vector3.zero); |
| if (vec != Vector3.zero) |
| if (vec != Vector3.zero) |
| { |
| { |
| base.transform.localScale = vec; |
| base.transform.localScale = vec; |
| } |
| } |
| else |
| else |
| { |
| { |
| float @float = this.m_zdo.GetFloat(ZDOVars.s_scaleScalarHash, base.transform.localScale.x); |
| float @float = this.m_zdo.GetFloat(ZDOVars.s_scaleScalarHash, base.transform.localScale.x); |
| if (!base.transform.localScale.x.Equals(@float)) |
| if (!base.transform.localScale.x.Equals(@float)) |
| { |
| { |
| base.transform.localScale = new Vector3(@float, @float, @float); |
| base.transform.localScale = new Vector3(@float, @float, @float); |
| } |
| } |
| } |
| } |
| } |
| } |
| if (this.m_body) |
| if (this.m_body) |
| { |
| { |
| this.m_body.Sleep(); |
| this.m_body.Sleep(); |
| } |
| } |
| } |
| } |
| else |
| else |
| { |
| { |
| string prefabName = this.GetPrefabName(); |
| string prefabName = this.GetPrefabName(); |
| this.m_zdo = ZDOMan.instance.CreateNewZDO(base.transform.position, prefabName.GetStableHashCode()); |
| this.m_zdo = ZDOMan.instance.CreateNewZDO(base.transform.position, prefabName.GetStableHashCode()); |
| this.m_zdo.Persistent = this.m_persistent; |
| this.m_zdo.Persistent = this.m_persistent; |
| this.m_zdo.Type = this.m_type; |
| this.m_zdo.Type = this.m_type; |
| this.m_zdo.Distant = this.m_distant; |
| this.m_zdo.Distant = this.m_distant; |
| this.m_zdo.SetPrefab(prefabName.GetStableHashCode()); |
| this.m_zdo.SetPrefab(prefabName.GetStableHashCode()); |
| this.m_zdo.SetRotation(base.transform.rotation); |
| this.m_zdo.SetRotation(base.transform.rotation); |
| if (this.m_syncInitialScale) |
| if (this.m_syncInitialScale) |
| { |
| { |
| this.SyncScale(true); |
| this.SyncScale(true); |
| } |
| } |
| if (ZNetView.m_ghostInit) |
| if (ZNetView.m_ghostInit) |
| { |
| { |
| this.m_ghost = true; |
| this.m_ghost = true; |
| return; |
| return; |
| } |
| } |
| } |
| } |
| this.LoadFields(); |
| this.LoadFields(); |
| ZNetScene.instance.AddInstance(this.m_zdo, this); |
| ZNetScene.instance.AddInstance(this.m_zdo, this); |
| } |
| } |
| |
| |
| public void SetLocalScale(Vector3 scale) |
| public void SetLocalScale(Vector3 scale) |
| { |
| { |
| if (base.transform.localScale == scale) |
| if (base.transform.localScale == scale) |
| { |
| { |
| return; |
| return; |
| } |
| } |
| base.transform.localScale = scale; |
| base.transform.localScale = scale; |
| if (this.m_zdo != null && this.m_syncInitialScale && this.IsOwner()) |
| if (this.m_zdo != null && this.m_syncInitialScale && this.IsOwner()) |
| { |
| { |
| this.SyncScale(false); |
| this.SyncScale(false); |
| } |
| } |
| } |
| } |
| |
| |
| private void SyncScale(bool skipOne = false) |
| private void SyncScale(bool skipOne = false) |
| { |
| { |
| if (!Mathf.Approximately(base.transform.localScale.x, base.transform.localScale.y) || !Mathf.Approximately(base.transform.localScale.x, base.transform.localScale.z)) |
| if (!Mathf.Approximately(base.transform.localScale.x, base.transform.localScale.y) || !Mathf.Approximately(base.transform.localScale.x, base.transform.localScale.z)) |
| { |
| { |
| this.m_zdo.Set(ZDOVars.s_scaleHash, base.transform.localScale); |
| this.m_zdo.Set(ZDOVars.s_scaleHash, base.transform.localScale); |
| return; |
| return; |
| } |
| } |
| if (skipOne && Mathf.Approximately(base.transform.localScale.x, 1f)) |
| if (skipOne && Mathf.Approximately(base.transform.localScale.x, 1f)) |
| { |
| { |
| return; |
| return; |
| } |
| } |
| this.m_zdo.Set(ZDOVars.s_scaleScalarHash, base.transform.localScale.x); |
| this.m_zdo.Set(ZDOVars.s_scaleScalarHash, base.transform.localScale.x); |
| } |
| } |
| |
| |
| private void OnDestroy() |
| private void OnDestroy() |
| { |
| { |
| ZNetScene.instance; |
| ZNetScene.instance; |
| if (this.m_heldReferences != null) |
| if (this.m_heldReferences != null) |
| { |
| { |
| for (int i = 0; i < this.m_heldReferences.Count; i++) |
| for (int i = 0; i < this.m_heldReferences.Count; i++) |
| { |
| { |
| this.m_heldReferences[i].Release(); |
| this.m_heldReferences[i].Release(); |
| } |
| } |
| this.m_heldReferences.Clear(); |
| this.m_heldReferences.Clear(); |
| } |
| } |
| } |
| } |
| |
| |
| public void LoadFields() |
| public void LoadFields() |
| { |
| { |
| ZDO zdo = this.GetZDO(); |
| ZDO zdo = this.GetZDO(); |
| if (!zdo.GetBool("HasFields", false)) |
| if (!zdo.GetBool("HasFields", false)) |
| { |
| { |
| return; |
| return; |
| } |
| } |
| base.gameObject.GetComponentsInChildren<MonoBehaviour>(ZNetView.m_tempComponents); |
| base.gameObject.GetComponentsInChildren<MonoBehaviour>(ZNetView.m_tempComponents); |
| foreach (MonoBehaviour monoBehaviour in ZNetView.m_tempComponents) |
| foreach (MonoBehaviour monoBehaviour in ZNetView.m_tempComponents) |
| { |
| { |
| string text = monoBehaviour.GetType().Name; |
| string text = monoBehaviour.GetType().Name; |
| ("HasFields" + text).GetStableHashCode(); |
| ("HasFields" + text).GetStableHashCode(); |
| if (zdo.GetBool("HasFields" + text, false)) |
| if (zdo.GetBool("HasFields" + text, false)) |
| { |
| { |
| foreach (FieldInfo fieldInfo in monoBehaviour.GetType().GetFields(BindingFlags.Instance | BindingFlags.Public)) |
| foreach (FieldInfo fieldInfo in monoBehaviour.GetType().GetFields(BindingFlags.Instance | BindingFlags.Public)) |
| { |
| { |
| text = monoBehaviour.GetType().Name + "." + fieldInfo.Name; |
| text = monoBehaviour.GetType().Name + "." + fieldInfo.Name; |
| int num; |
| int num; |
| float num2; |
| float num2; |
| bool flag; |
| bool flag; |
| Vector3 vector; |
| Vector3 vector; |
| string text2; |
| string text2; |
| string text3; |
| string text3; |
| if (fieldInfo.FieldType == typeof(int) && zdo.GetInt(text, out num)) |
| if (fieldInfo.FieldType == typeof(int) && zdo.GetInt(text, out num)) |
| { |
| { |
| fieldInfo.SetValue(monoBehaviour, num); |
| fieldInfo.SetValue(monoBehaviour, num); |
| } |
| } |
| else if (fieldInfo.FieldType == typeof(float) && zdo.GetFloat(text, out num2)) |
| else if (fieldInfo.FieldType == typeof(float) && zdo.GetFloat(text, out num2)) |
| { |
| { |
| fieldInfo.SetValue(monoBehaviour, num2); |
| fieldInfo.SetValue(monoBehaviour, num2); |
| } |
| } |
| else if (fieldInfo.FieldType == typeof(bool) && zdo.GetBool(text, out flag)) |
| else if (fieldInfo.FieldType == typeof(bool) && zdo.GetBool(text, out flag)) |
| { |
| { |
| fieldInfo.SetValue(monoBehaviour, flag); |
| fieldInfo.SetValue(monoBehaviour, flag); |
| } |
| } |
| else if (fieldInfo.FieldType == typeof(Vector3) && zdo.GetVec3(text, out vector)) |
| else if (fieldInfo.FieldType == typeof(Vector3) && zdo.GetVec3(text, out vector)) |
| { |
| { |
| fieldInfo.SetValue(monoBehaviour, vector); |
| fieldInfo.SetValue(monoBehaviour, vector); |
| } |
| } |
| else if (fieldInfo.FieldType == typeof(string) && zdo.GetString(text, out text2)) |
| else if (fieldInfo.FieldType == typeof(string) && zdo.GetString(text, out text2)) |
| { |
| { |
| fieldInfo.SetValue(monoBehaviour, text2); |
| fieldInfo.SetValue(monoBehaviour, text2); |
| } |
| } |
| else if (fieldInfo.FieldType == typeof(GameObject) && zdo.GetString(text, out text3)) |
| else if (fieldInfo.FieldType == typeof(GameObject) && zdo.GetString(text, out text3)) |
| { |
| { |
| GameObject prefab = ZNetScene.instance.GetPrefab(text3); |
| GameObject prefab = ZNetScene.instance.GetPrefab(text3); |
| if (prefab) |
| if (prefab) |
| { |
| { |
| fieldInfo.SetValue(monoBehaviour, prefab); |
| fieldInfo.SetValue(monoBehaviour, prefab); |
| } |
| } |
| } |
| } |
| } |
| } |
| } |
| } |
| } |
| } |
| } |
| } |
| |
| |
| private string GetPrefabName() |
| private string GetPrefabName() |
| { |
| { |
| . | return Utils.GetPrefabName(base.gameObject); |
| return global::Utils.GetPrefabName(base.gameObject); |
| } |
| } |
| |
| |
| public void Destroy() |
| public void Destroy() |
| { |
| { |
| ZNetScene.instance.Destroy(base.gameObject); |
| ZNetScene.instance.Destroy(base.gameObject); |
| } |
| } |
| |
| |
| public bool IsOwner() |
| public bool IsOwner() |
| { |
| { |
| return this.IsValid() && this.m_zdo.IsOwner(); |
| return this.IsValid() && this.m_zdo.IsOwner(); |
| } |
| } |
| |
| |
| public bool HasOwner() |
| public bool HasOwner() |
| { |
| { |
| return this.IsValid() && this.m_zdo.HasOwner(); |
| return this.IsValid() && this.m_zdo.HasOwner(); |
| } |
| } |
| |
| |
| public void ClaimOwnership() |
| public void ClaimOwnership() |
| { |
| { |
| if (this.IsOwner()) |
| if (this.IsOwner()) |
| { |
| { |
| return; |
| return; |
| } |
| } |
| this.m_zdo.SetOwner(ZDOMan.GetSessionID()); |
| this.m_zdo.SetOwner(ZDOMan.GetSessionID()); |
| } |
| } |
| |
| |
| public ZDO GetZDO() |
| public ZDO GetZDO() |
| { |
| { |
| return this.m_zdo; |
| return this.m_zdo; |
| } |
| } |
| |
| |
| public bool IsValid() |
| public bool IsValid() |
| { |
| { |
| return this.m_zdo != null && this.m_zdo.IsValid(); |
| return this.m_zdo != null && this.m_zdo.IsValid(); |
| } |
| } |
| |
| |
| public void ResetZDO() |
| public void ResetZDO() |
| { |
| { |
| this.m_zdo.Created = false; |
| this.m_zdo.Created = false; |
| this.m_zdo = null; |
| this.m_zdo = null; |
| } |
| } |
| |
| |
| public void Register(string name, Action<long> f) |
| public void Register(string name, Action<long> f) |
| { |
| { |
| this.m_functions.Add(name.GetStableHashCode(), new RoutedMethod(f)); |
| this.m_functions.Add(name.GetStableHashCode(), new RoutedMethod(f)); |
| } |
| } |
| |
| |
| public void Register<T>(string name, Action<long, T> f) |
| public void Register<T>(string name, Action<long, T> f) |
| { |
| { |
| this.m_functions.Add(name.GetStableHashCode(), new RoutedMethod<T>(f)); |
| this.m_functions.Add(name.GetStableHashCode(), new RoutedMethod<T>(f)); |
| } |
| } |
| |
| |
| public void Register<T, U>(string name, Action<long, T, U> f) |
| public void Register<T, U>(string name, Action<long, T, U> f) |
| { |
| { |
| this.m_functions.Add(name.GetStableHashCode(), new RoutedMethod<T, U>(f)); |
| this.m_functions.Add(name.GetStableHashCode(), new RoutedMethod<T, U>(f)); |
| } |
| } |
| |
| |
| public void Register<T, U, V>(string name, Action<long, T, U, V> f) |
| public void Register<T, U, V>(string name, Action<long, T, U, V> f) |
| { |
| { |
| this.m_functions.Add(name.GetStableHashCode(), new RoutedMethod<T, U, V>(f)); |
| this.m_functions.Add(name.GetStableHashCode(), new RoutedMethod<T, U, V>(f)); |
| } |
| } |
| |
| |
| public void Register<T, U, V, B>(string name, RoutedMethod<T, U, V, B>.Method f) |
| public void Register<T, U, V, B>(string name, RoutedMethod<T, U, V, B>.Method f) |
| { |
| { |
| this.m_functions.Add(name.GetStableHashCode(), new RoutedMethod<T, U, V, B>(f)); |
| this.m_functions.Add(name.GetStableHashCode(), new RoutedMethod<T, U, V, B>(f)); |
| } |
| } |
| |
| |
| public void Unregister(string name) |
| public void Unregister(string name) |
| { |
| { |
| int stableHashCode = name.GetStableHashCode(); |
| int stableHashCode = name.GetStableHashCode(); |
| this.m_functions.Remove(stableHashCode); |
| this.m_functions.Remove(stableHashCode); |
| } |
| } |
| |
| |
| public void HandleRoutedRPC(ZRoutedRpc.RoutedRPCData rpcData) |
| public void HandleRoutedRPC(ZRoutedRpc.RoutedRPCData rpcData) |
| { |
| { |
| RoutedMethodBase routedMethodBase; |
| RoutedMethodBase routedMethodBase; |
| if (this.m_functions.TryGetValue(rpcData.m_methodHash, out routedMethodBase)) |
| if (this.m_functions.TryGetValue(rpcData.m_methodHash, out routedMethodBase)) |
| { |
| { |
| routedMethodBase.Invoke(rpcData.m_senderPeerID, rpcData.m_parameters); |
| routedMethodBase.Invoke(rpcData.m_senderPeerID, rpcData.m_parameters); |
| return; |
| return; |
| } |
| } |
| ZLog.LogWarning("Failed to find rpc method " + rpcData.m_methodHash.ToString()); |
| ZLog.LogWarning("Failed to find rpc method " + rpcData.m_methodHash.ToString()); |
| } |
| } |
| |
| |
| public void InvokeRPC(long targetID, string method, params object[] parameters) |
| public void InvokeRPC(long targetID, string method, params object[] parameters) |
| { |
| { |
| ZRoutedRpc.instance.InvokeRoutedRPC(targetID, this.m_zdo.m_uid, method, parameters); |
| ZRoutedRpc.instance.InvokeRoutedRPC(targetID, this.m_zdo.m_uid, method, parameters); |
| } |
| } |
| |
| |
| public void InvokeRPC(string method, params object[] parameters) |
| public void InvokeRPC(string method, params object[] parameters) |
| { |
| { |
| ZRoutedRpc.instance.InvokeRoutedRPC(this.m_zdo.GetOwner(), this.m_zdo.m_uid, method, parameters); |
| ZRoutedRpc.instance.InvokeRoutedRPC(this.m_zdo.GetOwner(), this.m_zdo.m_uid, method, parameters); |
| } |
| } |
| |
| |
| public static object[] Deserialize(long callerID, ParameterInfo[] paramInfo, ZPackage pkg) |
| public static object[] Deserialize(long callerID, ParameterInfo[] paramInfo, ZPackage pkg) |
| { |
| { |
| List<object> list = new List<object>(); |
| List<object> list = new List<object>(); |
| list.Add(callerID); |
| list.Add(callerID); |
| ZRpc.Deserialize(paramInfo, pkg, ref list); |
| ZRpc.Deserialize(paramInfo, pkg, ref list); |
| return list.ToArray(); |
| return list.ToArray(); |
| } |
| } |
| |
| |
| public static void StartGhostInit() |
| public static void StartGhostInit() |
| { |
| { |
| ZNetView.m_ghostInit = true; |
| ZNetView.m_ghostInit = true; |
| } |
| } |
| |
| |
| public static void FinishGhostInit() |
| public static void FinishGhostInit() |
| { |
| { |
| ZNetView.m_ghostInit = false; |
| ZNetView.m_ghostInit = false; |
| } |
| } |
| |
| |
| public void HoldReferenceTo(IReferenceCounted reference) |
| public void HoldReferenceTo(IReferenceCounted reference) |
| { |
| { |
| if (this.m_heldReferences == null) |
| if (this.m_heldReferences == null) |
| { |
| { |
| this.m_heldReferences = new List<IReferenceCounted>(1); |
| this.m_heldReferences = new List<IReferenceCounted>(1); |
| } |
| } |
| reference.HoldReference(); |
| reference.HoldReference(); |
| this.m_heldReferences.Add(reference); |
| this.m_heldReferences.Add(reference); |
| } |
| } |
| |
| |
| public void ReleaseReferenceTo(IReferenceCounted reference) |
| public void ReleaseReferenceTo(IReferenceCounted reference) |
| { |
| { |
| if (this.m_heldReferences == null) |
| if (this.m_heldReferences == null) |
| { |
| { |
| return; |
| return; |
| } |
| } |
| if (this.m_heldReferences.Remove(reference)) |
| if (this.m_heldReferences.Remove(reference)) |
| { |
| { |
| reference.Release(); |
| reference.Release(); |
| } |
| } |
| } |
| } |
| |
| |
| public const string CustomFieldsStr = "HasFields"; |
| public const string CustomFieldsStr = "HasFields"; |
| |
| |
| public static long Everybody = 0L; |
| public static long Everybody = 0L; |
| |
| |
| public bool m_persistent; |
| public bool m_persistent; |
| |
| |
| public bool m_distant; |
| public bool m_distant; |
| |
| |
| public ZDO.ObjectType m_type; |
| public ZDO.ObjectType m_type; |
| |
| |
| public bool m_syncInitialScale; |
| public bool m_syncInitialScale; |
| |
| |
| public static bool m_useInitZDO = false; |
| public static bool m_useInitZDO = false; |
| |
| |
| public static ZDO m_initZDO = null; |
| public static ZDO m_initZDO = null; |
| |
| |
| public static bool m_forceDisableInit = false; |
| public static bool m_forceDisableInit = false; |
| |
| |
| private ZDO m_zdo; |
| private ZDO m_zdo; |
| |
| |
| private Rigidbody m_body; |
| private Rigidbody m_body; |
| |
| |
| private Dictionary<int, RoutedMethodBase> m_functions = new Dictionary<int, RoutedMethodBase>(); |
| private Dictionary<int, RoutedMethodBase> m_functions = new Dictionary<int, RoutedMethodBase>(); |
| |
| |
| private bool m_ghost; |
| private bool m_ghost; |
| |
| |
| private static List<MonoBehaviour> m_tempComponents = new List<MonoBehaviour>(); |
| private static List<MonoBehaviour> m_tempComponents = new List<MonoBehaviour>(); |
| |
| |
| private static bool m_ghostInit = false; |
| private static bool m_ghostInit = false; |
| |
| |
| private List<IReferenceCounted> m_heldReferences; |
| private List<IReferenceCounted> m_heldReferences; |
| } |
| } |
| |
| |