-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Expand file tree
/
Copy pathDataJewelFileLoader.lua
More file actions
95 lines (87 loc) · 2.92 KB
/
Copy pathDataJewelFileLoader.lua
File metadata and controls
95 lines (87 loc) · 2.92 KB
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
-- Path of Building
--
-- Module: Jewel Data File Loader
-- Loads compressed jewel data from one file or a numbered set of parts.
--
local t_concat = table.concat
local function loadJewelFile(jewelTypeName, cacheUncompressed)
local jewelPath = "/Data/TimelessJewelData/" .. jewelTypeName
local scriptPath = GetScriptPath()
if scriptPath == "" then
-- The desktop app supplies its script folder. Headless tests may start in
-- either the repository root or the src folder, so check both locations.
local relativePath = "." .. jewelPath
local file = io.open(relativePath .. ".zip", "rb") or io.open(relativePath .. ".zip.part0", "rb") or io.open(relativePath .. ".bin", "rb")
if file then
file:close()
scriptPath = "."
else
scriptPath = "./src"
end
end
cacheUncompressed = cacheUncompressed ~= false
local uncompressedFileAttr = { }
if cacheUncompressed then
local fileHandle = NewFileSearch(scriptPath .. jewelPath .. ".bin")
if fileHandle then
uncompressedFileAttr.fileName = fileHandle:GetFileName()
uncompressedFileAttr.modified = fileHandle:GetFileModifiedTime()
end
end
local compressedFileAttr = { }
local fileHandle = NewFileSearch(scriptPath .. jewelPath .. ".zip")
if fileHandle then
compressedFileAttr.modified = fileHandle:GetFileModifiedTime()
end
fileHandle = NewFileSearch(scriptPath .. jewelPath .. ".zip.part*")
if fileHandle then
compressedFileAttr.modified = fileHandle:GetFileModifiedTime()
end
if uncompressedFileAttr.modified and uncompressedFileAttr.modified > (compressedFileAttr.modified or 0) then
ConPrintf("Uncompressed jewel data is up-to-date, loading " .. uncompressedFileAttr.fileName)
local uncompressedFile = io.open(scriptPath .. jewelPath .. ".bin", "rb")
if uncompressedFile then
local jewelData = uncompressedFile:read("*a")
uncompressedFile:close()
if jewelData then
return jewelData
end
end
end
if cacheUncompressed then
ConPrintf("Failed to load " .. scriptPath .. jewelPath .. ".bin, or data is out of date, falling back to compressed file")
end
local compressedFile = io.open(scriptPath .. jewelPath .. ".zip", "rb")
local compressedData
if compressedFile then
compressedData = compressedFile:read("*a")
compressedFile:close()
else
local splitFile = { }
local part = 0
while true do
local file = io.open(scriptPath .. jewelPath .. ".zip.part" .. part, "rb")
if not file then
break
end
splitFile[part + 1] = file:read("*a")
file:close()
part = part + 1
end
compressedData = t_concat(splitFile, "")
end
if not compressedData or compressedData == "" then
ConPrintf("Failed to load jewel data: " .. jewelTypeName)
return
end
local jewelData = Inflate(compressedData)
if cacheUncompressed then
local uncompressedFile = io.open(scriptPath .. jewelPath .. ".bin", "wb+")
if uncompressedFile then
uncompressedFile:write(jewelData)
uncompressedFile:close()
end
end
return jewelData
end
return loadJewelFile