Source

Cubeia Styx / maven / protocol-generator-plugin / src / main / scripts / java_generator.rb

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
#
# Java output writer.
#

JAVA_TYPES = {
  'uint8'  => 'UnsignedByte',
  'int8'   => 'byte',
  'uint16' => 'UnsignedShort',
  'int16'  => 'short',
  'int32'  => 'int',
  'uint32' => 'UnsignedInt',
  'int64'  => 'long',
  'uint64' => 'long',
  'string' => 'String',
  'bool'   => 'boolean'
}

# Member type definitions, special handling for unsigned data types,
# since java decides to suck
UNSIGNED_TYPES = {
  'UnsignedByte'  => 'int',
  'UnsignedShort' => 'int',
  'UnsignedInt'   => 'long'
}

def jtype(generic_type)
  JAVA_TYPES[generic_type] || camelcasetype(generic_type)
end

def capitalize_first(s) 
  s[0..0].upcase + s[1..-1]  
end

def builtin?(generic_type)
  JAVA_TYPES.has_key?(generic_type)
end

def enum?(type)
  $enums.has_key?(type)
end

def camelcase(str)
  result = ''
  str.split('_').each_with_index do |e,index|
    e.capitalize! if index != 0
    result << e
  end
  result
end

def camelcasetype(str)
  str.split('_').map{|e| e.capitalize}.join
end

class JavaOut
  def initialize(output_file, visitor = false)
    @indent_level = 0
    @out = output_file
    @visitor = visitor
  end
  
  def order
    [:start_class, :nested_types, :members, :default_constructor, :field_constructor, :save, :load, :sysout, :end_class]
  end
  
  def start_class(name, class_id)
    @classname = name
    static_tag = @indent_level != 0 ? ' static' : ''
    parent_tag = @indent_level == 0 ? ' implements ProtocolObject' : ''
    if (@visitor == true and @indent_level == 0)
      visitable_tag = ', Visitable'
    end
    
    wi("@SuppressWarnings(\"unused\")")
    wi("public#{static_tag} class #{camelcasetype(name)}#{parent_tag}#{visitable_tag} {")
    @indent_level += 1
    if !class_id.to_s.empty?
      wi("public int classId() {")
      wi("return #{class_id};",1)
      wi("}")
      wi("")
      visitable()
    end
  end
  
  def visitable 
    if (@visitor == true)      
      wi("public void accept(ProtocolObjectVisitor visitor) {")
      wi("if (visitor instanceof PacketVisitor) {",1)
      wi("PacketVisitor handler = (PacketVisitor) visitor;",2)
      wi("handler.visit(this);",2)
      wi("}",1)
      wi("}")
      
    end
  end
  
  def end_scope
    @indent_level -= 1
    wi("}")
  end
  
  alias :end_class :end_scope
  alias :end_save :end_scope
  alias :end_load :end_scope
  
  def member_scalar(scalar)
    type = jtype(scalar.obj_type)
    if(UNSIGNED_TYPES.has_key?(type))
      type = UNSIGNED_TYPES[type]
    end
    wi("public #{type} #{camelcase(scalar.name)};")
    return type
  end
  
  def member_list(list)
    type = jtype(list.obj_type)
    # uint8 array *is* a byte-array
    if(list.obj_type == "uint8")
      wi("public byte[] #{camelcase(list.name)} = new byte[0];")
      result = "byte[]"   
    elsif(builtin?(list.obj_type))
      # If primitive, then use array instead of List  	
      wi("public #{type}[] #{camelcase(list.name)} = new #{type}[0];")
      result = "#{type}[]"
    else 
      # If complex type, then use List 
      if (UNSIGNED_TYPES.has_key?(type))
        type = UNSIGNED_TYPES[type]
        
      end
      wi("public List<#{type}> #{camelcase(list.name)};")
      result = "List<#{type}>"
    end
    return result
  end
  
  def member_enum(enum)
    enumtype = enum.type
    enumtype = enumtype.capitalize
    enumtype= camelcase(enumtype)
    name = camelcase(enum.name)
    wi("public Enums.#{enumtype} #{name} = Enums.make#{enumtype}(0);")
    return "Enums.#{enumtype}"
  end
  
  def start_save(struct)
    wi("")
    wi("public void save(PacketOutputStream ps) throws IOException {")
    @indent_level += 1
  end
  
  def save_scalar(scalar)
    name = camelcase(scalar.name)
    if(builtin?(scalar.obj_type))
      wi("ps.save#{capitalize_first(jtype(scalar.obj_type))}(#{name});")
    else
      wi("#{name}.save(ps);")
    end
  end
  
  def save_list(list)
    name = camelcase(list.name)
    ltype = jtype(list.obj_type)
    
    
    if enum?(list.obj_type)
      wi("if (#{name} == null) {")
      @indent_level += 1
      wi("ps.saveInt(0);")
      @indent_level -= 1
      wi("} else {")
      @indent_level += 1
      wi("ps.saveInt(#{name}.size());")
      wi("for(int i = 0; i != #{name}.size(); ++i) {")
      wi("ps.saveUnsignedByte(#{name}.get(i).ordinal());",1)
      wi("}")
      @indent_level -= 1
      wi("}")
      
    elsif(builtin?(list.obj_type))
      # Save as array
      wi("ps.saveInt(#{name}.length);")
      wi("ps.saveArray(#{name});")
      
    else
      # Save as List of Objects
      wi("if (#{name} == null) {")
      @indent_level += 1
      wi("ps.saveInt(0);")
      @indent_level -= 1
      wi("} else {")
      @indent_level += 1
      wi("ps.saveInt(#{name}.size());")
      wi("for(int i = 0; i != #{name}.size(); ++i)")
      if(builtin?(list.obj_type))
        wi("ps.save#{jtype(list.obj_type)}(#{name}.get(i));",1)
      else
        wi("#{name}.get(i).save(ps);",1)
      end
      @indent_level -= 1
      wi("}")
    end
  end
  
  def start_load(struct)
    wi("")
    wi("public void load(PacketInputStream ps) throws IOException {")
    @indent_level += 1
  end
  
  def load_enum(enum)
    name = enum.type.capitalize
    name = camelcase(name)
    wi("if (ps.peek() == -1) {");
    @indent_level += 1
    wi("this.#{camelcase(enum.name)} = null;")
    @indent_level -= 1
    wi("} else {")
    @indent_level += 1
    wi("this.#{camelcase(enum.name)} = Enums.make#{name}(ps.loadUnsignedByte());")
    @indent_level -= 1
    wi("}")
  end
  
  def save_enum(enum)
    name = camelcase(enum.name)
    wi("if (this.#{name} == null) {");
    @indent_level += 1
    wi("ps.saveUnsignedByte(-1);")
    @indent_level -= 1
    wi("} else {")
    @indent_level += 1
    wi("ps.saveUnsignedByte(this.#{name}.ordinal());")
    @indent_level -= 1
    wi("}")
  end
  
  def load_scalar(scalar)
    name = camelcase(scalar.name)
    if(builtin?(scalar.obj_type))
      wi("#{name} = ps.load#{capitalize_first(jtype(scalar.obj_type))}();")
    else
      wi("#{name} = new #{jtype(scalar.obj_type)}();")
      wi("#{name}.load(ps);")
    end
  end
  
  def load_list(list)
    name = camelcase(list.name)
    ltype = jtype(list.obj_type)
    
    
    if(UNSIGNED_TYPES.has_key?(ltype))
      ltype = UNSIGNED_TYPES[ltype]
    end
    
    if(list.obj_type == "uint8")
      # Array of uint8 *is* byte
      wi("int #{name}Count = ps.loadInt();")
      wi("#{name} = new byte[#{name}Count];")
      wi("ps.loadByteArray(#{name});")
      
    elsif(builtin?(list.obj_type))
      wi("int #{name}Count = ps.loadInt();")
      wi("#{name} = new #{ltype}[#{name}Count];")
      wi("ps.load#{ltype.capitalize}Array(#{name});")
      
    else
      
      wi("int #{name}Count = ps.loadInt();")
      wi("#{name} = new ArrayList<#{ltype}>(#{name}Count);")
      wi("for(int i = 0; i != #{name}Count; ++i) {")
      if(builtin?(list.obj_type))
        wi("#{name}.add(ps.load#{ltype}());",1)
      elsif(enum?(list.obj_type))
        wi("int ordinal = ps.loadUnsignedByte();",1)
        wi("#{ltype} _tmp = Enums.make#{ltype}(ordinal);",1)
        wi("#{name}.add(_tmp);",1)
      else
        wi("#{ltype} _tmp = new #{ltype}();",1)
        wi("_tmp.load(ps);",1)
        wi("#{name}.add(_tmp);",1)
      end
      wi("}")
    end
  end
  
  def object_factory(structs, version)
  	wi("@SuppressWarnings(\"unused\")")
    wi("public class ProtocolObjectFactory implements com.cubeia.firebase.io.ObjectFactory {")
    wi("public int version() {",1)
    wi("return #{version};",2)
    wi("}",1)
    wi("")
    wi("public ProtocolObject create(int classId) {",1)
    wi("switch(classId) {",2)
    
    structs.each do |struct|
      wi("case #{struct.class_id}:",3)
      wi("return new #{camelcasetype(struct.name)}();",4)
    end
    
    wi("}",2)
    wi("throw new IllegalArgumentException(\"Unknown class id: \" + classId);",2)
    wi("}",1)
    wi("}")
  end
  
  # Create Visitor interface
  def object_visitor(structs, version)
  	wi("@SuppressWarnings(\"unused\")")
    wi("public interface PacketVisitor extends ProtocolObjectVisitor {")
    structs.each do |struct|
      wi("public void visit(#{camelcasetype(struct.name)} packet);",1)
    end
    wi("}")
  end
  
  # Create Visitable interface (used by all ProtocolObjects if applicable)
  def object_visitable(structs, version)
    wi("public interface Visitable {")
    wi("public void accept(ProtocolObjectVisitor visitor);",1)
    wi("}")
  end
  
  def wi(line,extra = 0)
    if(!line.empty?)
      @out.write("    " * (@indent_level + extra))
    end
    @out.write(line + "\n")
  end
  
  def JavaOut.gen_enum_deserializer(name, values)
    name = name.capitalize
    name = camelcase(name)
    str = "    public static #{name} make#{name}(int value) {\n"
    str<< "        switch(value) {\n"
    values.each_with_index {|v,i| str << "            case #{i}: return #{name}.#{v};\n" }
    str<< "            default: throw new IllegalArgumentException(\"Invalid enum value for #{name}: \" + value);\n"
    str<< "        }\n"
    str<< "    }\n\n"
    str
  end
  
  def JavaOut.gen_enum(name, values)
    name = name.capitalize
    name = camelcase(name)
    "    public enum #{name} { #{values.join(', ')} };\n\n"
  end
  
  def JavaOut.generate_enums(f, enums)
  	f.write("@SuppressWarnings(\"unused\")")
    f.write("public final class Enums {\n")
    f.write("    private Enums() {}\n\n")
    enums.each_pair do |enum, values|
      f.write(gen_enum(enum.capitalize, values))
      f.write(gen_enum_deserializer(enum.capitalize, values))
    end
    f.write("}\n")
  end
  
  def sysout(list)
    wi("\n")
    wi("@Override\n")
    wi("public String toString() {")
    wi("StringBuilder result = new StringBuilder(\"#{camelcasetype(@classname)} :\");", 1)
    list.each do |member| 
      if (member.class_definition == "byte[]")
        wi("result.append(\" #{member.name}[\"+ ArrayUtils.toString(#{camelcase(member.name)}, 20) +\"]\");", 1)
      else
        wi("result.append(\" #{member.name}[\"+#{camelcase(member.name)}+\"]\");", 1)
      end
    end
    wi("return result.toString();", 1)
    wi("}\n")
  end
  
  def create_field_list(members)
    result = ""
    members.each do |member|
      result << "#{member.class_definition} #{camelcase(member.name)}, "
    end
    return result.chomp(", ")
  end
  
  def field_constructor(members)
    if (members.empty?)
      # Don't create a field construct if there are no fields.
      return
    end
    
    field_list = create_field_list(members)
    wi("")
    wi("public #{camelcasetype(@classname)}(#{field_list}) {")
    members.each do |member|
      var = camelcase(member.name)
      wi("this.#{var} = #{var};", 1)      
    end
    wi("}")
  end
  
  def default_constructor()
    wi("")
    wi("/**")
    wi(" * Default constructor.")
    wi(" *")
    wi(" */")
    wi("public #{camelcasetype(@classname)}() {")
    wi("// Nothing here", 1)
    wi("}")
  end  
  
  def JavaOut.file_header(package)
    <<HEADER_END
// I AM AUTO-GENERATED, DON'T CHECK ME INTO SUBVERSION (or else...)
package #{package};

import com.cubeia.firebase.io.PacketInputStream;
import com.cubeia.firebase.io.PacketOutputStream;
import com.cubeia.firebase.io.ProtocolObject;
import com.cubeia.firebase.io.ProtocolObjectVisitor;
import com.cubeia.firebase.io.Visitable;
import com.cubeia.firebase.styx.util.ArrayUtils;

import java.util.List;
import java.util.ArrayList;
import java.io.IOException;

import #{package}.Enums.*;



HEADER_END
  end
end