Decompiled source of Scenic v1.4.0

Scenic.dll

Decompiled 10 months ago
using System.Collections.Generic;
using System.Diagnostics;
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;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyTitle("Scenic")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("Scenic")]
[assembly: AssemblyCopyright("Copyright © 2023")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("95c8d063-19e8-40b7-a28e-cf885f8dac6b")]
[assembly: AssemblyFileVersion("1.4.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.4.0.0")]
[module: UnverifiableCode]
namespace Scenic;

public static class PluginConfig
{
	public static ConfigEntry<bool> IsModEnabled { get; private set; }

	public static void BindConfig(ConfigFile config)
	{
		IsModEnabled = config.Bind<bool>("_Global", "isModEnabled", true, "Globally enable or disable this mod.");
	}
}
[HarmonyPatch(typeof(ZNetScene))]
internal static class ZNetScenePatch
{
	private static readonly List<ZDO> _zdosToRemove = new List<ZDO>();

	[HarmonyPrefix]
	[HarmonyPatch("RemoveObjects")]
	private static bool RemoveObjectsPrefix(ZNetScene __instance, List<ZDO> currentNearObjects, List<ZDO> currentDistantObjects)
	{
		//IL_018d: Unknown result type (might be due to invalid IL or missing references)
		if (!PluginConfig.IsModEnabled.Value)
		{
			return true;
		}
		byte b = (byte)((uint)Time.frameCount & 0xFFu);
		int i = 0;
		for (int count = currentNearObjects.Count; i < count; i++)
		{
			currentNearObjects[i].TempRemoveEarmark = b;
		}
		int j = 0;
		for (int count2 = currentDistantObjects.Count; j < count2; j++)
		{
			currentDistantObjects[j].TempRemoveEarmark = b;
		}
		ZDOMan s_instance = ZDOMan.s_instance;
		Dictionary<ZDO, ZNetView> instances = __instance.m_instances;
		List<ZNetView> tempRemoved = __instance.m_tempRemoved;
		tempRemoved.Clear();
		_zdosToRemove.Clear();
		foreach (KeyValuePair<ZDO, ZNetView> item in instances)
		{
			if (item.Key != null)
			{
				if (!Object.op_Implicit((Object)(object)item.Value))
				{
					_zdosToRemove.Add(item.Key);
				}
				else if (item.Key.TempRemoveEarmark != b)
				{
					tempRemoved.Add(item.Value);
				}
			}
		}
		int k = 0;
		for (int count3 = tempRemoved.Count; k < count3; k++)
		{
			ZNetView val = tempRemoved[k];
			_zdosToRemove.Add(val.m_zdo);
			val.m_zdo.Created = false;
			val.m_zdo = null;
			Object.Destroy((Object)(object)((Component)val).gameObject);
		}
		int l = 0;
		for (int count4 = _zdosToRemove.Count; l < count4; l++)
		{
			ZDO val2 = _zdosToRemove[l];
			if (!val2.Persistent && val2.Owner)
			{
				s_instance.m_destroySendList.Add(val2.m_uid);
			}
			instances.Remove(val2);
		}
		tempRemoved.Clear();
		_zdosToRemove.Clear();
		return false;
	}
}
[BepInPlugin("redseiko.valheim.scenic", "Scenic", "1.4.0")]
public sealed class Scenic : BaseUnityPlugin
{
	public const string PluginGuid = "redseiko.valheim.scenic";

	public const string PluginName = "Scenic";

	public const string PluginVersion = "1.4.0";

	private Harmony _harmony;

	private void Awake()
	{
		PluginConfig.BindConfig(((BaseUnityPlugin)this).Config);
		_harmony = Harmony.CreateAndPatchAll(Assembly.GetExecutingAssembly(), "redseiko.valheim.scenic");
	}

	private void OnDestroy()
	{
		Harmony harmony = _harmony;
		if (harmony != null)
		{
			harmony.UnpatchSelf();
		}
	}
}