Skip to content

Commit

Permalink
add merge_msg convenience function
Browse files Browse the repository at this point in the history
  • Loading branch information
bigfarts committed Apr 24, 2023
1 parent 1c9ab88 commit bdb0492
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 13 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion chaudloader/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "chaudloader"
version = "0.7.0"
version = "0.7.1"
edition = "2021"

[lib]
Expand Down
29 changes: 18 additions & 11 deletions chaudloader/src/mods/lua/lib/chaudloader/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,33 @@ local function edit_msg(mpak, address, cb)
mpak[address] = chaudloader.pack_msg(cb(chaudloader.unpack_msg(mpak[address])))
end

-- Merges all msgs from a directory.
-- Merges two messages together, preferring the latter one.
--
-- - The directory must contain files named addresses of msgs to replace, followed by `.msg`.
-- Only non-empty entries from the new msg data will be merged.
local function merge_msg(old, new)
for i, entry in ipairs(new) do
if entry ~= "" then
old[i] = entry
else
end
end
return old
end

-- Merges all msgs from a directory.
--
-- The addresses may be either mapped ROM addresses (08XXXXXX) or unmapped file offsets (00XXXXXX): if they are unmapped file offsets, they will be automatically transformed into mapped ROM addresses.
-- The directory must contain files named addresses of msgs to replace, followed by `.msg`.
--
-- - Non-empty entries from the source msg data will be merged into the target msg data.
-- The addresses may be either mapped ROM addresses (08XXXXXX) or unmapped file offsets (00XXXXXX): if they are unmapped file offsets, they will be automatically transformed into mapped ROM addresses.
local function merge_msgs_from_mod_directory(mpak, dir)
for _, filename in ipairs(chaudloader.list_mod_directory(dir)) do
local raw_addr = string.match(filename, "^(%x+).msg$")
if raw_addr == nil then
goto continue
end
local addr = tonumber(raw_addr, 16) | 0x08000000
edit_msg(mpak, addr, function (ta)
for i, entry in ipairs(chaudloader.unpack_msg(chaudloader.read_mod_file(dir .. '/' .. filename))) do
if entry ~= "" then
ta[i] = entry
end
end
return ta
edit_msg(mpak, addr, function (msg)
return merge_msg(msg, chaudloader.unpack_msg(chaudloader.read_mod_file(dir .. '/' .. filename)))
end)
::continue::
end
Expand All @@ -44,5 +50,6 @@ end
return {
edit_mpak = edit_mpak,
edit_msg = edit_msg,
merge_msg = merge_msg,
merge_msgs_from_mod_directory = merge_msgs_from_mod_directory,
}

0 comments on commit bdb0492

Please sign in to comment.