Skip to content

Commit

Permalink
Added client code generator (Neo-Lux only for now )
Browse files Browse the repository at this point in the history
  • Loading branch information
Relfos committed Apr 1, 2018
1 parent 75cefc1 commit cdcf0b6
Show file tree
Hide file tree
Showing 5 changed files with 192 additions and 17 deletions.
128 changes: 128 additions & 0 deletions NEO-Debugger-Core/Generator/NeoLux.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
using Neo.Emulation;
using System;
using System.IO;
using System.Text;

namespace Neo.Debugger.Core.Generator
{
public static class NeoLux
{
public static string ConvertType(Emulator.Type type)
{
switch (type)
{
case Emulator.Type.Boolean: return "bool";
case Emulator.Type.String: return "string";

case Emulator.Type.Integer: return "BigInteger";
case Emulator.Type.ByteArray: return "byte[]";
case Emulator.Type.Array: return "object[]";

default: throw new ArgumentException("Invalid type: " + type);
}
}

public static string GenerateInterface(ABI abi)
{
var code = new StringBuilder();

code.AppendLine("using NeoLux;");
code.AppendLine("using Neo.Cryptography;");
code.AppendLine("using System.Numerics;");
code.AppendLine();


var contractName = abi.fileName;
while (contractName.Contains("."))
{
contractName = Path.GetFileNameWithoutExtension(contractName);
}

code.AppendLine("public class " + contractName + " {");
code.AppendLine();

code.AppendLine("\tpublic string ContractHash { get; private set; }");
code.AppendLine();

code.AppendLine("\tpublic " + contractName + "(string contractHash) {");
code.AppendLine("\t\tthis.ContractHash = contractHash;");
code.AppendLine("\t}");


foreach (var entry in abi.functions)
{
if (entry.Key == abi.entryPoint.name)
{
continue;
}

var method = entry.Value;

bool isPure = false;

var returnType = ConvertType(method.returnType);
code.AppendLine();

code.Append("\tpublic " + returnType + " " + entry.Key + "(");

int count = 0;

if (!isPure)
{
code.Append("KeyPair from_key");
count++;
}

foreach (var arg in method.inputs)
{
if (count > 0) code.Append(",");

var argType = ConvertType(arg.type);
code.Append(argType+" " +arg.name);
count++;
}

code.AppendLine(") {");

code.Append("\t\tvar response = api.CallContract(from_key, contractHash, \"" + method.name + "\", new object[] { ");

count = 0;
if (!isPure)
{
code.Append("from_key");
count++;
}


foreach (var arg in method.inputs)
{
if (count > 0) code.Append(",");

code.Append(arg.name);
count++;
}

code.AppendLine("});");

switch (method.returnType)
{
case Emulator.Type.Integer: code.AppendLine("\t\tvar result = new BigInteger((byte[])response.result[0]);"); break;
case Emulator.Type.String: code.AppendLine("\t\tvar result = System.Text.Encoding.UTF8.GetString((byte[])response.result[0]);"); break;
case Emulator.Type.Boolean: code.AppendLine("\t\tvar result = (bool)response.result[0];"); break;
case Emulator.Type.ByteArray: code.AppendLine("\t\tvar result = (byte[])response.result[0];"); break;
case Emulator.Type.Array: code.AppendLine("\t\tvar result = response.result[0];"); break;
default: throw new ArgumentException("Invalid type: " + method.returnType);
}


code.AppendLine("\t\treturn result;");

code.AppendLine("\t}");
}

code.AppendLine("}");

return code.ToString();
}
}
}
2 changes: 2 additions & 0 deletions NEO-Debugger-Core/NEO-Debugger-Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
<Compile Include="Data\Enums.cs" />
<Compile Include="Dissambler\NeoDebugMap.cs" />
<Compile Include="Dissambler\NeoDisassembler.cs" />
<Compile Include="Generator\NeoLux.cs" />
<Compile Include="Profiler\ProfilerContext.cs" />
<Compile Include="Shell\ContractCommand.cs" />
<Compile Include="Shell\HelpCommand.cs" />
Expand Down Expand Up @@ -96,5 +97,6 @@
<Analyzer Include="..\packages\Microsoft.CodeAnalysis.Analyzers.2.6.0\analyzers\dotnet\cs\Microsoft.CodeAnalysis.Analyzers.dll" />
<Analyzer Include="..\packages\Microsoft.CodeAnalysis.Analyzers.2.6.0\analyzers\dotnet\cs\Microsoft.CodeAnalysis.CSharp.Analyzers.dll" />
</ItemGroup>
<ItemGroup />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
5 changes: 5 additions & 0 deletions NEO-Debugger-Core/Utils/DebugManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,11 @@ public string GetContentFor(string path)
throw new ArgumentException("Invalid path: " + path);
}

public void SetContentFor(string path, string content)
{
_debugContent[path] = content;
}

public bool LoadAvmFile(string avmPath)
{
//Decide what we need to open
Expand Down
52 changes: 36 additions & 16 deletions NEO-Debugger-Winform/Forms/MainForm.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 21 additions & 1 deletion NEO-Debugger-Winform/Forms/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using Neo.Debugger.Core.Utils;
using Neo.Debugger.Core.Data;
using Neo.VM;
using Neo.Debugger.Core.Generator;

namespace Neo.Debugger.Forms
{
Expand Down Expand Up @@ -1332,11 +1333,13 @@ private void ReloadTextArea(string filePath, string content)
}
}

private void AddNodeToProjectTree(string path)
private TreeNode AddNodeToProjectTree(string path)
{
var fileName = Path.GetFileName(path);
var node = projectTree.Nodes.Add(path, fileName);
nodeMap[path] = node;

return node;
}

private void ReloadProjectTree()
Expand Down Expand Up @@ -1619,5 +1622,22 @@ private void rebuildToolStripMenuItem_Click(object sender, EventArgs e)

CompileContract();
}

private void neoLuxToolStripMenuItem_Click(object sender, EventArgs e)
{
if (_debugger.ABI == null || _debugger.ABI.functions.Count == 0)
{
MessageBox.Show("No ABI loaded for this contract!");
return;
}

var code = NeoLux.GenerateInterface(_debugger.ABI);

var path = _debugger.ContractName + "Client.cs";
_debugger.SetContentFor(path, code);
AddNodeToProjectTree(path);

ReloadTextArea(path);
}
}
}

0 comments on commit cdcf0b6

Please sign in to comment.