Inheritance: EpForceDirectedGraph.cs.AbstractRenderer
示例#1
0
    // Method for loading the JSON data from the API controller
    private IEnumerator LoadLayout()
    {
        graph = new Graph();

        statusText.text = "Loading radia.json...";

        string rawJson = null;

        //var req = new WWW(apiUrl + "/functions");
        string path_prefix;

        if (SystemInfo.operatingSystem.StartsWith("Win"))
        {
            path_prefix = "\\";
        }
        else
        {
            path_prefix = "/../../";
        }

        //local app path for finding the JSON files
        var req = new WWW("file://" + Application.dataPath + path_prefix + "radia.json");

        yield return(req);

        if (req.error != null)
        {
            statusText.text = "Error reading radia.json";
            return(false);
        }
        rawJson = req.text;

        statusText.text = "Processing Data";

        var j = JSON.Parse(rawJson);

        j = j["functions"];

        for (int i = 0; i < j.Count; i++)
        {
            float x = 0.0f;
            float y = 0.0f;
            float z = 0.0f;

            int category = 0;
            if (j[i]["category"] != null)
            {
                category = int.Parse(j[i]["category"]);
            }

            // (danger << 6) + (string << 5) + (fileio << 4) + (crypto << 3) + (socket << 2) + (heap << 1) + system
            //  64              32              16              8                4              2             1
            Function nodeObject;
            float    scale = 1.0f;
            if ((category & 64) == 64)
            {
                nodeObject = Instantiate(dangerPrefab, new Vector3(x, y, z), Quaternion.identity) as Function;
            }
            else if ((category & 8) == 8)
            {
                nodeObject = Instantiate(cryptoPrefab, new Vector3(x, y, z), Quaternion.identity) as Function;
            }
            else if ((category & 4) == 4)
            {
                nodeObject = Instantiate(socketPrefab, new Vector3(x, y, z), Quaternion.identity) as Function;
            }
            else if ((category & 32) == 32)
            {
                nodeObject = Instantiate(stringPrefab, new Vector3(x, y, z), Quaternion.identity) as Function;
            }
            else if ((category & 16) == 16)
            {
                nodeObject = Instantiate(filePrefab, new Vector3(x, y, z), Quaternion.identity) as Function;
                scale      = 1.5f;
            }
            else if ((category & 1) == 1)
            {
                nodeObject = Instantiate(systemPrefab, new Vector3(x, y, z), Quaternion.identity) as Function;
            }
            else if ((category & 2) == 2)
            {
                nodeObject = Instantiate(heapPrefab, new Vector3(x, y, z), Quaternion.identity) as Function;
                scale      = 2.0f;
            }
            else
            {
                nodeObject = Instantiate(defaultPrefab, new Vector3(x, y, z), Quaternion.identity) as Function;
            }

            nodeObject.funcname   = j[i]["name"];
            nodeObject.address    = ulong.Parse(j[i]["address"]);
            nodeObject.attributes = category;
            if (j[i]["size"] != null)
            {
                nodeObject.size = int.Parse(j[i]["size"]);
            }
            else
            {
                nodeObject.size = 0;
            }
            nodeObject.module_name   = j[i]["module_name"];
            nodeObject.functag       = j[i]["tag"];
            nodeObject.comment       = j[i]["comment"];
            nodeObject.longname      = j[i]["long_name"];
            nodeObject.basic_blk_cnt = int.Parse(j[i]["basic_blk_cnt"]);

            if (j[i]["dangerous_list"] != null)
            {
                nodeObject.dangerous_calls = new string[j[i]["dangerous_list"].Count];
                for (int c = 0; c < j[i]["dangerous_list"].Count; c++)
                {
                    nodeObject.dangerous_calls[c] = j[i]["dangerous_list"][c];
                }
            }

            if (j[i]["strings"] != null)
            {
                nodeObject.strings = new string[j[i]["strings"].Count];
                for (int c = 0; c < j[i]["strings"].Count; c++)
                {
                    nodeObject.strings[c] = j[i]["strings"][c];
                }
            }

            nodeObject.transform.localScale += new Vector3(scale, scale, scale);
            nodes.Add(nodeObject.address, nodeObject);

            // For force directed graph
            NodeData data = new NodeData();
            data.label = nodeObject.address.ToString();
            data.mass  = (float)nodeObject.size / 50.0f + 10.0f;
            graph.CreateNode(data);

            statusText.text = "Loading Functions: Function " + nodeObject.funcname;

            if (i % 100 == 0)
            {
                yield return(true);
            }
        }

        j = JSON.Parse(rawJson);
        j = j["callgraph"];


        for (int i = 0; i < j.Count; i++)
        {
            ulong srcid = ulong.Parse(j[i]["source"]);
            ulong dstid = ulong.Parse(j[i]["target"]);

            if (FindDupLink(srcid, dstid))
            {
                continue;
            }

            Link linkObject = Instantiate(linkPrefab, new Vector3(0, 0, 0), Quaternion.identity) as Link;
            linkObject.id       = i + 1;
            linkObject.sourceId = srcid;
            linkObject.targetId = dstid;
            links.Add(linkObject.id, linkObject);

            // For force directed graph
            Node     node1 = graph.GetNode(linkObject.sourceId.ToString());
            Node     node2 = graph.GetNode(linkObject.targetId.ToString());
            EdgeData data  = new EdgeData();
            data.label  = linkObject.sourceId.ToString() + "-" + linkObject.targetId.ToString();
            data.length = 1.0f;
            graph.CreateEdge(node1, node2, data);

            statusText.text = "Loading Callgraph: Call " + linkObject.id.ToString();

            if (i % 100 == 0)
            {
                yield return(true);
            }
        }

        // Map node edges
        MapLinkFunctions();

        // For force directed graph
        physics = new ForceDirected3D(graph,         // instance of Graph
                                      stiffness,     // stiffness of the spring
                                      repulsion,     // node repulsion rate
                                      damping        // damping rate
                                      );
        render = new FDRenderer(physics);
        render.setController(this);

        statusText.text = "";

        Camera.main.transform.LookAt(new Vector3(0f, 0f, 0f));

        renderThread = new Thread(new ThreadStart(FDRenderThread));
        renderThread.Start();
    }
示例#2
0
    // Method for loading the JSON data from the API controller
    private IEnumerator LoadLayout()
    {
        graph = new Graph();

        statusText.text = "Loading radia.json...";

        string rawJson = null;

        //var req = new WWW(apiUrl + "/functions");
        string path_prefix;
        if (SystemInfo.operatingSystem.StartsWith ("Win")) {
            path_prefix = "\\";
        } else {
            path_prefix = "/../../";
        }

        //local app path for finding the JSON files
        var req = new WWW ("file://" + Application.dataPath + path_prefix + "radia.json");
        yield return req;
        if (req.error != null) {
            statusText.text = "Error reading radia.json";
            return false;
        }
        rawJson = req.text;

        statusText.text = "Processing Data";

        var j = JSON.Parse(rawJson);
        j = j["functions"];

        for(int i = 0; i < j.Count; i++) {
            float x = 0.0f;
            float y = 0.0f;
            float z = 0.0f;

            int category = 0;
            if (j[i]["category"] != null) {
                category = int.Parse(j[i]["category"]);
            }

            // (danger << 6) + (string << 5) + (fileio << 4) + (crypto << 3) + (socket << 2) + (heap << 1) + system
            //  64              32              16              8                4              2             1
            Function nodeObject;
            float scale = 1.0f;
            if ((category & 64) == 64) {
                nodeObject = Instantiate(dangerPrefab, new Vector3(x, y, z), Quaternion.identity) as Function;
            }
            else if ((category & 8) == 8) {
                nodeObject = Instantiate(cryptoPrefab, new Vector3(x, y, z), Quaternion.identity) as Function;
            }
            else if ((category & 4) == 4) {
                nodeObject = Instantiate(socketPrefab, new Vector3(x, y, z), Quaternion.identity) as Function;
            }
            else if ((category & 32) == 32) {
                nodeObject = Instantiate(stringPrefab, new Vector3(x, y, z), Quaternion.identity) as Function;
            }
            else if ((category & 16) == 16) {
                nodeObject = Instantiate(filePrefab, new Vector3(x, y, z), Quaternion.identity) as Function;
                scale = 1.5f;
            }
            else if ((category & 1) == 1) {
                nodeObject = Instantiate(systemPrefab, new Vector3(x, y, z), Quaternion.identity) as Function;
            }
            else if ((category & 2) == 2) {
                nodeObject = Instantiate(heapPrefab, new Vector3(x, y, z), Quaternion.identity) as Function;
                scale = 2.0f;
            } else {
                nodeObject = Instantiate(defaultPrefab, new Vector3(x, y, z), Quaternion.identity) as Function;
            }

            nodeObject.funcname = j[i]["name"];
            nodeObject.address = ulong.Parse(j[i]["address"]);
            nodeObject.attributes = category;
            if (j[i]["size"] != null) {
                nodeObject.size = int.Parse(j[i]["size"]);
            } else {
                nodeObject.size = 0;
            }
            nodeObject.module_name   = j[i]["module_name"];
            nodeObject.functag		 = j[i]["tag"];
            nodeObject.comment		 = j[i]["comment"];
            nodeObject.longname		 = j[i]["long_name"];
            nodeObject.basic_blk_cnt = int.Parse(j[i]["basic_blk_cnt"]);

            if (j[i]["dangerous_list"] != null) {
                nodeObject.dangerous_calls = new string[j[i]["dangerous_list"].Count];
                for (int c = 0; c < j[i]["dangerous_list"].Count; c++) {
                    nodeObject.dangerous_calls[c] = j[i]["dangerous_list"][c];
                }
            }

            if (j[i]["strings"] != null) {
                nodeObject.strings = new string[j[i]["strings"].Count];
                for (int c = 0; c < j[i]["strings"].Count; c++) {
                    nodeObject.strings[c] = j[i]["strings"][c];
                }
            }

            nodeObject.transform.localScale += new Vector3(scale, scale, scale);
            nodes.Add(nodeObject.address, nodeObject);

            // For force directed graph
            NodeData data = new NodeData();
            data.label = nodeObject.address.ToString();
            data.mass = (float)nodeObject.size / 50.0f + 10.0f;
            graph.CreateNode(data);

            statusText.text = "Loading Functions: Function " + nodeObject.funcname;

            if(i % 100 == 0)
                yield return true;
        }

        j = JSON.Parse(rawJson);
        j = j["callgraph"];

        for(int i = 0; i < j.Count; i++) {
            ulong srcid = ulong.Parse(j[i]["source"]);
            ulong dstid = ulong.Parse(j[i]["target"]);

            if (FindDupLink (srcid, dstid)) {
                continue;
            }

            Link linkObject = Instantiate(linkPrefab, new Vector3(0, 0, 0), Quaternion.identity) as Link;
            linkObject.id       = i+1;
            linkObject.sourceId = srcid;
            linkObject.targetId = dstid;
            links.Add(linkObject.id, linkObject);

            // For force directed graph
            Node node1 = graph.GetNode(linkObject.sourceId.ToString());
            Node node2 = graph.GetNode(linkObject.targetId.ToString());
            EdgeData data = new EdgeData();
            data.label = linkObject.sourceId.ToString()+"-"+linkObject.targetId.ToString();
            data.length = 1.0f;
            graph.CreateEdge(node1, node2, data);

            statusText.text = "Loading Callgraph: Call " + linkObject.id.ToString();

            if(i % 100 == 0)
                yield return true;
        }

        // Map node edges
        MapLinkFunctions();

        // For force directed graph
        physics = new ForceDirected3D(graph, // instance of Graph
                                      stiffness, // stiffness of the spring
                                      repulsion, // node repulsion rate
                                      damping    // damping rate
                                     );
        render = new FDRenderer(physics);
        render.setController(this);

        statusText.text = "";

        Camera.main.transform.LookAt (new Vector3 (0f, 0f, 0f));

        renderThread = new Thread(new ThreadStart(FDRenderThread));
        renderThread.Start ();
    }