R3EXS 1.0.2 → 1.0.3

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.
@@ -1,105 +1,105 @@
1
- # frozen_string_literal: true
2
-
3
- require 'zlib'
4
- require_relative 'utils'
5
- require_relative 'RGSS3_R3EXS'
6
-
7
- module R3EXS
8
-
9
- # 将 Ruby 源码读取压缩后序列化到输出目录
10
- #
11
- # @param target_dir [String] 目标目录
12
- # @param output_dir [String] 输出目录
13
- #
14
- # @raise [ScriptsDirError] Scripts 目录不存在
15
- # @raise [ScriptsInfoPathError] Scripts_info.json 文件不存在
16
- #
17
- # @return [void]
18
- def R3EXS.rb_scripts(target_dir, output_dir)
19
- FileUtils.mkdir(output_dir) unless Dir.exist?(output_dir)
20
- scripts_array = []
21
- full_dir = File.join(target_dir, 'Scripts')
22
- script_info_file_path = File.join(full_dir, 'Scripts_info.json')
23
-
24
- Dir.exist?(full_dir) or raise ScriptsDirError.new(full_dir), "Scripts directory not found: #{full_dir}"
25
- File.exist?(script_info_file_path) or raise ScriptsInfoPathError.new(script_info_file_path), "Scripts_info.json not found: #{script_info_file_path}"
26
-
27
- print "#{Utils::ESCAPE}#{Utils::YELLOW_COLOR}Reading from #{Utils::RESET_COLOR}#{script_info_file_path}...\r" if $global_options[:verbose]
28
- scripts_info_array = Oj.load_file(script_info_file_path)
29
-
30
- scripts_info_array.each do |script_info|
31
- index = script_info[:index]
32
- script_file_path = File.join(full_dir, "#{format('%03d', index)}.rb")
33
- print "#{Utils::ESCAPE}#{Utils::YELLOW_COLOR}Reading from #{Utils::RESET_COLOR}#{script_file_path}...\r" if $global_options[:verbose]
34
- scripts_array << [114514, script_info[:name], Zlib::Deflate.deflate(File.read(script_file_path, mode: "r"))]
35
- end
36
- Utils.object_rvdata2(scripts_array, File.join(output_dir, 'Scripts.rvdata2'))
37
- end
38
-
39
- # 将指定目录下的所有JSON文件转换为 rvdata2 文件
40
- #
41
- # @param [String] target_dir 目标目录
42
- # @param [String] output_dir 输出目录
43
- # @param [String] original_dir 原始 rvdata2 文件目录
44
- # @param [Boolean] complete 是否完全转换
45
- # @param [Boolean] with_scripts 是否转换Scripts
46
- #
47
- # @raise [RPGJsonFileError] json 文件不是 RPG 模块中的对象
48
- # @raise [R3EXSJsonFileError] json 文件不是 R3EXS 模块中的对象
49
- # @raise [Rvdata2FileError] 原始 rvdata2 文件可能损坏
50
- # @raise [JsonDirError] target_dir 不存在
51
- # @raise [Rvdata2DirError] original_dir 不存在
52
- # @raise [ScriptsDirError] Scripts 目录不存在
53
- # @raise [ScriptsInfoPathError] Scripts_info.json 文件不存在
54
- #
55
- # @return [void]
56
- def R3EXS.json_rvdata2(target_dir, output_dir, original_dir, complete, with_scripts)
57
- FileUtils.mkdir(output_dir) unless Dir.exist?(output_dir)
58
-
59
- if complete
60
- Utils.all_json_files(target_dir, :RPG) do |object, file_basename|
61
- file_path = File.join(output_dir, "#{file_basename}.rvdata2")
62
- print "#{Utils::ESCAPE}#{Utils::MAGENTA_COLOR}Serializing to #{Utils::RESET_COLOR}#{file_path}...\n" if $global_options[:verbose]
63
- Utils.object_rvdata2(object, file_path)
64
- print "#{Utils::ESCAPE}#{Utils::GREEN_COLOR}Serialized #{Utils::RESET_COLOR}#{file_basename}\n" if $global_options[:verbose]
65
- end
66
- else
67
- # 检查 original_dir 是否存在
68
- Dir.exist?(original_dir) or raise Rvdata2DirError.new(original_dir), "Original rvdata2 directory not found: #{original_dir}"
69
- Utils.all_json_files(target_dir, :R3EXS) do |object, file_basename|
70
-
71
- original_file_path = File.join(original_dir, file_basename + '.rvdata2')
72
- print "#{Utils::ESCAPE}#{Utils::BLUE_COLOR}Reading and Deserializing #{Utils::RESET_COLOR}#{original_file_path}...\r" if $global_options[:verbose]
73
- original_object = File.open(original_file_path, 'rb') { |file| Marshal.load(file) }
74
-
75
- # 这里的类型检查要用紧凑模式,因为 rvdata2 文件中可能存在 nil 元素,必须忽略
76
- begin
77
- Utils.check_type(original_object, file_basename, true, :RPG)
78
- rescue RPGTypeError
79
- raise Rvdata2FileError.new(original_file_path), "Invalid rvdata2 file: #{original_file_path}"
80
- end
81
-
82
- file_path = File.join(output_dir, "#{file_basename}.rvdata2")
83
- print "#{Utils::ESCAPE}#{Utils::MAGENTA_COLOR}Serializing to #{Utils::RESET_COLOR}#{file_path}...\n" if $global_options[:verbose]
84
-
85
- # 根据是否为数组进行不同的处理
86
- if object.is_a?(Array)
87
- object.each do |obj|
88
- obj.inject_to(original_object[obj.index])
89
- end
90
- else
91
- object.inject_to(original_object)
92
- end
93
- Utils.object_rvdata2(original_object, file_path)
94
- print "#{Utils::ESCAPE}#{Utils::GREEN_COLOR}Serialized #{Utils::RESET_COLOR}#{file_basename}\n" if $global_options[:verbose]
95
- end
96
- end
97
-
98
- if with_scripts
99
- print "#{Utils::ESCAPE}#{Utils::MAGENTA_COLOR}Serializing to #{Utils::RESET_COLOR}#{File.join(output_dir, 'Scripts.rvdata2')}...\r" if $global_options[:verbose]
100
- rb_scripts(target_dir, output_dir)
101
- print "#{Utils::ESCAPE}#{Utils::GREEN_COLOR}Serialized #{Utils::RESET_COLOR}Scripts\n" if $global_options[:verbose]
102
- end
103
- end
104
-
1
+ # frozen_string_literal: true
2
+
3
+ require 'zlib'
4
+ require_relative 'utils'
5
+ require_relative 'RGSS3_R3EXS'
6
+
7
+ module R3EXS
8
+
9
+ # 将 Ruby 源码读取压缩后序列化到输出目录
10
+ #
11
+ # @param target_dir [String] 目标目录
12
+ # @param output_dir [String] 输出目录
13
+ #
14
+ # @raise [ScriptsDirError] Scripts 目录不存在
15
+ # @raise [ScriptsInfoPathError] Scripts_info.json 文件不存在
16
+ #
17
+ # @return [void]
18
+ def R3EXS.rb_scripts(target_dir, output_dir)
19
+ FileUtils.mkdir(output_dir) unless Dir.exist?(output_dir)
20
+ scripts_array = []
21
+ full_dir = File.join(target_dir, 'Scripts')
22
+ script_info_file_path = File.join(full_dir, 'Scripts_info.json')
23
+
24
+ Dir.exist?(full_dir) or raise ScriptsDirError.new(full_dir), "Scripts directory not found: #{full_dir}"
25
+ File.exist?(script_info_file_path) or raise ScriptsInfoPathError.new(script_info_file_path), "Scripts_info.json not found: #{script_info_file_path}"
26
+
27
+ print "#{Utils::ESCAPE}#{Utils::YELLOW_COLOR}Reading from #{Utils::RESET_COLOR}#{script_info_file_path}...\r" if $global_options[:verbose]
28
+ scripts_info_array = Oj.load_file(script_info_file_path)
29
+
30
+ scripts_info_array.each do |script_info|
31
+ index = script_info[:index]
32
+ script_file_path = File.join(full_dir, "#{format('%03d', index)}.rb")
33
+ print "#{Utils::ESCAPE}#{Utils::YELLOW_COLOR}Reading from #{Utils::RESET_COLOR}#{script_file_path}...\r" if $global_options[:verbose]
34
+ scripts_array << [114514, script_info[:name], Zlib::Deflate.deflate(File.read(script_file_path, mode: "r"))]
35
+ end
36
+ Utils.object_rvdata2(scripts_array, File.join(output_dir, 'Scripts.rvdata2'))
37
+ end
38
+
39
+ # 将指定目录下的所有JSON文件转换为 rvdata2 文件
40
+ #
41
+ # @param [String] target_dir 目标目录
42
+ # @param [String] output_dir 输出目录
43
+ # @param [String] original_dir 原始 rvdata2 文件目录
44
+ # @param [Boolean] complete 是否完全转换
45
+ # @param [Boolean] with_scripts 是否转换Scripts
46
+ #
47
+ # @raise [RPGJsonFileError] json 文件不是 RPG 模块中的对象
48
+ # @raise [R3EXSJsonFileError] json 文件不是 R3EXS 模块中的对象
49
+ # @raise [Rvdata2FileError] 原始 rvdata2 文件可能损坏
50
+ # @raise [JsonDirError] target_dir 不存在
51
+ # @raise [Rvdata2DirError] original_dir 不存在
52
+ # @raise [ScriptsDirError] Scripts 目录不存在
53
+ # @raise [ScriptsInfoPathError] Scripts_info.json 文件不存在
54
+ #
55
+ # @return [void]
56
+ def R3EXS.json_rvdata2(target_dir, output_dir, original_dir, complete, with_scripts)
57
+ FileUtils.mkdir(output_dir) unless Dir.exist?(output_dir)
58
+
59
+ if complete
60
+ Utils.all_json_files(target_dir, :RPG) do |object, file_basename|
61
+ file_path = File.join(output_dir, "#{file_basename}.rvdata2")
62
+ print "#{Utils::ESCAPE}#{Utils::MAGENTA_COLOR}Serializing to #{Utils::RESET_COLOR}#{file_path}...\n" if $global_options[:verbose]
63
+ Utils.object_rvdata2(object, file_path)
64
+ print "#{Utils::ESCAPE}#{Utils::GREEN_COLOR}Serialized #{Utils::RESET_COLOR}#{file_basename}\n" if $global_options[:verbose]
65
+ end
66
+ else
67
+ # 检查 original_dir 是否存在
68
+ Dir.exist?(original_dir) or raise Rvdata2DirError.new(original_dir), "Original rvdata2 directory not found: #{original_dir}"
69
+ Utils.all_json_files(target_dir, :R3EXS) do |object, file_basename|
70
+
71
+ original_file_path = File.join(original_dir, file_basename + '.rvdata2')
72
+ print "#{Utils::ESCAPE}#{Utils::BLUE_COLOR}Reading and Deserializing #{Utils::RESET_COLOR}#{original_file_path}...\r" if $global_options[:verbose]
73
+ original_object = File.open(original_file_path, 'rb') { |file| Marshal.load(file) }
74
+
75
+ # 这里的类型检查要用紧凑模式,因为 rvdata2 文件中可能存在 nil 元素,必须忽略
76
+ begin
77
+ Utils.check_type(original_object, file_basename, true, :RPG)
78
+ rescue RPGTypeError
79
+ raise Rvdata2FileError.new(original_file_path), "Invalid rvdata2 file: #{original_file_path}"
80
+ end
81
+
82
+ file_path = File.join(output_dir, "#{file_basename}.rvdata2")
83
+ print "#{Utils::ESCAPE}#{Utils::MAGENTA_COLOR}Serializing to #{Utils::RESET_COLOR}#{file_path}...\n" if $global_options[:verbose]
84
+
85
+ # 根据是否为数组进行不同的处理
86
+ if object.is_a?(Array)
87
+ object.each do |obj|
88
+ obj.inject_to(original_object[obj.index])
89
+ end
90
+ else
91
+ object.inject_to(original_object)
92
+ end
93
+ Utils.object_rvdata2(original_object, file_path)
94
+ print "#{Utils::ESCAPE}#{Utils::GREEN_COLOR}Serialized #{Utils::RESET_COLOR}#{file_basename}\n" if $global_options[:verbose]
95
+ end
96
+ end
97
+
98
+ if with_scripts
99
+ print "#{Utils::ESCAPE}#{Utils::MAGENTA_COLOR}Serializing to #{Utils::RESET_COLOR}#{File.join(output_dir, 'Scripts.rvdata2')}...\r" if $global_options[:verbose]
100
+ rb_scripts(target_dir, output_dir)
101
+ print "#{Utils::ESCAPE}#{Utils::GREEN_COLOR}Serialized #{Utils::RESET_COLOR}Scripts\n" if $global_options[:verbose]
102
+ end
103
+ end
104
+
105
105
  end