-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy paththread.gd
90 lines (57 loc) · 1.69 KB
/
thread.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
class_name OKThread extends Thread
signal module_loaded
signal load_progress
signal load_canceled
var _mutex: Mutex
var _load_counter: int
var _is_cancelled: bool
# Initialization
func _init(type: String):
set_meta("type", type)
_mutex = Mutex.new()
# Public Methods
func load_module(module: String, paths: Array):
var userdata = {"module": module, "paths": paths}
start(self, "_load_module", userdata, Thread.PRIORITY_HIGH)
func load_content(path: String) -> Resource:
var resource = ResourceLoader.load(path)
if resource == null:
printerr("Unable to load content")
return resource
func cancel():
_is_cancelled = true
# Private Method
func _load_module(userdata: Dictionary):
_is_cancelled = false
_load_counter = 0
var assets = {}
for path in userdata.paths:
if _is_cancelled:
_emit_cancel(userdata)
return
var resource = ResourceLoader.load(path)
_emit_progress()
match resource == null:
true: printerr("Unable to load content")
false: assets[path] = resource
_emit_loaded(userdata.module, assets)
# Emit Signals
func _emit_loaded(module: String, assets: Dictionary):
var result = {"name": module, "assets": assets}
_mutex.lock()
call_deferred("emit_signal", "module_loaded", result)
call_deferred("wait_to_finish")
_mutex.unlock()
func _emit_progress():
_mutex.lock()
_load_counter += 1
call_deferred("emit_signal", "load_progress")
_mutex.unlock()
func _emit_cancel(userdata: Dictionary):
var paths_count = userdata.paths.size()
var count = (paths_count - _load_counter)
var result = {"thread": self, "count": count}
_mutex.lock()
call_deferred("emit_signal", "load_canceled", result)
call_deferred("wait_to_finish")
_mutex.unlock()