Class: GameMachine::Protobuf::Generate

Inherits:
Object
  • Object
show all
Defined in:
server/lib/game_machine/protobuf/generate.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app_root) ⇒ Generate

Returns a new instance of Generate


15
16
17
18
19
20
21
22
23
24
25
26
# File 'server/lib/game_machine/protobuf/generate.rb', line 15

def initialize(app_root)
  @app_root = app_root

  jars = Dir[File.join(ENV['JAVA_ROOT'], 'local_lib', '*.jar')]
  jars.each do |jar|
    require jar
  end
  java_import java.lang.System
  java_import 'com.dyuproject.protostuff.compiler.CachingProtoLoader'
  java_import java.io.FileNotFoundException
  java_import 'com.dyuproject.protostuff.parser.ProtoUtil'
end

Instance Attribute Details

#app_rootObject (readonly)

Returns the value of attribute app_root


14
15
16
# File 'server/lib/game_machine/protobuf/generate.rb', line 14

def app_root
  @app_root
end

Class Method Details

.compile(path) ⇒ Object


32
33
34
35
36
# File 'server/lib/game_machine/protobuf/generate.rb', line 32

def self.compile(path)
  loader = CachingProtoLoader.new
  file = java.io.File.new(path)
  ProtoUtil.parseProto(file)
end

Instance Method Details

#classname(name) ⇒ Object


292
293
294
# File 'server/lib/game_machine/protobuf/generate.rb', line 292

def classname(name)
  name.slice(0,1).capitalize + name.slice(1..-1)
end

#client_templateObject


42
43
44
# File 'server/lib/game_machine/protobuf/generate.rb', line 42

def client_template
  File.join(java_root,'client.erb')
end

#config_pathObject


70
71
72
# File 'server/lib/game_machine/protobuf/generate.rb', line 70

def config_path
  File.join(app_root,'config')
end

#default_value(field) ⇒ Object


238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
# File 'server/lib/game_machine/protobuf/generate.rb', line 238

def default_value(field)
  if field.getJavaType.to_s == 'int'
    return 0
  elsif field.getJavaType.to_s == 'boolean'
      return 'false'
  elsif field.getJavaType.to_s == 'double'
    return '0D'
  elsif field.getJavaType.to_s == 'long'
    return '0L'
  elsif field.getJavaType.to_s == 'float'
    return '0F'
  elsif field.getJavaType.to_s == 'String'
    return 'null'
  else
    return nil
  end
end

#erb_templateObject


38
39
40
# File 'server/lib/game_machine/protobuf/generate.rb', line 38

def erb_template
  File.join(java_root,'component.erb')
end

#generateObject


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
# File 'server/lib/game_machine/protobuf/generate.rb', line 296

def generate
  FileUtils.rm Dir.glob(File.join(server_src,'*.java'))
  game_protofile = File.join(config_path,'game_messages.proto')
  protofile = File.join(config_path,'messages.proto')

  if File.exists?(game_protofile)
    game_messages = File.read(game_protofile)
    game_messages = game_messages.gsub('local_persistent_message','message')
    game_messages = game_messages.gsub('local_message','message')
    game_messages = game_messages.gsub('persistent_message','message')
    gm = Protobuf::GameMessages.new(game_protofile)
    persistent_messages = gm.persistent_messages
    entity_fields = gm.create_entity_fields
    game_entity_fields = entity_fields.join("\n")
  else
    game_messages = ''
    game_entity_fields = ''
    persistent_messages = []
  end
  messages = File.read(protofile)

  combined_messages = messages.sub('//GAME_MESSAGES',game_messages)
  combined_messages = combined_messages.sub('//GAME_ENTITY_MESSAGES',game_entity_fields)
  combined_messages_protofile = File.join(config_path,'combined_messages.proto')

  File.open(combined_messages_protofile,'w') {|f| f.write(combined_messages)}

  # test message defintion validity
  #tmp_dir = File.join('/tmp/proto_test')
  #FileUtils.rm_rf(tmp_dir)
  #FileUtils.mkdir_p(tmp_dir)
  #unless system("protoc #{combined_messages_protofile} --java_out=#{tmp_dir}")
  #  return false
  #end

  # This just stops generating code when it hits an error, but does not
  # throw an exception
  proto = self.class.compile(combined_messages_protofile)

  write_components(proto,persistent_messages)
  #FileUtils.rm(combined_messages_protofile)
  combined_messages
end

#get_type(field) ⇒ Object


276
277
278
279
280
281
282
283
284
285
286
# File 'server/lib/game_machine/protobuf/generate.rb', line 276

def get_type(field)
  if field.getJavaType.to_s == 'int'
    return 'int'
  elsif field.getJavaType.to_s == 'float'
    return 'float'
  elsif ['boolean','double','long'].include?(field.getJavaType.to_s)
    return field.getJavaType.to_s.clone
  else
    return field.getJavaType
  end
end

#java_rootObject


28
29
30
# File 'server/lib/game_machine/protobuf/generate.rb', line 28

def java_root
  ENV['JAVA_ROOT']
end

#model_srcObject


58
59
60
# File 'server/lib/game_machine/protobuf/generate.rb', line 58

def model_src
  File.join(java_root,'src','main','java','io', 'gamemachine','orm','models')
end

#model_templateObject


46
47
48
# File 'server/lib/game_machine/protobuf/generate.rb', line 46

def model_template
  File.join(java_root,'model.erb')
end

#nullable(field) ⇒ Object


256
257
258
259
260
261
262
263
264
# File 'server/lib/game_machine/protobuf/generate.rb', line 256

def nullable(field)
  if field.getJavaType.to_s == 'int'
    return 'Integer'
  elsif ['boolean','double','long','float'].include?(field.getJavaType.to_s)
    return field.getJavaType.to_s.clone.capitalize
  else
    return field.getJavaType
  end
end

#server_srcObject


62
63
64
# File 'server/lib/game_machine/protobuf/generate.rb', line 62

def server_src
  File.join(java_root,'src','main','java','io', 'gamemachine','messages')
end

#setter(field) ⇒ Object


266
267
268
269
270
271
272
273
274
# File 'server/lib/game_machine/protobuf/generate.rb', line 266

def setter(field)
  if field.getJavaType.to_s == 'int'
    return 'Integer'
  elsif ['boolean','double','long','float'].include?(field.getJavaType.to_s)
    return field.getJavaType.to_s.clone.capitalize
  else
    return field.getJavaType
  end
end

#shared_srcObject


66
67
68
# File 'server/lib/game_machine/protobuf/generate.rb', line 66

def shared_src
  File.join(app_root,'java','shared','src','main','java','io', 'gamemachine','client','messages')
end

#simple_value?(field) ⇒ Boolean

Returns:

  • (Boolean)

230
231
232
233
234
235
236
# File 'server/lib/game_machine/protobuf/generate.rb', line 230

def simple_value?(field)
  if ['boolean','double','float','long','int','String','ByteString'].include?(field.getJavaType.to_s)
    true
  else
    false
  end
end

#sql_column_name(klass, field) ⇒ Object


142
143
144
# File 'server/lib/game_machine/protobuf/generate.rb', line 142

def sql_column_name(klass,field)
  "#{klass.underscore}_#{field.name.underscore}"
end

#sql_field(klass, field, dbtype, force_null = false) ⇒ Object


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
# File 'server/lib/game_machine/protobuf/generate.rb', line 150

def sql_field(klass,field,dbtype,force_null=false)
  if dbtype == 'mysql'
    if field.isEnumField
      return "#{sql_column_name(klass,field)} int(11),"
    end
    txt = case field.getJavaType.to_s
    when 'boolean'
      "#{sql_column_name(klass,field)} tinyint(4)"
    when 'double'
      "#{sql_column_name(klass,field)} double"
    when 'float'
      "#{sql_column_name(klass,field)} float"
    when 'long'
      "#{sql_column_name(klass,field)} bigint"
    when 'int'
      "#{sql_column_name(klass,field)} int(11)"
    when 'String'
      if text_field?(field)
        "#{sql_column_name(klass,field)} TEXT"
      else
        "#{sql_column_name(klass,field)} varchar(256)"
      end
    when 'ByteString'
     "#{sql_column_name(klass,field)} varbinary(2048)"
    end
  elsif dbtype == 'postgres'
    if field.isEnumField
      return  "#{sql_column_name(klass,field)} integer,"
    end
    txt = case field.getJavaType.to_s
    when 'boolean'
      "#{sql_column_name(klass,field)} boolean"
    when 'double'
      "#{sql_column_name(klass,field)} double precision"
    when 'float'
      "#{sql_column_name(klass,field)} double precision"
    when 'long'
      "#{sql_column_name(klass,field)} bigint"
    when 'int'
      "#{sql_column_name(klass,field)} integer"
    when 'String'
      "#{sql_column_name(klass,field)} TEXT"
    when 'ByteString'
      "#{sql_column_name(klass,field)} bytea"
    end
  elsif dbtype == 'sqlite'
    if field.isEnumField
      return "#{sql_column_name(klass,field)} integer,"
    end
    txt = case field.getJavaType.to_s
    when 'boolean'
      "#{sql_column_name(klass,field)} boolean"
    when 'double'
      "#{sql_column_name(klass,field)} double precision"
    when 'float'
      "#{sql_column_name(klass,field)} double precision"
    when 'long'
      "#{sql_column_name(klass,field)} integer"
    when 'int'
      "#{sql_column_name(klass,field)} integer"
    when 'String'
      "#{sql_column_name(klass,field)} varchar(1024)"
    when 'ByteString'
      "#{sql_column_name(klass,field)} blob"
    end
  end

  return nil if txt.nil?

  if force_null
    return "#{txt} DEFAULT NULL,"
  end

  if field.is_required
    txt = "#{txt} NOT NULL,"
  else
    txt = "#{txt} DEFAULT NULL,"
  end
end

#sql_out(name) ⇒ Object


54
55
56
# File 'server/lib/game_machine/protobuf/generate.rb', line 54

def sql_out(name)
  File.join(config_path,'db',"#{name}_generated.sql")
end

#sql_template(name) ⇒ Object


50
51
52
# File 'server/lib/game_machine/protobuf/generate.rb', line 50

def sql_template(name)
  File.join(java_root,"#{name}.rb")
end

#text_field?(field) ⇒ Boolean

Returns:

  • (Boolean)

146
147
148
# File 'server/lib/game_machine/protobuf/generate.rb', line 146

def text_field?(field)
  field.name.match(/Text$/)
end

#varname(name) ⇒ Object


288
289
290
# File 'server/lib/game_machine/protobuf/generate.rb', line 288

def varname(name)
  name.slice(0,1).downcase + name.slice(1..-1)
end

#write_components(proto, persistent_messages) ⇒ Object


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
# File 'server/lib/game_machine/protobuf/generate.rb', line 74

def write_components(proto,persistent_messages)
  messages = proto.getMessages.reject {|message| message.getName == 'Components'}
  FileUtils.mkdir_p(server_src)
  FileUtils.mkdir_p(model_src)
  FileUtils.rm_rf(shared_src)
  FileUtils.mkdir_p(shared_src)
  message_names = proto.getMessages.map {|m| m.get_name}
  messages_index = proto.getMessages.each_with_object({}) {|v,res| res[v.getName] = v}

  sqlcontent = {}
  
  proto.getMessages.each do |message|
    message_fields = []
    message_field_names = []
    message.get_fields.each do |field|
      if message_names.include?(field.getJavaType.to_s) && !field.is_repeated
        message_fields << field.get_java_type.to_s
        message_field_names << field.name
      end
    end
    unless message_fields.empty?
      #puts "#{message.getName} #{message_fields.inspect}"
    end

    klass = message.getName
    persistent = persistent_messages.include?(klass)
    #puts "Message: #{message.getName}"
    out = ERB.new(File.read(erb_template),0,'>').result(binding)
    out = out.gsub(/^(\s*\r\n){2,}/,"\r\n")
    src_file = File.join(server_src,"#{message.getName}.java")
    File.open(src_file,'w') {|f| f.write out}

    #out = ERB.new(File.read(client_template),0,'>').result(binding)
    #out = out.gsub(/^(\s*\r\n){2,}/,"\r\n")
    #src_file = File.join(shared_src,"#{message.getName}.java")
    #File.open(src_file,'w') {|f| f.write out}

    if persistent
      out = ERB.new(File.read(model_template),0,'<>').result(binding)
      out = out.gsub(/^(\s*\r\n){2,}/,"\r\n")
      out = out.gsub(/^(\s*\n){2,}/,"\n")
      src_file = File.join(model_src,"#{message.getName}.java")
      File.open(src_file,'w') {|f| f.write out}
        
      ['sqlite','postgres','mysql'].each do |dbtype|
        sqlcontent[dbtype] ||= ''
        
        sql_out = ERB.new(File.read(sql_template(dbtype)),0,'<>').result(binding)
        sql_out = sql_out.gsub(/^(\s*\r\n){2,}/,"\r\n")
        sql_out = sql_out.gsub(/^(\s*\n){2,}/,"\n")
        sqlcontent[dbtype] << sql_out
      end
    end
    message.getFields.each do |field|
      if field.isEnumField
       
      end
      #puts field.default_value_set
      #puts field.getJavaType
      
    end
  end
  
  ['sqlite','postgres','mysql'].each do |dbtype|
    File.open(sql_out(dbtype),'w') {|f| f.write sqlcontent[dbtype]}
  end
end