LuaInjectionStation.cs 1.8 KB
Newer Older
timchen1002's avatar
update  
timchen1002 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
using System;
using System.Collections;
using System.Collections.Generic;

namespace LuaInterface
{
    [Flags]
    public enum InjectType
    {
        None = 0,
        After = 1,
        Before = 1 << 1,
        Replace = 1 << 2,
        ReplaceWithPreInvokeBase = 1 << 3,
        ReplaceWithPostInvokeBase = 1 << 4
    }

    public class LuaInjectionStation
    {
        public const byte NOT_INJECTION_FLAG = 0;
        public const byte INVALID_INJECTION_FLAG = byte.MaxValue;

        static int cacheSize;
        static byte[] injectionFlagCache;
        static LuaFunction[] injectFunctionCache;

        static LuaInjectionStation()
        {
            injectionFlagCache = new byte[cacheSize];
            injectFunctionCache = new LuaFunction[cacheSize];
        }

        [NoToLua]
        public static byte GetInjectFlag(int index)
        {
            byte result = injectionFlagCache[index];

            if (result == INVALID_INJECTION_FLAG)
            {
                return NOT_INJECTION_FLAG;
            }
            else if (result == NOT_INJECTION_FLAG)
            {
                /// Delay injection not supported
                if (LuaState.GetInjectInitState(index))
                {
                    injectionFlagCache[index] = INVALID_INJECTION_FLAG;
                }
            }

            return result;
        }

        [NoToLua]
        public static LuaFunction GetInjectionFunction(int index)
        {
            return injectFunctionCache[index];
        }

        public static void CacheInjectFunction(int index, byte injectFlag, LuaFunction func)
        {
            if (index >= cacheSize)
            {
                return;
            }

            injectFunctionCache[index] = func;
            injectionFlagCache[index] = injectFlag;
        }
    }
}