R3EXS 1.0.4 → 1.1.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.
@@ -1,105 +1,116 @@
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
+ # 将指定目录下的所有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
114
+ end
115
+
105
116
  end
@@ -8,32 +8,63 @@ module R3EXS
8
8
 
9
9
  # 将 Script 对象数组序列化为 Ruby 源码
10
10
  #
11
- # @param scripts [Array<Array>] 待转换的 Script 对象数组
12
- # @param output_dir [String] 输出目录
11
+ # @param scripts [Array<Object>] 待转换的 Script 对象数组
12
+ # @param output_dir [Pathname] 输出目录
13
13
  # @return [void]
14
14
  def R3EXS.scripts_rb(scripts, output_dir)
15
- FileUtils.mkdir(output_dir) unless Dir.exist?(output_dir)
16
- full_dir = File.join(output_dir, 'Scripts')
17
- FileUtils.mkdir(full_dir) unless Dir.exist?(full_dir)
15
+ output_dir.mkpath unless output_dir.exist?
16
+ full_dir = output_dir.join('Scripts')
17
+ full_dir.mkdir unless full_dir.exist?
18
18
 
19
19
  scripts_info_array = []
20
20
  scripts.each_with_index do |script, index|
21
21
  next if script.nil?
22
22
  scripts_info_array << { index: index, name: script[1] }
23
- script_file_path = File.join(full_dir, "#{format('%03d', index)}.rb")
23
+ script_file_path = full_dir.join("#{format('%03d', index)}.rb")
24
24
  print "#{Utils::ESCAPE}#{Utils::MAGENTA_COLOR}Serializing to #{Utils::RESET_COLOR}#{script_file_path}...\r" if $global_options[:verbose]
25
- File.write(script_file_path, Zlib::Inflate.inflate(script[2]).encode(universal_newline: true), mode: 'w')
25
+ script_file_path.write(Zlib::Inflate.inflate(script[2]).encode(universal_newline: true))
26
+ print "#{Utils::ESCAPE}#{Utils::GREEN_COLOR}Serialized #{Utils::RESET_COLOR}#{script_file_path}\n" if $global_options[:verbose]
26
27
  end
27
28
 
28
- script_info_file_path = File.join(full_dir, 'Scripts_info.json')
29
+ script_info_file_path = full_dir.join('Scripts_info.json')
29
30
  print "#{Utils::ESCAPE}#{Utils::MAGENTA_COLOR}Serializing to #{Utils::RESET_COLOR}#{script_info_file_path}\r" if $global_options[:verbose]
30
31
  Utils.object_json(scripts_info_array, script_info_file_path)
32
+ print "#{Utils::ESCAPE}#{Utils::GREEN_COLOR}Serialized #{Utils::RESET_COLOR}#{script_info_file_path}\n" if $global_options[:verbose]
33
+ end
34
+
35
+ # 将 CommonEvents 对象数组序列化为分开的 JSON 文件
36
+ #
37
+ # @param commonevents [Array<Object>] 待转换的 CommonEvents 对象数组
38
+ # @param output_dir [Pathname] 输出目录
39
+ # @param complete [Boolean] 是否序列化所有内容
40
+ # @param with_notes [Boolean] 是否包含备注
41
+ # @return [void]
42
+ def R3EXS.commonevents_json(commonevents, output_dir, complete, with_notes)
43
+ full_dir = output_dir.join('CommonEvents')
44
+
45
+ if complete
46
+ commonevents.each_with_index do |commonevent, index|
47
+ commonevent_file_path = full_dir.join("#{format('CommonEvent_%05d', index)}.json")
48
+ print "#{Utils::ESCAPE}#{Utils::MAGENTA_COLOR}Serializing to #{Utils::RESET_COLOR}#{commonevent_file_path}...\r" if $global_options[:verbose]
49
+ Utils.object_json(commonevent, commonevent_file_path)
50
+ print "#{Utils::ESCAPE}#{Utils::GREEN_COLOR}Serialized #{Utils::RESET_COLOR}#{commonevent_file_path}\n" if $global_options[:verbose]
51
+ end
52
+ else
53
+ commonevents = Utils.rpg_r3exs(commonevents, 'CommonEvents', with_notes)
54
+ commonevents.each do |commonevent|
55
+ index = commonevent.index
56
+ commonevent_file_path = full_dir.join("#{format('CommonEvent_%05d', index)}.json")
57
+ print "#{Utils::ESCAPE}#{Utils::MAGENTA_COLOR}Serializing to #{Utils::RESET_COLOR}#{commonevent_file_path}...\r" if $global_options[:verbose]
58
+ Utils.object_json(commonevent, commonevent_file_path)
59
+ print "#{Utils::ESCAPE}#{Utils::GREEN_COLOR}Serialized #{Utils::RESET_COLOR}#{commonevent_file_path}\n" if $global_options[:verbose]
60
+ end
61
+ end
31
62
  end
32
63
 
33
64
  # 将指定目录下的所有 rvdata2 文件序列化为 JSON 格式
34
65
  #
35
- # @param target_dir [String] 目标目录
36
- # @param output_dir [String] 输出目录
66
+ # @param target_dir [Pathname] 目标目录
67
+ # @param output_dir [Pathname] 输出目录
37
68
  # @param complete [Boolean] 是否序列化所有内容
38
69
  # @param with_scripts [Boolean] 是否包含脚本
39
70
  # @param with_notes [Boolean] 是否包含备注
@@ -43,21 +74,20 @@ module R3EXS
43
74
  #
44
75
  # @return [void]
45
76
  def R3EXS.rvdata2_json(target_dir, output_dir, complete, with_scripts, with_notes)
46
- FileUtils.mkdir(output_dir) unless Dir.exist?(output_dir)
47
- Utils.all_rvdata2_files(target_dir) do |object, file_basename|
77
+ Utils.all_rvdata2_files(target_dir) do |object, file_basename, parent_relative_dir|
48
78
  if file_basename == 'Scripts'
49
- next unless with_scripts
50
- scripts_rb(object, output_dir)
51
- print "#{Utils::ESCAPE}#{Utils::GREEN_COLOR}Serialized #{Utils::RESET_COLOR}#{file_basename}\n" if $global_options[:verbose]
79
+ scripts_rb(object, output_dir.join(parent_relative_dir)) if with_scripts
80
+ elsif file_basename == 'CommonEvents'
81
+ commonevents_json(object, output_dir.join(parent_relative_dir), complete, with_notes)
52
82
  else
53
- file_path = File.join(output_dir, "#{file_basename}.json")
83
+ file_path = output_dir.join(parent_relative_dir, "#{file_basename}.json")
54
84
  print "#{Utils::ESCAPE}#{Utils::MAGENTA_COLOR}Serializing to #{Utils::RESET_COLOR}#{file_path}...\r" if $global_options[:verbose]
55
85
  if complete
56
86
  Utils.object_json(object, file_path)
57
87
  else
58
88
  Utils.object_json(Utils.rpg_r3exs(object, file_basename, with_notes), file_path)
59
89
  end
60
- print "#{Utils::ESCAPE}#{Utils::GREEN_COLOR}Serialized #{Utils::RESET_COLOR}#{file_basename}\n" if $global_options[:verbose]
90
+ print "#{Utils::ESCAPE}#{Utils::GREEN_COLOR}Serialized #{Utils::RESET_COLOR}#{file_path}\n" if $global_options[:verbose]
61
91
  end
62
92
  end
63
93
  end