def _message_send_encode(self, descriptor, m_type): if m_type == "server": return f = self._file def write_field(field_name, field_type, *_): field_name = util.format_to_camel(field_name) if field_type == "string" and _[1]: f.write("%swriter.Write(wrapString(%s, %i));\n" % (TAB3, field_name, _[1])) elif field_type == "int[]": f.write("%swriter.Write((int)%s.Length);\n" % (TAB3, field_name)) f.write("%sfor (int i = 0; i < %s.Length; i++)\n" % (TAB3, field_name)) f.write("%swriter.Write(%s[i]);\n" % (TAB4, field_name)) f.write("\n") else: f.write("%swriter.Write(%s);\n" % (TAB3, field_name)) f.write("%spublic byte[] encode()\n" % TAB2) f.write("%s{\n" % TAB2) util.iterate_message_fields(descriptor, write_field) f.write("%sreturn wrapCommand();\n" % TAB3) f.write("%s}\n" % TAB2)
def _message_send_encode(self, descriptor, m_type): if m_type == 'server': return f = self._file def write_field(field_name, field_type, *_): field_name = util.format_to_camel(field_name) if field_type == 'string' and _[1]: f.write('%swriter.Write(wrapString(%s, %i));\n' % (TAB3, field_name, _[1])) elif field_type == 'int[]': f.write('%swriter.Write((int)%s.Length);\n' % (TAB3, field_name)) f.write('%sfor (int i = 0; i < %s.Length; i++)\n' % (TAB3, field_name)) f.write('%swriter.Write(%s[i]);\n' % (TAB4, field_name)) f.write('\n') else: f.write('%swriter.Write(%s);\n' % ( TAB3, field_name, )) f.write('%spublic byte[] encode()\n' % TAB2) f.write('%s{\n' % TAB2) util.iterate_message_fields(descriptor, write_field) f.write('%sreturn wrapCommand();\n' % TAB3) f.write('%s}\n' % TAB2)
def _message_send_constructor(self, descriptor, m_type): if m_type == "server": return f = self._file if len(descriptor) <= 2: return def write_constructor_args(field_name, field_type, last, *_): field_name = util.format_to_camel(field_name) spacing = " " if last else ", " f.write("%s %s%s" % (field_type, field_name, spacing)) def write_fields_init(field_name, *_): field_name = util.format_to_camel(field_name) f.write("%sthis.%s = %s;\n" % (TAB3, field_name, field_name)) f.write("%spublic %s( " % (TAB2, util.format_to_pascal(descriptor[0]))) util.iterate_message_fields(descriptor, write_constructor_args) f.write(")\n") f.write("%s{\n" % TAB2) f.write("%s%s();\n" % (TAB3, self._send_stream_creator_name)) util.iterate_message_fields(descriptor, write_fields_init) f.write("%s}\n" % TAB2)
def _message_send_constructor(self, descriptor, m_type): if m_type == 'server': return f = self._file if len(descriptor) <= 2: return def write_constructor_args(field_name, field_type, last, *_): field_name = util.format_to_camel(field_name) spacing = ' ' if last else ', ' f.write('%s %s%s' % (field_type, field_name, spacing)) def write_fields_init(field_name, *_): field_name = util.format_to_camel(field_name) f.write('%sthis.%s = %s;\n' % (TAB3, field_name, field_name)) f.write('%spublic %s( ' % (TAB2, util.format_to_pascal(descriptor[0]))) util.iterate_message_fields(descriptor, write_constructor_args) f.write(')\n') f.write('%s{\n' % TAB2) f.write('%s%s();\n' % ( TAB3, self._send_stream_creator_name, )) util.iterate_message_fields(descriptor, write_fields_init) f.write('%s}\n' % TAB2)
def _message_class_header(self, descriptor, message_id): f = self._file def write_field_definition(field_name, field_type, *_): field_name = util.format_to_camel(field_name) field_type = util.format_to_pascal( field_type) if field_type in self._custom_enums else field_type f.write('%spublic readonly %s %s;\n' % ( TAB2, field_type, field_name, )) cls_name = util.format_to_pascal(descriptor[0]) f.write('%spublic class %s : %s\n' % ( TAB, cls_name, self._base_class_name, )) f.write('%s{\n' % TAB) f.write('%spublic static byte ID { get { return %i; } }\n' % ( TAB2, message_id, )) f.write('%spublic override byte %s() { return %i; }\n' % ( TAB2, self._get_id_method_name, message_id, )) if len(descriptor) <= 2: return util.iterate_message_fields(descriptor, write_field_definition) f.write('%s\n' % TAB)
def _message_receive_constructor(self, descriptor, m_type): if m_type == 'client': return f = self._file def reader_method(field_type): if field_type == 'byte' or field_type in self._custom_enums: return 'ReadByte' if field_type == 'short': return 'ReadInt16' if field_type == 'int': return 'ReadInt32' if field_type == 'string': return 'ReadString' if field_type in ['string[]', 'byte[]', 'short[]']: return None assert False def read_from_byte_array(field_name, field_type, *_): field_name = util.format_to_camel(field_name) if field_type == 'string' and _[1] == 'long': f.write('%svar len = reader.ReadInt16();\n' % (TAB3, )) f.write('%schar[] strRaw = reader.ReadChars(len);\n' % (TAB3, )) f.write('%s%s = new string(strRaw);\n' % (TAB3, field_name)) else: method = reader_method(field_type) if field_type in self._custom_enums: f.write('%s%s = (%s)reader.%s();\n' % ( TAB3, field_name, util.format_to_pascal(field_type), method, )) elif method is not None: f.write('%s%s = reader.%s();\n' % ( TAB3, field_name, method, )) cls_name = util.format_to_pascal(descriptor[0]) f.write('%spublic %s(byte[] source)\n' % (TAB2, cls_name)) f.write('%s{\n' % TAB2) f.write('%s%s(source);\n' % ( TAB3, self._receive_stream_creator_name, )) util.iterate_message_fields(descriptor, read_from_byte_array) f.write('%s}\n' % TAB2)
def _message_class_header(self, descriptor, message_id): f = self._file def write_field_definition(field_name, field_type, *_): field_name = util.format_to_camel(field_name) field_type = util.format_to_pascal(field_type) if field_type in self._custom_enums else field_type f.write("%spublic readonly %s %s;\n" % (TAB2, field_type, field_name)) cls_name = util.format_to_pascal(descriptor[0]) f.write("%spublic class %s : %s\n" % (TAB, cls_name, self._base_class_name)) f.write("%s{\n" % TAB) f.write("%spublic static byte ID { get { return %i; } }\n" % (TAB2, message_id)) f.write("%spublic override byte %s() { return %i; }\n" % (TAB2, self._get_id_method_name, message_id)) if len(descriptor) <= 2: return util.iterate_message_fields(descriptor, write_field_definition) f.write("%s\n" % TAB)
def _message_receive_constructor(self, descriptor, m_type): if m_type == "client": return f = self._file def reader_method(field_type): if field_type == "byte" or field_type in self._custom_enums: return "ReadByte" if field_type == "short": return "ReadInt16" if field_type == "int": return "ReadInt32" if field_type == "string": return "ReadString" if field_type in ["string[]", "byte[]", "short[]"]: return None assert False def read_from_byte_array(field_name, field_type, *_): field_name = util.format_to_camel(field_name) if field_type == "string" and _[1] == "long": f.write("%svar len = reader.ReadInt16();\n" % (TAB3,)) f.write("%schar[] strRaw = reader.ReadChars(len);\n" % (TAB3,)) f.write("%s%s = new string(strRaw);\n" % (TAB3, field_name)) else: method = reader_method(field_type) if field_type in self._custom_enums: f.write("%s%s = (%s)reader.%s();\n" % (TAB3, field_name, util.format_to_pascal(field_type), method)) elif method is not None: f.write("%s%s = reader.%s();\n" % (TAB3, field_name, method)) cls_name = util.format_to_pascal(descriptor[0]) f.write("%spublic %s(byte[] source)\n" % (TAB2, cls_name)) f.write("%s{\n" % TAB2) f.write("%s%s(source);\n" % (TAB3, self._receive_stream_creator_name)) util.iterate_message_fields(descriptor, read_from_byte_array) f.write("%s}\n" % TAB2)
def _message_class_header(self, descriptor, message_id): f = self._file def write_field_definition(field_name, field_type, *_): default_value = util.default_for_type(field_type, self._custom_enums) if field_type in self._custom_enums: default_value = util.format_to_pascal( self._custom_enums[field_type]['cls']) + '.' + default_value.upper() f.write('%sself.%s = %s\n' % (TAB2, field_name, default_value, )) def add_field_format(field_name, field_type, *_): fmt = util.field_format(field_type, _[1] if field_type == 'string' else None, self._custom_enums) f.write(fmt) cls_name = util.format_to_pascal(descriptor[0]) f.write('\n') f.write('\n') f.write('class %s(%s):\n' % (cls_name, self._base_class_name, )) f.write('%sID = %i\n' % (TAB, message_id)) f.write('\n') f.write('%s@property\n' % TAB) f.write('%sdef %s(self):\n' % (TAB, self._get_id_method_name, )) f.write('%sreturn %i\n' % (TAB2, message_id, )) f.write('\n') f.write('%sdef __init__(self):\n' % TAB) f.write('%s%s.__init__(self)\n' % (TAB2, self._base_class_name, )) util.iterate_message_fields(descriptor, write_field_definition) f.write('\n') f.write('%sself._format += "' % TAB2) util.iterate_message_fields(descriptor, add_field_format) f.write('"\n') f.write('%sself._struct = struct.Struct(self._format)\n' % TAB2)