R3EXS 1.1.0 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -3,75 +3,98 @@
3
3
  require 'zlib'
4
4
  require_relative 'ast'
5
5
  require_relative 'utils'
6
- require_relative 'RGSS3_R3EXS'
6
+ require_relative 'logger'
7
7
 
8
8
  module R3EXS
9
+ # 提取 R3EXS 格式的 CommonEvent 文件中的字符串
10
+ #
11
+ # @param target_dir [Pathname] 目标目录
12
+ #
13
+ # @raise [JsonDirError] target_dir 不存在
14
+ # @raise [R3EXSJsonFileError] json 文件不是 R3EXS 模块中的对象
15
+ #
16
+ # @return [Array<String>]
17
+ def self.ex_commonevents(target_dir)
18
+ ex_strings = []
19
+ Utils.all_commonevent_json_files(target_dir, :R3EXS) do |commonevents, commonevents_paths, _|
20
+ commonevents.zip(commonevents_paths).each do |commonevent, commonevents_path|
21
+ ex_strings.concat(commonevent.ex_strings)
22
+ Logger.debug("Extract #{commonevents_path}")
23
+ end
24
+ end
25
+ ex_strings
26
+ end
9
27
 
10
- # 将指定目录下的所有已经序列化为 R3EXS 后的 JOSN 文件中的字符串提取出来
11
- #
12
- # @param target_dir [Pathname] 目标目录
13
- # @param output_dir [Pathname] 输出目录
14
- # @param with_scripts [Boolean] 是否包含脚本
15
- # @param with_symbol [Boolean] 是否包含脚本中的符号
16
- # @param with_scripts_separate [Boolean] 是否将脚本提取的字符串单独存放
17
- #
18
- # @raise [R3EXSJsonFileError] json 文件不是 R3EXS 模块中的对象
19
- # @raise [JsonDirError] target_dir 不存在
20
- #
21
- # @return [void]
22
- def R3EXS.ex_strings(target_dir, output_dir, with_scripts, with_symbol, with_scripts_separate)
23
- all_ex_strings = []
24
-
25
- # 处理常规的 JSON 文件
26
- Utils.all_json_files(target_dir, :R3EXS) do |object, file_basename, parent_relative_dir|
27
- file_path = target_dir.join(parent_relative_dir, "#{file_basename}.json")
28
- print "#{Utils::ESCAPE}#{Utils::MAGENTA_COLOR}Extracting from #{Utils::RESET_COLOR}#{file_path}...\r" if $global_options[:verbose]
29
- if object.is_a?(Array)
30
- object.each do |obj|
31
- all_ex_strings.concat(obj.ex_strings)
32
- end
33
- else
34
- all_ex_strings.concat(object.ex_strings)
35
- end
36
- print "#{Utils::ESCAPE}#{Utils::GREEN_COLOR}Extracted #{Utils::RESET_COLOR}#{file_path}\n" if $global_options[:verbose]
37
- end
38
-
39
- # 处理 CommonEvent_\d{5}.json 文件
40
- Utils.all_commonevent_json_files(target_dir, :R3EXS) do |commonevents, commonevents_basenames, parent_relative_dir|
41
- commonevents.zip(commonevents_basenames).each do |commonevent, commonevent_basename|
42
- file_path = target_dir.join(parent_relative_dir, "#{commonevent_basename}.json")
43
- print "#{Utils::ESCAPE}#{Utils::MAGENTA_COLOR}Extracting from #{Utils::RESET_COLOR}#{file_path}...\r" if $global_options[:verbose]
44
- all_ex_strings.concat(commonevent.ex_strings)
45
- print "#{Utils::ESCAPE}#{Utils::GREEN_COLOR}Extracted #{Utils::RESET_COLOR}#{file_path}\n" if $global_options[:verbose]
46
- end
47
- end
28
+ # 提取 Ruby 源码中的字符串
29
+ #
30
+ # @param target_dir [Pathname] 目标目录
31
+ # @param with_symbol [Boolean] 是否包含脚本中的符号
32
+ #
33
+ # @raise [JsonDirError] target_dir 不存在
34
+ #
35
+ # @return [Array<String>]
36
+ def self.ex_scripts(target_dir, with_symbol)
37
+ ex_strings = []
38
+ Utils.all_rb_files(target_dir) do |scripts, _, scripts_paths, _, _|
39
+ scripts.zip(scripts_paths).each do |script, scripts_path|
40
+ ex_strings.concat(StringsExtractor.extract(script, with_symbol))
41
+ Logger.debug("Extract #{scripts_path}")
42
+ end
43
+ end
44
+ ex_strings
45
+ end
48
46
 
49
- # 处理 \d{5}.rb 文件
50
- if with_scripts
51
- all_rb_strings = []
52
- strings_extractor = StringsExtractor.new(all_rb_strings, with_symbol)
47
+ # 提取 R3EXS 格式的 常规 JSON 文件中的字符串
48
+ #
49
+ # @param target_dir [Pathname] 目标目录
50
+ #
51
+ # @raise [JsonDirError] target_dir 不存在
52
+ # @raise [R3EXSJsonFileError] json 文件不是 R3EXS 模块中的对象
53
+ #
54
+ # @return [Array<String>]
55
+ def self.ex_regular(target_dir)
56
+ ex_strings = []
57
+ Utils.all_regular_json_files(target_dir, :R3EXS) do |obj, file_path|
58
+ ex_strings.concat(Utils.ex_r3exs(obj))
59
+ Logger.debug("Extract #{file_path}")
60
+ end
61
+ ex_strings
62
+ end
53
63
 
54
- Utils.all_rb_files(target_dir) do |scripts, scripts_basenames, parent_relative_dir|
55
- scripts.zip(scripts_basenames).each do |script, script_basename|
56
- file_path = target_dir.join(parent_relative_dir, "#{script_basename}.rb")
57
- print "#{Utils::ESCAPE}#{Utils::MAGENTA_COLOR}Extracting from #{Utils::RESET_COLOR}#{file_path}...\r" if $global_options[:verbose]
58
- strings_extractor.visit(Prism.parse(script).value)
59
- print "#{Utils::ESCAPE}#{Utils::GREEN_COLOR}Extracted #{Utils::RESET_COLOR}#{file_path}\n" if $global_options[:verbose]
60
- end
61
- end
64
+ # 提取指定目录下 R3EXS 格式的 JOSN 文件中的字符串
65
+ #
66
+ # @param target_dir [Pathname] 目标目录
67
+ # @param output_dir [Pathname] 输出目录
68
+ # @param with_scripts [Boolean] 是否包含脚本
69
+ # @param with_symbol [Boolean] 是否包含脚本中的符号
70
+ # @param with_scripts_separate [Boolean] 是否将脚本提取的字符串单独存放
71
+ #
72
+ # @raise [JsonDirError] target_dir 不存在
73
+ # @raise [R3EXSJsonFileError] json 文件不是 R3EXS 模块中的对象
74
+ #
75
+ # @return [void]
76
+ def self.ex_strings(target_dir, output_dir, with_scripts, with_symbol, with_scripts_separate)
77
+ all_ex_strings = []
62
78
 
63
- if with_scripts_separate
64
- # 去除 nil 元素
65
- all_rb_strings.compact!
66
- Utils.object_json(all_rb_strings.each_with_object({}) { |item, h| h[item] = item }, output_dir.join('ManualTransFile_scripts.json'))
67
- else
68
- all_ex_strings.concat(all_rb_strings)
69
- end
70
- end
79
+ # 处理 CommonEvent_\d{5}.json 文件
80
+ all_ex_strings.concat(ex_commonevents(target_dir))
71
81
 
72
- # 去除 nil 元素
73
- all_ex_strings.compact!
74
- Utils.object_json(all_ex_strings.each_with_object({}) { |item, h| h[item] = item }, output_dir.join('ManualTransFile.json'))
82
+ # 处理 Script_\d{3}.rb 文件
83
+ if with_scripts
84
+ rb_strings = ex_scripts(target_dir, with_symbol)
85
+ # 单独输出脚本提取的字符串
86
+ if with_scripts_separate
87
+ Utils.object_json(rb_strings.to_h { |str| [str, str] }, output_dir.join('ManualTransFile_scripts.json'))
88
+ else
89
+ all_ex_strings.concat(rb_strings)
90
+ end
75
91
  end
76
92
 
77
- end
93
+ # 处理常规 JSON 文件
94
+ all_ex_strings.concat(ex_regular(target_dir))
95
+
96
+ # 去除 nil 元素
97
+ all_ex_strings.compact!
98
+ Utils.object_json(all_ex_strings.to_h { |str| [str, str] }, output_dir.join('ManualTransFile.json'))
99
+ end
100
+ end
@@ -3,72 +3,94 @@
3
3
  require 'zlib'
4
4
  require_relative 'ast'
5
5
  require_relative 'utils'
6
- require_relative 'RGSS3_R3EXS'
6
+ require_relative 'logger'
7
7
 
8
8
  module R3EXS
9
+ # 注入 R3EXS 格式的 CommonEvent 文件中的字符串
10
+ #
11
+ # @param target_dir [Pathname] 目标目录
12
+ # @param output_dir [Pathname] 输出目录
13
+ # @param manual_trans_hash[Hash{String => String}] 翻译结果
14
+ #
15
+ # @raise [JsonDirError] target_dir 不存在
16
+ # @raise [R3EXSJsonFileError] json 文件不是 R3EXS 模块中的对象
17
+ #
18
+ # @return [void]
19
+ def self.in_commonevents(target_dir, output_dir, manual_trans_hash)
20
+ Utils.all_commonevent_json_files(target_dir, :R3EXS) do |commonevents, commonevents_paths, _|
21
+ commonevents.zip(commonevents_paths).each do |commonevent, commonevents_path|
22
+ file_path = output_dir.join(commonevents_path.relative_path_from(target_dir))
23
+ commonevent.in_strings(manual_trans_hash)
24
+ Utils.object_json(commonevent, file_path)
25
+ Logger.debug("Inject #{file_path}")
26
+ end
27
+ end
28
+ end
9
29
 
10
- # 将指定目录下的所有已经序列化为 R3EXS 后的 JOSN 文件按照 ManualTransFile.json 翻译注入
11
- #
12
- # @param target_dir [Pathname] 目标目录
13
- # @param output_dir [Pathname] 输出目录
14
- # @param manualtransfile_path [Pathname] ManualTransFile.json 文件路径
15
- # @param with_scripts [Boolean] 是否包含脚本
16
- #
17
- # @raise [R3EXSJsonFileError] json 文件不是 R3EXS 模块中的对象
18
- # @raise [JsonDirError] target_dir 不存在
19
- # @raise [ScriptsInfoPathError] Scripts_info.json 文件不存在
20
- # @raise [ManualTransFilePath] ManualTransFile.json 不存在
21
- #
22
- # @return [void]
23
- def R3EXS.in_strings(target_dir, output_dir, manualtransfile_path, with_scripts)
24
- manualtransfile_path.exist? or raise ManualTransFilePathError.new(manualtransfile_path.to_s), "ManualTransFile.json not found: #{manualtransfile_path}"
25
-
26
- manual_trans_hash = Oj.load_file(manualtransfile_path.to_s)
27
-
28
- # 处理常规的 JSON 文件
29
- Utils.all_json_files(target_dir, :R3EXS) do |object, file_basename, parent_relative_dir|
30
- file_path = output_dir.join(parent_relative_dir, "#{file_basename}.json")
31
- print "#{Utils::ESCAPE}#{Utils::MAGENTA_COLOR}Injecting to #{Utils::RESET_COLOR}#{file_path}...\r" if $global_options[:verbose]
32
- if object.is_a?(Array)
33
- object.each do |obj|
34
- obj.in_strings(manual_trans_hash)
35
- end
36
- else
37
- object.in_strings(manual_trans_hash)
38
- end
39
- Utils.object_json(object, file_path)
40
- print "#{Utils::ESCAPE}#{Utils::GREEN_COLOR}Injected #{Utils::RESET_COLOR}#{file_path}\n" if $global_options[:verbose]
41
- end
30
+ # 注入 Ruby 源码中的字符串
31
+ #
32
+ # @param target_dir [Pathname] 目标目录
33
+ # @param output_dir [Pathname] 输出目录
34
+ # @param manual_trans_hash[Hash{String => String}] 翻译结果
35
+ #
36
+ # @raise [JsonDirError] target_dir 不存在
37
+ # @raise [ScriptsInfoPathError] Scripts_info.json 不存在
38
+ #
39
+ # @return [void]
40
+ def self.in_scripts(target_dir, output_dir, manual_trans_hash)
41
+ Utils.all_rb_files(target_dir) do |scripts, script_info, scripts_paths, script_info_path, _|
42
+ scripts.zip(scripts_paths).each do |script, scripts_path|
43
+ file_path = output_dir.join(scripts_path.relative_path_from(target_dir))
44
+ Utils.script_rb(StringsInjector.inject(script, manual_trans_hash), file_path)
45
+ Logger.debug("Inject #{file_path}")
46
+ end
47
+ Utils.object_json(script_info, output_dir.join(script_info_path.relative_path_from(target_dir)))
48
+ end
49
+ end
42
50
 
43
- # 处理 CommonEvent_\d{5}.json 文件
44
- Utils.all_commonevent_json_files(target_dir, :R3EXS) do |commonevents, commonevents_basenames, parent_relative_dir|
45
- commonevents.zip(commonevents_basenames).each do |commonevent, commonevent_basename|
46
- file_path = output_dir.join(parent_relative_dir, "#{commonevent_basename}.json")
47
- print "#{Utils::ESCAPE}#{Utils::MAGENTA_COLOR}Injecting to #{Utils::RESET_COLOR}#{file_path}...\r" if $global_options[:verbose]
48
- commonevent.in_strings(manual_trans_hash)
49
- Utils.object_json(commonevent, file_path)
50
- print "#{Utils::ESCAPE}#{Utils::GREEN_COLOR}Injected #{Utils::RESET_COLOR}#{file_path}\n" if $global_options[:verbose]
51
- end
52
- end
51
+ # 注入 R3EXS 格式的 常规 JSON 文件中的字符串
52
+ #
53
+ # @param target_dir [Pathname] 目标目录
54
+ # @param output_dir [Pathname] 输出目录
55
+ # @param manual_trans_hash[Hash{String => String}] 翻译结果
56
+ #
57
+ # @raise [JsonDirError] target_dir 不存在
58
+ # @raise [R3EXSJsonFileError] json 文件不是 R3EXS 模块中的对象
59
+ #
60
+ # @return [void]
61
+ def self.in_regular(target_dir, output_dir, manual_trans_hash)
62
+ Utils.all_regular_json_files(target_dir, :R3EXS) do |obj, json_path|
63
+ file_path = output_dir.join(json_path.relative_path_from(target_dir))
64
+ Utils.in_r3exs(obj, manual_trans_hash)
65
+ Utils.object_json(obj, file_path)
66
+ Logger.debug("Inject #{file_path}")
67
+ end
68
+ end
53
69
 
54
- # 处理 \d{5}.rb 文件
55
- if with_scripts
56
- Utils.all_rb_files(target_dir) do |scripts, scripts_basenames, parent_relative_dir|
57
- full_output_dir = output_dir.join(parent_relative_dir)
58
- full_output_dir.mkdir unless full_output_dir.exist?
70
+ # 将指定目录下 R3EXS 格式的 JOSN 文件按照 ManualTransFile.json 翻译注入字符串
71
+ #
72
+ # @param target_dir [Pathname] 目标目录
73
+ # @param output_dir [Pathname] 输出目录
74
+ # @param manualtransfile_path [Pathname] ManualTransFile.json 文件路径
75
+ # @param with_scripts [Boolean] 是否包含脚本
76
+ #
77
+ # @raise [ManualTransFilePath] ManualTransFile.json 不存在
78
+ # @raise [JsonDirError] target_dir 不存在
79
+ # @raise [R3EXSJsonFileError] json 文件不是 R3EXS 模块中的对象
80
+ # @raise [ScriptsInfoPathError] Scripts_info.json 文件不存在
81
+ #
82
+ # @return [void]
83
+ def self.in_strings(target_dir, output_dir, manualtransfile_path, with_scripts)
84
+ manualtransfile_path.exist? or raise ManualTransFilePathError, "ManualTransFile.json not found: #{manualtransfile_path}"
85
+ manual_trans_hash = Oj.load_file(manualtransfile_path.to_s)
59
86
 
60
- script_info_file_path = target_dir.join(parent_relative_dir, 'Scripts_info.json')
61
- script_info_file_path.exist? or raise ScriptsInfoPathError.new(script_info_file_path.to_s), "Scripts_info.json not found: #{script_info_file_path}"
62
- # 记得把 Scripts_info.json 也复制过去
63
- FileUtils.cp(script_info_file_path, full_output_dir)
87
+ # 处理 CommonEvent_\d{5}.json 文件
88
+ in_commonevents(target_dir, output_dir, manual_trans_hash)
64
89
 
65
- scripts.zip(scripts_basenames).each do |script, script_basename|
66
- output_file_path = full_output_dir.join("#{script_basename}.rb")
67
- output_file_path.binwrite(StringsInjector.new(manual_trans_hash).rewrite(script, Prism.parse(script).value))
68
- print "#{Utils::ESCAPE}#{Utils::GREEN_COLOR}Injected #{Utils::RESET_COLOR}#{output_file_path}\n" if $global_options[:verbose]
69
- end
70
- end
71
- end
72
- end
90
+ # 处理 Script_\d{3}.rb 文件
91
+ in_scripts(target_dir, output_dir, manual_trans_hash) if with_scripts
73
92
 
74
- end
93
+ # 处理常规的 JSON 文件
94
+ in_regular(target_dir, output_dir, manual_trans_hash)
95
+ end
96
+ end
@@ -2,115 +2,128 @@
2
2
 
3
3
  require 'zlib'
4
4
  require_relative 'utils'
5
- require_relative 'RGSS3_R3EXS'
5
+ require_relative 'logger'
6
6
 
7
7
  module R3EXS
8
-
9
- # 将指定目录下的所有JSON文件转换为 rvdata2 文件
10
- #
11
- # @param [Pathname] target_dir 目标目录
12
- # @param [Pathname] output_dir 输出目录
13
- # @param [Pathname] original_dir 原始 rvdata2 文件目录
14
- # @param [Boolean] complete 是否完全转换
15
- # @param [Boolean] with_scripts 是否转换Scripts
16
- #
17
- # @raise [RPGJsonFileError] json 文件不是 RPG 模块中的对象
18
- # @raise [R3EXSJsonFileError] json 文件不是 R3EXS 模块中的对象
19
- # @raise [Rvdata2FileError] 原始 rvdata2 文件可能损坏
20
- # @raise [JsonDirError] target_dir 不存在
21
- # @raise [Rvdata2DirError] original_dir 不存在
22
- # @raise [ScriptsInfoPathError] Scripts_info.json 文件不存在
23
- #
24
- # @return [void]
25
- def R3EXS.json_rvdata2(target_dir, output_dir, original_dir, complete, with_scripts)
26
- # 处理常规的 JSON 文件
27
- Utils.all_json_files(target_dir, (complete) ? :RPG : :R3EXS) do |object, file_basename, parent_relative_dir|
28
- output_file_path = output_dir.join(parent_relative_dir, "#{file_basename}.rvdata2")
29
- if complete
30
- print "#{Utils::ESCAPE}#{Utils::MAGENTA_COLOR}Serializing to #{Utils::RESET_COLOR}#{output_file_path}...\r" if $global_options[:verbose]
31
- Utils.object_rvdata2(object, output_file_path)
32
- else
33
- # 检查 original_dir 是否存在
34
- original_dir.exist? or raise Rvdata2DirError.new(original_dir.to_s), "Original rvdata2 directory not found: #{original_dir}"
35
- original_file_path = original_dir.join(parent_relative_dir, "#{file_basename}.rvdata2")
36
- print "#{Utils::ESCAPE}#{Utils::BLUE_COLOR}Reading and Deserializing #{Utils::RESET_COLOR}#{original_file_path}...\r" if $global_options[:verbose]
37
- original_object = Marshal.load(original_file_path.binread)
38
-
39
- # 这里的类型检查要用紧凑模式,因为 rvdata2 文件中可能存在 nil 元素,必须忽略
40
- begin
41
- Utils.check_type(original_object, file_basename, true, :RPG)
42
- rescue RPGTypeError
43
- raise Rvdata2FileError.new(original_file_path.to_s), "Invalid rvdata2 file: #{original_file_path}"
44
- end
45
-
46
- print "#{Utils::ESCAPE}#{Utils::MAGENTA_COLOR}Serializing to #{Utils::RESET_COLOR}#{output_file_path}...\r" if $global_options[:verbose]
47
-
48
- # 根据是否为数组进行不同的处理
49
- if object.is_a?(Array)
50
- object.each do |obj|
51
- obj.inject_to(original_object[obj.index])
52
- end
53
- else
54
- object.inject_to(original_object)
55
- end
56
- Utils.object_rvdata2(original_object, output_file_path)
57
- end
58
- print "#{Utils::ESCAPE}#{Utils::GREEN_COLOR}Serialized #{Utils::RESET_COLOR}#{output_file_path}\n" if $global_options[:verbose]
59
- end
60
-
61
- # 处理 CommonEvent_\d{5}.json 文件
62
- Utils.all_commonevent_json_files(target_dir, (complete) ? :RPG : :R3EXS) do |commonevents, _, parent_relative_dir|
63
- output_file_path = output_dir.join(parent_relative_dir.parent, 'CommonEvents.rvdata2')
64
- if complete
65
- print "#{Utils::ESCAPE}#{Utils::MAGENTA_COLOR}Serializing to #{Utils::RESET_COLOR}#{output_file_path}...\r" if $global_options[:verbose]
66
- Utils.object_rvdata2(commonevents, output_file_path)
67
- else
68
- # 检查 original_dir 是否存在
69
- original_dir.exist? or raise Rvdata2DirError.new(original_dir.to_s), "Original rvdata2 directory not found: #{original_dir}"
70
- original_file_path = original_dir.join(parent_relative_dir.parent, 'CommonEvents.rvdata2')
71
- print "#{Utils::ESCAPE}#{Utils::BLUE_COLOR}Reading and Deserializing #{Utils::RESET_COLOR}#{original_file_path}...\r" if $global_options[:verbose]
72
- original_object = Marshal.load(original_file_path.binread)
73
-
74
- # 这里的类型检查要用紧凑模式,因为 rvdata2 文件中可能存在 nil 元素,必须忽略
75
- begin
76
- Utils.check_type(original_object, 'CommonEvents', true, :RPG)
77
- rescue RPGTypeError
78
- raise Rvdata2FileError.new(original_file_path.to_s), "Invalid rvdata2 file: #{original_file_path}"
79
- end
80
-
81
- print "#{Utils::ESCAPE}#{Utils::MAGENTA_COLOR}Serializing to #{Utils::RESET_COLOR}#{output_file_path}...\r" if $global_options[:verbose]
82
-
83
- commonevents.each do |commonevent|
84
- commonevent.inject_to(original_object[commonevent.index])
85
- end
86
-
87
- Utils.object_rvdata2(original_object, output_file_path)
88
- end
89
- print "#{Utils::ESCAPE}#{Utils::GREEN_COLOR}Serialized #{Utils::RESET_COLOR}#{output_file_path}\n" if $global_options[:verbose]
90
- end
91
-
92
- # 处理 \d{5}.rb 文件
93
- if with_scripts
94
- Utils.all_rb_files(target_dir) do |scripts, _, parent_relative_dir|
95
- output_file_path = output_dir.join(parent_relative_dir.parent, 'Scripts.rvdata2')
96
- script_info_file_path = target_dir.join(parent_relative_dir, 'Scripts_info.json')
97
- script_info_file_path.exist? or raise ScriptsInfoPathError.new(script_info_file_path.to_s), "Scripts_info.json not found: #{script_info_file_path}"
98
-
99
- print "#{Utils::ESCAPE}#{Utils::MAGENTA_COLOR}Serializing to #{Utils::RESET_COLOR}#{output_file_path}...\r" if $global_options[:verbose]
100
-
101
- print "#{Utils::ESCAPE}#{Utils::YELLOW_COLOR}Reading from #{Utils::RESET_COLOR}#{script_info_file_path}...\r" if $global_options[:verbose]
102
- scripts_info_array = Oj.load_file(script_info_file_path.to_s)
103
-
104
- output_scripts_array = []
105
- scripts_info_array.each do |script_info|
106
- index = script_info[:index]
107
- script = scripts[index]
108
- output_scripts_array << [114514, script_info[:name], Zlib::Deflate.deflate(script)]
109
- end
110
- Utils.object_rvdata2(output_scripts_array, output_file_path)
111
- print "#{Utils::ESCAPE}#{Utils::GREEN_COLOR}Serialized #{Utils::RESET_COLOR}#{output_file_path}\n" if $global_options[:verbose]
112
- end
113
- end
8
+ # 将 CommonEvent 文件反序列化为 rvdata2 文件
9
+ #
10
+ # @param target_dir [Pathname] 目标目录
11
+ # @param output_dir [Pathname] 输出目录
12
+ # @param original_dir [Pathname] 原始 rvdata2 文件目录
13
+ # @param complete [Boolean] 是否完全转换
14
+ #
15
+ # @raise [JsonDirError] target_dir 不存在
16
+ # @raise [RPGJsonFileError] json 文件不是 RPG 模块中的对象
17
+ # @raise [R3EXSJsonFileError] json 文件不是 R3EXS 模块中的对象
18
+ # @raise [Rvdata2PathError] 原始 rvdata2 文件不存在
19
+ #
20
+ # @return [void]
21
+ def self.commonevents_rvdata2(target_dir, output_dir, original_dir, complete)
22
+ if complete
23
+ Utils.all_commonevent_json_files(target_dir, :RPG) do |commonevents, _, rvdata2_file_path|
24
+ file_path = output_dir.join(rvdata2_file_path.relative_path_from(target_dir))
25
+ Utils.object_rvdata2(commonevents, file_path)
26
+ Logger.debug("Serialize #{file_path}")
27
+ end
28
+ else
29
+ Utils.all_commonevent_json_files(target_dir, :R3EXS) do |commonevents, _, rvdata2_file_path|
30
+ file_path = output_dir.join(rvdata2_file_path.relative_path_from(target_dir))
31
+ original_file_path = original_dir.join(rvdata2_file_path.relative_path_from(target_dir))
32
+ # 检查 original_file_path 是否存在
33
+ original_file_path.exist? or raise Rvdata2PathError, "Original rvdata2 file not found: #{original_file_path}"
34
+
35
+ original_commonevents = Marshal.load(original_file_path.binread)
36
+ Logger.debug("Deserialize #{original_file_path}")
37
+ Utils.r3exs_rpg(commonevents, original_commonevents)
38
+ Utils.object_rvdata2(original_commonevents, file_path)
39
+ Logger.debug("Serialize #{file_path}")
40
+ end
114
41
  end
115
-
116
- end
42
+ end
43
+
44
+ # 将 Ruby 源码反序列化为 rvdata2 文件
45
+ #
46
+ # @param target_dir [Pathname] 目标目录
47
+ # @param output_dir [Pathname] 输出目录
48
+ #
49
+ # @raise [JsonDirError] target_dir 不存在
50
+ # @raise [ScriptsInfoPathError] Scripts_info.json 不存在
51
+ #
52
+ # @return [void]
53
+ def self.scripts_rvdata2(target_dir, output_dir)
54
+ Utils.all_rb_files(target_dir) do |scripts, script_info, _, _, rvdata2_file_path|
55
+ file_path = output_dir.join(rvdata2_file_path.relative_path_from(target_dir))
56
+
57
+ output_scripts = []
58
+ script_info.each do |info|
59
+ index = info[:index]
60
+ script = scripts[index]
61
+ output_scripts << [114_514, info[:name], Zlib::Deflate.deflate(script)]
62
+ end
63
+ Utils.object_rvdata2(output_scripts, file_path)
64
+ Logger.debug("Serialize #{file_path}")
65
+ end
66
+ end
67
+
68
+ # 将常规 JSON 文件反序列化为 rvdata2 文件
69
+ #
70
+ # @param target_dir [Pathname] 目标目录
71
+ # @param output_dir [Pathname] 输出目录
72
+ # @param original_dir [Pathname] 原始 rvdata2 文件目录
73
+ # @param complete [Boolean] 是否完全转换
74
+ #
75
+ # @raise [JsonDirError] target_dir 不存在
76
+ # @raise [RPGJsonFileError] json 文件不是 RPG 模块中的对象
77
+ # @raise [R3EXSJsonFileError] json 文件不是 R3EXS 模块中的对象
78
+ # @raise [Rvdata2PathError] 原始 rvdata2 文件不存在
79
+ #
80
+ # @return [void]
81
+ def self.regular_rvdata2(target_dir, output_dir, original_dir, complete)
82
+ if complete
83
+ Utils.all_regular_json_files(target_dir, :RPG) do |obj, obj_path|
84
+ file_path = output_dir.join(obj_path.relative_path_from(target_dir)).sub_ext('.rvdata2')
85
+ Utils.object_rvdata2(obj, file_path)
86
+ Logger.debug("Serialize #{file_path}")
87
+ end
88
+ else
89
+ Utils.all_regular_json_files(target_dir, :R3EXS) do |obj, obj_path|
90
+ file_path = output_dir.join(obj_path.relative_path_from(target_dir)).sub_ext('.rvdata2')
91
+ original_file_path = original_dir.join(obj_path.relative_path_from(target_dir)).sub_ext('.rvdata2')
92
+ # 检查 original_file_path 是否存在
93
+ original_file_path.exist? or raise Rvdata2PathError, "Original rvdata2 file not found: #{original_file_path}"
94
+
95
+ original_object = Marshal.load(original_file_path.binread)
96
+ Logger.debug("Deserialize #{original_file_path}")
97
+ Utils.r3exs_rpg(obj, original_object)
98
+ Utils.object_rvdata2(original_object, file_path)
99
+ Logger.debug("Serialize #{file_path}")
100
+ end
101
+ end
102
+ end
103
+
104
+ # 将指定目录下的所有JSON文件反序列化为 rvdata2 文件
105
+ #
106
+ # @param target_dir [Pathname] 目标目录
107
+ # @param output_dir [Pathname] 输出目录
108
+ # @param original_dir [Pathname] 原始 rvdata2 文件目录
109
+ # @param complete [Boolean] 是否完全转换
110
+ # @param with_scripts [Boolean] 是否转换Scripts
111
+ #
112
+ # @raise [JsonDirError] target_dir 不存在
113
+ # @raise [RPGJsonFileError] json 文件不是 RPG 模块中的对象
114
+ # @raise [R3EXSJsonFileError] json 文件不是 R3EXS 模块中的对象
115
+ # @raise [Rvdata2PathError] 原始 rvdata2 文件不存在
116
+ # @raise [ScriptsInfoPathError] Scripts_info.json 文件不存在
117
+ #
118
+ # @return [void]
119
+ def self.json_rvdata2(target_dir, output_dir, original_dir, complete, with_scripts)
120
+ # 处理 CommonEvent_\d{5}.json 文件
121
+ commonevents_rvdata2(target_dir, output_dir, original_dir, complete)
122
+
123
+ # 处理 Script_\d{3}.rb 文件
124
+ scripts_rvdata2(target_dir, output_dir) if with_scripts
125
+
126
+ # 处理常规 JSON 文件
127
+ regular_rvdata2(target_dir, output_dir, original_dir, complete)
128
+ end
129
+ end
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+
3
+ module R3EXS
4
+ # 一个简单的 Logger
5
+ class Logger
6
+ # Low-level information, mostly for developers.
7
+ DEBUG = 0
8
+ # Generic (useful) information about system operation.
9
+ INFO = 1
10
+ # A warning.
11
+ WARN = 2
12
+
13
+ @level = INFO
14
+
15
+ class << self
16
+ attr_accessor :level
17
+
18
+ def debug(message)
19
+ logger(DEBUG, message) if level <= DEBUG
20
+ end
21
+
22
+ def info(message)
23
+ logger(INFO, message) if level <= INFO
24
+ end
25
+
26
+ def warn(message)
27
+ logger(WARN, message) if level <= WARN
28
+ end
29
+
30
+ def logger(level, message)
31
+ case level
32
+ when DEBUG
33
+ puts("#{Time.now.strftime('%Y-%m-%d %H:%M:%S')} [DEBUG]: #{message}")
34
+ when INFO
35
+ puts("#{Time.now.strftime('%Y-%m-%d %H:%M:%S')} [INFO]: #{message}")
36
+ when WARN
37
+ ::Kernel.warn("#{Time.now.strftime('%Y-%m-%d %H:%M:%S')} [WARN]: #{message}")
38
+ else
39
+ ::Kernel.warn("#{Time.now.strftime('%Y-%m-%d %H:%M:%S')} [UNKNOWN]: #{message}")
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end