-
Notifications
You must be signed in to change notification settings - Fork 280
Expand file tree
/
Copy pathlib.rs
More file actions
149 lines (131 loc) · 5.72 KB
/
lib.rs
File metadata and controls
149 lines (131 loc) · 5.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
use binaryninja::flowgraph::edge::Point;
use binaryninja::flowgraph::layout::{register_flowgraph_layout, FlowGraphLayout};
use binaryninja::flowgraph::{FlowGraph, FlowGraphNode};
use binaryninja::rc::Ref;
use std::collections::HashMap;
pub struct StableGraphBuilder;
impl StableGraphBuilder {
pub fn new() -> Self {
Self {}
}
pub fn build(
self,
nodes: &[FlowGraphNode],
) -> petgraph::stable_graph::StableDiGraph<Ref<FlowGraphNode>, ()> {
let mut graph = petgraph::stable_graph::StableDiGraph::<Ref<FlowGraphNode>, ()>::new();
let mut node_idx_map = HashMap::<Ref<FlowGraphNode>, petgraph::graph::NodeIndex>::new();
for node in nodes {
let owned_node = node.to_owned();
node_idx_map.insert(owned_node.clone(), graph.add_node(owned_node));
}
for node in nodes {
let node_idx = node_idx_map.get(node).unwrap();
for edge in &node.outgoing_edges() {
let target_node_idx = node_idx_map.get(&edge.target).unwrap();
graph.add_edge(*node_idx, *target_node_idx, ());
}
}
graph
}
}
struct SugiyamaLayout;
impl FlowGraphLayout for SugiyamaLayout {
fn layout(&self, graph: &FlowGraph, nodes: &[FlowGraphNode]) -> bool {
let mut config = rust_sugiyama::configure::Config::default();
config.vertex_spacing = 5.0;
let vertex_size = |_, node: &Ref<FlowGraphNode>| {
let (width, height) = node.size();
(width as f64 * 1.2, height as f64)
};
let pet_graph = StableGraphBuilder::new().build(nodes);
let layouts = rust_sugiyama::from_graph(&pet_graph, &vertex_size, &config);
// Position graph nodes
for (nodes, _, _) in &layouts {
for (node_idx, (x, y)) in nodes {
let node = pet_graph.node_weight(*node_idx).unwrap();
node.set_position(*x as i32, *y as i32);
}
}
// Add edges to graph nodes
for (nodes, _, _) in &layouts {
for (node_idx, (x, y)) in nodes {
let node = pet_graph.node_weight(*node_idx).unwrap();
let (width, height) = node.size();
for (edge_idx, edge) in node.outgoing_edges().iter().enumerate() {
let from_point_x = x + (width as f64 / 2.0);
let from_point_y = y + height as f64;
let from_point = Point {
x: from_point_x as f32,
y: from_point_y as f32,
};
let (target_node_x, target_node_y) = edge.target.position();
let (target_node_width, _) = edge.target.size();
let to_point_x = target_node_x as f64 + (target_node_width as f64 / 2.0);
let to_point_y = target_node_y;
let to_point = Point {
x: to_point_x as f32,
y: to_point_y as f32,
};
// NOTE: This does not do proper routing, this will add edge points from the outgoing node
// to the target node, this will lead to lines overlapping nodes and other rendering oddities.
// The reason we do not do proper routing is because that is quite a bit more code with some
// dependence on a navigation algorithm like a-star.
node.set_outgoing_edge_points(edge_idx, &[from_point, to_point]);
}
}
}
// Calculate graph size and node visibility
let mut min_x = f32::MAX;
let mut min_y = f32::MAX;
let mut max_x = f32::MIN;
let mut max_y = f32::MIN;
for node in nodes {
let (node_x, node_y) = node.position();
let (node_width, node_height) = node.size();
// Initialize per-node bounds based on the node's current box
let mut min_node_x = node_x;
let mut max_node_x = node_x + node_width;
let mut min_node_y = node_y;
let mut max_node_y = node_y + node_height;
for edge in &node.outgoing_edges() {
for point in &edge.points {
let px = point.x;
let py = point.y;
// Update Global Graph Bounds
min_x = min_x.min(px);
min_y = min_y.min(py);
max_x = max_x.max(px + 1.0);
max_y = max_y.max(py + 1.0);
// Update Node Visibility Bounds
min_node_x = min_node_x.min(px as i32);
max_node_x = max_node_x.max(px as i32 + 1);
min_node_y = min_node_y.min(py as i32);
max_node_y = max_node_y.max(py as i32 + 1);
}
}
node.set_visibility_region(
min_node_x,
min_node_y,
(max_node_x - min_node_x),
(max_node_y - min_node_y),
);
}
// Set final graph dimensions
if min_x != f32::MAX {
let (horiz_node_margin, vert_node_margin) = graph.node_margins();
let final_graph_width = (max_x - min_x) as i32 + horiz_node_margin * 2;
let final_graph_height = (max_y - min_y) as i32 + vert_node_margin * 2;
graph.set_size(final_graph_width, final_graph_height);
}
true
}
}
/// # Safety
/// This function is called from Binary Ninja once to initialize the plugin.
#[allow(non_snake_case)]
#[unsafe(no_mangle)]
pub unsafe extern "C" fn CorePluginInit() -> bool {
// Register flow graph layout
register_flowgraph_layout("Sugiyama", SugiyamaLayout);
true
}