C#:
using System;
using System.Collections.Generic;
using UnityEngine;
namespace Oxide.Plugins
{
[Info("BadConsoleFilter", "BADGYVER.COM", "1.0.0")]
[Description("The sale of the add-on or the code contained in this file is prohibited")]
public class BadConsoleFilter : RustPlugin
{
private Configuration _pluginConfig;
private UnityLogInterceptor _logInterceptor;
private class Configuration
{
public bool InterceptUnityLogs { get; set; } = true;
public bool InterceptOxideLogs { get; set; } = true;
public bool KillEntityOnNavMeshError { get; set; } = true;
public List<string> IgnoredLogStrings { get; set; } = new List<string>
{
"Failed to create agent because it is not close enough to the NavMesh",
"Agent still not on navmesh after a warp. No navmesh areas matching agent type? Agent type: 0",
"Look rotation viewing vector is zero",
"alphamapResolution is clamped to the range of",
"AngryAnt Behave version",
"Checking for new Steam Item Definitions..",
"Floating point textures aren't supported on this device",
"HDR Render Texture not supported, disabling HDR on reflection probe",
"Image Effects are not supported on this platform",
"Loading Prefab Bundle",
"Missing shader in",
"Missing projectileID",
"Motion vectors not supported on a platform that does not support",
"saddletest",
"SwitchParent Missed",
"The image effect Main Camera",
"The image effect effect -",
"The referenced script",
"Unsupported encoding: 'utf8'",
"Warning, null renderer for ScaleRenderer!",
"[AmplifyColor]",
"[AmplifyOcclusion]",
"[CoverageQueries] Disabled due to unsupported",
"[CustomProbe]",
"[Manifest] URI IS",
"[SpawnHandler] populationCounts"
};
}
protected override void LoadDefaultConfig()
{
_pluginConfig = new Configuration();
SaveConfig();
}
protected override void LoadConfig()
{
base.LoadConfig();
try
{
_pluginConfig = Config.ReadObject<Configuration>();
if (_pluginConfig == null)
{
LoadDefaultConfig();
}
}
catch
{
PrintWarning("Failed to load existing configuration. Creating a new default configuration.");
LoadDefaultConfig();
}
}
protected override void SaveConfig()
{
Config.WriteObject(_pluginConfig, true);
}
private void Init()
{
if (_pluginConfig.InterceptUnityLogs)
{
_logInterceptor = new UnityLogInterceptor(Debug.unityLogger.logHandler, CheckIfMessageIgnored, _pluginConfig.KillEntityOnNavMeshError);
Debug.unityLogger.logHandler = _logInterceptor;
}
}
private void Unload()
{
if (_logInterceptor != null)
{
if (Debug.unityLogger.logHandler == _logInterceptor)
{
Debug.unityLogger.logHandler = _logInterceptor.OriginalLogHandler;
}
_logInterceptor = null;
}
}
private object OnServerMessage(string message, string name)
{
if (_pluginConfig.InterceptOxideLogs && CheckIfMessageIgnored(message))
{
return true;
}
return null;
}
private bool CheckIfMessageIgnored(string message)
{
if (string.IsNullOrEmpty(message)) return false;
foreach (var ignoredText in _pluginConfig.IgnoredLogStrings)
{
if (string.IsNullOrEmpty(ignoredText)) continue;
if (message.IndexOf(ignoredText, StringComparison.OrdinalIgnoreCase) >= 0)
{
return true;
}
}
return false;
}
private class UnityLogInterceptor : ILogHandler
{
public ILogHandler OriginalLogHandler { get; }
private readonly Func<string, bool> _shouldIgnoreFunc;
private readonly bool _killEntity;
public UnityLogInterceptor(ILogHandler originalLogHandler, Func<string, bool> shouldIgnoreFunc, bool killEntity)
{
OriginalLogHandler = originalLogHandler;
_shouldIgnoreFunc = shouldIgnoreFunc;
_killEntity = killEntity;
}
private void TryKillEntity(UnityEngine.Object context)
{
if (!_killEntity || context == null) return;
GameObject go = context as GameObject;
if (go == null)
{
Component comp = context as Component;
if (comp != null)
{
go = comp.gameObject;
}
}
if (go != null)
{
BaseEntity entity = go.GetComponent<BaseEntity>();
if (entity != null && !entity.IsDestroyed)
{
UnityEngine.Debug.LogWarning($"[BadConsoleFilter] Destroying stuck entity '{entity.ShortPrefabName}' at {entity.transform.position} to prevent NavMesh spam.");
entity.Kill();
}
}
}
public void LogFormat(LogType logType, UnityEngine.Object context, string format, params object[] args)
{
string formattedMessage = format;
try
{
if (args != null && args.Length > 0)
{
formattedMessage = string.Format(format, args);
}
}
catch (FormatException)
{
formattedMessage = format;
}
if (!string.IsNullOrEmpty(formattedMessage) && _shouldIgnoreFunc(formattedMessage))
{
TryKillEntity(context);
return;
}
OriginalLogHandler?.LogFormat(logType, context, format, args);
}
public void LogException(Exception exception, UnityEngine.Object context)
{
if (exception != null && _shouldIgnoreFunc(exception.Message))
{
TryKillEntity(context);
return;
}
OriginalLogHandler?.LogException(exception, context);
}
}
}
}