-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathCodeResources.cs
More file actions
41 lines (36 loc) · 1.45 KB
/
CodeResources.cs
File metadata and controls
41 lines (36 loc) · 1.45 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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using Serilog;
namespace ModShardLauncher
{
internal static class CodeResources
{
private static readonly Lazy<Dictionary<string, string>> _scripts = new(LoadAllScripts);
public static string GetGML(string name)
{
if (_scripts.Value.TryGetValue(name, out var script)) return script;
throw new ArgumentException($"GML script '{name}' not found");
}
private static Dictionary<string, string> LoadAllScripts()
{
Dictionary<string, string> scripts = new();
Assembly assembly = Assembly.GetExecutingAssembly();
// Load all .gml resources
IEnumerable<string> resourceNames = assembly.GetManifestResourceNames()
.Where(name => name.EndsWith(".gml"));
foreach (string resourceName in resourceNames)
{
string? scriptName = Path.GetFileNameWithoutExtension(resourceName);
using Stream? stream =
assembly.GetManifestResourceStream(resourceName) ??
throw new FileNotFoundException($"GML script '{scriptName}' not found");
using StreamReader reader = new(stream);
scripts[scriptName.Split('.').Last()] = reader.ReadToEnd();
}
return scripts;
}
}
}