// compares with another UnitStats, returns flags. checks only fields with ModFlags attribute public ModifiedFlags CompareStats(UnitStats other) { ModifiedFlags flags = 0; FieldInfo[] fields = typeof(UnitStats).GetFields(); foreach (FieldInfo field in fields) { ModFlags[] mf = (ModFlags[])field.GetCustomAttributes(typeof(ModFlags), false); if (mf.Length <= 0) { continue; } ModifiedFlags applyMF = 0; foreach (var singleMF in mf) { applyMF |= singleMF.Flags; } if ((flags & applyMF) != 0) // already checked { continue; } if (field.GetValue(this) != field.GetValue(other)) { flags |= applyMF; } } return(flags); }
protected virtual void SetMaterial(Material value) { if (material != value) { modifiedFlag |= ModifiedFlags.ModifiedMaterial; material = value; UpdateMaterial(); } }
protected virtual void SetModifiedFlag(ModifiedFlags value) { if (modifiedFlag == 0 && value != 0) { tmManager.Instance.RegisterModifiedRender(this); } modifiedFlag = value; }
//public static UnitStats operator +(UnitStats a, UnitStats b) public void MergeStats(UnitStats b) { UnitStats a = this; UnitStats outObj = this; FieldInfo[] fields = typeof(UnitStats).GetFields(); foreach (FieldInfo field in fields) { PreserveStats[] ps = (PreserveStats[])field.GetCustomAttributes(typeof(PreserveStats), false); if (ps.Length > 0) { continue; } ModFlags[] mf = (ModFlags[])field.GetCustomAttributes(typeof(ModFlags), false); ModifiedFlags applyMF = 0; foreach (var singleMF in mf) { applyMF |= singleMF.Flags; } // we process byte, short, int, float and long. // handle overflows. if (field.FieldType == typeof(byte)) { int fa = (byte)field.GetValue(a); int fb = (byte)field.GetValue(b); field.SetValue(outObj, (byte)Math.Min(fa + fb, 255)); } else if (field.FieldType == typeof(short)) { int fa = (short)field.GetValue(a); int fb = (short)field.GetValue(b); field.SetValue(outObj, (short)Math.Max(Math.Min(fa + fb, 32767), -32768)); } else if (field.FieldType == typeof(int)) { long fa = (int)field.GetValue(a); long fb = (int)field.GetValue(b); field.SetValue(outObj, (int)Math.Max(Math.Min(fa + fb, 2147483647), -2147483648)); } else if (field.FieldType == typeof(long)) { long fa = (long)field.GetValue(a); long fb = (long)field.GetValue(b); field.SetValue(outObj, fa + fb); } else if (field.FieldType == typeof(float)) { float fa = (float)field.GetValue(a); float fb = (float)field.GetValue(b); field.SetValue(outObj, fa + fb); } } }
// PackStats creates a compacted serialized UnitStats struct (for networking) which can be later merged using MergePackedStats public byte[] PackStats(ModifiedFlags flags) { using (MemoryStream stream = new MemoryStream()) using (BinaryWriter bw = new BinaryWriter(stream)) { bw.Write((uint)flags); FieldInfo[] fields = typeof(UnitStats).GetFields(); foreach (FieldInfo field in fields) { ModFlags[] mf = (ModFlags[])field.GetCustomAttributes(typeof(ModFlags), false); if (mf.Length <= 0) { continue; } ModifiedFlags applyMF = 0; foreach (var singleMF in mf) { applyMF |= singleMF.Flags; } if ((applyMF & flags) == 0) { continue; } // we process byte, short, int, float and long. if (field.FieldType == typeof(byte)) { bw.Write((byte)field.GetValue(this)); } else if (field.FieldType == typeof(short)) { bw.Write((short)field.GetValue(this)); } else if (field.FieldType == typeof(int)) { bw.Write((int)field.GetValue(this)); } else if (field.FieldType == typeof(float)) { bw.Write((float)field.GetValue(this)); } else if (field.FieldType == typeof(long)) { bw.Write((long)field.GetValue(this)); } } return(stream.ToArray()); } }
// applies packed stats structure produced by PackStats public void MergePackedStats(byte[] packedStats) { using (MemoryStream stream = new MemoryStream(packedStats)) using (BinaryReader br = new BinaryReader(stream)) { ModifiedFlags flags = (ModifiedFlags)br.ReadUInt32(); FieldInfo[] fields = typeof(UnitStats).GetFields(); foreach (FieldInfo field in fields) { ModFlags[] mf = (ModFlags[])field.GetCustomAttributes(typeof(ModFlags), false); if (mf.Length <= 0) { continue; } ModifiedFlags applyMF = 0; foreach (var singleMF in mf) { applyMF |= singleMF.Flags; } if ((applyMF & flags) == 0) { continue; } // we process byte, short, int, float and long. if (field.FieldType == typeof(byte)) { field.SetValue(this, br.ReadByte()); } else if (field.FieldType == typeof(short)) { field.SetValue(this, br.ReadInt16()); } else if (field.FieldType == typeof(int)) { field.SetValue(this, br.ReadInt32()); } else if (field.FieldType == typeof(float)) { field.SetValue(this, br.ReadSingle()); } else if (field.FieldType == typeof(long)) { field.SetValue(this, br.ReadInt64()); } } } }
public ModFlags(ModifiedFlags flags) { Flags = flags; }