Skip to content

Commit 7f7377d

Browse files
committed
a separated directory with a unique name for each import closes #18
1 parent 2cfa205 commit 7f7377d

File tree

6 files changed

+194
-63
lines changed

6 files changed

+194
-63
lines changed
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
extends Object
2+
3+
const _Result = preload("result.gd").Class
4+
5+
class CreationResult:
6+
extends _Result
7+
var path: String
8+
func success(path: String) -> void:
9+
super._success()
10+
self.path = path
11+
12+
class RemovalResult:
13+
extends _Result
14+
15+
static func create_directory_with_unique_name(base_directory_path: String) -> CreationResult:
16+
const error_description: String = "Failed to create a directory with unique name"
17+
var name: String
18+
var path: String
19+
var result = CreationResult.new()
20+
21+
var error = DirAccess.make_dir_recursive_absolute(base_directory_path)
22+
match error:
23+
OK, ERR_ALREADY_EXISTS:
24+
pass
25+
_:
26+
var inner_result: CreationResult = CreationResult.new()
27+
inner_result.fail(ERR_QUERY_FAILED, "Failed to create base directory recursive")
28+
result.fail(
29+
ERR_CANT_CREATE,
30+
"%s: %s \"%s\"" %
31+
[error_description, error, error_string(error)],
32+
inner_result)
33+
return result
34+
35+
while true:
36+
name = "%d" % (Time.get_unix_time_from_system() * 1000)
37+
path = base_directory_path.path_join(name)
38+
if not DirAccess.dir_exists_absolute(path):
39+
error = DirAccess.make_dir_absolute(path)
40+
match error:
41+
ERR_ALREADY_EXISTS:
42+
pass
43+
OK:
44+
result.success(path)
45+
break
46+
_:
47+
result.fail(
48+
ERR_CANT_CREATE,
49+
"%s: %s \"%s\"" %
50+
[error_description, error, error_string(error)])
51+
break
52+
return result
53+
54+
static func remove_dir_recursive(dir_path: String) -> RemovalResult:
55+
const error_description: String = "Failed to remove a directory with contents recursive"
56+
var result: RemovalResult = RemovalResult.new()
57+
for child_file_name in DirAccess.get_files_at(dir_path):
58+
var child_file_path = dir_path.path_join(child_file_name)
59+
var error: Error = DirAccess.remove_absolute(child_file_path)
60+
if error:
61+
var inner_result: RemovalResult = RemovalResult.new()
62+
inner_result.fail(
63+
ERR_QUERY_FAILED,
64+
"Failed to remove a file: \"%s\". Error: %s \"%s\"" %
65+
[child_file_path, error, error_string(error)])
66+
result.fail(ERR_QUERY_FAILED, "%s: \"%s\"" % [error_description, dir_path], inner_result)
67+
return result
68+
for child_dir_name in DirAccess.get_directories_at(dir_path):
69+
var child_dir_path = dir_path.path_join(child_dir_name)
70+
var inner_result: RemovalResult = remove_dir_recursive(child_dir_path)
71+
if inner_result.error:
72+
result.fail(ERR_QUERY_FAILED, "%s: \"%s\"" % [error_description, dir_path], inner_result)
73+
return result
74+
var error: Error = DirAccess.remove_absolute(dir_path)
75+
if error:
76+
result.fail(
77+
ERR_QUERY_FAILED,
78+
"%s: \"%s\". Error: %s \"%s\"" %
79+
[error_description, dir_path, error, error_string(error)])
80+
return result

addons/nklbdev.importality/export/_.gd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const _Result = preload("../result.gd").Class
55
const _Common = preload("../common.gd")
66
const _Options = preload("../options.gd")
77
const _ProjectSetting = preload("../project_setting.gd")
8+
const _DirAccessExtensions = preload("../dir_access_ext.gd")
89

910
const _SpriteSheetBuilderBase = preload("../sprite_sheet_builder/_.gd")
1011
const _GridBasedSpriteSheetBuilder = preload("../sprite_sheet_builder/grid_based.gd")

addons/nklbdev.importality/export/aseprite.gd

Lines changed: 33 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -44,16 +44,17 @@ func _export(res_source_file_path: String, options: Dictionary) -> ExportResult:
4444
return result
4545
var global_temp_dir_path: String = ProjectSettings.globalize_path(
4646
temp_dir_path_result.value.strip_edges())
47+
var unique_temp_dir_creation_result: _DirAccessExtensions.CreationResult = \
48+
_DirAccessExtensions.create_directory_with_unique_name(global_temp_dir_path)
49+
if unique_temp_dir_creation_result.error:
50+
result.fail(ERR_QUERY_FAILED, "Failed to create unique temporary directory to export spritesheet", unique_temp_dir_creation_result)
51+
return result
52+
var unique_temp_dir_path: String = unique_temp_dir_creation_result.path
4753

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, "Failed to create directory for temporary files \"%s\" with error %s \"%s\"" %
52-
[global_temp_dir_path, err, error_string(err)])
53-
return result
54+
var global_source_file_path: String = ProjectSettings.globalize_path(res_source_file_path)
5455

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")
56+
var global_png_path: String = unique_temp_dir_path.path_join("temp.png")
57+
var global_json_path: String = unique_temp_dir_path.path_join("temp.json")
5758

5859
var command: String = os_command_result.value.strip_edges()
5960
var arguments: PackedStringArray = \
@@ -64,7 +65,7 @@ func _export(res_source_file_path: String, options: Dictionary) -> ExportResult:
6465
"--list-tags",
6566
"--sheet", global_png_path,
6667
"--data", global_json_path,
67-
ProjectSettings.globalize_path(res_source_file_path)])
68+
global_source_file_path])
6869

6970
var output: Array = []
7071
var exit_code: int = OS.execute(command, arguments, output, true, false)
@@ -77,13 +78,11 @@ func _export(res_source_file_path: String, options: Dictionary) -> ExportResult:
7778
]) % [exit_code, command, "".join(arguments)])
7879
return result
7980
var raw_atlas_image: Image = Image.load_from_file(global_png_path)
80-
DirAccess.remove_absolute(global_png_path)
8181
var json = JSON.new()
8282
err = json.parse(FileAccess.get_file_as_string(global_json_path))
8383
if err:
8484
result.fail(ERR_INVALID_DATA, "Failed to parse sprite sheet json data with error %s \"%s\"" % [err, error_string(err)])
8585
return result
86-
DirAccess.remove_absolute(global_json_path)
8786
var raw_sprite_sheet_data: Dictionary = json.data
8887

8988
var sprite_sheet_layout: _Common.SpriteSheetLayout = options[_Options.SPRITE_SHEET_LAYOUT]
@@ -186,6 +185,11 @@ func _export(res_source_file_path: String, options: Dictionary) -> ExportResult:
186185
if not autoplay_animation_name.is_empty() and animation_library.autoplay_index < 0:
187186
push_warning("Autoplay animation name not found: \"%s\". Continuing..." % [autoplay_animation_name])
188187

188+
if _DirAccessExtensions.remove_dir_recursive(unique_temp_dir_path).error:
189+
push_warning(
190+
"Failed to remove unique temporary directory: \"%s\"" %
191+
[unique_temp_dir_path])
192+
189193
result.success(sprite_sheet_building_result.atlas_image, sprite_sheet, animation_library)
190194
return result
191195

@@ -225,20 +229,21 @@ class CustomImageFormatLoaderExtension:
225229
push_error(os_command_arguments_result.error_description)
226230
return os_command_arguments_result.error
227231

228-
var temp_dir_path_result: _ProjectSetting.GettingValueResult = __common_temporary_files_directory_path_project_setting.get_value()
232+
var temp_dir_path_result: _ProjectSetting.GettingValueResult = _Common.common_temporary_files_directory_path_project_setting.get_value()
229233
if temp_dir_path_result.error:
230-
push_error(temp_dir_path_result.error_description)
234+
push_error("Failed to get Temporary Files Directory Path to export spritesheet")
231235
return temp_dir_path_result.error
232-
var global_temp_dir_path: String = ProjectSettings.globalize_path(temp_dir_path_result.value.strip_edges())
233-
234-
var global_png_path: String = global_temp_dir_path.path_join("temp.png")
235-
var global_json_path: String = global_temp_dir_path.path_join("temp.json")
236-
if not DirAccess.dir_exists_absolute(global_temp_dir_path):
237-
err = DirAccess.make_dir_recursive_absolute(global_temp_dir_path)
238-
if err:
239-
push_error("Failed to create directory for temporary files \"%s\" with error %s \"%s\"" %
240-
[global_temp_dir_path, err, error_string(err)])
241-
return ERR_QUERY_FAILED
236+
var global_temp_dir_path: String = ProjectSettings.globalize_path(
237+
temp_dir_path_result.value.strip_edges())
238+
var unique_temp_dir_creation_result: _DirAccessExtensions.CreationResult = \
239+
_DirAccessExtensions.create_directory_with_unique_name(global_temp_dir_path)
240+
if unique_temp_dir_creation_result.error:
241+
push_error("Failed to create unique temporary directory to export spritesheet")
242+
return unique_temp_dir_creation_result.error
243+
var unique_temp_dir_path: String = unique_temp_dir_creation_result.path
244+
245+
var global_png_path: String = unique_temp_dir_path.path_join("temp.png")
246+
var global_json_path: String = unique_temp_dir_path.path_join("temp.json")
242247

243248
var command: String = os_command_result.value.strip_edges()
244249
var arguments: PackedStringArray = \
@@ -264,17 +269,20 @@ class CustomImageFormatLoaderExtension:
264269
return ERR_QUERY_FAILED
265270

266271
var raw_atlas_image: Image = Image.load_from_file(global_png_path)
267-
DirAccess.remove_absolute(global_png_path)
268272
var json = JSON.new()
269273
err = json.parse(FileAccess.get_file_as_string(global_json_path))
270274
if err:
271275
push_error("Failed to parse sprite sheet json data with error %s \"%s\"" % [err, error_string(err)])
272276
return ERR_INVALID_DATA
273-
DirAccess.remove_absolute(global_json_path)
274277
var raw_sprite_sheet_data: Dictionary = json.data
275278

276279
var source_image_size: Vector2i = _Common.get_vector2i(
277280
raw_sprite_sheet_data.frames[0].sourceSize, "w", "h")
278281

282+
if _DirAccessExtensions.remove_dir_recursive(unique_temp_dir_path).error:
283+
push_warning(
284+
"Failed to remove unique temporary directory: \"%s\"" %
285+
[unique_temp_dir_path])
286+
279287
image.copy_from(raw_atlas_image.get_region(Rect2i(Vector2i.ZERO, source_image_size)))
280288
return OK

addons/nklbdev.importality/export/krita.gd

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,14 @@ func _export(res_source_file_path: String, options: Dictionary) -> ExportResult:
5151
if temp_dir_path_result.error:
5252
result.fail(ERR_UNCONFIGURED, "Failed to get Temporary Files Directory Path to export spritesheet", temp_dir_path_result)
5353
return result
54-
var global_temp_dir_path: String = ProjectSettings.globalize_path(temp_dir_path_result.value.strip_edges())
54+
var global_temp_dir_path: String = ProjectSettings.globalize_path(
55+
temp_dir_path_result.value.strip_edges())
56+
var unique_temp_dir_creation_result: _DirAccessExtensions.CreationResult = \
57+
_DirAccessExtensions.create_directory_with_unique_name(global_temp_dir_path)
58+
if unique_temp_dir_creation_result.error:
59+
result.fail(ERR_QUERY_FAILED, "Failed to create unique temporary directory to export spritesheet", unique_temp_dir_creation_result)
60+
return result
61+
var unique_temp_dir_path: String = unique_temp_dir_creation_result.path
5562

5663
var global_source_file_path: String = ProjectSettings.globalize_path(res_source_file_path)
5764

@@ -146,7 +153,7 @@ func _export(res_source_file_path: String, options: Dictionary) -> ExportResult:
146153
animation_range_xml_element.attributes["from"] = str(first_animations_frame_index)
147154
animation_range_xml_element.attributes["to"] = str(last_animations_frame_index)
148155

149-
global_temp_kra_path = temp_dir_path_result.value.path_join(temp_kra_file_name)
156+
global_temp_kra_path = unique_temp_dir_path.path_join(temp_kra_file_name)
150157

151158
animation_index_animation_metadata_range_xml_element.attributes["from"] = str(first_animations_frame_index)
152159
animation_index_animation_metadata_range_xml_element.attributes["to"] = str(last_animations_frame_index)
@@ -180,13 +187,7 @@ func _export(res_source_file_path: String, options: Dictionary) -> ExportResult:
180187

181188
zip_reader.close()
182189

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, "Failed 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_pattern: String = global_temp_dir_path.path_join(temp_png_file_name_pattern)
190+
var global_temp_png_path_pattern: String = unique_temp_dir_path.path_join(temp_png_file_name_pattern)
190191

191192
var command: String = os_command_result.value.strip_edges()
192193
var arguments: PackedStringArray = \
@@ -207,21 +208,16 @@ func _export(res_source_file_path: String, options: Dictionary) -> ExportResult:
207208
]) % [exit_code, command, "".join(arguments)])
208209
return result
209210

210-
if global_temp_kra_path != global_source_file_path:
211-
DirAccess.remove_absolute(global_temp_kra_path)
212-
pass
213-
214211
var unique_frames_count: int = last_animations_frame_index + 1 # - first_stories_frame
215212
var frames_images: Array[Image]
216213
for image_idx in unique_frames_count:
217-
var global_frame_png_path: String = temp_dir_path_result.value \
214+
var global_frame_png_path: String = unique_temp_dir_path \
218215
.path_join("%s%04d.png" % [temp_file_base_name, image_idx])
219216
if FileAccess.file_exists(global_frame_png_path):
220217
var image: Image = Image.load_from_file(global_frame_png_path)
221218
frames_images.push_back(image)
222219
else:
223220
frames_images.push_back(frames_images.back())
224-
DirAccess.remove_absolute(global_frame_png_path)
225221

226222
var sprite_sheet_builder: _SpriteSheetBuilderBase = _create_sprite_sheet_builder(options)
227223

@@ -263,6 +259,11 @@ func _export(res_source_file_path: String, options: Dictionary) -> ExportResult:
263259
if not autoplay_animation_name.is_empty() and animation_library.autoplay_index < 0:
264260
push_warning("Autoplay animation name not found: \"%s\". Continuing..." % [autoplay_animation_name])
265261

262+
if _DirAccessExtensions.remove_dir_recursive(unique_temp_dir_path).error:
263+
push_warning(
264+
"Failed to remove unique temporary directory: \"%s\"" %
265+
[unique_temp_dir_path])
266+
266267
result.success(sprite_sheet_building_result.atlas_image, sprite_sheet, animation_library)
267268
return result
268269

addons/nklbdev.importality/export/pencil2d.gd

Lines changed: 35 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,14 @@ func _export(res_source_file_path: String, options: Dictionary) -> ExportResult:
4343
if temp_dir_path_result.error:
4444
result.fail(ERR_UNCONFIGURED, "Failed to get Temporary Files Directory Path to export spritesheet", temp_dir_path_result)
4545
return result
46+
var global_temp_dir_path: String = ProjectSettings.globalize_path(
47+
temp_dir_path_result.value.strip_edges())
48+
var unique_temp_dir_creation_result: _DirAccessExtensions.CreationResult = \
49+
_DirAccessExtensions.create_directory_with_unique_name(global_temp_dir_path)
50+
if unique_temp_dir_creation_result.error:
51+
result.fail(ERR_QUERY_FAILED, "Failed to create unique temporary directory to export spritesheet", unique_temp_dir_creation_result)
52+
return result
53+
var unique_temp_dir_path: String = unique_temp_dir_creation_result.path
4654

4755
var global_source_file_path: String = ProjectSettings.globalize_path(res_source_file_path)
4856

@@ -93,14 +101,8 @@ func _export(res_source_file_path: String, options: Dictionary) -> ExportResult:
93101
# --end <frame> The last frame you want to include in the exported movie. Can also be last or last-sound to automatically use the last frame containing animation or sound respectively
94102
# --transparency Render transparency when possible
95103
# input Path to input pencil file
96-
var global_temp_dir_path: String = ProjectSettings.globalize_path(temp_dir_path_result.value.strip_edges())
97-
if not DirAccess.dir_exists_absolute(global_temp_dir_path):
98-
err = DirAccess.make_dir_recursive_absolute(global_temp_dir_path)
99-
if err:
100-
result.fail(ERR_UNCONFIGURED, "Failed to create directory for temporary files \"%s\" with error %s \"%s\"" %
101-
[global_temp_dir_path, err, error_string(err)])
102104
var png_base_name: String = "temp"
103-
var global_temp_png_path: String = temp_dir_path_result.value.path_join("%s.png" % png_base_name)
105+
var global_temp_png_path: String = unique_temp_dir_path.path_join("%s.png" % png_base_name)
104106

105107
var command: String = os_command_result.value.strip_edges()
106108
var arguments: PackedStringArray = \
@@ -125,12 +127,9 @@ func _export(res_source_file_path: String, options: Dictionary) -> ExportResult:
125127

126128
var frames_images: Array[Image]
127129
for image_idx in unique_frames_count:
128-
var global_frame_png_path: String = temp_dir_path_result.value \
130+
var global_frame_png_path: String = unique_temp_dir_path \
129131
.path_join("%s%04d.png" % [png_base_name, image_idx + 1])
130132
frames_images.push_back(Image.load_from_file(global_frame_png_path))
131-
var remove_frame_file_error: Error = DirAccess.remove_absolute(global_frame_png_path)
132-
if remove_frame_file_error: push_error("Failed to remove temp file: \"%s\" with error: %s %s, continuing..." %
133-
[global_frame_png_path, remove_frame_file_error, error_string(remove_frame_file_error)])
134133

135134
var sprite_sheet_builder: _SpriteSheetBuilderBase = _create_sprite_sheet_builder(options)
136135

@@ -171,6 +170,11 @@ func _export(res_source_file_path: String, options: Dictionary) -> ExportResult:
171170
if not autoplay_animation_name.is_empty() and animation_library.autoplay_index < 0:
172171
push_warning("Autoplay animation name not found: \"%s\". Continuing..." % [autoplay_animation_name])
173172

173+
if _DirAccessExtensions.remove_dir_recursive(unique_temp_dir_path).error:
174+
push_warning(
175+
"Failed to remove unique temporary directory: \"%s\"" %
176+
[unique_temp_dir_path])
177+
174178
result.success(sprite_sheet_building_result.atlas_image, sprite_sheet, animation_library)
175179
return result
176180

@@ -208,21 +212,23 @@ class CustomImageFormatLoaderExtension:
208212
push_error(os_command_arguments_result.error_description)
209213
return os_command_arguments_result.error
210214

211-
var temp_dir_path_result: _ProjectSetting.GettingValueResult = __common_temporary_files_directory_path_project_setting.get_value()
215+
var temp_dir_path_result: _ProjectSetting.GettingValueResult = _Common.common_temporary_files_directory_path_project_setting.get_value()
212216
if temp_dir_path_result.error:
213-
push_error(temp_dir_path_result.error_description)
217+
push_error("Failed to get Temporary Files Directory Path to export spritesheet")
214218
return temp_dir_path_result.error
219+
var global_temp_dir_path: String = ProjectSettings.globalize_path(
220+
temp_dir_path_result.value.strip_edges())
221+
var unique_temp_dir_creation_result: _DirAccessExtensions.CreationResult = \
222+
_DirAccessExtensions.create_directory_with_unique_name(global_temp_dir_path)
223+
if unique_temp_dir_creation_result.error:
224+
push_error("Failed to create unique temporary directory to export spritesheet")
225+
return unique_temp_dir_creation_result.error
226+
var unique_temp_dir_path: String = unique_temp_dir_creation_result.path
215227

216228
var global_source_file_path: String = ProjectSettings.globalize_path(file_access.get_path())
217-
var global_temp_dir_path: String = ProjectSettings.globalize_path(temp_dir_path_result.value.strip_edges())
218-
if not DirAccess.dir_exists_absolute(global_temp_dir_path):
219-
err = DirAccess.make_dir_recursive_absolute(global_temp_dir_path)
220-
if err:
221-
push_error("Failed to create directory for temporary files \"%s\" with error %s \"%s\"" %
222-
[global_temp_dir_path, err, error_string(err)])
223-
return ERR_QUERY_FAILED
224-
var png_base_name: String = "img"
225-
var global_temp_png_path: String = temp_dir_path_result.value.path_join("%s.png" % png_base_name)
229+
230+
const png_base_name: String = "img"
231+
var global_temp_png_path: String = unique_temp_dir_path.path_join("%s.png" % png_base_name)
226232

227233
var command: String = os_command_result.value.strip_edges()
228234
var arguments: PackedStringArray = \
@@ -245,11 +251,17 @@ class CustomImageFormatLoaderExtension:
245251
]) % [exit_code, command, "".join(arguments)])
246252
return ERR_QUERY_FAILED
247253

248-
var global_frame_png_path: String = temp_dir_path_result.value \
254+
var global_frame_png_path: String = unique_temp_dir_path \
249255
.path_join("%s0001.png" % [png_base_name])
250256
err = image.load_png_from_buffer(FileAccess.get_file_as_bytes(global_frame_png_path))
251257
if err:
252258
push_error("An error occurred while image loading")
253259
return err
260+
261+
if _DirAccessExtensions.remove_dir_recursive(unique_temp_dir_path).error:
262+
push_warning(
263+
"Failed to remove unique temporary directory: \"%s\"" %
264+
[unique_temp_dir_path])
265+
254266
return OK
255267

0 commit comments

Comments
 (0)