Skip to content

Commit

Permalink
Merge pull request #354 from RagnarokResearchLab/132-decode-fog-param…
Browse files Browse the repository at this point in the history
…eters

Add support for decoding fog parameters to the FileFormats library
  • Loading branch information
rdw-software authored Jan 31, 2024
2 parents 9d86ef4 + 3486b9e commit a53889e
Show file tree
Hide file tree
Showing 6 changed files with 220 additions and 1 deletion.
73 changes: 73 additions & 0 deletions Core/FileFormats/FogParameters.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
local Color = require("Core.NativeClient.DebugDraw.Color")

local pairs = pairs
local tonumber = tonumber
local string_gsub = string.gsub
local string_match = string.match

local FogParameters = {}

function FogParameters:DecodeFileContents(fileContents)
local lastEncounteredMap = nil
local numAddedLines = 0

local results = {}
local lines = string.explode(fileContents, "\r\n")
for index, line in ipairs(lines) do
local isMapID = self:IsMapID(line)
if isMapID then
local mapFileName = self:StripHashTag(line)
local mapID = path.basename(mapFileName, ".rsw")

lastEncounteredMap = {
mapID = mapID,
params = {},
}
end

if lastEncounteredMap ~= nil then -- In the process of building a fog table entry
numAddedLines = numAddedLines + 1
lastEncounteredMap.params[numAddedLines] = line
end

if numAddedLines == 5 then -- Got them all, save the entry for future postprocessing
results[lastEncounteredMap.mapID] = lastEncounteredMap
-- Reset
lastEncounteredMap = nil
numAddedLines = 0
end
end

local output = {}

for mapID, properties in pairs(results) do
local near = self:StripHashTag(properties.params[2])
local far = self:StripHashTag(properties.params[3])
local hexColor = self:StripHashTag(properties.params[4])
local density = self:StripHashTag(properties.params[5])
local color = Color:HexStringToRGBA(hexColor)
output[mapID] = {
near = tonumber(near),
far = tonumber(far),
color = { -- Can't serialize cdata as JSON
red = color.red,
green = color.green,
blue = color.blue,
alpha = color.alpha,
},
density = tonumber(density),
}
end

return output
end

function FogParameters:IsMapID(line)
return string_match(line, ".rsw")
end

function FogParameters:StripHashTag(line)
return string_gsub(line, "#", "")
end

return FogParameters
59 changes: 58 additions & 1 deletion Core/NativeClient/DebugDraw/Color.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
local ffi = require("ffi")
local transform = require("transform")

local tonumber = tonumber
local format = string.format
local string_sub = string.sub
local transform_bold = transform.bold

local Color = {
WHITE = { red = 1, green = 1, blue = 1 },
RED = { red = 1, green = 0, blue = 0 },
Expand All @@ -11,4 +19,53 @@ local Color = {
GREY = { red = 0.05, green = 0.05, blue = 0.05 }, -- Clear color (development)
}

return Color
ffi.cdef([[
typedef struct Color {
float red;
float green;
float blue;
float alpha;
} Color;
]])

function Color:HexStringToRGBA(hexColorString)
local color = ffi.new("Color")

local hexAlpha = string_sub(hexColorString, 3, 4)
local hexRed = string_sub(hexColorString, 5, 6)
local hexGreen = string_sub(hexColorString, 7, 8)
local hexBlue = string_sub(hexColorString, 9, 10)

local alpha = tonumber(hexAlpha, 16)
local red = tonumber(hexRed, 16)
local green = tonumber(hexGreen, 16)
local blue = tonumber(hexBlue, 16)

color.red = red / 255
color.green = green / 255
color.blue = blue / 255
color.alpha = alpha / 255

return color
end

function Color:__tostring()
local formatted = {
red = format("%.3f", self.red),
green = format("%.3f", self.green),
blue = format("%.3f", self.blue),
alpha = format("%.3f", self.alpha),
}
local firstRow = format(
"{ red = %s, green = %s, blue = %s, alpha = %s }",
formatted.red,
formatted.green,
formatted.blue,
formatted.alpha
)
return format("%s %s", transform_bold("Color"), firstRow)
end

Color.__index = Color

return ffi.metatype("Color", Color)
29 changes: 29 additions & 0 deletions Tests/FileFormats/FogParameters.spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
local FogParameters = require("Core.FileFormats.FogParameters")

describe("FogParameters", function()
describe("DecodeFileContents", function()
it("should return a table containing the fog parameters", function()
local inputFileContents = [[
// Comment line (should be discarded, just like the empty line that follows)
mapname.rsw#
0.25#
0.7#
0x3367ABEF#
0.55#
]]
local fogParametersTable = FogParameters:DecodeFileContents(inputFileContents)

assertEquals(table.count(fogParametersTable), 1)

assertEquals(fogParametersTable["mapname"].near, 0.25)
assertEquals(fogParametersTable["mapname"].far, 0.7)
assertEquals(fogParametersTable["mapname"].density, 0.55)
assertEqualNumbers(fogParametersTable["mapname"].color.red, 0.40392157435417, 1E-3)
assertEqualNumbers(fogParametersTable["mapname"].color.green, 0.67058825492859, 1E-3)
assertEqualNumbers(fogParametersTable["mapname"].color.blue, 0.93725490570068, 1E-3)
assertEqualNumbers(fogParametersTable["mapname"].color.alpha, 0.20000000298023, 1E-3)
end)
end)
end)
36 changes: 36 additions & 0 deletions Tests/NativeClient/DebugDraw/Color.spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
local Color = require("Core.NativeClient.DebugDraw.Color")

local transform = require("transform")
local bold = transform.bold

describe("Color", function()
describe("__tostring", function()
it("should return a human-readable representation of the contained RGBA values", function()
local color = Color({
red = 117 / 255,
green = 169 / 255,
blue = 255 / 255,
alpha = 34 / 255,
})
local expected = format("%s { red = 0.459, green = 0.663, blue = 1.000, alpha = 0.133 }", bold("Color"))
assertEquals(tostring(color), expected)
end)
end)

describe("HexStringToRGBA", function()
it("should return the color represented by the provided hex string", function()
local input = "0x2275A9FF"
local expectedResult = {
red = 117 / 255,
green = 169 / 255,
blue = 255 / 255,
alpha = 34 / 255,
}
local result = Color:HexStringToRGBA(input)
assertEqualNumbers(result.alpha, expectedResult.alpha, 1E-3)
assertEqualNumbers(result.red, expectedResult.red, 1E-3)
assertEqualNumbers(result.green, expectedResult.green, 1E-3)
assertEqualNumbers(result.blue, expectedResult.blue, 1E-3)
end)
end)
end)
2 changes: 2 additions & 0 deletions Tests/unit-test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ require("Core.RuntimeExtensions.C_Runtime")

local specFiles = {
"Tests/FileFormats/BinaryReader.spec.lua",
"Tests/FileFormats/FogParameters.spec.lua",
"Tests/FileFormats/RagnarokACT.spec.lua",
"Tests/FileFormats/RagnarokGAT.spec.lua",
"Tests/FileFormats/RagnarokGND.spec.lua",
Expand All @@ -26,6 +27,7 @@ local specFiles = {
"Tests/NativeClient/NativeClient.spec.lua",
"Tests/NativeClient/Renderer.spec.lua",
"Tests/NativeClient/DebugDraw/Box.spec.lua",
"Tests/NativeClient/DebugDraw/Color.spec.lua",
"Tests/NativeClient/DebugDraw/Cone.spec.lua",
"Tests/NativeClient/DebugDraw/Cylinder.spec.lua",
"Tests/NativeClient/DebugDraw/DebugScene.spec.lua",
Expand Down
22 changes: 22 additions & 0 deletions Tools/export-fog-parameters.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
local RagnarokGRF = require("Core.FileFormats.RagnarokGRF")
local FogParameters = require("Core.FileFormats.FogParameters")

local json = require("json")

local inputFilePath = "data/fogparametertable.txt"
printf("Exporting fog parameters: %s", inputFilePath)

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

local inputFileContents = grf:ExtractFileInMemory(inputFilePath)

grf:Close()

local fogParametersTable = FogParameters:DecodeFileContents(inputFileContents)

local outputFilePath = path.join("Exports", "fog-parameters.json")
local jsonFileContents = json.prettier(fogParametersTable)
printf("Saving fog parameters for %s maps: %s", table.count(fogParametersTable), outputFilePath)
C_FileSystem.WriteFile(outputFilePath, jsonFileContents)

0 comments on commit a53889e

Please sign in to comment.