|
5 | 5 | using UnityEditor; |
6 | 6 | using UnityEngine; |
7 | 7 |
|
8 | | -public class ContractGeneratorEditor : EditorWindow |
| 8 | +namespace MetaMask |
9 | 9 | { |
10 | | - private static ContractGeneratorEditor instance; |
11 | | - |
12 | | - private Vector2 AbiScrollPos; |
13 | | - private string AbiJson = "[\n\t{ }\n]"; |
14 | | - private string rootNamespace = "Contracts"; |
15 | | - |
16 | | - [MenuItem("Tools/MetaMask/Contract ABI Converter")] |
17 | | - public static void Initialize() |
18 | | - { |
19 | | - instance = GetWindow<ContractGeneratorEditor>(); |
20 | | - instance.titleContent = new GUIContent("Contract ABI -> Code Generator"); |
21 | | - instance.minSize = new Vector2(440, 292); |
22 | | - instance.Show(); |
23 | | - } |
24 | | - |
25 | | - private void OnEnable() |
| 10 | + public class ContractGeneratorEditor : EditorWindow |
26 | 11 | { |
27 | | - EditorApplication.update += EditorUpdate; |
28 | | - } |
| 12 | + private static ContractGeneratorEditor instance; |
29 | 13 |
|
30 | | - private void OnDisable() |
31 | | - { |
32 | | - EditorApplication.update -= EditorUpdate; |
33 | | - } |
| 14 | + private Vector2 AbiScrollPos; |
| 15 | + private string AbiJson = "[\n\t{ }\n]"; |
| 16 | + private string rootNamespace = "Contracts"; |
34 | 17 |
|
35 | | - private void EditorUpdate() |
36 | | - { |
37 | | - |
38 | | - } |
39 | | - |
40 | | - private void OnGUI() |
41 | | - { |
42 | | - if (instance == null) |
| 18 | + [MenuItem("Tools/MetaMask/Contract ABI Converter")] |
| 19 | + public static void Initialize() |
43 | 20 | { |
44 | | - Initialize(); |
| 21 | + instance = GetWindow<ContractGeneratorEditor>(); |
| 22 | + instance.titleContent = new GUIContent("Contract ABI -> Code Generator"); |
| 23 | + instance.minSize = new Vector2(440, 292); |
| 24 | + instance.Show(); |
45 | 25 | } |
46 | 26 |
|
47 | | - EditorGUILayout.LabelField("Contract ABI Json", EditorStyles.boldLabel); |
48 | | - EditorGUILayout.HelpBox("Copy and paste your Contract ABI JSON to the text box below, then click Convert. You may optionally set options to customize the generated C# code.", MessageType.Info); |
49 | | - |
50 | | - AbiScrollPos = EditorGUILayout.BeginScrollView(AbiScrollPos); |
51 | | - AbiJson = EditorGUILayout.TextArea(AbiJson, GUILayout.ExpandHeight(true)); |
52 | | - EditorGUILayout.EndScrollView(); |
53 | | - |
54 | | - GUILayout.Space(15f); |
55 | | - |
56 | | - EditorGUILayout.LabelField("Contract Code Settings", EditorStyles.boldLabel); |
| 27 | + private void OnEnable() |
| 28 | + { |
| 29 | + EditorApplication.update += EditorUpdate; |
| 30 | + } |
57 | 31 |
|
58 | | - rootNamespace = EditorGUILayout.TextField("Root Namespace", rootNamespace); |
| 32 | + private void OnDisable() |
| 33 | + { |
| 34 | + EditorApplication.update -= EditorUpdate; |
| 35 | + } |
59 | 36 |
|
60 | | - GUILayout.Space(15f); |
| 37 | + private void EditorUpdate() { } |
61 | 38 |
|
62 | | - |
63 | | - if (GUILayout.Button("Convert")) |
| 39 | + private void OnGUI() |
64 | 40 | { |
65 | | - string contractName; |
66 | | - string bytecode = null; |
67 | | - ContractABI abi; |
68 | | - try |
69 | | - { |
70 | | - var contractArtifact = JsonConvert.DeserializeObject<ContractArtifact>(AbiJson); |
71 | | - contractName = contractArtifact.ContractName; |
72 | | - bytecode = contractArtifact.Bytecode; |
73 | | - abi = contractArtifact.ABI; |
74 | | - } |
75 | | - catch (JsonSerializationException) |
| 41 | + if (instance == null) |
76 | 42 | { |
77 | | - // Try just ContractABI |
78 | | - abi = JsonConvert.DeserializeObject<ContractABI>(AbiJson); |
79 | | - contractName = rootNamespace.Split(".")[0]; |
| 43 | + Initialize(); |
80 | 44 | } |
81 | | - |
82 | | - // Choose save directory |
83 | | - string path = EditorUtility.SaveFolderPanel("Choose folder to save scripts", "Assets", contractName); |
84 | 45 |
|
85 | | - ContractInterfaceGenerator generator = new ContractInterfaceGenerator(rootNamespace, contractName, bytecode, abi); |
86 | | - var items = generator.GenerateAll(); |
| 46 | + EditorGUILayout.LabelField("Contract ABI Json", EditorStyles.boldLabel); |
| 47 | + EditorGUILayout.HelpBox( |
| 48 | + "Copy and paste your Contract ABI JSON to the text box below, then click Convert. You may optionally set options to customize the generated C# code.", |
| 49 | + MessageType.Info |
| 50 | + ); |
| 51 | + |
| 52 | + AbiScrollPos = EditorGUILayout.BeginScrollView(AbiScrollPos); |
| 53 | + AbiJson = EditorGUILayout.TextArea(AbiJson, GUILayout.ExpandHeight(true)); |
| 54 | + EditorGUILayout.EndScrollView(); |
| 55 | + |
| 56 | + GUILayout.Space(15f); |
| 57 | + |
| 58 | + EditorGUILayout.LabelField("Contract Code Settings", EditorStyles.boldLabel); |
| 59 | + |
| 60 | + rootNamespace = EditorGUILayout.TextField("Root Namespace", rootNamespace); |
87 | 61 |
|
88 | | - foreach (var item in items.Keys) |
| 62 | + GUILayout.Space(15f); |
| 63 | + |
| 64 | + if (GUILayout.Button("Convert")) |
89 | 65 | { |
90 | | - var filepath = path + "/" + item + ".cs"; |
91 | | - var text = items[item]; |
92 | | - |
93 | | - File.WriteAllText(filepath, text); |
94 | | - } |
| 66 | + string contractName; |
| 67 | + string bytecode = null; |
| 68 | + ContractABI abi; |
| 69 | + try |
| 70 | + { |
| 71 | + var contractArtifact = JsonConvert.DeserializeObject<ContractArtifact>(AbiJson); |
| 72 | + contractName = contractArtifact.ContractName; |
| 73 | + bytecode = contractArtifact.Bytecode; |
| 74 | + abi = contractArtifact.ABI; |
| 75 | + } |
| 76 | + catch (JsonSerializationException) |
| 77 | + { |
| 78 | + // Try just ContractABI |
| 79 | + abi = JsonConvert.DeserializeObject<ContractABI>(AbiJson); |
| 80 | + contractName = rootNamespace.Split(".")[0]; |
| 81 | + } |
| 82 | + |
| 83 | + // Choose save directory |
| 84 | + string path = EditorUtility.SaveFolderPanel("Choose folder to save scripts", "Assets", contractName); |
| 85 | + |
| 86 | + ContractInterfaceGenerator generator = new ContractInterfaceGenerator(rootNamespace, contractName, bytecode, abi); |
| 87 | + var items = generator.GenerateAll(); |
95 | 88 |
|
96 | | - AssetDatabase.Refresh(); |
| 89 | + foreach (var item in items.Keys) |
| 90 | + { |
| 91 | + var filepath = path + "/" + item + ".cs"; |
| 92 | + var text = items[item]; |
| 93 | + |
| 94 | + File.WriteAllText(filepath, text); |
| 95 | + } |
| 96 | + |
| 97 | + AssetDatabase.Refresh(); |
| 98 | + } |
97 | 99 | } |
98 | 100 | } |
99 | | - |
100 | | - |
101 | 101 | } |
0 commit comments