GithubHelp home page GithubHelp logo

Comments (13)

pardeike avatar pardeike commented on May 12, 2024 1

This is my test case. Done in VS2017, with .NET 4.6.1 and Release build:

using Harmony;
using System;

namespace HarmonyConsoleApp
{
	class Program
	{
		static void Main(string[] args)
		{
			var a = new Test();

			var harmony = HarmonyInstance.Create("com.company.project.product");
			var original = typeof(Test).GetMethod("Call");
			var prefix = typeof(Patch).GetMethod("Before");
			var postfix = typeof(Patch).GetMethod("After");
			harmony.Patch(original, new HarmonyMethod(prefix), new HarmonyMethod(postfix));

			var b = new Test();

			Console.WriteLine("a=" + a.Call());
			Console.WriteLine("b=" + b.Call());
			Console.ReadKey();
		}
	}

	public class Test
	{
		// if Call() is too simple, it will get inlined and no patching is ever possible
		/*
		public string Call()
		{
			return "ORIGINAL";
		}*/

		public string Call()
		{
			var c = new char[] { 'O', 'R', 'I', 'G', 'I', 'N', 'A', 'L' };
			return string.Join("", c);
		}
	}

	public class Patch
	{
		public static bool Before(ref object __result)
		{
			Console.WriteLine("Before..");
			__result = "PATCHED";
			return false;
		}
		public static void After()
		{
			Console.WriteLine("After");
		}
	}
}

from harmony.

halittiryaki avatar halittiryaki commented on May 12, 2024

Seems to be a .NET 4+ issue. Works with 3.5. Any quickfix ideas?

from harmony.

pardeike avatar pardeike commented on May 12, 2024

Did you test with the master version?

from harmony.

halittiryaki avatar halittiryaki commented on May 12, 2024

yes. just cloned again.

and release mode seems to be not supported at all?

from harmony.

pardeike avatar pardeike commented on May 12, 2024

The release is currently for unity (mono) only. Iā€™m working on incorporating the pull requests to make an new master.

from harmony.

halittiryaki avatar halittiryaki commented on May 12, 2024

so next master branch will support release mode and .net 4+?

from harmony.

halittiryaki avatar halittiryaki commented on May 12, 2024

kinda sorted it out. The master branch downloaded as zip doesn't include the AccessViolation Fix. It doesn't even contain the NativeLibrary.cs.
Seems like it was reverted by hcoona.

however, git clone .. gets a master branch with the fix included, but still not solving the AccessViolation Exception on my Windows 10 x64, with .NET 4.0+ versions.

from harmony.

pardeike avatar pardeike commented on May 12, 2024

Maybe this is related to the content of your patch? Usually, access violations appear when patching, not during runtime. Or you are running this in a specific environment that enforces access rules more aggressively. Can you provide more details?

from harmony.

halittiryaki avatar halittiryaki commented on May 12, 2024

this is the content of my patch / target class, in a clean Console app project just attached to the Harmony solution:

public class Test
    {
        public string Call()
        {
            return "asd";
        }
    }

    public class Patch
    {
        public static bool Before(ref object __result)
        {
            Console.WriteLine("Before..");
            __result = "asdfa";
            return false;
        }
        public static void After()
        {
            Console.WriteLine("After");
        }
    }

the difference which makes this patch work is either:

  • in .Net 4+; have all new Test() instantiations before patching
  • or .Net 3.5
  • all .net versions; only Debug mode support (release mode completely ignoring patch, no exceptions either. the original target method being invoked as if there was no patch at all.)

my environment is windows 10 x64 (no vbox) with VS 2015.

that enforces access rules more aggressively

where to look further?

thanks

from harmony.

halittiryaki avatar halittiryaki commented on May 12, 2024

trying public static bool Before(ref string __result) just crashed the whole OS with a bluescreen

from harmony.

pardeike avatar pardeike commented on May 12, 2024

It should not. The prefix is called from within the main method as a subroutine and the ref argument is put on the call stack as an ordinary IL code. All you should get is a an exception from the CIL execution which cannot drag your binary down like you describe.

from harmony.

halittiryaki avatar halittiryaki commented on May 12, 2024

yes it shouldn't normally. happened once strangely.. cant get it there again without code change

from harmony.

pardeike avatar pardeike commented on May 12, 2024

Cannot reproduce with latest version

from harmony.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    šŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. šŸ“ŠšŸ“ˆšŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ā¤ļø Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.