forked from aedenthorn/ValheimMods
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4a0325a
commit 8f49015
Showing
9 changed files
with
368 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.