forked from RodZill4/material-maker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstart.gd
159 lines (150 loc) · 4.68 KB
/
start.gd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
extends Control
var resource = null
var progress : float = 0.0
onready var progress_bar = $VBoxContainer/ProgressBar
onready var mutex : Mutex = Mutex.new()
onready var semaphore : Semaphore = Semaphore.new()
onready var thread: Thread = Thread.new()
func _ready():
randomize()
set_process(false)
var resource_path : String
if Directory.new().file_exists("res://material_maker/main_window.tscn"):
if OS.get_cmdline_args().size() > 0 and (OS.get_cmdline_args()[0] == "--export" or OS.get_cmdline_args()[0] == "--export-material"):
var output = []
var dir : Directory = Directory.new()
match OS.get_name():
"Windows":
var bat_file_path : String = OS.get_system_dir(OS.SYSTEM_DIR_DOWNLOADS)+"\\mm_cd.bat"
var bat_file : File = File.new()
bat_file.open(bat_file_path, File.WRITE)
bat_file.store_line("cd")
bat_file.close()
OS.execute(bat_file_path, [], true, output)
dir.remove(bat_file_path)
dir.change_dir(output[0].split("\n")[2])
var target : String = "Godot"
var output_dir : String = dir.get_current_dir()
var size : int = 0
var files : Array = []
var i = 1
while i < OS.get_cmdline_args().size():
match OS.get_cmdline_args()[i]:
"-t", "--target":
i += 1
target = OS.get_cmdline_args()[i]
"-o", "--output-dir":
i += 1
output_dir = OS.get_cmdline_args()[i]
"--size":
i += 1
size = int(OS.get_cmdline_args()[i])
if size < 0:
show_error("ERROR: incorrect size "+OS.get_cmdline_args()[i])
return
_:
files.push_back(OS.get_cmdline_args()[i])
i += 1
if !dir.dir_exists(output_dir):
show_error("ERROR: Output directory '%s' does not exist" % output_dir)
return
var expanded_files = []
for f in files:
var basedir : String = f.get_base_dir()
if basedir == "":
basedir = "."
var basename : String = f.get_file()
if basename.find("*") != -1:
basename = basename.replace("*", ".*")
if dir.open(basedir) == OK:
var regex : RegEx = RegEx.new()
regex.compile("^"+basename+"$")
dir.list_dir_begin()
var file_name = dir.get_next()
while file_name != "":
if regex.search(file_name) and file_name.get_extension() == "ptex":
expanded_files.push_back(basedir+"/"+file_name)
file_name = dir.get_next()
else:
expanded_files.push_back(f)
export_files(expanded_files, output_dir, target, size)
return
else:
resource_path = "res://material_maker/main_window.tscn"
else:
resource_path = "res://demo/demo.tscn"
var locale = load("res://material_maker/locale/locale.gd").new()
locale.read_translations()
set_process(true)
thread.start(self, "load_resource", resource_path, Thread.PRIORITY_HIGH)
func load_resource(resource_path : String):
var loader : ResourceInteractiveLoader = ResourceLoader.load_interactive(resource_path)
if loader == null: # check for errors
return
while true:
mutex.lock()
if false:
# fake loading mode
progress += 0.02
if progress > 1:
progress = 0
else:
var err = loader.poll()
if err == ERR_FILE_EOF:
resource = loader.get_resource()
progress = -1
mutex.unlock()
return
elif err == OK:
progress = float(loader.get_stage()) / loader.get_stage_count()
else:
progress = -1
mutex.unlock()
return
mutex.unlock()
semaphore.wait()
var wait : float = 0.0
func _process(delta) -> void:
wait += delta
if wait < 0.01:
return
wait = 0.0
if mutex.try_lock() == OK:
if progress < 0:
if resource == null:
print("error")
queue_free()
else:
var scene = resource.instance()
get_node("/root").add_child(scene)
thread.wait_to_finish()
queue_free()
else:
progress_bar.value = 100.0*progress
mutex.unlock()
semaphore.post()
func export_files(files, output_dir, target, size) -> void:
$VBoxContainer/ProgressBar.min_value = 0
$VBoxContainer/ProgressBar.max_value = files.size()
$VBoxContainer/ProgressBar.value = 0
for f in files:
var gen = mm_loader.load_gen(f)
if gen != null:
add_child(gen)
for c in gen.get_children():
if c.has_method("export_material"):
if c.has_method("get_export_profiles"):
if c.get_export_profiles().find(target) == -1:
show_error("ERROR: Unsupported target %s"+target)
continue
$VBoxContainer/Label.text = "Exporting "+f.get_file()
var prefix : String = output_dir+"/"+f.get_file().get_basename()
var result = c.export_material(prefix, target, size)
while result is GDScriptFunctionState:
result = yield(result, "completed")
gen.queue_free()
$VBoxContainer/ProgressBar.value += 1
get_tree().quit()
func show_error(message : String):
$ErrorPanel.show()
$ErrorPanel/Label.text = message