public NetworkVar <T> Create <T>(T value = default(T), bool serverOnly = true, ulong syncPeriod = 1, bool defer = false, IEqualityComparer <T> comparer = null, bool haveOwner = false, ulong owner = 0, Action <NetworkVar <T> > valueUpdated = null) { var res = new NetworkVar <T>(nhs, counter++, entityId, value, serverOnly, syncPeriod, defer, comparer, haveOwner, owner, valueUpdated); nhs.RegisterVariable(res); return(res); }
//Gathers all NetworkVars attached to the current gameObject //Needs to be called again to be updated private void GetAllNetVars() { if (dicNetVars == null) { dicNetVars = new Dictionary <string, string>(); } MonoBehaviour[] mbs = GetComponents <MonoBehaviour>(); foreach (MonoBehaviour mb in mbs) { FieldInfo[] pis = mb.GetType().GetFields(); foreach (FieldInfo pi in pis) { object[] os = pi.GetCustomAttributes(true); foreach (object o in os) { NetworkVar nv = o as NetworkVar; if (nv != null) { //Key is comprised of component type + var name string key = pi.ReflectedType + "," + pi.Name; if (!dicNetVars.ContainsKey(key)) { //The value is the string object dicNetVars.Add(key, pi.GetValue(mb).ToString()); } } } } } }
//Checks for update on any vars found in GetAllNetVars() //Sends an update to the server should an update be found private void CheckAllNetVars() { if (dicNetVars == null) { dicNetVars = new Dictionary <string, string>(); } MonoBehaviour[] mbs = GetComponents <MonoBehaviour>(); foreach (MonoBehaviour mb in mbs) { FieldInfo[] pis = mb.GetType().GetFields(); foreach (FieldInfo pi in pis) { object[] os = pi.GetCustomAttributes(true); foreach (object o in os) { NetworkVar nv = o as NetworkVar; if (nv != null) { string key = pi.ReflectedType + "," + pi.Name; if (!dicNetVars[key].ToString().Equals(pi.GetValue(mb).ToString())) { //The value has changed, send an update message and our current transform/rigidbody dicNetVars[key] = pi.GetValue(mb).ToString(); client.SendMessageToServer(csvRecord(',', "change", id.ToString(), key, dicNetVars[key])); Debug.Log("Var Change: " + pi.GetValue(mb).ToString()); Vector3 pos = gameObject.transform.position; Vector3 rot = gameObject.transform.rotation.eulerAngles; client.SendMessageToServer(csvRecord(',', "utransform", id.ToString(), pos.x.ToString("F4"), pos.y.ToString("F4"), pos.z.ToString("F4"), rot.x.ToString("F4"), rot.y.ToString("F4"), rot.z.ToString("F4"))); Vector3 vel = rb.velocity; Vector3 rotvel = rb.rotation.eulerAngles; client.SendMessageToServer(csvRecord(',', "urigidbody", id.ToString(), vel.x.ToString("F4"), vel.y.ToString("F4"), vel.z.ToString("F4"), rotvel.x.ToString("F4"), rotvel.y.ToString("F4"), rotvel.z.ToString("F4"))); } } } } } }
internal void RegisterVariable(NetworkVar variable) { ConcurrentDictionary <ulong, NetworkVar> vars; if (variable.Entity == 0) { vars = globalVariables; } else { vars = entityVariables.GetOrAdd(variable.Entity, new ConcurrentDictionary <ulong, NetworkVar>()); } vars.AddOrUpdate(variable.Key, variable, (k, o) => { throw new Exception("WTF?"); }); if (!IsServer) { SendMessageToServer(new PacketNetworkVarRequest { Entity = variable.Entity, Variable = variable.Key }); } }
//When a client send an update from CheckAllNetVars(), it sends a message // which will be decoded and called into this on the server/other cliends side public void UpdateNetVar(string key, string value) { MonoBehaviour[] mbs = GetComponents <MonoBehaviour>(); foreach (MonoBehaviour mb in mbs) { FieldInfo[] pis = mb.GetType().GetFields(); foreach (FieldInfo pi in pis) { object[] os = pi.GetCustomAttributes(true); foreach (object o in os) { NetworkVar nv = o as NetworkVar; if (nv != null) { if (key.Equals(pi.ReflectedType + "," + pi.Name)) { Type t = pi.FieldType; pi.SetValue(mb, Convert.ChangeType(value, t)); } } } } } }
internal DisposablePause(NetworkVar networkVar) { source = networkVar; }
public void Dispose() { source?.Resume(); source = null; }