Decompiled source of BrightBlueDeer v1.0.0

BrightBlueDeer.dll

Decompiled a day ago
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyVersion("0.0.0.0")]
namespace BrightBlueDeer;

public static class ColorSetting
{
	public static bool TryParse(string text, out byte red, out byte green, out byte blue)
	{
		red = (green = (blue = 0));
		string[] array = (text ?? "").Split(',');
		if (array.Length == 3 && byte.TryParse(array[0].Trim(), out red) && byte.TryParse(array[1].Trim(), out green))
		{
			return byte.TryParse(array[2].Trim(), out blue);
		}
		return false;
	}
}
[BepInPlugin("com.parallel53.brightbluedeer", "Bright Blue Deer", "1.0.0")]
public class BrightBlueDeerPlugin : BaseUnityPlugin
{
	private const string DeerPrefab = "Deer";

	private const string HidePrefab = "DeerHide";

	private const string RugPrefab = "rug_deer";

	private const string CrowPrefab = "Crow";

	private const float DeerMinBrightness = 0.55f;

	private const float CrowMinBrightness = 0.9f;

	private const float BrightnessGain = 0.9f;

	private const string DefaultColor = "20, 115, 255";

	private const string DefaultCrowColor = "255, 200, 40";

	private static readonly Dictionary<Material, Material> PaintedMaterials = new Dictionary<Material, Material>();

	private static ManualLogSource log;

	private static Color deerColor;

	private static Color crowColor;

	private void Awake()
	{
		//IL_0088: Unknown result type (might be due to invalid IL or missing references)
		//IL_008d: 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_00a2: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ac: Unknown result type (might be due to invalid IL or missing references)
		log = ((BaseUnityPlugin)this).Logger;
		ConfigEntry<bool> obj = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Enabled", true, "Paint deer, deer hides, the deer rug and crows. Takes effect the next time the game starts.");
		ConfigEntry<string> val = ((BaseUnityPlugin)this).Config.Bind<string>("General", "Color", "20, 115, 255", "Deer colour as red, green, blue, each from 0 to 255. Takes effect the next time the game starts.");
		ConfigEntry<string> val2 = ((BaseUnityPlugin)this).Config.Bind<string>("General", "BirdColor", "255, 200, 40", "Crow colour as red, green, blue, each from 0 to 255. Takes effect the next time the game starts.");
		if (!obj.Value)
		{
			log.LogInfo((object)"Disabled in config");
			return;
		}
		deerColor = ParseColor(val.Value, "20, 115, 255");
		crowColor = ParseColor(val2.Value, "255, 200, 40");
		new Harmony("com.parallel53.brightbluedeer").PatchAll(typeof(BrightBlueDeerPlugin));
	}

	private static Color ParseColor(string text, string fallback)
	{
		//IL_003e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0043: Unknown result type (might be due to invalid IL or missing references)
		if (!ColorSetting.TryParse(text, out var red, out var green, out var blue))
		{
			log.LogWarning((object)("Color \"" + text + "\" is not red, green, blue from 0 to 255. Using " + fallback));
			ColorSetting.TryParse(fallback, out red, out green, out blue);
		}
		return Color32.op_Implicit(new Color32(red, green, blue, byte.MaxValue));
	}

	[HarmonyPostfix]
	[HarmonyPatch(typeof(ZNetScene), "Awake")]
	private static void PaintDeer(ZNetScene __instance)
	{
		//IL_0026: Unknown result type (might be due to invalid IL or missing references)
		//IL_0094: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a9: Unknown result type (might be due to invalid IL or missing references)
		//IL_00be: 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)
		GameObject prefab = __instance.GetPrefab("Deer");
		if ((Object)(object)prefab == (Object)null)
		{
			log.LogWarning((object)"No Deer prefab found");
			return;
		}
		Paint(prefab, deerColor, 0.55f);
		EffectData[] effectPrefabs = prefab.GetComponent<Character>().m_deathEffects.m_effectPrefabs;
		foreach (EffectData val in effectPrefabs)
		{
			if ((Object)(object)val.m_prefab != (Object)null && (Object)(object)val.m_prefab.GetComponent<Ragdoll>() != (Object)null)
			{
				Paint(val.m_prefab, deerColor, 0.55f);
			}
		}
		PaintIfFound(__instance, "DeerHide", deerColor, 0.55f);
		PaintIfFound(__instance, "rug_deer", deerColor, 0.55f);
		PaintIfFound(__instance, "Crow", crowColor, 0.9f);
	}

	private static void PaintIfFound(ZNetScene scene, string name, Color color, float minBrightness)
	{
		//IL_008c: Unknown result type (might be due to invalid IL or missing references)
		GameObject prefab = scene.GetPrefab(name);
		if ((Object)(object)prefab == (Object)null)
		{
			string[] value = (from candidate in scene.m_prefabs
				where (Object)(object)candidate.GetComponent<RandomFlyingBird>() != (Object)null
				select ((Object)candidate).name).ToArray();
			log.LogWarning((object)("No " + name + " prefab found. Birds in the game: " + string.Join(", ", value)));
		}
		else
		{
			Paint(prefab, color, minBrightness);
		}
	}

	private static void Paint(GameObject prefab, Color color, float minBrightness)
	{
		//IL_0031: Unknown result type (might be due to invalid IL or missing references)
		Renderer[] componentsInChildren = prefab.GetComponentsInChildren<Renderer>(true);
		foreach (Renderer val in componentsInChildren)
		{
			if (!(val is ParticleSystemRenderer))
			{
				Material[] sharedMaterials = val.sharedMaterials;
				for (int j = 0; j < sharedMaterials.Length; j++)
				{
					sharedMaterials[j] = Painted(sharedMaterials[j], ((Object)prefab).name, color, minBrightness);
				}
				val.sharedMaterials = sharedMaterials;
			}
		}
	}

	private static Material Painted(Material material, string prefabName, Color color, float minBrightness)
	{
		//IL_003a: Unknown result type (might be due to invalid IL or missing references)
		//IL_003f: 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_0053: Expected O, but got Unknown
		//IL_0061: Unknown result type (might be due to invalid IL or missing references)
		if ((Object)(object)material == (Object)null || PaintedMaterials.ContainsValue(material))
		{
			return material;
		}
		if (PaintedMaterials.TryGetValue(material, out var value))
		{
			return value;
		}
		if ((Object)(object)material.mainTexture == (Object)null)
		{
			return material;
		}
		value = new Material(material)
		{
			mainTexture = (Texture)(object)PaintedTexture(material.mainTexture, color, minBrightness)
		};
		if (value.HasProperty("_Color"))
		{
			value.color = Color.white;
		}
		PaintedMaterials[material] = value;
		log.LogInfo((object)(prefabName + ": painted " + ((Object)material).name + " (" + ((Object)material.shader).name + ")"));
		return value;
	}

	private static Texture2D PaintedTexture(Texture source, Color color, float minBrightness)
	{
		//IL_0036: Unknown result type (might be due to invalid IL or missing references)
		//IL_003c: Expected O, but got Unknown
		//IL_0055: Unknown result type (might be due to invalid IL or missing references)
		//IL_0095: 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_00b6: Unknown result type (might be due to invalid IL or missing references)
		//IL_00b8: Unknown result type (might be due to invalid IL or missing references)
		RenderTexture temporary = RenderTexture.GetTemporary(source.width, source.height, 0, (RenderTextureFormat)0, (RenderTextureReadWrite)2);
		RenderTexture active = RenderTexture.active;
		Graphics.Blit(source, temporary);
		RenderTexture.active = temporary;
		Texture2D val = new Texture2D(source.width, source.height, (TextureFormat)5, true);
		val.ReadPixels(new Rect(0f, 0f, (float)source.width, (float)source.height), 0, 0);
		RenderTexture.active = active;
		RenderTexture.ReleaseTemporary(temporary);
		Color[] pixels = val.GetPixels();
		for (int i = 0; i < pixels.Length; i++)
		{
			float num = Mathf.Clamp01(minBrightness + ((Color)(ref pixels[i])).maxColorComponent * 0.9f);
			Color val2 = color * num;
			val2.a = pixels[i].a;
			pixels[i] = val2;
		}
		val.SetPixels(pixels);
		val.Apply(true, true);
		return val;
	}
}