-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathInstruction.cs
More file actions
467 lines (423 loc) · 13 KB
/
Copy pathInstruction.cs
File metadata and controls
467 lines (423 loc) · 13 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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
using System.Diagnostics;
using System;
namespace YuRis_Tool
{
public abstract class Instruction
{
public static Instruction GetInstruction(int scriptId, Span<byte> data, ref int offset)
{
var id = (Opcode)data[offset];
var operandLength = BitConverter.ToInt16(data.Slice(offset + 1, sizeof(ushort)));
var operandData = data.Slice(offset + 3, operandLength);
offset += 3 + operandLength;
switch (id)
{
case Opcode.Add:
case Opcode.Subtract:
case Opcode.Multiply:
case Opcode.Divide:
case Opcode.Modulo:
return new ArithmeticOperator(null, null, (ArithmeticOperator.Type)id);
case Opcode.LogicalAnd:
case Opcode.LogicalOr:
case Opcode.BitwiseXor:
case Opcode.BitwiseAnd:
case Opcode.BitwiseOr:
return new LogicalOperator(null, null, (LogicalOperator.Type)id);
case Opcode.Equal:
case Opcode.NotEqual:
case Opcode.Less:
case Opcode.Greater:
case Opcode.LessEqual:
case Opcode.GreaterEqual:
return new RelationalOperator(null, null, (RelationalOperator.Type)id);
case Opcode.Negate:
case Opcode.ToString:
case Opcode.ToNumber:
return new UnaryOperator(null, (UnaryOperator.Type)id);
case Opcode.ArrayAccess:
return new ArrayAccess(null, null);
case Opcode.PushByte:
Trace.Assert(operandLength == 1);
return new ByteLiteral((sbyte)operandData[0]);
case Opcode.PushDouble:
Trace.Assert(operandLength == 8);
return new DecimalLiteral(BitConverter.ToDouble(operandData));
case Opcode.PushShort:
Trace.Assert(operandLength == 2);
return new ShortLiteral(BitConverter.ToInt16(operandData));
case Opcode.PushInt:
Trace.Assert(operandLength == 4);
return new IntLiteral(BitConverter.ToInt32(operandData));
case Opcode.PushLong:
Trace.Assert(operandLength == 8);
return new LongLiteral(BitConverter.ToInt64(operandData));
case Opcode.PushString:
return new StringLiteral(Extensions.DefaultEncoding.GetString(operandData));
case Opcode.LoadVariable:
Trace.Assert(operandLength == 3);
return new VariableAccess(scriptId, (VariableLoadMode)operandData[0], BitConverter.ToInt16(operandData[1..]));
case Opcode.LoadVariableRef:
Trace.Assert(operandLength == 3);
return new VariableRef(scriptId, (VariableLoadMode)operandData[0], BitConverter.ToInt16(operandData[1..]), false);
case Opcode.LoadVariableRef2:
Trace.Assert(operandLength == 3);
return new VariableRef(scriptId, (VariableLoadMode)operandData[0], BitConverter.ToInt16(operandData[1..]), true);
case Opcode.Nop:
return new Nop();
default:
throw new Exception($"Invalid opcode:{id:X2}");
}
}
}
public enum Opcode : byte
{
RawOperand = (byte)' ',//20
NotEqual = (byte)'!',//21
//"
//# VariableLoadMode.Pound
//$
Modulo = (byte)'%',//25
LogicalAnd = (byte)'&',//26
//'
//(
ArrayAccess = (byte)')',//29
Multiply = (byte)'*',//2A
Add = (byte)'+',//2B
Nop = (byte)',',//2C
Subtract = (byte)'-',//2D
//.
Divide = (byte)'/',//2F
//0~9
//:
//;
Less = (byte)'<',//3C
Equal = (byte)'=',//3D
Greater = (byte)'>',//3E
//?
//@ VariableLoadMode.At
BitwiseAnd = (byte)'A',//41
PushByte = (byte)'B',//42
//C
//D
//E
PushDouble = (byte)'F',//46
//G
LoadVariable = (byte)'H',//48
PushInt = (byte)'I',//49
//J
//K
PushLong = (byte)'L',//4C
PushString = (byte)'M',//4D
//N
BitwiseOr = (byte)'O',//4F
//P
//Q
Negate = (byte)'R',//52
LessEqual = (byte)'S',//53
//T
//U
LoadVariableRef = (byte)'V',//56
PushShort = (byte)'W',//57
//X
//Y
GreaterEqual = (byte)'Z',//5A
//[
//\
//]
BitwiseXor = (byte)'^',//5E
//_
//` VariableLoadMode.Badtick
//a~h
ToNumber = (byte)'i',//69
//j~r
ToString = (byte)'s',//73
//t
//u
LoadVariableRef2 = (byte)'v',//76
//w
//x
//y
//z
//{
LogicalOr = (byte)'|',//7C
//}
//~
}
public enum VariableLoadMode : byte
{
Backtick = (byte)'`',
Pound = (byte)'#',
At = (byte)'@',
}
public abstract class BinaryOperator<T> : Instruction where T : Enum
{
public Instruction Left;
public Instruction Right;
public T Operator;
public BinaryOperator(Instruction left, Instruction right, T op)
{
Left = left;
Right = right;
Operator = op;
}
public abstract string GetOperator(T type);
public override string ToString()
{
return $"({Left}){GetOperator(Operator)}({Right})";
}
}
public class ArithmeticOperator : BinaryOperator<ArithmeticOperator.Type>
{
public enum Type : byte
{
Add = Opcode.Add,
Subtract = Opcode.Subtract,
Multiply = Opcode.Multiply,
Divide = Opcode.Divide,
Modulo = Opcode.Modulo,
}
public bool Negate = false;
public ArithmeticOperator(Instruction left, Instruction right, Type op) : base(left,right, op) { }
public override string GetOperator(Type type)
{
switch (type)
{
case Type.Add:
return "+";
case Type.Subtract:
return "-";
case Type.Multiply:
return "*";
case Type.Divide:
return "/";
case Type.Modulo:
return "%";
default:
return type.ToString();
}
}
public override string ToString()
{
if (Negate)
return $"(-({base.ToString()}))";
return base.ToString();
}
};
public class RelationalOperator : BinaryOperator<RelationalOperator.Type>
{
public enum Type : byte
{
Equal = Opcode.Equal,
NotEqual = Opcode.NotEqual,
Less = Opcode.Less,
Greater = Opcode.Greater,
LessEqual = Opcode.LessEqual,
GreaterEqual = Opcode.GreaterEqual,
}
public RelationalOperator(Instruction left, Instruction right, Type op) : base(left, right, op) { }
public override string GetOperator(Type type)
{
switch (type)
{
case Type.Equal:
return "==";
case Type.NotEqual:
return "!=";
case Type.Less:
return "<";
case Type.Greater:
return ">";
case Type.LessEqual:
return "<=";
case Type.GreaterEqual:
return ">=";
default:
return type.ToString();
}
}
};
public class LogicalOperator : BinaryOperator<LogicalOperator.Type>
{
public enum Type : byte
{
LogicalAnd = Opcode.LogicalAnd,
LogicalOr = Opcode.LogicalOr,
BitwiseXor = Opcode.BitwiseXor,
BitwiseAnd = Opcode.BitwiseAnd,
BitwiseOr = Opcode.BitwiseOr,
}
public LogicalOperator(Instruction left, Instruction right, Type op) : base(left, right, op) { }
public override string GetOperator(Type type)
{
switch(type)
{
case Type.LogicalAnd:
return "&&";
case Type.LogicalOr:
return "||";
case Type.BitwiseXor:
return "^";
case Type.BitwiseAnd:
return "&";
case Type.BitwiseOr:
return "|";
default:
return type.ToString();
}
}
};
public abstract class Literal<T> : Instruction
{
public T Value;
public override string ToString()
{
if (Value is string s)
return s;
return Value.ToString();
}
}
public class UnaryOperator : Instruction
{
public enum Type : byte
{
Negate = Opcode.Negate,
String = Opcode.ToString,
Number = Opcode.ToNumber,
}
public Instruction Operand;
public Type Operator;
public UnaryOperator(Instruction operand, Type op)
{
Operand = operand;
Operator = op;
}
public override string ToString()
{
switch(Operator)
{
case Type.String:
{
return $"$({Operand})";
}
case Type.Number:
{
return $"@({Operand})";
}
}
return $"({Operator})({Operand})";
}
}
public class RawStringLiteral : Instruction
{
public string Value;
public RawStringLiteral(string value)
{
Value = value;
}
public override string ToString()
{
return Value;
}
}
public class ByteLiteral : Literal<sbyte>
{
public ByteLiteral(sbyte value)
{
Value = value;
}
}
public class ShortLiteral : Literal<short>
{
public ShortLiteral(short value)
{
Value = value;
}
}
public class IntLiteral : Literal<int>
{
public IntLiteral(int value)
{
Value = value;
}
}
public class LongLiteral : Literal<long>
{
public LongLiteral(long value)
{
Value = value;
}
}
public class DecimalLiteral : Literal<double>
{
public DecimalLiteral(double value)
{
Value = value;
}
}
public class StringLiteral : Literal<string>
{
public StringLiteral(string value)
{
Value = value;
}
}
public abstract class AccessInstruction : Instruction
{
public bool Negate = false;
}
public class VariableAccess : AccessInstruction
{
public VariableLoadMode Mode;
public short Index;
public YSVR.Variable _varInfo;
public VariableAccess(int scriptIndex, VariableLoadMode mode, short index)
{
Mode = mode;
Index = index;
_varInfo = YSVR.GetVariable(scriptIndex, Index);
}
public override string ToString()
{
if (Negate)
return $"(-{(char)Mode}{YSVR.GetDecompiledVarName(_varInfo)})";
return $"{(char)Mode}{YSVR.GetDecompiledVarName(_varInfo)}";
}
}
public class VariableRef: Instruction
{
public VariableLoadMode Mode;
public short Index;
public bool SmallV;
public YSVR.Variable _varInfo;
public VariableRef(int scriptIndex, VariableLoadMode mode, short index, bool smallV)
{
Mode = mode;
Index = index;
SmallV = smallV;
_varInfo = YSVR.GetVariable(scriptIndex, Index);
}
public override string ToString()
{
return $"{(char)Mode}{YSVR.GetDecompiledVarName(_varInfo)}";
}
}
public class ArrayAccess : AccessInstruction
{
public VariableRef Variable;
public Instruction[] Indices;
public ArrayAccess(VariableRef variable, Instruction[] indices)
{
Variable = variable;
Indices = indices;
}
public override string ToString()
{
if (Negate)
return $"(-{Variable}({string.Join<Instruction>(",", Indices)}))";
return $"{Variable}({string.Join<Instruction>(",", Indices)})";
}
}
public class Nop : Instruction { }
}