lingo3d
Version:
Lingo3D is a React/Vue 3d game development framework that ships with a complete visual editor
72 lines • 3.23 kB
JavaScript
import { equalsDefaultValue } from "../../interface/utils/getDefaultValue";
import toFixed, { toFixedPoint } from "./toFixed";
import { VERSION } from "../../globals";
import { isPoint } from "../../utils/isPoint";
import nonSerializedProperties from "./nonSerializedProperties";
import unsafeGetValue from "../../utils/unsafeGetValue";
import getStaticProperties from "../../display/utils/getStaticProperties";
import { appendableRoot } from "../../collections/appendableRoot";
import { isTemplate, isTemplateNode } from "../../collections/typeGuards";
const serialize = (children, skipUUID, skipTemplateCheck, skipDescendants) => {
const dataParent = [];
for (const child of children) {
if (child.$disableSerialize)
continue;
const { componentName, schema } = getStaticProperties(child);
const data = skipTemplateCheck
? { type: componentName }
: isTemplateNode(child)
? {
type: "templateNode",
source: componentName,
spawnNode: child.spawnNode
}
: isTemplate(child)
? { type: "template", source: componentName }
: { type: componentName };
for (const [key, type] of Object.entries(schema)) {
if (type === Function ||
(Array.isArray(type) && type.includes(Function)) ||
nonSerializedProperties.includes(key) ||
(skipUUID && key === "uuid"))
continue;
let value;
if (key === "animations" && "serializeAnimations" in child) {
value = child.serializeAnimations;
if (!value)
continue;
}
else if (key === "animation" && "serializeAnimation" in child) {
value = child.serializeAnimation;
if (!value)
continue;
}
else
value = unsafeGetValue(child, key);
const t = typeof value;
if (equalsDefaultValue(value, child, key) || t === "function")
continue;
if (t === "number")
value = toFixed(value);
else if (isPoint(value, t))
value = toFixedPoint(value);
else if (Array.isArray(value) && value.some((v) => isPoint(v)))
value = value.map((v) => (isPoint(v) ? toFixedPoint(v) : v));
data[key] = value;
}
if (!skipDescendants && child.children) {
const dataChildren = serialize(child.children, skipUUID, skipTemplateCheck, skipDescendants);
if (dataChildren.length)
data.children = dataChildren;
}
dataParent.push(data);
}
return dataParent;
};
export const serializeAppendable = (appendable, skipUUID = true, skipDescendants = false) => serialize([appendable], skipUUID, true, skipDescendants)[0];
export default (versionStamp) => {
const result = serialize(appendableRoot, false, false, false);
versionStamp && result.unshift({ type: "lingo3d", version: VERSION });
return result;
};
//# sourceMappingURL=serialize.js.map