private static void SerializeNetObjDict(NonAllocDictionary <int, NetObject> dict, byte[] buffer, ref int bitposition, ref SerializationFlags flags, SerializationFlags writeFlags) { foreach (var no in dict.Values) { /// Not end of netobjs write bool int holdStartPos = bitposition; /// Write viewID buffer.WritePackedBytes((uint)no.ViewID, ref bitposition, 32); /// Write hadData bool int holdHasDataPos = bitposition; buffer.WriteBool(true, ref bitposition); /// Log the data size write position and write a placeholder. int holdDataSizePos = bitposition; bitposition += BITS_FOR_NETOBJ_SIZE; var objflags = no.OnNetSerialize(_currFrameId, buffer, ref bitposition, writeFlags); /// Skip netobjs if they had nothing to say if (objflags == SerializationFlags.None) { /// Rewind if this is a no-data write. if (no.SkipWhenEmpty) { bitposition = holdStartPos; } else { bitposition = holdHasDataPos; buffer.WriteBool(false, ref bitposition); } } else { /// Revise the data size now that we know it. flags |= objflags; int bitcount = bitposition - holdDataSizePos; buffer.Write((uint)bitcount, ref holdDataSizePos, BITS_FOR_NETOBJ_SIZE); } //Debug.Log(objflags + " / flg: " + (onNetSerialize[i] as Component).name + " " + flags); } }
private static void SerializeAllAndSend() { byte[] buffer = NetMsgSends.reusableBuffer; int bitposition = 0; SerializationFlags writeFlags; SerializationFlags flags; if (TickManager.needToSendInitialForNewConn) { writeFlags = SerializationFlags.NewConnection | SerializationFlags.ForceReliable | SerializationFlags.Force; flags = SerializationFlags.HasContent; TickManager.needToSendInitialForNewConn = false; } else { writeFlags = SerializationFlags.None; flags = SerializationFlags.None; } /// Write frameId buffer.Write((uint)_currFrameId, ref bitposition, TickEngineSettings.frameCountBits); NetMasterCallbacks.OnPreSerializeTickCallbacks(_currFrameId, buffer, ref bitposition); #region NetObject Serialization /// Loop through owned NetObjects NetObject.NetObjDictsLocked = true; NonAllocDictionary <int, NetObject> controlledObjs = NetObject.activeControlledNetObjs; SerializeNetObjDict(controlledObjs, buffer, ref bitposition, ref flags, writeFlags); //NonAllocDictionary<int, NetObject> ownedButNotControlledObjs = NetObject.activeOwnedNetObjs; //if (ownedButNotControlledObjs.Count > 0) // Debug.Log("LIMBO COUNT " + ownedButNotControlledObjs.Count); //SerializeNetObjDict(ownedButNotControlledObjs, buffer, ref bitposition, ref flags, writeFlags); //foreach (var no in ownedObjs) //{ // /// Not end of netobjs write bool // int holdStartPos = bitposition; // /// Write viewID // buffer.WritePackedBytes((uint)no.ViewID, ref bitposition, 32); // /// Write hadData bool // int holdHasDataPos = bitposition; // buffer.WriteBool(true, ref bitposition); // /// Log the data size write position and write a placeholder. // int holdDataSizePos = bitposition; // bitposition += BITS_FOR_NETOBJ_SIZE; // var objflags = no.OnNetSerialize(_currFrameId, buffer, ref bitposition, writeFlags); // /// Skip netobjs if they had nothing to say // if (objflags == SerializationFlags.None) // { // /// Rewind if this is a no-data write. // if (no.SkipWhenEmpty) // { // bitposition = holdStartPos; // } // else // { // bitposition = holdHasDataPos; // buffer.WriteBool(false, ref bitposition); // } // } // else // { // /// Revise the data size now that we know it. // flags |= objflags; // int bitcount = bitposition - holdDataSizePos; // buffer.Write((uint)bitcount, ref holdDataSizePos, BITS_FOR_NETOBJ_SIZE); // } // //Debug.Log(objflags + " / flg: " + (onNetSerialize[i] as Component).name + " " + flags); //} NetObject.NetObjDictsLocked = false; #endregion // Any deferrered Ownership changes from SyncState happen here while (NetMasterCallbacks.postSerializationActions.Count > 0) { NetMasterCallbacks.postSerializationActions.Dequeue().Invoke(); } if (flags == SerializationFlags.None) { return; } /// End of NetObject write bool buffer.WritePackedBytes(0, ref bitposition, 32); NetMsgSends.Send(buffer, bitposition, null, flags, true); }
public static string PrintOpResponseParams(this NonAllocDictionary <byte, object> data, bool printType = false) { StringBuilder builder = new StringBuilder("{"); if (!data.IsNull() && data.Count > 0) { foreach (var param in data) { string name = string.Empty, value = string.Empty; if (param.Key == ParameterCode.PlayerProperties) { name = "PlayerProperties"; Hashtable props = param.Value as Hashtable; if (!props.IsNull()) { builder.AppendFormat("{0}:{{", name); if (props.Count > 0) { foreach (var p in props.Keys) { Hashtable tmp = props[p] as Hashtable; builder.AppendFormat("{0}:{1},", p, tmp.PrintParams(typeof(ActorProperties), printType)); } builder.Remove(builder.Length - 1, 1); } builder.Append("},"); continue; } } else if (param.Key == ParameterCode.GameProperties) { name = "GameProperties"; value = (param.Value as Hashtable).PrintParams(typeof(GamePropertyKey), printType); } else if (param.Key == ParameterCode.Properties) { name = "Properties"; value = (param.Value as Hashtable).PrintParams(typeof(GamePropertyKey), printType); } else if (param.Key == ParameterCode.GameList) { name = "GameList"; Hashtable hashtable = param.Value as Hashtable; if (hashtable.Count > 0) { builder.AppendFormat("{0}:{{", name); foreach (string room in hashtable.Keys) { Hashtable hash = hashtable[room] as Hashtable; builder.AppendFormat("{0}:{1},", room, hash.PrintParams(typeof(GamePropertyKey), printType)); } builder.Remove(builder.Length - 1, 1); builder.Append("}}"); } else { builder.AppendFormat("{0}:{{}}}}", name); } continue; } else { name = UtilityClass.GetConstantNameFromValue(typeof(ParameterCode), param.Key); if (name.IsNullOrEmpty()) { name = param.Key.ToString(); } value = param.Value.Stringify(printType); } builder.AppendFormat("{0}:{1},", name, value); } builder.Remove(builder.Length - 1, 1); } builder.Append("}"); return(builder.ToString()); }