Class: GameMachine::Protobuf::Generate

Inherits:
Object
  • Object
show all
Defined in:
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.



17
18
19
# File 'lib/game_machine/protobuf/generate.rb', line 17

def initialize(app_root)
  @app_root = app_root
end

Instance Attribute Details

#app_rootObject (readonly)

Returns the value of attribute app_root.



16
17
18
# File 'lib/game_machine/protobuf/generate.rb', line 16

def app_root
  @app_root
end

Class Method Details

.compile(path) ⇒ Object



25
26
27
28
29
# File 'lib/game_machine/protobuf/generate.rb', line 25

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

Instance Method Details

#classname(name) ⇒ Object



164
165
166
# File 'lib/game_machine/protobuf/generate.rb', line 164

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

#config_pathObject



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

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

#erb_templateObject



31
32
33
# File 'lib/game_machine/protobuf/generate.rb', line 31

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

#generateObject



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

def generate
  FileUtils.rm Dir.glob(File.join(java_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



150
151
152
153
154
155
156
157
158
# File 'lib/game_machine/protobuf/generate.rb', line 150

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

#java_rootObject



21
22
23
# File 'lib/game_machine/protobuf/generate.rb', line 21

def java_root
  ENV['JAVA_ROOT']
end

#java_srcObject



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

def java_src
  File.join(java_root,'src','main','java','GameMachine', 'Messages')
end

#model_srcObject



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

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

#model_templateObject



35
36
37
# File 'lib/game_machine/protobuf/generate.rb', line 35

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

#simple_value?(field) ⇒ Boolean

Returns:

  • (Boolean)


142
143
144
145
146
147
148
# File 'lib/game_machine/protobuf/generate.rb', line 142

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

#sql_column_name(klass, field) ⇒ Object



92
93
94
# File 'lib/game_machine/protobuf/generate.rb', line 92

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

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



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 'lib/game_machine/protobuf/generate.rb', line 96

def sql_field(klass,field,dbtype,force_null=false)
  if dbtype == 'mysql'
    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)}` int(11)"
    when 'int'
      "`#{sql_column_name(klass,field)}` int(11)"
    when 'String'
      "`#{sql_column_name(klass,field)}` varchar(128)"
    end
  elsif dbtype == 'postgres'
    txt = case field.getJavaType.to_s
    when 'boolean'
      "#{sql_column_name(klass,field)} integer"
    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)} character varying(128)"
    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

#varname(name) ⇒ Object



160
161
162
# File 'lib/game_machine/protobuf/generate.rb', line 160

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

#write_components(proto, persistent_messages) ⇒ Object



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

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

  proto.getMessages.each do |message|
    message_fields = []
    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
      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(java_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}
    end
    #message.getFields.each do |field|
    #  puts field.default_value_set
    #  puts field.getJavaType
    #  puts field.toString
    #end
  end
end