Files
Ultimate-Fishing-Simulator-…/Assets/Scripts/Assembly-CSharp/AutoPerformanceTests/AutoPerformanceTestManager.cs
2026-03-04 09:37:33 +08:00

180 lines
4.9 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using ECM2;
using ECM2.Examples.FirstPerson;
using QFSW.QC;
using UFS3;
using Unity.Profiling.Memory;
using UnityEngine;
using UnityEngine.Profiling;
using UnityEngine.SceneManagement;
namespace AutoPerformanceTests
{
public class AutoPerformanceTestManager : MonoBehaviour
{
[SerializeField]
private QuantumConsole _console;
[SerializeField]
private Character _character;
[SerializeField]
private CameraManager _cameraManager;
[SerializeField]
private int _movementSpeed = 10;
private PlayerInput _playerInput;
private CharacterLook _look;
private Dictionary<string, AutoPerformanceTestSetup> _setups;
private bool _testInProgress;
private Vector2 _destination;
private void Start()
{
_playerInput = _character.GetComponent<PlayerInput>();
_look = _character.GetComponent<CharacterLook>();
}
private void Update()
{
}
[Command("perfo-test", Platform.AllPlatforms, MonoTargetType.Single)]
public void StartPerformanceTest(string testName)
{
AutoPerformanceTestSetup testSetup = GetTestSetup(testName);
if (testSetup == null)
{
Debug.Log("Setup " + testName + " is invalid. Aborting.");
return;
}
if (_console != null && _console.IsActive)
{
_console.Deactivate();
}
Debug.Log("Automatic performance test started.");
_playerInput.enabled = false;
_look.enabled = false;
_character.TeleportPosition(testSetup.GetPointPosition(0), interpolating: true, updateGround: true);
_character.rotationMode = Character.RotationMode.OrientRotationToMovement;
_cameraManager.SetTPPCam();
_testInProgress = true;
string baseFileName = Path.Join(Application.persistentDataPath, SceneManager.GetActiveScene().name + "_" + testName + DateTime.Now.ToString("_yyMMdd_HHmmss"));
StartCoroutine(IterateOverPoints(testSetup, baseFileName));
}
[Command("perfo-test-stop", Platform.AllPlatforms, MonoTargetType.Single)]
public void StopPerformanceTest()
{
_testInProgress = false;
}
private AutoPerformanceTestSetup GetTestSetup(string testName)
{
_setups = new Dictionary<string, AutoPerformanceTestSetup>();
AutoPerformanceTestSetup[] array = UnityEngine.Object.FindObjectsByType<AutoPerformanceTestSetup>(FindObjectsSortMode.None);
if (array.Length < 1)
{
Debug.Log("No test setup present in current scene. Aborting.");
return null;
}
AutoPerformanceTestSetup[] array2 = array;
foreach (AutoPerformanceTestSetup autoPerformanceTestSetup in array2)
{
_setups.Add(autoPerformanceTestSetup.TestName, autoPerformanceTestSetup);
}
if (!_setups.ContainsKey(testName))
{
Debug.Log("No test setup named " + testName + " present in current scene. Aborting.");
return null;
}
return _setups[testName];
}
private IEnumerator IterateOverPoints(AutoPerformanceTestSetup setup, string baseFileName)
{
while (!_character.IsGrounded())
{
yield return new WaitForEndOfFrame();
}
for (int pointIndex = 1; pointIndex < setup.PointsCount; pointIndex++)
{
_cameraManager.CameraLookAt(setup.GetPointTransform(pointIndex));
float timer = 5f;
while (timer > 0f)
{
_character.SetMovementDirection(Vector3.zero);
timer -= Time.deltaTime;
yield return new WaitForEndOfFrame();
}
string text = baseFileName + "_" + pointIndex;
ScreenCapture.CaptureScreenshot(text + ".png");
if (setup.TakeMemorySnapshots)
{
MemorySnapshot(text);
}
if (setup.RecordProfiler)
{
Profiler.logFile = text + ".raw";
Profiler.enableBinaryLog = true;
Profiler.enabled = true;
}
while (!WalkTowardsPoint(setup.GetPointPosition(pointIndex)))
{
yield return new WaitForEndOfFrame();
}
Profiler.enabled = false;
Profiler.logFile = "";
}
_cameraManager.CameraResetLookAt();
_playerInput.enabled = true;
_look.enabled = true;
_character.rotationMode = Character.RotationMode.None;
Debug.Log("Automatic performance test ended.");
}
private void MemorySnapshot(string basePath)
{
Action<string, bool> finishCallback = delegate(string path, bool success)
{
if (success)
{
Debug.Log("Memory snapshot captured successfully at " + path);
}
else
{
Debug.LogError("Failed to capture memory snapshot.");
}
};
MemoryProfiler.TakeSnapshot(basePath + ".snap", finishCallback);
}
private bool WalkTowardsPoint(Vector3 destination)
{
Vector3 zero = Vector3.zero;
Vector2 zero2 = Vector2.zero;
zero2.x = destination.x;
zero2.y = destination.z;
zero += (destination - _character.transform.position).normalized * _movementSpeed;
_character.SetMovementDirection(zero);
Vector2 a = default(Vector2);
a.x = _character.transform.position.x;
a.y = _character.transform.position.z;
if (Vector2.Distance(a, zero2) < 1f)
{
zero = Vector3.zero;
return true;
}
return false;
}
}
}