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

增加个快捷Hook的类 #24

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

tylearymf
Copy link

目前使用上来,hook一些多重载的方法的时候会有些麻烦,所以才有意搞了这个类

举个例子,下面只需要传入 hook的类型和方法名,则可自动搜索需要hook的目标类型与当前类型中的方法,两者方法签名一致的就会自动hook住

[InitializeOnLoad]
public class LogFilter
{
    static QuickHook hook;

    static LogFilter()
    {
        hook = new QuickHook(typeof(LogFilter));
        hook.HookMethod(Debug.unityLogger.GetType(), "Log");
        hook.HookMethod(Debug.unityLogger.GetType(), "LogFormat");
    }

    [MethodImpl(MethodImplOptions.NoOptimization)]
    static void Log(ILogger logger, LogType logType, string tag, object message, Object context)
    {
        if (logType == LogType.Log)
        {
            var msg = message?.ToString();
            if (context == null && string.IsNullOrEmpty(tag) && !string.IsNullOrEmpty(msg))
            {
                // 测试数据
                if (msg.Contains("test"))
                    LogProxy(logger, logType, tag, message, context);
            }
        }
        else
        {
            LogProxy(logger, logType, tag, message, context);
        }
    }
    [MethodImpl(MethodImplOptions.NoOptimization)]
    static void LogProxy(ILogger logger, LogType logType, string tag, object message, Object context) { }


    [MethodImpl(MethodImplOptions.NoOptimization)]
    static void Log(ILogger logger, LogType logType, object message)
    {
        Log(logger, logType, null, message, null);
    }
    [MethodImpl(MethodImplOptions.NoOptimization)]
    static void LogProxy(ILogger logger, LogType logType, object message) { }


    [MethodImpl(MethodImplOptions.NoOptimization)]
    static void Log(ILogger logger, LogType logType, object message, Object context)
    {
        Log(logger, logType, null, message, context);
    }
    [MethodImpl(MethodImplOptions.NoOptimization)]
    static void LogProxy(ILogger logger, LogType logType, object message, Object context) { }


    [MethodImpl(MethodImplOptions.NoOptimization)]
    static void Log(ILogger logger, string tag, object message)
    {
        Log(logger, LogType.Log, tag, message, null);
    }
    [MethodImpl(MethodImplOptions.NoOptimization)]
    static void LogProxy(ILogger logger, string tag, object message) { }


    [MethodImpl(MethodImplOptions.NoOptimization)]
    static void LogFormat(ILogger logger, LogType logType, string format, params object[] args)
    {
        Log(logger, logType, null, string.Format(format, args), null);
    }
    [MethodImpl(MethodImplOptions.NoOptimization)]
    static void LogFormatProxy(ILogger logger, LogType logType, string format, params object[] args) { }
}

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.

1 participant