static bool Prefix(UnitAbilityResourceCollection __instance, BlueprintScriptableObject blueprint, ref int __result) { var owner = Helpers.GetField <UnitDescriptor>(__instance, "m_Owner"); var connected_resources = owner?.Get <UnitPartConnectResource>()?.getConnectedResource(blueprint as BlueprintAbilityResource); if (connected_resources == null) { return(true); } int val = 0; UnitAbilityResource resource = Harmony12.Traverse.Create(__instance).Method("GetResource", blueprint).GetValue <UnitAbilityResource>(); if (resource != null) { val = resource.Amount; } __result = 0; foreach (var cr in connected_resources) { __result += __instance.GetResourceAmount(cr); } __result = Math.Min(__result, val); return(false); }
static void Postfix(UnitAbilityResourceCollection __instance, BlueprintScriptableObject blueprint, int amount) { var owner = Helpers.GetField <UnitDescriptor>(__instance, "m_Owner"); var connected_resources = owner?.Get <UnitPartConnectResource>()?.getConnectedResource(blueprint as BlueprintAbilityResource); if (connected_resources == null || connected_resources.Length == 0) { return; } connected_resources = connected_resources.OrderByDescending(c => __instance.GetResourceAmount(c)).ToArray(); var max_resource_id = 0; while (amount > 0) { var next_resource_id = (max_resource_id + 1) % connected_resources.Length; if (__instance.GetResourceAmount(connected_resources[next_resource_id]) > __instance.GetResourceAmount(connected_resources[max_resource_id])) { max_resource_id = next_resource_id; } __instance.Spend(connected_resources[max_resource_id], 1); amount--; } }
static bool Prefix(UnitAbilityResourceCollection __instance, BlueprintScriptableObject blueprint, int amount, ref bool __result) { __result = __instance.GetResourceAmount(blueprint) >= amount; return(false); }