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.
@@ -2,94 +2,91 @@
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
- # 将 Script 对象数组序列化为 Ruby 源码
10
- #
11
- # @param scripts [Array<Object>] 待转换的 Script 对象数组
12
- # @param output_dir [Pathname] 输出目录
13
- # @return [void]
14
- def R3EXS.scripts_rb(scripts, output_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
-
19
- scripts_info_array = []
20
- scripts.each_with_index do |script, index|
21
- next if script.nil?
22
- scripts_info_array << { index: index, name: script[1] }
23
- script_file_path = full_dir.join("#{format('%03d', index)}.rb")
24
- print "#{Utils::ESCAPE}#{Utils::MAGENTA_COLOR}Serializing to #{Utils::RESET_COLOR}#{script_file_path}...\r" if $global_options[:verbose]
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]
27
- end
28
-
29
- script_info_file_path = full_dir.join('Scripts_info.json')
30
- print "#{Utils::ESCAPE}#{Utils::MAGENTA_COLOR}Serializing to #{Utils::RESET_COLOR}#{script_info_file_path}\r" if $global_options[:verbose]
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]
8
+ # 将 CommonEvents 对象数组序列化为分开的 JSON 文件
9
+ #
10
+ # @param commonevents [Array<RPG::CommonEvent>] CommonEvents 对象数组
11
+ # @param output_dir [Pathname] 输出目录
12
+ # @param complete [Boolean] 是否序列化所有内容
13
+ #
14
+ # @return [void]
15
+ def self.commonevents_json(commonevents, output_dir, complete)
16
+ if complete
17
+ commonevents.each_with_index do |commonevent, index|
18
+ file_path = output_dir.join("#{format('CommonEvent_%05d', index)}.json")
19
+ Utils.object_json(commonevent, file_path)
20
+ Logger.debug("Serialize #{file_path}")
21
+ end
22
+ else
23
+ commonevents = Utils.rpg_r3exs(commonevents, R3EXS::CommonEvent)
24
+ commonevents.each do |commonevent|
25
+ file_path = output_dir.join("#{format('CommonEvent_%05d', commonevent.index)}.json")
26
+ Utils.object_json(commonevent, file_path)
27
+ Logger.debug("Serialize #{file_path}")
28
+ end
33
29
  end
30
+ end
34
31
 
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')
32
+ # 将 Script 对象数组序列化为分开的 Ruby 源码
33
+ #
34
+ # @param scripts [Array<Object>] Script 对象数组
35
+ # @param output_dir [Pathname] 输出目录
36
+ #
37
+ # @return [void]
38
+ def self.scripts_rb(scripts, output_dir)
39
+ scripts_info = []
40
+ script_info_file_path = output_dir.join('Scripts_info.json')
41
+ scripts.each_with_index do |script, index|
42
+ next if script.nil?
44
43
 
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
44
+ scripts_info << { index: index, name: script[1] }
45
+ file_path = output_dir.join("#{format('Script_%03d', index)}.rb")
46
+ Utils.script_rb_compressing(script[2], file_path)
47
+ Logger.debug("Serialize #{file_path}")
62
48
  end
49
+ Utils.object_json(scripts_info, script_info_file_path)
50
+ Logger.debug("Serialize #{script_info_file_path}")
51
+ end
63
52
 
64
- # 将指定目录下的所有 rvdata2 文件序列化为 JSON 格式
65
- #
66
- # @param target_dir [Pathname] 目标目录
67
- # @param output_dir [Pathname] 输出目录
68
- # @param complete [Boolean] 是否序列化所有内容
69
- # @param with_scripts [Boolean] 是否包含脚本
70
- # @param with_notes [Boolean] 是否包含备注
71
- #
72
- # @raise [Rvdata2FileError] rvdata2 文件可能损坏
73
- # @raise [Rvdata2DirError] target_dir 不存在
74
- #
75
- # @return [void]
76
- def R3EXS.rvdata2_json(target_dir, output_dir, complete, with_scripts, with_notes)
77
- Utils.all_rvdata2_files(target_dir) do |object, file_basename, parent_relative_dir|
78
- if file_basename == 'Scripts'
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)
82
- else
83
- file_path = output_dir.join(parent_relative_dir, "#{file_basename}.json")
84
- print "#{Utils::ESCAPE}#{Utils::MAGENTA_COLOR}Serializing to #{Utils::RESET_COLOR}#{file_path}...\r" if $global_options[:verbose]
85
- if complete
86
- Utils.object_json(object, file_path)
87
- else
88
- Utils.object_json(Utils.rpg_r3exs(object, file_basename, with_notes), file_path)
89
- end
90
- print "#{Utils::ESCAPE}#{Utils::GREEN_COLOR}Serialized #{Utils::RESET_COLOR}#{file_path}\n" if $global_options[:verbose]
91
- end
92
- end
53
+ # 将常规对象数组序列化为 JSON 文件
54
+ #
55
+ # @param obj [Array<Object>] 常规对象数组
56
+ # @param klass [::Class] 对应 R3EXS 模块的类
57
+ # @param file_path [Pathname] 输出文件路径
58
+ # @param complete [Boolean] 是否序列化所有内容
59
+ #
60
+ # @return [void]
61
+ def self.regular_json(obj, klass, file_path, complete)
62
+ if complete
63
+ Utils.object_json(obj, file_path)
64
+ else
65
+ Utils.object_json(Utils.rpg_r3exs(obj, klass), file_path)
93
66
  end
67
+ Logger.debug("Serialize #{file_path}")
68
+ end
94
69
 
95
- end
70
+ # 将指定目录下的所有 rvdata2 文件序列化为 JSON 格式
71
+ #
72
+ # @param target_dir [Pathname] 目标目录
73
+ # @param output_dir [Pathname] 输出目录
74
+ # @param complete [Boolean] 是否序列化所有内容
75
+ # @param with_scripts [Boolean] 是否包含脚本
76
+ #
77
+ # @raise [Rvdata2DirError] target_dir 不存在
78
+ #
79
+ # @return [void]
80
+ def self.rvdata2_json(target_dir, output_dir, complete, with_scripts)
81
+ Utils.all_rvdata2_files(target_dir) do |obj, klass, file_basename, rvdata2_path|
82
+ file_path = output_dir.join(rvdata2_path.relative_path_from(target_dir)).sub_ext('.json')
83
+ if file_basename.to_s == 'CommonEvents'
84
+ commonevents_json(obj, file_path.sub_ext(''), complete)
85
+ elsif file_basename.to_s == 'Scripts'
86
+ scripts_rb(obj, file_path.sub_ext('')) if with_scripts
87
+ else
88
+ regular_json(obj, klass, file_path, complete)
89
+ end
90
+ end
91
+ end
92
+ end