samedi 23 juillet 2022

Accessing Struct Constructor Values or Creating new Struct Properties at Runtme

I am using BepInEx, which is a derivative of Harmony, to patch a game's assembly at runtime.

The original struct I want to modify looks like this:

public struct OriginalStruct
{
    
    public OriginalStruct(SomeClass someClass)
    {
        this.Property = someClass.SomeClassProperty1;
    }

    public float Property;

I am able to modify the constructor itself, and add variables that access properties from SomeClass. However, I need to create public properties of these new variables in other for other patched methods in external classes I have modified to be able to access them via these new properties.

The patched struct will look like this:

public struct OriginalStruct
{
    
    public OriginalStruct(SomeClass someClass)
    {
        this.Property = someClass.SomeClassProperty1;
        this.MyProperty= someClass.SomeClassProperty2;

    }

    public float Property;

    public float MyProperty;

The original method that accesses OriginalStruct looks like this:

public void SomeOriginalMethod(ref OriginalStuct originalStuct)
{
 float someVariable = originalStruct.Property
}

I want to patch it to look like this:

public void PatchedSomeOriginalMethod(ref OriginalStuct originalStuct)
{
 float someVariable = originalStruct.Property
 float myVariable = originalStruct.MyProperty
}

So the issue is finding some way of accessing struct constructor values directly, or accessing its instance of SomeClass so that I can access its property from within SomeOriginalMethod.

The only alternative I see is to add properties to OriginalStuct.

My understanding is that this is beyond the limits of runtime patching with the CIL. I've seen suggestions of subclassing the original class/struct via System.Reflection.Emit but I haven't found any good, understandable examples that does what I'm trying to do here specifically. I'm open to any ideas on how to achieve this, and I'm looking for solid example code.





Aucun commentaire:

Enregistrer un commentaire