88 lines
3.8 KiB
C#
88 lines
3.8 KiB
C#
using BoneSync.Networking.Messages;
|
|
using BoneSync.Sync;
|
|
using BoneSync.Sync.Components;
|
|
using HarmonyLib;
|
|
using StressLevelZero.Combat;
|
|
using StressLevelZero.Data;
|
|
using StressLevelZero.Props;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Runtime.InteropServices;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using UnityEngine;
|
|
|
|
namespace BoneSync.Patching
|
|
{
|
|
|
|
[HarmonyPatch(typeof(ObjectDestructable))]
|
|
public class ObjectDestructablePatches
|
|
{
|
|
[HarmonyPatch(nameof(ObjectDestructable.TakeDamage))]
|
|
[HarmonyPrefix]
|
|
private static bool TakeDamagePatch(ObjectDestructable __instance, ref Vector3 normal, ref float damage, ref bool crit, ref AttackType attackType)
|
|
{
|
|
if (CallPatchedMethods.allowPatchedMethodCall) return true;
|
|
if (!BoneSync.IsConnected) return true;
|
|
if (damage < 0.05f) return true; // ignore small damage (e.g. a little bit of fall damage)
|
|
//MelonLoader.SyncLogger.Msg("ObjectDestructable.TakeDamage: " + damage);
|
|
Syncable syncable = ObjectSync.MakeOrGetSyncable(__instance.gameObject);
|
|
if (syncable != null)
|
|
{
|
|
if (damage > 0.5f) syncable.RegisterSyncable(); // register syncable if damage is very significant, e.g. a bullet hit
|
|
if (syncable.Registered && !syncable.isOwner) return false;
|
|
//MelonLoader.SyncLogger.Msg("Patch: ObjectDestructable.TakeDamage: " + damage + " " + syncable.gameObject.name);
|
|
ObjectHealthInfo objectHealthInfo = new ObjectHealthInfo(__instance._health, __instance._hits, normal, damage, crit, attackType);
|
|
|
|
ObjectSync.SendObjectDamageMessage(syncable, ObjectDamageType.DestructibleTakeDamage, objectHealthInfo);
|
|
|
|
return false; // prevent networked objects from taking damage locally
|
|
}
|
|
return true;
|
|
}
|
|
|
|
/*// patch the getter for lootTable to return null if the object is networked
|
|
[HarmonyPatch(nameof(ObjectDestructable.lootTable), MethodType.Getter)]
|
|
[HarmonyPrefix]
|
|
private static bool LootTablePatch(ObjectDestructable __instance, ref LootTableData __result)
|
|
{
|
|
if (!BoneSync.IsConnected) return true;
|
|
Syncable syncable = ObjectSync.MakeOrGetSyncable(__instance.gameObject);
|
|
if (syncable != null && !BoneSync.lobby.IsHost)
|
|
{
|
|
__result = null;
|
|
return false;
|
|
}
|
|
return true;
|
|
}*/
|
|
}
|
|
|
|
[HarmonyPatch(typeof(Prop_Health))]
|
|
public class PropHealthPatches
|
|
{
|
|
[HarmonyPatch(nameof(Prop_Health.TAKEDAMAGE))]
|
|
[HarmonyPrefix]
|
|
private static bool TakeDamagePatch(Prop_Health __instance, ref float damage, ref bool crit, ref AttackType attackType)
|
|
{
|
|
if (CallPatchedMethods.allowPatchedMethodCall) return true;
|
|
if (!BoneSync.IsConnected) return true;
|
|
//MelonLoader.SyncLogger.Msg("Prop_Health.TAKEDAMAGE: " + damage);
|
|
Syncable syncable = ObjectSync.MakeOrGetSyncable(__instance.gameObject);
|
|
if (syncable != null)
|
|
{
|
|
if (damage > 0.5f) syncable.RegisterSyncable();
|
|
if (syncable.Registered && !syncable.isOwner) return false;
|
|
//MelonLoader.SyncLogger.Msg("Patch: Prop_Health.TAKEDAMAGE: " + damage + " " + syncable.gameObject.name);
|
|
ObjectHealthInfo objectHealthInfo = new ObjectHealthInfo(__instance.cur_Health, __instance.hits, Vector3.zero, damage, crit, attackType);
|
|
|
|
ObjectSync.SendObjectDamageMessage(syncable, ObjectDamageType.PropHealthTakeDamage, objectHealthInfo);
|
|
|
|
return false; // prevent networked objects from taking damage locally
|
|
}
|
|
return true;
|
|
}
|
|
|
|
}
|
|
}
|