R3EXS 1.1.1 → 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,74 +3,98 @@
3
3
  require 'zlib'
4
4
  require_relative 'ast'
5
5
  require_relative 'utils'
6
+ require_relative 'logger'
6
7
 
7
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
8
27
 
9
- # 将指定目录下的所有已经序列化为 R3EXS 后的 JOSN 文件中的字符串提取出来
10
- #
11
- # @param target_dir [Pathname] 目标目录
12
- # @param output_dir [Pathname] 输出目录
13
- # @param with_scripts [Boolean] 是否包含脚本
14
- # @param with_symbol [Boolean] 是否包含脚本中的符号
15
- # @param with_scripts_separate [Boolean] 是否将脚本提取的字符串单独存放
16
- #
17
- # @raise [R3EXSJsonFileError] json 文件不是 R3EXS 模块中的对象
18
- # @raise [JsonDirError] target_dir 不存在
19
- #
20
- # @return [void]
21
- def R3EXS.ex_strings(target_dir, output_dir, with_scripts, with_symbol, with_scripts_separate)
22
- all_ex_strings = []
23
-
24
- # 处理常规的 JSON 文件
25
- Utils.all_json_files(target_dir, :R3EXS) do |object, file_basename, parent_relative_dir|
26
- file_path = target_dir.join(parent_relative_dir, "#{file_basename}.json")
27
- print "#{Utils::ESCAPE}#{Utils::MAGENTA_COLOR}Extracting from #{Utils::RESET_COLOR}#{file_path}...\r" if $global_options[:verbose]
28
- if object.is_a?(Array)
29
- object.each do |obj|
30
- all_ex_strings.concat(obj.ex_strings)
31
- end
32
- else
33
- all_ex_strings.concat(object.ex_strings)
34
- end
35
- print "#{Utils::ESCAPE}#{Utils::GREEN_COLOR}Extracted #{Utils::RESET_COLOR}#{file_path}\n" if $global_options[:verbose]
36
- end
37
-
38
- # 处理 CommonEvent_\d{5}.json 文件
39
- Utils.all_commonevent_json_files(target_dir, :R3EXS) do |commonevents, commonevents_basenames, parent_relative_dir|
40
- commonevents.zip(commonevents_basenames).each do |commonevent, commonevent_basename|
41
- file_path = target_dir.join(parent_relative_dir, "#{commonevent_basename}.json")
42
- print "#{Utils::ESCAPE}#{Utils::MAGENTA_COLOR}Extracting from #{Utils::RESET_COLOR}#{file_path}...\r" if $global_options[:verbose]
43
- all_ex_strings.concat(commonevent.ex_strings)
44
- print "#{Utils::ESCAPE}#{Utils::GREEN_COLOR}Extracted #{Utils::RESET_COLOR}#{file_path}\n" if $global_options[:verbose]
45
- end
46
- 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
47
46
 
48
- # 处理 \d{5}.rb 文件
49
- if with_scripts
50
- all_rb_strings = []
51
- 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
52
63
 
53
- Utils.all_rb_files(target_dir) do |scripts, scripts_basenames, parent_relative_dir|
54
- scripts.zip(scripts_basenames).each do |script, script_basename|
55
- file_path = target_dir.join(parent_relative_dir, "#{script_basename}.rb")
56
- print "#{Utils::ESCAPE}#{Utils::MAGENTA_COLOR}Extracting from #{Utils::RESET_COLOR}#{file_path}...\r" if $global_options[:verbose]
57
- strings_extractor.visit(Prism.parse(script).value)
58
- print "#{Utils::ESCAPE}#{Utils::GREEN_COLOR}Extracted #{Utils::RESET_COLOR}#{file_path}\n" if $global_options[:verbose]
59
- end
60
- 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 = []
61
78
 
62
- if with_scripts_separate
63
- # 去除 nil 元素
64
- all_rb_strings.compact!
65
- Utils.object_json(all_rb_strings.each_with_object({}) { |item, h| h[item] = item }, output_dir.join('ManualTransFile_scripts.json'))
66
- else
67
- all_ex_strings.concat(all_rb_strings)
68
- end
69
- end
79
+ # 处理 CommonEvent_\d{5}.json 文件
80
+ all_ex_strings.concat(ex_commonevents(target_dir))
70
81
 
71
- # 去除 nil 元素
72
- all_ex_strings.compact!
73
- 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
74
91
  end
75
92
 
76
- 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,71 +3,94 @@
3
3
  require 'zlib'
4
4
  require_relative 'ast'
5
5
  require_relative 'utils'
6
+ require_relative 'logger'
6
7
 
7
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
8
29
 
9
- # 将指定目录下的所有已经序列化为 R3EXS 后的 JOSN 文件按照 ManualTransFile.json 翻译注入
10
- #
11
- # @param target_dir [Pathname] 目标目录
12
- # @param output_dir [Pathname] 输出目录
13
- # @param manualtransfile_path [Pathname] ManualTransFile.json 文件路径
14
- # @param with_scripts [Boolean] 是否包含脚本
15
- #
16
- # @raise [R3EXSJsonFileError] json 文件不是 R3EXS 模块中的对象
17
- # @raise [JsonDirError] target_dir 不存在
18
- # @raise [ScriptsInfoPathError] Scripts_info.json 文件不存在
19
- # @raise [ManualTransFilePath] ManualTransFile.json 不存在
20
- #
21
- # @return [void]
22
- def R3EXS.in_strings(target_dir, output_dir, manualtransfile_path, with_scripts)
23
- manualtransfile_path.exist? or raise ManualTransFilePathError.new(manualtransfile_path.to_s), "ManualTransFile.json not found: #{manualtransfile_path}"
24
-
25
- manual_trans_hash = Oj.load_file(manualtransfile_path.to_s)
26
-
27
- # 处理常规的 JSON 文件
28
- Utils.all_json_files(target_dir, :R3EXS) do |object, file_basename, parent_relative_dir|
29
- file_path = output_dir.join(parent_relative_dir, "#{file_basename}.json")
30
- print "#{Utils::ESCAPE}#{Utils::MAGENTA_COLOR}Injecting to #{Utils::RESET_COLOR}#{file_path}...\r" if $global_options[:verbose]
31
- if object.is_a?(Array)
32
- object.each do |obj|
33
- obj.in_strings(manual_trans_hash)
34
- end
35
- else
36
- object.in_strings(manual_trans_hash)
37
- end
38
- Utils.object_json(object, file_path)
39
- print "#{Utils::ESCAPE}#{Utils::GREEN_COLOR}Injected #{Utils::RESET_COLOR}#{file_path}\n" if $global_options[:verbose]
40
- 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
41
50
 
42
- # 处理 CommonEvent_\d{5}.json 文件
43
- Utils.all_commonevent_json_files(target_dir, :R3EXS) do |commonevents, commonevents_basenames, parent_relative_dir|
44
- commonevents.zip(commonevents_basenames).each do |commonevent, commonevent_basename|
45
- file_path = output_dir.join(parent_relative_dir, "#{commonevent_basename}.json")
46
- print "#{Utils::ESCAPE}#{Utils::MAGENTA_COLOR}Injecting to #{Utils::RESET_COLOR}#{file_path}...\r" if $global_options[:verbose]
47
- commonevent.in_strings(manual_trans_hash)
48
- Utils.object_json(commonevent, file_path)
49
- print "#{Utils::ESCAPE}#{Utils::GREEN_COLOR}Injected #{Utils::RESET_COLOR}#{file_path}\n" if $global_options[:verbose]
50
- end
51
- 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
52
69
 
53
- # 处理 \d{5}.rb 文件
54
- if with_scripts
55
- Utils.all_rb_files(target_dir) do |scripts, scripts_basenames, parent_relative_dir|
56
- full_output_dir = output_dir.join(parent_relative_dir)
57
- 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)
58
86
 
59
- script_info_file_path = target_dir.join(parent_relative_dir, 'Scripts_info.json')
60
- script_info_file_path.exist? or raise ScriptsInfoPathError.new(script_info_file_path.to_s), "Scripts_info.json not found: #{script_info_file_path}"
61
- # 记得把 Scripts_info.json 也复制过去
62
- FileUtils.cp(script_info_file_path, full_output_dir)
87
+ # 处理 CommonEvent_\d{5}.json 文件
88
+ in_commonevents(target_dir, output_dir, manual_trans_hash)
63
89
 
64
- scripts.zip(scripts_basenames).each do |script, script_basename|
65
- output_file_path = full_output_dir.join("#{script_basename}.rb")
66
- output_file_path.binwrite(StringsInjector.new(manual_trans_hash).rewrite(script, Prism.parse(script).value))
67
- print "#{Utils::ESCAPE}#{Utils::GREEN_COLOR}Injected #{Utils::RESET_COLOR}#{output_file_path}\n" if $global_options[:verbose]
68
- end
69
- end
70
- end
71
- end
90
+ # 处理 Script_\d{3}.rb 文件
91
+ in_scripts(target_dir, output_dir, manual_trans_hash) if with_scripts
72
92
 
73
- end
93
+ # 处理常规的 JSON 文件
94
+ in_regular(target_dir, output_dir, manual_trans_hash)
95
+ end
96
+ end
@@ -2,114 +2,128 @@
2
2
 
3
3
  require 'zlib'
4
4
  require_relative 'utils'
5
+ require_relative 'logger'
5
6
 
6
7
  module R3EXS
7
-
8
- # 将指定目录下的所有JSON文件转换为 rvdata2 文件
9
- #
10
- # @param [Pathname] target_dir 目标目录
11
- # @param [Pathname] output_dir 输出目录
12
- # @param [Pathname] original_dir 原始 rvdata2 文件目录
13
- # @param [Boolean] complete 是否完全转换
14
- # @param [Boolean] with_scripts 是否转换Scripts
15
- #
16
- # @raise [RPGJsonFileError] json 文件不是 RPG 模块中的对象
17
- # @raise [R3EXSJsonFileError] json 文件不是 R3EXS 模块中的对象
18
- # @raise [Rvdata2FileError] 原始 rvdata2 文件可能损坏
19
- # @raise [JsonDirError] target_dir 不存在
20
- # @raise [Rvdata2DirError] original_dir 不存在
21
- # @raise [ScriptsInfoPathError] Scripts_info.json 文件不存在
22
- #
23
- # @return [void]
24
- def R3EXS.json_rvdata2(target_dir, output_dir, original_dir, complete, with_scripts)
25
- # 处理常规的 JSON 文件
26
- Utils.all_json_files(target_dir, (complete) ? :RPG : :R3EXS) do |object, file_basename, parent_relative_dir|
27
- output_file_path = output_dir.join(parent_relative_dir, "#{file_basename}.rvdata2")
28
- if complete
29
- print "#{Utils::ESCAPE}#{Utils::MAGENTA_COLOR}Serializing to #{Utils::RESET_COLOR}#{output_file_path}...\r" if $global_options[:verbose]
30
- Utils.object_rvdata2(object, output_file_path)
31
- else
32
- # 检查 original_dir 是否存在
33
- original_dir.exist? or raise Rvdata2DirError.new(original_dir.to_s), "Original rvdata2 directory not found: #{original_dir}"
34
- original_file_path = original_dir.join(parent_relative_dir, "#{file_basename}.rvdata2")
35
- print "#{Utils::ESCAPE}#{Utils::BLUE_COLOR}Reading and Deserializing #{Utils::RESET_COLOR}#{original_file_path}...\r" if $global_options[:verbose]
36
- original_object = Marshal.load(original_file_path.binread)
37
-
38
- # 这里的类型检查要用紧凑模式,因为 rvdata2 文件中可能存在 nil 元素,必须忽略
39
- begin
40
- Utils.check_type(original_object, file_basename, true, :RPG)
41
- rescue RPGTypeError
42
- raise Rvdata2FileError.new(original_file_path.to_s), "Invalid rvdata2 file: #{original_file_path}"
43
- end
44
-
45
- print "#{Utils::ESCAPE}#{Utils::MAGENTA_COLOR}Serializing to #{Utils::RESET_COLOR}#{output_file_path}...\r" if $global_options[:verbose]
46
-
47
- # 根据是否为数组进行不同的处理
48
- if object.is_a?(Array)
49
- object.each do |obj|
50
- obj.inject_to(original_object[obj.index])
51
- end
52
- else
53
- object.inject_to(original_object)
54
- end
55
- Utils.object_rvdata2(original_object, output_file_path)
56
- end
57
- print "#{Utils::ESCAPE}#{Utils::GREEN_COLOR}Serialized #{Utils::RESET_COLOR}#{output_file_path}\n" if $global_options[:verbose]
58
- end
59
-
60
- # 处理 CommonEvent_\d{5}.json 文件
61
- Utils.all_commonevent_json_files(target_dir, (complete) ? :RPG : :R3EXS) do |commonevents, _, parent_relative_dir|
62
- output_file_path = output_dir.join(parent_relative_dir.parent, 'CommonEvents.rvdata2')
63
- if complete
64
- print "#{Utils::ESCAPE}#{Utils::MAGENTA_COLOR}Serializing to #{Utils::RESET_COLOR}#{output_file_path}...\r" if $global_options[:verbose]
65
- Utils.object_rvdata2(commonevents, output_file_path)
66
- else
67
- # 检查 original_dir 是否存在
68
- original_dir.exist? or raise Rvdata2DirError.new(original_dir.to_s), "Original rvdata2 directory not found: #{original_dir}"
69
- original_file_path = original_dir.join(parent_relative_dir.parent, 'CommonEvents.rvdata2')
70
- print "#{Utils::ESCAPE}#{Utils::BLUE_COLOR}Reading and Deserializing #{Utils::RESET_COLOR}#{original_file_path}...\r" if $global_options[:verbose]
71
- original_object = Marshal.load(original_file_path.binread)
72
-
73
- # 这里的类型检查要用紧凑模式,因为 rvdata2 文件中可能存在 nil 元素,必须忽略
74
- begin
75
- Utils.check_type(original_object, 'CommonEvents', true, :RPG)
76
- rescue RPGTypeError
77
- raise Rvdata2FileError.new(original_file_path.to_s), "Invalid rvdata2 file: #{original_file_path}"
78
- end
79
-
80
- print "#{Utils::ESCAPE}#{Utils::MAGENTA_COLOR}Serializing to #{Utils::RESET_COLOR}#{output_file_path}...\r" if $global_options[:verbose]
81
-
82
- commonevents.each do |commonevent|
83
- commonevent.inject_to(original_object[commonevent.index])
84
- end
85
-
86
- Utils.object_rvdata2(original_object, output_file_path)
87
- end
88
- print "#{Utils::ESCAPE}#{Utils::GREEN_COLOR}Serialized #{Utils::RESET_COLOR}#{output_file_path}\n" if $global_options[:verbose]
89
- end
90
-
91
- # 处理 \d{5}.rb 文件
92
- if with_scripts
93
- Utils.all_rb_files(target_dir) do |scripts, _, parent_relative_dir|
94
- output_file_path = output_dir.join(parent_relative_dir.parent, 'Scripts.rvdata2')
95
- script_info_file_path = target_dir.join(parent_relative_dir, 'Scripts_info.json')
96
- script_info_file_path.exist? or raise ScriptsInfoPathError.new(script_info_file_path.to_s), "Scripts_info.json not found: #{script_info_file_path}"
97
-
98
- print "#{Utils::ESCAPE}#{Utils::MAGENTA_COLOR}Serializing to #{Utils::RESET_COLOR}#{output_file_path}...\r" if $global_options[:verbose]
99
-
100
- print "#{Utils::ESCAPE}#{Utils::YELLOW_COLOR}Reading from #{Utils::RESET_COLOR}#{script_info_file_path}...\r" if $global_options[:verbose]
101
- scripts_info_array = Oj.load_file(script_info_file_path.to_s)
102
-
103
- output_scripts_array = []
104
- scripts_info_array.each do |script_info|
105
- index = script_info[:index]
106
- script = scripts[index]
107
- output_scripts_array << [114514, script_info[:name], Zlib::Deflate.deflate(script)]
108
- end
109
- Utils.object_rvdata2(output_scripts_array, output_file_path)
110
- print "#{Utils::ESCAPE}#{Utils::GREEN_COLOR}Serialized #{Utils::RESET_COLOR}#{output_file_path}\n" if $global_options[:verbose]
111
- end
112
- 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
113
41
  end
114
-
115
- 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