Skip to content

Commit

Permalink
l2/converter xml->py work with strings
Browse files Browse the repository at this point in the history
  • Loading branch information
yv84 committed Sep 20, 2014
1 parent 143e91c commit 7e8fce0
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 23 deletions.
83 changes: 63 additions & 20 deletions src/l2/converter/test/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
import random
import struct
import binascii

from itertools import chain
import string

import numpy

Expand All @@ -35,12 +36,25 @@ def xml_string_trim (xml_string):
return b''.join(re.findall(b"<.+?>", xml_string))

@staticmethod
def random_i(count):
def random_i4(count):
return (lambda count: [(lambda x: \
[struct.pack(b'i', x), x]) \
(int(0xffffffff*random.random())-2147483648) \
for i in range(count)])(count)

@staticmethod
def random_string(count):
MIN_STRING = 1
MAX_STRING = 16
return (lambda count: [(lambda x: \
[b''.join([x, '\x00'.encode('UTF-16LE')]), x[:-1] ]) \
(''.join([random.choice(string.ascii_letters+string.digits) \
for i in range(
int(random.random()*(MAX_STRING-MIN_STRING)+MIN_STRING))
]).encode('UTF-16LE')) \
for i in range(count)])(count)


def setUp(self):
self.ini_to_xml = IniToXml()
self.xml_to_py = XmlToPy()
Expand All @@ -53,7 +67,11 @@ def tearDown(self):
py_header = """import struct
class UTF():
unicode_string = lambda data: str(data[::2].find(b'\\x00')+1)*2
@staticmethod
def unicode_string(i, data):
while data and data[i:i+2] != b"\\x00\\x00":
i += 2
return str(i+1)
pck_client = {}
pck_server = {}
Expand All @@ -77,8 +95,8 @@ def testEasy1(self):
"""
py_string = """
class c_Logout(UTF):
@staticmethod
def dtype(act, data):
@classmethod
def dtype(self, data):
dtype = [('pck_type', 'i1')]
return dtype
Expand All @@ -99,9 +117,12 @@ def dtype(act, data):
exec(code, ns)
pack_value, unpack_value = [], []
[(pack_value.append(i[0]), unpack_value.append(i[1])) \
for i in [(b"\x00", 0)]]
for i in chain(
[(b"\x00", 0),],
)
]
py_execute = b"".join(pack_value)
dtype = ns['pck'].client[pack_value[0]].dtype(0, py_execute)
dtype = ns['pck'].client[pack_value[0]].dtype(py_execute)
pck_np_array = numpy.zeros(1,dtype)
pck_np_array[:] = py_execute
self.assertEqual(pck_np_array['pck_type'].item(),
Expand All @@ -125,8 +146,8 @@ def testMiddle1(self):
"""
py_string = """
class c_AttackRequest(UTF):
@staticmethod
def dtype(act, data):
@classmethod
def dtype(self, data):
dtype = [('pck_type', 'i1'), ('ObjectID', 'i4'), ('OrigX', 'i4'), ('OrigY', 'i4'), ('OrigZ', 'i4'), ('AttackClick', 'i1')]
return dtype
Expand All @@ -147,13 +168,14 @@ def dtype(act, data):
exec(code, ns)
pack_value, unpack_value = [], []
[(pack_value.append(i[0]), unpack_value.append(i[1])) \
for i in [(b"\x01", 1)]]
[(pack_value.append(i[0]), unpack_value.append(i[1])) \
for i in self.random_i(4)]
[(pack_value.append(i[0]), unpack_value.append(i[1])) \
for i in [(b"\x01", 1)]]
for i in chain(
[(b"\x01", 1),],
self.random_i4(4),
[(b"\x01", 1),],
)
]
py_execute = b"".join(pack_value)
dtype = ns['pck'].client[pack_value[0]].dtype(0, py_execute)
dtype = ns['pck'].client[pack_value[0]].dtype(py_execute)
pck_np_array = numpy.zeros(1,dtype)
pck_np_array[:] = py_execute
self.assertEqual(pck_np_array['pck_type'].item(),
Expand Down Expand Up @@ -184,9 +206,9 @@ def testMiddle2(self):
"""
py_string = """
class c_ReqStartPledgeWar(UTF):
@staticmethod
def dtype(act, data):
dtype = [('pck_type', 'i1'), ('PledgeName', '|S'+self.unicode_string(data))]
@classmethod
def dtype(self, data):
dtype = [('pck_type', 'i1'), ('PledgeName', '|S'+self.unicode_string(1, data))]
return dtype
pck_client[b'\\x03'] = c_ReqStartPledgeWar"""
Expand All @@ -201,6 +223,27 @@ def dtype(act, data):
self.xml_to_py.convert(xml_string),
py_string,
)
code = ''.join([self.py_header, py_string, self.py_footer])
code = compile(code, '<string>', 'exec')
ns = {}
exec(code, ns)
pack_value, unpack_value = [], []
[(pack_value.append(i[0]), unpack_value.append(i[1])) \
for i in chain(
[(b"\x03", 3),],
self.random_string(1),
)
]
py_execute = b"".join(pack_value)
dtype = ns['pck'].client[pack_value[0]].dtype(py_execute)
pck_np_array = numpy.zeros(1,dtype)
pck_np_array[:] = py_execute
self.assertEqual(pck_np_array['pck_type'].item(),
unpack_value[0])
self.assertEqual(pck_np_array['PledgeName'].item(),
unpack_value[1])



def testMiddle4(self):
ini_string = b"""
Expand Down Expand Up @@ -228,8 +271,8 @@ def testMiddle4(self):
)
py_string = """
class s_ExDominionWarStart(UTF):
@staticmethod
def dtype(act, data):
@classmethod
def dtype(self, data):
dtype = [('pck_type', 'i1'), ('subID', 'i2'), ('objID', 'i4'), ('1', 'i4'), ('terrID', 'i4'), ('isDisguised', 'i4'), ('isDisgTerrID', 'i4')]
return dtype
Expand Down
7 changes: 4 additions & 3 deletions src/l2/converter/xml_to_py.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@ def convert(self, xml_string):
root.getchildren()
pck_struct = root.getchildren()[0]
dtype = [('pck_type', 'i1'),]
i = 1
for primitive in pck_struct.iterchildren():
dtype.append((primitive.attrib['name'],
primitive.attrib['type']))

py_string = """
class {name}(UTF):
@staticmethod
def dtype(act, data):
@classmethod
def dtype(self, data):
dtype = {dtype}
return dtype
Expand All @@ -28,6 +29,6 @@ def dtype(act, data):
dtype=dtype,
b_type=binascii.unhexlify(pck_struct.attrib["type"]),
**pck_struct.attrib) \
.replace("'S')", "'|S'+self.unicode_string(data))")
.replace("'S')", "'|S'+self.unicode_string("+str(i)+", data))")

return py_string

0 comments on commit 7e8fce0

Please sign in to comment.