Skip to content

Commit

Permalink
tweaks, and improved logging
Browse files Browse the repository at this point in the history
  • Loading branch information
mattymatty97 committed Sep 2, 2024
1 parent f819a80 commit 7eb6486
Show file tree
Hide file tree
Showing 14 changed files with 346 additions and 392 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
.idea
*.user
packages
Releases
**/bin
**/obj
**/GameDirectory.targets
**/*.dll
ShipLobby.sln.DotSettings.user
83 changes: 0 additions & 83 deletions Plugin/src/Dependency/AsyncLoggerProxy.cs

This file was deleted.

10 changes: 10 additions & 0 deletions Plugin/src/Dependency/LethalConfigProxy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,16 @@ public static void AddConfig(ConfigEntry<int> entry, bool requiresRestart = fals
}));
}

[MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.NoOptimization)]
public static void AddConfig<T>(ConfigEntry<T> entry, bool requiresRestart = false) where T : Enum
{
LethalConfigManager.AddConfigItem(new EnumDropDownConfigItem<T>(entry, new EnumDropDownOptions()
{
RequiresRestart = requiresRestart,
CanModifyCallback = () => (false, "THIS IS A FLAG TYPE ENUM, EDITING CURRENTLY NOT SUPPORTED!")
}));
}

[MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.NoOptimization)]
public static void AddButton(string section, string name, string description, string buttonText, Action callback)
{
Expand Down
26 changes: 17 additions & 9 deletions Plugin/src/MattyFixes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ internal partial class MattyFixes : BaseUnityPlugin
{
public const string GUID = "mattymatty.MattyFixes";
public const string NAME = "Matty's Fixes";
public const string VERSION = "1.1.17";
public const string VERSION = "1.1.18";
internal static ManualLogSource Log;

internal static MattyFixes Instance { get; private set; }
Expand All @@ -32,8 +32,6 @@ private void Awake()
if (LobbyCompatibilityChecker.Enabled)
LobbyCompatibilityChecker.Init();

if (AsyncLoggerProxy.Enabled)
AsyncLoggerProxy.WriteEvent(NAME, "Awake", "Started");

Log.LogInfo("Initializing Configs");

Expand All @@ -44,9 +42,6 @@ private void Awake()
harmony.PatchAll(Assembly.GetExecutingAssembly());

Log.LogInfo(NAME + " v" + VERSION + " Loaded!");

if (AsyncLoggerProxy.Enabled)
AsyncLoggerProxy.WriteEvent(NAME, "Awake", "Finished");

}
catch (Exception ex)
Expand All @@ -55,9 +50,22 @@ private void Awake()
}
}

internal static void VerboseLog(string logmessage)

internal static void VerboseMeshLog(LogLevel logLevel, Func<string> message)
{
if ((PluginConfig.Debug.VerboseMeshes.Value & logLevel) != 0)
Log.Log(logLevel, message());
}

internal static void VerboseCupboardLog(LogLevel logLevel, Func<string> message)
{
if ((PluginConfig.Debug.VerboseCupboard.Value & logLevel) != 0)
Log.Log(logLevel, message());
}

internal static void VerboseItemsLog(LogLevel logLevel, Func<string> message)
{
if (PluginConfig.Debug.Verbose.Value)
Log.LogDebug(logmessage);
if ((PluginConfig.Debug.VerboseItems.Value & logLevel) != 0)
Log.Log(logLevel, message());
}
}
39 changes: 0 additions & 39 deletions Plugin/src/Patches/CruiserFixes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,43 +105,4 @@ private static bool AlternateCruiserParenting()
MattyFixes.PluginConfig.CruiserFixes.AlternateItemDrop.Value;
}

/*
[HarmonyPostfix]
[HarmonyPatch(typeof(VehicleCollisionTrigger), nameof(VehicleCollisionTrigger.OnTriggerEnter))]
private static void OnCruiserTrigger(VehicleCollisionTrigger __instance, Collider other)
{
if (!ShouldSkipCruiserParenting())
return;
if (!other.CompareTag("PhysicProp"))
return;
if (!other.TryGetComponent<GrabbableObject>(out var grabbable))
{
grabbable = other.GetComponentInChildren<GrabbableObject>();
if (grabbable == null)
return;
}
if (grabbable.hasHitGround)
return;
var cruiserT = __instance.transform.root;
if (grabbable.transform.parent == cruiserT)
return;
var startPos = grabbable.transform.parent.TransformPoint(grabbable.startFallingPosition);
var targetPos = grabbable.transform.parent.TransformPoint(grabbable.targetFloorPosition);
var direction = (targetPos - startPos).normalized;
var ray = new Ray(startPos, direction);
if (Physics.Raycast(ray, out var hit, 80f, 1342179585, QueryTriggerInteraction.Ignore))
{
var pos = hit.point + hit.transform.up * (0.04f + grabbable.itemProperties.verticalOffset);
var localpos = cruiserT.InverseTransformPoint(pos);
StartOfRound.Instance.localPlayerController.ThrowObjectServerRpc();
}
}*/
}
22 changes: 10 additions & 12 deletions Plugin/src/Patches/CupBoardFix.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using HarmonyLib;
using Unity.Netcode;
using UnityEngine;
using LogLevel = BepInEx.Logging.LogLevel;
using Object = UnityEngine.Object;

namespace MattyFixes.Patches;
Expand Down Expand Up @@ -80,17 +81,14 @@ private static void OnServerSpawn(GrabbableObject __instance)
var grabbables = Object.FindObjectsOfType<GrabbableObject>();
foreach (var grabbable in grabbables.Where(g => g.isInShipRoom))
{
var offset = 0f;
if (!MattyFixes.PluginConfig.OutOfBounds.Enabled.Value)
if (grabbable.hasHitGround)
offset = grabbable.itemProperties.verticalOffset;
ShelfCheck(grabbable, offset);
ShelfCheck(grabbable);
}
}

private static void ShelfCheck(GrabbableObject grabbable, float offset = 0f)
{
MattyFixes.Log.LogDebug(

MattyFixes.VerboseCupboardLog(LogLevel.Info, () =>
$"{grabbable.itemProperties.itemName}({grabbable.NetworkObjectId}) - Cupboard Triggered!");

if (grabbable is ClipboardItem ||
Expand All @@ -103,7 +101,7 @@ private static void ShelfCheck(GrabbableObject grabbable, float offset = 0f)
{
var pos = grabbable.transform.position + Vector3.down * offset;

MattyFixes.Log.LogDebug(
MattyFixes.VerboseCupboardLog(LogLevel.Debug, () =>
$"{grabbable.itemProperties.itemName}({grabbable.NetworkObjectId}) - Item pos {pos}!");

var closet = Closet;
Expand All @@ -112,7 +110,7 @@ private static void ShelfCheck(GrabbableObject grabbable, float offset = 0f)
PlaceableObjectsSurface found = null;
Vector3? closest = null;

MattyFixes.Log.LogDebug(
MattyFixes.VerboseCupboardLog(LogLevel.Debug, () =>
$"{grabbable.itemProperties.itemName}({grabbable.NetworkObjectId}) - Cupboard pos {closet.Collider.bounds.min}!");

var closetCollider = closet.Collider;
Expand All @@ -123,7 +121,7 @@ private static void ShelfCheck(GrabbableObject grabbable, float offset = 0f)
var hitPoint = shelfHolder.Collider.ClosestPoint(pos);
var tmp = pos.y - hitPoint.y;

MattyFixes.VerboseLog(
MattyFixes.VerboseCupboardLog(LogLevel.Debug, () =>
$"{grabbable.itemProperties.itemName}({grabbable.NetworkObjectId}) - Shelve is {tmp} away!");

if (tmp >= 0 && tmp < distance)
Expand All @@ -134,10 +132,10 @@ private static void ShelfCheck(GrabbableObject grabbable, float offset = 0f)
}
}

MattyFixes.VerboseLog(
MattyFixes.VerboseCupboardLog(LogLevel.Debug, () =>
$"{grabbable.itemProperties.itemName}({grabbable.NetworkObjectId}) - Chosen Shelve is {distance} away!");

MattyFixes.VerboseLog(
MattyFixes.VerboseCupboardLog(LogLevel.Debug, () =>
$"{grabbable.itemProperties.itemName}({grabbable.NetworkObjectId}) - With hitpoint at {closest}!");
}

Expand All @@ -152,7 +150,7 @@ private static void ShelfCheck(GrabbableObject grabbable, float offset = 0f)
transform.parent = closet.gameObject.transform;
transform.position = newPos;
grabbable.targetFloorPosition = transform.localPosition;
MattyFixes.Log.LogDebug(
MattyFixes.VerboseCupboardLog(LogLevel.Info, () =>
$"{grabbable.itemProperties.itemName}({grabbable.NetworkObjectId}) - Pos on shelf {newPos}!");
}
}
Expand Down
87 changes: 87 additions & 0 deletions Plugin/src/Patches/GrabbableStartPatch.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
using System.Collections.Generic;
using System.Linq;
using System.Reflection.Emit;
using BepInEx.Logging;
using HarmonyLib;

namespace MattyFixes.Patches;

[HarmonyPatch]
internal class GrabbableStartPatch
{

[HarmonyTranspiler]
[HarmonyPatch(typeof(GrabbableObject), nameof(GrabbableObject.Start))]
private static IEnumerable<CodeInstruction> RedirectSpawnOnGroundCheck(IEnumerable<CodeInstruction> instructions)
{
var codes = instructions.ToList();

var itemPropertiesFld = AccessTools.Field(typeof(GrabbableObject), nameof(GrabbableObject.itemProperties));
var spawnsOnGroundFld = AccessTools.Field(typeof(Item), nameof(Item.itemSpawnsOnGround));

var replacementMethod = AccessTools.Method(typeof(GrabbableStartPatch), nameof(NewSpawnOnGroundCheck));

var matcher = new CodeMatcher(codes);


matcher.MatchForward(false,
new CodeMatch(OpCodes.Ldarg_0),
new CodeMatch(OpCodes.Ldfld, itemPropertiesFld),
new CodeMatch(OpCodes.Ldfld, spawnsOnGroundFld),
new CodeMatch(OpCodes.Brfalse)
);

if (matcher.IsInvalid)
{
return codes;
}

matcher.Advance(1);

matcher.RemoveInstructions(2);

matcher.Insert(new CodeInstruction(OpCodes.Call, replacementMethod));

MattyFixes.Log.LogDebug("GrabbableObject.Start patched!");

return matcher.Instructions();
}

private static bool NewSpawnOnGroundCheck(GrabbableObject grabbableObject)
{
var ret = grabbableObject.itemProperties.itemSpawnsOnGround;

MattyFixes.VerboseItemsLog(LogLevel.Debug, () =>
$"{grabbableObject.itemProperties.itemName}({grabbableObject.NetworkObjectId}) processing GrabbableObject pos {grabbableObject.transform.position}");

//run normal code if settings are off
if (!MattyFixes.PluginConfig.OutOfBounds.Enabled.Value && !MattyFixes.PluginConfig.CupBoard.Enabled.Value)
return ret;

//or if it's one of the pre-existing items
if (grabbableObject is ClipboardItem ||
(grabbableObject is PhysicsProp && grabbableObject.itemProperties.itemName == "Sticky note"))
return ret;

if (!StartOfRound.Instance.localPlayerController || StartOfRoundPatch._isInitializingGame)
{
if (MattyFixes.PluginConfig.OutOfBounds.Enabled.Value)
{
ret = grabbableObject.IsServer;
}

if (MattyFixes.PluginConfig.CupBoard.Enabled.Value)
{
if (CupBoardFix.Closet.gameObject &&
grabbableObject.transform.parent == CupBoardFix.Closet.gameObject.transform)
ret = false;
}
}

MattyFixes.VerboseItemsLog(LogLevel.Debug, () =>
$"{grabbableObject.itemProperties.itemName}({grabbableObject.NetworkObjectId}) processing GrabbableObject spawnState " +
$"OnGround - was: {grabbableObject.itemProperties.itemSpawnsOnGround} new:{ret}");

return ret;
}
}
Loading

0 comments on commit 7eb6486

Please sign in to comment.