2019-11-23 04:45:57 +00:00
|
|
|
|
/*
|
|
|
|
|
* Copyright 2019 faddenSoft
|
|
|
|
|
*
|
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
|
*
|
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
*
|
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
|
* limitations under the License.
|
|
|
|
|
*/
|
|
|
|
|
using System;
|
2019-11-25 22:27:38 +00:00
|
|
|
|
using System.Collections;
|
2019-11-23 04:45:57 +00:00
|
|
|
|
using System.Collections.Generic;
|
2019-12-04 23:50:19 +00:00
|
|
|
|
using System.Collections.ObjectModel;
|
2019-12-03 22:34:45 +00:00
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
|
|
|
|
|
using PluginCommon;
|
2019-11-23 04:45:57 +00:00
|
|
|
|
|
|
|
|
|
namespace SourceGen {
|
2019-11-27 02:54:42 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Ordered list of visualization objects.
|
|
|
|
|
/// </summary>
|
2019-12-03 00:38:32 +00:00
|
|
|
|
/// <remarks>
|
2019-12-03 22:34:45 +00:00
|
|
|
|
/// There's not much separating this from a plain List<>, except perhaps the operator== stuff.
|
2019-12-03 00:38:32 +00:00
|
|
|
|
/// </remarks>
|
2019-11-25 22:27:38 +00:00
|
|
|
|
public class VisualizationSet : IEnumerable<Visualization> {
|
|
|
|
|
/// <summary>
|
2019-11-27 02:54:42 +00:00
|
|
|
|
/// Object list.
|
2019-11-25 22:27:38 +00:00
|
|
|
|
/// </summary>
|
|
|
|
|
private List<Visualization> mList;
|
2019-11-23 04:45:57 +00:00
|
|
|
|
|
2019-11-25 22:27:38 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Constructor.
|
|
|
|
|
/// </summary>
|
2019-12-03 00:38:32 +00:00
|
|
|
|
/// <param name="initialCap">Initial capacity.</param>
|
|
|
|
|
public VisualizationSet(int initialCap = 1) {
|
|
|
|
|
mList = new List<Visualization>(initialCap);
|
2019-11-25 22:27:38 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// IEnumerable
|
|
|
|
|
public IEnumerator<Visualization> GetEnumerator() {
|
|
|
|
|
return mList.GetEnumerator();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// IEnumerable
|
|
|
|
|
IEnumerator IEnumerable.GetEnumerator() {
|
|
|
|
|
return mList.GetEnumerator();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The number of entries in the table.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public int Count {
|
|
|
|
|
get { return mList.Count; }
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-03 00:38:32 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Accesses the Nth element.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="key">Element number.</param>
|
|
|
|
|
public Visualization this[int key] {
|
|
|
|
|
get {
|
|
|
|
|
return mList[key];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-25 22:27:38 +00:00
|
|
|
|
public void Add(Visualization vis) {
|
|
|
|
|
mList.Add(vis);
|
2019-11-23 04:45:57 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-12-03 00:38:32 +00:00
|
|
|
|
public void Remove(Visualization vis) {
|
|
|
|
|
mList.Remove(vis);
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-03 22:34:45 +00:00
|
|
|
|
public Visualization[] ToArray() {
|
|
|
|
|
Visualization[] arr = new Visualization[mList.Count];
|
|
|
|
|
for (int i = 0; i < mList.Count; i++) {
|
|
|
|
|
arr[i] = mList[i];
|
|
|
|
|
}
|
|
|
|
|
return arr;
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-22 02:04:44 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Finds a Visualization by serial number.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="visSets">List of sets of visualizations.</param>
|
|
|
|
|
/// <param name="serial">Serial number to search for.</param>
|
|
|
|
|
/// <returns>Matching Visualization, or null if not found.</returns>
|
|
|
|
|
public static Visualization FindVisualizationBySerial(
|
|
|
|
|
SortedList<int, VisualizationSet> visSets, int serial) {
|
|
|
|
|
foreach (KeyValuePair<int, VisualizationSet> kvp in visSets) {
|
|
|
|
|
VisualizationSet visSet = kvp.Value;
|
|
|
|
|
foreach (Visualization vis in visSet) {
|
|
|
|
|
if (vis.SerialNumber == serial) {
|
|
|
|
|
return vis;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Strips a set of Visualizations out of animations in the set. The set is not
|
|
|
|
|
/// modified; rather, a new set is generated with updated entries.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="visSet">Input set.</param>
|
|
|
|
|
/// <param name="removedSerials">Serial numbers of removed items.</param>
|
|
|
|
|
/// <param name="newSet">Updated set.</param>
|
|
|
|
|
/// <returns>True if entries were removed.</returns>
|
|
|
|
|
public static bool StripEntriesFromAnimations(VisualizationSet visSet,
|
|
|
|
|
List<int> removedSerials, out VisualizationSet newSet) {
|
|
|
|
|
bool somethingRemoved = false;
|
|
|
|
|
newSet = new VisualizationSet(visSet.Count);
|
|
|
|
|
foreach (Visualization vis in visSet) {
|
2020-03-08 00:52:36 +00:00
|
|
|
|
if (!(vis is VisBitmapAnimation)) {
|
2019-12-22 02:04:44 +00:00
|
|
|
|
newSet.Add(vis);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-08 00:52:36 +00:00
|
|
|
|
if (VisBitmapAnimation.StripEntries((VisBitmapAnimation) vis,
|
|
|
|
|
removedSerials, out VisBitmapAnimation newAnim)) {
|
2019-12-22 02:04:44 +00:00
|
|
|
|
somethingRemoved = true;
|
2019-12-23 00:56:57 +00:00
|
|
|
|
if (newAnim.Count != 0) {
|
2019-12-22 02:04:44 +00:00
|
|
|
|
newSet.Add(newAnim);
|
|
|
|
|
} else {
|
|
|
|
|
Debug.WriteLine("Deleting empty animation " + vis.Tag);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return somethingRemoved;
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-03 22:34:45 +00:00
|
|
|
|
#region Image generation
|
|
|
|
|
|
|
|
|
|
private class ScriptSupport : MarshalByRefObject, PluginCommon.IApplication {
|
2020-03-11 22:20:03 +00:00
|
|
|
|
public string MsgTag { get; set; } = string.Empty;
|
|
|
|
|
|
2019-12-03 22:34:45 +00:00
|
|
|
|
public ScriptSupport() { }
|
|
|
|
|
|
2019-12-05 18:21:07 +00:00
|
|
|
|
public void ReportError(string msg) {
|
|
|
|
|
DebugLog(msg);
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-03 22:34:45 +00:00
|
|
|
|
public void DebugLog(string msg) {
|
2020-03-11 22:20:03 +00:00
|
|
|
|
Debug.WriteLine("Vis [" + MsgTag + "]: " + msg);
|
2019-12-03 22:34:45 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool SetOperandFormat(int offset, DataSubType subType, string label) {
|
|
|
|
|
throw new InvalidOperationException();
|
|
|
|
|
}
|
|
|
|
|
public bool SetInlineDataFormat(int offset, int length, DataType type,
|
|
|
|
|
DataSubType subType, string label) {
|
|
|
|
|
throw new InvalidOperationException();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Informs all list elements that a refresh is needed. Call this when the set of active
|
|
|
|
|
/// plugins changes. The actual refresh will happen later.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public void RefreshNeeded() {
|
|
|
|
|
foreach (Visualization vis in mList) {
|
|
|
|
|
vis.SetThumbnail(null);
|
2020-03-15 18:49:11 +00:00
|
|
|
|
vis.SetThumbnail(null, null);
|
2019-12-03 22:34:45 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Attempts to refresh broken thumbnails across all visualization sets in the project.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="project">Project reference.</param>
|
|
|
|
|
public static void RefreshAllThumbnails(DisasmProject project) {
|
|
|
|
|
ScriptSupport iapp = null;
|
2019-12-28 22:00:48 +00:00
|
|
|
|
Dictionary<string, IPlugin> plugins = null;
|
2019-12-03 22:34:45 +00:00
|
|
|
|
|
2019-12-22 02:04:44 +00:00
|
|
|
|
SortedList<int, VisualizationSet> visSets = project.VisualizationSets;
|
|
|
|
|
|
|
|
|
|
foreach (KeyValuePair<int, VisualizationSet> kvp in visSets) {
|
2019-12-03 22:34:45 +00:00
|
|
|
|
VisualizationSet visSet = kvp.Value;
|
|
|
|
|
foreach (Visualization vis in visSet) {
|
2019-12-22 02:04:44 +00:00
|
|
|
|
if (vis.HasImage) {
|
2019-12-03 22:34:45 +00:00
|
|
|
|
continue;
|
|
|
|
|
}
|
2019-12-18 00:40:27 +00:00
|
|
|
|
//Debug.WriteLine("Vis needs refresh: " + vis.Tag);
|
2019-12-03 22:34:45 +00:00
|
|
|
|
|
2020-03-08 00:52:36 +00:00
|
|
|
|
if (vis is VisBitmapAnimation) {
|
2019-12-22 02:04:44 +00:00
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-05 18:21:07 +00:00
|
|
|
|
if (iapp == null) {
|
|
|
|
|
// Prep the plugins on first need.
|
|
|
|
|
iapp = new ScriptSupport();
|
|
|
|
|
project.PrepareScripts(iapp);
|
|
|
|
|
}
|
2020-03-11 22:20:03 +00:00
|
|
|
|
iapp.MsgTag = vis.Tag;
|
2019-12-05 18:21:07 +00:00
|
|
|
|
|
2019-12-03 22:34:45 +00:00
|
|
|
|
if (plugins == null) {
|
|
|
|
|
plugins = project.GetActivePlugins();
|
|
|
|
|
}
|
|
|
|
|
IPlugin_Visualizer vplug = FindPluginByVisGenIdent(plugins,
|
|
|
|
|
vis.VisGenIdent, out VisDescr visDescr);
|
|
|
|
|
if (vplug == null) {
|
2019-12-05 18:21:07 +00:00
|
|
|
|
Debug.WriteLine("Unable to refresh " + vis.Tag + ": plugin not found");
|
2019-12-03 22:34:45 +00:00
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-03 01:12:59 +00:00
|
|
|
|
IVisualization2d vis2d = null;
|
|
|
|
|
IVisualizationWireframe visWire = null;
|
|
|
|
|
ReadOnlyDictionary<string, object> parms =
|
|
|
|
|
new ReadOnlyDictionary<string, object>(vis.VisGenParams);
|
2019-12-03 22:34:45 +00:00
|
|
|
|
try {
|
2020-03-03 01:12:59 +00:00
|
|
|
|
if (visDescr.VisualizationType == VisDescr.VisType.Bitmap) {
|
|
|
|
|
vis2d = vplug.Generate2d(visDescr, parms);
|
|
|
|
|
if (vis2d == null) {
|
|
|
|
|
Debug.WriteLine("Vis2d generator returned null");
|
|
|
|
|
}
|
|
|
|
|
} else if (visDescr.VisualizationType == VisDescr.VisType.Wireframe) {
|
|
|
|
|
IPlugin_Visualizer_v2 plugin2 = (IPlugin_Visualizer_v2)vplug;
|
|
|
|
|
visWire = plugin2.GenerateWireframe(visDescr, parms);
|
|
|
|
|
if (visWire == null) {
|
|
|
|
|
Debug.WriteLine("VisWire generator returned null");
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
Debug.Assert(false);
|
2019-12-03 22:34:45 +00:00
|
|
|
|
}
|
|
|
|
|
} catch (Exception ex) {
|
|
|
|
|
Debug.WriteLine("Vis generation failed: " + ex);
|
|
|
|
|
}
|
|
|
|
|
if (vis2d != null) {
|
2019-12-18 00:40:27 +00:00
|
|
|
|
//Debug.WriteLine(" Rendered thumbnail: " + vis.Tag);
|
2019-12-03 22:34:45 +00:00
|
|
|
|
vis.SetThumbnail(vis2d);
|
2020-03-03 01:12:59 +00:00
|
|
|
|
} else if (visWire != null) {
|
|
|
|
|
vis.SetThumbnail(visWire, parms);
|
2019-12-03 22:34:45 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (iapp != null) {
|
|
|
|
|
project.UnprepareScripts();
|
|
|
|
|
}
|
2019-12-22 02:04:44 +00:00
|
|
|
|
|
|
|
|
|
// Now that we've generated images for the Visualizations, update any
|
2020-03-08 00:52:36 +00:00
|
|
|
|
// VisBitmapAnimation thumbnails that may have been affected.
|
2019-12-22 02:04:44 +00:00
|
|
|
|
foreach (KeyValuePair<int, VisualizationSet> kvp in visSets) {
|
|
|
|
|
VisualizationSet visSet = kvp.Value;
|
|
|
|
|
foreach (Visualization vis in visSet) {
|
2020-03-08 00:52:36 +00:00
|
|
|
|
if (!(vis is VisBitmapAnimation)) {
|
2019-12-22 02:04:44 +00:00
|
|
|
|
continue;
|
|
|
|
|
}
|
2020-03-08 00:52:36 +00:00
|
|
|
|
VisBitmapAnimation visAnim = (VisBitmapAnimation)vis;
|
2019-12-22 02:04:44 +00:00
|
|
|
|
visAnim.GenerateImage(visSets);
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-12-03 22:34:45 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Finds a plugin that provides the named visualization generator.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="plugins">List of plugins, from project ScriptManager.</param>
|
|
|
|
|
/// <param name="visGenIdent">Visualization generator identifier.</param>
|
|
|
|
|
/// <returns>A plugin that matches, or null if none found.</returns>
|
2019-12-28 22:00:48 +00:00
|
|
|
|
private static IPlugin_Visualizer FindPluginByVisGenIdent(
|
|
|
|
|
Dictionary<string, IPlugin> plugins, string visGenIdent, out VisDescr visDescr) {
|
|
|
|
|
foreach (IPlugin chkPlug in plugins.Values) {
|
2019-12-03 22:34:45 +00:00
|
|
|
|
if (!(chkPlug is IPlugin_Visualizer)) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
IPlugin_Visualizer vplug = (IPlugin_Visualizer)chkPlug;
|
|
|
|
|
foreach (VisDescr descr in vplug.GetVisGenDescrs()) {
|
|
|
|
|
if (descr.Ident == visGenIdent) {
|
|
|
|
|
visDescr = descr;
|
|
|
|
|
return vplug;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
visDescr = null;
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion Image generation
|
|
|
|
|
|
2019-11-23 04:45:57 +00:00
|
|
|
|
|
|
|
|
|
public override string ToString() {
|
2019-11-25 22:27:38 +00:00
|
|
|
|
return "[VS: " + mList.Count + " items]";
|
2019-11-23 04:45:57 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static bool operator ==(VisualizationSet a, VisualizationSet b) {
|
|
|
|
|
if (ReferenceEquals(a, b)) {
|
|
|
|
|
return true; // same object, or both null
|
|
|
|
|
}
|
|
|
|
|
if (ReferenceEquals(a, null) || ReferenceEquals(b, null)) {
|
|
|
|
|
return false; // one is null
|
|
|
|
|
}
|
|
|
|
|
// All fields must be equal.
|
2019-11-25 22:27:38 +00:00
|
|
|
|
if (a.mList.Count != b.mList.Count) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2019-12-22 02:04:44 +00:00
|
|
|
|
// Order matters. Use Equals() rather than == to get polymorphism.
|
2019-11-25 22:27:38 +00:00
|
|
|
|
for (int i = 0; i < a.mList.Count; i++) {
|
2019-12-22 02:04:44 +00:00
|
|
|
|
if (!a.mList[i].Equals(b.mList[i])) {
|
2019-11-25 22:27:38 +00:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return true;
|
2019-11-23 04:45:57 +00:00
|
|
|
|
}
|
|
|
|
|
public static bool operator !=(VisualizationSet a, VisualizationSet b) {
|
|
|
|
|
return !(a == b);
|
|
|
|
|
}
|
|
|
|
|
public override bool Equals(object obj) {
|
|
|
|
|
return obj is VisualizationSet && this == (VisualizationSet)obj;
|
|
|
|
|
}
|
|
|
|
|
public override int GetHashCode() {
|
2019-11-25 22:27:38 +00:00
|
|
|
|
int hashCode = 0;
|
|
|
|
|
foreach (Visualization vis in mList) {
|
|
|
|
|
hashCode ^= vis.GetHashCode();
|
|
|
|
|
}
|
|
|
|
|
return hashCode;
|
2019-11-23 04:45:57 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|