rmxp_extractor 1.5 → 1.7

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,69 +1,69 @@
1
- module RMXPExtractor
2
- def self.import(format)
3
- STDERR.puts "No Data_JSON Directory!" unless Dir.exists? "./Data_#{format.upcase}"
4
- exit 1 unless Dir.exists? "./Data_#{format.upcase}"
5
-
6
- require "oj"
7
- require "yaml"
8
- require "ruby-progressbar"
9
- require "fileutils"
10
- require "pathname"
11
- require "readline"
12
- require_relative "classnames"
13
- require_relative "script_handler"
14
-
15
- if format == "ron"
16
- puts "RON not supported for importing yet"
17
- exit 1
18
- end
19
-
20
- window_size = 120
21
- progress_format = "%a /%e |%B| %p%% %c/%C %r files/sec %t, currently processing: "
22
- progress = ProgressBar.create(
23
- format: progress_format,
24
- starting_at: 0,
25
- total: nil,
26
- output: $stderr,
27
- length: window_size,
28
- title: "imported",
29
- remainder_mark: "\e[0;30m█\e[0m",
30
- progress_mark: "█",
31
- unknown_progress_animation_steps: ["==>", ">==", "=>="],
32
- )
33
- Dir.mkdir "./Data" unless Dir.exists? "./Data"
34
- paths = Pathname.glob(("./Data_#{format.upcase}/" + ("*" + ".#{format}")))
35
- count = paths.size
36
- progress.total = count
37
- paths.each_with_index do |path, i|
38
- name = path.basename(".#{format}")
39
- progress.format = progress_format + name.to_s
40
- file_contents = path.read(mode: "rb")
41
- hash = case format
42
- when "json"
43
- Oj.load file_contents
44
- when "yaml"
45
- YAML.load file_contents
46
- when "toml"
47
- TomlRB.parse file_contents
48
- when "rb"
49
- eval file_contents # Yes, this SHOULD work. Is it bad? Yes.
50
- end
51
-
52
- warn "\n\e[33;1mWARNING: Incompatible version format in #{name.to_s}!\e[0m\n" if hash["version"] != VERSION
53
-
54
- case name.to_s
55
- when "xScripts", "Scripts"
56
- RMXPExtractor.rpgscript("./", "./Scripts", "#{name.to_s}")
57
- progress.increment
58
- return
59
- else
60
- content = create_from_rmxp_serialize(hash["data"])
61
- end
62
-
63
- rxdata = File.open("./Data/" + name.sub_ext(".rxdata").to_s, "wb")
64
- rxdata.puts Marshal.dump(content)
65
-
66
- progress.increment
67
- end
68
- end
69
- end
1
+ module RMXPExtractor
2
+ def self.import(format)
3
+ STDERR.puts "No Data_JSON Directory!" unless Dir.exists? "./Data_#{format.upcase}"
4
+ exit 1 unless Dir.exists? "./Data_#{format.upcase}"
5
+
6
+ require "oj"
7
+ require "yaml"
8
+ require "ruby-progressbar"
9
+ require "fileutils"
10
+ require "pathname"
11
+ require "readline"
12
+ require_relative "classnames"
13
+ require_relative "script_handler"
14
+
15
+ if format == "ron"
16
+ puts "RON not supported for importing yet"
17
+ exit 1
18
+ end
19
+
20
+ window_size = 120
21
+ progress_format = "%a /%e |%B| %p%% %c/%C %r files/sec %t, currently processing: "
22
+ progress = ProgressBar.create(
23
+ format: progress_format,
24
+ starting_at: 0,
25
+ total: nil,
26
+ output: $stderr,
27
+ length: window_size,
28
+ title: "imported",
29
+ remainder_mark: "\e[0;30m█\e[0m",
30
+ progress_mark: "█",
31
+ unknown_progress_animation_steps: ["==>", ">==", "=>="],
32
+ )
33
+ Dir.mkdir "./Data" unless Dir.exists? "./Data"
34
+ paths = Pathname.glob(("./Data_#{format.upcase}/" + ("*" + ".#{format}")))
35
+ count = paths.size
36
+ progress.total = count
37
+ paths.each_with_index do |path, i|
38
+ name = path.basename(".#{format}")
39
+ progress.format = progress_format + name.to_s
40
+ file_contents = path.read(mode: "rb")
41
+ hash = case format
42
+ when "json"
43
+ Oj.load file_contents
44
+ when "yaml"
45
+ YAML.load file_contents
46
+ when "toml"
47
+ TomlRB.parse file_contents
48
+ when "rb"
49
+ eval file_contents # Yes, this SHOULD work. Is it bad? Yes.
50
+ end
51
+
52
+ warn "\n\e[33;1mWARNING: Incompatible version format in #{name.to_s}!\e[0m\n" if hash["version"] != VERSION
53
+
54
+ case name.to_s
55
+ when "xScripts", "Scripts"
56
+ RMXPExtractor.rpgscript("./", "./Scripts", "#{name.to_s}")
57
+ progress.increment
58
+ return
59
+ else
60
+ content = create_from_rmxp_serialize(hash["data"])
61
+ end
62
+
63
+ rxdata = File.open("./Data/" + name.sub_ext(".rxdata").to_s, "wb")
64
+ rxdata.puts Marshal.dump(content)
65
+
66
+ progress.increment
67
+ end
68
+ end
69
+ end
@@ -1,146 +1,144 @@
1
- # This file handles rusty object notation.
2
- require_relative "classnames"
3
-
4
- $indent = 0
5
-
6
- class Object
7
- def rmxp_serialize
8
- $indent += 2
9
- str = "#{self.class.name.split("::").last}(\n"
10
- self.instance_variables.each { |var| str += "#{" " * $indent}#{var.to_s.delete("@")}: #{self.instance_variable_get(var).rmxp_serialize},\n" }
11
- $indent -= 2
12
- str += "#{" " * $indent})"
13
- str
14
- end
15
- end
16
-
17
- class Table
18
- def rmxp_serialize
19
- str = "Array(\n"
20
- $indent += 2
21
- str += "#{" " * $indent}v: 1,\n"
22
- str += "#{" " * $indent}dim: ("
23
- str += case @num_of_dimensions
24
- when 1
25
- "#{@xsize}"
26
- when 2
27
- "#{@ysize}, #{@xsize}"
28
- when 3
29
- "#{@zsize}, #{@ysize}, #{@xsize}"
30
- end
31
- str += "),\n"
32
- str += "#{" " * $indent}data: [\n"
33
- $indent += 2
34
- case @num_of_dimensions
35
- when 1
36
- str += "#{" " * $indent}"
37
- @elements.each_with_index do |e, index|
38
- str += "#{e}, "
39
- str += "\n#{" " * $indent}" if (index + 1) % 8 == 0
40
- end
41
- str += "\n"
42
- when 2
43
- @elements.each do |y|
44
- str += " " * $indent
45
- y.each { |e| str += "#{e}, " }
46
- str += "\n"
47
- end
48
- when 3
49
- @elements.each do |z|
50
- z.each do |y|
51
- str += " " * $indent
52
- y.each { |e| str += "#{e}, " }
53
- str += "\n"
54
- end
55
- str += "\n"
56
- end
57
- end
58
- $indent -= 2
59
- str += "#{" " * $indent}]\n"
60
- $indent -= 2
61
- str += "#{" " * $indent})"
62
- str
63
- end
64
- end
65
-
66
- class Hash
67
- def rmxp_serialize
68
- str = "{\n"
69
- $indent += 2
70
- self.each { |key, value| str += "#{" " * $indent}#{key.rmxp_serialize}: #{value.rmxp_serialize},\n" }
71
- $indent -= 2
72
- str += "#{" " * $indent}}"
73
- str
74
- end
75
- end
76
-
77
- # BAD BAD
78
- class NilClass
79
- def rmxp_serialize
80
- "None"
81
- end
82
- end
83
-
84
- class String
85
- def rmxp_serialize
86
- self.inspect
87
- end
88
- end
89
-
90
- class Array
91
- def rmxp_serialize
92
- unless self.empty?
93
- str = "[\n"
94
- $indent += 2
95
- self.each { |e| next unless e; str += "#{" " * $indent}#{e.rmxp_serialize},\n" }
96
- $indent -= 2
97
- str += "#{" " * $indent}]"
98
- else
99
- str = "[]"
100
- end
101
- str
102
- end
103
- end
104
-
105
- module RPG
106
- class EventCommand
107
- def rmxp_serialize
108
- str = "EventCommand(\n"
109
- $indent += 2
110
- str += "#{" " * $indent}indent: #{@indent},\n"
111
- str += "#{" " * $indent}code: #{@code},\n"
112
- str += "#{" " * $indent}parameters: ["
113
- if @parameters.empty?
114
- str += "],\n"
115
- else
116
- $indent += 2
117
- @parameters.each do |param|
118
- str += "\n#{" " * $indent}#{param.class.name.split("::").last}(#{param.rmxp_serialize}),"
119
- end
120
- $indent -= 2
121
- str += "\n#{" " * $indent}],\n"
122
- end
123
- $indent -= 2
124
- str += "#{" " * $indent})"
125
- str
126
- end
127
- end
128
-
129
- class MoveCommand
130
- def rmxp_serialize
131
- str = "MoveCommand(\n"
132
- $indent += 2
133
- str += "#{" " * $indent}code: #{@code},\n"
134
- str += "#{" " * $indent}parameters: ["
135
- $indent += 2
136
- @parameters.each do |param|
137
- str += "\n#{" " * $indent}#{param.class.name.split("::").last}(#{param.rmxp_serialize}),"
138
- end
139
- $indent -= 2
140
- str += "\n#{" " * $indent}],\n"
141
- $indent -= 2
142
- str += "#{" " * $indent})"
143
- str
144
- end
145
- end
146
- end
1
+ # This file handles rusty object notation.
2
+ require_relative "classnames"
3
+
4
+ $indent = 0
5
+
6
+ class Object
7
+ def rmxp_serialize
8
+ $indent += 2
9
+ str = "#{self.class.name.split("::").last}(\n"
10
+ self.instance_variables.each { |var| str += "#{" " * $indent}#{var.to_s.delete("@")}: #{self.instance_variable_get(var).rmxp_serialize},\n" }
11
+ $indent -= 2
12
+ str += "#{" " * $indent})"
13
+ str
14
+ end
15
+ end
16
+
17
+ class Table
18
+ def rmxp_serialize
19
+ str = "Table#{@num_of_dimensions}(\n"
20
+ $indent += 2
21
+ str += "#{" " * $indent}"
22
+ str += case @num_of_dimensions
23
+ when 1
24
+ "xsize: #{@xsize},\n"
25
+ when 2
26
+ "xsize: #{@xsize}, ysize: #{@ysize},\n"
27
+ when 3
28
+ "xsize: #{@xsize}, ysize: #{@ysize}, zsize: #{@zsize},\n"
29
+ end
30
+ str += "#{" " * $indent}data: [\n"
31
+ $indent += 2
32
+ case @num_of_dimensions
33
+ when 1
34
+ str += "#{" " * $indent}"
35
+ @elements.each_with_index do |e, index|
36
+ str += "#{e}, "
37
+ str += "\n#{" " * $indent}" if (index + 1) % 8 == 0
38
+ end
39
+ str += "\n"
40
+ when 2
41
+ @elements.each do |y|
42
+ str += " " * $indent
43
+ y.each { |e| str += "#{e}, " }
44
+ str += "\n"
45
+ end
46
+ when 3
47
+ @elements.each do |z|
48
+ z.each do |y|
49
+ str += " " * $indent
50
+ y.each { |e| str += "#{e}, " }
51
+ str += "\n"
52
+ end
53
+ str += "\n"
54
+ end
55
+ end
56
+ $indent -= 2
57
+ str += "#{" " * $indent}]\n"
58
+ $indent -= 2
59
+ str += "#{" " * $indent})"
60
+ str
61
+ end
62
+ end
63
+
64
+ class Hash
65
+ def rmxp_serialize
66
+ str = "{\n"
67
+ $indent += 2
68
+ self.each { |key, value| str += "#{" " * $indent}#{key.rmxp_serialize}: #{value.rmxp_serialize},\n" }
69
+ $indent -= 2
70
+ str += "#{" " * $indent}}"
71
+ str
72
+ end
73
+ end
74
+
75
+ # BAD BAD
76
+ class NilClass
77
+ def rmxp_serialize
78
+ "None"
79
+ end
80
+ end
81
+
82
+ class String
83
+ def rmxp_serialize
84
+ self.inspect
85
+ end
86
+ end
87
+
88
+ class Array
89
+ def rmxp_serialize
90
+ unless self.empty?
91
+ str = "[\n"
92
+ $indent += 2
93
+ self.each { |e| next unless e; str += "#{" " * $indent}#{e.rmxp_serialize},\n" }
94
+ $indent -= 2
95
+ str += "#{" " * $indent}]"
96
+ else
97
+ str = "[]"
98
+ end
99
+ str
100
+ end
101
+ end
102
+
103
+ module RPG
104
+ class EventCommand
105
+ def rmxp_serialize
106
+ str = "EventCommand(\n"
107
+ $indent += 2
108
+ str += "#{" " * $indent}indent: #{@indent},\n"
109
+ str += "#{" " * $indent}code: #{@code},\n"
110
+ str += "#{" " * $indent}parameters: ["
111
+ if @parameters.empty?
112
+ str += "],\n"
113
+ else
114
+ $indent += 2
115
+ @parameters.each do |param|
116
+ str += "\n#{" " * $indent}#{param.class.name.split("::").last}(#{param.rmxp_serialize}),"
117
+ end
118
+ $indent -= 2
119
+ str += "\n#{" " * $indent}],\n"
120
+ end
121
+ $indent -= 2
122
+ str += "#{" " * $indent})"
123
+ str
124
+ end
125
+ end
126
+
127
+ class MoveCommand
128
+ def rmxp_serialize
129
+ str = "MoveCommand(\n"
130
+ $indent += 2
131
+ str += "#{" " * $indent}code: #{@code},\n"
132
+ str += "#{" " * $indent}parameters: ["
133
+ $indent += 2
134
+ @parameters.each do |param|
135
+ str += "\n#{" " * $indent}#{param.class.name.split("::").last}(#{param.rmxp_serialize}),"
136
+ end
137
+ $indent -= 2
138
+ str += "\n#{" " * $indent}],\n"
139
+ $indent -= 2
140
+ str += "#{" " * $indent})"
141
+ str
142
+ end
143
+ end
144
+ end
@@ -1,85 +1,85 @@
1
- #!/usr/bin/ruby
2
- require "zlib"
3
-
4
- module RMXPExtractor
5
- def self.rpgscript(game_dir, scripts_dir, script_name, extract = false)
6
- # Determine version of game engine
7
- game_data_dir = File.join(game_dir, "Data")
8
- unless Dir.exist? game_data_dir
9
- STDERR.puts "error: #{game_dir} does not have a Data subdirectory"
10
- exit 1
11
- end
12
-
13
- target_path = File.join(game_data_dir, script_name)
14
-
15
- # Generate path of script list
16
- list_path = File.join(scripts_dir, "_scripts.txt")
17
-
18
- if extract
19
- # Make sure the script directory exists
20
- Dir.mkdir(scripts_dir) unless Dir.exists? scripts_dir
21
-
22
- # Keep track of names of scripts extracted so we can warn about duplicates
23
- names = Hash.new(0)
24
-
25
- # Read scripts
26
- File.open(target_path, "rb") do |fin|
27
- File.open(list_path, "w") do |flist|
28
- Marshal.load(fin).each_with_index do |script, index|
29
- name = script[1].strip
30
- data = Zlib::Inflate.inflate(script[2]).rstrip
31
- .gsub(/[ \t]*(?:$|\r\n?)/, "\n")
32
-
33
- # Make sure this file doesn't already exist
34
- if name.empty?
35
- if data.empty? || data == "\n"
36
- flist.puts
37
- next
38
- else
39
- name = "UNTITLED"
40
- end
41
- end
42
-
43
- names[name] += 1
44
- if names[name] > 1
45
- name << " (#{names[name]})"
46
- end
47
-
48
- if data.empty? || data == "\n"
49
- # Treat this like a comment
50
- flist.puts("# " + name)
51
- else
52
- # Write to file order list
53
- flist.puts(name)
54
-
55
- # Write script file
56
- File.open(File.join(scripts_dir, name + ".rb"), "wb") do |fout|
57
- fout.write(data)
58
- end
59
- end
60
- end
61
- end
62
- end
63
- #puts "#{target_path} extracted."
64
- else
65
- # Write scripts
66
- scripts = []
67
-
68
- IO.foreach(list_path) do |name|
69
- name.strip!
70
- next if name.empty? || name.start_with?("#")
71
-
72
- data = File.read(File.join(scripts_dir, name + ".rb")).rstrip.gsub("\n", "\r\n")
73
-
74
- script = Array.new(3)
75
- script[0] = 0
76
- script[1] = name
77
- script[2] = Zlib.deflate(data)
78
- scripts << script
79
- end
80
-
81
- File.open(target_path, "wb") { |f| f.write(Marshal.dump(scripts)) }
82
- #puts "#{target_path} written."
83
- end
84
- end
85
- end
1
+ #!/usr/bin/ruby
2
+ require "zlib"
3
+
4
+ module RMXPExtractor
5
+ def self.rpgscript(game_dir, scripts_dir, script_name, extract = false)
6
+ # Determine version of game engine
7
+ game_data_dir = File.join(game_dir, "Data")
8
+ unless Dir.exist? game_data_dir
9
+ STDERR.puts "error: #{game_dir} does not have a Data subdirectory"
10
+ exit 1
11
+ end
12
+
13
+ target_path = File.join(game_data_dir, script_name)
14
+
15
+ # Generate path of script list
16
+ list_path = File.join(scripts_dir, "_scripts.txt")
17
+
18
+ if extract
19
+ # Make sure the script directory exists
20
+ Dir.mkdir(scripts_dir) unless Dir.exists? scripts_dir
21
+
22
+ # Keep track of names of scripts extracted so we can warn about duplicates
23
+ names = Hash.new(0)
24
+
25
+ # Read scripts
26
+ File.open(target_path, "rb") do |fin|
27
+ File.open(list_path, "w") do |flist|
28
+ Marshal.load(fin).each_with_index do |script, index|
29
+ name = script[1].strip
30
+ data = Zlib::Inflate.inflate(script[2]).rstrip
31
+ .gsub(/[ \t]*(?:$|\r\n?)/, "\n")
32
+
33
+ # Make sure this file doesn't already exist
34
+ if name.empty?
35
+ if data.empty? || data == "\n"
36
+ flist.puts
37
+ next
38
+ else
39
+ name = "UNTITLED"
40
+ end
41
+ end
42
+
43
+ names[name] += 1
44
+ if names[name] > 1
45
+ name << " (#{names[name]})"
46
+ end
47
+
48
+ if data.empty? || data == "\n"
49
+ # Treat this like a comment
50
+ flist.puts("# " + name)
51
+ else
52
+ # Write to file order list
53
+ flist.puts(name)
54
+
55
+ # Write script file
56
+ File.open(File.join(scripts_dir, name + ".rb"), "wb") do |fout|
57
+ fout.write(data)
58
+ end
59
+ end
60
+ end
61
+ end
62
+ end
63
+ #puts "#{target_path} extracted."
64
+ else
65
+ # Write scripts
66
+ scripts = []
67
+
68
+ IO.foreach(list_path) do |name|
69
+ name.strip!
70
+ next if name.empty? || name.start_with?("#")
71
+
72
+ data = File.read(File.join(scripts_dir, name + ".rb")).rstrip.gsub("\n", "\r\n")
73
+
74
+ script = Array.new(3)
75
+ script[0] = 0
76
+ script[1] = name
77
+ script[2] = Zlib.deflate(data)
78
+ scripts << script
79
+ end
80
+
81
+ File.open(target_path, "wb") { |f| f.write(Marshal.dump(scripts)) }
82
+ #puts "#{target_path} written."
83
+ end
84
+ end
85
+ end
@@ -1,3 +1,3 @@
1
- module RMXPExtractor
2
- VERSION = "1.5"
3
- end
1
+ module RMXPExtractor
2
+ VERSION = "1.7"
3
+ end