schema.sbn 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. local setmetatable = setmetatable
  2. local pairs = pairs
  3. local ipairs = ipairs
  4. local tinsert = table.insert
  5. local function SimpleClass()
  6. local class = {}
  7. class.__index = class
  8. class.New = function(...)
  9. local ctor = class.ctor
  10. local o = ctor and ctor(...) or {}
  11. setmetatable(o, class)
  12. return o
  13. end
  14. return class
  15. end
  16. local function get_map_size(m)
  17. local n = 0
  18. for _ in pairs(m) do
  19. n = n + 1
  20. end
  21. return n
  22. end
  23. local enums =
  24. {
  25. {{~ for c in __enums ~}}
  26. ---@class {{c.full_name}} {{-if c.comment != ''}} @{{escape_comment c.comment}}{{end}}
  27. {{~ for item in c.items ~}}
  28. ---@field public {{item.name}} integer {{-if item.comment != ''}} @{{escape_comment item.comment}}{{end}}
  29. {{~end~}}
  30. ['{{c.full_name}}'] = { {{ for item in c.items }} {{item.name}}={{item.int_value}}, {{end}} };
  31. {{~end~}}
  32. }
  33. local tables =
  34. {
  35. {{~for table in __tables ~}}
  36. {{~if table.is_map_table ~}}
  37. { name='{{table.name}}', file='{{table.output_data_file}}', mode='map', index='{{table.index}}', value_type='{{table.value_ttype.def_bean.full_name}}' },
  38. {{~else if table.is_list_table ~}}
  39. { name='{{table.name}}', file='{{table.output_data_file}}', mode='list', index='{{table.index}}', value_type='{{table.value_ttype.def_bean.full_name}}' },
  40. {{~else~}}
  41. { name='{{table.name}}', file='{{table.output_data_file}}', mode='one', value_type='{{table.value_ttype.def_bean.full_name}}'},
  42. {{~end~}}
  43. {{~end~}}
  44. }
  45. local function InitTypes(methods)
  46. local readBool = methods.readBool
  47. local readByte = methods.readByte
  48. local readShort = methods.readShort
  49. local readFshort = methods.readFshort
  50. local readInt = methods.readInt
  51. local readFint = methods.readFint
  52. local readLong = methods.readLong
  53. local readFlong = methods.readFlong
  54. local readFloat = methods.readFloat
  55. local readDouble = methods.readDouble
  56. local readSize = methods.readSize
  57. local readString = methods.readString
  58. local function readList(bs, keyFun)
  59. local list = {}
  60. local v
  61. for i = 1, readSize(bs) do
  62. tinsert(list, keyFun(bs))
  63. end
  64. return list
  65. end
  66. local readArray = readList
  67. local function readSet(bs, keyFun)
  68. local set = {}
  69. local v
  70. for i = 1, readSize(bs) do
  71. tinsert(set, keyFun(bs))
  72. end
  73. return set
  74. end
  75. local function readMap(bs, keyFun, valueFun)
  76. local map = {}
  77. for i = 1, readSize(bs) do
  78. local k = keyFun(bs)
  79. local v = valueFun(bs)
  80. map[k] = v
  81. end
  82. return map
  83. end
  84. local function readNullableBool(bs)
  85. if readBool(bs) then
  86. return readBool(bs)
  87. end
  88. end
  89. local beans = {}
  90. {{~ for bean in __beans ~}}
  91. do
  92. ---@class {{bean.full_name}} {{if bean.parent_def_type}}:{{bean.parent}} {{end}} {{-if bean.comment != ''}} @{{escape_comment bean.comment}}{{end}}
  93. {{~ for field in bean.export_fields~}}
  94. ---@field public {{field.name}} {{comment_type field.ctype}} {{-if field.comment != ''}} @{{escape_comment field.comment}}{{end}}
  95. {{~end~}}
  96. local class = {
  97. {{~ for field in bean.export_fields~}}
  98. { name='{{field.name}}', type='{{comment_type field.ctype}}'},
  99. {{~end~}}
  100. }
  101. beans['{{bean.full_name}}'] = class
  102. end
  103. {{~end~}}
  104. local beans = {}
  105. {{~ for bean in __beans ~}}
  106. do
  107. ---@class {{bean.full_name}} {{if bean.parent_def_type}}:{{bean.parent}} {{end}}
  108. {{~ for field in bean.export_fields~}}
  109. ---@field public {{field.name}} {{comment_type field.ctype}}
  110. {{~end~}}
  111. local class = SimpleClass()
  112. class._id = {{bean.id}}
  113. class._type_ = '{{bean.full_name}}'
  114. local id2name = { {{for c in bean.hierarchy_not_abstract_children}} [{{c.id}}] = '{{c.full_name}}', {{end}} }
  115. {{~if bean.is_abstract_type~}}
  116. class._deserialize = function(bs)
  117. local id = readInt(bs)
  118. return beans[id2name[id]]._deserialize(bs)
  119. end
  120. {{~else~}}
  121. class._deserialize = function(bs)
  122. local o = {
  123. {{~ for field in bean.hierarchy_export_fields ~}}
  124. {{~if !(need_marshal_bool_prefix field.ctype)~}}
  125. {{field.name}} = {{deserialize 'bs' field.ctype}},
  126. {{~else~}}
  127. {{field.name}} = {{if !field.ctype.is_bool}}readBool(bs) and {{deserialize 'bs' field.ctype}} or nil {{-else-}} readNullableBool(bs) {{-end-}},
  128. {{~end~}}
  129. {{~end~}}
  130. }
  131. setmetatable(o, class)
  132. return o
  133. end
  134. {{~end~}}
  135. beans[class._type_] = class
  136. end
  137. {{~end~}}
  138. return { enums = enums, beans = beans, tables = tables }
  139. end
  140. return { InitTypes = InitTypes }