-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathYSVR.cs
More file actions
175 lines (156 loc) · 5.47 KB
/
Copy pathYSVR.cs
File metadata and controls
175 lines (156 loc) · 5.47 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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace YuRis_Tool
{
public class YSVR
{
public enum VariableType : byte
{
Void,
Integer,
Decimal,
String
}
public enum VariableScope : byte
{
local,
global,
script,
unknown
}
public class Variable
{
public VariableScope Scope;
public short ScriptIndex;
public short VariableId;
public byte Type;
public uint[] Dimensions;
public object Value;
public Variable(VariableScope scope, short scriptIndex, short variableId, byte type, uint[] dimensions, object value)
{
Scope = scope;
ScriptIndex = scriptIndex;
VariableId = variableId;
Type = type;
Dimensions = dimensions;
Value = value;
}
public override string ToString()
{
return $"{Scope}.{VariableId} -> {Type}({string.Join(",", Dimensions)})";
}
}
static List<Variable> _variables = new List<Variable>();
public static void Load(string filePath)
{
using (var stream = File.OpenRead(filePath))
using (var reader = new BinaryReader(stream))
{
Read(reader);
}
}
static void Read(BinaryReader reader)
{
var magic = reader.ReadInt32();
if (magic != 0x52565359)
{
throw new Exception("Not a valid YSVR file.");
}
var ver = reader.ReadInt32(); // version
var count = reader.ReadUInt16();
for (int i = 0; i < count; i++)
{
var scope = (VariableScope)reader.ReadByte();
//Workaround
if(ver > 450)
reader.ReadByte();
var scriptIndex = reader.ReadInt16();
var variableId = reader.ReadInt16();
var type = reader.ReadByte();
var dimensionCount = reader.ReadByte();
var dimensions = new uint[dimensionCount];
for (var o = 0; o < dimensionCount; o++)
{
dimensions[o] = reader.ReadUInt32();
}
object value = null;
switch (type)
{
case 1:
value = reader.ReadInt64();
break;
case 2:
value = reader.ReadDouble();
break;
case 3:
{
var offset = 0;
var length = reader.ReadUInt16();
if(length > 0)
value = Instruction.GetInstruction(0, reader.ReadBytes(length).AsSpan(), ref offset);
break;
}
default:
break;
};
_variables.Add(new Variable(scope, scriptIndex, variableId, type, dimensions, value));
}
}
public static Variable GetVariable(int scriptIndex, short variableId)
{
var variable = _variables.Where(v => v.ScriptIndex == scriptIndex && v.VariableId == variableId).FirstOrDefault();
if (variable == default)
{
variable = _variables.Where(v => v.VariableId == variableId).FirstOrDefault();
}
if (variable != default)
{
return variable;
}
else
{
//stack local var?
var ret = new Variable(VariableScope.local, Convert.ToInt16(scriptIndex), variableId, 0, Array.Empty<uint>(), null);
//register var
_variables.Add(ret);
return ret;
}
}
public static string GetDecompiledVarName(Variable variable)
{
if (variable.Scope == VariableScope.global && variable.VariableId < YSCD.ReservedVars.Count)
{
return $"{YSCD.ReservedVars[variable.VariableId].Name}";
}
return $"{variable.Scope}.{variable.VariableId}";
}
public static void WriteGlobalVarDecl(TextWriter writer)
{
foreach (var variable in _variables.Where(v => v.Scope == VariableScope.global && v.VariableId >= YSCD.ReservedVars.Count))
{
var dem = variable.Dimensions.Length > 0 ? $"({string.Join(",", variable.Dimensions)})" : "";
var v = $"{GetDecompiledVarName(GetVariable(0, variable.VariableId))}{dem}{(variable.Value == null ? "" : $"={variable.Value}")}";
switch (variable.Type)
{
case 1:
{
writer.WriteLine($"G_INT[@{v}]");
break;
}
case 2:
{
writer.WriteLine($"G_FLT[@{v}]");
break;
}
case 3:
{
writer.WriteLine($"G_STR[${v}]");
break;
}
}
}
}
}
}