-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathFileSystem.cs
More file actions
131 lines (107 loc) · 4.33 KB
/
Copy pathFileSystem.cs
File metadata and controls
131 lines (107 loc) · 4.33 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
using LabApi.Features.Console;
using LabApi.Loader.Features.Paths;
using SER.Code.Extensions;
using SER.Code.FlagSystem;
using SER.Code.Helpers;
using SER.Code.Helpers.ResultSystem;
using SER.Code.ScriptSystem;
using SER.Code.ScriptSystem.Structures;
using EventHandler = SER.Code.EventSystem.EventHandler;
namespace SER.Code.FileSystem;
public static class FileSystem
{
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 void UpdateScriptPathCollection()
{
List<string> paths = [];
paths.AddRange(Directory.GetFiles(MainDirPath, "*.txt", SearchOption.AllDirectories));
paths.AddRange(Directory.GetFiles(MainDirPath, "*.ser", SearchOption.AllDirectories));
RegisteredScriptPaths = paths
// ignore files with a pound sign at the start
.Where(path => Path.GetFileName(path).FirstOrDefault() != '#')
.ToArray();
var duplicates = RegisteredScriptPaths
.Select(Path.GetFileNameWithoutExtension)
.GroupBy(x => x)
.Where(g => g.Count() > 1)
.Select(g => (g.Key, g.Count()))
.ToList();
if (!duplicates.Any()) return;
Logger.Error(
$"There are {string.Join(", ", duplicates.Select(d => $"{d.Item2} scripts named '{d.Key}'"))}\n" +
$"Please rename them to avoid conflicts."
);
RegisteredScriptPaths = RegisteredScriptPaths
.Where(path => !duplicates.Select(d => d.Key).Contains(Path.GetFileNameWithoutExtension(path)))
.ToArray();
}
public static void Initialize()
{
if (!Directory.Exists(MainDirPath))
{
Directory.CreateDirectory(MainDirPath);
return;
}
UpdateScriptPathCollection();
ScriptFlagHandler.Clear();
EventHandler.Clear();
foreach (var scriptPath in RegisteredScriptPaths)
{
var scriptName = ScriptName.CreateUnsafe(Path.GetFileNameWithoutExtension(scriptPath));
if (Script
.CreateByVerifiedPath(scriptPath, ServerConsoleExecutor.Instance)
.GetFlagLines()
.HasErrored(out var error, out var lines))
{
Log.CompileError(scriptName, error);
continue;
}
if (lines.IsEmpty())
{
continue;
}
ScriptFlagHandler.RegisterScript(lines, scriptName);
}
}
public static TryGet<string> GetScriptPath(ScriptName scriptName)
{
UpdateScriptPathCollection();
if (RegisteredScriptPaths.FirstOrDefault(p => Path.GetFileNameWithoutExtension(p) == scriptName) is
{ } path)
{
return path.AsSuccess();
}
return $"Script '{scriptName}' does not exist anymore".AsError();
}
public static bool DoesScriptExistByName(string scriptName, out string path)
{
UpdateScriptPathCollection();
path = RegisteredScriptPaths.FirstOrDefault(p => Path.GetFileNameWithoutExtension(p) == scriptName) ?? "";
return !string.IsNullOrEmpty(path);
}
public static bool DoesScriptExistByPath(string path)
{
UpdateScriptPathCollection();
return RegisteredScriptPaths.Any(p => p == path);
}
public static void GenerateExamples()
{
var examples = ExampleHandler.GetAllExamples();
var exampleDir = Directory.CreateDirectory(Path.Combine(MainDirPath, "Example Scripts"));
foreach (var kvp in examples)
{
var path = Path.Combine(exampleDir.FullName, $"{kvp.Key}.ser");
if (File.Exists(path)) continue;
string? directory = Path.GetDirectoryName(path);
if (!string.IsNullOrEmpty(directory))
{
Directory.CreateDirectory(directory);
}
using var sw = File.CreateText(path);
sw.Write(kvp.Value);
}
}
}