Decompiled source of FrostChat v0.1.6

FrostChat_v0.1.6.dll

Decompiled 12 hours ago
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Text;
using BepInEx;
using BepInEx.Configuration;
using HarmonyLib;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("FrostChat")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("0.1.6.0")]
[assembly: AssemblyInformationalVersion("0.1.6")]
[assembly: AssemblyProduct("FrostChat")]
[assembly: AssemblyTitle("FrostChat")]
[assembly: AssemblyVersion("0.1.6.0")]
namespace FrostChat;

[BepInPlugin("DEEP_FROST.FrostChat", "FrostChat", "0.1.6")]
[BepInProcess("valheim.exe")]
public class FrostChatPlugin : BaseUnityPlugin
{
	public const string PluginGUID = "DEEP_FROST.FrostChat";

	public const string PluginName = "FrostChat";

	public const string PluginVersion = "0.1.6";

	private ConfigEntry<bool> _enabled;

	private ConfigEntry<string> _channel;

	private ConfigEntry<string>[] _phrases;

	private ConfigEntry<bool> _asEnabled;

	private ConfigEntry<int> _asMax;

	private ConfigEntry<int> _asWindow;

	private ConfigEntry<int> _asBlockMinutes;

	private ConfigEntry<string> _asBlockPhrase;

	private int _streak;

	private DateTime _streakStart = DateTime.MinValue;

	private DateTime _blockedUntil = DateTime.MinValue;

	private float _nextBlockHint;

	private void Awake()
	{
		_enabled = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Enabled", true, "Включён ли мод");
		_channel = ((BaseUnityPlugin)this).Config.Bind<string>("General", "Channel", "s", "Канал фраз без префикса: s = крик (весь мир), say = рядом, w = шёпот");
		_phrases = new ConfigEntry<string>[9];
		for (int i = 0; i < 9; i++)
		{
			_phrases[i] = ((BaseUnityPlugin)this).Config.Bind<string>("Phrases", "Num" + (i + 1), "", "Фраза Num" + (i + 1) + " (пусто = выкл). /s крик, /w шёпот, / команда");
		}
		_asEnabled = ((BaseUnityPlugin)this).Config.Bind<bool>("AntiSpam", "Enabled", true, "Защита от спама (вкл/выкл)");
		_asMax = ((BaseUnityPlugin)this).Config.Bind<int>("AntiSpam", "MaxInRow", 5, "Фраз подряд до блокировки");
		_asWindow = ((BaseUnityPlugin)this).Config.Bind<int>("AntiSpam", "WindowSeconds", 90, "Окно серии, секунд");
		_asBlockMinutes = ((BaseUnityPlugin)this).Config.Bind<int>("AntiSpam", "BlockMinutes", 30, "Блокировка биндов, минут");
		_asBlockPhrase = ((BaseUnityPlugin)this).Config.Bind<string>("AntiSpam", "BlockPhrase", "", "Фраза в чат при блокировке (пусто = нет)");
		ZLog.Log((object)"[FrostChat] 0.1.6 загружен");
	}

	private void Update()
	{
		if (!_enabled.Value)
		{
			return;
		}
		Chat instance = Chat.instance;
		if ((Object)(object)instance == (Object)null)
		{
			return;
		}
		int num = -1;
		for (int i = 1; i <= 9; i++)
		{
			if (Input.GetKeyDown((KeyCode)(257 + i - 1)))
			{
				num = i;
				break;
			}
		}
		bool keyDown = Input.GetKeyDown((KeyCode)270);
		if (Console.IsVisible() || TextInput.IsVisible())
		{
			return;
		}
		object? obj = AccessTools.Field(typeof(Terminal), "m_input")?.GetValue(instance);
		Component val = (Component)((obj is Component) ? obj : null);
		object obj2 = ((!((Object)(object)val != (Object)null)) ? null : AccessTools.Property(((object)val).GetType(), "isFocused")?.GetValue(val, null));
		bool flag = default(bool);
		int num2;
		if ((Object)(object)val != (Object)null && val.gameObject.activeSelf)
		{
			if (obj2 is bool)
			{
				flag = (bool)obj2;
				num2 = 1;
			}
			else
			{
				num2 = 0;
			}
		}
		else
		{
			num2 = 0;
		}
		if (((uint)num2 & (flag ? 1u : 0u)) == 0)
		{
			if (num > 0)
			{
				HandleBind(num);
			}
			else if (keyDown)
			{
				ShowList();
			}
		}
	}

	private string PhraseFor(int index)
	{
		string value = _phrases[index - 1].Value;
		if (value != null)
		{
			return value.Trim();
		}
		return "";
	}

	private void HandleBind(int index)
	{
		if (_asEnabled.Value && DateTime.UtcNow < _blockedUntil)
		{
			TimeSpan timeSpan = _blockedUntil - DateTime.UtcNow;
			if (Time.time >= _nextBlockHint)
			{
				if ((Object)(object)MessageHud.instance != (Object)null)
				{
					MessageHud.instance.ShowMessage((MessageType)1, "FrostChat: антиспам — бинды заблокированы ещё " + timeSpan.Minutes + " мин " + timeSpan.Seconds + " с", 0, (Sprite)null, false, true);
				}
				_nextBlockHint = Time.time + 3f;
			}
			return;
		}
		string text = PhraseFor(index);
		if (string.IsNullOrWhiteSpace(text))
		{
			return;
		}
		if (_asEnabled.Value)
		{
			DateTime utcNow = DateTime.UtcNow;
			if (_streak > 0 && (utcNow - _streakStart).TotalSeconds > (double)_asWindow.Value)
			{
				_streak = 0;
			}
			if (_streak == 0)
			{
				_streakStart = utcNow;
			}
			_streak++;
			if (_streak >= _asMax.Value)
			{
				TriggerBlock();
				return;
			}
			ZLog.Log((object)$"[FrostChat] антиспам: серия {_streak}/{_asMax.Value}");
		}
		SendNow(text);
	}

	private void TriggerBlock()
	{
		_blockedUntil = DateTime.UtcNow.AddMinutes(_asBlockMinutes.Value);
		_streak = 0;
		ZLog.Log((object)("[FrostChat] антиспам: бинды заблокированы до " + _blockedUntil.ToLocalTime().ToString("HH:mm")));
		string value = _asBlockPhrase.Value;
		if (!string.IsNullOrWhiteSpace(value))
		{
			SendNow(value);
		}
		if ((Object)(object)MessageHud.instance != (Object)null)
		{
			MessageHud.instance.ShowMessage((MessageType)1, "FrostChat: антиспам — бинды заблокированы на " + _asBlockMinutes.Value + " мин", 0, (Sprite)null, false, true);
		}
	}

	private void SendNow(string phrase)
	{
		Chat instance = Chat.instance;
		if (!((Object)(object)instance == (Object)null))
		{
			string text = (phrase.StartsWith("/") ? phrase.Substring(1) : (_channel.Value.Trim() + " " + phrase));
			((Terminal)instance).TryRunCommand(text, false, false);
			ZLog.Log((object)("[FrostChat] отправлено: " + text));
		}
	}

	private void ShowList()
	{
		if ((Object)(object)MessageHud.instance == (Object)null)
		{
			return;
		}
		StringBuilder stringBuilder = new StringBuilder("FrostChat (Num+)");
		bool flag = false;
		for (int i = 1; i <= 9; i++)
		{
			string value = PhraseFor(i);
			if (!string.IsNullOrWhiteSpace(value))
			{
				stringBuilder.Append("\nNum").Append(i).Append(": ")
					.Append(value);
				flag = true;
			}
		}
		if (!flag)
		{
			stringBuilder.Append("\nфраз нет — заполни Num1-9 в DEEP_FROST.FrostChat.cfg");
		}
		if (_asEnabled.Value && DateTime.UtcNow < _blockedUntil)
		{
			stringBuilder.Append("\nантиспам: бинды заблокированы до ").Append(_blockedUntil.ToLocalTime().ToString("HH:mm"));
		}
		MessageHud.instance.ShowMessage((MessageType)1, stringBuilder.ToString(), 0, (Sprite)null, false, true);
		ZLog.Log((object)"[FrostChat] список фраз выведен в HUD");
	}
}