Module: RGSS
- Defined in:
- lib/RGSS.rb,
lib/RGSS/serialize.rb,
lib/RGSS/BasicCoder.rb
Defined Under Namespace
Modules: BasicCoder Classes: Game_SelfSwitches, Game_Switches, Game_System, Game_Variables
Constant Summary collapse
- FLOW_CLASSES =
[Color, Tone, RPG::BGM, RPG::BGS, RPG::MoveCommand, RPG::SE]
- SCRIPTS_BASE =
'Scripts'
- ACE_DATA_EXT =
'.rvdata2'
- VX_DATA_EXT =
'.rvdata'
- XP_DATA_EXT =
'.rxdata'
- YAML_EXT =
'.yaml'
- RUBY_EXT =
'.rb'
Class Method Summary collapse
- .array_to_hash(arr, &block) ⇒ Object
- .change_extension(file, new_ext) ⇒ Object
- .convert(src, dest, options) ⇒ Object
- .convert_saves(base, src, dest, options) ⇒ Object
- .deflate(str) ⇒ Object
- .dump(dumper, file, data, time, options) ⇒ Object
- .dump_data_file(file, data, time, options) ⇒ Object
- .dump_raw_file(file, data, time, options) ⇒ Object
- .dump_save(file, data, time, options) ⇒ Object
- .dump_yaml_file(file, data, time, options) ⇒ Object
- .files_with_extension(directory, extension) ⇒ Object
- .get_data_directory(base) ⇒ Object
- .get_script_directory(base) ⇒ Object
- .get_yaml_directory(base) ⇒ Object
- .hash_to_array(hash) ⇒ Object
- .inflate(str) ⇒ Object
- .load(loader, file) ⇒ Object
- .load_data_file(file) ⇒ Object
- .load_raw_file(file) ⇒ Object
- .load_save(file) ⇒ Object
- .load_yaml_file(file) ⇒ Object
-
.process(root, name, *args) ⇒ Object
creates an empty class in a potentially nested scope.
- .process_file(file, src_file, dest_file, dest_ext, loader, dumper, options) ⇒ Object
- .remove_defined_method(scope, name) ⇒ Object
- .reset_const(scope, sym, value) ⇒ Object
- .reset_method(scope, name, method) ⇒ Object
- .sanitize_filename(filename) ⇒ Object
- .scripts_to_binary(dirs, src, dest, options) ⇒ Object
- .scripts_to_text(dirs, src, dest, options) ⇒ Object
-
.serialize(version, direction, directory, options = {}) ⇒ Object
- version
-
one of :ace, :vx, :xp [direction] one of :data_bin_to_text, :data_text_to_bin, :save_bin_to_text, :save_text_to_bin, :scripts_bin_to_text, :scripts_text_to_bin, :all_text_to_bin, :all_bin_to_text [directory] directory that project file is in [options] :force - ignores file dates when converting (default false) :round_trip - create yaml data that matches original marshalled data skips data cleanup operations (default false) :line_width - line width form YAML files, -1 for no line width limit (default 130) :table_width - maximum number of entries per row for table data, -1 for no table row limit (default 20).
- .setup_classes(version, options) ⇒ Object
- .setup_event_command(version, options) ⇒ Object
- .setup_interpreter(version) ⇒ Object
- .setup_system(version, options) ⇒ Object
Class Method Details
.array_to_hash(arr, &block) ⇒ Object
143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/RGSS.rb', line 143 def self.array_to_hash(arr, &block) h = {} arr.each_with_index do |val, index| r = block_given? ? block.call(val) : val h[index] = r unless r.nil? end if arr.length > 0 last = arr.length - 1 h[last] = nil unless h.has_key?(last) end return h end |
.change_extension(file, new_ext) ⇒ Object
28 29 30 |
# File 'lib/RGSS/serialize.rb', line 28 def self.change_extension(file, new_ext) return File.basename(file, '.*') + new_ext end |
.convert(src, dest, options) ⇒ Object
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 |
# File 'lib/RGSS/serialize.rb', line 300 def self.convert(src, dest, ) files = files_with_extension(src[:directory], src[:ext]) files -= src[:exclude] files.each do |file| src_file = File.join(src[:directory], file) dest_file = File.join(dest[:directory], change_extension(file, dest[:ext])) process_file( file, src_file, dest_file, dest[:ext], src[:load_file], dest[:dump_file], ) end end |
.convert_saves(base, src, dest, options) ⇒ Object
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 |
# File 'lib/RGSS/serialize.rb', line 321 def self.convert_saves(base, src, dest, ) save_files = files_with_extension(base, src[:ext]) save_files.each do |file| src_file = File.join(base, file) dest_file = File.join(base, change_extension(file, dest[:ext])) process_file( file, src_file, dest_file, dest[:ext], src[:load_save], dest[:dump_save], ) end end |
.deflate(str) ⇒ Object
49 50 51 |
# File 'lib/RGSS/serialize.rb', line 49 def self.deflate(str) return Zlib::Deflate.deflate(str, Zlib::BEST_COMPRESSION) end |
.dump(dumper, file, data, time, options) ⇒ Object
75 76 77 78 79 80 |
# File 'lib/RGSS/serialize.rb', line 75 def self.dump(dumper, file, data, time, ) self.method(dumper).call(file, data, time, ) rescue StandardError warn "Exception dumping #{file}" raise end |
.dump_data_file(file, data, time, options) ⇒ Object
53 54 55 56 |
# File 'lib/RGSS/serialize.rb', line 53 def self.dump_data_file(file, data, time, ) File.open(file, 'wb') { |f| Marshal.dump(data, f) } File.utime(time, time, file) end |
.dump_raw_file(file, data, time, options) ⇒ Object
70 71 72 73 |
# File 'lib/RGSS/serialize.rb', line 70 def self.dump_raw_file(file, data, time, ) File.open(file, 'wb') { |f| f.write(data) } File.utime(time, time, file) end |
.dump_save(file, data, time, options) ⇒ Object
63 64 65 66 67 68 |
# File 'lib/RGSS/serialize.rb', line 63 def self.dump_save(file, data, time, ) File.open(file, 'wb') do |f| data.each { |chunk| Marshal.dump(chunk, f) } end File.utime(time, time, file) end |
.dump_yaml_file(file, data, time, options) ⇒ Object
58 59 60 61 |
# File 'lib/RGSS/serialize.rb', line 58 def self.dump_yaml_file(file, data, time, ) File.open(file, 'wb') { |f| Psych.dump(data, f, ) } File.utime(time, time, file) end |
.files_with_extension(directory, extension) ⇒ Object
36 37 38 39 40 41 42 |
# File 'lib/RGSS/serialize.rb', line 36 def self.files_with_extension(directory, extension) return( Dir .entries(directory) .select { |file| File.extname(file) == extension } ) end |
.get_data_directory(base) ⇒ Object
334 335 336 |
# File 'lib/RGSS.rb', line 334 def self.get_data_directory(base) return File.join(base, 'Data') end |
.get_script_directory(base) ⇒ Object
342 343 344 |
# File 'lib/RGSS.rb', line 342 def self.get_script_directory(base) return File.join(base, 'Scripts') end |
.get_yaml_directory(base) ⇒ Object
338 339 340 |
# File 'lib/RGSS.rb', line 338 def self.get_yaml_directory(base) return File.join(base, 'YAML') end |
.hash_to_array(hash) ⇒ Object
156 157 158 159 160 |
# File 'lib/RGSS.rb', line 156 def self.hash_to_array(hash) arr = [] hash.each { |k, v| arr[k] = v } return arr end |
.inflate(str) ⇒ Object
44 45 46 47 |
# File 'lib/RGSS/serialize.rb', line 44 def self.inflate(str) text = Zlib::Inflate.inflate(str) return text.force_encoding('UTF-8') end |
.load(loader, file) ⇒ Object
159 160 161 162 163 164 |
# File 'lib/RGSS/serialize.rb', line 159 def self.load(loader, file) return self.method(loader).call(file) rescue StandardError warn "Exception loading #{file}" raise end |
.load_data_file(file) ⇒ Object
82 83 84 |
# File 'lib/RGSS/serialize.rb', line 82 def self.load_data_file(file) File.open(file, 'rb') { |f| return Marshal.load(f) } end |
.load_raw_file(file) ⇒ Object
144 145 146 |
# File 'lib/RGSS/serialize.rb', line 144 def self.load_raw_file(file) File.open(file, 'rb') { |f| return f.read } end |
.load_save(file) ⇒ Object
148 149 150 151 152 153 154 155 156 157 |
# File 'lib/RGSS/serialize.rb', line 148 def self.load_save(file) File.open(file, 'rb') do |f| data = [] while not f.eof? o = Marshal.load(f) data.push(o) end return data end end |
.load_yaml_file(file) ⇒ Object
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 |
# File 'lib/RGSS/serialize.rb', line 86 def self.load_yaml_file(file) formatador = Formatador.new obj = nil File.open(file, 'rb') { |f| obj = Psych.load(f) } max = 0 return obj unless obj.kind_of?(Array) seen = {} idx = obj.each do |elem| next if elem.nil? if elem.instance_variable_defined?('@id') id = elem.instance_variable_get('@id') else id = nil elem.instance_variable_set('@id', nil) end next if id.nil? if seen.has_key?(id) formatador.display_line( "[red]#{file}: Duplicate ID #{id}[/]" ) formatador.indent do formatador.indent do elem .pretty_inspect .split(/\n/) .each do |line| formatador.display_line("[red]#{line}[/]") end end formatador.display_line formatador.display_line("[red]Last seen at:\n[/]") formatador.indent do elem .pretty_inspect .split(/\n/) .each do |line| formatador.display_line("[red]#{line}[/]") end end end exit end seen[id] = elem max = ((id + 1) unless id < max) end obj.each do |elem| next if elem.nil? id = elem.instance_variable_get('@id') if id.nil? elem.instance_variable_set('@id', max) max += 1 end end return obj end |
.process(root, name, *args) ⇒ Object
creates an empty class in a potentially nested scope
166 167 168 169 170 171 172 173 174 |
# File 'lib/RGSS.rb', line 166 def self.process(root, name, *args) if args.length > 0 process(root.const_get(name), *args) else unless root.const_defined?(name, false) root.const_set(name, Class.new) end end end |
.process_file(file, src_file, dest_file, dest_ext, loader, dumper, options) ⇒ Object
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 |
# File 'lib/RGSS/serialize.rb', line 270 def self.process_file( file, src_file, dest_file, dest_ext, loader, dumper, ) formatador = Formatador.new fbase = File.basename(file, File.extname(file)) if (![:database].nil?) and ([:database].downcase != fbase.downcase) return end src_time = File.mtime(src_file) if ![:force] && File.exist?(dest_file) && (src_time - 1) < File.mtime(dest_file) formatador.display_line("[yellow]Skipping #{file}[/]") if $VERBOSE else if $VERBOSE formatador.display_line( "[green]Converting #{file} to #{dest_ext}[/]" ) end data = load(loader, src_file) dump(dumper, dest_file, data, src_time, ) end end |
.remove_defined_method(scope, name) ⇒ Object
127 128 129 130 131 |
# File 'lib/RGSS.rb', line 127 def self.remove_defined_method(scope, name) if scope.instance_methods(false).include?(name) scope.send(:remove_method, name) end end |
.reset_const(scope, sym, value) ⇒ Object
138 139 140 141 |
# File 'lib/RGSS.rb', line 138 def self.reset_const(scope, sym, value) scope.send(:remove_const, sym) if scope.const_defined?(sym) scope.send(:const_set, sym, value) end |
.reset_method(scope, name, method) ⇒ Object
133 134 135 136 |
# File 'lib/RGSS.rb', line 133 def self.reset_method(scope, name, method) remove_defined_method(scope, name) scope.send(:define_method, name, method) end |
.sanitize_filename(filename) ⇒ Object
32 33 34 |
# File 'lib/RGSS/serialize.rb', line 32 def self.sanitize_filename(filename) return filename.gsub(/[^0-9A-Za-z]+/, '_') end |
.scripts_to_binary(dirs, src, dest, options) ⇒ Object
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 |
# File 'lib/RGSS/serialize.rb', line 224 def self.scripts_to_binary(dirs, src, dest, ) formatador = Formatador.new src_file = File.join(dirs[:yaml], src) dest_file = File.join(dirs[:data], dest) raise "Missing #{src}" unless File.exist?(src_file) check_time = ![:force] && File.exist?(dest_file) newest_time = File.mtime(src_file) if check_time index = load(:load_yaml_file, src_file) script_entries = [] index.each do |entry| magic_number, script_name, filename = entry code = '' if filename full_filename = File.join(dirs[:script], filename) unless File.exist?(full_filename) raise "Missing script file #{filename}" end newest_time = [ File.mtime(full_filename), newest_time ].max if check_time code = load(:load_raw_file, full_filename) end script_entries.push([magic_number, script_name, deflate(code)]) end if check_time && (newest_time - 1) < File.mtime(dest_file) if $VERBOSE formatador.display_line('[yellow]Skipping scripts to binary[/]') end else if $VERBOSE formatador.display_line( '[green]Converting scripts to binary[/]' ) end dump( :dump_data_file, dest_file, script_entries, newest_time, ) end end |
.scripts_to_text(dirs, src, dest, options) ⇒ Object
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 |
# File 'lib/RGSS/serialize.rb', line 166 def self.scripts_to_text(dirs, src, dest, ) formatador = Formatador.new src_file = File.join(dirs[:data], src) dest_file = File.join(dirs[:yaml], dest) raise "Missing #{src}" unless File.exist?(src_file) script_entries = load(:load_data_file, src_file) check_time = ![:force] && File.exist?(dest_file) oldest_time = File.mtime(dest_file) if check_time file_map, script_index, script_code = Hash.new(-1), [], {} idx = 0 script_entries.each do |script| idx += 1 magic_number, script_name, code = idx, script[1], inflate(script[2]) script_name.force_encoding('UTF-8') if code.length > 0 filename = if script_name.empty? 'blank' else sanitize_filename(script_name) end key = filename.upcase value = (file_map[key] += 1) actual_filename = filename + (value == 0 ? '' : ".#{value}") + RUBY_EXT script_index.push([magic_number, script_name, actual_filename]) full_filename = File.join(dirs[:script], actual_filename) script_code[full_filename] = code check_time = false unless File.exist?(full_filename) oldest_time = [ File.mtime(full_filename), oldest_time ].min if check_time else script_index.push([magic_number, script_name, nil]) end end src_time = File.mtime(src_file) if check_time && (src_time - 1) < oldest_time if $VERBOSE formatador.display_line('[yellow]Skipping scripts to text[/]') end else if $VERBOSE formatador.display_line('[green]Converting scripts to text[/]') end dump(:dump_yaml_file, dest_file, script_index, src_time, ) script_code.each do |file, code| dump(:dump_raw_file, file, code, src_time, ) end end end |
.serialize(version, direction, directory, options = {}) ⇒ Object
- version
-
one of :ace, :vx, :xp
- direction
-
one of :data_bin_to_text, :data_text_to_bin, :save_bin_to_text, :save_text_to_bin, :scripts_bin_to_text, :scripts_text_to_bin, :all_text_to_bin, :all_bin_to_text
- directory
-
directory that project file is in
- options
-
:force - ignores file dates when converting (default false) :round_trip - create yaml data that matches original marshalled data skips
data cleanup operations (default false)
:line_width - line width form YAML files, -1 for no line width limit
(default 130)
:table_width - maximum number of entries per row for table data, -1 for no
table row limit (default 20)
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 437 438 439 440 441 442 443 444 445 446 447 |
# File 'lib/RGSS/serialize.rb', line 351 def self.serialize(version, direction, directory, = {}) raise "#{directory} not found" unless File.exist?(directory) setup_classes(version, ) = .clone [:sort] = true if %i[vx xp].include?(version) [:flow_classes] = FLOW_CLASSES [:line_width] ||= 130 table_width = [:table_width] RGSS.reset_const(Table, :MAX_ROW_LENGTH, table_width ? table_width : 20) base = File.realpath(directory) dirs = { base: base, data: get_data_directory(base), yaml: get_yaml_directory(base), script: get_script_directory(base) } dirs.values.each { |d| FileUtils.mkdir(d) unless File.exist?(d) } exts = { ace: ACE_DATA_EXT, vx: VX_DATA_EXT, xp: XP_DATA_EXT } yaml_scripts = SCRIPTS_BASE + YAML_EXT yaml = { directory: dirs[:yaml], exclude: [yaml_scripts], ext: YAML_EXT, load_file: :load_yaml_file, dump_file: :dump_yaml_file, load_save: :load_yaml_file, dump_save: :dump_yaml_file } scripts = SCRIPTS_BASE + exts[version] data = { directory: dirs[:data], exclude: [scripts], ext: exts[version], load_file: :load_data_file, dump_file: :dump_data_file, load_save: :load_save, dump_save: :dump_save } if [:database].nil? or [:database].downcase == 'scripts' convert_scripts = true else convert_scripts = false end if [:database].nil? or [:database].downcase == 'saves' convert_saves = true else convert_saves = false end case direction when :data_bin_to_text convert(data, yaml, ) if convert_scripts scripts_to_text(dirs, scripts, yaml_scripts, ) end when :data_text_to_bin convert(yaml, data, ) if convert_scripts scripts_to_binary(dirs, yaml_scripts, scripts, ) end when :save_bin_to_text convert_saves(base, data, yaml, ) if convert_saves when :save_text_to_bin convert_saves(base, yaml, data, ) if convert_saves when :scripts_bin_to_text if convert_scripts scripts_to_text(dirs, scripts, yaml_scripts, ) end when :scripts_text_to_bin if convert_scripts scripts_to_binary(dirs, yaml_scripts, scripts, ) end when :all_bin_to_text convert(data, yaml, ) if convert_scripts scripts_to_text(dirs, scripts, yaml_scripts, ) end convert_saves(base, data, yaml, ) if convert_saves when :all_text_to_bin convert(yaml, data, ) if convert_scripts scripts_to_binary(dirs, yaml_scripts, scripts, ) end convert_saves(base, yaml, data, ) if convert_saves else raise "Unrecognized direction :#{direction}" end end |
.setup_classes(version, options) ⇒ Object
317 318 319 320 321 322 |
# File 'lib/RGSS.rb', line 317 def self.setup_classes(version, ) setup_system(version, ) setup_interpreter(version) setup_event_command(version, ) BasicCoder.set_ivars_methods(version) end |
.setup_event_command(version, options) ⇒ Object
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 |
# File 'lib/RGSS.rb', line 299 def self.setup_event_command(version, ) # format event commands to flow style for the event codes that aren't move commands if [:round_trip] reset_method(RPG::EventCommand, :clean, -> { }) else reset_method( RPG::EventCommand, :clean, -> { @parameters[0].rstrip! if @code == 401 } ) end reset_const( RPG::EventCommand, :MOVE_LIST_CODE, version == :xp ? 209 : 205 ) end |
.setup_interpreter(version) ⇒ Object
284 285 286 287 288 289 290 291 292 293 294 295 296 297 |
# File 'lib/RGSS.rb', line 284 def self.setup_interpreter(version) # Game_Interpreter is marshalled differently in VX Ace if version == :ace reset_method(Game_Interpreter, :marshal_dump, -> { return @data }) reset_method( Game_Interpreter, :marshal_load, ->(obj) { @data = obj } ) else remove_defined_method(Game_Interpreter, :marshal_dump) remove_defined_method(Game_Interpreter, :marshal_load) end end |
.setup_system(version, options) ⇒ Object
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 |
# File 'lib/RGSS.rb', line 250 def self.setup_system(version, ) # convert variable and switch name arrays to a hash when serialized # if round_trip isn't set change version_id to fixed number if [:round_trip] iso = ->(val) { return val } reset_method(RPG::System, :reduce_string, iso) reset_method(RPG::System, :map_version, iso) reset_method(Game_System, :map_version, iso) else reset_method( RPG::System, :reduce_string, ->(str) do return nil if str.nil? stripped = str.strip return stripped.empty? ? nil : stripped end ) # These magic numbers should be different. If they are the same, the saved version # of the map in save files will be used instead of any updated version of the map reset_method( RPG::System, :map_version, ->(ignored) { return 12_345_678 } ) reset_method( Game_System, :map_version, ->(ignored) { return 87_654_321 } ) end end |