Skip to content

Commit

Permalink
Fixed some annoying runtime errors in the ILSpy plugin.
Browse files Browse the repository at this point in the history
  • Loading branch information
charlesbetros committed Feb 16, 2016
1 parent b2f873c commit f69f4c8
Show file tree
Hide file tree
Showing 7 changed files with 122 additions and 121 deletions.
8 changes: 0 additions & 8 deletions source/Cosmos.sln
Original file line number Diff line number Diff line change
Expand Up @@ -245,8 +245,6 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Cosmos.System.Tests", "Cosm
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Cosmos.Core.Tests", "Cosmos.Core.Tests\Cosmos.Core.Tests.csproj", "{6BAD532A-7DD4-4BAC-834A-10BA6497E19D}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CosmosdotPeekPlugin", "Tools\CosmosdotPeekPlugin\CosmosdotPeekPlugin.csproj", "{52185E9D-A621-4651-825C-C2437E8AA2CD}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|x64 = Debug|x64
Expand Down Expand Up @@ -800,11 +798,6 @@ Global
{6BAD532A-7DD4-4BAC-834A-10BA6497E19D}.Release|x64.Build.0 = Debug|x86
{6BAD532A-7DD4-4BAC-834A-10BA6497E19D}.Release|x86.ActiveCfg = Debug|x86
{6BAD532A-7DD4-4BAC-834A-10BA6497E19D}.Release|x86.Build.0 = Debug|x86
{52185E9D-A621-4651-825C-C2437E8AA2CD}.Debug|x64.ActiveCfg = Debug|x86
{52185E9D-A621-4651-825C-C2437E8AA2CD}.Debug|x86.ActiveCfg = Debug|x86
{52185E9D-A621-4651-825C-C2437E8AA2CD}.Debug|x86.Build.0 = Debug|x86
{52185E9D-A621-4651-825C-C2437E8AA2CD}.Release|x64.ActiveCfg = Debug|x86
{52185E9D-A621-4651-825C-C2437E8AA2CD}.Release|x86.ActiveCfg = Debug|x86
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -902,6 +895,5 @@ Global
{85E13410-C85A-4B0C-BEE5-F9A120ECC94E} = {F104F6BC-EF8E-4408-A786-D570D7565231}
{AAA0328F-60E1-442D-B949-A13DD34BD0AD} = {056A94C1-6C75-4730-B62A-675779CD07FB}
{6BAD532A-7DD4-4BAC-834A-10BA6497E19D} = {360A9DE9-F8CF-4A38-9593-3699648DA616}
{52185E9D-A621-4651-825C-C2437E8AA2CD} = {B521E2A2-81E4-4B2D-A471-7AB550E9551E}
EndGlobalSection
EndGlobal
55 changes: 29 additions & 26 deletions source/Tools/ILSpyPlugAddIn/GenerateFieldAccessParameterEntry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Windows;
using ICSharpCode.ILSpy;
using ICSharpCode.ILSpy.TreeNodes;
using Mono.Cecil;

namespace Cosmos.ILSpyPlugs.Plugin
{
Expand All @@ -14,46 +15,48 @@ public class GenerateFieldAccessParameterEntry: BaseContextMenuEntry
{
public override bool IsVisible(TextViewContext context)
{
if (context.SelectedTreeNodes.Length != 1)
if (context?.SelectedTreeNodes != null)
{
return false;
foreach (var node in context.SelectedTreeNodes)
{
var xCurrentField = node as FieldTreeNode;
if ((xCurrentField != null) && !xCurrentField.FieldDefinition.HasConstant)
{
return true;
}
}
}
var xCurrentField = context.SelectedTreeNodes[0] as FieldTreeNode;
if (xCurrentField == null)
{
return false;
}
if (xCurrentField.FieldDefinition.HasConstant)
{
return false;
}
return true;
return false;
}

public override void Execute(TextViewContext context)
{
if (context.SelectedTreeNodes.Length != 1)
{
throw new Exception("SelectedTreeNodes = " + context.SelectedTreeNodes.Length);
}
var xCurrentField = context.SelectedTreeNodes[0] as FieldTreeNode;
if (xCurrentField == null)
if (MessageBox.Show("Do you want to generate FieldAccess code to your clipboard?", "Cosmos Plug tool", MessageBoxButton.YesNo) == MessageBoxResult.No)
{
throw new Exception("Current TreeNode is not a Field!");
return;
}

if (MessageBox.Show("Do you want to generate FieldAccess code to your clipboard?", "Cosmos Plug tool", MessageBoxButton.YesNo) == MessageBoxResult.No)
StringBuilder xString = new StringBuilder();
foreach (var node in context.SelectedTreeNodes)
{
return;
var xCurrentField = node as FieldTreeNode;
if (xCurrentField != null)
{
xString.Append(GenerateField(xCurrentField.FieldDefinition));
xString.AppendLine();
}
}

Clipboard.SetText(xString.ToString());

Clipboard.SetText(String.Format("[FieldAccess(Name = \"{0} {1}.{2}\")] ref {3} field{2}",
xCurrentField.FieldDefinition.FieldType.FullName,
xCurrentField.FieldDefinition.DeclaringType.FullName,
xCurrentField.FieldDefinition.Name,
Utilities.GetCSharpTypeName(xCurrentField.FieldDefinition.FieldType)));
MessageBox.Show("Done", "Cosmos Plug tool");
}

public string GenerateField(FieldDefinition field)
{
StringBuilder xString = new StringBuilder();
xString.Append($"[FieldAccess(Name = \"{field.FieldType.FullName} {field.DeclaringType.FullName}.{field.Name}\")] ref {Utilities.GetCSharpTypeName(field.FieldType)} field{field.Name}");
return xString.ToString();
}
}
}
43 changes: 19 additions & 24 deletions source/Tools/ILSpyPlugAddIn/GenerateMethodPlugEntry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,37 +16,36 @@ public class GenerateMethodPlugEntry: BaseContextMenuEntry
{
public override bool IsVisible(TextViewContext context)
{
if (context.SelectedTreeNodes.Length != 1)
if (context?.SelectedTreeNodes != null)
{
return false;
}
var xCurrentMethod = context.SelectedTreeNodes[0] as MethodTreeNode;
if (xCurrentMethod == null)
{
return false;
foreach (var node in context.SelectedTreeNodes)
{
var xCurrentMethod = node as MethodTreeNode;
if (xCurrentMethod != null)
{
return true;
}
}
}
return true;
return false;
}

public override void Execute(TextViewContext context)
{
if (context.SelectedTreeNodes.Length != 1)
{
throw new Exception("SelectedTreeNodes = " + context.SelectedTreeNodes.Length);
}
var xCurrentMethod = context.SelectedTreeNodes[0] as MethodTreeNode;
if (xCurrentMethod == null)
if (MessageBox.Show("Do you want to generate plug code to your clipboard?", "Cosmos Plug tool", MessageBoxButton.YesNo) == MessageBoxResult.No)
{
throw new Exception("Current TreeNode is not a Method!");
return;
}

if (MessageBox.Show("Do you want to generate plug code to your clipboard?", "Cosmos Plug tool", MessageBoxButton.YesNo) == MessageBoxResult.No)
StringBuilder xString = new StringBuilder();
foreach (var node in context.SelectedTreeNodes)
{
return;
var xCurrentMethod = node as MethodTreeNode;
xString.Append(GenerateMethod(xCurrentMethod.MethodDefinition));
xString.AppendLine();
}

var xSB = GenerateMethod(xCurrentMethod.MethodDefinition);
Clipboard.SetText(xSB);
Clipboard.SetText(xString.ToString());

MessageBox.Show("Done", "Cosmos Plug tool");
}
Expand All @@ -55,11 +54,7 @@ public static string GenerateMethod(MethodDefinition method)
{
var xSB = new StringBuilder();

xSB.Append("public static ");
xSB.Append(Utilities.GetCSharpTypeName(method.ReturnType));
xSB.Append(" ");
xSB.Append(Utilities.GetMethodName(method));
xSB.Append("(");
xSB.Append($"public static {Utilities.GetCSharpTypeName(method.ReturnType)} {Utilities.GetMethodName(method)}(");
var xAddComma = false;

if (!method.IsStatic)
Expand Down
65 changes: 36 additions & 29 deletions source/Tools/ILSpyPlugAddIn/GeneratePropertyAccessorsPlugEntry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Windows;
using ICSharpCode.ILSpy;
using ICSharpCode.ILSpy.TreeNodes;
using Mono.Cecil;

namespace Cosmos.ILSpyPlugs.Plugin
{
Expand All @@ -14,51 +15,57 @@ public class GeneratePropertyAccessorsPlugEntry: BaseContextMenuEntry
{
public override bool IsVisible(TextViewContext context)
{
if (context.SelectedTreeNodes.Length != 1)
if (context?.SelectedTreeNodes != null)
{
return false;
foreach (var node in context.SelectedTreeNodes)
{
var xCurrentProperty = node as PropertyTreeNode;
if (xCurrentProperty != null)
{
return true;
}
}
}
var xCurrentProperty = context.SelectedTreeNodes[0] as PropertyTreeNode;
if (xCurrentProperty == null)
{
return false;
}
return true;
return false;
}

public override void Execute(TextViewContext context)
{
if (context.SelectedTreeNodes.Length != 1)
{
throw new Exception("SelectedTreeNodes = " + context.SelectedTreeNodes.Length);
}
var xCurrentProperty = context.SelectedTreeNodes[0] as PropertyTreeNode;
if (xCurrentProperty == null)
{
throw new Exception("Current TreeNode is not a Property!");
}

if (MessageBox.Show("Do you want to generate plug code to your clipboard?", "Cosmos Plug tool", MessageBoxButton.YesNo) == MessageBoxResult.No)
{
return;
}

var xProp = xCurrentProperty.PropertyDefinition;
var xSB = new StringBuilder();
if (xProp.GetMethod != null)
{
xSB.AppendLine(GenerateMethodPlugEntry.GenerateMethod(xProp.GetMethod));
xSB.AppendLine();
}
if (xProp.SetMethod != null)
var xString = new StringBuilder();
foreach (var node in context.SelectedTreeNodes)
{
xSB.AppendLine(GenerateMethodPlugEntry.GenerateMethod(xProp.SetMethod));
xSB.AppendLine();
var xCurrentProperty = node as PropertyTreeNode;
if (node != null)
{
xString.Append(GenerateProperty(xCurrentProperty.PropertyDefinition));
xString.AppendLine();
}
}

Clipboard.SetText(xSB.ToString().Trim());
Clipboard.SetText(xString.ToString().Trim());

MessageBox.Show("Done", "Cosmos Plug tool");
}

public string GenerateProperty(PropertyDefinition property)
{
StringBuilder xString = new StringBuilder();
if (property.GetMethod != null)
{
xString.AppendLine(GenerateMethodPlugEntry.GenerateMethod(property.GetMethod));
xString.AppendLine();
}
if (property.SetMethod != null)
{
xString.AppendLine(GenerateMethodPlugEntry.GenerateMethod(property.SetMethod));
xString.AppendLine();
}
return xString.ToString();
}
}
}
68 changes: 36 additions & 32 deletions source/Tools/ILSpyPlugAddIn/GenerateTypePlugEntry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,61 +6,65 @@
using System.Windows;
using ICSharpCode.ILSpy;
using ICSharpCode.ILSpy.TreeNodes;
using Mono.Cecil;

namespace Cosmos.ILSpyPlugs.Plugin
{
[ExportContextMenuEntry(Header = "Cosmos Plug: Generate plug")]
public class GenerateTypePlugEntry: BaseContextMenuEntry
public class GenerateTypePlugEntry : BaseContextMenuEntry
{
public override bool IsVisible(TextViewContext context)
{
if (context.SelectedTreeNodes.Length != 1)
if (context?.SelectedTreeNodes != null)
{
return false;
foreach (var node in context.SelectedTreeNodes)
{
var xCurrentType = node as TypeTreeNode;
if (xCurrentType != null)
{
return true;
}
}
}
var xCurrentType = context.SelectedTreeNodes[0] as TypeTreeNode;
if (xCurrentType == null)
{
return false;
}
return true;
return false;
}

public override void Execute(TextViewContext context)
{
if (context.SelectedTreeNodes.Length != 1)
{
throw new Exception("SelectedTreeNodes = " + context.SelectedTreeNodes.Length);
}
var xCurrentType = context.SelectedTreeNodes[0] as TypeTreeNode;
if (xCurrentType == null)
{
throw new Exception("Current TreeNode is not a Type!");
}

if (MessageBox.Show("Do you want to generate plug code to your clipboard?", "Cosmos Plug tool", MessageBoxButton.YesNo) == MessageBoxResult.No)
{
return;
}

var xSB = new StringBuilder();
if (xCurrentType.TypeDefinition.IsPublic)
{
xSB.AppendFormat("[Plug(Target = typeof(global::{0}))]", Utilities.GetCSharpTypeName(xCurrentType.TypeDefinition));
}
else
var xString = new StringBuilder();
foreach (var node in context.SelectedTreeNodes)
{
xSB.AppendFormat("[Plug(TargetName = \"{0}\")]", Utilities.GetCSharpTypeName(xCurrentType.TypeDefinition));
var xCurrentType = node as TypeTreeNode;
if (xCurrentType != null)
{
xString.Append(GenerateType(xCurrentType.TypeDefinition));
xString.AppendLine();
}
}
xSB.AppendLine();
xSB.AppendFormat("public static class {0}Plug", xCurrentType.Name);
xSB.AppendLine();
xSB.AppendLine("{");
xSB.AppendLine("}");

Clipboard.SetText(xSB.ToString());
Clipboard.SetText(xString.ToString());

MessageBox.Show("Done", "Cosmos Plug tool");
}

public string GenerateType(TypeDefinition type)
{
var xString = new StringBuilder();
xString.AppendFormat(
type.IsPublic
? "[Plug(Target = typeof(global::{0}))]"
: "[Plug(TargetName = \"{0}\")]", Utilities.GetCSharpTypeName(type));
xString.AppendLine();
xString.AppendFormat("public static class {0}Impl", type.Name);
xString.AppendLine();
xString.AppendLine("{");
xString.AppendLine("}");
return xString.ToString();
}
}
}
2 changes: 1 addition & 1 deletion source/Tools/ILSpyPlugAddIn/ILSpyPlugAddIn.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<DebugType>full</DebugType>
<PlatformTarget>x86</PlatformTarget>
<PlatformTarget>AnyCPU</PlatformTarget>
<ErrorReport>prompt</ErrorReport>
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>
Expand Down
2 changes: 1 addition & 1 deletion source/Tools/ILSpyPlugAddIn/Utilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public static string GetMethodName(MethodDefinition method)
{
if (method.IsStatic)
{
return "CCtor";
return "Cctor";
}
else
{
Expand Down

0 comments on commit f69f4c8

Please sign in to comment.