Skip to content

Commit

Permalink
W3C: Update and improve
Browse files Browse the repository at this point in the history
Issue: #99

We now automatically determine the correct file format in both `unpack`
and `pack`.  This is done by seeing if the entire input is consumed in
`unpack`, and checking for the presence of the new camera values in
`pack`.

Other changes include support of `position` in `unpack`.
  • Loading branch information
nvs committed Jul 7, 2022
1 parent c342a69 commit 99028ec
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 46 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
- W3S (`w3s`):
- Now supports format `3`.
- Output format has changed.
- W3C (`w3c`):
- File format for 1.31 is now automatically detected.

## [0.10.1] - 2020-01-05
### Fixed
Expand Down
132 changes: 86 additions & 46 deletions file/war3map/w3c.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,62 +8,102 @@ local W3C = {}
local unpack = string.unpack
local pack = string.pack

function W3C.unpack (input, version)
assert (type (input) == 'string')
assert (type (version) == 'table')

local format, count, position = unpack ('< i4 i4', input)
assert (format == 0)

local output = {
format = format
}

for index = 1, count do
local camera = {
target = {}
}

camera.target.x,
camera.target.y,
camera.z_offset,
camera.rotation,
camera.angle_of_attack,
camera.distance,
camera.roll,
camera.field_of_view,
camera.far_z,
camera.near_z,
position = unpack (
'< f f f f f f f f f f', input, position)

if version.minor >= 31 then
local is_format = {
[0] = true
}

local unpackers = {
-- Format for patch `< 1.31`.
function (input, position)
local count
local output = {}

output.format, count,
position = unpack ('< i4 i4', input, position)

assert (is_format [output.format])

for index = 1, count do
local camera = {
target = {}
}

camera.target.x,
camera.target.y,
camera.z_offset,
camera.rotation,
camera.angle_of_attack,
camera.distance,
camera.roll,
camera.field_of_view,
camera.far_z,
camera.near_z,
camera.name,
position = unpack (
'< f f f f f f f f f f z', input, position)

output [index] = camera
end

return output, position
end,

-- Format for patch '>= 1.31`.
function (input, position)
local count
local output = {}

output.format, count,
position = unpack ('< i4 i4', input, position)

assert (is_format [output.format])

for index = 1, count do
local camera = {
target = {}
}

camera.target.x,
camera.target.y,
camera.z_offset,
camera.rotation,
camera.angle_of_attack,
camera.distance,
camera.roll,
camera.field_of_view,
camera.far_z,
camera.near_z,
camera.local_pitch,
camera.local_yaw,
camera.local_roll,
position = unpack ('< f f f', input, position)
end
camera.name,
position = unpack (
'< f f f f f f f f f f f f f z', input, position)

camera.name,
position = unpack ('z', input, position)
output [index] = camera
end

output [index] = camera
return output, position
end
}

assert (#input == position - 1)
function W3C.unpack (input, position)
-- Attempt to use the older format first. If the `input` is not fully
-- consumed, then we assume we need to use the latest format.
for _, unpacker in ipairs (unpackers) do
local output, _position = unpacker (input, position)

return output
if _position > #input then
return output, _position
end
end
end

function W3C.pack (input, version)
assert (type (input) == 'table')
assert (type (version) == 'table')
function W3C.pack (input)
assert (is_format [input.format])

local output = {}
local format = input.format or 0
assert (format == 0)

output [#output + 1] = pack ('< i4 i4', format, #input)
output [#output + 1] = pack ('< i4 i4', input.format, #input)

for _, camera in ipairs (input) do
output [#output + 1] = pack (
Expand All @@ -79,15 +119,15 @@ function W3C.pack (input, version)
camera.far_z,
camera.near_z)

if version.minor >= 31 then
if camera.local_pitch then
output [#output + 1] = pack (
'< f f f',
camera.local_pitch,
camera.local_yaw,
camera.local_roll)
end

output [#output + 1] = pack ('z', camera.name)
output [#output + 1] = pack ('< z', camera.name)
end

return table.concat (output)
Expand Down

0 comments on commit 99028ec

Please sign in to comment.