Skip to content

Commit

Permalink
Added Comments.
Browse files Browse the repository at this point in the history
Implemented CodeGenerator.
  • Loading branch information
KN4CK3R committed Oct 28, 2016
1 parent f26d403 commit 3ce074c
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 12 deletions.
1 change: 1 addition & 0 deletions FrostbitePlugin.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
<DesignTime>True</DesignTime>
<DependentUpon>Resources.resx</DependentUpon>
</Compile>
<Compile Include="WeakPtrCodeGenerator.cs" />
<Compile Include="WeakPtrNode.cs" />
<Compile Include="WeakPtrSchemaConverter.cs" />
</ItemGroup>
Expand Down
6 changes: 4 additions & 2 deletions FrostbitePluginExt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public class FrostbitePluginExt : Plugin
private INodeInfoReader reader;

private WeakPtrSchemaConverter converter;
private WeakPtrCodeGenerator generator;

public override Image Icon => Properties.Resources.logo_frostbite;

Expand Down Expand Up @@ -44,14 +45,15 @@ public override bool Initialize(IPluginHost host)

// Register the WeakPtr node
converter = new WeakPtrSchemaConverter();
host.RegisterNodeType(typeof(WeakPtrNode), converter, "Frostbite WeakPtr", Icon);
generator = new WeakPtrCodeGenerator();
host.RegisterNodeType(typeof(WeakPtrNode), "Frostbite WeakPtr", Icon, converter, generator);

return true;
}

public override void Terminate()
{
host.UnregisterNodeType(typeof(WeakPtrNode), converter);
host.UnregisterNodeType(typeof(WeakPtrNode), converter, generator);

host.UnregisterNodeInfoReader(reader);

Expand Down
23 changes: 23 additions & 0 deletions WeakPtrCodeGenerator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using ReClassNET.CodeGenerator;
using ReClassNET.Nodes;

namespace FrostbitePlugin
{
class WeakPtrCodeGenerator : ICustomCodeGenerator
{
/// <summary>Checks if the language is C++ and the node is a WeakPtrNode.</summary>
/// <param name="node">The node to check.</param>
/// <param name="language">The language to check.</param>
/// <returns>True if we can generate code, false if not.</returns>
public bool CanGenerateCode(BaseNode node, Language language) => language == Language.Cpp && node is WeakPtrNode;

/// <summary>Gets the member definition of the node.</summary>
/// <param name="node">The member node.</param>
/// <param name="language">The language to generate.</param>
/// <returns>The member definition of the node.</returns>
public MemberDefinition GetMemberDefinition(BaseNode node, Language language)
{
return new MemberDefinition(node, "fb::WeakPtr");
}
}
}
1 change: 1 addition & 0 deletions WeakPtrNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class WeakPtrNode : BaseReferenceNode
/// <summary>Size of the node in bytes.</summary>
public override int MemorySize => IntPtr.Size;

/// <summary>Called when the node was created. Creates a new class as inner node.</summary>
public override void Intialize()
{
var node = ClassManager.CreateClass();
Expand Down
50 changes: 40 additions & 10 deletions WeakPtrSchemaConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace FrostbitePlugin
{
/// <summary>A schema for the WeakPtrNode.</summary>
class WeakPtrSchemaNode : SchemaCustomNode
{
public SchemaClassNode InnerNode { get; }
Expand All @@ -21,15 +22,30 @@ public WeakPtrSchemaNode(SchemaClassNode inner)

class WeakPtrSchemaConverter : ICustomSchemaConverter
{
/// <summary>Name of the type used in the XML data.</summary>
private const string XmlType = "FrostBite::WeakPtr";

public bool CanReadNode(BaseNode node) => node is WeakPtrNode;
/// <summary>Checks if the node can be handled.</summary>
/// <param name="node">The node to check.</param>
/// <returns>True if we can handle the node, false if not.</returns>
public bool CanHandleNode(BaseNode node) => node is WeakPtrNode;

public bool CanReadNode(XElement element) => element.Attribute(ReClassNetFile.XmlTypeAttribute)?.Value == XmlType;
/// <summary>Checks if the element can be handled.</summary>
/// <param name="element">The element to check.</param>
/// <returns>True if we can read node, false if not.</returns>
public bool CanHandleElement(XElement element) => element.Attribute(ReClassNetFile.XmlTypeAttribute)?.Value == XmlType;

public bool CanWriteNode(SchemaCustomNode node) => node is WeakPtrSchemaNode;
/// <summary>Checks if the schema can be handled.</summary>
/// <param name="schema">The schema to check.</param>
/// <returns>True if we can handle schema, false if not.</returns>
public bool CanHandleSchema(SchemaCustomNode schema) => schema is WeakPtrSchemaNode;

public SchemaCustomNode ReadFromNode(BaseNode node, IReadOnlyDictionary<ClassNode, SchemaClassNode> classes, ILogger logger)
/// <summary>Creates the schema which represents the node.</summary>
/// <param name="node">The node to convert.</param>
/// <param name="classes">The mapping from classes to their schema.</param>
/// <param name="logger">The logger.</param>
/// <returns>The schema which represents the node.</returns>
public SchemaCustomNode CreateSchemaFromNode(BaseNode node, IReadOnlyDictionary<ClassNode, SchemaClassNode> classes, ILogger logger)
{
return new WeakPtrSchemaNode(classes[(node as WeakPtrNode).InnerNode as ClassNode])
{
Expand All @@ -38,7 +54,12 @@ public SchemaCustomNode ReadFromNode(BaseNode node, IReadOnlyDictionary<ClassNod
};
}

public SchemaCustomNode ReadFromXml(XElement element, IReadOnlyDictionary<string, SchemaClassNode> classes, ILogger logger)
/// <summary>Creates the schema which represents the element.</summary>
/// <param name="element">The element to convert.</param>
/// <param name="classes">The mapping from class names to their schema.</param>
/// <param name="logger">The logger.</param>
/// <returns>The schema which represents the element.</returns>
public SchemaCustomNode CreateSchemaFromElement(XElement element, IReadOnlyDictionary<string, SchemaClassNode> classes, ILogger logger)
{
var reference = element.Attribute(ReClassNetFile.XmlReferenceAttribute)?.Value;
if (reference == null || !classes.ContainsKey(reference))
Expand All @@ -56,7 +77,12 @@ public SchemaCustomNode ReadFromXml(XElement element, IReadOnlyDictionary<string
};
}

public BaseNode WriteToNode(SchemaCustomNode schema, IReadOnlyDictionary<SchemaClassNode, ClassNode> classes, ILogger logger)
/// <summary>Creates the node which is represented by the schema.</summary>
/// <param name="schema">The schema to convert.</param>
/// <param name="classes">The mapping from class schemas to their nodes.</param>
/// <param name="logger">The logger.</param>
/// <returns>The node which is represented by the schema.</returns>
public BaseNode CreateNodeFromSchema(SchemaCustomNode schema, IReadOnlyDictionary<SchemaClassNode, ClassNode> classes, ILogger logger)
{
return new WeakPtrNode()
{
Expand All @@ -66,14 +92,18 @@ public BaseNode WriteToNode(SchemaCustomNode schema, IReadOnlyDictionary<SchemaC
};
}

public XElement WriteToXml(SchemaCustomNode node, ILogger logger)
/// <summary>Creates the element which represents the schema.</summary>
/// <param name="schema">The schema to convert.</param>
/// <param name="logger">The logger.</param>
/// <returns>The element which represents the schema.</returns>
public XElement CreateElementFromSchema(SchemaCustomNode schema, ILogger logger)
{
return new XElement(
ReClassNetFile.XmlNodeElement,
new XAttribute(ReClassNetFile.XmlNameAttribute, node.Name ?? string.Empty),
new XAttribute(ReClassNetFile.XmlCommentAttribute, node.Comment ?? string.Empty),
new XAttribute(ReClassNetFile.XmlNameAttribute, schema.Name ?? string.Empty),
new XAttribute(ReClassNetFile.XmlCommentAttribute, schema.Comment ?? string.Empty),
new XAttribute(ReClassNetFile.XmlTypeAttribute, XmlType),
new XAttribute(ReClassNetFile.XmlReferenceAttribute, (node as WeakPtrSchemaNode).InnerNode.Name ?? string.Empty)
new XAttribute(ReClassNetFile.XmlReferenceAttribute, (schema as WeakPtrSchemaNode).InnerNode.Name ?? string.Empty)
);
}
}
Expand Down

0 comments on commit 3ce074c

Please sign in to comment.