Jump to content

Recommended Posts

Hi all ! :)

I want to create mod that will cause mining rockets to return only when diamond drill is depleted or storage is full (get them to wait for POI regeneration instead of returning half-empty).

My current understanding of codebase is that rocket is starting to return home because of 
RocketClusterDestinationSelector.cs -> OnStorageChange() triggering it because
RocketClusterDestinationSelector.cs -> CanRocketHarvest() -> statesInstance.CheckIfCanHarvest()
ResourceHarvestModule.cs -> CheckIfCanHarvest() -> !smi.POICanBeHarvested()
HarvestablePOIStates.cs -> POICanBeHarvested() ... being overmined and getting poiCapacity < 0

so i want to pause execution of ResourceHarvestModule.cs -> HarvestFromPOI() when poiCapacity is getting too low

using HarmonyLib;
using System;
using System.Collections.Generic;
using System.Reflection;

namespace ONI_Mod_HarvestFromPOIbyFullBaskets
{
    public class Patches
    {
        //[HarmonyPatch]
        [HarmonyPatch(typeof(ResourceHarvestModule.StatesInstance))]
        [HarmonyPatch(nameof(ResourceHarvestModule.StatesInstance.HarvestFromPOI))]
        public class ResourceHarvestModule_StatesInstance_HarvestFromPOI_Patch
        {
            //public static MethodBase TargetMethod()
            //{

            //    var type = AccessTools.FirstInner(typeof(ResourceHarvestModule), t => t.Name.Contains("StatesInstance"));
            //    Debug.Log("popo2");
            //    return AccessTools.FirstMethod(type, method => method.Name.Contains("HarvestFromPOI"));
            //}
            public static bool Prefix(ResourceHarvestModule.StatesInstance __instance, float dt)
            {
                Debug.Log("I execute before ResourceHarvestModule.StatesInstance.HarvestFromPOI!");
                Clustercraft component = __instance.GetComponent<RocketModuleCluster>().CraftInterface.GetComponent<Clustercraft>();
                if (!__instance.CheckIfCanHarvest())
                    return false;
                ClusterGridEntity atCurrentLocation = component.GetPOIAtCurrentLocation();
                if ((UnityEngine.Object)atCurrentLocation == (UnityEngine.Object)null || (UnityEngine.Object)atCurrentLocation.GetComponent<HarvestablePOIClusterGridEntity>() == (UnityEngine.Object)null)
                    return false;
                HarvestablePOIStates.Instance smi = atCurrentLocation.GetSMI<HarvestablePOIStates.Instance>();
                if (100.0f > smi.poiCapacity)
                    return false;
                return true;
            }
        }
    }

    public class Patches2
    {
        [HarmonyPatch(typeof(Db))]
        [HarmonyPatch("Initialize")]
        public class Db_Initialize_Patch
        {
            public static bool Prefix(Db __instance)
            {
                var type = AccessTools.FirstInner(typeof(ResourceHarvestModule), t => t.Name.Contains("StatesInstance"));
                MethodInfo x = AccessTools.FirstMethod(type, method => method.Name.Contains("HarvestFromPOI"));
                Debug.Log("Popo " + type.Name);
                Debug.Log("Popo " + x.ToString());
                Debug.Log("I execute before Db.Initialize! " + __instance.GetHashCode().ToString());
                return true;
            }

            public static void Postfix()
            {
                Debug.Log("I execute after Db.Initialize!");
            }
        }
    }
}

                if (100.0f > smi.poiCapacity)
                    return false;
is basically whole added code here.
The problem is such Harmony patch is not working / "I execute before ResourceHarvestModule.StatesInstance.HarvestFromPOI!" is not being logged.
"I execute before Db.Initialize! " is logging/working.

I also tried to verify whole idea by hard-disabling method by modifying IL in dnSpy

image.png.bf5d9482e74228f2b826f0910dd2e7e3.pngimage.png.595a7eb6e017e568a60a9df2ec1641e9.png
but the generated .dll differs by 143KB in size ... and is still mining.

Pls help? :) 

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
  • Create New...