this repo has no description
1import { NodeManager } from "../Mangers/NodeManager";
2import { NodesByID } from "../Nodes/Nodes";
3import { Node } from "../structs/node";
4
5export let encodeNodeList = ( selectedNodes: Node[], mousePos: [ number, number ] ): string => {
6 let arr: any[] = [];
7
8 for(let node of selectedNodes){
9 arr.push({
10 id: node.id,
11 type_id: node.typeId,
12 statics: node.statics,
13 x: node.x - mousePos[0],
14 y: node.y - mousePos[1],
15 outputs: node.outputs.map(x => {
16 return x.connections.map(x => {
17 return { node: x.parent.id, index: x.index } }) })
18 })
19 }
20
21 for(let node of arr){
22 for(let output of node.outputs){
23 for(let i in output){
24 let indx = arr.findIndex(x => x.id === output[i].node);
25 if(indx === -1)
26 delete output[i];
27 else
28 output[i].node = indx;
29 }
30 }
31 }
32
33 for(let node of arr)delete node.id;
34
35 console.log(arr);
36 return 'VRCMACRO' + btoa(JSON.stringify(arr));
37}
38
39export let decodeNodeList = async ( text: string, mousePos: [ number, number ] ): Promise<Node[] | null> => {
40 if(!text.startsWith("VRCMACRO"))return null;
41
42 let data = text.slice(8);
43 let json = JSON.parse(atob(data));
44
45 let nodes: Node[] = [];
46 for(let node of json){
47 let n = new Node(
48 [ node.x + mousePos[0] + 10, node.y + mousePos[1] + 10 ],
49 NodesByID[node.type_id],
50 await NodeManager.Instance.GetNewNodeId()
51 );
52
53 n.statics = node.statics;
54 await n.onStaticsUpdate(n);
55
56 nodes.push(n);
57 }
58
59 for(let i in nodes){
60 let outputs: { node: number, index: number }[][] = json[i].outputs;
61 let node = nodes[i];
62
63 for(let j in outputs){
64 let output = node.outputs[j];
65
66 for(let k in outputs[j]){
67 let connection = outputs[j][k];
68 if(!connection)continue;
69
70 let peerNode = nodes[connection.node];
71 let input = peerNode.inputs[connection.index];
72
73 output.connections.push(input);
74 input.connections.push(output);
75 }
76 }
77 }
78
79 return nodes;
80}