--!strict -- model/md5: Doom 3 / Quake 4 MD5 (MD5Version 10 text): .md5mesh and .md5anim (model/md5/Read); a mesh loads -- opts.anims, joints matched by name. Not supported: writing, .md5camera. -- ------------------------------------------------------------------------------------------------ requires local Err = require("@rosetta/core/Err") local Bytes = require("@rosetta/core/Bytes") local Input = require("@rosetta/core/Input") local Sniff = require("@rosetta/core/Sniff") local Types = require("@rosetta/Types") local Read = require("@rosetta/model/md5/Read") -- ------------------------------------------------------------------------------------------------ contract local Md5 = { name = "md5", category = "model", extensions = { "md5mesh", "md5anim" }, mime = { "model/x-md5mesh", "model/x-md5anim" }, deps = { "core/Err", "core/Bytes", "core/Input", "core/Sniff", "Types", "codecs/model/Rig" }, caps = { decode = true, encode = false, skins = true, animation = true }, } -- ------------------------------------------------------------------------------------------------ types export type DecodeOptions = { anims: { string }?, resolve: ((uri: string) -> (buffer | string)?)?, files: { [string]: buffer | string }?, skipMissing: boolean?, -- default false: UNSUPPORTED name: string?, maxVertices: number?, -- default 2^24 } -- ------------------------------------------------------------------------------------------------ sniff function Md5.sniff(r: Bytes.Reader): number local head = Sniff.text(r, 512, true) head = string.gsub(head, "^//[^\n]*\n%s*", "") return if string.match(head, "^MD5Version%s+%d+") then 1 else 0 end -- ------------------------------------------------------------------------------------------------ decode local function stem(name: string): string local base = string.match(name, "([^/\\]+)$") or name return (string.gsub(base, "%.[^.]*$", "")) end function Md5.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Scene?, Err.Err?) local text = Sniff.dropBom(r:string(r:remaining())) if string.find(text, "numAnimatedComponents", 1, true) then local a, msg = Read.anim(text) if not a then return nil, Err.from(msg) end return Read.animScene(a, if opts and type(opts.name) == "string" then stem(opts.name) else "animation"), nil end local scene, byName = Read.mesh(text, opts :: any) if not scene then return nil, Err.from(byName) end if opts and type(opts.anims) == "table" then for _, name in opts.anims do local got = if type(name) == "string" then Input.sibling(opts, name) else nil local src = if got then buffer.tostring(got) else nil if not src then if not opts.skipMissing then return nil, Err.new(Err.UNSUPPORTED, "animation " .. tostring(name) .. " not available (opts.resolve / opts.files)") end else local a, msg = Read.anim(Sniff.dropBom(src)) if not a then return nil, Err.from("animation " .. name .. ": " .. tostring(msg)) end Read.addAnim(scene, byName, a, stem(name)) end end end return scene, nil end return Md5