Some additions to ReflectionCache source generator

This commit is contained in:
YoshiRulz 2021-11-28 03:29:47 +10:00
parent 9224a4a81c
commit 0a4bbce3f2
No known key found for this signature in database
GPG Key ID: C4DE31C245353FB7
2 changed files with 13 additions and 1 deletions

View File

@ -64,15 +64,19 @@ namespace BizHawk.SrcGen.ReflectionCache
var src = $@"#nullable enable
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
{(nSpace == "BizHawk.Common" ? string.Empty : "\nusing BizHawk.Common;")}
using BizHawk.Common.StringExtensions;
namespace {nSpace}
{{
public static class ReflectionCache
{{
private const string EMBED_PREFIX = ""{nSpace}."";
private static Type[]? _types = null;
private static readonly Assembly Asm = typeof({nSpace}.ReflectionCache).Assembly;
@ -81,11 +85,19 @@ namespace {nSpace}
public static Type[] Types => _types ??= Asm.GetTypesWithoutLoadErrors().ToArray();
public static IEnumerable<string> EmbeddedResourceList(string extraPrefix)
{{
var fullPrefix = EMBED_PREFIX + extraPrefix;
return Asm.GetManifestResourceNames().Where(s => s.StartsWith(fullPrefix)).Select(s => s.RemovePrefix(fullPrefix));
}}
public static IEnumerable<string> EmbeddedResourceList()
=> EmbeddedResourceList(string.Empty);
/// <exception cref=""ArgumentException"">not found</exception>
public static Stream EmbeddedResourceStream(string embedPath)
{{
var fullPath = $""{nSpace}.{{embedPath}}"";
var fullPath = EMBED_PREFIX + embedPath;
return Asm.GetManifestResourceStream(fullPath) ?? throw new ArgumentException($""resource at {{fullPath}} not found"", nameof(embedPath));
}}
}}