Skip to content

Commit

Permalink
movie can run while one is running already
Browse files Browse the repository at this point in the history
  • Loading branch information
Eddio0141 committed Jun 24, 2023
1 parent 79e8b96 commit 137e6f7
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 31 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace UniTAS.Patcher.Exceptions.Movie.Runner;

public class MovieAlreadySettingUpException : MovieRunnerException
{
public MovieAlreadySettingUpException() : base("Movie is already running")
{
}
}
46 changes: 23 additions & 23 deletions UniTAS/Patcher/Implementations/Movie/MovieRunner.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using UniTAS.Patcher.Exceptions.Movie.Runner;
using UniTAS.Patcher.Interfaces.Coroutine;
using UniTAS.Patcher.Interfaces.DependencyInjection;
using UniTAS.Patcher.Interfaces.Events.MonoBehaviourEvents.DontRunIfPaused;
using UniTAS.Patcher.Interfaces.Events.Movie;
using UniTAS.Patcher.Models.Coroutine;
using UniTAS.Patcher.Models.DependencyInjection;
using UniTAS.Patcher.Models.Movie;
using UniTAS.Patcher.Services;
Expand All @@ -21,7 +24,6 @@ public class MovieRunner : IMovieRunner, IOnInputUpdateActual, IMovieRunnerEvent
private readonly IGameRestart _gameRestart;

public bool MovieEnd { get; private set; } = true;
private bool _cleanUp;
private bool _setup;

private readonly IMovieParser _parser;
Expand All @@ -35,13 +37,14 @@ public class MovieRunner : IMovieRunner, IOnInputUpdateActual, IMovieRunnerEvent
private readonly IVirtualEnvController _virtualEnvController;
private readonly ITimeEnv _timeEnv;
private readonly IRandomEnv _randomEnv;
private readonly ICoroutine _coroutine;

public UpdateType UpdateType { get; set; }

public MovieRunner(IGameRestart gameRestart, IMovieParser parser, IMovieLogger movieLogger,
IOnMovieRunningStatusChange[] onMovieRunningStatusChange,
IVirtualEnvController virtualEnvController, ITimeEnv timeEnv, IRandomEnv randomEnv, ILogger logger,
IOnMovieUpdate[] onMovieUpdates)
IOnMovieUpdate[] onMovieUpdates, ICoroutine coroutine)
{
_gameRestart = gameRestart;
_parser = parser;
Expand All @@ -52,16 +55,21 @@ public MovieRunner(IGameRestart gameRestart, IMovieParser parser, IMovieLogger m
_randomEnv = randomEnv;
_logger = logger;
_onMovieUpdates = onMovieUpdates;
_coroutine = coroutine;

_gameRestart.OnGameRestartResume += OnGameRestartResume;
}

public void RunFromInput(string input)
{
if (!MovieEnd || _setup) throw new MovieAlreadyRunningException();

if (_setup) throw new MovieAlreadySettingUpException();
_setup = true;

if (!MovieEnd)
{
MovieRunningStatusChange(false);
}

Tuple<IMovieEngine, PropertiesModel> parsed;
try
{
Expand All @@ -71,7 +79,7 @@ public void RunFromInput(string input)
{
MovieRunningStatusChange(false);
_setup = false;
_movieLogger.LogError($"Failed to run TAS movie, an exception was thrown!");
_movieLogger.LogError("Failed to run TAS movie, an exception was thrown!");
_movieLogger.LogError(e.Message);
_logger.LogDebug(e);

Expand Down Expand Up @@ -106,13 +114,6 @@ private void OnGameRestartResume(DateTime startupTime, bool preMonoBehaviourResu

public void InputUpdateActual(bool fixedUpdate, bool newInputSystemUpdate)
{
if (_cleanUp)
{
_virtualEnvController.RunVirtualEnvironment = false;
_cleanUp = false;
return;
}

if (MovieEnd) return;

// skip if update type doesn't match current update type
Expand All @@ -130,7 +131,10 @@ public void InputUpdateActual(bool fixedUpdate, bool newInputSystemUpdate)

if (_engine.Finished)
{
AtMovieEnd();
_timeEnv.FrameTime = 0;
MovieRunningStatusChange(false);
_coroutine.Start(FinishMovieCleanup());
_movieLogger.LogInfo("movie end");
}
}

Expand All @@ -142,16 +146,6 @@ private void RunUpdateEvents(bool fixedUpdate)
}
}

private void AtMovieEnd()
{
_timeEnv.FrameTime = 0;
_cleanUp = true;
_setup = false;
MovieRunningStatusChange(false);

_movieLogger.LogInfo("movie end");
}

private void MovieRunningStatusChange(bool running)
{
if (running)
Expand All @@ -170,6 +164,12 @@ private void MovieRunningStatusChange(bool running)
}
}

private IEnumerator<CoroutineWait> FinishMovieCleanup()
{
yield return new WaitForUpdateUnconditional();
_virtualEnvController.RunVirtualEnvironment = false;
}

public event Action OnMovieStart;
public event Action OnMovieEnd;
}

0 comments on commit 137e6f7

Please sign in to comment.