merge ws/perf-core
This commit is contained in:
commit
de755ca036
37 files changed
+5317
-2562
No files matched your search
@@ -151,11 +151,12 @@ function Mtl.toMaterial(e: Entry, texture: (Map) -> number): Types.Material
|
||||
return m
|
||||
end
|
||||
|
||||
local NUM_FORMATS = { "%.6g", "%.9g", "%.17g" }
|
||||
local function num(v: number): string
|
||||
if v == math.floor(v) and math.abs(v) < 1e15 then
|
||||
return string.format("%d", v)
|
||||
end
|
||||
for _, f in { "%.6g", "%.9g", "%.17g" } do
|
||||
for _, f in NUM_FORMATS do
|
||||
local s = string.format(f, v)
|
||||
if tonumber(s) == v then
|
||||
return s
|
||||
|
||||
+122
-46
@@ -89,6 +89,11 @@ type Prim = {
|
||||
}
|
||||
type Section = { object: string?, group: string?, prims: { Prim }, byKey: { [string]: Prim }, smoothing: string? }
|
||||
|
||||
local TOK = "[ \t]+([^%s#\\]+)"
|
||||
local V2 = "^" .. TOK .. TOK .. "[ \t]*\r?\n()"
|
||||
local V3 = "^" .. TOK .. TOK .. TOK .. "[ \t]*\r?\n()"
|
||||
local V4 = "^" .. TOK .. TOK .. TOK .. TOK .. "[ \t]*\r?\n()"
|
||||
|
||||
function Obj.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Scene?, Err.Err?)
|
||||
local triangulate = not (opts and opts.triangulate == false)
|
||||
local flipV = not (opts and opts.flipV == false)
|
||||
@@ -164,7 +169,117 @@ function Obj.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Scene?, Err.E
|
||||
return vi, ti, ni
|
||||
end
|
||||
local fs: { string } = {}
|
||||
local SV, ST, SN = {}, {}, {}
|
||||
local function element(key: string, k: number): Err.Err?
|
||||
local mode = if key == "f" then (if triangulate then "triangles" else "polygons") elseif key == "l" then "lines" else "points"
|
||||
if (key == "f" and k < 3) or (key == "l" and k < 2) or k < 1 then
|
||||
return Err.new(Err.CORRUPT, "an element with too few corners on line " .. lineNo)
|
||||
end
|
||||
corners += k
|
||||
if corners > maxV * 4 then
|
||||
return Err.new(Err.LIMIT, "too many face corners")
|
||||
end
|
||||
local p = prim(mode)
|
||||
for i = 1, k do
|
||||
local tok = fs[i]
|
||||
local vi, ti, ni = corner(tok)
|
||||
if not vi then
|
||||
return Err.new(Err.CORRUPT, "bad index '" .. tok .. "' on line " .. lineNo)
|
||||
end
|
||||
SV[i], ST[i], SN[i] = vi, ti, ni
|
||||
if ti >= 0 then
|
||||
p.hasT = true
|
||||
end
|
||||
if ni >= 0 then
|
||||
p.hasN = true
|
||||
end
|
||||
end
|
||||
local cv, ct, cn = p.cv, p.ct, p.cn
|
||||
local o = #cv
|
||||
if mode == "triangles" then
|
||||
for a = 2, k - 1 do
|
||||
cv[o + 1], ct[o + 1], cn[o + 1] = SV[1], ST[1], SN[1]
|
||||
cv[o + 2], ct[o + 2], cn[o + 2] = SV[a], ST[a], SN[a]
|
||||
cv[o + 3], ct[o + 3], cn[o + 3] = SV[a + 1], ST[a + 1], SN[a + 1]
|
||||
o += 3
|
||||
end
|
||||
elseif mode == "lines" then
|
||||
for a = 1, k - 1 do
|
||||
cv[o + 1], ct[o + 1], cn[o + 1] = SV[a], ST[a], SN[a]
|
||||
cv[o + 2], ct[o + 2], cn[o + 2] = SV[a + 1], ST[a + 1], SN[a + 1]
|
||||
o += 2
|
||||
end
|
||||
else
|
||||
for i = 1, k do
|
||||
cv[o + i], ct[o + i], cn[o + i] = SV[i], ST[i], SN[i]
|
||||
end
|
||||
if mode == "polygons" then
|
||||
table.insert(p.faces, k)
|
||||
end
|
||||
end
|
||||
return nil
|
||||
end
|
||||
while pos <= len do
|
||||
-- plain "v x y z", "vt u v", "vn x y z" and three- / four-corner "f" lines; anything else takes the general path
|
||||
local c0 = string.byte(text, pos)
|
||||
if c0 == 118 then -- v
|
||||
local c1 = string.byte(text, pos + 1)
|
||||
if c1 == 32 or c1 == 9 then
|
||||
local x, y, z, ne = string.match(text, V3, pos + 1)
|
||||
if x then
|
||||
local fx, fy, fz = tonumber(x), tonumber(y), tonumber(z)
|
||||
if fx and fy and fz and np < maxV then
|
||||
P[np * 3 + 1], P[np * 3 + 2], P[np * 3 + 3] = fx, fy, fz
|
||||
np += 1
|
||||
lineNo += 1
|
||||
pos = ne
|
||||
continue
|
||||
end
|
||||
end
|
||||
elseif c1 == 110 then -- vn
|
||||
local x, y, z, ne = string.match(text, V3, pos + 2)
|
||||
if x then
|
||||
local fx, fy, fz = tonumber(x), tonumber(y), tonumber(z)
|
||||
if fx and fy and fz and nn < maxV then
|
||||
N[nn * 3 + 1], N[nn * 3 + 2], N[nn * 3 + 3] = fx, fy, fz
|
||||
nn += 1
|
||||
lineNo += 1
|
||||
pos = ne
|
||||
continue
|
||||
end
|
||||
end
|
||||
elseif c1 == 116 then -- vt
|
||||
local u, v, ne = string.match(text, V2, pos + 2)
|
||||
if u then
|
||||
local fu, fv = tonumber(u), tonumber(v)
|
||||
if fu and fv and nt < maxV then
|
||||
UV[nt * 2 + 1], UV[nt * 2 + 2] = fu, if flipV then 1 - fv else fv
|
||||
nt += 1
|
||||
lineNo += 1
|
||||
pos = ne
|
||||
continue
|
||||
end
|
||||
end
|
||||
end
|
||||
elseif c0 == 102 then -- f
|
||||
local a, b, c, ne = string.match(text, V3, pos + 1)
|
||||
local k = 3
|
||||
local d = nil
|
||||
if not a then
|
||||
a, b, c, d, ne = string.match(text, V4, pos + 1)
|
||||
k = 4
|
||||
end
|
||||
if a then
|
||||
fs[1], fs[2], fs[3], fs[4] = a, b, c, d
|
||||
lineNo += 1
|
||||
local err = element("f", k)
|
||||
if err then
|
||||
return nil, err
|
||||
end
|
||||
pos = ne
|
||||
continue
|
||||
end
|
||||
end
|
||||
local e = string.find(text, "\n", pos, true) or (len + 1)
|
||||
local line = string.sub(text, pos, e - 1)
|
||||
pos = e + 1
|
||||
@@ -229,50 +344,9 @@ function Obj.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Scene?, Err.E
|
||||
for tok in string.gmatch(rest, "%S+") do
|
||||
table.insert(fs, tok)
|
||||
end
|
||||
local k = #fs
|
||||
local mode = if key == "f" then (if triangulate then "triangles" else "polygons") elseif key == "l" then "lines" else "points"
|
||||
if (key == "f" and k < 3) or (key == "l" and k < 2) or k < 1 then
|
||||
return nil, Err.new(Err.CORRUPT, "an element with too few corners on line " .. lineNo)
|
||||
end
|
||||
corners += k
|
||||
if corners > maxV * 4 then
|
||||
return nil, Err.new(Err.LIMIT, "too many face corners")
|
||||
end
|
||||
local p = prim(mode)
|
||||
local base = #p.cv
|
||||
for i, tok in fs do
|
||||
local vi, ti, ni = corner(tok)
|
||||
if not vi then
|
||||
return nil, Err.new(Err.CORRUPT, "bad index '" .. tok .. "' on line " .. lineNo)
|
||||
end
|
||||
p.cv[base + i], p.ct[base + i], p.cn[base + i] = vi, ti, ni
|
||||
if ti >= 0 then
|
||||
p.hasT = true
|
||||
end
|
||||
if ni >= 0 then
|
||||
p.hasN = true
|
||||
end
|
||||
end
|
||||
if mode == "triangles" and k > 3 then
|
||||
local cv, ct, cn = table.move(p.cv, base + 1, base + k, 1, {}), table.move(p.ct, base + 1, base + k, 1, {}), table.move(p.cn, base + 1, base + k, 1, {})
|
||||
local o = base
|
||||
for a = 2, k - 1 do
|
||||
for _, j in { 1, a, a + 1 } do
|
||||
o += 1
|
||||
p.cv[o], p.ct[o], p.cn[o] = cv[j], ct[j], cn[j]
|
||||
end
|
||||
end
|
||||
elseif mode == "lines" then
|
||||
local cv, ct, cn = table.move(p.cv, base + 1, base + k, 1, {}), table.move(p.ct, base + 1, base + k, 1, {}), table.move(p.cn, base + 1, base + k, 1, {})
|
||||
local o = base
|
||||
for a = 1, k - 1 do
|
||||
for _, j in { a, a + 1 } do
|
||||
o += 1
|
||||
p.cv[o], p.ct[o], p.cn[o] = cv[j], ct[j], cn[j]
|
||||
end
|
||||
end
|
||||
elseif mode == "polygons" then
|
||||
table.insert(p.faces, k)
|
||||
local err = element(key, #fs)
|
||||
if err then
|
||||
return nil, err
|
||||
end
|
||||
elseif key == "o" then
|
||||
object = rest
|
||||
@@ -471,6 +545,7 @@ end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ encode
|
||||
local probe = buffer.create(4)
|
||||
local F32_FORMATS = { "%.6g", "%.7g", "%.8g" }
|
||||
local function f32str(v: number): string
|
||||
if v ~= v or v == math.huge or v == -math.huge then
|
||||
return "0"
|
||||
@@ -480,7 +555,7 @@ local function f32str(v: number): string
|
||||
end
|
||||
buffer.writef32(probe, 0, v)
|
||||
local want = buffer.readf32(probe, 0)
|
||||
for _, f in { "%.6g", "%.7g", "%.8g" } do
|
||||
for _, f in F32_FORMATS do
|
||||
local s = string.format(f, v)
|
||||
buffer.writef32(probe, 0, tonumber(s) :: number)
|
||||
if buffer.readf32(probe, 0) == want then
|
||||
@@ -658,7 +733,8 @@ function Obj.encode(scene: Types.Scene, opts: EncodeOptions?): (buffer?, Err.Err
|
||||
local mode = prim.mode or "triangles"
|
||||
if mode == "triangles" then
|
||||
for t = 0, count // 3 - 1 do
|
||||
face({ at(t * 3), at(t * 3 + 1), at(t * 3 + 2) })
|
||||
local a, b, c = ref(at(t * 3)), ref(at(t * 3 + 1)), ref(at(t * 3 + 2))
|
||||
table.insert(out, if mirror then "f " .. c .. " " .. b .. " " .. a else "f " .. a .. " " .. b .. " " .. c)
|
||||
end
|
||||
elseif mode == "polygons" then
|
||||
local F = if prim.faces then Types.Scene.reader(prim.faces) else nil
|
||||
|
||||
@@ -244,12 +244,13 @@ function Rbxm.decode(r: Bytes.Reader, opts: DecodeOptions?): (Rbx.Tree?, Err.Err
|
||||
local vals, msg = Binary.decode(t, body, q + 1, be, n, ctx)
|
||||
if vals then
|
||||
local tname = Rbx.TYPE_NAMES[t]
|
||||
for i, ref in cls.refs do
|
||||
local inst = byRef[ref]
|
||||
if pname == "Name" and t == 0x01 then
|
||||
inst.name = vals[i]
|
||||
else
|
||||
inst.properties[pname] = { type = tname, value = vals[i] }
|
||||
if pname == "Name" and t == 0x01 then
|
||||
for i, ref in cls.refs do
|
||||
byRef[ref].name = vals[i]
|
||||
end
|
||||
else
|
||||
for i, ref in cls.refs do
|
||||
byRef[ref].properties[pname] = { type = tname, value = vals[i] }
|
||||
end
|
||||
end
|
||||
elseif msg == "unknown" then
|
||||
@@ -322,7 +323,7 @@ local function columnType(insts: { Rbx.Instance }, pname: string): (number?, str
|
||||
local id, tname = nil, nil
|
||||
for _, inst in insts do
|
||||
local v = inst.properties[pname]
|
||||
if v then
|
||||
if v and (type(v) ~= "table" or tname == nil or v.type ~= tname) then
|
||||
if type(v) ~= "table" or type(v.type) ~= "string" then
|
||||
return nil, "property " .. pname .. " of " .. inst.className .. " is not a { type, value } table"
|
||||
end
|
||||
|
||||
@@ -81,6 +81,10 @@ local function num(s: string?): number
|
||||
if s == nil then
|
||||
return 0
|
||||
end
|
||||
local v = tonumber(s)
|
||||
if v then
|
||||
return v
|
||||
end
|
||||
local t = string.lower((string.gsub(s, "^%s+", "")))
|
||||
t = string.gsub(t, "%s+$", "")
|
||||
if t == "inf" or t == "+inf" or t == "infinity" then
|
||||
@@ -100,8 +104,27 @@ end
|
||||
local function comps(node: Xml.Node?, names: { string }): { number }
|
||||
local out = table.create(#names, 0)
|
||||
if node then
|
||||
-- the children in order, element i usually named names[i]
|
||||
local kids = node.children
|
||||
local k = 1
|
||||
for i, n in names do
|
||||
out[i] = num(childText(node, n))
|
||||
local c = nil
|
||||
while k <= #kids do
|
||||
local ch = kids[k]
|
||||
if type(ch) == "table" then
|
||||
if ch.name == n then
|
||||
c = ch
|
||||
end
|
||||
break
|
||||
end
|
||||
k += 1
|
||||
end
|
||||
if c then
|
||||
k += 1
|
||||
else
|
||||
c = Xml.child(node, n)
|
||||
end
|
||||
out[i] = num(if c then Xml.text(c) else nil)
|
||||
end
|
||||
end
|
||||
return out
|
||||
@@ -117,10 +140,10 @@ end
|
||||
local CF_NAMES = { "X", "Y", "Z", "R00", "R01", "R02", "R10", "R11", "R12", "R20", "R21", "R22" }
|
||||
local PHYS_NAMES = { "Density", "Friction", "Elasticity", "FrictionWeight", "ElasticityWeight" }
|
||||
|
||||
local function readRaw(node: Xml.Node, shared: { [string]: string }, key: string): (string, any)
|
||||
local function readRaw(node: Xml.Node, shared: { [string]: string }, cls: string, pname: string): (string, any)
|
||||
local tag = node.name
|
||||
local scalar = SCALAR_TAGS[tag]
|
||||
if tag == "int" and BRICKCOLOR[key] then
|
||||
if tag == "int" and BRICKCOLOR[cls .. "." .. pname] then
|
||||
scalar = "BrickColor"
|
||||
end
|
||||
if scalar then
|
||||
@@ -264,8 +287,8 @@ local function round32(v: any, which: any): any
|
||||
end
|
||||
return v
|
||||
end
|
||||
local function readValue(node: Xml.Node, shared: { [string]: string }, key: string): (string, any)
|
||||
local t, v = readRaw(node, shared, key)
|
||||
local function readValue(node: Xml.Node, shared: { [string]: string }, cls: string, pname: string): (string, any)
|
||||
local t, v = readRaw(node, shared, cls, pname)
|
||||
local which = F32[t]
|
||||
if which then
|
||||
v = round32(v, which)
|
||||
@@ -316,7 +339,7 @@ function Rbxmx.decode(r: Bytes.Reader, opts: DecodeOptions?): (Rbx.Tree?, Err.Er
|
||||
if type(pnode) == "table" and string.sub(pnode.name, 1, 1) ~= "#" then
|
||||
local pname = pnode.attrs.name
|
||||
if type(pname) == "string" then
|
||||
local t, v = readValue(pnode, shared, cls .. "." .. pname)
|
||||
local t, v = readValue(pnode, shared, cls, pname)
|
||||
if pname == "Name" and (t == "String" or t == "ProtectedString") then
|
||||
inst.name = v
|
||||
else
|
||||
|
||||
Reference in new issue
Block a user