forked from elizagamedev/mkxp-oneshot
-
Notifications
You must be signed in to change notification settings - Fork 6
/
mklangsrc.rb
executable file
·320 lines (296 loc) · 7.49 KB
/
mklangsrc.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
#!/usr/bin/ruby
require 'zlib'
# Dummy RGSS template for unmarshalling
module RPG
# Useful
class Actor
attr_reader :name
end
class Map
attr_reader :events
end
class MapInfo
attr_reader :name
attr_reader :parent_id
end
class Event
attr_reader :name
attr_reader :pages
class Page
attr_reader :list
class Condition
end
class Graphic
end
end
end
class EventCommand
attr_reader :code
attr_reader :parameters
end
class CommonEvent
attr_reader :name
attr_reader :list
end
class Item
attr_reader :name
attr_reader :description
end
# Useless
class AudioFile
end
class MoveRoute
end
class MoveCommand
end
end
class Table
def self._load(foo)
end
end
class Color
def self._load(foo)
end
end
class Tone
def self._load(foo)
end
end
# Various escape functions
# https://stackoverflow.com/questions/8639642/best-way-to-escape-and-unescape-strings-in-ruby
module Escape
# Ruby
UNESCAPES = {
'a' => "\x07", 'b' => "\x08", 't' => "\x09",
'n' => "\x0a", 'v' => "\x0b", 'f' => "\x0c",
'r' => "\x0d", 'e' => "\x1b", "\\" => "\x5c",
'"' => "\x22", "'" => "\x27"
}
ESCAPES = {
"\x07" => 'a', "\x08" => 'b', "\x09" => 't',
"\x0a" => 'n', "\x0b" => 'v', "\x0c" => 'f',
"\x0d" => 'r', "\x1b" => 'e', "\x5c" => "\\",
"\x22" => '"'
}
def self.unescape(str)
str.gsub(/\\(?:([#{Regexp.escape(UNESCAPES.keys.join)}])|u([\da-fA-F]{4}))|\\0?x([\da-fA-F]{2})/) do
if $1
UNESCAPES[$1]
elsif $2 # escape \u0000 unicode
["#$2".hex].pack('U*')
elsif $3 # escape \0xff or \xff
[$3].pack('H2')
end
end
end
# gettext
def self.escape(str)
str.gsub(/[#{Regexp.escape(ESCAPES.keys.join)}]/) do |m|
"\\" + ESCAPES[m]
end
end
end
class StringList
class Entry
attr_accessor :string
attr_accessor :contexts
attr_accessor :comments
def initialize(string)
@string = string
@contexts = []
@comments = []
end
def add(context, comment)
@contexts << context unless @contexts.include?(context) || context == nil
case comment
when String
@comments << comment unless @comments.include?(comment)
when Array
comment.each do |com|
@comments << com unless @comments.include?(com)
end
end
end
end
def initialize
@strings = Hash.new { |k, v| k[v] = Entry.new(v) }
@order = []
end
def add(string, context, comment)
@order << string unless @strings.include? string
@strings[string].add(context, comment)
end
def dump(filename)
File.open(filename, 'w') do |file|
# Write header
file.puts '# Translation template for OneShot'
file.puts
file.puts 'msgid ""'
file.puts 'msgstr ""'
file.puts
@order.each do |str|
entry = @strings[str]
entry.comments.each do |comment|
file.puts "#. #{comment}"
end
unless entry.contexts.empty?
file.puts "#: #{entry.contexts.join(' ')}"
end
file.puts "msgid \"#{Escape.escape(entry.string)}\""
file.puts "msgstr \"\""
file.puts
end
end
end
end
def load_data(filename)
Marshal.load(File.read(filename))
end
# Script
if ARGV.size < 2
STDERR.puts "usage: mklangsrc.rb game_dir out_file"
exit 1
end
game_dir = ARGV[0]
out_file = ARGV[1]
strlist = StringList.new
# Actors
load_data(File.join(game_dir, 'Data/Actors.rxdata')).each_with_index do |actor, i|
next if !actor || actor.name.empty?
strlist.add(actor.name, "Actors:#{i}", 'actor name')
end
# Items
load_data(File.join(game_dir, 'Data/Items.rxdata')).each_with_index do |item, i|
next if !item || item.name.empty?
strlist.add(item.name, "Items:#{i}", 'item name')
next if item.description.empty?
strlist.add(item.description, "Items:#{i}", 'item description')
end
# Scripts
load_data(File.join(game_dir, 'Data/xScripts.rxdata')).each do |script|
script_name = script[1]
comment = nil
Zlib::Inflate.inflate(script[2]).each_line.with_index do |line, line_num|
# Scan for tr() calls
line.scan(/(?:^|[^_A-Za-z0-9])tr\s*\(\s*(?:'((?:\\.|[^'])*)'|"((?:\\.|[^"])*)")\s*\)/) do |single, double|
string = single || double
string = Escape.unescape(string)
next if string =~ /^\s*$/
strlist.add(string, "Scripts/#{script_name}:#{line_num+1}", comment)
end
# Scan for any comments on this line
m = line.match(/^(?:[^"'#]|"(?:\\"|[^"])*"|'(?:\\'|[^'])*')*(?:#\s*(.*))?$/)
if !m || m.captures.empty?
comment = nil
else
comment = m.captures.first
end
end
end
# Yields all translatable EdText strings
def tr_edtext(script)
# Scan for tr() calls
script.scan(/(?:^|[^_A-Za-z0-9])EdText\.\w+\s*\(\s*(?:'((?:\\.|[^'])*)'|"((?:\\.|[^"])*)")\s*\)/m) do |single, double|
string = single || double
string = Escape.unescape(string).gsub(/\s+/, ' ').gsub(/^\s+/, '').gsub(/\s+$/, '')
next if string =~ /^\s*$/
yield string
end
end
# Events
def parse_event_list(strlist, name, page, list)
# Parse commands
if page == nil
context = name
else
context = "#{name}:#{page}"
end
comment = nil
i = 0
while i < list.size
case list[i].code
when 101
# Message box
string = list[i].parameters[0].rstrip
loop do
i += 1
break unless list[i].code == 401
string << " " << list[i].parameters[0].rstrip
end
string.gsub!(/\s*\\n\s*/, '\\n')
string.strip!
unless string.empty?
strlist.add(string, "#{context}:#{i}:text", comment)
end
comment = nil
when 102
# Choices
list[i].parameters[0].each_with_index do |choice, j|
unless choice.empty?
strlist.add(choice, "#{context}:#{i}:choice:#{j}", comment)
end
end
i += 1
comment = nil
when 108
# Comment
comment = []
loop do
comment << list[i].parameters[0].rstrip
i += 1
break unless list[i].code == 408
end
when 111
# Conditional branch (may contain script text)
if list[i].parameters[0] == 12
tr_edtext(list[i].parameters[1]) do |string|
strlist.add(string, "#{context}:#{i}:condition", comment)
end
end
i += 1
comment = nil
when 355
# Script
script = list[i].parameters[0]
loop do
i += 1
break unless list[i].code == 655
script += "\n" + list[i].parameters[0]
end
tr_edtext(script) do |string|
strlist.add(string, "#{context}:#{i}:script", comment)
end
comment = nil
else
i += 1
comment = nil
end
end
end
# Do common events
load_data(File.join(game_dir, 'Data/CommonEvents.rxdata')).each do |event|
parse_event_list(strlist, 'CommonEvents/' + event.name, nil, event.list) if event
end
# Do map events
map_info = load_data(File.join(game_dir, 'Data/MapInfos.rxdata'))
map_info.each do |map_id, foo|
# Construct full name of map
map_name = ''
i = map_id
begin
map_name.insert(0, "/" + map_info[i].name)
i = map_info[i].parent_id
end while i > 0
# Load map
map = Marshal.load(File.read(File.join(game_dir, sprintf('Data/Map%03d.rxdata', map_id))))
# Parse each event
map.events.sort.each do |_, event|
# Construct full name of event
name = "Maps#{map_name}/#{event.name}"
# Iterate through pages
event.pages.each_with_index do |page, i|
parse_event_list(strlist, name, i + 1, page.list)
end
end
end
strlist.dump(out_file)