Decompiled source of TimeControlLib v0.1.0

plugins/TimeControlLib.dll

Decompiled 2 hours ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Logging;
using ExitGames.Client.Photon;
using Photon.Pun;
using Photon.Realtime;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyTitle("TimeControlLib")]
[assembly: AssemblyDescription("时间控制 API 库(闭源)")]
[assembly: AssemblyProduct("TimeControlLib")]
[assembly: AssemblyCopyright("Copyright (c) 2026 cn_xc. All rights reserved.")]
[assembly: AssemblyCompany("cn_xc")]
[assembly: AssemblyFileVersion("0.1.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyVersion("0.1.0.0")]
namespace cn_xc.TimeControl;

public static class TimeControl
{
	public static bool IsActive
	{
		get
		{
			if ((Object)(object)Runner.Instance != (Object)null)
			{
				return Runner.Instance.Active;
			}
			return false;
		}
	}

	public static float CurrentScale
	{
		get
		{
			if (!((Object)(object)Runner.Instance != (Object)null))
			{
				return 1f;
			}
			return Runner.Instance.Scale;
		}
	}

	public static event Action<float> ScaleChanged;

	public static void Slow(float scale, float duration = 0f, bool broadcast = true)
	{
		Apply(Mathf.Clamp(scale, 0.05f, 0.95f), duration, broadcast);
	}

	public static void Fast(float scale, float duration = 0f, bool broadcast = true)
	{
		Apply(Mathf.Clamp(scale, 1.05f, 4f), duration, broadcast);
	}

	public static void Apply(float scale, float duration = 0f, bool broadcast = true)
	{
		if ((Object)(object)Runner.Instance == (Object)null)
		{
			Debug.LogWarning((object)"[TimeControl] 运行器未初始化");
		}
		else
		{
			Runner.Instance.ApplyInternal(scale, duration, broadcast);
		}
	}

	public static void Restore(bool broadcast = true)
	{
		if (!((Object)(object)Runner.Instance == (Object)null))
		{
			Runner.Instance.ApplyInternal(1f, 0f, broadcast);
		}
	}

	internal static void Raise(float scale)
	{
		try
		{
			TimeControl.ScaleChanged?.Invoke(scale);
		}
		catch
		{
		}
	}
}
internal sealed class Runner : MonoBehaviour
{
	internal const string RoomPropKey = "cn_xc.TimeControl.Scale";

	internal static Runner Instance;

	internal static ManualLogSource Log;

	internal bool Active;

	internal float Scale = 1f;

	private float _target = 1f;

	private float _endAt;

	private bool _hasEnd;

	private float _defaultFixedDelta = 0.02f;

	private float _savedMoveSpeed = -1f;

	private float _savedSprint = -1f;

	private float _savedCrouch = -1f;

	private float _nextSync;

	private bool _master;

	internal static Runner Ensure(GameObject host, ManualLogSource log)
	{
		//IL_001e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0023: Unknown result type (might be due to invalid IL or missing references)
		//IL_0029: Expected O, but got Unknown
		if ((Object)(object)Instance != (Object)null)
		{
			return Instance;
		}
		Log = log;
		GameObject val = new GameObject("cn_xc.TimeControlRunner");
		Object.DontDestroyOnLoad((Object)val);
		Instance = val.AddComponent<Runner>();
		Instance._defaultFixedDelta = Time.fixedDeltaTime;
		return Instance;
	}

	internal void ApplyInternal(float scale, float duration, bool broadcast)
	{
		_target = Mathf.Clamp(scale, 0.05f, 4f);
		_hasEnd = duration > 0.001f;
		_endAt = Time.unscaledTime + duration;
		Active = Mathf.Abs(_target - 1f) > 0.0001f;
		if (!Active)
		{
			_hasEnd = false;
		}
		if (broadcast)
		{
			Publish(_target);
		}
		if (Log != null)
		{
			Log.LogInfo((object)("[TimeControl] 倍率 -> " + _target.ToString("F2") + ((duration > 0f) ? ("(" + duration + "s)") : "")));
		}
	}

	private void Update()
	{
		try
		{
			if (_hasEnd && Active && Time.unscaledTime >= _endAt)
			{
				ApplyInternal(1f, 0f, broadcast: true);
				_hasEnd = false;
			}
			if (PhotonNetwork.InRoom && !PhotonNetwork.IsMasterClient && Time.unscaledTime >= _nextSync)
			{
				_nextSync = Time.unscaledTime + 0.5f;
				try
				{
					Hashtable val = ((PhotonNetwork.CurrentRoom != null) ? ((RoomInfo)PhotonNetwork.CurrentRoom).CustomProperties : null);
					if (val != null && ((Dictionary<object, object>)(object)val).ContainsKey((object)"cn_xc.TimeControl.Scale") && val[(object)"cn_xc.TimeControl.Scale"] is float target)
					{
						_target = target;
					}
					else
					{
						_target = 1f;
					}
					Active = Mathf.Abs(_target - 1f) > 0.0001f;
				}
				catch
				{
				}
			}
			Time.timeScale = _target;
			Time.fixedDeltaTime = _defaultFixedDelta * _target;
			PlayerController instance = PlayerController.instance;
			if ((Object)(object)instance != (Object)null)
			{
				float num = 1f / Mathf.Max(_target, 0.05f);
				if (_target < 0.999f)
				{
					if (_savedMoveSpeed < 0f)
					{
						_savedMoveSpeed = instance.MoveSpeed;
						_savedSprint = instance.SprintSpeed;
						_savedCrouch = instance.CrouchSpeed;
					}
					instance.MoveSpeed = _savedMoveSpeed * num;
					instance.SprintSpeed = _savedSprint * num;
					instance.CrouchSpeed = _savedCrouch * num;
				}
				else if (_savedMoveSpeed >= 0f)
				{
					instance.MoveSpeed = _savedMoveSpeed;
					instance.SprintSpeed = _savedSprint;
					instance.CrouchSpeed = _savedCrouch;
					_savedMoveSpeed = -1f;
				}
			}
			Scale = _target;
			TimeControl.Raise(_target);
		}
		catch
		{
		}
	}

	private void Publish(float scale)
	{
		//IL_0019: Unknown result type (might be due to invalid IL or missing references)
		//IL_001e: Unknown result type (might be due to invalid IL or missing references)
		//IL_002f: Expected O, but got Unknown
		//IL_0030: Expected O, but got Unknown
		try
		{
			if (PhotonNetwork.InRoom && PhotonNetwork.CurrentRoom != null && PhotonNetwork.IsMasterClient)
			{
				Hashtable val = new Hashtable();
				((Dictionary<object, object>)val).Add((object)"cn_xc.TimeControl.Scale", (object)scale);
				Hashtable val2 = val;
				PhotonNetwork.CurrentRoom.SetCustomProperties(val2, (Hashtable)null, (WebFlags)null);
			}
		}
		catch
		{
		}
	}

	private void OnDestroy()
	{
		try
		{
			Time.timeScale = 1f;
			Time.fixedDeltaTime = _defaultFixedDelta;
		}
		catch
		{
		}
	}
}
[BepInPlugin("cn_xc.TimeControlLib", "TimeControlLib", "0.1.0")]
public class Plugin : BaseUnityPlugin
{
	private void Awake()
	{
		Runner.Ensure(((Component)this).gameObject, ((BaseUnityPlugin)this).Logger);
		((BaseUnityPlugin)this).Logger.LogInfo((object)"TimeControlLib 0.1.0 已加载(Copyright (c) 2026 cn_xc. All rights reserved.)");
	}
}