Files
2026-08-27 10:56:38 -06:00

74 lines
2.2 KiB
C#

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
namespace PureCrack.Util;
internal static class EmbeddedAssets
{
private static readonly Assembly Asm = typeof(EmbeddedAssets).Assembly;
private const string Prefix = "PureCrack.assets.";
private static Dictionary<string, string>? _innerSources;
private static string? _loader;
private static byte[]? _pbNet;
private static byte[]? _canned;
public static IReadOnlyDictionary<string, string> InnerSources
{
get
{
if (_innerSources != null)
{
return _innerSources;
}
Dictionary<string, string> dictionary = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
string[] manifestResourceNames = Asm.GetManifestResourceNames();
foreach (string text in manifestResourceNames)
{
if (text.StartsWith("PureCrack.assets.inner.", StringComparison.Ordinal) && text.EndsWith(".cs", StringComparison.Ordinal))
{
string key = text.Substring("PureCrack.assets.".Length + "inner.".Length);
dictionary[key] = ReadString(text);
}
}
return _innerSources = dictionary;
}
}
public static string LoaderTemplate => _loader ?? (_loader = ReadString("PureCrack.assets.inner.Loader.tmpl"));
public static byte[] ProtobufNetDll => _pbNet ?? (_pbNet = ReadBytes("PureCrack.assets.inner.protobuf-net.dll"));
public static byte[] CannedCompileResponse => _canned ?? (_canned = ReadBytes("PureCrack.assets.compile_response.bin"));
private static string ReadString(string resName)
{
using Stream stream = Asm.GetManifestResourceStream(resName) ?? throw new FileNotFoundException("missing embedded resource: " + resName);
using StreamReader streamReader = new StreamReader(stream, Encoding.UTF8);
return streamReader.ReadToEnd();
}
private static byte[] ReadBytes(string resName)
{
using Stream stream = Asm.GetManifestResourceStream(resName) ?? throw new FileNotFoundException("missing embedded resource: " + resName);
using MemoryStream memoryStream = new MemoryStream();
stream.CopyTo(memoryStream);
return memoryStream.ToArray();
}
public static IEnumerable<string> AllResources()
{
return from n in Asm.GetManifestResourceNames()
orderby n
select n;
}
}