using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using HarmonyLib;
[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyTitle("DigDeeper")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("Crystal")]
[assembly: AssemblyProduct("DigDeeper")]
[assembly: AssemblyCopyright("Copyright © 2023 Crystal Ferrai")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("257c1abb-2713-468a-8321-84c583805faa")]
[assembly: AssemblyFileVersion("1.1.5.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyVersion("1.1.5.0")]
namespace DigDeeper;
[BepInPlugin("dev.crystal.digdeeper", "Dig Deeper", "1.1.5.0")]
[BepInProcess("valheim.exe")]
[BepInProcess("valheim_server.exe")]
public class DigDeeperPlugin : BaseUnityPlugin
{
[HarmonyPatch(typeof(Heightmap))]
private static class Heightmap_Patches
{
private enum TranspilerState
{
Searching,
Replacing
}
[HarmonyPatch("LevelTerrain")]
[HarmonyTranspiler]
private static IEnumerable<CodeInstruction> LevelTerrain_Transpiler(IEnumerable<CodeInstruction> instructions)
{
TranspilerState state = TranspilerState.Searching;
CodeInstruction valueInstruction = null;
foreach (CodeInstruction instruction in instructions)
{
switch (state)
{
case TranspilerState.Searching:
if (instruction.opcode == OpCodes.Ldc_R4 && (float)instruction.operand == 8f)
{
valueInstruction = instruction;
state = TranspilerState.Replacing;
}
else
{
yield return instruction;
}
break;
case TranspilerState.Replacing:
if (instruction.opcode == OpCodes.Sub)
{
valueInstruction.operand = MaximumDepth.Value;
}
else if (instruction.opcode == OpCodes.Add)
{
valueInstruction.operand = MaximumHeight.Value;
}
yield return valueInstruction;
yield return instruction;
valueInstruction = null;
state = TranspilerState.Searching;
break;
}
}
}
}
[HarmonyPatch(typeof(TerrainComp))]
private static class TerrainComp_Patches
{
private enum TranspilerState
{
Searching,
Replacing
}
[HarmonyPatch("ApplyToHeightmap")]
[HarmonyTranspiler]
private static IEnumerable<CodeInstruction> ApplyToHeightmap_Transpiler(IEnumerable<CodeInstruction> instructions)
{
TranspilerState state = TranspilerState.Searching;
CodeInstruction valueInstruction = null;
foreach (CodeInstruction instruction in instructions)
{
switch (state)
{
case TranspilerState.Searching:
if (instruction.opcode == OpCodes.Ldc_R4 && (float)instruction.operand == 8f)
{
valueInstruction = instruction;
state = TranspilerState.Replacing;
}
else
{
yield return instruction;
}
break;
case TranspilerState.Replacing:
if (instruction.opcode == OpCodes.Sub)
{
valueInstruction.operand = MaximumDepth.Value;
}
else if (instruction.opcode == OpCodes.Add)
{
valueInstruction.operand = MaximumHeight.Value;
}
yield return valueInstruction;
yield return instruction;
valueInstruction = null;
state = TranspilerState.Searching;
break;
}
}
}
[HarmonyPatch("LevelTerrain")]
[HarmonyTranspiler]
private static IEnumerable<CodeInstruction> LevelTerrain_Transpiler(IEnumerable<CodeInstruction> instructions)
{
foreach (CodeInstruction instruction in instructions)
{
if (instruction.opcode == OpCodes.Ldc_R4)
{
if ((float)instruction.operand == 8f)
{
instruction.operand = MaximumHeight.Value;
}
else if ((float)instruction.operand == -8f)
{
instruction.operand = 0f - MaximumDepth.Value;
}
}
yield return instruction;
}
}
[HarmonyPatch("RaiseTerrain")]
[HarmonyTranspiler]
private static IEnumerable<CodeInstruction> RaiseTerrain_Transpiler(IEnumerable<CodeInstruction> instructions)
{
foreach (CodeInstruction instruction in instructions)
{
if (instruction.opcode == OpCodes.Ldc_R4)
{
if ((float)instruction.operand == 8f)
{
instruction.operand = MaximumHeight.Value;
}
else if ((float)instruction.operand == -8f)
{
instruction.operand = 0f - MaximumDepth.Value;
}
}
yield return instruction;
}
}
}
public const string ModId = "dev.crystal.digdeeper";
public static ConfigEntry<float> MaximumDepth;
public static ConfigEntry<float> MaximumHeight;
private static Harmony sHeightmapHarmony;
private static Harmony sTerrainCompHarmony;
private void Awake()
{
//IL_007e: Unknown result type (might be due to invalid IL or missing references)
//IL_0088: Expected O, but got Unknown
//IL_00a1: Unknown result type (might be due to invalid IL or missing references)
//IL_00ab: Expected O, but got Unknown
MaximumDepth = ((BaseUnityPlugin)this).Config.Bind<float>("Digging", "MaximumDepth", 20f, "The maximum depth you can dig below the terrain surface. Range 0-128. Game default is 8.");
MaximumDepth.SettingChanged += Config_SettingChanged;
MaximumHeight = ((BaseUnityPlugin)this).Config.Bind<float>("Digging", "MaximumHeight", 8f, "The maximum height you can raise the terrain. Range 0-128. Game default is 8.");
MaximumHeight.SettingChanged += Config_SettingChanged;
ClampConfig();
sHeightmapHarmony = new Harmony("dev.crystal.digdeeper_Heightmap");
sHeightmapHarmony.PatchAll(typeof(Heightmap_Patches));
sTerrainCompHarmony = new Harmony("dev.crystal.digdeeper_TerrainComp");
sTerrainCompHarmony.PatchAll(typeof(TerrainComp_Patches));
}
private void OnDestroy()
{
sHeightmapHarmony.UnpatchSelf();
}
private static void ClampConfig()
{
if (MaximumDepth.Value < 0f)
{
MaximumDepth.Value = 0f;
}
if (MaximumDepth.Value > 128f)
{
MaximumDepth.Value = 128f;
}
if (MaximumHeight.Value < 0f)
{
MaximumHeight.Value = 0f;
}
if (MaximumHeight.Value > 128f)
{
MaximumHeight.Value = 128f;
}
}
private static void Config_SettingChanged(object sender, EventArgs e)
{
ClampConfig();
sHeightmapHarmony.UnpatchSelf();
sHeightmapHarmony.PatchAll(typeof(Heightmap_Patches));
sTerrainCompHarmony.UnpatchSelf();
sTerrainCompHarmony.PatchAll(typeof(TerrainComp_Patches));
}
}