Extract assembly resource management from UnityHeader to ResourceHelper

This commit is contained in:
Katy Coe
2020-07-20 07:05:20 +02:00
parent 964685e44a
commit ea91c2b4ec
2 changed files with 50 additions and 46 deletions

View File

@@ -34,62 +34,29 @@ namespace Il2CppInspector.Cpp.UnityHeaders
public override string ToString() => Version.ToString(); public override string ToString() => Version.ToString();
// Return the contents of this header file as a string // Return the contents of this header file as a string
public string GetHeaderText() { public string GetHeaderText() => ResourceHelper.GetText(typeof(UnityHeader).Namespace + "." + HeaderFilename);
string resourceName = typeof(UnityHeader).Namespace + "." + HeaderFilename;
Assembly assembly = Assembly.GetCallingAssembly();
using Stream stream = assembly.GetManifestResourceStream(resourceName);
if (stream == null) {
throw new FileNotFoundException(resourceName);
}
using StreamReader reader = new StreamReader(stream);
string result = reader.ReadToEnd();
return result;
}
// List all header files embedded into this build of Il2CppInspector // List all header files embedded into this build of Il2CppInspector
public static IEnumerable<UnityHeader> GetAllHeaders() { public static IEnumerable<UnityHeader> GetAllHeaders() => ResourceHelper.GetNamesForNamespace(typeof(UnityHeader).Namespace)
string prefix = typeof(UnityHeader).Namespace + "."; .Where(s => s.EndsWith(".h"))
Assembly assembly = Assembly.GetExecutingAssembly(); .Select(s => new UnityHeader(s.Substring(typeof(UnityHeader).Namespace.Length + 1)));
return assembly.GetManifestResourceNames()
.Where(s => s.StartsWith(prefix) && s.EndsWith(".h"))
.Select(s => new UnityHeader(s.Substring(prefix.Length)));
}
// List all API header files and versions embedded into this build of Il2CppInspector // List all API header files and versions embedded into this build of Il2CppInspector
public static IEnumerable<(string resourceName, UnityVersion minVersion, UnityVersion maxVersion)> GetAPIList() { public static Dictionary<string, UnityVersionRange> GetAllAPIs() {
string prefix = "Il2CppInspector.Cpp.Il2CppAPIHeaders."; var list = ResourceHelper.GetNamesForNamespace("Il2CppInspector.Cpp.Il2CppAPIHeaders");
Assembly assembly = Assembly.GetExecutingAssembly(); return list.Select(i => new {
var versions = new List<(string resourceName, UnityVersion minVersion, UnityVersion maxVersion)>(); Key = i,
Value = UnityVersionRange.FromFilename(i),
foreach (var headerFilename in assembly.GetManifestResourceNames().Where(s => s.StartsWith(prefix) && s.EndsWith(".h"))) { }).ToDictionary(kv => kv.Key, kv => kv.Value);
var bits = headerFilename.Substring(prefix.Length).Replace(".h", "").Split("-");
var min = new UnityVersion(bits[0]);
UnityVersion max = min;
if (bits.Length == 2 && bits[1] != "")
max = new UnityVersion(bits[1]);
versions.Add((headerFilename, min, max));
}
return versions;
} }
// Get the header file which supports the given version of Unity // Get the header file which supports the given version of Unity
public static UnityHeader GetHeaderForVersion(string version) => GetHeaderForVersion(new UnityVersion(version)); public static UnityHeader GetHeaderForVersion(string version) => GetHeaderForVersion(new UnityVersion(version));
public static UnityHeader GetHeaderForVersion(UnityVersion version) => GetAllHeaders().First(h => h.Version.Contains(version)); public static UnityHeader GetHeaderForVersion(UnityVersion version) => GetAllHeaders().First(h => h.Version.Contains(version));
public static string GetAPIResourceNameForVersion(UnityVersion version) => // Get API file resources
GetAPIList().First(v => version.CompareTo(v.minVersion) >= 0 && (v.maxVersion == null || version.CompareTo(v.maxVersion) <= 0)).resourceName; public static string GetAPIResourceNameForVersion(UnityVersion version) => GetAllAPIs().First(h => h.Value.Contains(version)).Key;
public static string GetAPITextForVersion(UnityVersion version) => ResourceHelper.GetText(GetAPIResourceNameForVersion(version));
public static string GetAPITextForVersion(UnityVersion version) {
var apiResource = GetAPIResourceNameForVersion(version);
Assembly assembly = Assembly.GetCallingAssembly();
using Stream stream = assembly.GetManifestResourceStream(apiResource);
if (stream == null) {
throw new FileNotFoundException(apiResource);
}
using StreamReader reader = new StreamReader(stream);
string result = reader.ReadToEnd();
return result;
}
// Guess which header file(s) correspond to the given metadata+binary. // Guess which header file(s) correspond to the given metadata+binary.
// Note that this may match multiple headers due to structural changes between versions // Note that this may match multiple headers due to structural changes between versions

View File

@@ -0,0 +1,37 @@
/*
Copyright 2017-2020 Katy Coe - http://www.djkaty.com - https://github.com/djkaty
Copyright 2020 Robert Xiao - https://robertxiao.ca
All rights reserved.
*/
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using Il2CppInspector.Cpp.UnityHeaders;
namespace Il2CppInspector
{
internal static class ResourceHelper
{
// Get a string resource
public static string GetText(string resourceName) {
Assembly assembly = Assembly.GetCallingAssembly();
using Stream stream = assembly.GetManifestResourceStream(resourceName);
if (stream == null) {
throw new FileNotFoundException(resourceName);
}
using StreamReader reader = new StreamReader(stream);
string result = reader.ReadToEnd();
return result;
}
// Get a list of resources for a namespace
public static IEnumerable<string> GetNamesForNamespace(string ns) {
Assembly assembly = Assembly.GetExecutingAssembly();
return assembly.GetManifestResourceNames().Where(s => s.StartsWith(ns + "."));
}
}
}