Skip to content

Commit ba5232d

Browse files
Added possibility for using one file for differnet atlas mounts
1 parent 5e9e4aa commit ba5232d

File tree

2 files changed

+93
-39
lines changed

2 files changed

+93
-39
lines changed

atlas_loader/mount_loader.lua

+92-38
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ local M = {}
55
local PREFIX = "zip:"
66

77
---@type table<hash, atlas_mount_data>
8-
local files = {}
8+
local atlas_mounts = {}
9+
---@type table<hash, atlas_file>
10+
local atlas_files = {}
911
local progress = 0
1012
local files_count = 0
1113
local max_priority = 0
@@ -45,7 +47,6 @@ M.attempt_count = 50 ---Attempts count for loading mount
4547

4648
---@class atlas_mount_data
4749
---@field file_name string
48-
---@field path_to_file string
4950
---@field priority number
5051
---@field allow_old_mount boolean
5152
---@field mount_key hash
@@ -54,11 +55,20 @@ M.attempt_count = 50 ---Attempts count for loading mount
5455
---@field old_mount_using boolean
5556
---@field loaded boolean
5657

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+
5766
---Check if the data already mounted
5867
---@param data atlas_mount_data
59-
---@param mounts atlas_mount[]
68+
---@param mounts atlas_mount[]|nil
6069
---@return boolean
6170
local function is_in_mount(data, mounts)
71+
mounts = mounts or liveupdate.get_mounts()
6272
for key, mount in pairs(mounts) do
6373
if data.file_name == mount.name then
6474
return true
@@ -71,26 +81,49 @@ end
7181
---@param mount_key hash
7282
---@return atlas_mount_data
7383
local function get_data(mount_key)
74-
local data = files[mount_key]
84+
local data = atlas_mounts[mount_key]
7585
if not data then
7686
---@type atlas_mount_data
7787
data = {
7888
mount_key = mount_key,
7989
file_name = "",
80-
path_to_file = "./",
8190
priority = 0,
8291
allow_old_mount = false,
8392
proxies = {},
8493
processing = false,
8594
loaded = false,
8695
old_mount_using = false
8796
}
88-
files[mount_key] = data
97+
atlas_mounts[mount_key] = data
8998
files_count = files_count + 1
9099
end
91100
return data
92101
end
93102

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+
94127
---Increase progress value
95128
---@param value number
96129
local function add_progress(value)
@@ -107,7 +140,7 @@ end
107140
---Check for complete loading
108141
local function check_for_complete()
109142
local process_files = false
110-
for key, data in pairs(files) do
143+
for key, data in pairs(atlas_mounts) do
111144
if data.processing then
112145
process_files = true
113146
break
@@ -135,53 +168,59 @@ local function mount_loaded(data)
135168
end
136169

137170
---Handle error
138-
---@param data atlas_mount_data
171+
---@param file_data atlas_file
139172
---@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)
147186
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)})
150188
end
151189
if M.use_html_loader and html_loader then
152190
html_loader.set_text("Loading error")
153191
end
154-
M.events.trigger(M.EVENTS.DATA_NOT_LOADED, {mount_key = data.mount_key, error = tostring(error)})
155192
check_for_complete()
156193
end
157194

158195
---Mount data
159-
---@param data atlas_mount_data
196+
---@param data atlas_file
160197
---@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
165205
end)
166206
end
167207

168208
---Request data
169-
---@param data atlas_mount_data
170-
---@param index number
209+
---@param data atlas_file
171210
---@param attempt number|nil
172-
local function request_data(data, index, attempt)
211+
local function request_data(data, attempt)
173212
attempt = attempt or 1
174213
-- global used here to workaround this issue until it will be fixed in the engine:
175214
-- https://github.com/defold/defold/pull/8906/files#r1729148892
176215
---TODO: add info about loading
177216
_G.atlas_loader_save_path = sys.get_save_file(M.external_key, data.file_name)
178217
http.request(data.path_to_file .. data.file_name, "GET", function(self, id, response)
179218
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)
181220
elseif response.status >= 400 or response.status == 0 or response.error ~= nil then
182221
if attempt <= M.attempt_count then
183222
attempt = attempt + 1
184-
request_data(data, index, attempt)
223+
request_data(data, attempt)
185224
else
186225
on_error(data, response.error)
187226
end
@@ -229,7 +268,14 @@ end
229268
---@param mount_key hash
230269
---@return atlas_mount_data|nil
231270
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)]
233279
end
234280

235281
---Load external data
@@ -240,7 +286,6 @@ function M.load(info)
240286
assert(not data.loaded, "Mount already loaded. File name: " .. tostring(data.file_name) .. "; Mount key: " .. tostring(data.mount_key))
241287
data.allow_old_mount = info.allow_old_mount == true
242288
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
244289
data.file_name = info.file_name and info.file_name or data.file_name
245290
data.processing = true
246291
if M.use_html_loader and html_loader then
@@ -254,21 +299,30 @@ function M.load(info)
254299
if is_in_mount(data, mounts) then
255300
mount_loaded(data)
256301
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)
262317
end
263-
request_data(data, data.priority)
264318
end
265319
end
266320
end
267321

268322
---Check if any old mount is using
269323
---@return boolean
270324
function M.is_old_mount_using()
271-
for i, data in pairs(files) do
325+
for i, data in pairs(atlas_mounts) do
272326
if data.old_mount_using then
273327
return true
274328
end
@@ -289,7 +343,7 @@ function M.remove_free_mounts()
289343
for key, mount in pairs(mounts) do
290344
if mount.name ~= BASE then
291345
old_mount = true
292-
for i, data in pairs(files) do
346+
for i, data in pairs(atlas_mounts) do
293347
if mount.name == data.file_name then
294348
old_mount = false
295349
break

game.project

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ input_method = HiddenInputField
1313

1414
[project]
1515
title = AtlasLoader
16-
version = 1.1.0
16+
version = 1.1.1
1717
dependencies#0 = https://github.com/andsve/dirtylarry/archive/refs/heads/master.zip
1818

1919
[library]

0 commit comments

Comments
 (0)