Skip to content

Commit 00fe32f

Browse files
committed
Unpack downloaded model file
1 parent 9212776 commit 00fe32f

File tree

3 files changed

+94
-12
lines changed

3 files changed

+94
-12
lines changed

addons/sketchfab/ModelDialog.gd

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -128,24 +128,46 @@ func _on_Download_pressed():
128128
var file_regex = RegEx.new()
129129
file_regex.compile("[^/]+?\\.zip")
130130
var filename = file_regex.search(url).get_string()
131+
var zip_path = "res://sketchfab/%s" % filename
131132

132133
downloader.connect("download_progressed", self, "_on_download_progressed")
133-
downloader.request(path, null, { "download_to": "res://sketchfab/%s" % filename })
134+
downloader.request(path, null, { "download_to": zip_path })
134135
result = yield(downloader, "completed")
136+
if !result:
137+
return
135138
downloader.term()
136139
downloader = null
137140

138-
download.visible = true
139-
progress.visible = false
140-
size_label.visible = false
141-
if result:
142-
if result.ok && result.code == 200:
143-
download.text = "Model downloaded!"
144-
download.disabled = true
145-
else:
146-
OS.alert(
147-
"Please check your network connectivity, free disk space and try again.",
148-
"Download error")
141+
if !result.ok || result.code != 200:
142+
download.visible = true
143+
progress.visible = false
144+
size_label.visible = false
145+
OS.alert(
146+
"Please check your network connectivity, free disk space and try again.",
147+
"Download error")
148+
return
149+
150+
# Unpack
151+
152+
progress.percent_visible = false
153+
size_label.text = " Model downloaded! Unpacking..."
154+
yield(get_tree(), "idle_frame")
155+
if !get_tree():
156+
return
157+
158+
var out = []
159+
OS.execute(OS.get_executable_path(), [
160+
"-s", ProjectSettings.globalize_path("res://addons/sketchfab/unzip.gd"),
161+
"--zip-to-unpack %s" % ProjectSettings.globalize_path(zip_path),
162+
"--no-window",
163+
"--quit",
164+
], true, out)
165+
print(out)
166+
167+
size_label.text = " Model unpacked into project!"
168+
169+
# Trigger import
170+
get_tree().get_meta("__editor_interface").get_resource_filesystem().scan()
149171

150172
func _on_download_progressed(bytes, total_bytes):
151173
if !get_tree():

addons/sketchfab/plugin.gd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const Utils = preload("res://addons/sketchfab/Utils.gd")
66
var main = preload("res://addons/sketchfab/Main.tscn").instance()
77

88
func _enter_tree():
9+
get_tree().set_meta("__editor_interface", get_editor_interface())
910
get_editor_interface().get_editor_viewport().add_child(main)
1011
main.visible = false
1112

addons/sketchfab/unzip.gd

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
extends SceneTree
2+
3+
const ARG_PREFIX = "--zip-to-unpack "
4+
5+
func _init():
6+
print("Unzipper started")
7+
8+
var zip_path
9+
for arg in OS.get_cmdline_args():
10+
if arg.begins_with(ARG_PREFIX):
11+
zip_path = arg.right(ARG_PREFIX.length())
12+
break
13+
14+
if !zip_path:
15+
print("No file specified")
16+
return
17+
18+
print("Unpacking %s..." % zip_path)
19+
20+
if !ProjectSettings.load_resource_pack(zip_path):
21+
print("Package file not found")
22+
return
23+
24+
var name_regex = RegEx.new()
25+
name_regex.compile("([^/\\\\]+)\\.zip")
26+
var base_name = name_regex.search(zip_path).get_string(1)
27+
28+
var out_path = zip_path.left(zip_path.find(base_name)) + base_name + "/"
29+
Directory.new().make_dir(out_path)
30+
unpack_dir("res://", out_path)
31+
32+
print("Done!")
33+
34+
func unpack_dir(src_path, out_path):
35+
print("Directory: %s -> %s" % [src_path, out_path])
36+
37+
var dir = Directory.new()
38+
dir.open(src_path)
39+
dir.list_dir_begin(true)
40+
41+
var file_name = dir.get_next()
42+
while file_name != "":
43+
if dir.current_is_dir():
44+
var new_src_path = "%s%s/" % [src_path, file_name]
45+
var new_out_path = "%s%s/" % [out_path, file_name]
46+
Directory.new().make_dir(new_out_path)
47+
unpack_dir(new_src_path, new_out_path)
48+
else:
49+
var file_src_path = "%s%s" % [src_path, file_name]
50+
var file_out_path = "%s%s" % [out_path, file_name]
51+
print("File: %s -> %s" % [file_src_path, file_out_path])
52+
var file = File.new()
53+
file.open(file_src_path, File.READ)
54+
var data = file.get_buffer(file.get_len())
55+
file.close()
56+
file.open(file_out_path, File.WRITE)
57+
file.store_buffer(data)
58+
file.close()
59+
file_name = dir.get_next()

0 commit comments

Comments
 (0)