Skip to content
This repository was archived by the owner on Jun 27, 2024. It is now read-only.

Commit ff4d8c6

Browse files
committed
Create Reflection wrappers
1 parent 5c49eaf commit ff4d8c6

8 files changed

Lines changed: 207 additions & 0 deletions
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using System.Reflection;
2+
3+
namespace OpenMod.Installer.RocketMod.Helpers.Wrapper
4+
{
5+
public class NuGetConsoleLoggerWrapper : TypeWrapper
6+
{
7+
public NuGetConsoleLoggerWrapper(Assembly nugetAssembly) : base(nugetAssembly, "NuGetConsoleLogger")
8+
{ }
9+
}
10+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
using System.Reflection;
2+
using System.Threading.Tasks;
3+
4+
namespace OpenMod.Installer.RocketMod.Helpers.Wrapper
5+
{
6+
public class NuGetPackageManagerWrapper : TypeWrapper
7+
{
8+
protected static PropertyInfo m_Logger;
9+
protected static MethodInfo m_IgnoreDependencies;
10+
protected static MethodInfo m_GetLatestPackageIdentityAsync;
11+
protected static MethodInfo m_QueryPackageExactAsync;
12+
protected static MethodInfo m_InstallAsync;
13+
14+
public NuGetPackageManagerWrapper(Assembly nugetAssembly, params object[] args) : base(nugetAssembly, "NuGetPackageManager", args)
15+
{
16+
m_Logger ??= s_Type.GetProperty("Logger");
17+
m_IgnoreDependencies ??= s_Type.GetMethod("IgnoreDependencies");
18+
m_GetLatestPackageIdentityAsync ??= s_Type.GetMethod("GetLatestPackageIdentityAsync");
19+
m_QueryPackageExactAsync ??= s_Type.GetMethod("QueryPackageExactAsync");
20+
m_InstallAsync ??= s_Type.GetMethod("InstallAsync");
21+
}
22+
23+
public void SetLogger(NuGetConsoleLoggerWrapper logger)
24+
{
25+
m_Logger.SetValue(m_Instance, logger.Instance);
26+
}
27+
28+
public void IgnoreDependencies(params string[] packageIds)
29+
{
30+
m_IgnoreDependencies.Invoke(m_Instance, new object[] { packageIds });
31+
}
32+
33+
public async Task<PackageIdentityWrapper> GetLatestPackageIdentityAsync(string packageId)
34+
{
35+
var taskObj = m_GetLatestPackageIdentityAsync.Invoke(m_Instance, new object[] { packageId }) as Task;
36+
var taskWrapper = new TaskWrapper<PackageIdentityWrapper>(taskObj);
37+
return await taskWrapper.GetResult();
38+
}
39+
40+
public async Task<PackageSearchMetadataWrapper> QueryPackageExactAsync(string packageId, string version = null, bool includePreRelease = false)
41+
{
42+
var taskObj = m_QueryPackageExactAsync.Invoke(m_Instance, new object[] { packageId, version, includePreRelease }) as Task;
43+
var taskWrapper = new TaskWrapper<PackageSearchMetadataWrapper>(taskObj);
44+
return await taskWrapper.GetResult();
45+
}
46+
47+
public async Task<NuGetInstallResultWrapper> InstallAsync(PackageIdentityWrapper packageIdentity, bool allowPreReleaseVersions = false)
48+
{
49+
var taskObj = m_InstallAsync.Invoke(m_Instance, new[] { packageIdentity.Instance, allowPreReleaseVersions }) as Task;
50+
var taskWrapper = new TaskWrapper<NuGetInstallResultWrapper>(taskObj);
51+
return await taskWrapper.GetResult();
52+
}
53+
}
54+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace OpenMod.Installer.RocketMod.Helpers.Wrapper
2+
{
3+
public class NuGetVersionWrapper : TypeWrapper
4+
{
5+
public NuGetVersionWrapper(object instance) : base(instance)
6+
{ }
7+
}
8+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using System.Reflection;
2+
3+
namespace OpenMod.Installer.RocketMod.Helpers.Wrapper
4+
{
5+
public class NuGetInstallResultWrapper : TypeWrapper
6+
{
7+
protected static PropertyInfo m_Code;
8+
9+
public int Code => (int)m_Code.GetValue(m_Instance);
10+
11+
public NuGetInstallResultWrapper(object instance) : base(instance)
12+
{
13+
m_Code = s_Type.GetProperty("Code");
14+
}
15+
}
16+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System.Reflection;
2+
3+
namespace OpenMod.Installer.RocketMod.Helpers.Wrapper
4+
{
5+
public class PackageIdentityWrapper : TypeWrapper
6+
{
7+
protected static FieldInfo m_Id;
8+
protected static FieldInfo m_Version;
9+
10+
public string Id => m_Id.GetValue(m_Instance) as string;
11+
12+
public bool HasVersion
13+
{
14+
get
15+
{
16+
var version = m_Version.GetValue(m_Instance);
17+
return version != null;
18+
}
19+
}
20+
21+
public NuGetVersionWrapper Version
22+
{
23+
get
24+
{
25+
var version = m_Version.GetValue(m_Instance);
26+
return version == null ? null : new NuGetVersionWrapper(version);
27+
}
28+
}
29+
30+
public PackageIdentityWrapper(object instance) : base(instance)
31+
{
32+
m_Id = s_Type.GetField("_id", BindingFlags.NonPublic | BindingFlags.Instance);
33+
m_Version = s_Type.GetField("_version", BindingFlags.NonPublic | BindingFlags.Instance);
34+
}
35+
}
36+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using System.Reflection;
2+
3+
namespace OpenMod.Installer.RocketMod.Helpers.Wrapper
4+
{
5+
public class PackageSearchMetadataWrapper : TypeWrapper
6+
{
7+
protected static PropertyInfo m_Identity;
8+
9+
public PackageIdentityWrapper Identity
10+
{
11+
get
12+
{
13+
var identity = m_Identity.GetValue(m_Instance);
14+
return new PackageIdentityWrapper(identity);
15+
}
16+
}
17+
18+
public PackageSearchMetadataWrapper(object instance) : base(instance)
19+
{
20+
m_Identity = s_Type.GetProperty("Identity");
21+
}
22+
}
23+
}

src/Helpers/Wrapper/TaskWrapper.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using System;
2+
using System.Reflection;
3+
using System.Threading.Tasks;
4+
5+
namespace OpenMod.Installer.RocketMod.Helpers.Wrapper
6+
{
7+
public class TaskWrapper<TResult>
8+
{
9+
protected PropertyInfo m_Result;
10+
protected Task m_Instance;
11+
12+
public Task Instance => m_Instance;
13+
14+
public async Task<TResult> GetResult()
15+
{
16+
await m_Instance;
17+
18+
var resultObj = m_Result.GetValue(m_Instance);
19+
return resultObj switch
20+
{
21+
null => default,
22+
TResult result => result,
23+
_ => (TResult)Activator.CreateInstance(typeof(TResult), resultObj)
24+
};
25+
}
26+
27+
public TaskWrapper(Task instance)
28+
{
29+
m_Instance = instance;
30+
m_Result = instance.GetType().GetProperty("Result");
31+
}
32+
}
33+
}

src/Helpers/Wrapper/TypeWrapper.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using System;
2+
using System.Linq;
3+
using System.Reflection;
4+
5+
namespace OpenMod.Installer.RocketMod.Helpers.Wrapper
6+
{
7+
public abstract class TypeWrapper
8+
{
9+
protected Type s_Type;
10+
11+
protected object m_Instance;
12+
13+
public object Instance => m_Instance;
14+
15+
protected TypeWrapper(object instance)
16+
{
17+
m_Instance = instance;
18+
s_Type = instance.GetType();
19+
}
20+
21+
protected TypeWrapper(Assembly nugetAssembly, string typeName, params object[] args)
22+
{
23+
s_Type = nugetAssembly.GetTypes().First(tp => tp.Name.Equals(typeName, StringComparison.OrdinalIgnoreCase));
24+
m_Instance = Activator.CreateInstance(s_Type, args);
25+
}
26+
}
27+
}

0 commit comments

Comments
 (0)