Skip to content

Commit

Permalink
op code preview function
Browse files Browse the repository at this point in the history
  • Loading branch information
steam0r committed Dec 18, 2024
1 parent 3de57dc commit 0f828a6
Showing 1 changed file with 128 additions and 1 deletion.
129 changes: 128 additions & 1 deletion shared/api/utils/shared_ops_util.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ export default class SharedOpsUtil extends SharedUtil
"-": "___"
};

this.OP_PORT_TYPE_NUMBER = 0;
this.OP_PORT_TYPE_TRIGGER = 1;
this.OP_PORT_TYPE_OBJECT = 2;
this.OP_PORT_TYPE_ARRAY = 3;
this.OP_PORT_TYPE_DYNAMIC = 4;
this.OP_PORT_TYPE_STRING = 5;

this.FXHASH_OP_NAME = "Ops.Extension.FxHash.FxHash";

this.SUBPATCH_ATTACHMENT_NAME = "att_subpatch_json";
Expand Down Expand Up @@ -1399,6 +1406,126 @@ export default class SharedOpsUtil extends SharedUtil
return fullCode;
}

buildPreviewCode(opNames)
{
let codeNamespaces = [];
let code = "";

for (const i in opNames)
{
let opName = opNames[i];
const opId = this.getOpIdByObjName(opName);
const opJsonFile = this.getOpAbsoluteJsonFilename(opName);
try
{
const opJson = jsonfile.readFileSync(opJsonFile);
const opLayout = opJson.layout;
if (opLayout)
{
let opCode = "";
const codeHead = "\n\n// **************************************************************\n" +
"// \n" +
"// " + opName + "\n" +
"// \n" +
"// **************************************************************\n\n" +
opName + " = function()\n{\nCABLES.Op.apply(this,arguments);\nconst op=this;\n";

if (opLayout.portsIn)
{
opLayout.portsIn.forEach((port) =>
{
if (port.name)
{
switch (port.type)
{
case this.OP_PORT_TYPE_TRIGGER:
opCode += "op.inTrigger(\"" + port.name + "\");\n";
break;
case this.OP_PORT_TYPE_OBJECT:
opCode += "op.inObject(\"" + port.name + "\");\n";
break;
case this.OP_PORT_TYPE_ARRAY:
opCode += "op.inArray(\"" + port.name + "\");\n";
break;
case this.OP_PORT_TYPE_DYNAMIC:
opCode += "op.inDynamic(\"" + port.name + "\");\n";
break;
case this.OP_PORT_TYPE_STRING:
opCode += "op.inString(\"" + port.name + "\");\n";
break;
case this.OP_PORT_TYPE_NUMBER:
default:
opCode += "op.inFloat(\"" + port.name + "\");\n";
break;
}
}
});
}

if (opLayout.portsOut)
{
opLayout.portsOut.forEach((port) =>
{
if (port.name)
{
switch (port.type)
{
case this.OP_PORT_TYPE_TRIGGER:
opCode += "op.outTrigger(\"" + port.name + "\");\n";
break;
case this.OP_PORT_TYPE_OBJECT:
opCode += "op.outObject(\"" + port.name + "\");\n";
break;
case this.OP_PORT_TYPE_ARRAY:
opCode += "op.outArray(\"" + port.name + "\");\n";
break;
case this.OP_PORT_TYPE_STRING:
opCode += "op.outString(\"" + port.name + "\");\n";
break;
case this.OP_PORT_TYPE_NUMBER:
default:
opCode += "op.outNumber(\"" + port.name + "\");\n";
break;
}
}
});
}

let codeFoot = "\n\n};\n\n" + opName + ".prototype = new CABLES.Op();\n";

if (opId) codeFoot += "CABLES.OPS[\"" + opId + "\"]={f:" + opName + ",objName:\"" + opName + "\"};";
codeFoot += "\n\n\n";

code += codeHead + opCode + codeFoot;
}
const parts = opName.split(".");
for (let k = 1; k < parts.length; k++)
{
let partPartname = "";
for (let j = 0; j < k; j++) partPartname += parts[j] + ".";

partPartname = partPartname.substr(0, partPartname.length - 1);
codeNamespaces.push(partPartname + "=" + partPartname + " || {};");
}
}
catch (e)
{
this._log.warn("op layout read error: " + opName, this.getOpAbsoluteJsonFilename(opName), e);
}
}

codeNamespaces = this._helperUtil.sortAndReduce(codeNamespaces);
let fullCode = this.OPS_CODE_PREFIX;
if (codeNamespaces && codeNamespaces.length > 0)
{
codeNamespaces[0] = "var " + codeNamespaces[0];
fullCode += codeNamespaces.join("\n") + "\n\n";
}

fullCode += code;
return fullCode;
}

validateAndFormatOpCode(code)
{
const { results } = this.cli.executeOnText(code);
Expand Down Expand Up @@ -3616,7 +3743,7 @@ export default class SharedOpsUtil extends SharedUtil
if (!envDocs.id) envDocs.id = envDoc.id;
if (!envDocs.name) envDocs.name = envDoc.name;
}
else if (result.data && result.data.name)
else if (result.error && result.code === 403 && result.data && result.data.name)
{
const envName = envUrls[i].hostname;
envDocs.environments.push(envName);
Expand Down

0 comments on commit 0f828a6

Please sign in to comment.