Skip to content

Commit

Permalink
GRF: Add support for optionally loading CGRF buffers
Browse files Browse the repository at this point in the history
This can drastically reduce the loading times if the TOC has been compiled before.
  • Loading branch information
rdw-software committed Feb 6, 2024
1 parent 060dd79 commit c929e4c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
18 changes: 13 additions & 5 deletions Core/FileFormats/RagnarokGRF.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
local CompiledGRF = require("Core.FileFormats.Optimized.CompiledGRF")

local cstring = require("Core.RuntimeExtensions.cstring")

local bit = require("bit")
Expand Down Expand Up @@ -58,16 +60,16 @@ function RagnarokGRF:Construct()
fileTable = {},
}

setmetatable(instance, self)
setmetatable(instance, {
__index = self,
})

return instance
end

RagnarokGRF.__index = RagnarokGRF
RagnarokGRF.__call = RagnarokGRF.Construct
setmetatable(RagnarokGRF, RagnarokGRF)
class("RagnarokGRF", RagnarokGRF)

function RagnarokGRF:Open(pathToGRF)
function RagnarokGRF:Open(pathToGRF, cgrfFileContents)
local isValidPath = C_FileSystem.Exists(pathToGRF)
if not isValidPath then
error(format("Failed to open archive %s (No such file exists)", pathToGRF), 0)
Expand All @@ -82,6 +84,7 @@ function RagnarokGRF:Open(pathToGRF)
self.fileHandle = assert(io.open(pathToGRF, "rb"))

self.pathToGRF = pathToGRF
self.cgrfFileContents = cgrfFileContents

self:DecodeArchiveMetadata()
end
Expand Down Expand Up @@ -128,6 +131,11 @@ function RagnarokGRF:DecodeHeader()
end

function RagnarokGRF:DecodeFileTable()
if rawget(self, "cgrfFileContents") then
CompiledGRF:RestoreTableOfContents(self, self.cgrfFileContents)
return
end

self:DecodeTableHeader()
self:DecodeFileEntries()
end
Expand Down
13 changes: 13 additions & 0 deletions Tests/FileFormats/RagnarokGRF.spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,19 @@ describe("RagnarokGRF", function()
assertEquals(grf.fileTable.entries["uppercase.png"].typeID, RagnarokGRF.COMPRESSED_FILE_ENTRY_TYPE)
assertEquals(grf.fileTable.entries["uppercase.png"].offsetRelativeToHeader, 160)
end)

it("should be able to restore the file table from a CGRF buffer", function()
local cgrfFilePath = path.join("Tests", "Fixtures", "test.cgrf")
local cgrfFileContents = C_FileSystem.ReadFile(cgrfFilePath)

local grf = RagnarokGRF()
grf:Open("Tests/Fixtures/test.grf", cgrfFileContents)
grf:Close()

-- These aren't stored in the CGRF (because they're useless on their own)
assertEquals(grf.fileTable.compressedSizeInBytes, 0)
assertEquals(grf.fileTable.decompressedSizeInBytes, 0)
end)
end)

describe("FindLargestFileEntry", function()
Expand Down

0 comments on commit c929e4c

Please sign in to comment.