Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support non-static signatures in NativeDetourMethodPatcher, fixes #122. #123

Merged
merged 2 commits into from
Jan 11, 2025

Conversation

CptMoore
Copy link
Contributor

@CptMoore CptMoore commented Dec 31, 2024

Fixes #122

No clue why NativeDetourMethodPatcher was hardcoded to static method signatures, it wasn't even verifying the input.

This PR just switches from

MethodSignature(Original, true);

to

MethodSignature(Original);

The second parameter was ignoreThis, which means only static methods were properly processed when set to true. Now non-static methods are processed in addition to static ones.

Verified on Windows with Unity 2018 and Mono (.NET Framework 4.7.2).

@ds5678
Copy link
Contributor

ds5678 commented Jan 10, 2025

Can you write a unit test to ensure this doesn't occur again?

@CptMoore
Copy link
Contributor Author

Can you write a unit test to ensure this doesn't occur again?

I think that is not possible due to 2 reasons:

  1. There simple does not exists a normal extern method, especially across .NET Framework versions. The closest I found are unsafe String constructors, which I am unwilling to work with as constructor handling is its own can of worms. We patch Unity functions, which are readily available in our game obviously. dotnet runtime: https://github.com/search?q=repo%3Adotnet%2Fruntime+%22public+extern+%22&type=code
  2. I don't know how to create extern methods, especially across platforms, I think I need pinvoke and dlls, so, etc..

@ds5678
Copy link
Contributor

ds5678 commented Jan 10, 2025

I don't know how to create extern methods, especially across platforms

On .NET 8+, you can create unsafe accessors. https://learn.microsoft.com/en-us/dotnet/api/system.runtime.compilerservices.unsafeaccessorattribute

Though, I guess those wouldn't be instance.

@ds5678
Copy link
Contributor

ds5678 commented Jan 10, 2025

My other concern was in regards to the proxies we're creating. I'm not sure they should ever be instance methods. That's part of why I wanted a unit test. I suspect we should still be emitting static methods, just with a @this parameter.

@ManlyMarco ManlyMarco merged commit af69e0f into BepInEx:master Jan 11, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Patching instance extern method fails
3 participants