using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Configuration;
using HarmonyLib;
using UnityEngine;
using UnityEngine.Rendering;
[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyTitle("MassFarming")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("MassFarming")]
[assembly: AssemblyCopyright("Copyright © 2021")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("e1da477d-c14c-4355-be8f-c043e789791a")]
[assembly: AssemblyFileVersion("1.5.2")]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.5.2.0")]
[module: UnverifiableCode]
namespace MassFarming;
[BepInPlugin("EardwulfDoesMods.Comfy.MassFarming", "Comfy.MassFarming", "1.5.2")]
public class MassFarming : BaseUnityPlugin
{
[HarmonyPatch]
public class MassPlant
{
private static FieldInfo m_placementGhostField = AccessTools.Field(typeof(Player), "m_placementGhost");
private static FieldInfo m_buildPiecesField = AccessTools.Field(typeof(Player), "m_buildPieces");
private static MethodInfo _GetRightItemMethod = AccessTools.Method(typeof(Humanoid), "GetRightItem", (Type[])null, (Type[])null);
private static Vector3 placedPosition;
private static Quaternion placedRotation;
private static Piece placedPiece;
private static bool placeSuccessful = false;
private static int _plantSpaceMask = LayerMask.GetMask(new string[5] { "Default", "static_solid", "Default_small", "piece", "piece_nonsolid" });
private static GameObject[] _placementGhosts = (GameObject[])(object)new GameObject[1];
private static Piece _fakeResourcePiece;
[HarmonyPostfix]
[HarmonyPatch(typeof(Player), "TryPlacePiece")]
public static void PlacePiecePostfix(Player __instance, ref bool __result, Piece piece)
{
//IL_0016: Unknown result type (might be due to invalid IL or missing references)
//IL_001b: Unknown result type (might be due to invalid IL or missing references)
//IL_0021: Unknown result type (might be due to invalid IL or missing references)
//IL_0026: Unknown result type (might be due to invalid IL or missing references)
//IL_0030: Unknown result type (might be due to invalid IL or missing references)
//IL_0035: Unknown result type (might be due to invalid IL or missing references)
placeSuccessful = __result;
if (__result)
{
GameObject val = (GameObject)m_placementGhostField.GetValue(__instance);
placedPosition = val.transform.position;
placedRotation = val.transform.rotation;
placedPiece = piece;
}
}
[HarmonyPrefix]
[HarmonyPatch(typeof(Player), "UpdatePlacement")]
public static void UpdatePlacementPrefix(bool takeInput, float dt)
{
placeSuccessful = false;
}
[HarmonyPostfix]
[HarmonyPatch(typeof(Player), "UpdatePlacement")]
public static void UpdatePlacementPostfix(Player __instance, bool takeInput, float dt)
{
//IL_0021: Unknown result type (might be due to invalid IL or missing references)
//IL_0074: Unknown result type (might be due to invalid IL or missing references)
//IL_007a: Unknown result type (might be due to invalid IL or missing references)
//IL_0046: Unknown result type (might be due to invalid IL or missing references)
//IL_004b: Unknown result type (might be due to invalid IL or missing references)
//IL_004e: Unknown result type (might be due to invalid IL or missing references)
//IL_005f: Unknown result type (might be due to invalid IL or missing references)
//IL_0064: Unknown result type (might be due to invalid IL or missing references)
//IL_0067: Unknown result type (might be due to invalid IL or missing references)
//IL_0091: Unknown result type (might be due to invalid IL or missing references)
//IL_0096: Unknown result type (might be due to invalid IL or missing references)
//IL_00b1: Unknown result type (might be due to invalid IL or missing references)
//IL_00b6: Unknown result type (might be due to invalid IL or missing references)
//IL_00a5: Unknown result type (might be due to invalid IL or missing references)
//IL_0121: Unknown result type (might be due to invalid IL or missing references)
//IL_0141: Unknown result type (might be due to invalid IL or missing references)
//IL_0143: Unknown result type (might be due to invalid IL or missing references)
//IL_0178: Unknown result type (might be due to invalid IL or missing references)
//IL_017a: Unknown result type (might be due to invalid IL or missing references)
if (!placeSuccessful)
{
return;
}
Plant component = ((Component)placedPiece).gameObject.GetComponent<Plant>();
if (!Object.op_Implicit((Object)(object)component))
{
return;
}
Heightmap val = Heightmap.FindHeightmap(placedPosition);
if (!Object.op_Implicit((Object)(object)val))
{
return;
}
if (!PluginConfig.NoActionKeyNeeded.Value)
{
KeyboardShortcut value = PluginConfig.ControllerPickupHotkey.Value;
if (!Input.GetKey(((KeyboardShortcut)(ref value)).MainKey))
{
value = PluginConfig.MassActionHotkey.Value;
if (!Input.GetKey(((KeyboardShortcut)(ref value)).MainKey))
{
return;
}
}
}
foreach (Vector3 item in BuildPlantingGridPositions(placedPosition, component, placedRotation))
{
if ((placedPiece.m_cultivatedGroundOnly && !val.IsCultivated(item)) || placedPosition == item)
{
continue;
}
object? obj = _GetRightItemMethod.Invoke(__instance, Array.Empty<object>());
ItemData val2 = (ItemData)((obj is ItemData) ? obj : null);
if (!((Character)__instance).HaveStamina((val2.m_shared.m_attack.m_attackStamina + ((Character)__instance).GetEquipmentHomeItemModifier()) * 1.7695f))
{
Hud.instance.StaminaBarUppgradeFlash();
break;
}
if (!__instance.HaveRequirements(placedPiece, (RequirementMode)1))
{
break;
}
if (HasGrowSpace(item, ((Component)placedPiece).gameObject))
{
GameObject val3 = Object.Instantiate<GameObject>(((Component)placedPiece).gameObject, item, placedRotation);
Piece component2 = val3.GetComponent<Piece>();
if (Object.op_Implicit((Object)(object)component2))
{
component2.SetCreator(__instance.GetPlayerID());
}
placedPiece.m_placeEffect.Create(item, placedRotation, val3.transform, 1f, -1);
Game.instance.IncrementPlayerStat((PlayerStatType)2, 1f);
((Character)__instance).RaiseSkill((SkillType)106, 1f);
__instance.ConsumeResources(placedPiece.m_resources, 0, -1, 1);
((Character)__instance).UseStamina((val2.m_shared.m_attack.m_attackStamina + ((Character)__instance).GetEquipmentHomeItemModifier()) * 1.7695f);
val2.m_durability -= val2.m_shared.m_useDurabilityDrain;
if (val2.m_durability <= 0f)
{
break;
}
}
}
}
private static List<Vector3> BuildPlantingGridPositions(Vector3 originPos, Plant placedPlant, Quaternion rotation)
{
//IL_0028: Unknown result type (might be due to invalid IL or missing references)
//IL_0029: Unknown result type (might be due to invalid IL or missing references)
//IL_002e: Unknown result type (might be due to invalid IL or missing references)
//IL_0034: Unknown result type (might be due to invalid IL or missing references)
//IL_0039: Unknown result type (might be due to invalid IL or missing references)
//IL_003a: Unknown result type (might be due to invalid IL or missing references)
//IL_003b: Unknown result type (might be due to invalid IL or missing references)
//IL_0040: Unknown result type (might be due to invalid IL or missing references)
//IL_0046: Unknown result type (might be due to invalid IL or missing references)
//IL_004b: Unknown result type (might be due to invalid IL or missing references)
//IL_004c: Unknown result type (might be due to invalid IL or missing references)
//IL_004d: Unknown result type (might be due to invalid IL or missing references)
//IL_005b: Unknown result type (might be due to invalid IL or missing references)
//IL_005d: Unknown result type (might be due to invalid IL or missing references)
//IL_006b: Unknown result type (might be due to invalid IL or missing references)
//IL_0070: Unknown result type (might be due to invalid IL or missing references)
//IL_0075: Unknown result type (might be due to invalid IL or missing references)
//IL_0083: Unknown result type (might be due to invalid IL or missing references)
//IL_0085: Unknown result type (might be due to invalid IL or missing references)
//IL_0093: Unknown result type (might be due to invalid IL or missing references)
//IL_0098: Unknown result type (might be due to invalid IL or missing references)
//IL_009d: Unknown result type (might be due to invalid IL or missing references)
//IL_00a4: Unknown result type (might be due to invalid IL or missing references)
//IL_00a6: Unknown result type (might be due to invalid IL or missing references)
//IL_00b4: Unknown result type (might be due to invalid IL or missing references)
//IL_00c1: Unknown result type (might be due to invalid IL or missing references)
//IL_00c8: Unknown result type (might be due to invalid IL or missing references)
//IL_00ca: Unknown result type (might be due to invalid IL or missing references)
//IL_00cb: Unknown result type (might be due to invalid IL or missing references)
//IL_00d0: Unknown result type (might be due to invalid IL or missing references)
//IL_00e6: Unknown result type (might be due to invalid IL or missing references)
//IL_00e8: Unknown result type (might be due to invalid IL or missing references)
//IL_00e9: Unknown result type (might be due to invalid IL or missing references)
//IL_00ee: Unknown result type (might be due to invalid IL or missing references)
float num = placedPlant.m_growRadius * 2f;
List<Vector3> list = new List<Vector3>(PluginConfig.PlantGridWidth.Value * PluginConfig.PlantGridLength.Value);
Vector3 val = rotation * Vector3.left * num;
Vector3 val2 = rotation * Vector3.forward * num;
Vector3 val3 = originPos;
if (PluginConfig.GridAnchorLength.Value)
{
val3 -= val2 * (float)(PluginConfig.PlantGridLength.Value / 2);
}
if (PluginConfig.GridAnchorWidth.Value)
{
val3 -= val * (float)(PluginConfig.PlantGridWidth.Value / 2);
}
for (int i = 0; i < PluginConfig.PlantGridLength.Value; i++)
{
Vector3 val4 = val3;
for (int j = 0; j < PluginConfig.PlantGridWidth.Value; j++)
{
val4.y = ZoneSystem.instance.GetGroundHeight(val4);
list.Add(val4);
val4 += val;
}
val3 += val2;
}
return list;
}
private static bool HasGrowSpace(Vector3 newPos, GameObject go)
{
//IL_000a: Unknown result type (might be due to invalid IL or missing references)
Plant component = go.GetComponent<Plant>();
if (component != null)
{
return Physics.OverlapSphere(newPos, component.m_growRadius, _plantSpaceMask).Length == 0;
}
return true;
}
[HarmonyPostfix]
[HarmonyPatch(typeof(Player), "SetupPlacementGhost")]
public static void SetupPlacementGhostPostfix(Player __instance)
{
DestroyGhosts();
}
[HarmonyPostfix]
[HarmonyPatch(typeof(Player), "UpdatePlacementGhost")]
public static void UpdatePlacementGhostPostfix(Player __instance, bool flashGuardStone)
{
//IL_000b: Unknown result type (might be due to invalid IL or missing references)
//IL_0011: Expected O, but got Unknown
//IL_0039: Unknown result type (might be due to invalid IL or missing references)
//IL_003e: Unknown result type (might be due to invalid IL or missing references)
//IL_0042: Unknown result type (might be due to invalid IL or missing references)
//IL_0053: Unknown result type (might be due to invalid IL or missing references)
//IL_0058: Unknown result type (might be due to invalid IL or missing references)
//IL_005c: Unknown result type (might be due to invalid IL or missing references)
//IL_0116: Unknown result type (might be due to invalid IL or missing references)
//IL_0128: Unknown result type (might be due to invalid IL or missing references)
//IL_0134: Unknown result type (might be due to invalid IL or missing references)
//IL_014c: Unknown result type (might be due to invalid IL or missing references)
//IL_0151: Unknown result type (might be due to invalid IL or missing references)
//IL_0159: Unknown result type (might be due to invalid IL or missing references)
//IL_015e: Unknown result type (might be due to invalid IL or missing references)
//IL_01a5: Unknown result type (might be due to invalid IL or missing references)
//IL_01bf: Unknown result type (might be due to invalid IL or missing references)
//IL_01f7: Unknown result type (might be due to invalid IL or missing references)
//IL_01e9: Unknown result type (might be due to invalid IL or missing references)
GameObject val = (GameObject)m_placementGhostField.GetValue(__instance);
if (!Object.op_Implicit((Object)(object)val) || !val.activeSelf)
{
SetGhostsActive(active: false);
return;
}
if (!PluginConfig.NoActionKeyNeeded.Value)
{
KeyboardShortcut value = PluginConfig.ControllerPickupHotkey.Value;
if (!Input.GetKey(((KeyboardShortcut)(ref value)).MainKey))
{
value = PluginConfig.MassActionHotkey.Value;
if (!Input.GetKey(((KeyboardShortcut)(ref value)).MainKey))
{
SetGhostsActive(active: false);
return;
}
}
}
Plant component = val.GetComponent<Plant>();
if (!Object.op_Implicit((Object)(object)component))
{
SetGhostsActive(active: false);
return;
}
if (!EnsureGhostsBuilt(__instance))
{
SetGhostsActive(active: false);
return;
}
Requirement val2 = ((IEnumerable<Requirement>)val.GetComponent<Piece>().m_resources).FirstOrDefault((Func<Requirement, bool>)((Requirement r) => Object.op_Implicit((Object)(object)r.m_resItem) && r.m_amount > 0));
_fakeResourcePiece.m_resources[0].m_resItem = val2.m_resItem;
_fakeResourcePiece.m_resources[0].m_amount = val2.m_amount;
float num = __instance.GetStamina();
object? obj = _GetRightItemMethod.Invoke(__instance, Array.Empty<object>());
ItemData val3 = (ItemData)((obj is ItemData) ? obj : null);
Heightmap val4 = Heightmap.FindHeightmap(val.transform.position);
List<Vector3> list = BuildPlantingGridPositions(val.transform.position, component, val.transform.rotation);
for (int i = 0; i < _placementGhosts.Length; i++)
{
Vector3 val5 = list[i];
if (val.transform.position == val5)
{
_placementGhosts[i].SetActive(false);
continue;
}
Requirement obj2 = _fakeResourcePiece.m_resources[0];
obj2.m_amount += val2.m_amount;
_placementGhosts[i].transform.position = val5;
_placementGhosts[i].transform.rotation = val.transform.rotation;
_placementGhosts[i].SetActive(true);
bool invalidPlacementHeightlight = false;
if (val.GetComponent<Piece>().m_cultivatedGroundOnly && !val4.IsCultivated(val5))
{
invalidPlacementHeightlight = true;
}
else if (!HasGrowSpace(val5, val.gameObject))
{
invalidPlacementHeightlight = true;
}
else if (num < val3.m_shared.m_attack.m_attackStamina * 1.7999f)
{
Hud.instance.StaminaBarUppgradeFlash();
invalidPlacementHeightlight = true;
}
else if (!__instance.HaveRequirements(_fakeResourcePiece, (RequirementMode)1))
{
invalidPlacementHeightlight = true;
}
num -= val3.m_shared.m_attack.m_attackStamina * 1.7999f;
_placementGhosts[i].GetComponent<Piece>().SetInvalidPlacementHeightlight(invalidPlacementHeightlight);
}
}
private static bool EnsureGhostsBuilt(Player player)
{
int num = PluginConfig.PlantGridWidth.Value * PluginConfig.PlantGridLength.Value;
if (!Object.op_Implicit((Object)(object)_placementGhosts[0]) || _placementGhosts.Length != num)
{
DestroyGhosts();
if (_placementGhosts.Length != num)
{
_placementGhosts = (GameObject[])(object)new GameObject[num];
}
object? value = m_buildPiecesField.GetValue(player);
PieceTable val = (PieceTable)((value is PieceTable) ? value : null);
if (val != null)
{
GameObject selectedPrefab = val.GetSelectedPrefab();
if (selectedPrefab != null)
{
if (selectedPrefab.GetComponent<Piece>().m_repairPiece)
{
return false;
}
for (int i = 0; i < _placementGhosts.Length; i++)
{
_placementGhosts[i] = SetupMyGhost(player, selectedPrefab);
}
goto IL_00a1;
}
}
return false;
}
goto IL_00a1;
IL_00a1:
return true;
}
private static void DestroyGhosts()
{
for (int i = 0; i < _placementGhosts.Length; i++)
{
if (Object.op_Implicit((Object)(object)_placementGhosts[i]))
{
Object.Destroy((Object)(object)_placementGhosts[i]);
_placementGhosts[i] = null;
}
}
}
private static void SetGhostsActive(bool active)
{
GameObject[] placementGhosts = _placementGhosts;
foreach (GameObject obj in placementGhosts)
{
if (obj != null)
{
obj.SetActive(active);
}
}
}
private static GameObject SetupMyGhost(Player player, GameObject prefab)
{
//IL_0166: Unknown result type (might be due to invalid IL or missing references)
//IL_016d: Expected O, but got Unknown
ZNetView.m_forceDisableInit = true;
GameObject val = Object.Instantiate<GameObject>(prefab);
ZNetView.m_forceDisableInit = false;
((Object)val).name = ((Object)prefab).name;
Joint[] componentsInChildren = val.GetComponentsInChildren<Joint>();
for (int i = 0; i < componentsInChildren.Length; i++)
{
Object.Destroy((Object)(object)componentsInChildren[i]);
}
Rigidbody[] componentsInChildren2 = val.GetComponentsInChildren<Rigidbody>();
for (int i = 0; i < componentsInChildren2.Length; i++)
{
Object.Destroy((Object)(object)componentsInChildren2[i]);
}
int layer = LayerMask.NameToLayer("ghost");
Transform[] componentsInChildren3 = val.GetComponentsInChildren<Transform>();
for (int i = 0; i < componentsInChildren3.Length; i++)
{
((Component)componentsInChildren3[i]).gameObject.layer = layer;
}
TerrainModifier[] componentsInChildren4 = val.GetComponentsInChildren<TerrainModifier>();
for (int i = 0; i < componentsInChildren4.Length; i++)
{
Object.Destroy((Object)(object)componentsInChildren4[i]);
}
GuidePoint[] componentsInChildren5 = val.GetComponentsInChildren<GuidePoint>();
for (int i = 0; i < componentsInChildren5.Length; i++)
{
Object.Destroy((Object)(object)componentsInChildren5[i]);
}
Light[] componentsInChildren6 = val.GetComponentsInChildren<Light>();
for (int i = 0; i < componentsInChildren6.Length; i++)
{
Object.Destroy((Object)(object)componentsInChildren6[i]);
}
Transform val2 = val.transform.Find("_GhostOnly");
if (Object.op_Implicit((Object)(object)val2))
{
((Component)val2).gameObject.SetActive(true);
}
MeshRenderer[] componentsInChildren7 = val.GetComponentsInChildren<MeshRenderer>();
foreach (MeshRenderer val3 in componentsInChildren7)
{
if (!((Object)(object)((Renderer)val3).sharedMaterial == (Object)null))
{
Material[] sharedMaterials = ((Renderer)val3).sharedMaterials;
for (int j = 0; j < sharedMaterials.Length; j++)
{
Material val4 = new Material(sharedMaterials[j]);
val4.SetFloat("_RippleDistance", 0f);
val4.SetFloat("_ValueNoise", 0f);
sharedMaterials[j] = val4;
}
((Renderer)val3).sharedMaterials = sharedMaterials;
((Renderer)val3).shadowCastingMode = (ShadowCastingMode)0;
}
}
return val;
}
static MassPlant()
{
//IL_0096: Unknown result type (might be due to invalid IL or missing references)
//IL_009c: Expected O, but got Unknown
//IL_00b0: Unknown result type (might be due to invalid IL or missing references)
//IL_00b6: Expected O, but got Unknown
Piece val = new Piece();
val.m_dlc = string.Empty;
val.m_resources = (Requirement[])(object)new Requirement[1]
{
new Requirement()
};
_fakeResourcePiece = val;
}
}
public const string PluginGuid = "EardwulfDoesMods.Comfy.MassFarming";
public const string PluginName = "Comfy.MassFarming";
public const string PluginVersion = "1.5.2";
private Harmony _harmony;
public void Awake()
{
PluginConfig.BindConfig(((BaseUnityPlugin)this).Config);
_harmony = Harmony.CreateAndPatchAll(Assembly.GetExecutingAssembly(), "EardwulfDoesMods.Comfy.MassFarming");
}
}
public class PluginConfig
{
public static ConfigEntry<KeyboardShortcut> MassActionHotkey { get; private set; }
public static ConfigEntry<bool> NoActionKeyNeeded { get; private set; }
public static ConfigEntry<KeyboardShortcut> ControllerPickupHotkey { get; private set; }
public static ConfigEntry<int> PlantGridWidth { get; private set; }
public static ConfigEntry<int> PlantGridLength { get; private set; }
public static ConfigEntry<bool> GridAnchorWidth { get; private set; }
public static ConfigEntry<bool> GridAnchorLength { get; private set; }
public static void BindConfig(ConfigFile config)
{
//IL_0015: Unknown result type (might be due to invalid IL or missing references)
//IL_003e: Unknown result type (might be due to invalid IL or missing references)
//IL_008b: Unknown result type (might be due to invalid IL or missing references)
//IL_0095: Expected O, but got Unknown
//IL_00b8: Unknown result type (might be due to invalid IL or missing references)
//IL_00c2: Expected O, but got Unknown
MassActionHotkey = config.Bind<KeyboardShortcut>("Hotkeys", "MassActionHotkey", new KeyboardShortcut((KeyCode)308, Array.Empty<KeyCode>()), "Mass activation hotkey for multi-plant.");
ControllerPickupHotkey = config.Bind<KeyboardShortcut>("Hotkeys", "ControllerPickupHotkey", new KeyboardShortcut((KeyCode)334, Array.Empty<KeyCode>()), "Mass activation hotkey for multi-pickup/multi-plant for controller.");
NoActionKeyNeeded = config.Bind<bool>("Override Grid Plant", "NoActionKeyNeeded", false, "Enable or disable grid planting without an action key");
PlantGridWidth = config.Bind<int>("Plant Grid", "Plant Grid Width", 3, new ConfigDescription("The width of mass planting grid. Default value is 3.", (AcceptableValueBase)(object)new AcceptableValueRange<int>(1, 9), Array.Empty<object>()));
PlantGridLength = config.Bind<int>("Plant Grid", "Plant Grid Length", 3, new ConfigDescription("The lenght of the mass planting grid. Default value is 3.", (AcceptableValueBase)(object)new AcceptableValueRange<int>(1, 9), Array.Empty<object>()));
GridAnchorWidth = config.Bind<bool>("PlantGrid", "GridAnchorWidth", true, "Planting grid anchor point (width). Default is 'enabled' so the grid will extend left and right from the crosshairs.Disable to set the anchor to the side of the grid. Disable both anchors to set to corner.");
GridAnchorLength = config.Bind<bool>("PlantGrid", "GridAnchorLength", true, "Planting grid anchor point (length). Default is 'enabled' so the grid will extend forward and backward from the crosshairs.Disable to set the anchor to the side of the grid. Disable both anchors to set to corner.");
PlantGridLength.SettingChanged += delegate
{
GridSizeChecker();
};
PlantGridWidth.SettingChanged += delegate
{
GridSizeChecker();
};
}
private static void GridSizeChecker()
{
if (PlantGridWidth.Value * PlantGridLength.Value > 9)
{
PlantGridWidth.Value = 1;
PlantGridLength.Value = 1;
}
}
}