using System;
using System.Collections.Generic;
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 System.Security;
using System.Security.Permissions;
using System.Text;
using System.Threading;
using BepInEx;
using BepInEx.Bootstrap;
using BepInEx.Configuration;
using BepInEx.Logging;
using Digitalroot.Valheim.Common;
using Digitalroot.Valheim.Common.Json;
using HarmonyLib;
using JetBrains.Annotations;
using Jotunn.Entities;
using Jotunn.Utils;
using Mono.Cecil;
using SimpleJson;
using UnityEngine;
[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyTitle("Digitalroot.Valheim.PluginInfo")]
[assembly: AssemblyDescription("Digitalroot Plug-in Info")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("Digitalroot Technologies")]
[assembly: AssemblyProduct("Digitalroot Valheim Mods")]
[assembly: AssemblyCopyright("Copyright © Digitalroot Technologies 2021 - 2024")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("b809a6e8-c70f-47f0-9cfb-bfab5737c7f4")]
[assembly: AssemblyFileVersion("1.4.62")]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.4.62.0")]
[module: UnverifiableCode]
namespace Digitalroot.Valheim.PluginInfo
{
internal sealed class ConfigurationManagerAttributes
{
public bool? ShowRangeAsPercent;
public Action<ConfigEntryBase> CustomDrawer;
public bool? Browsable;
public string Category;
public object DefaultValue;
public bool? HideDefaultButton;
public bool? HideSettingName;
public string Description;
public string DispName;
public int? Order;
public bool? ReadOnly;
public bool? IsAdvanced;
public Func<object, string> ObjToStr;
public Func<string, object> StrToObj;
}
[BepInPlugin("digitalroot.mods.plugininfo", "Digitalroot Plug-in Info", "1.4.62")]
[BepInDependency(/*Could not decode attribute arguments.*/)]
public class Main : BaseUnityPlugin, ITraceableLogging
{
private class BepInExPluginInfoProxy
{
private readonly PluginInfo _pluginInfo;
public BepInPlugin Metadata => _pluginInfo.Metadata;
[UsedImplicitly]
public IEnumerable<BepInProcess> Processes => _pluginInfo.Processes;
[UsedImplicitly]
public IEnumerable<BepInDependency> Dependencies => _pluginInfo.Dependencies;
[UsedImplicitly]
public IEnumerable<BepInIncompatibility> Incompatibilities => _pluginInfo.Incompatibilities;
public string Location { get; }
[UsedImplicitly]
public BaseUnityPlugin Instance => _pluginInfo.Instance;
public BepInExPluginInfoProxy(PluginInfo pluginInfo, string location)
{
_pluginInfo = pluginInfo;
Location = location;
}
public override string ToString()
{
return ((object)_pluginInfo).ToString();
}
}
private Harmony _harmony;
[UsedImplicitly]
public static ConfigEntry<int> NexusId;
public static Main Instance;
private const string JVLGuid = "com.jotunn.jotunn";
private const string CacheName = "chainloader";
public const string Version = "1.4.62";
public const string Name = "Digitalroot Plug-in Info";
public const string Guid = "digitalroot.mods.plugininfo";
public const string Namespace = "Digitalroot.Valheim.PluginInfo";
public string Source => "Digitalroot.Valheim.PluginInfo";
public bool EnableTrace { get; }
public Main()
{
//IL_0054: Unknown result type (might be due to invalid IL or missing references)
//IL_005e: Expected O, but got Unknown
try
{
EnableTrace = false;
Instance = this;
NexusId = ((BaseUnityPlugin)this).Config.Bind<int>("General", "NexusID", 1302, new ConfigDescription("Nexus mod ID for updates", (AcceptableValueBase)null, new object[1]
{
new ConfigurationManagerAttributes
{
Browsable = false,
ReadOnly = true
}
}));
Log.RegisterSource(Instance);
Log.Trace(Instance, ((object)this).GetType().Namespace + "." + ((object)this).GetType().Name + "." + MethodBase.GetCurrentMethod()?.Name + "()");
}
catch (Exception ex)
{
ZLog.LogError((object)ex);
}
}
[UsedImplicitly]
private void Awake()
{
try
{
Log.Trace(Instance, ((object)this).GetType().Namespace + "." + ((object)this).GetType().Name + "." + MethodBase.GetCurrentMethod()?.Name + "()");
_harmony = Harmony.CreateAndPatchAll(typeof(Main).Assembly, "digitalroot.mods.plugininfo");
}
catch (Exception e)
{
Log.Error(Instance, e);
}
}
[UsedImplicitly]
private void OnDestroy()
{
try
{
Log.Trace(Instance, ((object)this).GetType().Namespace + "." + ((object)this).GetType().Name + "." + MethodBase.GetCurrentMethod()?.Name + "()");
Harmony harmony = _harmony;
if (harmony != null)
{
harmony.UnpatchSelf();
}
}
catch (Exception e)
{
Log.Error(Instance, e);
}
}
public void OnFejdStartupStart()
{
try
{
Log.Trace(Instance, ((object)this).GetType().Namespace + "." + ((object)this).GetType().Name + "." + MethodBase.GetCurrentMethod()?.Name + "()");
HandleBepInExData();
HandleDupModDetection();
if (Utils.DoesPluginExist("com.jotunn.jotunn"))
{
HandleJVLData();
}
}
catch (Exception e)
{
Log.Error(Instance, e);
}
}
private void HandleBepInExData()
{
//IL_013b: Unknown result type (might be due to invalid IL or missing references)
try
{
Log.Debug(Instance, "******* [Digitalroot Plug-ins Loaded] *******");
foreach (KeyValuePair<string, PluginInfo> pluginInfo in Chainloader.PluginInfos)
{
Log.Debug(Instance, "Key: " + pluginInfo.Key);
Log.Debug(Instance, $"Value: {pluginInfo.Value}");
Log.Debug(Instance, "GUID: " + pluginInfo.Value.Metadata.GUID);
Log.Debug(Instance, "Name: " + pluginInfo.Value.Metadata.Name);
Log.Debug(Instance, $"Version: {pluginInfo.Value.Metadata.Version}");
Log.Debug(Instance, "Location: " + pluginInfo.Value.Location);
Log.Debug(Instance, "Dependencies:");
foreach (BepInDependency dependency in pluginInfo.Value.Dependencies)
{
Log.Debug(Instance, "DependencyGUID: " + dependency.DependencyGUID);
Log.Debug(Instance, $"Flags: {dependency.Flags}");
Log.Debug(Instance, $"MinimumVersion: {dependency.MinimumVersion}");
}
Log.Debug(Instance, "Incompatibilities:");
foreach (BepInIncompatibility incompatibility in pluginInfo.Value.Incompatibilities)
{
Log.Debug(Instance, "DependencyGUID: " + incompatibility.IncompatibilityGUID);
}
Log.Debug(Instance, $"Instance: {pluginInfo.Value.Instance}");
Log.Debug(Instance, "***************************************");
}
Log.Debug(Instance, "DependencyErrors");
foreach (string dependencyError in Chainloader.DependencyErrors)
{
Log.Debug(Instance, dependencyError ?? "");
}
Log.Debug(Instance, "***************************************");
}
catch (Exception e)
{
Log.Error(Instance, e);
}
}
private void HandleDupModDetection()
{
try
{
Log.Trace(Instance, ((object)this).GetType().Namespace + "." + ((object)this).GetType().Name + "." + MethodBase.GetCurrentMethod()?.Name + "()");
Log.Debug(Instance, "******* [Digitalroot Duplicate Mod Info ] *******");
foreach (IGrouping<string, BepInExPluginInfoProxy> item in from info in GetPlugins().ToList()
group info by info.Metadata.GUID into pi
where pi.Count() >= 2
select pi)
{
Log.Warning(Instance, $"Duplicates found for '{item.Key}', Count {item.Count()}");
foreach (BepInExPluginInfoProxy item2 in item)
{
Log.Warning(Instance, " Name: " + item2.Metadata.Name);
Log.Warning(Instance, " GUID: " + item2.Metadata.GUID);
Log.Warning(Instance, $" Version: {item2.Metadata.Version}");
Log.Warning(Instance, " Location: " + item2.Location);
Log.Warning(Instance, " ---------------------------------------");
}
}
Log.Debug(Instance, "***************************************");
}
catch (Exception e)
{
Log.Error(Instance, e);
}
}
private void HandleJVLData()
{
try
{
Log.Trace(Instance, ((object)this).GetType().Namespace + "." + ((object)this).GetType().Name + "." + MethodBase.GetCurrentMethod()?.Name + "()");
Log.Debug(Instance, "******* [Digitalroot JVL Info ] *******");
foreach (ModInfo mod in ModRegistry.GetMods(false))
{
Log.Debug(Instance, "Name: " + mod.Name);
Log.Debug(Instance, $"Version: {mod.Version}");
Log.Debug(Instance, "GUID: " + mod.GUID);
if (mod.Commands.Any())
{
Log.Debug(Instance, string.Empty);
Log.Debug(Instance, "Commands:");
foreach (ConsoleCommand command in mod.Commands)
{
Log.Debug(Instance, command.Name + " - " + command.Help);
}
}
if (mod.Prefabs.Any())
{
Log.Debug(Instance, string.Empty);
Log.Debug(Instance, "Prefabs:");
foreach (CustomPrefab prefab in mod.Prefabs)
{
Log.Debug(Instance, ((Object)prefab.Prefab).name ?? "");
}
}
if (mod.Items.Any())
{
Log.Debug(Instance, string.Empty);
Log.Debug(Instance, "Items: (Token/ItemName => PrefabName)");
foreach (CustomItem item in mod.Items)
{
Log.Debug(Instance, item.ItemDrop.m_itemData.m_shared.m_name + " => " + ((Object)item.ItemPrefab).name);
}
}
if (mod.Recipes.Any())
{
Log.Debug(Instance, string.Empty);
Log.Debug(Instance, "Recipes: (RecipesName => PrefabName)");
foreach (CustomRecipe recipe in mod.Recipes)
{
Log.Debug(Instance, ((Object)recipe.Recipe).name + " => " + ((Object)recipe.Recipe.m_item).name.Replace("JVLmock_", string.Empty));
}
}
if (mod.ItemConversions.Any())
{
Log.Debug(Instance, string.Empty);
Log.Debug(Instance, "ItemConversions: (PieceName: PrefabName => PrefabName)");
foreach (CustomItemConversion itemConversion in mod.ItemConversions)
{
Log.Debug(Instance, itemConversion.Config.Station + ": " + itemConversion.Config.FromItem + " => " + itemConversion.Config.ToItem);
}
}
if (mod.PieceTables.Any())
{
Log.Debug(Instance, string.Empty);
Log.Debug(Instance, "PieceTables: (PieceTableName: (CategoryNames))");
foreach (CustomPieceTable pieceTable in mod.PieceTables)
{
Log.Debug(Instance, string.Format("{0}: ({1})", pieceTable.PieceTable, GeneralExtensions.Join<string>((IEnumerable<string>)pieceTable.Categories, (Func<string, string>)null, ", ")));
}
}
if (mod.Pieces.Any())
{
Log.Debug(Instance, string.Empty);
Log.Debug(Instance, "Pieces: (PieceTableName: Token/PieceName => PrefabName)");
foreach (CustomPiece piece in mod.Pieces)
{
Piece component = piece.PiecePrefab.GetComponent<Piece>();
Log.Debug(Instance, piece.PieceTable + ": " + component?.m_name + " => " + ((Object)piece.Piece).name);
}
}
if (mod.StatusEffects.Any())
{
Log.Debug(Instance, string.Empty);
Log.Debug(Instance, "StatusEffects: (Token/StatusEffectName)");
foreach (CustomStatusEffect statusEffect in mod.StatusEffects)
{
Log.Debug(Instance, ((Object)statusEffect.StatusEffect).name ?? "");
}
}
if (mod.Translations.Any())
{
Log.Debug(Instance, string.Empty);
Log.Debug(Instance, "Translations: (Token => Value)");
foreach (CustomLocalization translation in mod.Translations)
{
foreach (string language in translation.GetLanguages())
{
string current11 = language;
Log.Debug(Instance, "[" + current11 + "]");
foreach (KeyValuePair<string, string> translation2 in translation.GetTranslations(ref current11))
{
Log.Debug(Instance, "$" + translation2.Key + " => " + translation2.Value);
}
Log.Debug(Instance, string.Empty);
}
}
}
Log.Debug(Instance, "***************************************");
}
}
catch (Exception e)
{
Log.Error(Instance, e);
}
}
[UsedImplicitly]
public static bool DoesPluginExist(string pluginGuid)
{
return Chainloader.PluginInfos.Any((KeyValuePair<string, PluginInfo> keyValuePair) => keyValuePair.Value.Metadata.GUID == pluginGuid);
}
[UsedImplicitly]
public IEnumerable<FileInfo> GetModAssemblies()
{
return from filePath in Directory.GetFiles(Paths.PluginPath, "*.dll", SearchOption.AllDirectories)
select new FileInfo(filePath);
}
private IEnumerable<BepInExPluginInfoProxy> GetPlugins()
{
Dictionary<string, List<PluginInfo>> dictionary = TypeLoader.FindPluginTypes<PluginInfo>(Paths.PluginPath, (Func<TypeDefinition, PluginInfo>)Chainloader.ToPluginInfo, (Func<AssemblyDefinition, bool>)null, "chainloader");
foreach (KeyValuePair<string, List<PluginInfo>> keyValuePair in dictionary)
{
foreach (PluginInfo item in keyValuePair.Value)
{
yield return new BepInExPluginInfoProxy(item, keyValuePair.Key);
}
}
}
}
public class Patch
{
[HarmonyPatch(typeof(FejdStartup), "Start")]
public class PatchFejdStartupStart
{
[HarmonyPostfix]
[HarmonyPriority(200)]
public static void Postfix()
{
try
{
Log.Trace(Main.Instance, "Digitalroot.Valheim.PluginInfo." + MethodBase.GetCurrentMethod()?.DeclaringType?.Name + "." + MethodBase.GetCurrentMethod()?.Name);
Main.Instance.OnFejdStartupStart();
}
catch (Exception e)
{
Log.Error(Main.Instance, e);
}
}
}
}
}
namespace Digitalroot.Valheim.Common
{
internal interface ITraceableLogging
{
string Source { get; }
bool EnableTrace { get; }
}
internal sealed class Log
{
private static readonly Dictionary<string, TraceLogger> TraceLoggers;
[UsedImplicitly]
private static Log Instance { get; }
static Log()
{
TraceLoggers = new Dictionary<string, TraceLogger>();
Instance = new Log();
}
private Log()
{
TraceLoggers.Add("Digitalroot", new TraceLogger("Digitalroot", enableTrace: false));
}
public static void RegisterSource(ITraceableLogging sender)
{
if ((!TraceLoggers.ContainsKey(sender.Source) || TraceLoggers[sender.Source].IsTraceEnabled != sender.EnableTrace) && (!TraceLoggers.ContainsKey(sender.Source) || sender.EnableTrace))
{
if (TraceLoggers.ContainsKey(sender.Source) && sender.EnableTrace)
{
TraceLoggers[sender.Source].EnableTrace();
}
else
{
TraceLoggers.Add(sender.Source, new TraceLogger(sender.Source, sender.EnableTrace));
}
}
}
private static TraceLogger GetTraceLogger(ITraceableLogging sender)
{
if (!TraceLoggers.ContainsKey(sender.Source))
{
return TraceLoggers["Digitalroot"];
}
return TraceLoggers[sender.Source];
}
[UsedImplicitly]
public static void SetEnableTrace(ITraceableLogging sender, bool value)
{
if (value)
{
GetTraceLogger(sender).EnableTrace();
}
else
{
GetTraceLogger(sender).DisableTrace();
}
}
[UsedImplicitly]
public static void SetEnableTraceForAllLoggers(bool value)
{
foreach (TraceLogger value2 in TraceLoggers.Values)
{
if (value)
{
value2.EnableTrace();
}
else
{
value2.DisableTrace();
}
}
}
[UsedImplicitly]
public static void Debug(ITraceableLogging sender, object value)
{
GetTraceLogger(sender).LoggerRef.LogDebug(value);
}
[UsedImplicitly]
public static void Error(ITraceableLogging sender, Exception e, int i = 1)
{
Error(sender, "Message: " + e.Message);
Error(sender, $"TargetSite: {e.TargetSite}");
Error(sender, "StackTrace: " + e.StackTrace);
Error(sender, "Source: " + e.Source);
if (e.Data.Count > 0)
{
foreach (object key in e.Data.Keys)
{
Error(sender, $"key: {key}, value: {e.Data[key]}");
}
}
if (e.InnerException != null)
{
Error(sender, $"--- InnerException [{i}][Start] ---");
Error(sender, e.InnerException, ++i);
}
}
[UsedImplicitly]
public static void Error(ITraceableLogging sender, object value)
{
GetTraceLogger(sender).LoggerRef.LogError(value);
}
[UsedImplicitly]
public static void Info(ITraceableLogging sender, object value)
{
GetTraceLogger(sender).LoggerRef.LogInfo(value);
}
[UsedImplicitly]
public static void Fatal(ITraceableLogging sender, Exception e, int i = 1)
{
Fatal(sender, "Message: " + e.Message);
Fatal(sender, $"TargetSite: {e.TargetSite}");
Fatal(sender, "StackTrace: " + e.StackTrace);
Fatal(sender, "Source: " + e.Source);
if (e.Data.Count > 0)
{
foreach (object key in e.Data.Keys)
{
Fatal(sender, $"key: {key}, value: {e.Data[key]}");
}
}
if (e.InnerException != null)
{
Fatal(sender, $"--- InnerException [{i}][Start] ---");
Fatal(sender, e.InnerException, ++i);
}
}
[UsedImplicitly]
public static void Fatal(ITraceableLogging sender, object value)
{
GetTraceLogger(sender).LoggerRef.LogFatal(value);
}
[UsedImplicitly]
public static void Message(ITraceableLogging sender, object value)
{
GetTraceLogger(sender).LoggerRef.LogMessage(value);
}
[UsedImplicitly]
public static void Trace(ITraceableLogging sender, object value)
{
if (GetTraceLogger(sender).IsTraceEnabled || sender.EnableTrace)
{
GetTraceLogger(sender).LoggerRef.Log((LogLevel)63, value);
}
}
[UsedImplicitly]
public static void Warning(ITraceableLogging sender, object value)
{
GetTraceLogger(sender).LoggerRef.LogWarning(value);
}
}
internal class TraceLogger
{
internal readonly ManualLogSource LoggerRef;
private readonly string _source;
private readonly FileInfo _traceFileInfo;
public bool IsTraceEnabled { get; private set; }
private DirectoryInfo AssemblyDirectory => new FileInfo(Uri.UnescapeDataString(new UriBuilder(Assembly.GetExecutingAssembly().CodeBase).Path)).Directory;
public TraceLogger(string source, bool enableTrace)
{
_source = source;
IsTraceEnabled = enableTrace;
LoggerRef = Logger.CreateLogSource(_source);
_traceFileInfo = new FileInfo(Path.Combine(Paths.BepInExRootPath ?? AssemblyDirectory.FullName, "logs", _source + ".Trace.log"));
if (_traceFileInfo.DirectoryName != null)
{
Directory.CreateDirectory(_traceFileInfo.DirectoryName);
}
if (_traceFileInfo.Exists)
{
_traceFileInfo.Delete();
_traceFileInfo.Refresh();
}
LoggerRef.LogEvent += OnLogEvent;
}
public void EnableTrace()
{
IsTraceEnabled = true;
}
public void DisableTrace()
{
IsTraceEnabled = false;
}
[UsedImplicitly]
public void StopTrace()
{
LoggerRef.LogEvent -= OnLogEvent;
}
private void OnLogEvent(object sender, LogEventArgs e)
{
//IL_00b0: Unknown result type (might be due to invalid IL or missing references)
//IL_005a: Unknown result type (might be due to invalid IL or missing references)
if (e.Source.SourceName != _source || !IsTraceEnabled)
{
return;
}
using Mutex mutex = new Mutex(initiallyOwned: false, "Digitalroot.Valheim.Common.TraceLogger." + _source);
mutex.WaitOne();
try
{
if (e.Data is string)
{
string contents = $"[{e.Level,-7}:{e.Source.SourceName,10}] {e.Data}{Environment.NewLine}";
File.AppendAllText(_traceFileInfo.FullName, contents, Encoding.UTF8);
}
else
{
string contents2 = $"[{e.Level,-7}:{e.Source.SourceName,10}] {JsonSerializationProvider.Serialize(e.Data)}{Environment.NewLine}";
File.AppendAllText(_traceFileInfo.FullName, contents2, Encoding.UTF8);
}
}
finally
{
mutex.ReleaseMutex();
}
}
}
}
namespace Digitalroot.Valheim.Common.Json
{
[UsedImplicitly]
internal static class JsonSerializationProvider
{
[Obsolete("Use Deserialize<T>()")]
public static T FromJson<T>(string json)
{
return Deserialize<T>(json);
}
public static T Deserialize<T>(string json)
{
return SimpleJson.DeserializeObject<T>(json, (IJsonSerializerStrategy)(object)new DigitalrootJsonSerializerStrategy());
}
[Obsolete("Use Serialize()")]
public static string ToJson(object obj, bool pretty = false)
{
return Serialize(obj);
}
public static string Serialize(object obj)
{
return SimpleJson.SerializeObject(obj, (IJsonSerializerStrategy)(object)new DigitalrootJsonSerializerStrategy());
}
}
internal class DigitalrootJsonSerializerStrategy : PocoJsonSerializerStrategy
{
public override bool TrySerializeNonPrimitiveObject(object input, out object output)
{
//IL_0009: Unknown result type (might be due to invalid IL or missing references)
//IL_000e: Unknown result type (might be due to invalid IL or missing references)
//IL_001a: Unknown result type (might be due to invalid IL or missing references)
//IL_001f: Unknown result type (might be due to invalid IL or missing references)
//IL_002b: 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_003d: Unknown result type (might be due to invalid IL or missing references)
//IL_0050: Unknown result type (might be due to invalid IL or missing references)
//IL_0059: Unknown result type (might be due to invalid IL or missing references)
//IL_0062: 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)
if (!(input is Vector3 val))
{
if (input is Quaternion val2)
{
output = new float[4] { val2.x, val2.y, val2.z, val2.w };
return true;
}
return ((PocoJsonSerializerStrategy)this).TrySerializeNonPrimitiveObject(input, ref output);
}
output = new float[3] { val.x, val.y, val.z };
return true;
}
public override object DeserializeObject(object value, Type type)
{
//IL_009b: Unknown result type (might be due to invalid IL or missing references)
//IL_0102: Unknown result type (might be due to invalid IL or missing references)
if (type == null)
{
throw new ArgumentNullException("type");
}
if (value == null)
{
throw new ArgumentNullException("value");
}
if (value is string value2)
{
if (string.IsNullOrWhiteSpace(value2))
{
throw new ArgumentNullException("value");
}
if (type == typeof(Vector3))
{
if (!(((PocoJsonSerializerStrategy)this).DeserializeObject(value, typeof(float[])) is float[] array) || (array != null && array.Length != 3))
{
throw new ArgumentException(string.Format("The value '{0}' can be converted to a {1}.", value, "Vector3"), "value");
}
return (object)new Vector3(array[0], array[1], array[2]);
}
if (type == typeof(Quaternion))
{
if (!(((PocoJsonSerializerStrategy)this).DeserializeObject(value, typeof(float[])) is float[] array2) || (array2 != null && array2.Length != 4))
{
throw new ArgumentException(string.Format("The value '{0}' can be converted to a {1}.", value, "Quaternion"), "value");
}
return (object)new Quaternion(array2[0], array2[1], array2[2], array2[3]);
}
return ((PocoJsonSerializerStrategy)this).DeserializeObject(value, type);
}
throw new ArgumentException($"The value '{value}' can be converted to a {type.Name}.", "value");
}
}
}
namespace Digitalroot.Valheim.Common
{
internal static class Utils
{
private static readonly ITraceableLogging Logger = GetLogger();
[UsedImplicitly]
public static DirectoryInfo AssemblyDirectory => new FileInfo(Uri.UnescapeDataString(new UriBuilder(Assembly.GetExecutingAssembly().CodeBase).Path)).Directory;
[UsedImplicitly]
public static bool IsDedicated => ZNet.instance.IsDedicated();
[UsedImplicitly]
public static bool IsServer => ZNet.instance.IsServer();
public static bool IsRunningFromNUnit => AppDomain.CurrentDomain.GetAssemblies().Any((Assembly a) => a.FullName.ToLowerInvariant().StartsWith("nunit.framework"));
public static string Namespace => "Digitalroot.Valheim.Common";
private static ITraceableLogging GetLogger()
{
return new StaticSourceLogger();
}
[UsedImplicitly]
public static bool IsHeadless()
{
//IL_0000: Unknown result type (might be due to invalid IL or missing references)
//IL_0006: Invalid comparison between Unknown and I4
return (int)SystemInfo.graphicsDeviceType == 4;
}
[UsedImplicitly]
public static List<T> AllOf<T>()
{
return Enum.GetValues(typeof(T)).OfType<T>().ToList();
}
[UsedImplicitly]
public static IEnumerable<string> AllNames(Type type)
{
foreach (FieldInfo item in from f1 in type.GetFields()
where f1.FieldType == typeof(string)
select f1)
{
yield return item.GetValue(null).ToString();
}
}
[UsedImplicitly]
public static bool DoesPluginExist(string pluginGuid)
{
return Chainloader.PluginInfos.Any((KeyValuePair<string, PluginInfo> keyValuePair) => keyValuePair.Value.Metadata.GUID == pluginGuid);
}
[UsedImplicitly]
public static Vector3 GetGroundHeight(int x, int z)
{
//IL_0007: Unknown result type (might be due to invalid IL or missing references)
//IL_000c: Unknown result type (might be due to invalid IL or missing references)
return Utils.GetGroundHeight(new Vector3Int(x, 500, z));
}
[UsedImplicitly]
public static Vector3 GetGroundHeight(float x, float z)
{
//IL_0007: Unknown result type (might be due to invalid IL or missing references)
//IL_000c: Unknown result type (might be due to invalid IL or missing references)
return Utils.GetGroundHeight(new Vector3(x, 500f, z));
}
public static Vector3 GetGroundHeight(Vector3Int vector3)
{
//IL_000d: Unknown result type (might be due to invalid IL or missing references)
//IL_000e: Unknown result type (might be due to invalid IL or missing references)
//IL_0020: Unknown result type (might be due to invalid IL or missing references)
return new Vector3((float)((Vector3Int)(ref vector3)).x, ZoneSystem.instance.GetGroundHeight(Vector3Int.op_Implicit(vector3)), (float)((Vector3Int)(ref vector3)).z);
}
public static Vector3 GetGroundHeight(Vector3 vector3)
{
//IL_0000: Unknown result type (might be due to invalid IL or missing references)
//IL_000b: Unknown result type (might be due to invalid IL or missing references)
//IL_0011: Unknown result type (might be due to invalid IL or missing references)
//IL_0017: Unknown result type (might be due to invalid IL or missing references)
return new Vector3(vector3.x, ZoneSystem.instance.GetGroundHeight(vector3), vector3.z);
}
[UsedImplicitly]
public static GameObject GetItemPrefab(string itemName)
{
if (!IsObjectDBReady())
{
return null;
}
return GetObjectDB().GetItemPrefab(itemName);
}
[UsedImplicitly]
public static GameObject GetItemPrefab(int hash)
{
if (!IsObjectDBReady())
{
return null;
}
return GetObjectDB().GetItemPrefab(hash);
}
[UsedImplicitly]
public static Player GetLocalPlayer()
{
return Player.m_localPlayer;
}
[UsedImplicitly]
public static Vector3 GetLocalPlayersPosition()
{
//IL_000a: Unknown result type (might be due to invalid IL or missing references)
return ((Component)Player.m_localPlayer).transform.position;
}
[UsedImplicitly]
public static ObjectDB GetObjectDB()
{
return ObjectDB.instance;
}
[UsedImplicitly]
public static string GetPluginPath(Type modPluginType)
{
return Path.GetDirectoryName(modPluginType.Assembly.Location);
}
[UsedImplicitly]
public static GameObject GetPrefab(string itemName)
{
if (!IsZNetSceneReady())
{
return null;
}
return ZNetScene.instance.GetPrefab(itemName);
}
[UsedImplicitly]
public static GameObject GetPrefab(int hash)
{
if (!IsZNetSceneReady())
{
return null;
}
return ZNetScene.instance.GetPrefab(hash);
}
[UsedImplicitly]
public static T GetPrivateField<T>(object instance, string name)
{
FieldInfo field = instance.GetType().GetField(name, BindingFlags.Instance | BindingFlags.NonPublic);
if (field != null)
{
return (T)field.GetValue(instance);
}
Log.Error(Logger, "Variable " + name + " does not exist on type: " + instance.GetType());
return default(T);
}
[UsedImplicitly]
public static object InvokePrivate(object instance, string name, object[] args = null)
{
MethodInfo method = instance.GetType().GetMethod(name, BindingFlags.Instance | BindingFlags.NonPublic);
if (method == null)
{
Type[] types = ((args == null) ? Type.EmptyTypes : args.Select((object arg) => arg.GetType()).ToArray());
method = instance.GetType().GetMethod(name, types);
}
if (method == null)
{
Log.Error(Logger, "Method " + name + " does not exist on type: " + instance.GetType());
return null;
}
return method.Invoke(instance, args);
}
[UsedImplicitly]
public static bool IsGameInMainScene()
{
return (Object)(object)ZNetScene.instance != (Object)null;
}
[UsedImplicitly]
public static bool IsObjectDBReady()
{
if (!((Object)(object)GetObjectDB() != (Object)null) || GetObjectDB().m_items.Count == 0 || !((Object)(object)GetItemPrefab("Amber") != (Object)null))
{
return IsRunningFromNUnit;
}
return true;
}
[UsedImplicitly]
public static bool IsPlayerReady()
{
return (Object)(object)GetLocalPlayer() != (Object)null;
}
[UsedImplicitly]
public static bool IsZNetSceneReady()
{
if ((Object)(object)ZNetScene.instance != (Object)null)
{
List<GameObject> list = ZNetScene.instance?.m_prefabs;
if (list != null)
{
return list.Count > 0;
}
return false;
}
return false;
}
[UsedImplicitly]
public static bool IsZNetReady()
{
return (Object)(object)ZNet.instance != (Object)null;
}
[UsedImplicitly]
public static string Localize(string value)
{
return Localization.instance.Localize(value);
}
[UsedImplicitly]
public static Vector3 GetStartTemplesPosition()
{
//IL_000a: 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_0022: Unknown result type (might be due to invalid IL or missing references)
//IL_0023: Unknown result type (might be due to invalid IL or missing references)
//IL_0037: Unknown result type (might be due to invalid IL or missing references)
//IL_0038: Unknown result type (might be due to invalid IL or missing references)
LocationInstance val = default(LocationInstance);
if (ZoneSystem.instance.FindClosestLocation("StartTemple", Vector3.zero, ref val))
{
Log.Trace(Logger, $"[GetStartTemplesPosition] StartTemple at {val.m_position}");
return val.m_position;
}
Log.Error(Logger, "[GetStartTemplesPosition] Can't find StartTemple");
return Vector3.zero;
}
[UsedImplicitly]
public static void SetPrivateField(object instance, string name, object value)
{
FieldInfo field = instance.GetType().GetField(name, BindingFlags.Instance | BindingFlags.NonPublic);
if (field == null)
{
Log.Error(Logger, "Variable " + name + " does not exist on type: " + instance.GetType());
}
else
{
field.SetValue(instance, value);
}
}
[UsedImplicitly]
public static GameObject Spawn([NotNull] string prefabName, Vector3 location, [CanBeNull] Transform parent = null)
{
//IL_0044: Unknown result type (might be due to invalid IL or missing references)
//IL_007f: Unknown result type (might be due to invalid IL or missing references)
Log.Trace(Logger, $"{Namespace}.{MethodBase.GetCurrentMethod().DeclaringType?.Name}.{MethodBase.GetCurrentMethod().Name}({prefabName}, {location}, {((parent != null) ? ((Object)parent).name : null)})");
GameObject itemPrefab = ObjectDB.instance.GetItemPrefab(StringExtensionMethods.GetStableHashCode(prefabName));
if (!((Object)(object)itemPrefab == (Object)null))
{
return Spawn(itemPrefab, location, parent);
}
return null;
}
[UsedImplicitly]
public static GameObject Spawn([NotNull] GameObject prefab, Vector3 location, [CanBeNull] Transform parent = null)
{
//IL_0049: Unknown result type (might be due to invalid IL or missing references)
//IL_0080: Unknown result type (might be due to invalid IL or missing references)
//IL_0081: Unknown result type (might be due to invalid IL or missing references)
//IL_0073: 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)
Log.Trace(Logger, $"{Namespace}.{MethodBase.GetCurrentMethod().DeclaringType?.Name}.{MethodBase.GetCurrentMethod().Name}({((Object)prefab).name}, {location}, {((parent != null) ? ((Object)parent).name : null)})");
if ((Object)(object)parent == (Object)null)
{
return Object.Instantiate<GameObject>(prefab, location, Quaternion.identity);
}
return Object.Instantiate<GameObject>(prefab, location, Quaternion.identity, parent);
}
public static AssetBundle LoadAssetBundleFromResources(string bundleName, Assembly resourceAssembly)
{
if (resourceAssembly == null)
{
throw new ArgumentNullException("resourceAssembly");
}
string text = null;
try
{
text = resourceAssembly.GetManifestResourceNames().Single((string str) => str.EndsWith(bundleName));
}
catch (Exception)
{
}
if (text == null)
{
Log.Error(Logger, "AssetBundle " + bundleName + " not found in assembly manifest");
return null;
}
using Stream stream = resourceAssembly.GetManifestResourceStream(text);
return AssetBundle.LoadFromStream(stream);
}
}
[DebuggerDisplay("Source = {Source}, EnableTrace = {EnableTrace}", Name = "{Source}")]
internal class StaticSourceLogger : ITraceableLogging
{
public static StaticSourceLogger PreMadeTraceableInstance = new StaticSourceLogger(enableTrace: true);
public static StaticSourceLogger PreMadeNonTraceableInstance = new StaticSourceLogger();
public string Source { get; }
public bool EnableTrace { get; }
public StaticSourceLogger(bool enableTrace = false)
: this("Digitalroot", enableTrace)
{
}
public StaticSourceLogger(string source, bool enableTrace = false)
{
Source = source;
EnableTrace = enableTrace;
}
}
}
internal class DigitalrootValheimPluginInfo_ProcessedByFody
{
internal const string FodyVersion = "6.6.0.0";
internal const string ILMerge = "1.22.0.0";
}