//Console.WriteLine("helo ha:"+args[0]); //普通输出 //Console.WriteLine("<WARN> 这是一个严重的问题。");//警告输出,黄字 //Console.WriteLine("<WARN|aaaa.cs(1)> 这是ee一个严重的问题。");//警告输出,带文件名行号 //Console.WriteLine("<ERR> 这是一个严重的问题。");//错误输出,红字 //Console.WriteLine("<ERR|aaaa.cs> 这是ee一个严重的问题。");//错误输出,带文件名 //Console.WriteLine("SUCC");//输出这个表示编译成功 //控制台输出约定了特别的语法 public static void Main(string[] args) { string outpath = "C:\\NeoCli\\SmartContracts"; //set console Console.OutputEncoding = System.Text.Encoding.UTF8; var log = new DefLogger(); log.Log("Neo.Compiler.MSIL<Debug> console app v" + Assembly.GetEntryAssembly().GetName().Version); bool bCompatible = true; //bool bCompatible = false; string filename = null; for (var i = 0; i < args.Length; i++) { if (args[i][0] == '-') { if (args[i] == "--uncompatible") { bCompatible = false; } //other option } else { filename = args[i]; } } if (filename == null) { log.Log("NEL Forked"); log.Log("need one param for DLL filename."); log.Log("[--uncompatible] able nep8 function"); log.Log("Example:neon abc.dll --uncompatible"); return; } if (bCompatible) { log.Log("use --uncompatible able nep8"); } string onlyname = System.IO.Path.GetFileNameWithoutExtension(filename); string filepdb = onlyname + ".pdb"; var path = Path.GetDirectoryName(filename); if (!string.IsNullOrEmpty(path)) { try { Directory.SetCurrentDirectory(path); } catch { log.Log("Could not find path: " + path); Environment.Exit(-1); } } ILModule mod = new ILModule(); System.IO.Stream fs = null; System.IO.Stream fspdb = null; //open file try { fs = System.IO.File.OpenRead(filename); if (System.IO.File.Exists(filepdb)) { fspdb = System.IO.File.OpenRead(filepdb); } } catch (Exception err) { log.Log("Open File Error:" + err.ToString()); return; } //load module try { mod.LoadModule(fs, fspdb); } catch (Exception err) { log.Log("LoadModule Error:" + err.ToString()); return; } byte[] bytes = null; bool bSucc = false; string jsonstr = null; NeoModule neoM = null; MyJson.JsonNode_Object abijson = null; //convert and build try { var conv = new ModuleConverter(log); ConvOption option = new ConvOption(); option.useNep8 = !bCompatible; NeoModule am = conv.Convert(mod, option); neoM = am; bytes = am.Build(); log.Log("convert succ"); try { abijson = vmtool.FuncExport.Export(am, bytes); StringBuilder sb = new StringBuilder(); abijson.ConvertToStringWithFormat(sb, 0); jsonstr = sb.ToString(); log.Log("gen abi succ"); } catch (Exception err) { log.Log("gen abi Error:" + err.ToString()); } } catch (Exception err) { log.Log("Convert Error:" + err.ToString()); return; } //write bytes try { string bytesname = onlyname + ".avm"; System.IO.File.Delete(bytesname); System.IO.File.WriteAllBytes(bytesname, bytes); log.Log("write:" + bytesname); bSucc = true; } catch (Exception err) { log.Log("Write Bytes Error:" + err.ToString()); return; } try { string abiname = onlyname + ".abi.json"; System.IO.File.Delete(abiname); System.IO.File.WriteAllText(abiname, jsonstr); log.Log("write:" + abiname); bSucc = true; } catch (Exception err) { log.Log("Write abi Error:" + err.ToString()); return; } try { fs.Dispose(); if (fspdb != null) { fspdb.Dispose(); } } catch { } if (bSucc) { _DebugOutput.DebugOutput(outpath, neoM, bytes, abijson); log.Log("SUCC"); } }
//Console.WriteLine("helo ha:"+args[0]); //普通输出 //Console.WriteLine("<WARN> 这是一个严重的问题。");//警告输出,黄字 //Console.WriteLine("<WARN|aaaa.cs(1)> 这是ee一个严重的问题。");//警告输出,带文件名行号 //Console.WriteLine("<ERR> 这是一个严重的问题。");//错误输出,红字 //Console.WriteLine("<ERR|aaaa.cs> 这是ee一个严重的问题。");//错误输出,带文件名 //Console.WriteLine("SUCC");//输出这个表示编译成功 //控制台输出约定了特别的语法 public static void Main(string[] args) { // Set console Console.OutputEncoding = Encoding.UTF8; var log = new DefLogger(); log.Log("Neo.Compiler.MSIL console app v" + Assembly.GetEntryAssembly().GetName().Version); // Check argmuents if (args.Length == 0) { log.Log("You need a parameter to specify the DLL or the file name of the project."); log.Log("Examples: "); log.Log(" neon mySmartContract.dll"); log.Log(" neon mySmartContract.csproj"); Environment.Exit(-1); return; } var fileInfo = new FileInfo(args[0]); // Set current directory if (!fileInfo.Exists) { log.Log("Could not find file " + fileInfo.FullName); Environment.Exit(-1); return; } Stream fs; Stream fspdb; var onlyname = Path.GetFileNameWithoutExtension(fileInfo.Name); var path = fileInfo.Directory.FullName; if (!string.IsNullOrEmpty(path)) { try { Directory.SetCurrentDirectory(path); } catch { log.Log("Could not find path: " + path); Environment.Exit(-1); return; } } switch (fileInfo.Extension.ToLowerInvariant()) { case ".csproj": { // Compile csproj file log.Log("Compiling from csproj project"); var output = Compiler.CompileCSProj(fileInfo.FullName); fs = new MemoryStream(output.Dll); fspdb = new MemoryStream(output.Pdb); break; } case ".vbproj": { // Compile vbproj file log.Log("Compiling from vbproj project"); var output = Compiler.CompileVBProj(fileInfo.FullName); fs = new MemoryStream(output.Dll); fspdb = new MemoryStream(output.Pdb); break; } case ".cs": { // Compile C# files log.Log("Compiling from c# source"); var output = Compiler.CompileCSFile(new string[] { fileInfo.FullName }, new string[0]); fs = new MemoryStream(output.Dll); fspdb = new MemoryStream(output.Pdb); break; } case ".vb": { // Compile VB files log.Log("Compiling from VB source"); var output = Compiler.CompileVBFile(new string[] { fileInfo.FullName }, new string[0]); fs = new MemoryStream(output.Dll); fspdb = new MemoryStream(output.Pdb); break; } case ".dll": { string filepdb = onlyname + ".pdb"; // Open file try { fs = fileInfo.OpenRead(); if (File.Exists(filepdb)) { fspdb = File.OpenRead(filepdb); } else { fspdb = null; } } catch (Exception err) { log.Log("Open File Error:" + err.ToString()); return; } break; } default: { log.Log("File format not supported by neon: " + path); Environment.Exit(-1); return; } } ILModule mod = new ILModule(log); // Load module try { mod.LoadModule(fs, fspdb); } catch (Exception err) { log.Log("LoadModule Error:" + err.ToString()); return; } byte[] bytes; int bSucc = 0; string jsonstr = null; NeoModule module = null; // Convert and build try { var conv = new ModuleConverter(log); ConvOption option = new ConvOption(); module = conv.Convert(mod, option); bytes = module.Build(); log.Log("convert succ"); try { var outjson = vmtool.FuncExport.Export(module, bytes); StringBuilder sb = new StringBuilder(); outjson.ConvertToStringWithFormat(sb, 0); jsonstr = sb.ToString(); log.Log("gen abi succ"); } catch (Exception err) { log.Log("gen abi Error:" + err.ToString()); } } catch (Exception err) { log.Log("Convert Error:" + err.ToString()); return; } // Write bytes try { string bytesname = onlyname + ".nef"; var nef = new NefFile { Compiler = "neon", Version = Version.Parse(((AssemblyFileVersionAttribute)Assembly.GetExecutingAssembly() .GetCustomAttribute(typeof(AssemblyFileVersionAttribute))).Version), Script = bytes, ScriptHash = bytes.ToScriptHash() }; nef.CheckSum = NefFile.ComputeChecksum(nef); File.Delete(bytesname); using (var stream = File.OpenWrite(bytesname)) using (var writer = new BinaryWriter(stream)) { nef.Serialize(writer); } log.Log("write:" + bytesname); bSucc++; } catch (Exception err) { log.Log("Write Bytes Error:" + err.ToString()); return; } try { string abiname = onlyname + ".abi.json"; File.Delete(abiname); File.WriteAllText(abiname, jsonstr); log.Log("write:" + abiname); bSucc++; } catch (Exception err) { log.Log("Write abi Error:" + err.ToString()); return; } try { var features = module == null ? ContractFeatures.NoProperty : module.attributes .Where(u => u.AttributeType.Name == "FeaturesAttribute") .Select(u => (ContractFeatures)u.ConstructorArguments.FirstOrDefault().Value) .FirstOrDefault(); var storage = features.HasFlag(ContractFeatures.HasStorage).ToString().ToLowerInvariant(); var payable = features.HasFlag(ContractFeatures.Payable).ToString().ToLowerInvariant(); string manifest = onlyname + ".manifest.json"; string defManifest = @"{""groups"":[],""features"":{""storage"":" + storage + @",""payable"":" + payable + @"},""abi"":" + jsonstr + @",""permissions"":[{""contract"":""*"",""methods"":""*""}],""trusts"":[],""safeMethods"":[]}"; File.Delete(manifest); File.WriteAllText(manifest, defManifest); log.Log("write:" + manifest); bSucc++; } catch (Exception err) { log.Log("Write manifest Error:" + err.ToString()); return; } try { fs.Dispose(); if (fspdb != null) { fspdb.Dispose(); } } catch { } if (bSucc == 3) { log.Log("SUCC"); } }
public static int Compile(Options options, ILogger log = null) { // Set console Console.OutputEncoding = Encoding.UTF8; log ??= new DefLogger(); log.Log("Neo.Compiler.MSIL console app v" + Assembly.GetAssembly(typeof(Program)).GetName().Version); var fileInfo = new FileInfo(options.File); // Set current directory if (!fileInfo.Exists) { log.Log("Could not find file " + fileInfo.FullName); return(-1); } Stream fs; Stream fspdb; var onlyname = Path.GetFileNameWithoutExtension(fileInfo.Name); var path = fileInfo.Directory.FullName; if (!string.IsNullOrEmpty(path)) { try { Directory.SetCurrentDirectory(path); } catch { log.Log("Could not find path: " + path); return(-1); } } switch (fileInfo.Extension.ToLowerInvariant()) { case ".csproj": { // Compile csproj file log.Log("Compiling from csproj project"); var output = Compiler.CompileCSProj(fileInfo.FullName); fs = new MemoryStream(output.Dll); fspdb = new MemoryStream(output.Pdb); break; } case ".vbproj": { // Compile vbproj file log.Log("Compiling from vbproj project"); var output = Compiler.CompileVBProj(fileInfo.FullName); fs = new MemoryStream(output.Dll); fspdb = new MemoryStream(output.Pdb); break; } case ".cs": { // Compile C# files log.Log("Compiling from c# source"); var output = Compiler.CompileCSFiles(new string[] { fileInfo.FullName }, new string[0]); fs = new MemoryStream(output.Dll); fspdb = new MemoryStream(output.Pdb); break; } case ".vb": { // Compile VB files log.Log("Compiling from VB source"); var output = Compiler.CompileVBFiles(new string[] { fileInfo.FullName }, new string[0]); fs = new MemoryStream(output.Dll); fspdb = new MemoryStream(output.Pdb); break; } case ".dll": { string filepdb = onlyname + ".pdb"; // Open file try { fs = fileInfo.OpenRead(); if (File.Exists(filepdb)) { fspdb = File.OpenRead(filepdb); } else { fspdb = null; } } catch (Exception err) { log.Log("Open File Error:" + err.ToString()); return(-1); } break; } default: { log.Log("File format not supported by neon: " + path); return(-1); } } ILModule mod = new ILModule(log); // Load module try { mod.LoadModule(fs, fspdb); } catch (Exception err) { log.Log("LoadModule Error:" + err.ToString()); return(-1); } JObject abi; byte[] bytes; int bSucc = 0; string debugstr = null; NeoModule module; // Convert and build try { var conv = new ModuleConverter(log); ConvOption option = new ConvOption(); module = conv.Convert(mod, option); bytes = module.Build(); log.Log("convert succ"); Dictionary <int, int> addrConvTable = null; if (options.Optimize) { HashSet <int> entryPoints = new HashSet <int>(); foreach (var func in module.mapMethods) { entryPoints.Add(func.Value.funcaddr); } var optimize = NefOptimizeTool.Optimize(bytes, entryPoints.ToArray(), out addrConvTable); log.Log("optimization succ " + (((bytes.Length / (optimize.Length + 0.0)) * 100.0) - 100).ToString("0.00 '%'")); bytes = optimize; } try { abi = FuncExport.Export(module, bytes, addrConvTable); log.Log("gen abi succ"); } catch (Exception err) { log.Log("gen abi Error:" + err.ToString()); return(-1); } try { var outjson = DebugExport.Export(module, bytes, addrConvTable); debugstr = outjson.ToString(false); log.Log("gen debug succ"); } catch (Exception err) { log.Log("gen debug Error:" + err.ToString()); } } catch (Exception err) { log.Log("Convert Error:" + err.ToString()); return(-1); } // Write bytes try { string bytesname = onlyname + ".nef"; var nef = new NefFile { Compiler = "neon", Version = Version.Parse(((AssemblyFileVersionAttribute)Assembly.GetAssembly(typeof(Program)) .GetCustomAttribute(typeof(AssemblyFileVersionAttribute))).Version), Script = bytes, ScriptHash = bytes.ToScriptHash() }; nef.CheckSum = NefFile.ComputeChecksum(nef); File.Delete(bytesname); using (var stream = File.OpenWrite(bytesname)) using (var writer = new BinaryWriter(stream)) { nef.Serialize(writer); } log.Log("write:" + bytesname); bSucc++; } catch (Exception err) { log.Log("Write Bytes Error:" + err.ToString()); return(-1); } try { var sbABI = abi.ToString(false); string abiname = onlyname + ".abi.json"; File.Delete(abiname); File.WriteAllText(abiname, sbABI.ToString()); log.Log("write:" + abiname); bSucc++; } catch (Exception err) { log.Log("Write abi Error:" + err.ToString()); return(-1); } try { string debugname = onlyname + ".debug.json"; string debugzip = onlyname + ".nefdbgnfo"; var tempName = Path.GetTempFileName(); File.Delete(tempName); File.WriteAllText(tempName, debugstr); File.Delete(debugzip); using (var archive = ZipFile.Open(debugzip, ZipArchiveMode.Create)) { archive.CreateEntryFromFile(tempName, Path.GetFileName(debugname)); } File.Delete(tempName); log.Log("write:" + debugzip); bSucc++; } catch (Exception err) { log.Log("Write debug Error:" + err.ToString()); return(-1); } try { string manifest = onlyname + ".manifest.json"; var defManifest = FuncExport.GenerateManifest(abi, module); File.Delete(manifest); File.WriteAllText(manifest, defManifest); log.Log("write:" + manifest); bSucc++; } catch (Exception err) { log.Log("Write manifest Error:" + err.ToString()); return(-1); } try { fs.Dispose(); if (fspdb != null) { fspdb.Dispose(); } } catch { } if (bSucc == 4) { log.Log("SUCC"); return(0); } return(-1); }
//Console.WriteLine("helo ha:"+args[0]); //普通输出 //Console.WriteLine("<WARN> 这是一个严重的问题。");//警告输出,黄字 //Console.WriteLine("<WARN|aaaa.cs(1)> 这是ee一个严重的问题。");//警告输出,带文件名行号 //Console.WriteLine("<ERR> 这是一个严重的问题。");//错误输出,红字 //Console.WriteLine("<ERR|aaaa.cs> 这是ee一个严重的问题。");//错误输出,带文件名 //Console.WriteLine("SUCC");//输出这个表示编译成功 //控制台输出约定了特别的语法 public static void Main(string[] args) { //set console Console.OutputEncoding = System.Text.Encoding.UTF8; var log = new DefLogger(); log.Log("Neo.Compiler.MSIL console app v" + Assembly.GetEntryAssembly().GetName().Version); if (args.Length == 0) { log.Log("need one param for DLL filename."); log.Log("Example:neon abc.dll"); return; } string filename = args[0]; string onlyname = Path.GetFileNameWithoutExtension(filename); string filepdb = onlyname + ".pdb"; var path = Path.GetDirectoryName(filename); if (!string.IsNullOrEmpty(path)) { try { Directory.SetCurrentDirectory(path); } catch { log.Log("Could not find path: " + path); Environment.Exit(-1); return; } } ILModule mod = new ILModule(log); Stream fs; Stream fspdb = null; //open file try { fs = File.OpenRead(filename); if (File.Exists(filepdb)) { fspdb = File.OpenRead(filepdb); } } catch (Exception err) { log.Log("Open File Error:" + err.ToString()); return; } //load module try { mod.LoadModule(fs, fspdb); } catch (Exception err) { log.Log("LoadModule Error:" + err.ToString()); return; } byte[] bytes; bool bSucc; string jsonstr = null; //convert and build try { var conv = new ModuleConverter(log); ConvOption option = new ConvOption(); NeoModule am = conv.Convert(mod, option); bytes = am.Build(); log.Log("convert succ"); try { var outjson = vmtool.FuncExport.Export(am, bytes); StringBuilder sb = new StringBuilder(); outjson.ConvertToStringWithFormat(sb, 0); jsonstr = sb.ToString(); log.Log("gen abi succ"); } catch (Exception err) { log.Log("gen abi Error:" + err.ToString()); } } catch (Exception err) { log.Log("Convert Error:" + err.ToString()); return; } //write bytes try { string bytesname = onlyname + ".avm"; File.Delete(bytesname); File.WriteAllBytes(bytesname, bytes); log.Log("write:" + bytesname); bSucc = true; } catch (Exception err) { log.Log("Write Bytes Error:" + err.ToString()); return; } try { string abiname = onlyname + ".abi.json"; File.Delete(abiname); File.WriteAllText(abiname, jsonstr); log.Log("write:" + abiname); bSucc = true; } catch (Exception err) { log.Log("Write abi Error:" + err.ToString()); return; } try { fs.Dispose(); if (fspdb != null) { fspdb.Dispose(); } } catch { } if (bSucc) { log.Log("SUCC"); } }
public static int Compile(Options options) { // Set console Console.OutputEncoding = Encoding.UTF8; var log = new DefLogger(); log.Log("Neo.Compiler.MSIL console app v" + Assembly.GetEntryAssembly().GetName().Version); var fileInfo = new FileInfo(options.File); // Set current directory if (!fileInfo.Exists) { log.Log("Could not find file " + fileInfo.FullName); return(-1); } Stream fs; Stream fspdb; var onlyname = Path.GetFileNameWithoutExtension(fileInfo.Name); var path = fileInfo.Directory.FullName; if (!string.IsNullOrEmpty(path)) { try { Directory.SetCurrentDirectory(path); } catch { log.Log("Could not find path: " + path); return(-1); } } switch (fileInfo.Extension.ToLowerInvariant()) { case ".csproj": { // Compile csproj file log.Log("Compiling from csproj project"); var output = Compiler.CompileCSProj(fileInfo.FullName); fs = new MemoryStream(output.Dll); fspdb = new MemoryStream(output.Pdb); break; } case ".vbproj": { // Compile vbproj file log.Log("Compiling from vbproj project"); var output = Compiler.CompileVBProj(fileInfo.FullName); fs = new MemoryStream(output.Dll); fspdb = new MemoryStream(output.Pdb); break; } case ".cs": { // Compile C# files log.Log("Compiling from c# source"); var output = Compiler.CompileCSFiles(new string[] { fileInfo.FullName }, new string[0]); fs = new MemoryStream(output.Dll); fspdb = new MemoryStream(output.Pdb); break; } case ".vb": { // Compile VB files log.Log("Compiling from VB source"); var output = Compiler.CompileVBFiles(new string[] { fileInfo.FullName }, new string[0]); fs = new MemoryStream(output.Dll); fspdb = new MemoryStream(output.Pdb); break; } case ".dll": { string filepdb = onlyname + ".pdb"; // Open file try { fs = fileInfo.OpenRead(); if (File.Exists(filepdb)) { fspdb = File.OpenRead(filepdb); } else { fspdb = null; } } catch (Exception err) { log.Log("Open File Error:" + err.ToString()); return(-1); } break; } default: { log.Log("File format not supported by neon: " + path); return(-1); } } ILModule mod = new ILModule(log); // Load module try { mod.LoadModule(fs, fspdb); } catch (Exception err) { log.Log("LoadModule Error:" + err.ToString()); return(-1); } byte[] bytes; int bSucc = 0; string jsonstr = null; NeoModule module = null; // Convert and build try { var conv = new ModuleConverter(log); ConvOption option = new ConvOption(); module = conv.Convert(mod, option); bytes = module.Build(); log.Log("convert succ"); Dictionary <int, int> addrConvTable = null; if (options.Optimize) { module.ConvertFuncAddr(); List <int> entryPoints = new List <int>(); foreach (var func in module.mapMethods) { int srcaddr = func.Value.funcaddr; if (entryPoints.Contains(srcaddr) == false) { entryPoints.Add(srcaddr); } } var optimize = NefOptimizeTool.Optimize(bytes, entryPoints.ToArray(), out addrConvTable); log.Log("optimization succ " + (((bytes.Length / (optimize.Length + 0.0)) * 100.0) - 100).ToString("0.00 '%'")); bytes = optimize; } try { var outjson = vmtool.FuncExport.Export(module, bytes, addrConvTable); StringBuilder sb = new StringBuilder(); outjson.ConvertToStringWithFormat(sb, 0); jsonstr = sb.ToString(); log.Log("gen abi succ"); } catch (Exception err) { log.Log("gen abi Error:" + err.ToString()); return(-1); } } catch (Exception err) { log.Log("Convert Error:" + err.ToString()); return(-1); } // Write bytes try { string bytesname = onlyname + ".nef"; var nef = new NefFile { Compiler = "neon", Version = Version.Parse(((AssemblyFileVersionAttribute)Assembly.GetExecutingAssembly() .GetCustomAttribute(typeof(AssemblyFileVersionAttribute))).Version), Script = bytes, ScriptHash = bytes.ToScriptHash() }; nef.CheckSum = NefFile.ComputeChecksum(nef); File.Delete(bytesname); using (var stream = File.OpenWrite(bytesname)) using (var writer = new BinaryWriter(stream)) { nef.Serialize(writer); } log.Log("write:" + bytesname); bSucc++; } catch (Exception err) { log.Log("Write Bytes Error:" + err.ToString()); return(-1); } try { string abiname = onlyname + ".abi.json"; File.Delete(abiname); File.WriteAllText(abiname, jsonstr); log.Log("write:" + abiname); bSucc++; } catch (Exception err) { log.Log("Write abi Error:" + err.ToString()); return(-1); } try { var features = module == null ? ContractFeatures.NoProperty : module.attributes .Where(u => u.AttributeType.Name == "FeaturesAttribute") .Select(u => (ContractFeatures)u.ConstructorArguments.FirstOrDefault().Value) .FirstOrDefault(); var extraAttributes = module == null ? new List <Mono.Collections.Generic.Collection <CustomAttributeArgument> >() : module.attributes.Where(u => u.AttributeType.Name == "ManifestExtraAttribute").Select(attribute => attribute.ConstructorArguments).ToList(); var extra = BuildExtraAttributes(extraAttributes); var storage = features.HasFlag(ContractFeatures.HasStorage).ToString().ToLowerInvariant(); var payable = features.HasFlag(ContractFeatures.Payable).ToString().ToLowerInvariant(); string manifest = onlyname + ".manifest.json"; string defManifest = @"{""groups"":[],""features"":{""storage"":" + storage + @",""payable"":" + payable + @"},""abi"":" + jsonstr + @",""permissions"":[{""contract"":""*"",""methods"":""*""}],""trusts"":[],""safeMethods"":[],""extra"":" + extra + "}"; File.Delete(manifest); File.WriteAllText(manifest, defManifest); log.Log("write:" + manifest); bSucc++; } catch (Exception err) { log.Log("Write manifest Error:" + err.ToString()); return(-1); } try { fs.Dispose(); if (fspdb != null) { fspdb.Dispose(); } } catch { } if (bSucc == 3) { log.Log("SUCC"); return(0); } return(-1); }