@@ -5,7 +5,9 @@ local M = {}
5
5
local PREFIX = " zip:"
6
6
7
7
--- @type table<hash , atlas_mount_data>
8
- local files = {}
8
+ local atlas_mounts = {}
9
+ --- @type table<hash , atlas_file>
10
+ local atlas_files = {}
9
11
local progress = 0
10
12
local files_count = 0
11
13
local max_priority = 0
@@ -45,7 +47,6 @@ M.attempt_count = 50 ---Attempts count for loading mount
45
47
46
48
--- @class atlas_mount_data
47
49
--- @field file_name string
48
- --- @field path_to_file string
49
50
--- @field priority number
50
51
--- @field allow_old_mount boolean
51
52
--- @field mount_key hash
@@ -54,11 +55,20 @@ M.attempt_count = 50 ---Attempts count for loading mount
54
55
--- @field old_mount_using boolean
55
56
--- @field loaded boolean
56
57
58
+ --- @class atlas_file
59
+ --- @field file_name string
60
+ --- @field path_to_file string
61
+ --- @field mounts table<hash , atlas_mount_data>
62
+ --- @field processing boolean
63
+ --- @field loaded boolean
64
+ --- @field priority number
65
+
57
66
--- Check if the data already mounted
58
67
--- @param data atlas_mount_data
59
- --- @param mounts atlas_mount[]
68
+ --- @param mounts atlas_mount[] | nil
60
69
--- @return boolean
61
70
local function is_in_mount (data , mounts )
71
+ mounts = mounts or liveupdate .get_mounts ()
62
72
for key , mount in pairs (mounts ) do
63
73
if data .file_name == mount .name then
64
74
return true
71
81
--- @param mount_key hash
72
82
--- @return atlas_mount_data
73
83
local function get_data (mount_key )
74
- local data = files [mount_key ]
84
+ local data = atlas_mounts [mount_key ]
75
85
if not data then
76
86
--- @type atlas_mount_data
77
87
data = {
78
88
mount_key = mount_key ,
79
89
file_name = " " ,
80
- path_to_file = " ./" ,
81
90
priority = 0 ,
82
91
allow_old_mount = false ,
83
92
proxies = {},
84
93
processing = false ,
85
94
loaded = false ,
86
95
old_mount_using = false
87
96
}
88
- files [mount_key ] = data
97
+ atlas_mounts [mount_key ] = data
89
98
files_count = files_count + 1
90
99
end
91
100
return data
92
101
end
93
102
103
+ --- Return file
104
+ --- @param mount atlas_mount_data
105
+ --- @return atlas_file
106
+ local function get_file (mount )
107
+ local h_name = hash (mount .file_name )
108
+ local data = atlas_files [h_name ]
109
+ if not data then
110
+ --- @type atlas_file
111
+ data = {
112
+ loaded = false ,
113
+ mounts = {[mount .mount_key ] = mount },
114
+ file_name = mount .file_name ,
115
+ path_to_file = " ./" ,
116
+ processing = false ,
117
+ priority = 0
118
+ }
119
+ atlas_files [h_name ] = data
120
+ return data
121
+ else
122
+ data .mounts [mount .mount_key ] = mount
123
+ end
124
+ return data
125
+ end
126
+
94
127
--- Increase progress value
95
128
--- @param value number
96
129
local function add_progress (value )
107
140
--- Check for complete loading
108
141
local function check_for_complete ()
109
142
local process_files = false
110
- for key , data in pairs (files ) do
143
+ for key , data in pairs (atlas_mounts ) do
111
144
if data .processing then
112
145
process_files = true
113
146
break
@@ -135,53 +168,59 @@ local function mount_loaded(data)
135
168
end
136
169
137
170
--- Handle error
138
- --- @param data atlas_mount_data
171
+ --- @param file_data atlas_file
139
172
--- @param error string
140
- local function on_error (data , error )
141
- data .loaded = false
142
- data .processing = false
143
- if data .allow_old_mount then
144
- data .old_mount_using = true
145
- for key , url in pairs (data .proxies ) do
146
- msg .post (url , CHECK_PROXY_STATE )
173
+ local function on_error (file_data , error )
174
+ file_data .loaded = false
175
+ file_data .processing = false
176
+ for key , mount in pairs (file_data .mounts ) do
177
+ mount .loaded = false
178
+ mount .processing = false
179
+ if mount .allow_old_mount then
180
+ mount .old_mount_using = true
181
+ for key , url in pairs (mount .proxies ) do
182
+ msg .post (url , CHECK_PROXY_STATE )
183
+ end
184
+ else
185
+ add_progress (PROGRESS_FILE_ERROR )
147
186
end
148
- else
149
- add_progress (PROGRESS_FILE_ERROR )
187
+ M .events .trigger (M .EVENTS .DATA_NOT_LOADED , {mount_key = mount .mount_key , error = tostring (error )})
150
188
end
151
189
if M .use_html_loader and html_loader then
152
190
html_loader .set_text (" Loading error" )
153
191
end
154
- M .events .trigger (M .EVENTS .DATA_NOT_LOADED , {mount_key = data .mount_key , error = tostring (error )})
155
192
check_for_complete ()
156
193
end
157
194
158
195
--- Mount data
159
- --- @param data atlas_mount_data
196
+ --- @param data atlas_file
160
197
--- @param save_path string
161
- --- @param priority number
162
- local function on_file_received (data , save_path , priority )
163
- liveupdate .add_mount (data .file_name , PREFIX .. save_path , priority , function (self , name , uri , priority )
164
- mount_loaded (data )
198
+ local function on_file_received (data , save_path )
199
+ liveupdate .add_mount (data .file_name , PREFIX .. save_path , data .priority , function (self , name , uri , priority )
200
+ data .processing = false
201
+ data .loaded = true
202
+ for key , mount in pairs (data .mounts ) do
203
+ mount_loaded (mount )
204
+ end
165
205
end )
166
206
end
167
207
168
208
--- Request data
169
- --- @param data atlas_mount_data
170
- --- @param index number
209
+ --- @param data atlas_file
171
210
--- @param attempt number | nil
172
- local function request_data (data , index , attempt )
211
+ local function request_data (data , attempt )
173
212
attempt = attempt or 1
174
213
-- global used here to workaround this issue until it will be fixed in the engine:
175
214
-- https://github.com/defold/defold/pull/8906/files#r1729148892
176
215
--- TODO: add info about loading
177
216
_G .atlas_loader_save_path = sys .get_save_file (M .external_key , data .file_name )
178
217
http .request (data .path_to_file .. data .file_name , " GET" , function (self , id , response )
179
218
if (response .status == 200 or response .status == 304 ) and response .error == nil then
180
- on_file_received (data , response .path , index )
219
+ on_file_received (data , response .path )
181
220
elseif response .status >= 400 or response .status == 0 or response .error ~= nil then
182
221
if attempt <= M .attempt_count then
183
222
attempt = attempt + 1
184
- request_data (data , index , attempt )
223
+ request_data (data , attempt )
185
224
else
186
225
on_error (data , response .error )
187
226
end
229
268
--- @param mount_key hash
230
269
--- @return atlas_mount_data | nil
231
270
function M .get_mount_data (mount_key )
232
- return files [mount_key ]
271
+ return atlas_mounts [mount_key ]
272
+ end
273
+
274
+ --- Return file data
275
+ --- @param file_name string
276
+ --- @return atlas_file | nil
277
+ function M .get_file_data (file_name )
278
+ return atlas_files [hash (file_name )]
233
279
end
234
280
235
281
--- Load external data
@@ -240,7 +286,6 @@ function M.load(info)
240
286
assert (not data .loaded , " Mount already loaded. File name: " .. tostring (data .file_name ) .. " ; Mount key: " .. tostring (data .mount_key ))
241
287
data .allow_old_mount = info .allow_old_mount == true
242
288
data .priority = info .priority and info .priority or data .priority
243
- data .path_to_file = info .path_to_file and info .path_to_file or data .path_to_file
244
289
data .file_name = info .file_name and info .file_name or data .file_name
245
290
data .processing = true
246
291
if M .use_html_loader and html_loader then
@@ -254,21 +299,30 @@ function M.load(info)
254
299
if is_in_mount (data , mounts ) then
255
300
mount_loaded (data )
256
301
else
257
- if not info .priority then
258
- data .priority = get_max_priority (mounts )
259
- end
260
- if data .priority > max_priority then
261
- max_priority = data .priority
302
+ local file_data = get_file (data )
303
+ if file_data .loaded then
304
+ data .priority = file_data .priority
305
+ mount_loaded (data )
306
+ elseif not file_data .processing then
307
+ if not info .priority then
308
+ data .priority = get_max_priority (mounts )
309
+ end
310
+ if data .priority > max_priority then
311
+ max_priority = data .priority
312
+ end
313
+ file_data .processing = true
314
+ file_data .path_to_file = info .path_to_file and info .path_to_file or file_data .path_to_file
315
+ file_data .priority = data .priority
316
+ request_data (file_data )
262
317
end
263
- request_data (data , data .priority )
264
318
end
265
319
end
266
320
end
267
321
268
322
--- Check if any old mount is using
269
323
--- @return boolean
270
324
function M .is_old_mount_using ()
271
- for i , data in pairs (files ) do
325
+ for i , data in pairs (atlas_mounts ) do
272
326
if data .old_mount_using then
273
327
return true
274
328
end
@@ -289,7 +343,7 @@ function M.remove_free_mounts()
289
343
for key , mount in pairs (mounts ) do
290
344
if mount .name ~= BASE then
291
345
old_mount = true
292
- for i , data in pairs (files ) do
346
+ for i , data in pairs (atlas_mounts ) do
293
347
if mount .name == data .file_name then
294
348
old_mount = false
295
349
break
0 commit comments