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

Invalid memory read unmarshaling parameters for patched empty method #839

Open
ShroudedNight opened this issue Apr 21, 2024 · 0 comments
Open
Labels
bug Something isn't working

Comments

@ShroudedNight
Copy link

ShroudedNight commented Apr 21, 2024

What happened?

Modding Shadows of Doubt (latest experimental as of submission):
Attempting to patch the empty method [1] Actor.SelectedDebug(string, Actor.HumanDebug) yields invalid memory reads.

Yields the following profligacy of errors:
https://gist.github.com/ShroudedNight/34023da6a8145bbb5f35190cb2a38da9

[1] Game supplies a mono build for aiding in mod engineering. In that version, the method is empty.

Steps to reproduce

[BepInPlugin(MyPluginInfo.PLUGIN_GUID, MyPluginInfo.PLUGIN_NAME, MyPluginInfo.PLUGIN_VERSION)]
public class Plugin : BasePlugin
{
    private static PluginRuntime Runtime { get; set; }

    public override void Load()
    {
        try
        {
            Runtime = new PluginRuntime(Log);
            var harmony = new Harmony($"{MyPluginInfo.PLUGIN_GUID}");
            harmony.PatchAll();
        }
        catch (Exception e)
        {
            Log.FATAL($"Unable to load ${MyPluginInfo.PLUGIN_GUID}: {e}");
            throw;
        }
        Log.INFO($"Plugin {MyPluginInfo.PLUGIN_GUID} is loaded!");
    }

    [HarmonyPatch(typeof(Actor), nameof(Actor.SelectedDebug), [typeof(string), typeof(Actor.HumanDebug)])]
    public class Actor_SelectedDebug
    {
        public static bool Prefix(Actor __instance, string str, Actor.HumanDebug debug)
        {
            // Just a means of accessing the ManualLogSource
            Runtime.Log(LogLevel.Info, debug switch
            {
                Actor.HumanDebug.movement => $"Movement: {str}",
                Actor.HumanDebug.actions => $"Actions: {str}",
                Actor.HumanDebug.attacks => $"Attacks: {str}",
                Actor.HumanDebug.updates => $"Updates: {str}",
                Actor.HumanDebug.misc => $"Misc: {str}",
                Actor.HumanDebug.sight => $"Sight: {str}",
                _ => $"OutOfRange: {debug}"
            });
            return true;
        }

        public static void PostFix()
        {
            Runtime.Log(LogLevel.Info, "Actor.SelectedDebug(string, Actor.HumanDebug) => PostFix");
        }
    }

    [HarmonyPatch(typeof(Il2CppInterop.Runtime.IL2CPP), nameof(Il2CppInterop.Runtime.IL2CPP.Il2CppStringToManaged),
        [typeof(IntPtr)])]
    public class DebugStringConversion
    {
        public static unsafe bool Prefix(IntPtr il2CppString, ref string __result)
        {
            if (il2CppString == IntPtr.Zero)
            {
                __result = null;
                return false;
            }
            var length = IL2CPP.il2cpp_string_length(il2CppString);
            __result = length >= 0 ? new string(IL2CPP.il2cpp_string_chars(il2CppString), 0, length) : $"{il2CppString.ToInt64()}: {length}";
            return false;
        }
    }
}

BepInEx Distribution

3rd Party [Thunderstore]

Log outputs

Too long, See gist here: https://gist.github.com/ShroudedNight/34023da6a8145bbb5f35190cb2a38da9

Environment

- OS: Windows 10
- BepInEx: BepInExPack_IL2CPP v6.0.667 by BepInEx
- Game: Shadows of Doubt [latest experimental]
@ShroudedNight ShroudedNight added the bug Something isn't working label Apr 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant