model/blend: compressed .blend sniffing (gzip peek, zstd up to 4 MB) so F.model.load finds them
This commit is contained in:
1 parent
3c8d5e99d7
commit
08f3b1c5bd
3 files changed
+36
-3
No files matched your search
@@ -740,7 +740,7 @@ local list: { Entry } = {
|
||||
path = "model/blend",
|
||||
extensions = { "blend" },
|
||||
mime = { "application/x-blender" },
|
||||
deps = { "core/Err", "core/Bytes", "Types", "containers/Blend", "codecs/model/Mat4", "codecs/model/Rig", "codecs/model/Polygon" },
|
||||
deps = { "core/Err", "core/Bytes", "Types", "containers/Blend", "codecs/compress/Deflate", "codecs/compress/Zstd", "codecs/model/Mat4", "codecs/model/Rig", "codecs/model/Polygon" },
|
||||
caps = { decode = true, encode = false, materials = true, textures = true, animation = true, skins = true, morphTargets = true, cameras = true, lights = true, polygons = true, compressed = true },
|
||||
load = function()
|
||||
return require("@rosetta/model/blend")
|
||||
|
||||
@@ -13,6 +13,8 @@ local Err = require("@rosetta/core/Err")
|
||||
local Bytes = require("@rosetta/core/Bytes")
|
||||
local Types = require("@rosetta/Types")
|
||||
local Blend = require("@rosetta/containers/Blend")
|
||||
local Deflate = require("@rosetta/codecs/compress/Deflate")
|
||||
local Zstd = require("@rosetta/codecs/compress/Zstd")
|
||||
local Read = require("@rosetta/model/blend/Read")
|
||||
local Anim = require("@rosetta/model/blend/Anim")
|
||||
|
||||
@@ -22,7 +24,7 @@ local BlendFormat = {
|
||||
category = "model",
|
||||
extensions = { "blend" },
|
||||
mime = { "application/x-blender" },
|
||||
deps = { "core/Err", "core/Bytes", "Types", "containers/Blend", "codecs/model/Mat4", "codecs/model/Rig", "codecs/model/Polygon" },
|
||||
deps = { "core/Err", "core/Bytes", "Types", "containers/Blend", "codecs/compress/Deflate", "codecs/compress/Zstd", "codecs/model/Mat4", "codecs/model/Rig", "codecs/model/Polygon" },
|
||||
caps = { decode = true, encode = false, materials = true, textures = true, animation = true, skins = true, morphTargets = true, cameras = true, lights = true, polygons = true, compressed = true },
|
||||
}
|
||||
|
||||
@@ -41,10 +43,25 @@ export type DecodeOptions = {
|
||||
}
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ sniff
|
||||
-- compressed files: gzip by peeking the first bytes; zstd by decompressing files up to 4 MB (larger zstd streams
|
||||
-- score 0.5: picked for model loads, the archive otherwise). Ties with archive/gzip and archive/zst go to the extension.
|
||||
function BlendFormat.sniff(r: Bytes.Reader): number
|
||||
if r:peek(7) == "BLENDER" then
|
||||
local head = r:peek(7)
|
||||
if head == "BLENDER" then
|
||||
return 1
|
||||
end
|
||||
local n = r.limit - r.base
|
||||
if string.sub(head, 1, 3) == "\31\139\8" then
|
||||
local inner = Deflate.peek(r.buf, r.base, n, 7)
|
||||
return if inner and buffer.tostring(inner) == "BLENDER" then 1 else 0
|
||||
end
|
||||
if string.sub(head, 1, 4) == Zstd.MAGIC then
|
||||
if n > 4 * 2 ^ 20 then
|
||||
return 0.5
|
||||
end
|
||||
local out = Zstd.decompress(r.buf, r.base, n, { verify = false })
|
||||
return if out and buffer.len(out) >= 7 and buffer.readstring(out, 0, 7) == "BLENDER" then 1 else 0
|
||||
end
|
||||
return 0
|
||||
end
|
||||
|
||||
|
||||
@@ -620,6 +620,22 @@ if cur then
|
||||
t:check(s and s.kind == "scene", "F.load")
|
||||
end
|
||||
|
||||
-- compressed files: by content for model loads, by extension otherwise
|
||||
for _, c in Sets.cur do
|
||||
if string.find(c.name, "_z%.blend$") then
|
||||
local b = buffer.fromstring(c.data)
|
||||
local s = F.model.load(b)
|
||||
t:check(s and s.meta.source.compression == "zstd", "F.model.load of a zstd .blend")
|
||||
t:eq(F.detect(b, { name = "x.blend" }), "blend", "F.detect of a zstd .blend by name")
|
||||
end
|
||||
end
|
||||
for _, c in Sets["293"] do
|
||||
if string.find(c.name, "_z%.blend$") then
|
||||
local s = F.model.load(buffer.fromstring(c.data))
|
||||
t:check(s and s.meta.source.compression == "gzip", "F.model.load of a gzip .blend")
|
||||
end
|
||||
end
|
||||
|
||||
-- errors
|
||||
local function code(data: string): string?
|
||||
local s, e = decode(data)
|
||||
|
||||
Reference in new issue
Block a user