mirror of
https://github.com/dwolfe884/obsidian-x86-flow-graph.git
synced 2026-07-22 07:30:26 +00:00
Removed import, moved everything inside class, added more this. calls.
This commit is contained in:
parent
ded6a2bfc7
commit
28542b3492
2 changed files with 251 additions and 249 deletions
343
main.js
343
main.js
|
|
@ -28,213 +28,216 @@ __export(src_exports, {
|
||||||
});
|
});
|
||||||
module.exports = __toCommonJS(src_exports);
|
module.exports = __toCommonJS(src_exports);
|
||||||
var import_obsidian = require("obsidian");
|
var import_obsidian = require("obsidian");
|
||||||
var nodeid = 1;
|
|
||||||
var edgeid = 5555;
|
|
||||||
var workingx = 0;
|
|
||||||
var workingy = 0;
|
|
||||||
var nodes = [];
|
|
||||||
var edges = [];
|
|
||||||
var lines = [];
|
|
||||||
var fromnode = -1;
|
|
||||||
var locations = {};
|
|
||||||
var visitslocs = {};
|
|
||||||
var colorGreen = "4";
|
|
||||||
var x86_flow_graph = class extends import_obsidian.Plugin {
|
var x86_flow_graph = class extends import_obsidian.Plugin {
|
||||||
|
constructor() {
|
||||||
|
super(...arguments);
|
||||||
|
this.nodeid = 1;
|
||||||
|
this.edgeid = 5555;
|
||||||
|
this.workingx = 0;
|
||||||
|
this.workingy = 0;
|
||||||
|
this.nodes = [];
|
||||||
|
this.edges = [];
|
||||||
|
this.lines = [];
|
||||||
|
this.fromnode = -1;
|
||||||
|
this.locations = {};
|
||||||
|
this.visitslocs = {};
|
||||||
|
this.colorGreen = "4";
|
||||||
|
this.colorRed = "1";
|
||||||
|
}
|
||||||
async onload() {
|
async onload() {
|
||||||
this.addCommand({
|
this.addCommand({
|
||||||
id: "x86-create-flow-diagram",
|
id: "x86-create-flow-diagram",
|
||||||
name: "Convert x86 assembly into a flow diagram on a canvas",
|
name: "Convert x86 assembly into a flow diagram on a canvas",
|
||||||
editorCallback: (editor, view) => {
|
editorCallback: (editor, view) => {
|
||||||
lines = [];
|
this.lines = [];
|
||||||
nodes = [];
|
this.nodes = [];
|
||||||
nodes = [];
|
this.edges = [];
|
||||||
edges = [];
|
this.visitslocs = {};
|
||||||
visitslocs = {};
|
this.locations = {};
|
||||||
locations = {};
|
this.nodeid = 1;
|
||||||
nodeid = 1;
|
this.edgeid = 5555;
|
||||||
edgeid = 5555;
|
this.workingx = 0;
|
||||||
workingx = 0;
|
this.workingy = 0;
|
||||||
workingy = 0;
|
|
||||||
const tmp = editor.getSelection().split("\n");
|
const tmp = editor.getSelection().split("\n");
|
||||||
tmp.forEach((element) => {
|
tmp.forEach((element) => {
|
||||||
if (element != "" && !element.contains("```")) {
|
if (element != "" && !element.contains("```")) {
|
||||||
lines.push(element);
|
this.lines.push(element);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
lines.forEach((line, linenum) => {
|
this.lines.forEach((line, linenum) => {
|
||||||
if (line[0] != " " && line[0] != " ") {
|
if (line[0] != " " && line[0] != " ") {
|
||||||
let newkey = line.trim().split("#")[0].trim();
|
let newkey = line.trim().split("#")[0].trim();
|
||||||
if (newkey[newkey.length - 1] == ":") {
|
if (newkey[newkey.length - 1] == ":") {
|
||||||
newkey = newkey.slice(0, newkey.length - 1);
|
newkey = newkey.slice(0, newkey.length - 1);
|
||||||
}
|
}
|
||||||
if (!(newkey in locations)) {
|
if (!(newkey in this.locations)) {
|
||||||
locations[newkey] = linenum;
|
this.locations[newkey] = linenum;
|
||||||
visitslocs[newkey] = 0;
|
this.visitslocs[newkey] = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
generatenodes(0, lines, fromnode, "");
|
this.generatenodes(0, this.lines, this.fromnode, "");
|
||||||
let outfile = "";
|
let outfile = "";
|
||||||
const currfile = this.app.workspace.getActiveFile();
|
const currfile = this.app.workspace.getActiveFile();
|
||||||
const d = new Date();
|
const d = new Date();
|
||||||
if (currfile) {
|
if (currfile) {
|
||||||
outfile = currfile.parent.path + "/" + d.getTime() + ".canvas";
|
outfile = currfile.parent.path + "/" + d.getTime() + ".canvas";
|
||||||
}
|
}
|
||||||
let finalCanvas = { nodes, edges };
|
let finalCanvas = { nodes: this.nodes, edges: this.edges };
|
||||||
this.app.vault.create(outfile, JSON.stringify(finalCanvas));
|
this.app.vault.create(outfile, JSON.stringify(finalCanvas));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
};
|
generatenodes(linenum, text, fromnode, edgelabel) {
|
||||||
function generatenodes(linenum, text, fromnode2, edgelabel) {
|
const retarray = this.MakeNodeFromLineToNextJump(linenum, text, fromnode, edgelabel);
|
||||||
const retarray = MakeNodeFromLineToNextJump(linenum, text, fromnode2, edgelabel);
|
let newnode = retarray[0];
|
||||||
let newnode = retarray[0];
|
let whereto = retarray[1];
|
||||||
let whereto = retarray[1];
|
if (newnode != null) {
|
||||||
if (newnode != null) {
|
fromnode = newnode["id"];
|
||||||
fromnode2 = newnode["id"];
|
const wegood = this.nodeAlredyAdded(newnode);
|
||||||
const wegood = nodeAlredyAdded(newnode);
|
if (wegood) {
|
||||||
if (wegood) {
|
return;
|
||||||
return;
|
} else {
|
||||||
} else {
|
this.nodes.push(newnode);
|
||||||
nodes.push(newnode);
|
}
|
||||||
|
}
|
||||||
|
if (whereto == "fin") {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
edgelabel = "";
|
||||||
|
if (whereto.length != 1) {
|
||||||
|
edgelabel = "true";
|
||||||
|
}
|
||||||
|
if (!(whereto[0] in this.locations)) {
|
||||||
|
newnode = this.generateNewNode("```\nERROR: Jumping to non-existant\nlocation " + whereto[0].trim() + "\n```", -1, -1, 1);
|
||||||
|
const newedge = { id: this.edgeid.toString(), fromNode: this.nodes[this.nodes.length - 1].id.toString().toString(), fromSide: "bottom", toNode: newnode["id"], toSide: "top", label: "true", color: this.colorGreen };
|
||||||
|
this.edges.push(newedge);
|
||||||
|
this.nodes.push(newnode);
|
||||||
|
this.edgeid = this.edgeid + 1;
|
||||||
|
} else {
|
||||||
|
this.generatenodes(this.locations[whereto[0]], text, fromnode, edgelabel);
|
||||||
|
}
|
||||||
|
if (whereto.length == 2) {
|
||||||
|
this.generatenodes(whereto[1], text, fromnode, "false");
|
||||||
}
|
}
|
||||||
}
|
|
||||||
if (whereto == "fin") {
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
edgelabel = "";
|
nodeAlredyAdded(checknode) {
|
||||||
if (whereto.length != 1) {
|
let retval = false;
|
||||||
edgelabel = "true";
|
this.nodes.forEach((node) => {
|
||||||
|
if (checknode["startline"] == node["startline"] && checknode["endline"] == node["endline"]) {
|
||||||
|
retval = true;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
return retval;
|
||||||
}
|
}
|
||||||
if (!(whereto[0] in locations)) {
|
MakeNodeFromLineToNextJump(linenum, text, fromnode, edgelabel) {
|
||||||
newnode = generateNewNode("```\nERROR: Jumping to non-existant\nlocation " + whereto[0].trim() + "\n```", -1, -1, 1);
|
let currnode = "```\n";
|
||||||
const newedge = { id: edgeid.toString(), fromNode: (nodeid - 2).toString(), fromSide: "bottom", toNode: newnode["id"], toSide: "top", label: "true", color: colorGreen };
|
let i = linenum;
|
||||||
edges.push(newedge);
|
let newnode;
|
||||||
nodes.push(newnode);
|
let jmploc;
|
||||||
edgeid = edgeid + 1;
|
let newedge;
|
||||||
} else {
|
let edgecolor = "";
|
||||||
generatenodes(locations[whereto[0]], text, fromnode2, edgelabel);
|
let side = 1;
|
||||||
}
|
if (edgelabel == "false") {
|
||||||
if (whereto.length == 2) {
|
edgecolor = "1";
|
||||||
generatenodes(whereto[1], text, fromnode2, "false");
|
side = -1;
|
||||||
}
|
} else if (edgelabel == "true") {
|
||||||
return;
|
edgecolor = "4";
|
||||||
}
|
side = 1;
|
||||||
function nodeAlredyAdded(checknode) {
|
|
||||||
let retval = false;
|
|
||||||
nodes.forEach((node) => {
|
|
||||||
if (checknode["startline"] == node["startline"] && checknode["endline"] == node["endline"]) {
|
|
||||||
retval = true;
|
|
||||||
}
|
}
|
||||||
});
|
while (i < text.length) {
|
||||||
return retval;
|
let line = text[i];
|
||||||
}
|
if (line[0] == " " || line[0] == " ") {
|
||||||
function MakeNodeFromLineToNextJump(linenum, text, fromnode2, edgelabel) {
|
if (line.trim()[0] == "j") {
|
||||||
let currnode = "```\n";
|
currnode = currnode + line + "\n```";
|
||||||
let i = linenum;
|
newnode = this.generateNewNode(currnode, linenum, i, side);
|
||||||
let newnode;
|
if (fromnode != -1) {
|
||||||
let jmploc;
|
newedge = { id: this.edgeid.toString(), fromNode: fromnode.toString(), fromSide: "bottom", toNode: newnode["id"], toSide: "top", label: edgelabel, color: edgecolor };
|
||||||
let newedge;
|
this.edges.push(newedge);
|
||||||
let edgecolor = "";
|
this.edgeid = this.edgeid + 1;
|
||||||
let side = 1;
|
|
||||||
if (edgelabel == "false") {
|
|
||||||
edgecolor = "1";
|
|
||||||
side = -1;
|
|
||||||
} else if (edgelabel == "true") {
|
|
||||||
edgecolor = "4";
|
|
||||||
side = 1;
|
|
||||||
}
|
|
||||||
while (i < text.length) {
|
|
||||||
let line = text[i];
|
|
||||||
if (line[0] == " " || line[0] == " ") {
|
|
||||||
if (line.trim()[0] == "j") {
|
|
||||||
currnode = currnode + line + "\n```";
|
|
||||||
newnode = generateNewNode(currnode, linenum, i, side);
|
|
||||||
if (fromnode2 != -1) {
|
|
||||||
newedge = { id: edgeid.toString(), fromNode: fromnode2.toString(), fromSide: "bottom", toNode: newnode["id"], toSide: "top", label: edgelabel, color: edgecolor };
|
|
||||||
edges.push(newedge);
|
|
||||||
edgeid = edgeid + 1;
|
|
||||||
}
|
|
||||||
if (line.trim().slice(0, 3) == "jmp") {
|
|
||||||
jmploc = [line.trim().slice(line.trim().indexOf(" ") + 1, line.length).split("#")[0].trim()];
|
|
||||||
} else {
|
|
||||||
jmploc = [line.trim().slice(line.trim().indexOf(" ") + 1, line.length).split("#")[0].trim(), i + 1];
|
|
||||||
}
|
|
||||||
i = text.length + 20;
|
|
||||||
} else {
|
|
||||||
currnode = currnode + line + "\n";
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
line = line.trim();
|
|
||||||
if (line[line.length - 1] == ":") {
|
|
||||||
line = line.slice(0, line.length - 1);
|
|
||||||
}
|
|
||||||
if (visitslocs[line.trim()] == 0 && i != linenum) {
|
|
||||||
currnode = currnode + "```";
|
|
||||||
newnode = generateNewNode(currnode, linenum, i, side);
|
|
||||||
jmploc = [line.trim().slice(line.trim().indexOf(" ") + 1, line.length).split("#")[0].trim()];
|
|
||||||
if (fromnode2 != -1) {
|
|
||||||
newedge = { id: edgeid.toString(), fromNode: fromnode2.toString(), fromSide: "bottom", toNode: newnode["id"].toString(), toSide: "top", label: edgelabel, color: edgecolor };
|
|
||||||
edges.push(newedge);
|
|
||||||
edgeid = edgeid + 1;
|
|
||||||
}
|
|
||||||
i = text.length + 20;
|
|
||||||
} else if (visitslocs[line.trim()] == 0 && i == linenum) {
|
|
||||||
currnode = currnode + line + "\n";
|
|
||||||
visitslocs[line.trim()] = nodeid;
|
|
||||||
} else {
|
|
||||||
if (currnode == "```\n") {
|
|
||||||
newedge = { id: edgeid.toString(), fromNode: fromnode2.toString(), fromSide: "bottom", toNode: visitslocs[line.trim()].toString(), toSide: "top", label: edgelabel, color: edgecolor };
|
|
||||||
edges.push(newedge);
|
|
||||||
edgeid = edgeid + 1;
|
|
||||||
} else {
|
|
||||||
currnode = currnode + "\n```";
|
|
||||||
newnode = generateNewNode(currnode, linenum, i, side);
|
|
||||||
if (fromnode2 != -1) {
|
|
||||||
newedge = { id: edgeid.toString(), fromNode: fromnode2.toString(), fromSide: "bottom", toNode: newnode["id"].toString(), toSide: "top", label: edgelabel, color: edgecolor };
|
|
||||||
edges.push(newedge);
|
|
||||||
edgeid = edgeid + 1;
|
|
||||||
newedge = { id: edgeid.toString(), fromNode: newnode["id"].toString(), fromSide: "bottom", toNode: visitslocs[line.trim()].toString(), toSide: "top", label: "" };
|
|
||||||
edges.push(newedge);
|
|
||||||
edgeid = edgeid + 1;
|
|
||||||
}
|
}
|
||||||
|
if (line.trim().slice(0, 3) == "jmp") {
|
||||||
|
jmploc = [line.trim().slice(line.trim().indexOf(" ") + 1, line.length).split("#")[0].trim()];
|
||||||
|
} else {
|
||||||
|
jmploc = [line.trim().slice(line.trim().indexOf(" ") + 1, line.length).split("#")[0].trim(), i + 1];
|
||||||
|
}
|
||||||
|
i = text.length + 20;
|
||||||
|
} else {
|
||||||
|
currnode = currnode + line + "\n";
|
||||||
}
|
}
|
||||||
i = text.length + 20;
|
} else {
|
||||||
jmploc = "fin";
|
line = line.trim();
|
||||||
|
if (line[line.length - 1] == ":") {
|
||||||
|
line = line.slice(0, line.length - 1);
|
||||||
|
}
|
||||||
|
if (this.visitslocs[line.trim()] == 0 && i != linenum) {
|
||||||
|
currnode = currnode + "```";
|
||||||
|
newnode = this.generateNewNode(currnode, linenum, i, side);
|
||||||
|
jmploc = [line.trim().slice(line.trim().indexOf(" ") + 1, line.length).split("#")[0].trim()];
|
||||||
|
if (fromnode != -1) {
|
||||||
|
newedge = { id: this.edgeid.toString(), fromNode: fromnode.toString(), fromSide: "bottom", toNode: newnode["id"].toString(), toSide: "top", label: edgelabel, color: edgecolor };
|
||||||
|
this.edges.push(newedge);
|
||||||
|
this.edgeid = this.edgeid + 1;
|
||||||
|
}
|
||||||
|
i = text.length + 20;
|
||||||
|
} else if (this.visitslocs[line.trim()] == 0 && i == linenum) {
|
||||||
|
currnode = currnode + line + "\n";
|
||||||
|
this.visitslocs[line.trim()] = this.nodeid;
|
||||||
|
} else {
|
||||||
|
if (currnode == "```\n") {
|
||||||
|
newedge = { id: this.edgeid.toString(), fromNode: fromnode.toString(), fromSide: "bottom", toNode: this.visitslocs[line.trim()].toString(), toSide: "top", label: edgelabel, color: edgecolor };
|
||||||
|
this.edges.push(newedge);
|
||||||
|
this.edgeid = this.edgeid + 1;
|
||||||
|
} else {
|
||||||
|
currnode = currnode + "\n```";
|
||||||
|
newnode = this.generateNewNode(currnode, linenum, i, side);
|
||||||
|
if (fromnode != -1) {
|
||||||
|
newedge = { id: this.edgeid.toString(), fromNode: fromnode.toString(), fromSide: "bottom", toNode: newnode["id"].toString(), toSide: "top", label: edgelabel, color: edgecolor };
|
||||||
|
this.edges.push(newedge);
|
||||||
|
this.edgeid = this.edgeid + 1;
|
||||||
|
newedge = { id: this.edgeid.toString(), fromNode: newnode["id"].toString(), fromSide: "bottom", toNode: this.visitslocs[line.trim()].toString(), toSide: "top", label: "" };
|
||||||
|
this.edges.push(newedge);
|
||||||
|
this.edgeid = this.edgeid + 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
i = text.length + 20;
|
||||||
|
jmploc = "fin";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
i = i + 1;
|
||||||
|
}
|
||||||
|
if (i != text.length + 21) {
|
||||||
|
if (currnode == "```\n") {
|
||||||
|
currnode = currnode + "End of assembly\n```";
|
||||||
|
} else {
|
||||||
|
currnode = currnode + "```";
|
||||||
|
}
|
||||||
|
newnode = this.generateNewNode(currnode, linenum, i, side);
|
||||||
|
jmploc = "fin";
|
||||||
|
if (fromnode != -1) {
|
||||||
|
newedge = { id: this.edgeid.toString(), fromNode: fromnode.toString(), fromSide: "bottom", toNode: newnode["id"].toString(), toSide: "top", label: edgelabel, color: edgecolor };
|
||||||
|
this.edges.push(newedge);
|
||||||
|
this.edgeid = this.edgeid + 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
i = i + 1;
|
return [newnode, jmploc];
|
||||||
}
|
}
|
||||||
if (i != text.length + 21) {
|
generateNewNode(nodeText, startLineNum, endLineNum, side) {
|
||||||
if (currnode == "```\n") {
|
const newnode = {
|
||||||
currnode = currnode + "End of assembly\n```";
|
type: "text",
|
||||||
} else {
|
id: this.nodeid.toString(),
|
||||||
currnode = currnode + "```";
|
x: this.workingx * side,
|
||||||
}
|
y: this.workingy,
|
||||||
newnode = generateNewNode(currnode, linenum, i, side);
|
width: 550,
|
||||||
jmploc = "fin";
|
height: 35 * nodeText.split("\n").length,
|
||||||
if (fromnode2 != -1) {
|
text: nodeText,
|
||||||
newedge = { id: edgeid.toString(), fromNode: fromnode2.toString(), fromSide: "bottom", toNode: newnode["id"].toString(), toSide: "top", label: edgelabel, color: edgecolor };
|
startline: startLineNum,
|
||||||
edges.push(newedge);
|
endline: endLineNum
|
||||||
edgeid = edgeid + 1;
|
};
|
||||||
}
|
this.workingy = this.workingy + 300;
|
||||||
|
this.workingx = this.workingx + 50 * this.nodeid;
|
||||||
|
this.nodeid = this.nodeid + 1;
|
||||||
|
return newnode;
|
||||||
}
|
}
|
||||||
return [newnode, jmploc];
|
};
|
||||||
}
|
|
||||||
function generateNewNode(nodeText, startLineNum, endLineNum, side) {
|
|
||||||
const newnode = {
|
|
||||||
type: "text",
|
|
||||||
id: nodeid.toString(),
|
|
||||||
x: workingx * side,
|
|
||||||
y: workingy,
|
|
||||||
width: 550,
|
|
||||||
height: 35 * nodeText.split("\n").length,
|
|
||||||
text: nodeText,
|
|
||||||
startline: startLineNum,
|
|
||||||
endline: endLineNum
|
|
||||||
};
|
|
||||||
workingy = workingy + 300;
|
|
||||||
workingx = workingx + 50 * nodeid;
|
|
||||||
nodeid = nodeid + 1;
|
|
||||||
return newnode;
|
|
||||||
}
|
|
||||||
|
|
|
||||||
157
src/index.ts
157
src/index.ts
|
|
@ -1,22 +1,23 @@
|
||||||
import { App, Editor, MarkdownView, Modal, Notice, Plugin, PluginSettingTab, Setting, Vault, Workspace } from 'obsidian';
|
import { App, Editor, MarkdownView, Modal, Notice, Plugin, PluginSettingTab, Setting, Vault, Workspace } from 'obsidian';
|
||||||
import { CanvasTextData, CanvasColor, NodeSide, CanvasEdgeData, CanvasData } from 'obsidian/canvas';
|
import { CanvasTextData, CanvasColor, NodeSide, CanvasEdgeData, CanvasData } from 'obsidian/canvas';
|
||||||
import * as internal from 'stream';
|
|
||||||
|
|
||||||
let nodeid = 1;
|
|
||||||
let edgeid = 5555
|
|
||||||
let workingx = 0;
|
|
||||||
let workingy = 0;
|
|
||||||
let nodes: any[] = []
|
|
||||||
let edges: CanvasEdgeData[] = [] //{"id":"d1e0d15da69178a9","fromNode":"4018052da21dde12","fromSide":"bottom","toNode":"0afaa4e14a75cfe1","toSide":"top","label":"false"}
|
|
||||||
let lines: string[] = []
|
|
||||||
let fromnode = -1
|
|
||||||
let locations: any = {}
|
|
||||||
let visitslocs: any = {}
|
|
||||||
let colorGreen: CanvasColor = "4"
|
|
||||||
let colorRed: CanvasColor = "1"
|
|
||||||
|
|
||||||
export default class x86_flow_graph extends Plugin {
|
export default class x86_flow_graph extends Plugin {
|
||||||
|
|
||||||
|
nodeid = 1;
|
||||||
|
edgeid = 5555
|
||||||
|
workingx = 0;
|
||||||
|
workingy = 0;
|
||||||
|
nodes: any[] = []
|
||||||
|
edges: CanvasEdgeData[] = [] //{"id":"d1e0d15da69178a9","fromNode":"4018052da21dde12","fromSide":"bottom","toNode":"0afaa4e14a75cfe1","toSide":"top","label":"false"}
|
||||||
|
lines: string[] = []
|
||||||
|
fromnode = -1
|
||||||
|
locations: any = {}
|
||||||
|
visitslocs: any = {}
|
||||||
|
colorGreen: CanvasColor = "4"
|
||||||
|
colorRed: CanvasColor = "1"
|
||||||
|
|
||||||
async onload(){
|
async onload(){
|
||||||
// This adds the command to take selected text and create code flow diagram
|
// This adds the command to take selected text and create code flow diagram
|
||||||
this.addCommand({
|
this.addCommand({
|
||||||
|
|
@ -24,28 +25,27 @@ export default class x86_flow_graph extends Plugin {
|
||||||
name: 'Convert x86 assembly into a flow diagram on a canvas',
|
name: 'Convert x86 assembly into a flow diagram on a canvas',
|
||||||
editorCallback: (editor: Editor, view: MarkdownView) => {
|
editorCallback: (editor: Editor, view: MarkdownView) => {
|
||||||
// Array of splt
|
// Array of splt
|
||||||
lines = [];
|
this.lines = [];
|
||||||
nodes = [];
|
this.nodes = [];
|
||||||
nodes = [];
|
this.edges = [];
|
||||||
edges = [];
|
|
||||||
// Dictionary where keys = memory locations and values = if they have been seen in a node (1) or not (0)
|
// Dictionary where keys = memory locations and values = if they have been seen in a node (1) or not (0)
|
||||||
visitslocs = {};
|
this.visitslocs = {};
|
||||||
// Dictionary where keys = memory locations and values = line number
|
// Dictionary where keys = memory locations and values = line number
|
||||||
locations = {};
|
this.locations = {};
|
||||||
//NodeID's and EdgeID's must be unique for each node in a canvas
|
//NodeID's and EdgeID's must be unique for each node in a canvas
|
||||||
nodeid = 1;
|
this.nodeid = 1;
|
||||||
edgeid = 5555;
|
this.edgeid = 5555;
|
||||||
workingx = 0;
|
this.workingx = 0;
|
||||||
workingy = 0;
|
this.workingy = 0;
|
||||||
const tmp = editor.getSelection().split("\n")
|
const tmp = editor.getSelection().split("\n")
|
||||||
//For loop to remove blank lines and lines containing codeblock characters
|
//For loop to remove blank lines and lines containing codeblock characters
|
||||||
tmp.forEach(element => {
|
tmp.forEach(element => {
|
||||||
if(element != "" && !element.contains("```")){
|
if(element != "" && !element.contains("```")){
|
||||||
lines.push(element)
|
this.lines.push(element)
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
lines.forEach((line,linenum) => {
|
this.lines.forEach((line,linenum) => {
|
||||||
//Only look at lines that could be locations
|
//Only look at lines that could be locations
|
||||||
if(line[0] != '\t' && line[0] != " "){
|
if(line[0] != '\t' && line[0] != " "){
|
||||||
//Cut out white space and comments (#)
|
//Cut out white space and comments (#)
|
||||||
|
|
@ -55,15 +55,15 @@ export default class x86_flow_graph extends Plugin {
|
||||||
newkey = newkey.slice(0,newkey.length-1)
|
newkey = newkey.slice(0,newkey.length-1)
|
||||||
}
|
}
|
||||||
//Populate locations and visits array with line numbers and all 0's
|
//Populate locations and visits array with line numbers and all 0's
|
||||||
if (!(newkey in locations)) {
|
if (!(newkey in this.locations)) {
|
||||||
locations[newkey] = linenum;
|
this.locations[newkey] = linenum;
|
||||||
visitslocs[newkey] = 0
|
this.visitslocs[newkey] = 0
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
//Enter recursive function
|
//Enter recursive function
|
||||||
generatenodes(0,lines,fromnode,"")
|
this.generatenodes(0,this.lines,this.fromnode,"")
|
||||||
|
|
||||||
//Get current directory to produce canvas in
|
//Get current directory to produce canvas in
|
||||||
let outfile = ""
|
let outfile = ""
|
||||||
|
|
@ -72,29 +72,27 @@ export default class x86_flow_graph extends Plugin {
|
||||||
if(currfile){
|
if(currfile){
|
||||||
outfile = currfile.parent.path + "/" + d.getTime() + ".canvas"
|
outfile = currfile.parent.path + "/" + d.getTime() + ".canvas"
|
||||||
}
|
}
|
||||||
let finalCanvas: CanvasData = {nodes: nodes, edges: edges}
|
let finalCanvas: CanvasData = {nodes: this.nodes, edges: this.edges}
|
||||||
this.app.vault.create(outfile,JSON.stringify(finalCanvas))
|
this.app.vault.create(outfile,JSON.stringify(finalCanvas))
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
//Recursive function alert! Runs itself once for uncondontional jumps (jmps) and twice for conditional jumps (jz)
|
//Recursive function alert! Runs itself once for uncondontional jumps (jmps) and twice for conditional jumps (jz)
|
||||||
//Returns when it reaches the end of the assembly section OR if it returns to a code block it's seen before
|
//Returns when it reaches the end of the assembly section OR if it returns to a code block it's seen before
|
||||||
function generatenodes(linenum: any, text: any, fromnode: any, edgelabel: string){
|
generatenodes(linenum: any, text: any, fromnode: any, edgelabel: string){
|
||||||
const retarray = MakeNodeFromLineToNextJump(linenum, text, fromnode, edgelabel)
|
const retarray = this.MakeNodeFromLineToNextJump(linenum, text, fromnode, edgelabel)
|
||||||
let newnode: any = retarray[0]
|
let newnode: any = retarray[0]
|
||||||
let whereto: any = retarray[1]
|
let whereto: any = retarray[1]
|
||||||
if(newnode != null){
|
if(newnode != null){
|
||||||
fromnode = newnode['id']
|
fromnode = newnode['id']
|
||||||
//Check if node is already in list
|
//Check if node is already in list
|
||||||
const wegood = nodeAlredyAdded(newnode)
|
const wegood = this.nodeAlredyAdded(newnode)
|
||||||
if(wegood){
|
if(wegood){
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
else{
|
else{
|
||||||
nodes.push(newnode)
|
this.nodes.push(newnode)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
//We are at the end of the file
|
//We are at the end of the file
|
||||||
|
|
@ -106,28 +104,28 @@ function generatenodes(linenum: any, text: any, fromnode: any, edgelabel: string
|
||||||
edgelabel = "true"
|
edgelabel = "true"
|
||||||
}
|
}
|
||||||
//Error Handling for trying to jump to a non-existant location
|
//Error Handling for trying to jump to a non-existant location
|
||||||
if(!(whereto[0] in locations)){
|
if(!(whereto[0] in this.locations)){
|
||||||
//Make the error node
|
//Make the error node
|
||||||
newnode = generateNewNode("```\nERROR: Jumping to non-existant\nlocation " + whereto[0].trim() + "\n```", -1, -1, 1)
|
newnode = this.generateNewNode("```\nERROR: Jumping to non-existant\nlocation " + whereto[0].trim() + "\n```", -1, -1, 1)
|
||||||
//nodeid-2 refers to the ID of the node that generated the error
|
//nodeid-2 refers to the ID of the node that generated the error
|
||||||
const newedge: CanvasEdgeData = {id:edgeid.toString(),fromNode:(nodeid-2).toString(),fromSide:"bottom",toNode:newnode['id'],toSide:"top",label:"true", color:colorGreen}
|
const newedge: CanvasEdgeData = {id:this.edgeid.toString(),fromNode:(this.nodes[this.nodes.length-1].id.toString()/*this.nodeid-2*/).toString(),fromSide:"bottom",toNode:newnode['id'],toSide:"top",label:"true", color:this.colorGreen}
|
||||||
edges.push(newedge)
|
this.edges.push(newedge)
|
||||||
nodes.push(newnode)
|
this.nodes.push(newnode)
|
||||||
edgeid = edgeid + 1
|
this.edgeid = this.edgeid + 1
|
||||||
}
|
}
|
||||||
else{
|
else{
|
||||||
generatenodes(locations[whereto[0]], text, fromnode, edgelabel)
|
this.generatenodes(this.locations[whereto[0]], text, fromnode, edgelabel)
|
||||||
}
|
}
|
||||||
if(whereto.length == 2){
|
if(whereto.length == 2){
|
||||||
generatenodes(whereto[1], text, fromnode, "false")
|
this.generatenodes(whereto[1], text, fromnode, "false")
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
//This function returns true if the node has already been added to the nodes array
|
//This function returns true if the node has already been added to the nodes array
|
||||||
function nodeAlredyAdded(checknode: any){
|
nodeAlredyAdded(checknode: any){
|
||||||
let retval = false
|
let retval = false
|
||||||
nodes.forEach(node => {
|
this.nodes.forEach((node: { [x: string]: any; }) => {
|
||||||
if(checknode["startline"] == node["startline"] && checknode["endline"] == node["endline"]){
|
if(checknode["startline"] == node["startline"] && checknode["endline"] == node["endline"]){
|
||||||
retval = true
|
retval = true
|
||||||
}
|
}
|
||||||
|
|
@ -141,7 +139,7 @@ function nodeAlredyAdded(checknode: any){
|
||||||
//text : full assembly code block
|
//text : full assembly code block
|
||||||
//fromnode : nodeID of the previous node that needs to be connected via an edge (-1 if there isn't one)
|
//fromnode : nodeID of the previous node that needs to be connected via an edge (-1 if there isn't one)
|
||||||
//edgelabel : "true","false", or "" to color and label the edges
|
//edgelabel : "true","false", or "" to color and label the edges
|
||||||
function MakeNodeFromLineToNextJump(linenum: any, text: any, fromnode: any, edgelabel: string) {
|
MakeNodeFromLineToNextJump(linenum: any, text: any, fromnode: any, edgelabel: string) {
|
||||||
let currnode = "```\n"
|
let currnode = "```\n"
|
||||||
let i = linenum
|
let i = linenum
|
||||||
let newnode
|
let newnode
|
||||||
|
|
@ -164,12 +162,12 @@ function MakeNodeFromLineToNextJump(linenum: any, text: any, fromnode: any, edge
|
||||||
//If the current instruction is a jump
|
//If the current instruction is a jump
|
||||||
if(line.trim()[0] == 'j'){
|
if(line.trim()[0] == 'j'){
|
||||||
currnode = currnode + line + "\n```"
|
currnode = currnode + line + "\n```"
|
||||||
newnode = generateNewNode(currnode, linenum, i, side)
|
newnode = this.generateNewNode(currnode, linenum, i, side)
|
||||||
//Only parse the first node
|
//Only parse the first node
|
||||||
if(fromnode != -1){
|
if(fromnode != -1){
|
||||||
newedge = {id:edgeid.toString(),fromNode:fromnode.toString(),fromSide:"bottom",toNode:newnode["id"],toSide:"top",label:edgelabel, color:edgecolor}
|
newedge = {id:this.edgeid.toString(),fromNode:fromnode.toString(),fromSide:"bottom",toNode:newnode["id"],toSide:"top",label:edgelabel, color:edgecolor}
|
||||||
edges.push(newedge)
|
this.edges.push(newedge)
|
||||||
edgeid = edgeid + 1
|
this.edgeid = this.edgeid + 1
|
||||||
}
|
}
|
||||||
if(line.trim().slice(0,3) == 'jmp'){
|
if(line.trim().slice(0,3) == 'jmp'){
|
||||||
jmploc = [line.trim().slice(line.trim().indexOf(" ")+1,line.length).split("#")[0].trim()]
|
jmploc = [line.trim().slice(line.trim().indexOf(" ")+1,line.length).split("#")[0].trim()]
|
||||||
|
|
@ -191,46 +189,46 @@ function MakeNodeFromLineToNextJump(linenum: any, text: any, fromnode: any, edge
|
||||||
line = line.slice(0,line.length-1)
|
line = line.slice(0,line.length-1)
|
||||||
}
|
}
|
||||||
//Have we visited this location before (0 == no) and is this not the first line of a node?
|
//Have we visited this location before (0 == no) and is this not the first line of a node?
|
||||||
if(visitslocs[line.trim()] == 0 && i != linenum){
|
if(this.visitslocs[line.trim()] == 0 && i != linenum){
|
||||||
//Close the node text and create a node object
|
//Close the node text and create a node object
|
||||||
currnode = currnode + "```"
|
currnode = currnode + "```"
|
||||||
newnode = generateNewNode(currnode, linenum, i, side)
|
newnode = this.generateNewNode(currnode, linenum, i, side)
|
||||||
//Set the jump location to the location name strimming away the command and comments
|
//Set the jump location to the location name strimming away the command and comments
|
||||||
jmploc = [line.trim().slice(line.trim().indexOf(" ")+1,line.length).split("#")[0].trim()]
|
jmploc = [line.trim().slice(line.trim().indexOf(" ")+1,line.length).split("#")[0].trim()]
|
||||||
//If there is a node before the current one
|
//If there is a node before the current one
|
||||||
if(fromnode != -1){
|
if(fromnode != -1){
|
||||||
newedge = {id:edgeid.toString(),fromNode:fromnode.toString(),fromSide:"bottom",toNode:newnode["id"].toString(),toSide:"top",label:edgelabel,color:edgecolor}
|
newedge = {id:this.edgeid.toString(),fromNode:fromnode.toString(),fromSide:"bottom",toNode:newnode["id"].toString(),toSide:"top",label:edgelabel,color:edgecolor}
|
||||||
edges.push(newedge)
|
this.edges.push(newedge)
|
||||||
edgeid = edgeid + 1
|
this.edgeid = this.edgeid + 1
|
||||||
}
|
}
|
||||||
i = text.length+20
|
i = text.length+20
|
||||||
}
|
}
|
||||||
//Have we visited this location before and is this the first line of a node?
|
//Have we visited this location before and is this the first line of a node?
|
||||||
else if(visitslocs[line.trim()] == 0 && i == linenum){
|
else if(this.visitslocs[line.trim()] == 0 && i == linenum){
|
||||||
currnode = currnode + line + "\n"
|
currnode = currnode + line + "\n"
|
||||||
visitslocs[line.trim()] = nodeid
|
this.visitslocs[line.trim()] = this.nodeid
|
||||||
}
|
}
|
||||||
//We have visited this before
|
//We have visited this before
|
||||||
else{
|
else{
|
||||||
//If this is an empty node, just draw a line from the previous node to the already visited one
|
//If this is an empty node, just draw a line from the previous node to the already visited one
|
||||||
if(currnode == "```\n"){
|
if(currnode == "```\n"){
|
||||||
newedge = {id:edgeid.toString(),fromNode:fromnode.toString(),fromSide:"bottom",toNode:visitslocs[line.trim()].toString(),toSide:"top",label:edgelabel,color:edgecolor}
|
newedge = {id:this.edgeid.toString(),fromNode:fromnode.toString(),fromSide:"bottom",toNode:this.visitslocs[line.trim()].toString(),toSide:"top",label:edgelabel,color:edgecolor}
|
||||||
edges.push(newedge)
|
this.edges.push(newedge)
|
||||||
edgeid = edgeid + 1
|
this.edgeid = this.edgeid + 1
|
||||||
}
|
}
|
||||||
//If it's not empty make the node and draw 2 edges
|
//If it's not empty make the node and draw 2 edges
|
||||||
else{
|
else{
|
||||||
currnode = currnode + "\n```"
|
currnode = currnode + "\n```"
|
||||||
newnode = generateNewNode(currnode, linenum, i, side)
|
newnode = this.generateNewNode(currnode, linenum, i, side)
|
||||||
if(fromnode != -1){
|
if(fromnode != -1){
|
||||||
//Draw edge from previous node to the new node
|
//Draw edge from previous node to the new node
|
||||||
newedge = {id:edgeid.toString(),fromNode:fromnode.toString(),fromSide:"bottom",toNode:newnode["id"].toString(),toSide:"top",label:edgelabel,color:edgecolor}
|
newedge = {id:this.edgeid.toString(),fromNode:fromnode.toString(),fromSide:"bottom",toNode:newnode["id"].toString(),toSide:"top",label:edgelabel,color:edgecolor}
|
||||||
edges.push(newedge)
|
this.edges.push(newedge)
|
||||||
edgeid = edgeid + 1
|
this.edgeid = this.edgeid + 1
|
||||||
//Draw edge from new node to previously seen node
|
//Draw edge from new node to previously seen node
|
||||||
newedge = {id:edgeid.toString(),fromNode:newnode["id"].toString(),fromSide:"bottom",toNode:visitslocs[line.trim()].toString(),toSide:"top",label:""}
|
newedge = {id:this.edgeid.toString(),fromNode:newnode["id"].toString(),fromSide:"bottom",toNode:this.visitslocs[line.trim()].toString(),toSide:"top",label:""}
|
||||||
edges.push(newedge)
|
this.edges.push(newedge)
|
||||||
edgeid = edgeid + 1
|
this.edgeid = this.edgeid + 1
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
i = text.length+20
|
i = text.length+20
|
||||||
|
|
@ -250,24 +248,24 @@ function MakeNodeFromLineToNextJump(linenum: any, text: any, fromnode: any, edge
|
||||||
else{
|
else{
|
||||||
currnode = currnode + "```"
|
currnode = currnode + "```"
|
||||||
}
|
}
|
||||||
newnode = generateNewNode(currnode, linenum, i, side)
|
newnode = this.generateNewNode(currnode, linenum, i, side)
|
||||||
jmploc = "fin"
|
jmploc = "fin"
|
||||||
if(fromnode != -1){
|
if(fromnode != -1){
|
||||||
newedge = {id:edgeid.toString(),fromNode:fromnode.toString(),fromSide:"bottom",toNode:newnode["id"].toString(),toSide:"top",label:edgelabel, color:edgecolor}
|
newedge = {id:this.edgeid.toString(),fromNode:fromnode.toString(),fromSide:"bottom",toNode:newnode["id"].toString(),toSide:"top",label:edgelabel, color:edgecolor}
|
||||||
edges.push(newedge)
|
this.edges.push(newedge)
|
||||||
edgeid = edgeid + 1
|
this.edgeid = this.edgeid + 1
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return [newnode, jmploc]
|
return [newnode, jmploc]
|
||||||
}
|
}
|
||||||
|
|
||||||
function generateNewNode(nodeText: string, startLineNum: any, endLineNum: any, side: any){
|
generateNewNode(nodeText: string, startLineNum: any, endLineNum: any, side: any){
|
||||||
//const newnode = {"id":nodeid, "x": workingx*side, "y": workingy, "width": 550,"height": 25*nodeText.split("\n").length, "type": "text", "text": nodeText, "startline": startLineNum,"endline": endLineNum}
|
//const newnode = {"id":nodeid, "x": workingx*side, "y": workingy, "width": 550,"height": 25*nodeText.split("\n").length, "type": "text", "text": nodeText, "startline": startLineNum,"endline": endLineNum}
|
||||||
const newnode: x86Node = {
|
const newnode: x86Node = {
|
||||||
type: 'text',
|
type: 'text',
|
||||||
id: nodeid.toString(),
|
id: this.nodeid.toString(),
|
||||||
x: workingx*side,
|
x: this.workingx*side,
|
||||||
y: workingy,
|
y: this.workingy,
|
||||||
width: 550,
|
width: 550,
|
||||||
height: 35*nodeText.split("\n").length,
|
height: 35*nodeText.split("\n").length,
|
||||||
text: nodeText,
|
text: nodeText,
|
||||||
|
|
@ -275,11 +273,12 @@ function generateNewNode(nodeText: string, startLineNum: any, endLineNum: any, s
|
||||||
endline: endLineNum
|
endline: endLineNum
|
||||||
};
|
};
|
||||||
|
|
||||||
workingy = workingy + 300
|
this.workingy = this.workingy + 300
|
||||||
workingx = workingx + (50*nodeid)
|
this.workingx = this.workingx + (50*this.nodeid)
|
||||||
nodeid = nodeid + 1
|
this.nodeid = this.nodeid + 1
|
||||||
return newnode
|
return newnode
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
//Extending CanvasNodeData to add attributes for detecting repeated blocks of code
|
//Extending CanvasNodeData to add attributes for detecting repeated blocks of code
|
||||||
interface x86Node extends CanvasTextData{
|
interface x86Node extends CanvasTextData{
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue