public void ReadEvents(NetDataReader reader, bool arbiter) { KeyValuePair <MonoBehaviour, List <PropertyInfo> > selectedBehaviour = default; // TODO: This while should no longer be necessary? while (!reader.EndOfData) { DataType datatype = (DataType)reader.GetByte(); // We have read all of the fields for every behaviour // on this object. if (datatype == DataType.Object) { return; } else if (datatype == DataType.Behaviour) { int behaviourIndex = reader.GetInt(); selectedBehaviour = stateEventsByMonoBehaviour[behaviourIndex]; } else if (datatype == DataType.Field) { int eventIndex = reader.GetInt(); StatefulEventBase statefulEvent = (StatefulEventBase)selectedBehaviour.Value[eventIndex].GetValue(selectedBehaviour.Key); statefulEvent.Read(reader); if (statefulEvent.HasControl) { statefulEvent.HasPendingCall = false; } } } }
public void SetControl(bool value) { HasControl = value; for (int i = 0; i < stateFieldsByMonoBehaviour.Count; i++) { var kvp = stateFieldsByMonoBehaviour[i]; for (int j = 0; j < kvp.Value.Count; j++) { PropertyInfo info = kvp.Value[j]; IStateField field = (IStateField)info.GetValue(kvp.Key); field.HasControl = value; } } for (int i = 0; i < stateEventsByMonoBehaviour.Count; i++) { var kvp = stateEventsByMonoBehaviour[i]; for (int j = 0; j < kvp.Value.Count; j++) { PropertyInfo info = kvp.Value[j]; StatefulEventBase statefulEvent = (StatefulEventBase)info.GetValue(kvp.Key); statefulEvent.HasControl = value; } } }
// TODO: Much of this code is duplicated from the StateField functionality; // this should be consolidated somewhere. #region StateEvents public void WriteEvents(NetDataWriter writer, int id) { bool objectHeaderWritten, behaviourHeaderWritten; objectHeaderWritten = false; for (int i = 0; i < stateEventsByMonoBehaviour.Count; i++) { behaviourHeaderWritten = false; var kvp = stateEventsByMonoBehaviour[i]; for (int j = 0; j < kvp.Value.Count; j++) { PropertyInfo info = kvp.Value[j]; StatefulEventBase statefulEvent = (StatefulEventBase)info.GetValue(kvp.Key); if (statefulEvent.HasPendingCall) { // If this is the first field written for this object, // we need to write in this object's ID. if (!objectHeaderWritten) { writer.Put((byte)DataType.Object); writer.Put(id); objectHeaderWritten = true; } // Same as above, but per-behaviour. if (!behaviourHeaderWritten) { writer.Put((byte)DataType.Behaviour); writer.Put(i); behaviourHeaderWritten = true; } // TODO: DataType.Field should be renamed to be more general. writer.Put((byte)DataType.Field); writer.Put(j); statefulEvent.Write(writer); statefulEvent.HasPendingCall = false; Debug.Log($"Writing StatefulEvent <b>{info.Name}</b>.", id, LogType.Event); } } } }