Decompiled source of AutoCommands v0.4.0

Mods/AutoCommands.dll

Decompiled 2 months ago
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Resources;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using AutoCommands;
using FishNet;
using MelonLoader;
using MelonLoader.Preferences;
using Microsoft.CodeAnalysis;
using ScheduleOne;
using ScheduleOne.DevUtilities;
using ScheduleOne.Persistence;
using ScheduleOne.PlayerScripts;
using UnityEngine;
using UnityEngine.SceneManagement;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: MelonInfo(typeof(Core), "AutoCommands", "1.0.0", "Michiyo", null)]
[assembly: MelonGame("TVGS", "Schedule I")]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("AutoCommands")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("AutoCommands")]
[assembly: AssemblyTitle("AutoCommands")]
[assembly: NeutralResourcesLanguage("en-US")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[module: UnverifiableCode]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	[AttributeUsage(AttributeTargets.Module, AllowMultiple = false, Inherited = false)]
	internal sealed class RefSafetyRulesAttribute : Attribute
	{
		public readonly int Version;

		public RefSafetyRulesAttribute(int P_0)
		{
			Version = P_0;
		}
	}
}
namespace AutoCommands
{
	public class Core : MelonMod
	{
		private class CommandEntry
		{
			public string Command;

			public float Interval;

			public float LastRun;
		}

		private List<CommandEntry> _commands = new List<CommandEntry>();

		private bool? _isHost = false;

		public override void OnInitializeMelon()
		{
			MelonPreferences_Category val = MelonPreferences.CreateCategory("AutoCommands", "AutoCommands Config");
			MelonPreferences_Entry<int> val2 = val.CreateEntry<int>("CommandCount", 3, (string)null, (string)null, false, false, (ValueValidator)null, (string)null);
			((MelonPreferences_Entry)val2).Comment = "How many AutoCommand slots to load. Increase this number to add more commands";
			MelonPreferences_Entry<string> val3 = val.CreateEntry<string>("Command_0", "cleartrash; 0", (string)null, (string)null, false, false, (ValueValidator)null, (string)null);
			((MelonPreferences_Entry)val3).Comment = "Example: Run cleartrash every X seconds (disabled by default, set interval > 0)";
			MelonPreferences_Entry<string> val4 = val.CreateEntry<string>("Command_1", "save; 0", (string)null, (string)null, false, false, (ValueValidator)null, (string)null);
			((MelonPreferences_Entry)val4).Comment = "Example: Force save every X seconds (disabled by default, set interval > 0)";
			int num = Math.Max(0, val2.Value);
			for (int i = 2; i < num; i++)
			{
				MelonPreferences_Entry<string> val5 = val.CreateEntry<string>($"Command_{i}", "", $"Command entry {i + 1} (Format: command; interval)", (string)null, false, false, (ValueValidator)null, (string)null);
				((MelonPreferences_Entry)val5).Comment = $"AutoCommand {i + 1} in format: 'command; intervalInSeconds'. Set interval to 0 to disable.";
			}
			MelonPreferences.Save();
			foreach (MelonPreferences_Entry entry in val.Entries)
			{
				if (!entry.Identifier.StartsWith("Command_") || !(entry is MelonPreferences_Entry<string> val6))
				{
					continue;
				}
				string value = val6.Value;
				if (string.IsNullOrWhiteSpace(value))
				{
					continue;
				}
				string[] array = value.Split(';');
				if (array.Length != 2)
				{
					((MelonBase)this).LoggerInstance.Warning("Invalid format in " + ((MelonPreferences_Entry)val6).Identifier + ": '" + value + "'");
					continue;
				}
				string text = array[0].Trim();
				if (!float.TryParse(array[1].Trim(), out var result))
				{
					((MelonBase)this).LoggerInstance.Warning("Invalid interval in " + ((MelonPreferences_Entry)val6).Identifier + ": '" + array[1] + "'");
				}
				else if (result <= 0f)
				{
					((MelonBase)this).LoggerInstance.Msg("Skipping disabled command: '" + text + "'");
				}
				else
				{
					_commands.Add(new CommandEntry
					{
						Command = text,
						Interval = result,
						LastRun = 0f
					});
					((MelonBase)this).LoggerInstance.Msg($"Registered AutoCommand: '{text}' every {result} seconds");
				}
			}
			((MelonBase)this).LoggerInstance.Msg("AutoCommands initialized.");
		}

		public override void OnSceneWasLoaded(int buildIndex, string sceneName)
		{
			if (sceneName == "Main")
			{
				MelonCoroutines.Start(WaitForPlayerSpawn());
			}
		}

		private IEnumerator WaitForPlayerSpawn()
		{
			while ((Object)(object)Player.Local == (Object)null || (Object)(object)((Component)Player.Local).gameObject == (Object)null)
			{
				yield return null;
			}
			if (InstanceFinder.IsHost)
			{
				((MelonBase)this).LoggerInstance.Msg("Network ready. Host confirmed. Starting command loop.");
				_isHost = true;
			}
			else
			{
				((MelonBase)this).LoggerInstance.Msg("Network ready. Not host. AutoCommands disabled.");
				_isHost = false;
			}
		}

		public override void OnUpdate()
		{
			//IL_0001: Unknown result type (might be due to invalid IL or missing references)
			//IL_0006: Unknown result type (might be due to invalid IL or missing references)
			Scene activeScene = SceneManager.GetActiveScene();
			string name = ((Scene)(ref activeScene)).name;
			if (name != "Main" || !_isHost.GetValueOrDefault())
			{
				return;
			}
			float realtimeSinceStartup = Time.realtimeSinceStartup;
			foreach (CommandEntry command in _commands)
			{
				if (realtimeSinceStartup - command.LastRun >= command.Interval)
				{
					if (command.Command.Equals("save", StringComparison.OrdinalIgnoreCase) && (!Singleton<LoadManager>.Instance.IsGameLoaded || (Object)(object)Player.Local == (Object)null))
					{
						((MelonBase)this).LoggerInstance.Warning("Skipping 'save' command — game not fully loaded.");
						continue;
					}
					((MelonBase)this).LoggerInstance.Msg("Executing: " + command.Command);
					Console.SubmitCommand(command.Command);
					command.LastRun = realtimeSinceStartup;
				}
			}
		}
	}
}