Guest User

Untitled

a guest
Dec 6th, 2011
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.78 KB | None | 0 0
  1. import struct
  2. from struct import unpack
  3. from time import time
  4.  
  5. class Tile(object):
  6. __slots__ = 'items', 'counts'
  7.  
  8. def __init__(self, items):
  9. self.items = items
  10. self.counts = len(items)
  11.  
  12. class Item(object):
  13. __slots__ = 'itemId', 'count', 'attributes'
  14.  
  15. def __init__(self, itemId, count=1, **attributes):
  16. self.itemId = itemId
  17. self.count = count
  18. self.attributes = attributes
  19.  
  20. # 16 x 32 x 32 x 4
  21. real = {}
  22. for z in xrange(16):
  23. real[z] = []
  24. for x in xrange(32):
  25. real[z].append([])
  26. for y in xrange(32):
  27. real[z][x].append([100, 102, 10, 10])
  28.  
  29. def generator(obj):
  30. code = ""
  31. for level in obj:
  32. code += "%s" % chr(level)
  33.  
  34. for x in obj[level]:
  35. for y in x:
  36. items = []
  37. for item in y:
  38. items.append(struct.pack("HBB", item, 1, 0))
  39.  
  40. code += ','.join(items)
  41. code +=";"
  42.  
  43. return code
  44. with open("w", "w+b") as f:
  45. f.write(generator(real))
  46.  
  47. code = open("w").read()
  48. print "Length: %d" % len(code)
  49.  
  50. itemCache = {}
  51.  
  52. def reader(code):
  53. w = {}
  54. pos = 0
  55. l = len(code)
  56.  
  57. # Avoid 1k calls to making the format :)
  58. #l_unpack = struct.Struct("HBB").unpack
  59. l_unpack = struct.unpack
  60.  
  61. # Bind them locally, this is suppose to make a small speedup as well, local things can be more optimized :)
  62. # Pypy gain nothing, but CPython does.
  63. l_Item = Item
  64. l_Tile = Tile
  65.  
  66. # Also attempt to local the itemCache, pypy doesn't like this tho.
  67. l_itemCache = itemCache
  68.  
  69. while True:
  70. level = ord(code[pos])
  71. pos += 1
  72. xlevel = []
  73. l_xlevel_append = xlevel.append
  74.  
  75. for xr in xrange(32):
  76. ywork = []
  77. l_ywork_append = ywork.append
  78.  
  79. for yr in xrange(32):
  80. items = []
  81. l_items_append = items.append
  82.  
  83. while True:
  84. itemId, count, attrNr = l_unpack("HBB", code[pos:pos+4])
  85. #itemId, count, attrNr = l_unpack(code[pos:pos+4])
  86.  
  87. if attrNr:
  88. pos += 4
  89. attr = {}
  90. for n in xrange(attrNr):
  91. name=''
  92. value=''
  93. data=code[pos]
  94. while data != "\xff":
  95. name += data
  96. pos += 1
  97. data = code[pos]
  98. pos += 1
  99.  
  100. data=code[pos]
  101. while data != "\xff":
  102. value += data
  103. pos += 1
  104. data = code[pos]
  105. pos +=2
  106. attr[name] = value
  107.  
  108. l_items_append(l_Item(itemId, count, **attr))
  109. else:
  110. pos += 5
  111. try:
  112. l_items_append(l_itemCache[itemId])
  113. except KeyError:
  114. i = l_itemCache[itemId] = l_Item(itemId, count)
  115. l_items_append(i)
  116.  
  117.  
  118.  
  119. if code[pos-1] == ';': break
  120. l_ywork_append(l_Tile(items))
  121. l_xlevel_append(ywork)
  122.  
  123. w[level] = xlevel
  124. if pos >= l:
  125. return w
  126.  
  127. t = time()
  128. for x in xrange(100):
  129. reader(code)
  130.  
  131. print("Struct: %f" % (time() - t))
  132.  
Advertisement
Add Comment
Please, Sign In to add comment