-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathYuRisScript.cs
More file actions
133 lines (114 loc) · 4.43 KB
/
Copy pathYuRisScript.cs
File metadata and controls
133 lines (114 loc) · 4.43 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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace YuRis_Tool
{
class YuRisScript
{
string _dirPath;
YSCM _yscm;
YSLB _yslb;
YSTL _ystl;
byte[] _ybnKey;
public void Init(string dirPath, byte[] ybnKey)
{
_dirPath = dirPath;
_yscm = new YSCM();
_yscm.Load(Path.Combine(dirPath, "ysc.ybn"));
_yslb = new YSLB();
_yslb.Load(Path.Combine(dirPath, "ysl.ybn"));
_ystl = new YSTL();
_ystl.Load(Path.Combine(dirPath, "yst_list.ybn"));
YSVR.Load(Path.Combine(dirPath, "ysv.ybn"));
_ybnKey = ybnKey;
}
public bool Decompile(int scriptIndex, TextWriter outputStream = null)
{
Console.Write($"Decompiling yst{scriptIndex:D5}.ybn ...");
var ystb = new YSTB(_yscm, _yslb);
if (!ystb.Load(Path.Combine(_dirPath, $"yst{scriptIndex:D5}.ybn"), scriptIndex, _ybnKey))
return false;
outputStream ??= Console.Out;
var commands = ystb.Commands;
var nestDepth = 0;
for (var i = 0; i < commands.Count; i++)
{
var labels = _yslb.Find(scriptIndex, i);
if (labels != null)
{
foreach(var label in labels)
outputStream.WriteLine($"#={label.Name}");
}
var cmd = commands[i];
switch (cmd.Id.ToString())
{
case "IF":
case "LOOP":
{
outputStream.Write("".PadLeft(nestDepth * 4, ' '));
nestDepth++;
break;
}
case "ELSE":
{
outputStream.Write("".PadLeft((nestDepth - 1) * 4, ' '));
break;
}
case "IFEND":
case "LOOPEND":
{
nestDepth--;
outputStream.Write("".PadLeft(nestDepth * 4, ' '));
break;
}
default:
{
outputStream.Write("".PadLeft(nestDepth * 4, ' '));
break;
}
}
outputStream.WriteLine(cmd);
}
return true;
}
public void DecompileProject()
{
List<string> sourcePaths = new List<string>();
foreach(var script in _ystl)
{
var sourcePath = Path.Combine(_dirPath, script.Source);
Directory.CreateDirectory(Path.GetDirectoryName(sourcePath));
sourcePaths.Add(sourcePath);
using var textWriter = new StringWriter();
if(Decompile(script.Id, textWriter))
{
//File.WriteAllText(sourcePath, textWriter.ToString());
var data = textWriter.ToString();
if (data.StartsWith("END[]") && data.Length < 8)
File.WriteAllText(sourcePath, "//Empty file.");
else
File.WriteAllBytes(sourcePath, Extensions.DefaultEncoding.GetBytes(data[..^8]));
Console.Write($" -> {sourcePath}");
}
else
{
Console.Write($" -> Failed. No such file.");
}
Console.WriteLine("");
}
string[] longestCommonPathComponents = sourcePaths
.Select(path => path.Split(Path.DirectorySeparatorChar))
.Transpose()
.Select(parts => parts.Distinct(StringComparer.OrdinalIgnoreCase))
.TakeWhile(distinct => distinct.Count() == 1)
.Select(distinct => distinct.First())
.Append("global.txt")
.ToArray();
using var globalVarWriter = new StringWriter();
YSVR.WriteGlobalVarDecl(globalVarWriter);
//File.WriteAllText(Path.Combine(longestCommonPathComponents), globalVarWriter.ToString());
File.WriteAllBytes(Path.Combine(longestCommonPathComponents), Extensions.DefaultEncoding.GetBytes(globalVarWriter.ToString()));
}
}
}