源战役客户端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

463 line
16 KiB

  1. -- --
  2. -- --------------------------------------------------------------------------------
  3. -- -- FILE: encoder.lua
  4. -- -- DESCRIPTION: protoc-gen-lua
  5. -- -- Google's Protocol Buffers project, ported to lua.
  6. -- -- https://code.google.com/p/protoc-gen-lua/
  7. -- --
  8. -- -- Copyright (c) 2010 , 林卓毅 (Zhuoyi Lin) netsnail@gmail.com
  9. -- -- All rights reserved.
  10. -- --
  11. -- -- Use, modification and distribution are subject to the "New BSD License"
  12. -- -- as listed at <url: http://www.opensource.org/licenses/bsd-license.php >.
  13. -- --
  14. -- -- COMPANY: NetEase
  15. -- -- CREATED: 2010年07月29日 19时30分46秒 CST
  16. -- --------------------------------------------------------------------------------
  17. -- --
  18. -- local string = string
  19. -- local table = table
  20. -- local ipairs = ipairs
  21. -- local assert =assert
  22. -- local pb = require "pb"
  23. -- local wire_format = require "protobuf.wire_format"
  24. -- module "protobuf.encoder"
  25. -- function _VarintSize(value)
  26. -- if value <= 0x7f then return 1 end
  27. -- if value <= 0x3fff then return 2 end
  28. -- if value <= 0x1fffff then return 3 end
  29. -- if value <= 0xfffffff then return 4 end
  30. -- return 5
  31. -- end
  32. -- function _SignedVarintSize(value)
  33. -- if value < 0 then return 10 end
  34. -- if value <= 0x7f then return 1 end
  35. -- if value <= 0x3fff then return 2 end
  36. -- if value <= 0x1fffff then return 3 end
  37. -- if value <= 0xfffffff then return 4 end
  38. -- return 5
  39. -- end
  40. -- function _TagSize(field_number)
  41. -- return _VarintSize(wire_format.PackTag(field_number, 0))
  42. -- end
  43. -- function _SimpleSizer(compute_value_size)
  44. -- return function(field_number, is_repeated, is_packed)
  45. -- local tag_size = _TagSize(field_number)
  46. -- if is_packed then
  47. -- local VarintSize = _VarintSize
  48. -- return function(value)
  49. -- local result = 0
  50. -- for _, element in ipairs(value) do
  51. -- result = result + compute_value_size(element)
  52. -- end
  53. -- return result + VarintSize(result) + tag_size
  54. -- end
  55. -- elseif is_repeated then
  56. -- return function(value)
  57. -- local result = tag_size * #value
  58. -- for _, element in ipairs(value) do
  59. -- result = result + compute_value_size(element)
  60. -- end
  61. -- return result
  62. -- end
  63. -- else
  64. -- return function (value)
  65. -- return tag_size + compute_value_size(value)
  66. -- end
  67. -- end
  68. -- end
  69. -- end
  70. -- function _ModifiedSizer(compute_value_size, modify_value)
  71. -- return function (field_number, is_repeated, is_packed)
  72. -- local tag_size = _TagSize(field_number)
  73. -- if is_packed then
  74. -- local VarintSize = _VarintSize
  75. -- return function (value)
  76. -- local result = 0
  77. -- for _, element in ipairs(value) do
  78. -- result = result + compute_value_size(modify_value(element))
  79. -- end
  80. -- return result + VarintSize(result) + tag_size
  81. -- end
  82. -- elseif is_repeated then
  83. -- return function (value)
  84. -- local result = tag_size * #value
  85. -- for _, element in ipairs(value) do
  86. -- result = result + compute_value_size(modify_value(element))
  87. -- end
  88. -- return result
  89. -- end
  90. -- else
  91. -- return function (value)
  92. -- return tag_size + compute_value_size(modify_value(value))
  93. -- end
  94. -- end
  95. -- end
  96. -- end
  97. -- function _FixedSizer(value_size)
  98. -- return function (field_number, is_repeated, is_packed)
  99. -- local tag_size = _TagSize(field_number)
  100. -- if is_packed then
  101. -- local VarintSize = _VarintSize
  102. -- return function (value)
  103. -- local result = #value * value_size
  104. -- return result + VarintSize(result) + tag_size
  105. -- end
  106. -- elseif is_repeated then
  107. -- local element_size = value_size + tag_size
  108. -- return function(value)
  109. -- return #value * element_size
  110. -- end
  111. -- else
  112. -- local field_size = value_size + tag_size
  113. -- return function (value)
  114. -- return field_size
  115. -- end
  116. -- end
  117. -- end
  118. -- end
  119. -- Int32Sizer = _SimpleSizer(_SignedVarintSize)
  120. -- Int64Sizer = Int32Sizer
  121. -- EnumSizer = Int32Sizer
  122. -- UInt32Sizer = _SimpleSizer(_VarintSize)
  123. -- UInt64Sizer = UInt32Sizer
  124. -- SInt32Sizer = _ModifiedSizer(_SignedVarintSize, wire_format.ZigZagEncode)
  125. -- SInt64Sizer = SInt32Sizer
  126. -- Fixed32Sizer = _FixedSizer(4)
  127. -- SFixed32Sizer = Fixed32Sizer
  128. -- FloatSizer = Fixed32Sizer
  129. -- Fixed64Sizer = _FixedSizer(8)
  130. -- SFixed64Sizer = Fixed64Sizer
  131. -- DoubleSizer = Fixed64Sizer
  132. -- BoolSizer = _FixedSizer(1)
  133. -- function StringSizer(field_number, is_repeated, is_packed)
  134. -- local tag_size = _TagSize(field_number)
  135. -- local VarintSize = _VarintSize
  136. -- assert(not is_packed)
  137. -- if is_repeated then
  138. -- return function(value)
  139. -- local result = tag_size * #value
  140. -- for _, element in ipairs(value) do
  141. -- local l = #element
  142. -- result = result + VarintSize(l) + l
  143. -- end
  144. -- return result
  145. -- end
  146. -- else
  147. -- return function(value)
  148. -- local l = #value
  149. -- return tag_size + VarintSize(l) + l
  150. -- end
  151. -- end
  152. -- end
  153. -- function BytesSizer(field_number, is_repeated, is_packed)
  154. -- local tag_size = _TagSize(field_number)
  155. -- local VarintSize = _VarintSize
  156. -- assert(not is_packed)
  157. -- if is_repeated then
  158. -- return function (value)
  159. -- local result = tag_size * #value
  160. -- for _,element in ipairs(value) do
  161. -- local l = #element
  162. -- result = result + VarintSize(l) + l
  163. -- end
  164. -- return result
  165. -- end
  166. -- else
  167. -- return function (value)
  168. -- local l = #value
  169. -- return tag_size + VarintSize(l) + l
  170. -- end
  171. -- end
  172. -- end
  173. -- function MessageSizer(field_number, is_repeated, is_packed)
  174. -- local tag_size = _TagSize(field_number)
  175. -- local VarintSize = _VarintSize
  176. -- assert(not is_packed)
  177. -- if is_repeated then
  178. -- return function(value)
  179. -- local result = tag_size * #value
  180. -- for _,element in ipairs(value) do
  181. -- local l = element:ByteSize()
  182. -- result = result + VarintSize(l) + l
  183. -- end
  184. -- return result
  185. -- end
  186. -- else
  187. -- return function (value)
  188. -- local l = value:ByteSize()
  189. -- return tag_size + VarintSize(l) + l
  190. -- end
  191. -- end
  192. -- end
  193. -- -- ====================================================================
  194. -- -- Encoders!
  195. -- local _EncodeVarint = pb.varint_encoder
  196. -- local _EncodeSignedVarint = pb.signed_varint_encoder
  197. -- local _EncodeVarint64 = pb.varint_encoder64
  198. -- local _EncodeSignedVarint64 = pb.signed_varint_encoder64
  199. -- function _VarintBytes(value)
  200. -- local out = {}
  201. -- local write = function(value)
  202. -- out[#out + 1 ] = value
  203. -- end
  204. -- _EncodeSignedVarint(write, value)
  205. -- return table.concat(out)
  206. -- end
  207. -- function TagBytes(field_number, wire_type)
  208. -- return _VarintBytes(wire_format.PackTag(field_number, wire_type))
  209. -- end
  210. -- function _SimpleEncoder(wire_type, encode_value, compute_value_size)
  211. -- return function(field_number, is_repeated, is_packed)
  212. -- if is_packed then
  213. -- local tag_bytes = TagBytes(field_number, wire_format.WIRETYPE_LENGTH_DELIMITED)
  214. -- local EncodeVarint = _EncodeVarint
  215. -- return function(write, value)
  216. -- write(tag_bytes)
  217. -- local size = 0
  218. -- for _, element in ipairs(value) do
  219. -- size = size + compute_value_size(element)
  220. -- end
  221. -- EncodeVarint(write, size)
  222. -- for element in value do
  223. -- encode_value(write, element)
  224. -- end
  225. -- end
  226. -- elseif is_repeated then
  227. -- local tag_bytes = TagBytes(field_number, wire_type)
  228. -- return function(write, value)
  229. -- for _, element in ipairs(value) do
  230. -- write(tag_bytes)
  231. -- encode_value(write, element)
  232. -- end
  233. -- end
  234. -- else
  235. -- local tag_bytes = TagBytes(field_number, wire_type)
  236. -- return function(write, value)
  237. -- write(tag_bytes)
  238. -- encode_value(write, value)
  239. -- end
  240. -- end
  241. -- end
  242. -- end
  243. -- function _ModifiedEncoder(wire_type, encode_value, compute_value_size, modify_value)
  244. -- return function (field_number, is_repeated, is_packed)
  245. -- if is_packed then
  246. -- local tag_bytes = TagBytes(field_number, wire_format.WIRETYPE_LENGTH_DELIMITED)
  247. -- local EncodeVarint = _EncodeVarint
  248. -- return function (write, value)
  249. -- write(tag_bytes)
  250. -- local size = 0
  251. -- for _, element in ipairs(value) do
  252. -- size = size + compute_value_size(modify_value(element))
  253. -- end
  254. -- EncodeVarint(write, size)
  255. -- for _, element in ipairs(value) do
  256. -- encode_value(write, modify_value(element))
  257. -- end
  258. -- end
  259. -- elseif is_repeated then
  260. -- local tag_bytes = TagBytes(field_number, wire_type)
  261. -- return function (write, value)
  262. -- for _, element in ipairs(value) do
  263. -- write(tag_bytes)
  264. -- encode_value(write, modify_value(element))
  265. -- end
  266. -- end
  267. -- else
  268. -- local tag_bytes = TagBytes(field_number, wire_type)
  269. -- return function (write, value)
  270. -- write(tag_bytes)
  271. -- encode_value(write, modify_value(value))
  272. -- end
  273. -- end
  274. -- end
  275. -- end
  276. -- function _StructPackEncoder(wire_type, value_size, format)
  277. -- return function(field_number, is_repeated, is_packed)
  278. -- local struct_pack = pb.struct_pack
  279. -- if is_packed then
  280. -- local tag_bytes = TagBytes(field_number, wire_format.WIRETYPE_LENGTH_DELIMITED)
  281. -- local EncodeVarint = _EncodeVarint
  282. -- return function (write, value)
  283. -- write(tag_bytes)
  284. -- EncodeVarint(write, #value * value_size)
  285. -- for _, element in ipairs(value) do
  286. -- struct_pack(write, format, element)
  287. -- end
  288. -- end
  289. -- elseif is_repeated then
  290. -- local tag_bytes = TagBytes(field_number, wire_type)
  291. -- return function (write, value)
  292. -- for _, element in ipairs(value) do
  293. -- write(tag_bytes)
  294. -- struct_pack(write, format, element)
  295. -- end
  296. -- end
  297. -- else
  298. -- local tag_bytes = TagBytes(field_number, wire_type)
  299. -- return function (write, value)
  300. -- write(tag_bytes)
  301. -- struct_pack(write, format, value)
  302. -- end
  303. -- end
  304. -- end
  305. -- end
  306. -- Int32Encoder = _SimpleEncoder(wire_format.WIRETYPE_VARINT, _EncodeSignedVarint, _SignedVarintSize)
  307. -- Int64Encoder = _SimpleEncoder(wire_format.WIRETYPE_VARINT, _EncodeSignedVarint64, _SignedVarintSize)
  308. -- EnumEncoder = Int32Encoder
  309. -- UInt32Encoder = _SimpleEncoder(wire_format.WIRETYPE_VARINT, _EncodeVarint, _VarintSize)
  310. -- UInt64Encoder = _SimpleEncoder(wire_format.WIRETYPE_VARINT, _EncodeVarint64, _VarintSize)
  311. -- SInt32Encoder = _ModifiedEncoder(
  312. -- wire_format.WIRETYPE_VARINT, _EncodeVarint, _VarintSize,
  313. -- wire_format.ZigZagEncode32)
  314. -- SInt64Encoder = _ModifiedEncoder(
  315. -- wire_format.WIRETYPE_VARINT, _EncodeVarint64, _VarintSize,
  316. -- wire_format.ZigZagEncode64)
  317. -- Fixed32Encoder = _StructPackEncoder(wire_format.WIRETYPE_FIXED32, 4, string.byte('I'))
  318. -- Fixed64Encoder = _StructPackEncoder(wire_format.WIRETYPE_FIXED64, 8, string.byte('Q'))
  319. -- SFixed32Encoder = _StructPackEncoder(wire_format.WIRETYPE_FIXED32, 4, string.byte('i'))
  320. -- SFixed64Encoder = _StructPackEncoder(wire_format.WIRETYPE_FIXED64, 8, string.byte('q'))
  321. -- FloatEncoder = _StructPackEncoder(wire_format.WIRETYPE_FIXED32, 4, string.byte('f'))
  322. -- DoubleEncoder = _StructPackEncoder(wire_format.WIRETYPE_FIXED64, 8, string.byte('d'))
  323. -- function BoolEncoder(field_number, is_repeated, is_packed)
  324. -- local false_byte = '\0'
  325. -- local true_byte = '\1'
  326. -- if is_packed then
  327. -- local tag_bytes = TagBytes(field_number, wire_format.WIRETYPE_LENGTH_DELIMITED)
  328. -- local EncodeVarint = _EncodeVarint
  329. -- return function (write, value)
  330. -- write(tag_bytes)
  331. -- EncodeVarint(write, #value)
  332. -- for _, element in ipairs(value) do
  333. -- if element then
  334. -- write(true_byte)
  335. -- else
  336. -- write(false_byte)
  337. -- end
  338. -- end
  339. -- end
  340. -- elseif is_repeated then
  341. -- local tag_bytes = TagBytes(field_number, wire_format.WIRETYPE_VARINT)
  342. -- return function(write, value)
  343. -- for _, element in ipairs(value) do
  344. -- write(tag_bytes)
  345. -- if element then
  346. -- write(true_byte)
  347. -- else
  348. -- write(false_byte)
  349. -- end
  350. -- end
  351. -- end
  352. -- else
  353. -- local tag_bytes = TagBytes(field_number, wire_format.WIRETYPE_VARINT)
  354. -- return function (write, value)
  355. -- write(tag_bytes)
  356. -- if value then
  357. -- return write(true_byte)
  358. -- end
  359. -- return write(false_byte)
  360. -- end
  361. -- end
  362. -- end
  363. -- function StringEncoder(field_number, is_repeated, is_packed)
  364. -- local tag = TagBytes(field_number, wire_format.WIRETYPE_LENGTH_DELIMITED)
  365. -- local EncodeVarint = _EncodeVarint
  366. -- assert(not is_packed)
  367. -- if is_repeated then
  368. -- return function (write, value)
  369. -- for _, element in ipairs(value) do
  370. -- -- encoded = element.encode('utf-8')
  371. -- write(tag)
  372. -- EncodeVarint(write, #element)
  373. -- write(element)
  374. -- end
  375. -- end
  376. -- else
  377. -- return function (write, value)
  378. -- -- local encoded = value.encode('utf-8')
  379. -- write(tag)
  380. -- EncodeVarint(write, #value)
  381. -- return write(value)
  382. -- end
  383. -- end
  384. -- end
  385. -- function BytesEncoder(field_number, is_repeated, is_packed)
  386. -- local tag = TagBytes(field_number, wire_format.WIRETYPE_LENGTH_DELIMITED)
  387. -- local EncodeVarint = _EncodeVarint
  388. -- assert(not is_packed)
  389. -- if is_repeated then
  390. -- return function (write, value)
  391. -- for _, element in ipairs(value) do
  392. -- write(tag)
  393. -- EncodeVarint(write, #element)
  394. -- write(element)
  395. -- end
  396. -- end
  397. -- else
  398. -- return function(write, value)
  399. -- write(tag)
  400. -- EncodeVarint(write, #value)
  401. -- return write(value)
  402. -- end
  403. -- end
  404. -- end
  405. -- function MessageEncoder(field_number, is_repeated, is_packed)
  406. -- local tag = TagBytes(field_number, wire_format.WIRETYPE_LENGTH_DELIMITED)
  407. -- local EncodeVarint = _EncodeVarint
  408. -- assert(not is_packed)
  409. -- if is_repeated then
  410. -- return function(write, value)
  411. -- for _, element in ipairs(value) do
  412. -- write(tag)
  413. -- EncodeVarint(write, element:ByteSize())
  414. -- element:_InternalSerialize(write)
  415. -- end
  416. -- end
  417. -- else
  418. -- return function (write, value)
  419. -- write(tag)
  420. -- EncodeVarint(write, value:ByteSize())
  421. -- return value:_InternalSerialize(write)
  422. -- end
  423. -- end
  424. -- end