|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Text; |
| 5 | +using System.Threading.Tasks; |
| 6 | +using System.IO; |
| 7 | +using System.Xml.Linq; |
| 8 | +using ESPSharp.Enums; |
| 9 | +using ESPSharp.Enums.Flags; |
| 10 | +using ESPSharp.Interfaces; |
| 11 | +using ESPSharp.Subrecords; |
| 12 | +using ESPSharp.SubrecordCollections; |
| 13 | +using ESPSharp.DataTypes; |
| 14 | + |
| 15 | +namespace ESPSharp.DataTypes |
| 16 | +{ |
| 17 | + public class Comparison : IESPSerializable, ICloneable<Comparison>, IReferenceContainer |
| 18 | + { |
| 19 | + public ConditionFlags Flags { get; set; } |
| 20 | + public ConditionComparisonType Operator { get; set; } |
| 21 | + public byte[] Unused { get; set; } |
| 22 | + public dynamic ComparisonValue { get; set; } |
| 23 | + |
| 24 | + public Comparison() |
| 25 | + { |
| 26 | + Flags = new ConditionFlags(); |
| 27 | + Operator = new ConditionComparisonType(); |
| 28 | + Unused = new byte[3]; |
| 29 | + ComparisonValue = 0; |
| 30 | + } |
| 31 | + |
| 32 | + public Comparison(ConditionFlags Flags, ConditionComparisonType Operator, byte[] Unused, FormID ComparisonValue) |
| 33 | + { |
| 34 | + this.Flags = Flags; |
| 35 | + this.Operator = Operator; |
| 36 | + this.Unused = Unused; |
| 37 | + this.ComparisonValue = ComparisonValue.Clone(); |
| 38 | + } |
| 39 | + |
| 40 | + public Comparison(ConditionFlags Flags, ConditionComparisonType Operator, byte[] Unused, float ComparisonValue) |
| 41 | + { |
| 42 | + this.Flags = Flags; |
| 43 | + this.Operator = Operator; |
| 44 | + this.Unused = Unused; |
| 45 | + this.ComparisonValue = ComparisonValue; |
| 46 | + } |
| 47 | + |
| 48 | + public Comparison(Comparison copyObject) |
| 49 | + { |
| 50 | + Flags = copyObject.Flags; |
| 51 | + Operator = copyObject.Operator; |
| 52 | + Unused = (byte[])copyObject.Unused.Clone(); |
| 53 | + if (copyObject.ComparisonValue is FormID) |
| 54 | + ComparisonValue = copyObject.ComparisonValue.Clone(); |
| 55 | + else |
| 56 | + ComparisonValue = copyObject.ComparisonValue; |
| 57 | + } |
| 58 | + |
| 59 | + public void ReadBinary(ESPReader reader) |
| 60 | + { |
| 61 | + byte temp = reader.ReadByte(); |
| 62 | + Flags = (ConditionFlags)(temp & (byte)0x0F); |
| 63 | + Operator = (ConditionComparisonType)(temp & (byte)0xF0); |
| 64 | + Unused = reader.ReadBytes(3); |
| 65 | + if (Flags.HasFlag(ConditionFlags.UseGlobal)) |
| 66 | + ComparisonValue = new FormID(reader.ReadUInt32()); |
| 67 | + else |
| 68 | + ComparisonValue = reader.ReadSingle(); |
| 69 | + } |
| 70 | + |
| 71 | + public void WriteBinary(ESPWriter writer) |
| 72 | + { |
| 73 | + writer.Write((byte)((byte)Flags | (byte)Operator)); |
| 74 | + writer.Write(Unused); |
| 75 | + if (Flags.HasFlag(ConditionFlags.UseGlobal)) |
| 76 | + ComparisonValue.WriteBinary(writer); |
| 77 | + else |
| 78 | + writer.Write((Single)ComparisonValue); |
| 79 | + } |
| 80 | + |
| 81 | + public void WriteXML(XElement ele) |
| 82 | + { |
| 83 | + XElement subEle; |
| 84 | + |
| 85 | + ele.TryPathTo("Flags", true, out subEle); |
| 86 | + subEle.Value = Flags.ToString(); |
| 87 | + |
| 88 | + ele.TryPathTo("Operator", true, out subEle); |
| 89 | + subEle.Value = Operator.ToString(); |
| 90 | + |
| 91 | + ele.TryPathTo("Unused", true, out subEle); |
| 92 | + subEle.Value = Unused.ToHex(); |
| 93 | + |
| 94 | + ele.TryPathTo("ComparisonValue", true, out subEle); |
| 95 | + if (Flags.HasFlag(ConditionFlags.UseGlobal)) |
| 96 | + ComparisonValue.WriteXML(subEle); |
| 97 | + else |
| 98 | + subEle.Value = ComparisonValue.ToString(); |
| 99 | + } |
| 100 | + |
| 101 | + public void ReadXML(XElement ele) |
| 102 | + { |
| 103 | + XElement subEle; |
| 104 | + |
| 105 | + if (ele.TryPathTo("Flags", false, out subEle)) |
| 106 | + { |
| 107 | + Flags = subEle.ToEnum<ConditionFlags>(); |
| 108 | + } |
| 109 | + |
| 110 | + if (ele.TryPathTo("Operator", false, out subEle)) |
| 111 | + { |
| 112 | + Operator = subEle.ToEnum<ConditionComparisonType>(); |
| 113 | + } |
| 114 | + |
| 115 | + if (ele.TryPathTo("Unused", false, out subEle)) |
| 116 | + { |
| 117 | + Unused = subEle.ToBytes(); |
| 118 | + } |
| 119 | + |
| 120 | + if (ele.TryPathTo("ComparisonValue", false, out subEle)) |
| 121 | + { |
| 122 | + if (Flags.HasFlag(ConditionFlags.UseGlobal)) |
| 123 | + { |
| 124 | + ComparisonValue = new FormID(); |
| 125 | + ComparisonValue.ReadXML(subEle); |
| 126 | + } |
| 127 | + else |
| 128 | + ComparisonValue = subEle.ToSingle(); |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + public Comparison Clone() |
| 133 | + { |
| 134 | + return new Comparison(this); |
| 135 | + } |
| 136 | + } |
| 137 | +} |
0 commit comments