using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using BepInEx;
using Jotunn.Entities;
using Jotunn.Managers;
using UnityEngine;
[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: AssemblyTitle("ModValheim")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("ModValheim")]
[assembly: AssemblyCopyright("Copyright © 2026")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("3faf8761-48cd-4cb8-bc11-557cfeb45591")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyVersion("1.0.0.0")]
[BepInPlugin("hofheim.orelhadeelfo", "Orelha de Elfo", "1.0.0")]
public class Plugin : BaseUnityPlugin
{
private const string OrelhaBundleFile = "11aorelhadeelfo";
private const string OrelhaPrefabName = "OrelhaDeElfo";
private const string DentesBundleFile = "1adentedeorc";
private const string DentesPrefabName = "DenteDeOrcs";
private const string Dentes2BundleFile = "1adentedeorc2";
private const string Dentes2PrefabName = "DenteDeOrcs2";
private AssetBundle orelhaBundle;
private AssetBundle dentesBundle;
private AssetBundle dentes2Bundle;
private GameObject orelhaPrefab;
private GameObject dentesPrefab;
private GameObject dentes2Prefab;
private void Awake()
{
((BaseUnityPlugin)this).Logger.LogInfo((object)"Iniciando plugin...");
orelhaBundle = LoadBundle("11aorelhadeelfo");
dentesBundle = LoadBundle("1adentedeorc");
dentes2Bundle = LoadBundle("1adentedeorc2");
orelhaPrefab = LoadPrefab(orelhaBundle, "OrelhaDeElfo");
dentesPrefab = LoadPrefab(dentesBundle, "DenteDeOrcs");
dentes2Prefab = LoadPrefab(dentes2Bundle, "DenteDeOrcs2");
RegisterItem(orelhaPrefab, "Orelha de Elfo");
RegisterItem(dentesPrefab, "Dentes de Orcs");
RegisterItem(dentes2Prefab, "Dentes de Orcs 2");
((BaseUnityPlugin)this).Logger.LogInfo((object)"Plugin carregado!");
}
private AssetBundle LoadBundle(string bundleShortName)
{
AssetBundle val = LoadBundleFromEmbeddedResource(bundleShortName);
if ((Object)(object)val != (Object)null)
{
((BaseUnityPlugin)this).Logger.LogInfo((object)("Bundle '" + bundleShortName + "' carregado da DLL."));
return val;
}
val = LoadBundleFromPluginFolder(bundleShortName);
if ((Object)(object)val != (Object)null)
{
((BaseUnityPlugin)this).Logger.LogInfo((object)("Bundle '" + bundleShortName + "' carregado da pasta do plugin."));
return val;
}
((BaseUnityPlugin)this).Logger.LogWarning((object)("Bundle '" + bundleShortName + "' não encontrado nem embutido nem externo."));
return null;
}
private AssetBundle LoadBundleFromEmbeddedResource(string bundleShortName)
{
Assembly executingAssembly = Assembly.GetExecutingAssembly();
string[] manifestResourceNames = executingAssembly.GetManifestResourceNames();
string text = manifestResourceNames.FirstOrDefault((string r) => r.Equals(bundleShortName, StringComparison.OrdinalIgnoreCase) || r.EndsWith("." + bundleShortName, StringComparison.OrdinalIgnoreCase));
if (string.IsNullOrEmpty(text))
{
return null;
}
((BaseUnityPlugin)this).Logger.LogInfo((object)("Recurso embutido encontrado: " + text));
Stream manifestResourceStream = executingAssembly.GetManifestResourceStream(text);
if (manifestResourceStream == null)
{
((BaseUnityPlugin)this).Logger.LogError((object)("Não foi possível abrir o recurso embutido: " + text));
return null;
}
using (manifestResourceStream)
{
using MemoryStream memoryStream = new MemoryStream();
manifestResourceStream.CopyTo(memoryStream);
byte[] array = memoryStream.ToArray();
return AssetBundle.LoadFromMemory(array);
}
}
private AssetBundle LoadBundleFromPluginFolder(string bundleShortName)
{
string directoryName = Path.GetDirectoryName(((BaseUnityPlugin)this).Info.Location);
string text = Path.Combine(directoryName, bundleShortName);
((BaseUnityPlugin)this).Logger.LogInfo((object)("Tentando carregar bundle externo em: " + text));
if (!File.Exists(text))
{
((BaseUnityPlugin)this).Logger.LogWarning((object)("Arquivo externo não encontrado: " + text));
return null;
}
AssetBundle val = AssetBundle.LoadFromFile(text);
if ((Object)(object)val == (Object)null)
{
((BaseUnityPlugin)this).Logger.LogError((object)("Erro ao carregar bundle externo: " + text));
return null;
}
return val;
}
private GameObject LoadPrefab(AssetBundle bundle, string prefabName)
{
if ((Object)(object)bundle == (Object)null)
{
((BaseUnityPlugin)this).Logger.LogWarning((object)("Bundle do prefab '" + prefabName + "' é nulo."));
return null;
}
GameObject val = bundle.LoadAsset<GameObject>(prefabName);
if ((Object)(object)val != (Object)null)
{
((BaseUnityPlugin)this).Logger.LogInfo((object)("Prefab '" + prefabName + "' carregado pelo nome direto."));
return val;
}
string[] allAssetNames = bundle.GetAllAssetNames();
((BaseUnityPlugin)this).Logger.LogWarning((object)("Prefab '" + prefabName + "' não foi encontrado pelo nome direto. Assets disponíveis:"));
string[] array = allAssetNames;
foreach (string text in array)
{
((BaseUnityPlugin)this).Logger.LogInfo((object)(" - " + text));
}
string text2 = allAssetNames.FirstOrDefault((string a) => Path.GetFileNameWithoutExtension(a).Equals(prefabName, StringComparison.OrdinalIgnoreCase));
if (string.IsNullOrEmpty(text2))
{
((BaseUnityPlugin)this).Logger.LogError((object)("Prefab '" + prefabName + "' não encontrado no bundle."));
return null;
}
val = bundle.LoadAsset<GameObject>(text2);
if ((Object)(object)val == (Object)null)
{
((BaseUnityPlugin)this).Logger.LogError((object)("Falha ao carregar prefab '" + prefabName + "' pelo caminho '" + text2 + "'."));
return null;
}
((BaseUnityPlugin)this).Logger.LogInfo((object)("Prefab '" + prefabName + "' carregado pelo caminho '" + text2 + "'."));
return val;
}
private void RegisterItem(GameObject prefab, string itemLabel)
{
//IL_002d: Unknown result type (might be due to invalid IL or missing references)
//IL_0033: Expected O, but got Unknown
if ((Object)(object)prefab == (Object)null)
{
((BaseUnityPlugin)this).Logger.LogWarning((object)("Item '" + itemLabel + "' não foi registrado porque o prefab é nulo."));
return;
}
CustomItem val = new CustomItem(prefab, true);
ItemManager.Instance.AddItem(val);
((BaseUnityPlugin)this).Logger.LogInfo((object)("Item '" + itemLabel + "' registrado no jogo."));
}
}