-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExecute.cs
260 lines (228 loc) · 8.8 KB
/
Execute.cs
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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Antlr4.Runtime;
using MCFBuilder.Type;
using MCFBuilder.Utility;
using Newtonsoft.Json;
using System.Runtime.CompilerServices;
using System.Xml.Linq;
using MethodTimer;
using System.Diagnostics;
using System.Runtime.ExceptionServices;
using Antlr4.Runtime.Misc;
using MCFBuilder.MCData;
using MCFBuilder.Utility.BuiltIn.Enum;
namespace MCFBuilder
{
public static class Execute
{
public static string? Namespace { get; set; }
public static string? CurrentFile { get; set; }
private static ArgsOptions Options = new() { };
private static List<string> Commands { get; set; } = new();
[Time($"Compiled code...")]
public static async Task Run(string filePath)
{
var fileContents = await File.ReadAllTextAsync(filePath);
AntlrInputStream inputStream = new AntlrInputStream(fileContents);
var lexer = new MCFBuilderLexer(inputStream);
CommonTokenStream tokens = new CommonTokenStream(lexer);
MCFBuilderParser parser = new MCFBuilderParser(tokens);
parser.AddErrorListener(new SyntaxErrorListener());
var context = parser.program();
FunctionCompiler.Lines = new();
ScriptVisitor visitor = new ScriptVisitor();
visitor.Visit(context);
visitor.FunctionEndAction();
}
[Time("Loaded global variables...")]
private static async Task LoadGlobal(string filePath)
{
var fileContents = await File.ReadAllTextAsync(filePath);
AntlrInputStream inputStream = new AntlrInputStream(fileContents);
var lexer = new MCFBuilderLexer(inputStream);
CommonTokenStream tokens = new CommonTokenStream(lexer);
MCFBuilderParser parser = new MCFBuilderParser(tokens);
ScriptVisitor visitor = new ScriptVisitor();
visitor.Init = true;
await visitor.GlobalAssignment(parser.program());
visitor.Init = false;
}
private static void ErrorListener(object? sender, FirstChanceExceptionEventArgs args)
{
Logging.Error(args.Exception);
}
[Time("Application finished...")]
public static async Task<int> Main(string[] args)
{
//Init
EffectsDatas.Init();
StatisticsCustomTypes.Init();
MinecraftItems.Init();
MinecraftBlocks.Init();
MinecraftEntities.Init();
for (int i = 0; i < args.Length; i++)
{
if (args[i].StartsWith("--") || args[i].StartsWith("-"))
{
switch (args[i][2..^0])
{
case "loglevel":
try
{
Options.LogLevel = int.Parse(args[i + 1]);
Logging.LogLevel = int.Parse(args[i + 1]);
}
catch (Exception e)
{
ErrorMessage.Send($"Invalid value of loglevel: '{args[1]}'");
Logging.Fatal(e);
}
break;
case "version":
try
{
Options.Version = int.Parse(args[i + 1]);
}
catch (Exception e)
{
ErrorMessage.Send($"Invalid value of loglevel: '{args[1]}'");
Logging.Fatal(e);
}
break;
default:
throw new NotImplementedException();
}
i += 1;
continue;
}
else
Commands.Add(args[i]);
}
Logging.Init();
AppDomain.CurrentDomain.FirstChanceException += ErrorListener;
if (args.Length < 1)
{
ErrorMessage.Send("Input arguments must contain at least 1");
return 1;
}
string command = args[0];
if (command == "compile")
{
return await Compile();
}
else if (command == "new")
{
return await New();
}
else
ErrorMessage.Send("Missing Argument: command");
return 0;
}
private async static Task<int> Compile()
{
string[] namespaces = Directory.GetFiles(".", "*.*", SearchOption.AllDirectories)
.Where(v => v.EndsWith(".mcfconfig"))
.ToArray();
if (namespaces.Length == 0)
{
ErrorMessage.Send("Unable to find any working namespaces, please create a new namepsace with 'new' command");
Logging.Error(ErrorType.RuntimeException, "No namepsace avaliable");
return 1;
}
foreach (string ns in namespaces)
{
DatapackData datapackData = JsonConvert.DeserializeObject<DatapackData>(File.ReadAllText(ns));
Namespace = datapackData.Name;
Directory.Delete($"./{datapackData.Name}/data/{datapackData.Name}/functions", true);
Directory.CreateDirectory($"./{datapackData.Name}/data/{datapackData.Name}/functions");
string[] scripts = Directory.GetFiles($"./{datapackData.Name}/scripts", "*.mcf", SearchOption.AllDirectories)
.ToArray();
foreach (string file in scripts)
{
CurrentFile = file;
await LoadGlobal(file);
}
foreach (string file in scripts)
{
CurrentFile = file;
await Run(file);
}
}
return 0;
}
private async static Task<int> New()
{
string name = Commands[1].ToLower();
int? version;
if (Options.Version == null)
{
version = Options.Version;
}
else
{
version = 10;
}
if (Directory.Exists(name))
{
ErrorMessage.Send($"namespace '{name}' is already existed");
return 1;
}
string[] allfiles = Directory.GetFiles(".", "*.*", SearchOption.AllDirectories);
DatapackData datapackData = new DatapackData()
{
Name = name,
Version = version
};
//Create folders
Directory.CreateDirectory(name);
string jsonString = JsonConvert.SerializeObject(datapackData);
File.WriteAllText($"{name}/.mcfconfig", jsonString);
Directory.CreateDirectory($"{name}/data");
await File.WriteAllTextAsync($"{name}/pack.mcmeta", @$"{{
""pack"": {{
""pack_format"": {version},
""description"": ""The default data for Minecraft""
}}
}}");
Directory.CreateDirectory($"{name}/data/{name}/advancements");
Directory.CreateDirectory($"{name}/data/{name}/functions");
Directory.CreateDirectory($"{name}/data/{name}/predicates");
Directory.CreateDirectory($"{name}/data/{name}/loot_tables");
Directory.CreateDirectory($"{name}/data/{name}/dimension");
Directory.CreateDirectory($"{name}/data/{name}/dimension_type");
Directory.CreateDirectory($"{name}/data/{name}/tags");
Directory.CreateDirectory($"{name}/data/{name}/recipes");
Directory.CreateDirectory($"{name}/data/{name}/structures");
Directory.CreateDirectory($"{name}/scripts");
Directory.CreateDirectory($"{name}/data/minecraft/tags");
await File.WriteAllTextAsync($"{name}/data/minecraft/tags/load.json", $@"{{
""values"": [
""{name}:load""
]
}}");
await File.WriteAllTextAsync($"{name}/data/minecraft/tags/tick.json", $@"{{
""values"": [
""{name}:tick""
]
}}");
Console.WriteLine($"created new namespace: {name}");
return 0;
}
}
struct DatapackData
{
public string Name { get; set; }
public int? Version { get; set; }
//public List<string> FilesPath { get; set; }
}
struct ArgsOptions
{
public int? LogLevel { get; set; }
public int? Version { get; set; }
}
}