using System;
using System.Collections;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using UnityEngine;
[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyCompany("NoWoodPortals")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("NoWoodPortals")]
[assembly: AssemblyTitle("NoWoodPortals")]
[assembly: AssemblyVersion("1.0.0.0")]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
[CompilerGenerated]
[Microsoft.CodeAnalysis.Embedded]
internal sealed class EmbeddedAttribute : Attribute
{
}
}
namespace System.Runtime.CompilerServices
{
[CompilerGenerated]
[Microsoft.CodeAnalysis.Embedded]
[AttributeUsage(AttributeTargets.Module, AllowMultiple = false, Inherited = false)]
internal sealed class RefSafetyRulesAttribute : Attribute
{
public readonly int Version;
public RefSafetyRulesAttribute(int P_0)
{
Version = P_0;
}
}
}
namespace NoWoodPortals
{
[BepInPlugin("com.dumba.valheim.nowoodportals", "No Wood Portals", "1.0.0")]
public class NoWoodPortalsPlugin : BaseUnityPlugin
{
public const string ModGuid = "com.dumba.valheim.nowoodportals";
public const string ModName = "No Wood Portals";
public const string ModVersion = "1.0.0";
private Harmony _harmony;
internal static NoWoodPortalsPlugin Instance { get; private set; }
private void Awake()
{
//IL_000c: Unknown result type (might be due to invalid IL or missing references)
//IL_0016: Expected O, but got Unknown
Instance = this;
_harmony = new Harmony("com.dumba.valheim.nowoodportals");
_harmony.PatchAll();
((BaseUnityPlugin)this).Logger.LogInfo((object)"Loaded No Wood Portals");
}
private void OnDestroy()
{
try
{
Harmony harmony = _harmony;
if (harmony != null)
{
harmony.UnpatchSelf();
}
}
catch
{
}
if (Instance == this)
{
Instance = null;
}
}
internal void Log(string msg)
{
((BaseUnityPlugin)this).Logger.LogInfo((object)msg);
}
internal void LogWarn(string msg)
{
((BaseUnityPlugin)this).Logger.LogWarning((object)msg);
}
internal void LogErr(string msg)
{
((BaseUnityPlugin)this).Logger.LogError((object)msg);
}
}
[HarmonyPatch(typeof(ObjectDB), "Awake")]
internal static class Patch_ObjectDB_Awake
{
private static void Postfix()
{
NoWoodPortalsPlugin instance = NoWoodPortalsPlugin.Instance;
if ((Object)(object)instance != (Object)null)
{
((MonoBehaviour)instance).StartCoroutine(WaitThenRemovePortalWood());
}
}
private static IEnumerator WaitThenRemovePortalWood()
{
while ((Object)(object)ObjectDB.instance == (Object)null || ObjectDB.instance.m_items == null || ObjectDB.instance.m_items.Count == 0)
{
yield return null;
}
while ((Object)(object)ZNetScene.instance == (Object)null)
{
yield return null;
}
int safety = 600;
while (safety-- > 0 && (Object)(object)ZNetScene.instance.GetPrefab("portal_wood") == (Object)null)
{
yield return null;
}
RemovePieceFromHammer("portal_wood");
}
private static void RemovePieceFromHammer(string piecePrefabName)
{
NoWoodPortalsPlugin instance = NoWoodPortalsPlugin.Instance;
if ((Object)(object)instance == (Object)null)
{
return;
}
GameObject itemPrefab = ObjectDB.instance.GetItemPrefab("Hammer");
if ((Object)(object)itemPrefab == (Object)null)
{
instance.LogWarn("Hammer item prefab not found in ObjectDB.");
return;
}
PieceTable val = itemPrefab.GetComponent<ItemDrop>()?.m_itemData?.m_shared?.m_buildPieces;
if ((Object)(object)val == (Object)null)
{
instance.LogWarn("Hammer PieceTable not found.");
return;
}
GameObject portalPrefab = ZNetScene.instance.GetPrefab(piecePrefabName);
if ((Object)(object)portalPrefab == (Object)null)
{
instance.LogWarn("Prefab '" + piecePrefabName + "' not found in ZNetScene.");
return;
}
int num = 0;
if (val.m_pieces != null)
{
int count = val.m_pieces.Count;
val.m_pieces.RemoveAll((GameObject p) => (Object)(object)p == (Object)(object)portalPrefab);
num += count - val.m_pieces.Count;
}
instance.Log($"Removed {num} '{piecePrefabName}' entries from Hammer build menu.");
}
}
}