|
| 1 | +namespace CodexContractsPlugin |
| 2 | +{ |
| 3 | + public class SelfUpdater |
| 4 | + { |
| 5 | + public void Update(string abi, string bytecode) |
| 6 | + { |
| 7 | + var filePath = GetMarketplaceFilePath(); |
| 8 | + var content = GenerateContent(abi, bytecode); |
| 9 | + var contentLines = content.Split("\r\n"); |
| 10 | + |
| 11 | + var beginWith = new string[] |
| 12 | + { |
| 13 | + "using Nethereum.ABI.FunctionEncoding.Attributes;", |
| 14 | + "using Nethereum.Contracts;", |
| 15 | + "using System.Numerics;", |
| 16 | + "", |
| 17 | + "// Generated code, do not modify.", |
| 18 | + "", |
| 19 | + "#pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.", |
| 20 | + "namespace CodexContractsPlugin.Marketplace", |
| 21 | + "{" |
| 22 | + }; |
| 23 | + |
| 24 | + var endWith = new string[] |
| 25 | + { |
| 26 | + "}", |
| 27 | + "#pragma warning restore CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable." |
| 28 | + }; |
| 29 | + |
| 30 | + File.Delete(filePath); |
| 31 | + File.WriteAllLines(filePath, |
| 32 | + beginWith.Concat( |
| 33 | + contentLines.Concat( |
| 34 | + endWith)) |
| 35 | + ); |
| 36 | + |
| 37 | + throw new Exception("Oh no! CodexContracts were updated. Current build of CodexContractsPlugin is incompatible. " + |
| 38 | + "But fear not! SelfUpdater.cs has automatically updated the plugin. Just rebuild and rerun and it should work. " + |
| 39 | + "Just in case, manual update instructions are found here: 'CodexContractsPlugin/Marketplace/README.md'."); |
| 40 | + } |
| 41 | + |
| 42 | + private string GetMarketplaceFilePath() |
| 43 | + { |
| 44 | + var here = Directory.GetCurrentDirectory(); |
| 45 | + while (true) |
| 46 | + { |
| 47 | + var path = GetMarketplaceFile(here); |
| 48 | + if (path != null) return path; |
| 49 | + |
| 50 | + var parent = Directory.GetParent(here); |
| 51 | + var up = parent?.FullName; |
| 52 | + if (up == null || up == here) throw new Exception("Unable to locate ProjectPlugins folder. Unable to update contracts."); |
| 53 | + here = up; |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + private string? GetMarketplaceFile(string root) |
| 58 | + { |
| 59 | + var path = Path.Combine(root, "ProjectPlugins", "CodexContractsPlugin", "Marketplace", "Marketplace.cs"); |
| 60 | + if (File.Exists(path)) return path; |
| 61 | + return null; |
| 62 | + } |
| 63 | + |
| 64 | + private string GenerateContent(string abi, string bytecode) |
| 65 | + { |
| 66 | + var deserializer = new Nethereum.Generators.Net.GeneratorModelABIDeserialiser(); |
| 67 | + var abiModel = deserializer.DeserialiseABI(abi); |
| 68 | + var abiCtor = abiModel.Constructor; |
| 69 | + var c = new Nethereum.Generators.CQS.ContractDeploymentCQSMessageGenerator(abiCtor, "namespace", bytecode, "Marketplace", Nethereum.Generators.Core.CodeGenLanguage.CSharp); |
| 70 | + var lines = ""; |
| 71 | + lines += c.GenerateClass(); |
| 72 | + lines += "\r\n"; |
| 73 | + |
| 74 | + foreach (var eventAbi in abiModel.Events) |
| 75 | + { |
| 76 | + var d = new Nethereum.Generators.DTOs.EventDTOGenerator(eventAbi, "namespace", Nethereum.Generators.Core.CodeGenLanguage.CSharp); |
| 77 | + lines += d.GenerateClass(); |
| 78 | + lines += "\r\n"; |
| 79 | + } |
| 80 | + |
| 81 | + foreach (var errorAbi in abiModel.Errors) |
| 82 | + { |
| 83 | + var e = new Nethereum.Generators.DTOs.ErrorDTOGenerator(errorAbi, "namespace", Nethereum.Generators.Core.CodeGenLanguage.CSharp); |
| 84 | + lines += e.GenerateClass(); |
| 85 | + lines += "\r\n"; |
| 86 | + } |
| 87 | + |
| 88 | + foreach (var funcAbi in abiModel.Functions) |
| 89 | + { |
| 90 | + var f = new Nethereum.Generators.DTOs.FunctionOutputDTOGenerator(funcAbi, "namespace", Nethereum.Generators.Core.CodeGenLanguage.CSharp); |
| 91 | + var ff = new Nethereum.Generators.CQS.FunctionCQSMessageGenerator(funcAbi, "namespace", "funcoutput", Nethereum.Generators.Core.CodeGenLanguage.CSharp); |
| 92 | + lines += f.GenerateClass(); |
| 93 | + lines += "\r\n"; |
| 94 | + lines += ff.GenerateClass(); |
| 95 | + lines += "\r\n"; |
| 96 | + } |
| 97 | + |
| 98 | + foreach (var structAbi in abiModel.Structs) |
| 99 | + { |
| 100 | + var g = new Nethereum.Generators.DTOs.StructTypeGenerator(structAbi, "namespace", Nethereum.Generators.Core.CodeGenLanguage.CSharp); |
| 101 | + lines += g.GenerateClass(); |
| 102 | + lines += "\r\n"; |
| 103 | + } |
| 104 | + |
| 105 | + return lines; |
| 106 | + } |
| 107 | + } |
| 108 | +} |
0 commit comments