|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Runtime.InteropServices; |
| 4 | + |
| 5 | +namespace BinaryNinja |
| 6 | +{ |
| 7 | + public partial class FlowGraph |
| 8 | + { |
| 9 | + private static readonly object externalReferenceLock = new object(); |
| 10 | + |
| 11 | + private static readonly List<FlowGraph> externallyReferencedGraphs = |
| 12 | + new List<FlowGraph>(); |
| 13 | + |
| 14 | + private NativeDelegates.BNCustomFlowGraphEvent? prepareForLayoutCallback; |
| 15 | + |
| 16 | + private NativeDelegates.BNCustomFlowGraphEvent? populateNodesCallback; |
| 17 | + |
| 18 | + private NativeDelegates.BNCustomFlowGraphEvent? completeLayoutCallback; |
| 19 | + |
| 20 | + private NativeDelegates.BNCustomFlowGraphUpdate? updateCallback; |
| 21 | + |
| 22 | + private NativeDelegates.BNCustomFlowGraphEvent? externalReferenceTakenCallback; |
| 23 | + |
| 24 | + private NativeDelegates.BNCustomFlowGraphEvent? externalReferenceReleasedCallback; |
| 25 | + |
| 26 | + /// <summary>Prepares this graph before the layout worker requests nodes.</summary> |
| 27 | + protected virtual void PrepareForLayout() |
| 28 | + { |
| 29 | + this.FinishPrepareForLayout(); |
| 30 | + } |
| 31 | + |
| 32 | + /// <summary>Populates this graph on the layout worker thread.</summary> |
| 33 | + protected virtual void PopulateNodes() |
| 34 | + { |
| 35 | + } |
| 36 | + |
| 37 | + /// <summary>Runs after the core completes graph layout.</summary> |
| 38 | + protected virtual void CompleteLayout() |
| 39 | + { |
| 40 | + } |
| 41 | + |
| 42 | + private void InitializeCustomGraph() |
| 43 | + { |
| 44 | + this.prepareForLayoutCallback = new NativeDelegates.BNCustomFlowGraphEvent( |
| 45 | + this.InvokePrepareForLayout |
| 46 | + ); |
| 47 | + this.populateNodesCallback = new NativeDelegates.BNCustomFlowGraphEvent( |
| 48 | + this.InvokePopulateNodes |
| 49 | + ); |
| 50 | + this.completeLayoutCallback = new NativeDelegates.BNCustomFlowGraphEvent( |
| 51 | + this.InvokeCompleteLayout |
| 52 | + ); |
| 53 | + this.updateCallback = new NativeDelegates.BNCustomFlowGraphUpdate( |
| 54 | + this.InvokeUpdate |
| 55 | + ); |
| 56 | + this.externalReferenceTakenCallback = |
| 57 | + new NativeDelegates.BNCustomFlowGraphEvent( |
| 58 | + this.InvokeExternalReferenceTaken |
| 59 | + ); |
| 60 | + this.externalReferenceReleasedCallback = |
| 61 | + new NativeDelegates.BNCustomFlowGraphEvent( |
| 62 | + this.InvokeExternalReferenceReleased |
| 63 | + ); |
| 64 | + |
| 65 | + BNCustomFlowGraph callbacks = new BNCustomFlowGraph(); |
| 66 | + callbacks.context = IntPtr.Zero; |
| 67 | + callbacks.prepareForLayout = Marshal.GetFunctionPointerForDelegate( |
| 68 | + this.prepareForLayoutCallback |
| 69 | + ); |
| 70 | + callbacks.populateNodes = Marshal.GetFunctionPointerForDelegate( |
| 71 | + this.populateNodesCallback |
| 72 | + ); |
| 73 | + callbacks.completeLayout = Marshal.GetFunctionPointerForDelegate( |
| 74 | + this.completeLayoutCallback |
| 75 | + ); |
| 76 | + callbacks.update = Marshal.GetFunctionPointerForDelegate( |
| 77 | + this.updateCallback |
| 78 | + ); |
| 79 | + callbacks.freeObject = IntPtr.Zero; |
| 80 | + callbacks.externalRefTaken = Marshal.GetFunctionPointerForDelegate( |
| 81 | + this.externalReferenceTakenCallback |
| 82 | + ); |
| 83 | + callbacks.externalRefReleased = Marshal.GetFunctionPointerForDelegate( |
| 84 | + this.externalReferenceReleasedCallback |
| 85 | + ); |
| 86 | + |
| 87 | + IntPtr handle = NativeMethods.BNCreateCustomFlowGraph(in callbacks); |
| 88 | + if (IntPtr.Zero == handle) |
| 89 | + { |
| 90 | + throw new InvalidOperationException( |
| 91 | + "The core rejected the custom flow graph." |
| 92 | + ); |
| 93 | + } |
| 94 | + |
| 95 | + this.SetHandle(handle); |
| 96 | + } |
| 97 | + |
| 98 | + private void InvokePrepareForLayout(IntPtr context) |
| 99 | + { |
| 100 | + try |
| 101 | + { |
| 102 | + this.PrepareForLayout(); |
| 103 | + } |
| 104 | + catch (Exception exception) |
| 105 | + { |
| 106 | + Core.LogError( |
| 107 | + "Unhandled exception in FlowGraph.PrepareForLayout: {0}", |
| 108 | + exception |
| 109 | + ); |
| 110 | + this.FinishPrepareForLayout(); |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + private void InvokePopulateNodes(IntPtr context) |
| 115 | + { |
| 116 | + try |
| 117 | + { |
| 118 | + this.PopulateNodes(); |
| 119 | + } |
| 120 | + catch (Exception exception) |
| 121 | + { |
| 122 | + Core.LogError( |
| 123 | + "Unhandled exception in FlowGraph.PopulateNodes: {0}", |
| 124 | + exception |
| 125 | + ); |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + private void InvokeCompleteLayout(IntPtr context) |
| 130 | + { |
| 131 | + try |
| 132 | + { |
| 133 | + this.CompleteLayout(); |
| 134 | + } |
| 135 | + catch (Exception exception) |
| 136 | + { |
| 137 | + Core.LogError( |
| 138 | + "Unhandled exception in FlowGraph.CompleteLayout: {0}", |
| 139 | + exception |
| 140 | + ); |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + private IntPtr InvokeUpdate(IntPtr context) |
| 145 | + { |
| 146 | + try |
| 147 | + { |
| 148 | + FlowGraph? graph = this.Update(); |
| 149 | + if (null == graph) |
| 150 | + { |
| 151 | + return IntPtr.Zero; |
| 152 | + } |
| 153 | + |
| 154 | + return NativeMethods.BNNewFlowGraphReference( |
| 155 | + graph.DangerousGetHandle() |
| 156 | + ); |
| 157 | + } |
| 158 | + catch (Exception exception) |
| 159 | + { |
| 160 | + Core.LogError( |
| 161 | + "Unhandled exception in FlowGraph.Update: {0}", |
| 162 | + exception |
| 163 | + ); |
| 164 | + return IntPtr.Zero; |
| 165 | + } |
| 166 | + } |
| 167 | + |
| 168 | + private void InvokeExternalReferenceTaken(IntPtr context) |
| 169 | + { |
| 170 | + lock (FlowGraph.externalReferenceLock) |
| 171 | + { |
| 172 | + FlowGraph.externallyReferencedGraphs.Add(this); |
| 173 | + } |
| 174 | + } |
| 175 | + |
| 176 | + private void InvokeExternalReferenceReleased(IntPtr context) |
| 177 | + { |
| 178 | + lock (FlowGraph.externalReferenceLock) |
| 179 | + { |
| 180 | + FlowGraph.externallyReferencedGraphs.Remove(this); |
| 181 | + } |
| 182 | + } |
| 183 | + |
| 184 | + private sealed class CoreFlowGraph : FlowGraph |
| 185 | + { |
| 186 | + internal CoreFlowGraph(IntPtr handle, bool owner) |
| 187 | + : base(handle, owner) |
| 188 | + { |
| 189 | + } |
| 190 | + |
| 191 | + public override bool HasUpdates |
| 192 | + { |
| 193 | + get |
| 194 | + { |
| 195 | + if (!this.UpdateQueryMode) |
| 196 | + { |
| 197 | + return false; |
| 198 | + } |
| 199 | + |
| 200 | + return NativeMethods.BNFlowGraphHasUpdates(this.handle); |
| 201 | + } |
| 202 | + } |
| 203 | + |
| 204 | + public override FlowGraph? Update() |
| 205 | + { |
| 206 | + return FlowGraph.TakeHandle( |
| 207 | + NativeMethods.BNUpdateFlowGraph(this.handle) |
| 208 | + ); |
| 209 | + } |
| 210 | + } |
| 211 | + } |
| 212 | +} |
0 commit comments