local solvitdb = {} local fs = require("filesystem") local function checkValidityAndOpen(path) local handle = assert(fs.open(path)) local data = assert(handle:read(8)) if data:sub(5, 8) == "RTFM" then local patLength = string.unpack(" location - i then readAmount = location - i end if readAmount == 0 then break end local data = readHandle:read(readAmount) i = i + readAmount assert(writeHandle:write(data)) end assert(writeHandle:write(bytes)) while true do local data = readHandle:read(chunkLength) if not data then break end assert(writeHandle:write(data)) end readHandle:close() writeHandle:close() fs.rename(tmpFilePath, filePath) end local function remove(filePath, location, length) local chunkLength = 512 if length > 512 then chunkLength = length end -- The file has to get shortened, so I have no choice but to do these shenanigans local readHandle = assert(fs.open(filePath, "r")) local tmpFilePath = filePath .. ".tmp" local writeHandle = assert(fs.open(tmpFilePath, "w")) local i = 0 while true do local readAmount = chunkLength if readAmount > location - i then readAmount = location - i end if readAmount == 0 then break end local data = readHandle:read(readAmount) i = i + readAmount assert(writeHandle:write(data)) end readHandle:seek(length) while true do local data = readHandle:read(chunkLength) if not data then break end assert(writeHandle:write(data)) end readHandle:close() writeHandle:close() fs.rename(tmpFilePath, filePath) end local function adjustPatLocationsAfterPackage(filePath, packageName, offset) local readHandle, patLength = checkValidityAndOpen(filePath) local pat = readPat(readHandle, patLength) readHandle:close() local modifiedLocation = pat[packageName] local toAdjust = {} for name, location in pairs(pat) do if location > modifiedLocation then table.insert(toAdjust, { name = name, location = location }) end end if #toAdjust == 0 then return end local readHandle = assert(fs.open(filePath, "r")) readHandle:seek(8) -- Skip header local patBytes = assert(readHandle:read(patLength)) readHandle:close() local patFieldPositions = {} for _, entry in ipairs(toAdjust) do local needle = entry.name .. "." local startPos = patBytes:find(needle, 1, true) patFieldPositions[entry.name] = 8 + startPos + #needle - 1 end local writeHandle = assert(fs.open(filePath, "a")) for _, entry in ipairs(toAdjust) do local newLocation = entry.location + offset writeHandle:seek("set", patFieldPositions[entry.name]) writeHandle:write(string.pack(" 0 then writeHandle:write(encodedString:sub(1, #encodedString - difference)) local currentSeek = writeHandle:seek() writeHandle:close() insert(path, currentSeek + 1, encodedString:sub(#data - difference, -1)) adjustPatLocationsAfterPackage(path, name, difference) end else readHandle:close() writeHandle:seek("end") local newPackageLocation = writeHandle:seek() - patLength - 8 writeHandle:write(encodedString .. "\n") writeHandle:close() local patData = ("%s.%s;"):format(name, string.pack("