Skip to content

Commit

Permalink
etc
Browse files Browse the repository at this point in the history
  • Loading branch information
aedenthorn committed Jun 3, 2021
1 parent 4a0325a commit 8f49015
Show file tree
Hide file tree
Showing 9 changed files with 368 additions and 64 deletions.
140 changes: 81 additions & 59 deletions BuildingDemolish/BepInExPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace BuildingDemolish
{
[BepInPlugin("aedenthorn.BuildingDemolish", "BuildingDemolish", "0.3.2")]
[BepInPlugin("aedenthorn.BuildingDemolish", "Building Demolish", "0.4.0")]
public class BepInExPlugin : BaseUnityPlugin
{
private static readonly bool isDebug = true;
Expand Down Expand Up @@ -57,65 +57,70 @@ private void Update()
{
if (!AedenthornUtils.IgnoreKeyPresses(true) && AedenthornUtils.CheckKeyDown(hotKey.Value))
{
Player player = Player.m_localPlayer;
Dbgl($"Ka-boom");
Collider[] array = Physics.OverlapSphere(player.transform.position, destroyRadius.Value, destroyMask);
for (int i = 0; i < array.Length; i++)
int count = DemolishPieces(destroyRadius.Value);
Dbgl($"Demolished {count} pieces.");
}
}

private static int DemolishPieces(float radius)
{
Player player = Player.m_localPlayer;
if (!player)
return 0;
int count = 0;
Collider[] array = Physics.OverlapSphere(player.transform.position, radius, destroyMask);
for (int i = 0; i < array.Length; i++)
{
Piece piece = array[i].GetComponentInParent<Piece>();
if (piece)
{
Piece piece = array[i].GetComponentInParent<Piece>();
if (piece)
if (!piece.IsCreator() && (piece.GetCreator() != 0 || !allowDestroyUncreated.Value))
{
continue;
}
if (!piece.m_canBeRemoved)
{
continue;
}
if (Location.IsInsideNoBuildLocation(piece.transform.position))
{
continue;
}
if (!PrivateArea.CheckAccess(piece.transform.position, 0f, true, false))
{
continue;
}
if (requireCraftingStation.Value && !Traverse.Create(player).Method("CheckCanRemovePiece", new object[] { piece }).GetValue<bool>())
{
continue;
}
ZNetView component = piece.GetComponent<ZNetView>();
if (component == null)
{
continue;
}
if (!piece.CanBeRemoved())
{
continue;
}
count++;
WearNTear component2 = piece.GetComponent<WearNTear>();
if (component2)
{
component2.Remove();
}
else
{
if (!piece.IsCreator() && (piece.GetCreator() != 0 || !allowDestroyUncreated.Value))
{
continue;
}
if (!piece.m_canBeRemoved)
{
continue;
}
if (Location.IsInsideNoBuildLocation(piece.transform.position))
{
continue;
}
if (!PrivateArea.CheckAccess(piece.transform.position, 0f, true, false))
{
continue;
}
if (requireCraftingStation.Value && !Traverse.Create(player).Method("CheckCanRemovePiece", new object[] { piece }).GetValue<bool>())
{
continue;
}
ZNetView component = piece.GetComponent<ZNetView>();
if (component == null)
{
continue;
}
if (!piece.CanBeRemoved())
{
continue;
}
WearNTear component2 = piece.GetComponent<WearNTear>();
if (component2)
{
component2.Remove();
}
else
{
component.ClaimOwnership();
piece.DropResources();
piece.m_placeEffect.Create(piece.transform.position, piece.transform.rotation, piece.gameObject.transform, 1f);
player.m_removeEffects.Create(piece.transform.position, Quaternion.identity, null, 1f);
ZNetScene.instance.Destroy(piece.gameObject);
}
ItemDrop.ItemData rightItem = player.GetRightItem();
if (rightItem != null)
{
player.FaceLookDirection();
Traverse.Create(player).Field("m_zanim").GetValue<ZSyncAnimation>().SetTrigger(rightItem.m_shared.m_attack.m_attackAnimation);
}
component.ClaimOwnership();
piece.DropResources();
piece.m_placeEffect.Create(piece.transform.position, piece.transform.rotation, piece.gameObject.transform, 1f);
player.m_removeEffects.Create(piece.transform.position, Quaternion.identity, null, 1f);
ZNetScene.instance.Destroy(piece.gameObject);
}

}
}
return count;
}

[HarmonyPatch(typeof(Console), "InputText")]
Expand All @@ -126,14 +131,31 @@ static bool Prefix(Console __instance)
if (!modEnabled.Value)
return true;
string text = __instance.m_input.text;
if (text.ToLower().Equals("buildingdemolish reset"))
if (text.ToLower().Equals($"{typeof(BepInExPlugin).Namespace.ToLower()} reset"))
{
Traverse.Create(__instance).Method("AddString", new object[] { text }).GetValue();
context.Config.Reload();
context.Config.Save();
//if (debugEnabled.Value)
// Player.m_debugMode = true;
Traverse.Create(__instance).Method("AddString", new object[] { $"{context.Info.Metadata.Name} config reloaded" }).GetValue();
return false;
}
if (text.ToLower().Equals($"{typeof(BepInExPlugin).Namespace.ToLower()} demolish"))
{
Traverse.Create(__instance).Method("AddString", new object[] { text }).GetValue();
int count = DemolishPieces(destroyRadius.Value);
Traverse.Create(__instance).Method("AddString", new object[] { $"{context.Info.Metadata.Name} demolished {count} pieces" }).GetValue();
return false;
}
if (text.ToLower().StartsWith($"{typeof(BepInExPlugin).Namespace.ToLower()} demolish "))
{
Traverse.Create(__instance).Method("AddString", new object[] { text }).GetValue();
Traverse.Create(__instance).Method("AddString", new object[] { "config reloaded" }).GetValue();
if(int.TryParse(text.Split(' ')[2], out int radius))
{
int count = DemolishPieces(radius);
Traverse.Create(__instance).Method("AddString", new object[] { $"{context.Info.Metadata.Name} demolished {count} pieces" }).GetValue();
}
else
Traverse.Create(__instance).Method("AddString", new object[] { $"{context.Info.Metadata.Name} syntax error" }).GetValue();
return false;
}
return true;
Expand Down
33 changes: 33 additions & 0 deletions BuildingRepair/AedenthornUtils.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using UnityEngine;

public class AedenthornUtils
{
public static bool IgnoreKeyPresses(bool extra = false)
{
if (!extra)
return ZNetScene.instance == null || Player.m_localPlayer == null || Minimap.IsOpen() || Console.IsVisible() || TextInput.IsVisible() || ZNet.instance.InPasswordDialog() || Chat.instance?.HasFocus() == true;
return ZNetScene.instance == null || Player.m_localPlayer == null || Minimap.IsOpen() || Console.IsVisible() || TextInput.IsVisible() || ZNet.instance.InPasswordDialog() || Chat.instance?.HasFocus() == true || StoreGui.IsVisible() || InventoryGui.IsVisible() || Menu.IsVisible() || TextViewer.instance?.IsVisible() == true;
}
public static bool CheckKeyDown(string value)
{
try
{
return Input.GetKeyDown(value.ToLower());
}
catch
{
return false;
}
}
public static bool CheckKeyHeld(string value, bool req = true)
{
try
{
return Input.GetKey(value.ToLower());
}
catch
{
return !req;
}
}
}
141 changes: 141 additions & 0 deletions BuildingRepair/BepInExPlugin.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
using BepInEx;
using BepInEx.Configuration;
using HarmonyLib;
using System.Reflection;
using UnityEngine;

namespace BuildingRepair
{
[BepInPlugin("aedenthorn.BuildingRepair", "Building Repair", "0.1.1")]
public class BepInExPlugin : BaseUnityPlugin
{
private static readonly bool isDebug = true;
private static BepInExPlugin context;

public static ConfigEntry<bool> modEnabled;
public static ConfigEntry<int> nexusID;

public static ConfigEntry<bool> allowRepairOther;
public static ConfigEntry<bool> requireCraftingStation;
public static ConfigEntry<float> repairRadius;
public static ConfigEntry<string> hotKey;
public static ConfigEntry<string> repairMessage;
public static int destroyMask = LayerMask.GetMask(new string[]
{
"Default",
"static_solid",
"Default_small",
"piece",
"piece_nonsolid",
"terrain",
"vehicle"
});

public static void Dbgl(string str = "", bool pref = true)
{
if (isDebug)
Debug.Log((pref ? typeof(BepInExPlugin).Namespace + " " : "") + str);
}
private void Awake()
{
context = this;
modEnabled = Config.Bind<bool>("General", "Enabled", true, "Enable this mod");
nexusID = Config.Bind<int>("General", "NexusID", 1277, "Nexus mod ID for updates");
repairRadius = Config.Bind<float>("General", "RepairRadius", 20, "Radius of repair");
allowRepairOther = Config.Bind<bool>("General", "AllowRepairOther", false, "Aloow repairing other player's pieces");
requireCraftingStation = Config.Bind<bool>("General", "RequireCraftingStation", true, "Require a nearby crafting station to repair corresponding pieces (this is a vanilla requirement)");
hotKey = Config.Bind<string>("General", "HotKey", "'", "Hotkey to initiate repair");
repairMessage = Config.Bind<string>("General", "RepairMessage", "Repaired {0} pieces.", "Repair message text.");

if (!modEnabled.Value)
return;

//if (debugEnabled.Value)
// Player.m_debugMode = true;

Harmony.CreateAndPatchAll(Assembly.GetExecutingAssembly(), null);
}
private void Update()
{
if (!AedenthornUtils.IgnoreKeyPresses(true) && AedenthornUtils.CheckKeyDown(hotKey.Value))
{
int count = repairPieces(repairRadius.Value);
Dbgl($"Repaired {count} pieces.");
}
}

private static int repairPieces(float radius)
{
Player player = Player.m_localPlayer;
if (!player)
return 0;
int count = 0;
Collider[] array = Physics.OverlapSphere(player.transform.position, radius, destroyMask);
for (int i = 0; i < array.Length; i++)
{
Piece piece = array[i].GetComponentInParent<Piece>();
if (piece)
{
if (!piece.IsCreator() && !allowRepairOther.Value)
{
continue;
}
if (requireCraftingStation.Value && !Traverse.Create(player).Method("CheckCanRemovePiece", new object[] { piece }).GetValue<bool>())
{
continue;
}
ZNetView component = piece.GetComponent<ZNetView>();
if (component == null)
{
continue;
}
WearNTear wnt = piece.GetComponent<WearNTear>();
if (!wnt || !wnt.Repair())
continue;
count++;
}
}
Player.m_localPlayer.Message(MessageHud.MessageType.Center, string.Format(repairMessage.Value, count));
return count;
}

[HarmonyPatch(typeof(Console), "InputText")]
static class InputText_Patch
{
static bool Prefix(Console __instance)
{
if (!modEnabled.Value)
return true;
string text = __instance.m_input.text;
if (text.ToLower().Equals($"{typeof(BepInExPlugin).Namespace.ToLower()} reset"))
{
Traverse.Create(__instance).Method("AddString", new object[] { text }).GetValue();
context.Config.Reload();
context.Config.Save();
Traverse.Create(__instance).Method("AddString", new object[] { $"{context.Info.Metadata.Name} config reloaded" }).GetValue();
return false;
}
if (text.ToLower().Equals($"{typeof(BepInExPlugin).Namespace.ToLower()} repair"))
{
Traverse.Create(__instance).Method("AddString", new object[] { text }).GetValue();
int count = repairPieces(repairRadius.Value);
Traverse.Create(__instance).Method("AddString", new object[] { $"{context.Info.Metadata.Name} repaired {count} pieces" }).GetValue();
return false;
}
if (text.ToLower().StartsWith($"{typeof(BepInExPlugin).Namespace.ToLower()} repair "))
{
Traverse.Create(__instance).Method("AddString", new object[] { text }).GetValue();
if(int.TryParse(text.Split(' ')[2], out int radius))
{
int count = repairPieces(radius);
Traverse.Create(__instance).Method("AddString", new object[] { $"{context.Info.Metadata.Name} repaired {count} pieces" }).GetValue();
}
else
Traverse.Create(__instance).Method("AddString", new object[] { $"{context.Info.Metadata.Name} syntax error" }).GetValue();
return false;
}
return true;
}
}
}
}
6 changes: 6 additions & 0 deletions BuildingRepair/BuildingRepair.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<Version>1.0.0</Version>
</PropertyGroup>
<Import Project="$(SolutionDir)\valheim.targets" />
</Project>
36 changes: 36 additions & 0 deletions BuildingRepair/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("BuildingRepair")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("BuildingRepair")]
[assembly: AssemblyCopyright("Copyright © 2021")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("683cb9ad-97c5-4eea-8e12-80086b3c5380")]

// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
1 change: 1 addition & 0 deletions ConfigurationManager/SettingSearcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public static void CollectSettings(out IEnumerable<SettingEntryBase> results, ou
{
BepInExPlugin.Dbgl($"plugin: {plugin.Info.Metadata.Name} enabled {plugin.enabled}");
}
//BepInExPlugin.Dbgl($"plugin: {plugin.Info.Metadata.Name} enabled {plugin.enabled}");

var type = plugin.GetType();

Expand Down
Loading

0 comments on commit 8f49015

Please sign in to comment.