-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathExploadUnityCodegen.cs
More file actions
269 lines (231 loc) · 8.5 KB
/
ExploadUnityCodegen.cs
File metadata and controls
269 lines (231 loc) · 8.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
using System;
using System.Collections;
using UnityEngine;
using UnityEngine.Networking;
using Newtonsoft.Json;
namespace Expload.Unity.Codegen
{
public static class ExploadTypeConverters
{
// signed integers
public static sbyte ParseInt8(string elem)
{
if (elem.StartsWith("int8.")) {
return sbyte.Parse(elem.Substring("int8.".Length));
} else {
throw new ArgumentException("Wrong format for int8 type: " + elem);
}
}
public static string PrintInt8(sbyte elem)
{
return "int8." + elem.ToString();
}
public static short ParseInt16(string elem)
{
if (elem.StartsWith("int16.")) {
return short.Parse(elem.Substring("int16.".Length));
} else {
throw new ArgumentException("Wrong format for int16 type: " + elem);
}
}
public static string PrintInt16(short elem)
{
return "int16." + elem.ToString();
}
public static int ParseInt32(string elem)
{
if (elem.StartsWith("int32.")) {
return int.Parse(elem.Substring("int32.".Length));
} else {
throw new ArgumentException("Wrong format for int32 type: " + elem);
}
}
public static string PrintInt32(int elem)
{
return "int32." + elem.ToString();
}
// unsigned integers
public static byte ParseUInt8(string elem)
{
if (elem.StartsWith("uint8.")) {
return byte.Parse(elem.Substring("uint8.".Length));
} else {
throw new ArgumentException("Wrong format for uint8 type: " + elem);
}
}
public static string PruintUInt8(byte elem)
{
return "uint8." + elem.ToString();
}
public static ushort ParseUInt16(string elem)
{
if (elem.StartsWith("uint16.")) {
return ushort.Parse(elem.Substring("uint16.".Length));
} else {
throw new ArgumentException("Wrong format for uint16 type: " + elem);
}
}
public static string PrintUInt16(ushort elem)
{
return "uint16." + elem.ToString();
}
public static uint ParseUInt32(string elem)
{
if (elem.StartsWith("uint32.")) {
return uint.Parse(elem.Substring("uint32.".Length));
} else {
throw new ArgumentException("Wrong format for uint32 type: " + elem);
}
}
public static string PrintUInt32(uint elem)
{
return "uint32." + elem.ToString();
}
// bool
public static bool ParseBool(string elem)
{
if (elem == "bool.true") {
return true;
} else if (elem == "bool.false") {
return false;
} else {
throw new ArgumentException("Wrong format for bool type: " + elem);
}
}
public static string PrintBool(bool elem)
{
return "bool." + (elem ? "true" : "false");
}
// utf8
public static string ParseUtf8(string elem)
{
if (elem.StartsWith("utf8.")) {
return elem.Substring("utf8.".Length);
} else {
throw new ArgumentException("Wrong format for utf8 type: " + elem);
}
}
public static string PrintUtf8(string elem)
{
return "utf8." + elem;
}
// bytes
public static byte[] ParseBytes(string elem)
{
if (elem.StartsWith("bytes.")) {
string hex = elem.Substring("bytes.".Length);
byte[] bytes = new byte[hex.Length / 2];
for (int i = 0; i < hex.Length; i += 2) {
bytes[i / 2] = Convert.ToByte(hex.Substring(i, 2), 16);
}
return bytes;
} else {
throw new ArgumentException("Wrong format for int32 type: " + elem);
}
}
public static string PrintBytes(byte[] elem)
{
return "bytes." + BitConverter.ToString(elem).Replace("-", "");
}
// null
public static object ParseNull(string elem)
{
if (elem == "null") {
return null;
} else {
throw new ArgumentException("Wrong format for null type: " + elem);
}
}
public static string PrintNull(object elem)
{
return "null";
}
}
public class ExploadMethodRequest
{
public string address { get; set; }
public string method { get; set; }
public string[] args { get; set; }
public ExploadMethodRequest(string address, string method, string[] args)
{
this.address = address;
this.method = method;
this.args = args;
}
}
public class ExploadResponseFinalState
{
public long spentWatts { get; set; }
public long refundWatts { get; set; }
public long totalWatts { get; set; }
public string[] stack { get; set; }
}
public class ExploadResponseData
{
public ExploadResponseFinalState finalState { get; set; }
}
public class ExploadResponse
{
public string transactionId { get; set; }
public string error { get; set; }
public string errorCode { get; set; }
public ExploadResponseData data { get; set; }
}
public abstract class ProgramRequest<T>
{
public byte[] ProgramAddress { get; protected set; }
public T Result { get; protected set; }
public string TransactionId { get; protected set; }
public string Error { get; protected set; }
public bool IsError { get; protected set; }
protected ProgramRequest(byte[] programAddress)
{
ProgramAddress = programAddress;
IsError = false;
Error = "";
}
protected abstract T ParseResult(string elem);
protected IEnumerator SendRequest(string method, string[] args)
{
var request = new ExploadMethodRequest(BitConverter.ToString(ProgramAddress).Replace("-", ""), method, args);
string json = JsonConvert.SerializeObject(request);
UnityWebRequest www = UnityWebRequest.Put("localhost:8087/api/program/method", json);
www.method = "POST";
www.SetRequestHeader("Content-Type", "application/json");
yield return www.SendWebRequest();
if (www.isNetworkError || www.isHttpError)
{
IsError = true;
Error = www.error + "\nHttp code: " + www.responseCode + "\nText: " + www.downloadHandler.text;
}
else
{
try
{
Debug.Log(www.downloadHandler.text);
var response = JsonConvert.DeserializeObject<ExploadResponse>(www.downloadHandler.text);
TransactionId = response.transactionId;
if (response.error.Length != 0) {
IsError = true;
Error = "Error from response: " + response.error;
} else if (response.data.finalState.stack.Length > 1) {
IsError = true;
Error = "Invalid method result:\n[" + String.Join(", ", response.data.finalState.stack) + "]";
} else if (response.data.finalState.stack.Length == 1) {
Result = ParseResult(response.data.finalState.stack[0]);
}
}
catch (JsonSerializationException e)
{
IsError = true;
Error = "Invalid JSON:\n" + www.downloadHandler.text + "\nError: " + e.Message;
}
catch (ArgumentException e)
{
IsError = true;
Error = "Invalid JSON:\n" + www.downloadHandler.text + "\nError: " + e.Message;
}
}
}
}
}