Skip to content

Commit bde5679

Browse files
committed
create temp dir and make errors more readable
1 parent 7559769 commit bde5679

File tree

5 files changed

+183
-127
lines changed

5 files changed

+183
-127
lines changed

addons/nklbdev.importality/command_line_image_format_loader_extension.gd

Lines changed: 6 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ func _load_image(
9191
push_error("Unable to find command template for file extension: " + extension)
9292
return ERR_UNCONFIGURED
9393

94-
var command_template_parts: PackedStringArray = split_words_with_quotes(command_template)
94+
var command_template_parts: PackedStringArray = _Common.split_words_with_quotes(command_template)
9595
if command_template_parts.is_empty():
9696
push_error("Unable to recognize command template parts for extension: %s" % [extension])
9797
return ERR_UNCONFIGURED
@@ -127,8 +127,11 @@ func _load_image(
127127
if exit_code:
128128
for arg_index in arguments.size():
129129
arguments[arg_index] = "\nArgument: " + arguments[arg_index]
130-
push_error("An error occurred while executing the OS command. Process exited with code %s:\nCommand: %s%s" %
131-
[exit_code, command, "".join(arguments)])
130+
push_error(" ".join([
131+
"An error occurred while executing",
132+
"the external image converting utility command.",
133+
"Process exited with code %s:\nCommand: %s%s"
134+
]) % [exit_code, command, "".join(arguments)])
132135
return ERR_QUERY_FAILED
133136

134137
if not FileAccess.file_exists(global_output_path):
@@ -146,58 +149,3 @@ func _load_image(
146149

147150
return OK
148151

149-
const backslash: String = "\\"
150-
const quote: String = "\""
151-
const space: String = " "
152-
const tab: String = "\t"
153-
const empty: String = ""
154-
static func split_words_with_quotes(source: String) -> PackedStringArray:
155-
var parts: PackedStringArray
156-
if source.is_empty():
157-
return parts
158-
159-
var quotation: bool
160-
161-
var previous: String
162-
var current: String
163-
var next: String = source[0]
164-
var chars_count = source.length()
165-
166-
var part: String
167-
for char_idx in chars_count:
168-
previous = current
169-
current = next
170-
next = source[char_idx + 1] if char_idx < chars_count - 1 else ""
171-
if quotation:
172-
# seek for quotation end
173-
if previous != backslash and current == quote:
174-
if next == space or next == tab or next == empty:
175-
quotation = false
176-
parts.push_back(part)
177-
part = ""
178-
continue
179-
else:
180-
push_error("Invalid quotation start at %s:\n%s\n%s" % [char_idx, source, " ".repeat(char_idx) + "^"])
181-
return PackedStringArray()
182-
else:
183-
# seek for quotation start
184-
if current == space or current == tab:
185-
if not part.is_empty():
186-
parts.push_back(part)
187-
part = ""
188-
continue
189-
else:
190-
if previous != backslash and current == quote:
191-
if previous == space or previous == tab or previous == empty:
192-
quotation = true
193-
continue
194-
else:
195-
push_error("Invalid quotation end at %s:\n%s\n%s" % [char_idx, source, " ".repeat(char_idx) + "^"])
196-
return PackedStringArray()
197-
part += current
198-
if quotation:
199-
push_error("Invalid quotation end at %s:\n%s\n%s" % [chars_count - 1, source, " ".repeat(chars_count - 1) + "^"])
200-
return PackedStringArray()
201-
if not part.is_empty():
202-
parts.push_back(part)
203-
return parts

addons/nklbdev.importality/common.gd

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,3 +148,59 @@ static func get_vector2i(dict: Dictionary, x_key: String, y_key: String) -> Vect
148148
static var common_temporary_files_directory_path_project_setting: _ProjectSetting = _ProjectSetting.new(
149149
"temporary_files_directory_path", "", TYPE_STRING, PROPERTY_HINT_GLOBAL_DIR,
150150
"", true, func(v: String): return v.is_empty())
151+
152+
const __backslash: String = "\\"
153+
const __quote: String = "\""
154+
const __space: String = " "
155+
const __tab: String = "\t"
156+
const __empty: String = ""
157+
static func split_words_with_quotes(source: String) -> PackedStringArray:
158+
var parts: PackedStringArray
159+
if source.is_empty():
160+
return parts
161+
162+
var quotation: bool
163+
164+
var previous: String
165+
var current: String
166+
var next: String = source[0]
167+
var chars_count = source.length()
168+
169+
var part: String
170+
for char_idx in chars_count:
171+
previous = current
172+
current = next
173+
next = source[char_idx + 1] if char_idx < chars_count - 1 else ""
174+
if quotation:
175+
# seek for quotation end
176+
if previous != __backslash and current == __quote:
177+
if next == __space or next == __tab or next == __empty:
178+
quotation = false
179+
parts.push_back(part)
180+
part = ""
181+
continue
182+
else:
183+
push_error("Invalid quotation start at %s:\n%s\n%s" % [char_idx, source, " ".repeat(char_idx) + "^"])
184+
return PackedStringArray()
185+
else:
186+
# seek for quotation start
187+
if current == __space or current == __tab:
188+
if not part.is_empty():
189+
parts.push_back(part)
190+
part = ""
191+
continue
192+
else:
193+
if previous != __backslash and current == __quote:
194+
if previous == __space or previous == __tab or previous == __empty:
195+
quotation = true
196+
continue
197+
else:
198+
push_error("Invalid quotation end at %s:\n%s\n%s" % [char_idx, source, " ".repeat(char_idx) + "^"])
199+
return PackedStringArray()
200+
part += current
201+
if quotation:
202+
push_error("Invalid quotation end at %s:\n%s\n%s" % [chars_count - 1, source, " ".repeat(chars_count - 1) + "^"])
203+
return PackedStringArray()
204+
if not part.is_empty():
205+
parts.push_back(part)
206+
return parts

addons/nklbdev.importality/export/aseprite.gd

Lines changed: 56 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ func _init(editor_file_system: EditorFileSystem) -> void:
2626

2727
func _export(res_source_file_path: String, atlas_maker: AtlasMaker, options: Dictionary) -> _Common.ExportResult:
2828
var result: _Common.ExportResult = _Common.ExportResult.new()
29+
var err: Error
2930

3031
var os_command_result: _ProjectSetting.Result = __os_command_project_setting.get_value()
3132
if os_command_result.error:
@@ -41,30 +42,44 @@ func _export(res_source_file_path: String, atlas_maker: AtlasMaker, options: Dic
4142
if temp_dir_path_result.error:
4243
result.fail(ERR_UNCONFIGURED, "Unable to get Temporary Files Directory Path to export spritesheet", temp_dir_path_result)
4344
return result
45+
var global_temp_dir_path: String = ProjectSettings.globalize_path(
46+
temp_dir_path_result.value.strip_edges())
4447

45-
var png_path: String = temp_dir_path_result.value.path_join("temp.png")
46-
var global_png_path: String = ProjectSettings.globalize_path(png_path)
47-
var json_path: String = temp_dir_path_result.value.path_join("temp.json")
48-
var global_json_path: String = ProjectSettings.globalize_path(json_path)
48+
if not DirAccess.dir_exists_absolute(global_temp_dir_path):
49+
err = DirAccess.make_dir_recursive_absolute(global_temp_dir_path)
50+
if err:
51+
result.fail(ERR_UNCONFIGURED, "Unable to create directory for temporary files \"%s\" with error %s \"%s\"" %
52+
[global_temp_dir_path, err, error_string(err)])
53+
return result
4954

50-
var output: Array = []
51-
var exit_code: int = OS.execute(
52-
os_command_result.value,
53-
os_command_arguments_result.value + PackedStringArray([
55+
var global_png_path: String = global_temp_dir_path.path_join("temp.png")
56+
var global_json_path: String = global_temp_dir_path.path_join("temp.json")
57+
58+
var command: String = os_command_result.value.strip_edges()
59+
var arguments: PackedStringArray = \
60+
os_command_arguments_result.value + \
61+
PackedStringArray([
5462
"--batch",
5563
"--format", "json-array",
5664
"--list-tags",
5765
"--sheet", global_png_path,
5866
"--data", global_json_path,
59-
ProjectSettings.globalize_path(res_source_file_path)]),
60-
output, true, false)
67+
ProjectSettings.globalize_path(res_source_file_path)])
68+
69+
var output: Array = []
70+
var exit_code: int = OS.execute(command, arguments, output, true, false)
6171
if exit_code:
62-
result.fail(ERR_QUERY_FAILED, "An error occurred while executing the Aseprite command. Process exited with code %s" % [exit_code])
72+
for arg_index in arguments.size():
73+
arguments[arg_index] = "\nArgument: " + arguments[arg_index]
74+
result.fail(ERR_QUERY_FAILED, " ".join([
75+
"An error occurred while executing the Aseprite command.",
76+
"Process exited with code %s:\nCommand: %s%s"
77+
]) % [exit_code, command, "".join(arguments)])
6378
return result
6479
var raw_atlas_image: Image = Image.load_from_file(global_png_path)
6580
DirAccess.remove_absolute(global_png_path)
6681
var json = JSON.new()
67-
var err: Error = json.parse(FileAccess.get_file_as_string(global_json_path))
82+
err = json.parse(FileAccess.get_file_as_string(global_json_path))
6883
if err:
6984
result.fail(ERR_INVALID_DATA, "Unable to parse sprite sheet json data with error %s \"%s\"" % [err, error_string(err)])
7085
return result
@@ -205,6 +220,7 @@ class CustomImageFormatLoaderExtension:
205220

206221
func _load_image(image: Image, file_access: FileAccess, flags: int, scale: float) -> Error:
207222
var global_source_file_path: String = file_access.get_path_absolute()
223+
var err: Error
208224

209225
var os_command_result: _ProjectSetting.Result = __os_command_project_setting.get_value()
210226
if os_command_result.error:
@@ -220,30 +236,44 @@ class CustomImageFormatLoaderExtension:
220236
if temp_dir_path_result.error:
221237
push_error(temp_dir_path_result.error_description)
222238
return temp_dir_path_result.error
223-
224-
var png_path: String = temp_dir_path_result.value.path_join("temp.png")
225-
var global_png_path: String = ProjectSettings.globalize_path(png_path)
226-
var json_path: String = temp_dir_path_result.value.path_join("temp.json")
227-
var global_json_path: String = ProjectSettings.globalize_path(json_path)
228-
229-
var output: Array = []
230-
var exit_code: int = OS.execute(
231-
os_command_result.value,
232-
os_command_arguments_result.value + PackedStringArray([
239+
var global_temp_dir_path: String = ProjectSettings.globalize_path(temp_dir_path_result.value.strip_edges())
240+
241+
var global_png_path: String = global_temp_dir_path.path_join("temp.png")
242+
var global_json_path: String = global_temp_dir_path.path_join("temp.json")
243+
if not DirAccess.dir_exists_absolute(global_temp_dir_path):
244+
err = DirAccess.make_dir_recursive_absolute(global_temp_dir_path)
245+
if err:
246+
push_error("Unable to create directory for temporary files \"%s\" with error %s \"%s\"" %
247+
[global_temp_dir_path, err, error_string(err)])
248+
return ERR_QUERY_FAILED
249+
250+
var command: String = os_command_result.value.strip_edges()
251+
var arguments: PackedStringArray = \
252+
os_command_arguments_result.value + \
253+
PackedStringArray([
233254
"--batch",
234255
"--format", "json-array",
235256
"--list-tags",
236257
"--sheet", global_png_path,
237258
"--data", global_json_path,
238-
global_source_file_path]),
239-
output, true, false)
259+
global_source_file_path,
260+
])
261+
262+
var output: Array = []
263+
var exit_code: int = OS.execute(command, arguments, output, true, false)
240264
if exit_code:
241-
push_error("An error occurred while executing the Aseprite command. Process exited with code %s" % [exit_code])
265+
for arg_index in arguments.size():
266+
arguments[arg_index] = "\nArgument: " + arguments[arg_index]
267+
push_error(" ".join([
268+
"An error occurred while executing the Aseprite command.",
269+
"Process exited with code %s:\nCommand: %s%s"
270+
]) % [exit_code, command, "".join(arguments)])
242271
return ERR_QUERY_FAILED
272+
243273
var raw_atlas_image: Image = Image.load_from_file(global_png_path)
244274
DirAccess.remove_absolute(global_png_path)
245275
var json = JSON.new()
246-
var err: Error = json.parse(FileAccess.get_file_as_string(global_json_path))
276+
err = json.parse(FileAccess.get_file_as_string(global_json_path))
247277
if err:
248278
push_error("Unable to parse sprite sheet json data with error %s \"%s\"" % [err, error_string(err)])
249279
return ERR_INVALID_DATA

addons/nklbdev.importality/export/krita.gd

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ func __validate_image_name(image_name: String) -> _Common.Result:
3535

3636
func _export(res_source_file_path: String, atlas_maker: AtlasMaker, options: Dictionary) -> _Common.ExportResult:
3737
var result: _Common.ExportResult = _Common.ExportResult.new()
38+
var err: Error
3839

3940
var os_command_result: _ProjectSetting.Result = __os_command_project_setting.get_value()
4041
if os_command_result.error:
@@ -50,6 +51,7 @@ func _export(res_source_file_path: String, atlas_maker: AtlasMaker, options: Dic
5051
if temp_dir_path_result.error:
5152
result.fail(ERR_UNCONFIGURED, "Unable to get Temporary Files Directory Path to export spritesheet", temp_dir_path_result)
5253
return result
54+
var global_temp_dir_path: String = ProjectSettings.globalize_path(temp_dir_path_result.value.strip_edges())
5355

5456
var global_source_file_path: String = ProjectSettings.globalize_path(res_source_file_path)
5557

@@ -104,8 +106,6 @@ func _export(res_source_file_path: String, atlas_maker: AtlasMaker, options: Dic
104106
var last_animations_frame_index: int = -1
105107
var png_base_name: String = "img"
106108
var global_temp_kra_path: String
107-
var global_temp_png_path: String = temp_dir_path_result.value.path_join("%s.png" % png_base_name)
108-
DirAccess.make_dir_recursive_absolute(temp_dir_path_result.value)
109109

110110
var storyboard_index_file_name: String = "%s/storyboard/index.xml" % image_name
111111
if storyboard_index_file_name in files_names_in_zip:
@@ -180,25 +180,31 @@ func _export(res_source_file_path: String, atlas_maker: AtlasMaker, options: Dic
180180

181181
zip_reader.close()
182182

183-
# Создать пользователя (например, KritaRunner) с паролем 111 с помощью Win+R -> netplwiz
184-
# Запустить Krita из-под этого пользователя с помощью утилиты PsExec
185-
# "C:\Program Files\WindowsApps\Microsoft.SysinternalsSuite_2023.6.0.0_x64__8wekyb3d8bbwe\Tools\PsExec.exe"
186-
# -u DEV-STATION\KritaRunner -p 111
187-
# "C:\Program Files\Krita (x64)\bin\krita.exe"
188-
# --export-sequence
189-
# --export-filename "R:\Temp\Godot\Krita Importers\img.png"
190-
# "D:\Godot 4.1\brave-mouse\project\tst2.kra"
191-
192-
var output: Array
193-
var exit_code: int = OS.execute(
194-
os_command_result.value,
195-
os_command_arguments_result.value + PackedStringArray([
183+
if not DirAccess.dir_exists_absolute(global_temp_dir_path):
184+
err = DirAccess.make_dir_recursive_absolute(global_temp_dir_path)
185+
if err:
186+
result.fail(ERR_QUERY_FAILED, "Unable to create directory temporary files \"%s\" with error %s \"%s\"" %
187+
[global_temp_dir_path, err, error_string(err)])
188+
return result
189+
var global_temp_png_path: String = global_temp_dir_path.path_join("temp.png")
190+
191+
var command: String = os_command_result.value.strip_edges()
192+
var arguments: PackedStringArray = \
193+
os_command_arguments_result.value + \
194+
PackedStringArray([
196195
"--export-sequence",
197196
"--export-filename", global_temp_png_path,
198-
global_temp_kra_path]),
199-
output, true, false)
197+
global_temp_kra_path])
198+
199+
var output: Array
200+
var exit_code: int = OS.execute(command, arguments, output, true, false)
200201
if exit_code:
201-
result.fail(ERR_QUERY_FAILED, "An error occurred while executing the Krita command. Process exited with code %s" % [exit_code])
202+
for arg_index in arguments.size():
203+
arguments[arg_index] = "\nArgument: " + arguments[arg_index]
204+
result.fail(ERR_QUERY_FAILED, " ".join([
205+
"An error occurred while executing the Krita command.",
206+
"Process exited with code %s:\nCommand: %s%s"
207+
]) % [exit_code, command, "".join(arguments)])
202208
return result
203209

204210
if global_temp_kra_path != global_source_file_path:

0 commit comments

Comments
 (0)