-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathFileSystem.cs
More file actions
211 lines (176 loc) · 7.77 KB
/
Copy pathFileSystem.cs
File metadata and controls
211 lines (176 loc) · 7.77 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
using LabApi.Features.Console;
using LabApi.Loader.Features.Paths;
using SER.Code.Extensions;
using SER.Code.Helpers;
using SER.Code.Helpers.ResultSystem;
using SER.Code.ScriptSystem;
using SER.Code.ScriptSystem.Structures;
namespace SER.Code.FileSystem;
public static class FileSystem
{
public readonly record struct ExampleGenerationSummary(int Created, int AlreadyExisted, string DirectoryPath);
public static readonly string MainDirPath = Path.Combine(PathManager.Configs.FullName, "Scripted Events Reloaded");
public static readonly string DbDirPath = Path.Combine(MainDirPath, "Databases");
public static readonly string ConfigsDirPath = Path.Combine(MainDirPath, "Custom Configs");
public static string[] RegisteredScriptPaths = [];
public static string[] DisabledScriptPaths = [];
public static IReadOnlyDictionary<string, string[]> DuplicateScriptPaths { get; private set; } =
new Dictionary<string, string[]>(StringComparer.OrdinalIgnoreCase);
public static TryGet<string> GetContainedPath(string rootDirectory, string name, string extension)
{
if (string.IsNullOrWhiteSpace(name))
{
return TryGet<string>.Error("A file name cannot be empty.");
}
try
{
var root = Path.GetFullPath(rootDirectory)
.TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar);
var path = Path.GetFullPath(Path.Combine(root, name + extension));
var rootPrefix = root + Path.DirectorySeparatorChar;
var pathComparison = Path.DirectorySeparatorChar == '\\'
? StringComparison.OrdinalIgnoreCase
: StringComparison.Ordinal;
if (!path.StartsWith(rootPrefix, pathComparison))
{
return TryGet<string>.Error($"Path '{name}' resolves outside the SER data directory.");
}
return path.AsSuccess();
}
catch (Exception ex) when (ex is ArgumentException or NotSupportedException or PathTooLongException)
{
return TryGet<string>.Error($"Path '{name}' is invalid: {ex.Message}");
}
}
public static void UpdateScriptPathCollection(bool logDuplicateErrors = true)
{
List<string> paths = [];
paths.AddRange(Directory.GetFiles(MainDirPath, "*.txt", SearchOption.AllDirectories));
paths.AddRange(Directory.GetFiles(MainDirPath, "*.ser", SearchOption.AllDirectories));
DisabledScriptPaths = paths
.Where(path => Path.GetFileName(path).StartsWith("#", StringComparison.Ordinal))
.OrderBy(path => path, StringComparer.OrdinalIgnoreCase)
.ToArray();
RegisteredScriptPaths = paths
// ignore files with a pound sign at the start
.Where(path => !Path.GetFileName(path).StartsWith("#", StringComparison.Ordinal))
.ToArray();
DuplicateScriptPaths = RegisteredScriptPaths
.GroupBy(Path.GetFileNameWithoutExtension, StringComparer.OrdinalIgnoreCase)
.Where(g => g.Count() > 1)
.ToDictionary(
group => group.Key,
group => group.OrderBy(path => path, StringComparer.OrdinalIgnoreCase).ToArray(),
StringComparer.OrdinalIgnoreCase);
foreach (var duplicate in DuplicateScriptPaths.Where(_ => logDuplicateErrors))
{
Logger.Error(
$"SER found {duplicate.Value.Length} scripts named '{duplicate.Key}'. " +
"Script names must be globally unique, so none of these files were loaded:\n" +
string.Join("\n", duplicate.Value.Select(path => $"> {path}")) +
"\nRename all but one of these files, then run 'serreload'."
);
}
if (DuplicateScriptPaths.Count == 0) return;
RegisteredScriptPaths = RegisteredScriptPaths
.Where(path => !DuplicateScriptPaths.ContainsKey(Path.GetFileNameWithoutExtension(path)))
.ToArray();
}
public static void Initialize()
{
if (!Directory.Exists(MainDirPath))
{
Directory.CreateDirectory(MainDirPath);
}
ScriptCatalog.Initialize();
}
public static ScriptCatalog.RefreshSummary RefreshAll(bool force = false) => ScriptCatalog.RefreshAll(force);
public static ScriptCatalog.RequestedRefreshResult RefreshRequested(ScriptName name) =>
ScriptCatalog.RefreshRequested(name);
public static void Shutdown() => ScriptCatalog.Shutdown();
public static TryGet<ScriptSection[]> GetScriptSections(string path)
{
try
{
return ScriptSection.Split(Path.GetFileNameWithoutExtension(path), File.ReadAllText(path), path);
}
catch (Exception exception) when (exception is IOException or UnauthorizedAccessException)
{
return $"Failed to read script '{path}': {exception.Message}";
}
}
public static TryGet<ScriptSection> GetScriptSection(ScriptName scriptName)
{
return ScriptCatalog.GetSection(scriptName);
}
public static TryGet<string> GetScriptPath(ScriptName scriptName)
{
return ScriptCatalog.GetPath(scriptName);
}
public static bool DoesScriptExistByName(string scriptName, out string path)
{
if (GetScriptSection(ScriptName.CreateUnsafe(scriptName)).HasErrored(out _, out var section))
{
path = "";
return false;
}
path = section.Path ?? "";
return true;
}
public static bool DoesScriptExistByPath(string path)
{
UpdateScriptPathCollection();
return RegisteredScriptPaths.Any(p => string.Equals(p, path, StringComparison.OrdinalIgnoreCase));
}
public static bool IsScriptOrFileName(Script script, string name)
{
return string.Equals(script.Name.ToString(), name, StringComparison.CurrentCultureIgnoreCase)
|| string.Equals(script.FileName.ToString(), name, StringComparison.CurrentCultureIgnoreCase);
}
internal static void ParseSectionSelector(string requestedName, out string fileName, out int? sectionNumber)
{
var separator = requestedName.LastIndexOf(':');
if (separator > 0
&& int.TryParse(requestedName[(separator + 1)..], out var parsed)
&& parsed > 0)
{
fileName = StripScriptExtension(Path.GetFileName(requestedName[..separator]));
sectionNumber = parsed;
return;
}
fileName = StripScriptExtension(Path.GetFileName(requestedName));
sectionNumber = null;
}
private static string StripScriptExtension(string name)
{
return name.EndsWith(".ser", StringComparison.OrdinalIgnoreCase)
|| name.EndsWith(".txt", StringComparison.OrdinalIgnoreCase)
? name[..^4]
: name;
}
public static ExampleGenerationSummary GenerateExamples()
{
var examples = ExampleHandler.GetAllExamples();
var exampleDir = Directory.CreateDirectory(Path.Combine(MainDirPath, "Example Scripts"));
var created = 0;
var alreadyExisted = 0;
foreach (var kvp in examples)
{
var path = Path.Combine(exampleDir.FullName, $"#{kvp.Key}.ser");
if (File.Exists(path))
{
alreadyExisted++;
continue;
}
string? directory = Path.GetDirectoryName(path);
if (!string.IsNullOrEmpty(directory))
{
Directory.CreateDirectory(directory);
}
using var sw = File.CreateText(path);
sw.Write(kvp.Value);
created++;
}
return new ExampleGenerationSummary(created, alreadyExisted, exampleDir.FullName);
}
}