Last active
September 22, 2015 03:53
-
-
Save hhrhhr/36ac2aa4243f5bad49ca to your computer and use it in GitHub Desktop.
Act of Agression: lang export. usage: "lua dic_export.lua path_to_file"
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
assert("Lua 5.3" == _VERSION, "\n\nERROR: Lua 5.3 needed\n") | |
local input = assert(arg[1]) | |
local output = arg[2] or input .. ".txt" | |
local r | |
local function read_uint32() | |
return string.unpack("I", r:read(4)) | |
end | |
local function toutf16le(str) | |
return string.gsub(str, "(.)", "%1\x00") | |
end | |
r = assert(io.open(input, "rb")) | |
local magic = r:read(4) | |
assert("TRAD" == magic, "\n\nERROR: magic not match\n") | |
local count = read_uint32() | |
local header = {} | |
for i = 1, count do | |
local t = {} | |
t.id = read_uint32() | |
t.zero = read_uint32() | |
t.offset = read_uint32() | |
t.length = read_uint32() * 2 | |
table.insert(header, t) | |
-- print(i, t.id, t.zero, t.offset, t.length) | |
if t.id == 0 and t.zero == 2147483648 and i ~= count then | |
-- the last element is not as expected | |
print("!!!") | |
end | |
end | |
local w = assert(io.open(output, "w+b")) | |
w:write("\xFF\xFE") -- UTF-16LE | |
for i = 1, count do | |
local t = header[i] | |
r:seek("set", t.offset) | |
local str = r:read(t.length) or "" | |
-- convert | |
w:write(toutf16le(string.format("[[%d]] = [[", t.id))) | |
-- write as is | |
w:write(str) | |
-- convert again | |
w:write(toutf16le("]]\n")) | |
end | |
w:close() | |
r:close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment