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.
@@ -2,95 +2,91 @@
2
2
 
3
3
  require 'zlib'
4
4
  require_relative 'utils'
5
+ require_relative 'logger'
5
6
 
6
7
  module R3EXS
7
-
8
- # 将 Script 对象数组序列化为 Ruby 源码
9
- #
10
- # @param scripts [Array<Object>] 待转换的 Script 对象数组
11
- # @param output_dir [Pathname] 输出目录
12
- #
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
- #
42
- # @return [void]
43
- def R3EXS.commonevents_json(commonevents, output_dir, complete, with_notes)
44
- 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?
45
43
 
46
- if complete
47
- commonevents.each_with_index do |commonevent, index|
48
- commonevent_file_path = full_dir.join("#{format('CommonEvent_%05d', index)}.json")
49
- print "#{Utils::ESCAPE}#{Utils::MAGENTA_COLOR}Serializing to #{Utils::RESET_COLOR}#{commonevent_file_path}...\r" if $global_options[:verbose]
50
- Utils.object_json(commonevent, commonevent_file_path)
51
- print "#{Utils::ESCAPE}#{Utils::GREEN_COLOR}Serialized #{Utils::RESET_COLOR}#{commonevent_file_path}\n" if $global_options[:verbose]
52
- end
53
- else
54
- commonevents = Utils.rpg_r3exs(commonevents, 'CommonEvents', with_notes)
55
- commonevents.each do |commonevent|
56
- index = commonevent.index
57
- commonevent_file_path = full_dir.join("#{format('CommonEvent_%05d', index)}.json")
58
- print "#{Utils::ESCAPE}#{Utils::MAGENTA_COLOR}Serializing to #{Utils::RESET_COLOR}#{commonevent_file_path}...\r" if $global_options[:verbose]
59
- Utils.object_json(commonevent, commonevent_file_path)
60
- print "#{Utils::ESCAPE}#{Utils::GREEN_COLOR}Serialized #{Utils::RESET_COLOR}#{commonevent_file_path}\n" if $global_options[:verbose]
61
- end
62
- 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}")
63
48
  end
49
+ Utils.object_json(scripts_info, script_info_file_path)
50
+ Logger.debug("Serialize #{script_info_file_path}")
51
+ end
64
52
 
65
- # 将指定目录下的所有 rvdata2 文件序列化为 JSON 格式
66
- #
67
- # @param target_dir [Pathname] 目标目录
68
- # @param output_dir [Pathname] 输出目录
69
- # @param complete [Boolean] 是否序列化所有内容
70
- # @param with_scripts [Boolean] 是否包含脚本
71
- # @param with_notes [Boolean] 是否包含备注
72
- #
73
- # @raise [Rvdata2FileError] rvdata2 文件可能损坏
74
- # @raise [Rvdata2DirError] target_dir 不存在
75
- #
76
- # @return [void]
77
- def R3EXS.rvdata2_json(target_dir, output_dir, complete, with_scripts, with_notes)
78
- Utils.all_rvdata2_files(target_dir) do |object, file_basename, parent_relative_dir|
79
- if file_basename == 'Scripts'
80
- scripts_rb(object, output_dir.join(parent_relative_dir)) if with_scripts
81
- elsif file_basename == 'CommonEvents'
82
- commonevents_json(object, output_dir.join(parent_relative_dir), complete, with_notes)
83
- else
84
- file_path = output_dir.join(parent_relative_dir, "#{file_basename}.json")
85
- print "#{Utils::ESCAPE}#{Utils::MAGENTA_COLOR}Serializing to #{Utils::RESET_COLOR}#{file_path}...\r" if $global_options[:verbose]
86
- if complete
87
- Utils.object_json(object, file_path)
88
- else
89
- Utils.object_json(Utils.rpg_r3exs(object, file_basename, with_notes), file_path)
90
- end
91
- print "#{Utils::ESCAPE}#{Utils::GREEN_COLOR}Serialized #{Utils::RESET_COLOR}#{file_path}\n" if $global_options[:verbose]
92
- end
93
- 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)
94
66
  end
67
+ Logger.debug("Serialize #{file_path}")
68
+ end
95
69
 
96
- 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