view support/casc/blte.lua @ 65:8b8b0bade520

Fixed support for mounts using the new MountJournal and mount IDs (no conversion of old profiles at the moment).
author Jerome Vuarand <jerome.vuarand@gmail.com>
date Thu, 23 Oct 2014 13:44:59 +0100
parents
children
line wrap: on
line source
local M, plat, bin = {}, require("casc.platform"), require("casc.bin")
local open, tconcat, assert, error = io.open, table.concat, assert, error
local uint32_le, uint32_be = bin.uint32_le, bin.uint32_be

local string_cursor do
	local function read(self, n)
		local p = self.pos
		self.pos = p + n
		return self.str:sub(p, p+n-1)
	end
	function string_cursor(s)
		return {str=s, read=read, pos=1}
	end
end

local function decodeChunk(chunk)
	local format = chunk:sub(1,1)
	if format == 'N' then
		return chunk:sub(2)
	elseif format == 'Z' then
		return plat.decompress(chunk:sub(2))
	else
		error('Unknown chunk format: ' .. tostring(format))
	end
end
local function parseBLTE(h, dataSize)
	local header = h:read(8)
	assert(header:sub(1,4) == 'BLTE', 'BLTE file magic signature')
	local ofs = uint32_be(header, 4)

	local chunks, ret = ofs > 0 and {}
	if ofs > 0 then
		local n = uint32_be(h:read(4)) % 2^16
		local buf, p = h:read(n*24), 0
		for i=1, n do
			chunks[i], p = uint32_be(buf, p), p + 24
		end
		for i=1, #chunks do
			chunks[i] = decodeChunk(h:read(chunks[i]))
		end
		ret = tconcat(chunks, "")
	else
		ret = decodeChunk(h:read(dataSize))
	end
	return ret
end

function M.readArchive(path, offset)
	assert(type(path) == "string" and type(offset) == "number", 'Syntax: "content" = casc.blte.readArchive("path", offset)')

	local h = open(path, "rb")
	h:seek("set", offset)
	local blockHead = h:read(30)
	local ret = parseBLTE(h, uint32_le(blockHead, 16)-30)
	h:close()
	
	return ret
end
function M.readData(str)
	assert(type(str) == "string", 'Syntax: "content" = casc.blte.readContent("str")')
	return parseBLTE(string_cursor(str), #str)
end

return M