Repository navigation
Expand file tree
/
Copy pathOperatingSystemLibrary.cs
More file actions
258 lines (234 loc) · 7.32 KB
/
Copy pathOperatingSystemLibrary.cs
File metadata and controls
258 lines (234 loc) · 7.32 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
using Lua.Standard.Internal;
namespace Lua.Standard;
public sealed class OperatingSystemLibrary
{
public static readonly OperatingSystemLibrary Instance = new();
public OperatingSystemLibrary()
{
Functions =
[
new("os", "clock", Clock),
new("os", "date", Date),
new("os", "difftime", DiffTime),
new("os", "execute", Execute),
new("os", "exit", Exit),
new("os", "getenv", GetEnv),
new("os", "remove", Remove),
new("os", "rename", Rename),
new("os", "setlocale", SetLocale),
new("os", "time", Time),
new("os", "tmpname", TmpName),
];
}
public readonly LibraryFunction[] Functions;
public ValueTask<int> Clock(
LuaFunctionExecutionContext context,
CancellationToken cancellationToken
)
{
return new(
context.Return(context.GlobalState.Platform.OsEnvironment.GetTotalProcessorTime())
);
}
public ValueTask<int> Date(
LuaFunctionExecutionContext context,
CancellationToken cancellationToken
)
{
var format = context.HasArgument(0) ? context.GetStringArgument(0).AsSpan() : "%c".AsSpan();
if (format.IsEmpty)
return new(context.Return(""));
var useUtc = format[0] == '!';
if (useUtc)
{
format = format[1..];
}
DateTime now;
if (context.HasArgument(1))
{
var time = context.GetArgument<double>(1);
now = DateTimeHelper.FromUnixTime(time);
if (!useUtc)
{
now = now.ToLocalTime();
}
}
else
{
var provider = context.GlobalState.Platform.TimeProvider;
now = useUtc ? provider.GetUtcNow().DateTime : provider.GetLocalNow().DateTime;
}
var isDst = !useUtc && now.IsDaylightSavingTime();
if (format is "*t")
{
LuaTable table = new()
{
["year"] = now.Year,
["month"] = now.Month,
["day"] = now.Day,
["hour"] = now.Hour,
["min"] = now.Minute,
["sec"] = now.Second,
["wday"] = (int)now.DayOfWeek + 1,
["yday"] = now.DayOfYear,
["isdst"] = isDst,
};
return new(context.Return(table));
}
else
{
return new(context.Return(DateTimeHelper.StrFTime(context.State, format, now)));
}
}
public ValueTask<int> DiffTime(
LuaFunctionExecutionContext context,
CancellationToken cancellationToken
)
{
var t2 = context.GetArgument<double>(0);
var t1 = context.GetArgument<double>(1);
return new(context.Return(t2 - t1));
}
public ValueTask<int> Execute(
LuaFunctionExecutionContext context,
CancellationToken cancellationToken
)
{
// os.execute(command) is not supported
if (context.HasArgument(0))
{
throw new NotSupportedException("os.execute(command) is not supported");
}
else
{
return new(context.Return(false));
}
}
public async ValueTask<int> Exit(
LuaFunctionExecutionContext context,
CancellationToken cancellationToken
)
{
// Ignore 'close' parameter
var exitCode = 0;
if (context.HasArgument(0))
{
var code = context.Arguments[0];
if (code.TryRead<bool>(out var b))
{
exitCode = b ? 0 : 1;
}
else if (code.TryRead<int>(out var d))
{
exitCode = d;
}
else
{
LuaRuntimeException.BadArgument(context.State, 1, LuaValueType.Nil, code.Type);
}
}
await context.GlobalState.Platform.OsEnvironment.Exit(exitCode, cancellationToken);
throw new InvalidOperationException("Unreachable code.. reached.");
}
public ValueTask<int> GetEnv(
LuaFunctionExecutionContext context,
CancellationToken cancellationToken
)
{
var variable = context.GetStringArgument(0);
var platform = context.State.Platform;
var value = platform.OsEnvironment.GetEnvironmentVariable(platform.DecodeText(variable));
return new(context.Return(value is null ? LuaValue.Nil : platform.EncodeText(value)));
}
public async ValueTask<int> Remove(
LuaFunctionExecutionContext context,
CancellationToken cancellationToken
)
{
var fileName = context.GetStringArgument(0);
try
{
await context.GlobalState.Platform.FileSystem.Remove(
context.State.Platform.DecodeText(fileName),
cancellationToken
);
return context.Return(true);
}
catch (IOException ex)
{
return context.Return(
LuaValue.Nil,
context.GlobalState.Platform.EncodeText(ex.Message),
ex.HResult
);
}
}
public async ValueTask<int> Rename(
LuaFunctionExecutionContext context,
CancellationToken cancellationToken
)
{
var oldName = context.GetStringArgument(0);
var newName = context.GetStringArgument(1);
try
{
await context.GlobalState.Platform.FileSystem.Rename(
context.State.Platform.DecodeText(oldName),
context.State.Platform.DecodeText(newName),
cancellationToken
);
return context.Return(true);
}
catch (IOException ex)
{
return context.Return(
LuaValue.Nil,
context.GlobalState.Platform.EncodeText(ex.Message),
ex.HResult
);
}
}
public ValueTask<int> SetLocale(
LuaFunctionExecutionContext context,
CancellationToken cancellationToken
)
{
// os.setlocale is not supported besides the standard "C" POSIX locale
// Any locale besides "C" will return nil
var locale = context.GetStringArgument(0);
if (locale == "C")
{
return new(context.Return(locale));
}
return new(context.Return(LuaValue.Nil));
}
public ValueTask<int> Time(
LuaFunctionExecutionContext context,
CancellationToken cancellationToken
)
{
if (context.HasArgument(0))
{
var table = context.GetArgument<LuaTable>(0);
var date = DateTimeHelper.ParseTimeTable(context.State, table);
return new(context.Return(DateTimeHelper.GetUnixTimeFromLocalTime(date)));
}
else
{
return new(
context.Return(
(double)
context.GlobalState.Platform.TimeProvider.GetUtcNow().ToUnixTimeSeconds()
)
);
}
}
public ValueTask<int> TmpName(
LuaFunctionExecutionContext context,
CancellationToken cancellationToken
)
{
var platform = context.State.Platform;
return new(context.Return(platform.EncodeText(platform.FileSystem.GetTempFileName())));
}
}