Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add SPR support to the data mining toolkit #110

Merged
merged 3 commits into from
Aug 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions Tests/Tools/FileAnalyzer.spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ local gndFiles = {
path.join("Tests", "Fixtures", "multiple-water-planes.gnd"),
}

local sprFiles = {
path.join("Tests", "Fixtures", "bmp-paletted.spr"),
path.join("Tests", "Fixtures", "bmp-tga-paletted.spr"),
path.join("Tests", "Fixtures", "rle-bmp-tga-paletted.spr"),
}

describe("FileAnalyzer", function()
describe("AnalyzeGND", function()
it("should return a summary of the metadata found in the given GND files", function()
Expand All @@ -22,4 +28,20 @@ describe("FileAnalyzer", function()
assertEquals(analysisResult.fields.gridSizeV[2], 3)
end)
end)

describe("AnalyzeSPR", function()
it("should return a summary of the metadata found in the given SPR files", function()
local analysisResult = FileAnalyzer:AnalyzeSPR(sprFiles)

assertEquals(analysisResult.numFilesAnalyzed, 3)

assertEquals(analysisResult.fields.version[1.1], 1)
assertEquals(analysisResult.fields.version[2.0], 1)
assertEquals(analysisResult.fields.version[2.1], 1)

assertEquals(analysisResult.fields.bmpImagesCount[2], 3)
assertEquals(analysisResult.fields.tgaImagesCount[0], 1)
assertEquals(analysisResult.fields.tgaImagesCount[1], 2)
end)
end)
end)
36 changes: 36 additions & 0 deletions Tools/FileAnalyzer.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
local RagnarokGND = require("Core.FileFormats.RagnarokGND")
local RagnarokSPR = require("Core.FileFormats.RagnarokSPR")

local FileAnalyzer = {}

Expand Down Expand Up @@ -40,4 +41,39 @@ function FileAnalyzer:AnalyzeGND(gndFiles)
return analysisResult
end

function FileAnalyzer:AnalyzeSPR(sprFiles)
local analysisResult = {
numFilesAnalyzed = 0,
fields = {
version = {},
bmpImagesCount = {},
tgaImagesCount = {},
},
}

local spr = RagnarokSPR()
for index, filePath in ipairs(sprFiles) do
printf("Analyzing file: %s", filePath)

local sprFileContents = C_FileSystem.ReadFile(filePath)
spr:DecodeFileContents(sprFileContents)

analysisResult.fields.version[spr.version] = analysisResult.fields.version[spr.version] or 0
analysisResult.fields.version[spr.version] = analysisResult.fields.version[spr.version] + 1

analysisResult.fields.bmpImagesCount[spr.bmpImagesCount] = analysisResult.fields.bmpImagesCount[spr.bmpImagesCount]
or 0
analysisResult.fields.tgaImagesCount[spr.tgaImagesCount] = analysisResult.fields.tgaImagesCount[spr.tgaImagesCount]
or 0
analysisResult.fields.bmpImagesCount[spr.bmpImagesCount] = analysisResult.fields.bmpImagesCount[spr.bmpImagesCount]
+ 1
analysisResult.fields.tgaImagesCount[spr.tgaImagesCount] = analysisResult.fields.tgaImagesCount[spr.tgaImagesCount]
+ 1

analysisResult.numFilesAnalyzed = analysisResult.numFilesAnalyzed + 1
end

return analysisResult
end

return FileAnalyzer
38 changes: 38 additions & 0 deletions Tools/analyze-spr-files.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
local RagnarokGRF = require("Core.FileFormats.RagnarokGRF")
local FileAnalyzer = require("Tools.FileAnalyzer")

local console = require("console")

local TEMP_DIR = "data.extracted.grf"
printf("Creating temporary directory: %s", TEMP_DIR)
C_FileSystem.MakeDirectory(TEMP_DIR)

local grf = RagnarokGRF()
grf:Open("data.grf")

console.startTimer("Extracting SPR files")

local fileList = grf:GetFileList()
local gndFiles = {}
for index, fileEntry in ipairs(fileList) do
local filePath = grf:GetNormalizedFilePath(fileEntry.name)
local isSPR = (path.extname(filePath) == ".spr")

if isSPR then
local tempFilePath = path.join(TEMP_DIR, fileEntry.name)
table.insert(gndFiles, tempFilePath)
if not C_FileSystem.Exists(tempFilePath) then
local tempDirectory = path.dirname(tempFilePath)
C_FileSystem.MakeDirectoryTree(tempDirectory)
printf("Extracting %s to %s", filePath, tempFilePath)
grf:ExtractFileToDisk(fileEntry.name, tempFilePath)
end
end
end

grf:Close()

local analysisResult = FileAnalyzer:AnalyzeSPR(gndFiles)
dump(analysisResult)

console.stopTimer("Extracting SPR files")
Loading