Comments and minor refactoring into lambdas

This commit is contained in:
Robert Xiao
2020-06-21 01:21:36 -07:00
committed by Katy
parent 3ccbab2461
commit 95ee085374
4 changed files with 70 additions and 9 deletions

View File

@@ -9,16 +9,22 @@ using System.Reflection;
using System.IO;
using System.Linq;
using System.Collections.Generic;
using System;
namespace Il2CppInspector.Outputs.UnityHeaders
{
// Each instance of UnityHeader represents one header file which potentially covers multiple versions of Unity.
public class UnityHeader
{
// Metadata version of this header. Multiple headers may have the same metadata version
public double MetadataVersion { get; }
// Minimum and maximum Unity version numbers corresponding to this header. Both endpoints are inclusive
public UnityVersion MinVersion { get; }
public UnityVersion MaxVersion { get; }
// Filename for the embedded .h resource file containing the header
public string HeaderFilename { get; }
private UnityHeader(string headerFilename) {
HeaderFilename = headerFilename;
var bits = headerFilename.Replace(".h", "").Split("-");
@@ -39,10 +45,10 @@ namespace Il2CppInspector.Outputs.UnityHeaders
return res;
}
public bool Contains(UnityVersion version) {
return version.CompareTo(MinVersion) >= 0 && (MaxVersion == null || version.CompareTo(MaxVersion) <= 0);
}
// Determine if this header supports the given version of Unity
public bool Contains(UnityVersion version) => version.CompareTo(MinVersion) >= 0 && (MaxVersion == null || version.CompareTo(MaxVersion) <= 0);
// Return the contents of this header file as a string
public string GetHeaderText() {
string resourceName = typeof(UnityHeader).Namespace + "." + HeaderFilename;
Assembly assembly = Assembly.GetCallingAssembly();
@@ -55,6 +61,7 @@ namespace Il2CppInspector.Outputs.UnityHeaders
return result;
}
// List all header files embedded into this build of Il2Cpp
public static IEnumerable<UnityHeader> GetAllHeaders() {
string prefix = typeof(UnityHeader).Namespace + ".";
Assembly assembly = Assembly.GetCallingAssembly();
@@ -63,11 +70,13 @@ namespace Il2CppInspector.Outputs.UnityHeaders
.Select(s => new UnityHeader(s.Substring(prefix.Length)));
}
// 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(UnityVersion version) {
return GetAllHeaders().Where(v => v.Contains(version)).First();
}
public static UnityHeader GetHeaderForVersion(UnityVersion version) => GetAllHeaders().Where(v => v.Contains(version)).First();
// Guess which header file(s) correspond to the given metadata+binary.
// Note that this may match multiple headers due to structural changes between versions
// that are not reflected in the metadata version.
public static List<UnityHeader> GuessHeadersForModel(Reflection.Il2CppModel model) {
List<UnityHeader> result = new List<UnityHeader>();
foreach (var v in GetAllHeaders()) {

View File

@@ -10,8 +10,10 @@ using System.Text.RegularExpressions;
namespace Il2CppInspector.Outputs.UnityHeaders
{
// Parsed representation of a Unity version number, such as 5.3.0f1 or 2019.3.7.
public class UnityVersion : IComparable<UnityVersion>, IEquatable<UnityVersion>
{
// A sorted enumeration of build types, in order of maturity
public enum BuildTypeEnum
{
Unspecified,
@@ -44,6 +46,7 @@ namespace Il2CppInspector.Outputs.UnityHeaders
_ => throw new ArgumentException("Unknown build type " + s),
};
// Unity version number is of the form <Major>.<Minor>.<Update>[<BuildType><BuildNumber>]
public int Major { get; }
public int Minor { get; }
public int Update { get; }
@@ -70,6 +73,7 @@ namespace Il2CppInspector.Outputs.UnityHeaders
return res;
}
// Compare two version numbers, intransitively (due to the Unspecified build type)
public int CompareTo(UnityVersion other) {
int res;
if (0 != (res = Major.CompareTo(other.Major)))