/src/tools/GameServer/TypeExtractor/D3TypeDescriptor/Overrides/NetAttributeKeyValue.cs

https://github.com/FeT/mooege
C# | 122 lines | 107 code | 15 blank | 0 comment | 10 complexity | 8227ec931c74e7ea5af5c86a6a41fe5c MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Xml.Linq;
  6. namespace D3TypeDescriptor
  7. {
  8. class NetAttributeKeyValue : StructureTypeDescriptor
  9. {
  10. [TypeOverrideIgnore]
  11. class AttributeFieldType : BasicTypeDescriptor
  12. {
  13. public AttributeFieldType()
  14. {
  15. Index = -1;
  16. Name = "GameAttribute";
  17. }
  18. public static AttributeFieldType Instance = new AttributeFieldType();
  19. public override void GenerateField(StringBuilder b, int pad, FieldDescriptor f)
  20. {
  21. b.Append(' ', pad); b.AppendLine("public GameAttribute " + f.GetFieldName() + ";");
  22. }
  23. public override void GenerateParseBitBuffer(StringBuilder b, int pad, FieldDescriptor f, string bitBufferName)
  24. {
  25. if (!f.HasMinMax || f.Min != 0)
  26. throw new NotImplementedException();
  27. b.Append(' ', pad); b.AppendLine(f.GetFieldName() + " = GameAttribute.Attributes[" + bitBufferName + ".ReadInt(" + f.EncodedBits + ")];");
  28. }
  29. public override void GenerateEncodeBitBuffer(StringBuilder b, int pad, FieldDescriptor f, string bitBufferName)
  30. {
  31. b.Append(' ', pad); b.AppendLine(bitBufferName + ".WriteInt(" + f.EncodedBits + ", " + f.GetFieldName() + ".Id);");
  32. }
  33. }
  34. public override void LoadFields(FieldDescriptor[] fields)
  35. {
  36. if (fields.Length != 3 ||
  37. fields[0].Type.Name != "DT_OPTIONAL" || fields[0].SubType.Name != "DT_INT" ||
  38. fields[1].Type.Name != "DT_INT" ||
  39. fields[2].Type != null)
  40. throw new Exception("Unexpected fields in NetAttributeKeyValue structure.");
  41. fields[1].Type = AttributeFieldType.Instance;
  42. base.LoadFields(fields);
  43. }
  44. public override void GenerateFields(StringBuilder b, int pad)
  45. {
  46. base.GenerateFields(b, pad);
  47. b.Append(' ', pad); b.AppendLine("public float Float; ");
  48. b.Append(' ', pad); b.AppendLine("public int Int; ");
  49. b.AppendLine();
  50. }
  51. public override void GenerateFieldsAndFunctions(StringBuilder b, int pad)
  52. {
  53. base.GenerateFieldsAndFunctions(b, pad);
  54. var fieldName = Fields[1].GetFieldName();
  55. b.IndentAppendLines(pad, @"public void ParseValue(GameBitBuffer buffer)
  56. {
  57. switch (" + fieldName + @".EncodingType)
  58. {
  59. case GameAttributeEncoding.Int:
  60. Int = buffer.ReadInt(" + fieldName + @".BitCount);
  61. break;
  62. case GameAttributeEncoding.IntMinMax:
  63. Int = buffer.ReadInt(" + fieldName + @".BitCount) + " + fieldName + @".Min;
  64. break;
  65. case GameAttributeEncoding.Float16:
  66. Float = buffer.ReadFloat16();
  67. break;
  68. case GameAttributeEncoding.Float16Or32:
  69. Float = buffer.ReadBool() ? buffer.ReadFloat16() : buffer.ReadFloat32();
  70. break;
  71. default:
  72. throw new Exception(""bad voodoo"");
  73. }
  74. }");
  75. b.AppendLine();
  76. b.IndentAppendLines(pad, @"public void EncodeValue(GameBitBuffer buffer)
  77. {
  78. switch (" + fieldName + @".EncodingType)
  79. {
  80. case GameAttributeEncoding.Int:
  81. buffer.WriteInt(" + fieldName + @".BitCount, Int);
  82. break;
  83. case GameAttributeEncoding.IntMinMax:
  84. buffer.WriteInt(" + fieldName + @".BitCount, Int - " + fieldName + @".Min);
  85. break;
  86. case GameAttributeEncoding.Float16:
  87. buffer.WriteFloat16(Float);
  88. break;
  89. case GameAttributeEncoding.Float16Or32:
  90. if (Float >= 65536.0f || -65536.0f >= Float)
  91. {
  92. buffer.WriteBool(false);
  93. buffer.WriteFloat32(Float);
  94. }
  95. else
  96. {
  97. buffer.WriteBool(true);
  98. buffer.WriteFloat16(Float);
  99. }
  100. break;
  101. default:
  102. throw new Exception(""bad voodoo"");
  103. }
  104. }");
  105. }
  106. }
  107. }