Example #1
0
 private void DefaultRemovalHook(object sender, HookedSimpleCacheEventArgs <TKey> e)
 {
     Remove(e.Target);
     if (HookedSimpleCacheState.NeedsCleanup(typeof(THookTarget)))
     {
         HookedSimpleCacheState.Cleanup(typeof(THookTarget));
     }
 }
Example #2
0
 internal static void PostfixHook(THookTarget __instance)
 {
     foreach (var cache in HookedSimpleCacheState.GetRegisteredCaches(typeof(THookTarget)))
     {
         var key = cache.ConvertTargetToKey(__instance);
         cache.OnHookPostfix(new HookedSimpleCacheEventArgs <TKey>((TKey)key));
     }
 }
Example #3
0
 private void DefaultRemovalHook(HookedSimpleCache <TKey, TValue, THookTarget> sender, HookedSimpleCacheEventArgs <TKey> e)
 {
     Assert.IsNotNull(this);
     sender.Remove(e.Target);
     if (HookedSimpleCacheState.NeedsCleanup(typeof(THookTarget)))
     {
         HookedSimpleCacheState.Cleanup(typeof(THookTarget));
     }
 }
Example #4
0
        public HookedSimpleCache(CacheDataLoader loader, HookConverter converter, bool useDefaultRemovalHook = false,
                                 bool emptyCacheOnSceneChange = false) : base(loader)
        {
            _convertTargetToKey     = converter;
            EmptyCacheOnSceneChange = emptyCacheOnSceneChange;
            UseDefaultRemovalHook   = useDefaultRemovalHook;

            var hookTarget = AccessTools.Method(typeof(THookTarget), "OnDestroy", new Type[0]);

            if (hookTarget is null)
            {
                throw new ArgumentException($"unable to hook OnDestroy for {typeof(THookTarget)})");
            }

            var prefix  = new HarmonyMethod(GetType(), nameof(PrefixHook));
            var postfix = new HarmonyMethod(GetType(), nameof(PostfixHook));

            if (prefix is null)
            {
                throw new Exception("Unable to wrap prefix");
            }

            if (postfix is null)
            {
                throw new Exception("Unable to wrap postfix");
            }

            var harmony = Harmony.CreateAndPatchAll(GetType());

            harmony.Patch(hookTarget, prefix, postfix);

            SceneManager.activeSceneChanged += SceneManager_activeSceneChanged;

            if (UseDefaultRemovalHook)
            {
                HookPrefix += DefaultRemovalHook;
            }

            HookedSimpleCacheState.RegisterCache(typeof(THookTarget), this);
        }