-
Notifications
You must be signed in to change notification settings - Fork 2
/
scripts-externalizer.rb
433 lines (413 loc) · 15.8 KB
/
scripts-externalizer.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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
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
# -*- coding: utf-8 -*-
#==============================================================================
# ** scripts-externalizer v1.1.0
#------------------------------------------------------------------------------
# By Joke @biloumaster <[email protected]>
# GitHub: https://github.com/RMEx/scripts-externalizer
#------------------------------------------------------------------------------
# Externalize all scripts from "Data/Scripts.rvdata2" to the given folder
# See the README.md on GitHub!
#==============================================================================
#==============================================================================
# ** CONFIGURATION
#==============================================================================
module XT_CONFIG
EXTRACT_TO = "Scripts" # Extracts the scripts to the folder you want.
# Can be "C:/.../MyScripts/" or "../../MyScripts/"
end
#==============================================================================
# ** FileTools
#------------------------------------------------------------------------------
# Tools for file manipulation
#==============================================================================
module FileTools
#--------------------------------------------------------------------------
# * Extend self
#--------------------------------------------------------------------------
extend self
#--------------------------------------------------------------------------
# * Win32API
#--------------------------------------------------------------------------
CopyFile = Win32API.new('kernel32', 'CopyFile', 'PPI', 'I')
#--------------------------------------------------------------------------
# * Write a file
#--------------------------------------------------------------------------
def write(file, str, flag = "w+")
File.open(file, flag) {|f| f.write(str)}
end
#--------------------------------------------------------------------------
# * Read a file
#--------------------------------------------------------------------------
def read(file)
File.open(file, 'r') { |f| f.read }
end
#--------------------------------------------------------------------------
# * Copy a file
#--------------------------------------------------------------------------
def copy(src, dst)
CopyFile.call(src,dst,0)
end
#--------------------------------------------------------------------------
# * Create a folder
#--------------------------------------------------------------------------
def mkdir(d)
unless Dir.exist?(d)
Dir.mkdir(d)
end
end
#--------------------------------------------------------------------------
# * Remove a folder
#--------------------------------------------------------------------------
def rmdir(d, v=false)
if Dir.exist?(d)
begin delete_all(d)
rescue Errno::ENOTEMPTY
end
end
end
#--------------------------------------------------------------------------
# * Delete all folders
#--------------------------------------------------------------------------
def delete_all(dir)
Dir.foreach(dir) do |e|
next if [".",".."].include? e
fullname = dir + File::Separator + e
if FileTest::directory?(fullname)
delete_all(fullname)
else
File.delete(fullname)
end
end
Dir.delete(dir)
end
end
#==============================================================================
# ** Prompt
#------------------------------------------------------------------------------
# Display prompt
#==============================================================================
module Prompt
#--------------------------------------------------------------------------
# * Extend self
#--------------------------------------------------------------------------
extend self
#--------------------------------------------------------------------------
# * Win32API
#--------------------------------------------------------------------------
FindWindow = Win32API.new('user32', 'FindWindow', 'pp', 'i')
MessageBox = Win32API.new('user32','MessageBox','lppl','i')
HWND = FindWindow.call('RGSS Player', 0)
#--------------------------------------------------------------------------
# * Yes no
#--------------------------------------------------------------------------
def yes_no?(title, message)
k = MessageBox.call(HWND, message, title, 305)
k == 1
end
#--------------------------------------------------------------------------
# * Yes no cancel
#--------------------------------------------------------------------------
def yes_no_cancel?(title, message)
k = MessageBox.call(HWND, message, title, 3)
return :yes if k == 6
return :no if k == 7
:cancel
end
end
#==============================================================================
# ** Externalizer
#------------------------------------------------------------------------------
# Externalize all scripts
#==============================================================================
module Externalizer
#--------------------------------------------------------------------------
# * Extend self
#--------------------------------------------------------------------------
extend self
#--------------------------------------------------------------------------
# * Get Scripts.rvdata2 path from Game.ini
#--------------------------------------------------------------------------
filename = './Game.ini'
section = 'Game'
key = 'Scripts'
buffer = [].pack('x256')
GetPrivateProfileString = Win32API.new('kernel32', 'GetPrivateProfileString', 'ppppip', 'i')
l = GetPrivateProfileString.call(section, key, nil, buffer, buffer.size, filename)
SCRIPTS = buffer[0, l]
#--------------------------------------------------------------------------
# * Get the name of the game from Game.ini
#--------------------------------------------------------------------------
key = 'Title'
buffer = [].pack('x256')
GetPrivateProfileString = Win32API.new('kernel32', 'GetPrivateProfileString', 'ppppip', 'i')
l = GetPrivateProfileString.call(section, key, nil, buffer, buffer.size, filename)
NAME = buffer[0, l]
#--------------------------------------------------------------------------
# * Run externalization
#--------------------------------------------------------------------------
def run
open_rvdata2
return if cancel
return if allready_externalized
return if folder_exist
externalize
rewrite_rvdata2
the_end
exit
end
#--------------------------------------------------------------------------
# * Open Scripts.rvdata2
#--------------------------------------------------------------------------
def open_rvdata2
@scripts = load_data SCRIPTS
n = 1
@scripts.each do |script|
script[2] = Zlib::Inflate.inflate script[2]
@ignored = script if script[2].include?("# ** scripts-externalizer")
script[2] = script[2].split("\r")
if script[2][0] && script[2][0] != "# -*- coding: utf-8 -*-"
script[2] = script[2].insert(0, "# -*- coding: utf-8 -*-\n")
end
script[2] = script[2].join("")
if script[1] == "" && script[2] != ""
script[1] = "untitled (#{n})"
n += 1
end
end
@scripts.delete(@ignored) if @ignored
end
#--------------------------------------------------------------------------
# * Open Scripts.rvdata2
#--------------------------------------------------------------------------
def cancel
msg = "Externalize all scripts to \"#{XT_CONFIG::EXTRACT_TO}\"?
(a backup for \"#{SCRIPTS}\" will be created)"
cp = Prompt.yes_no_cancel?("scripts-externalizer", msg)
if cp != :yes
return true
end
false
end
#--------------------------------------------------------------------------
# * Open Scripts.rvdata2
#--------------------------------------------------------------------------
def allready_externalized
if @scripts.length == 1
msgbox "Scripts are allready externalized... operation canceled."
return true
end
false
end
#--------------------------------------------------------------------------
# * Open Scripts.rvdata2
#--------------------------------------------------------------------------
def folder_exist
dir = XT_CONFIG::EXTRACT_TO.gsub(/\//, '\\')
if Dir.exist?(dir)
msg = "The folder \"#{dir}\" allready exist, do you want to overwrite it?"
cp = Prompt.yes_no_cancel?(NAME, msg)
if cp == :yes
FileTools.rmdir dir
Graphics.update while Dir.exist?(dir)
else
return true
end
end
system("mkdir #{dir}")
Graphics.update until Dir.exist?(dir)
false
end
#--------------------------------------------------------------------------
# * Externalize the scripts
#--------------------------------------------------------------------------
def externalize
@dir = [XT_CONFIG::EXTRACT_TO]
@list = Hash.new
@count = Hash.new
@scripts.each {|s| externalize_script(s)}
@list.each do |path, list|
FileTools.write(path + "/_list.rb", list.join("\n"))
end
FileTools.write(XT_CONFIG::EXTRACT_TO + "/scripts-loader.rb", scripts_loader)
end
#--------------------------------------------------------------------------
# * Externalize one script
#--------------------------------------------------------------------------
def externalize_script(s)
return if is_category? s
return if s[2] == ""
write_script(s)
end
#--------------------------------------------------------------------------
# * Check if the script is a category
#--------------------------------------------------------------------------
def is_category?(s)
if s[1].include?('▼')
s[1] = s[1].delete('▼').strip
@dir = [@dir[0]]
add_category(@dir[0], s[1])
elsif s[1].include?('■')
eval_depth(s[1])
s[1] = s[1].delete('■').strip
add_category(@dir.join("/"), s[1])
else
return false
end
Dir.mkdir(@dir.join("/"))
if s[2] != ""
s[1] = " " * ((@dir.length - 2) * 3) + s[1]
write_script(s)
end
return true
end
#--------------------------------------------------------------------------
# * Add a category into the list
#--------------------------------------------------------------------------
def add_category(path, name)
@list[path] ||= []
if @list[path].include?(name + "/")
@count[path + name + "/"] ||= 1
@count[path + name + "/"] += 1
name = name + " (#{@count[path + name + "/"]})"
end
@list[path] << name + "/"
@dir << name
end
#--------------------------------------------------------------------------
# * Add a script into the list
#--------------------------------------------------------------------------
def add_script(path, name)
@list[path] ||= []
if @list[path].include?(name)
@count[path + name] ||= 1
@count[path + name] += 1
name = name + " (#{@count[path + name]})"
end
@list[path] << name
name
end
#--------------------------------------------------------------------------
# * Eval the depth of the script or repertory (tree)
#--------------------------------------------------------------------------
def eval_depth(name)
depth = (name.length - name.lstrip.length) / 3 + 2
if depth < @dir.length
(@dir.length - depth).times {@dir.pop}
end
end
#--------------------------------------------------------------------------
# * Write a script file (.rb)
#--------------------------------------------------------------------------
def write_script(s)
eval_depth(s[1])
name = s[1].strip
path = @dir.join "/"
name = add_script(path, name)
FileTools.write(path + "/" + name + ".rb", s[2])
end
#--------------------------------------------------------------------------
# * Rewrite the rvdata2
#--------------------------------------------------------------------------
def rewrite_rvdata2
time = Time.now.strftime("%y%m%d-%H%M%S")
new_name = SCRIPTS.split('.').insert(1, "_backup-#{time}.").join('')
FileTools.copy(SCRIPTS, new_name)
new_rvdata = [
[
0, "Load scripts",
deflate("Kernel.send(:load, '#{XT_CONFIG::EXTRACT_TO}/scripts-loader.rb')")
]
]
save_data(new_rvdata, SCRIPTS)
end
#--------------------------------------------------------------------------
# * Compress the content
#--------------------------------------------------------------------------
def deflate(content)
docker = Zlib::Deflate.new(Zlib::BEST_COMPRESSION)
data = docker.deflate(content, Zlib::FINISH)
docker.close
data
end
#--------------------------------------------------------------------------
# * Epic END
#--------------------------------------------------------------------------
def the_end
begin
data_system = load_data("Data/System.rvdata2")
data_system.battle_end_me.play
rescue
end
msgbox "All scripts externalized to \"#{XT_CONFIG::EXTRACT_TO}\" folder! \\o/
Now CLOSE AND OPEN THE PROJECT and enjoy your scripts! :)
Thanks you for using this script! <3
BilouMaster Joke"
end
#--------------------------------------------------------------------------
# * Epic string script
#--------------------------------------------------------------------------
def scripts_loader
"# -*- coding: utf-8 -*-
#==============================================================================
# ** ORMS Converter v1.1.0
#------------------------------------------------------------------------------
# By Joke @biloumaster <[email protected]>
# GitHub: https://github.com/RMEx/scripts-externalizer
#------------------------------------------------------------------------------
# Loads all scripts in the Scripts folder
#
# To add a script: create a newscript.rb in the folder, and add his name
# in the _list.rb
#
# To add a folder: create a new folder, add the name of the folder in _list.rb
# with a \"/\" to the end of the name, create a new _list.rb in the folder
#==============================================================================
module XT_CONFIG
#==============================================================================
# ** CONFIGURATION
#==============================================================================
LOAD_FROM = \"#{XT_CONFIG::EXTRACT_TO}\" # Load the scripts from the folder you want.
# Can be \"C:/.../MyScripts/\" or \"../../MyScripts/\"
end
#==============================================================================
# ** Loader
#------------------------------------------------------------------------------
# Load all scripts
#==============================================================================
module Loader
#--------------------------------------------------------------------------
# * Extend self
#--------------------------------------------------------------------------
extend self
#--------------------------------------------------------------------------
# * Run the loader
#--------------------------------------------------------------------------
def run
read_list(XT_CONFIG::LOAD_FROM + \"/\")
end
#--------------------------------------------------------------------------
# * Read a file
#--------------------------------------------------------------------------
def read(file)
File.open(file, 'r') { |f| f.read }
end
#--------------------------------------------------------------------------
# * Read a list and load all the elements
#--------------------------------------------------------------------------
def read_list(path)
@list = read(path + \"_list.rb\").split(\"\\n\")
@list.each do |e|
e.strip!
next if e[0] == \"#\"
if e[-1] == \"/\"
read_list(path + e)
else
Kernel.send(:load, path + e + \".rb\")
end
end
end
end
Loader.run"
end
end
Externalizer.run