-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #354 from RagnarokResearchLab/132-decode-fog-param…
…eters Add support for decoding fog parameters to the FileFormats library
- Loading branch information
Showing
6 changed files
with
220 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |