Skip to content

Commit

Permalink
Port script compilation indicator from Rei
Browse files Browse the repository at this point in the history
  • Loading branch information
drojf committed Sep 21, 2022
1 parent 4ddd315 commit db65995
Show file tree
Hide file tree
Showing 2 changed files with 137 additions and 83 deletions.
196 changes: 120 additions & 76 deletions Assets.Scripts.Core/GameSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@
using Assets.Scripts.UI.Config;
using Assets.Scripts.UI.Prompt;
using MOD.Scripts.Core;
using MOD.Scripts.UI;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using UnityEngine;

namespace Assets.Scripts.Core
Expand Down Expand Up @@ -166,6 +168,7 @@ public class GameSystem : MonoBehaviour

public float AspectRatio;

private Thread CompileThread;
// Set to True to ignore normal gameplay inputs:
// - Disables normal inputs (e.g to advance text) in main GameSystem loop
// - Disables some GUI inputs by manually added checks in each button type
Expand Down Expand Up @@ -223,35 +226,14 @@ public GameState GameState
private void Initialize()
{
Logger.Log($"GameSystem: Starting GameSystem - DLL Version: {MODUtility.InformationalVersion()}");
MainUIController.InitializeToaster();
MODLocalization.LoadFromJSON();
IsInitialized = true;
AssetManager = new AssetManager();
AudioController = new AudioController();
TextController = new TextController();
TextHistory = new TextHistory();
try
{
AssetManager.CompileIfNeeded();
Logger.Log("GameSystem: Starting ScriptInterpreter");
Type type = Type.GetType(ScriptInterpreterName);
if (type == null)
{
throw new Exception("Cannot find class " + ScriptInterpreterName + " through reflection!");
}
ScriptSystem = (Activator.CreateInstance(type) as IScriptInterpreter);
if (ScriptSystem == null)
{
throw new Exception("Failed to instantiate ScriptSystem!");
}
ScriptSystem.Initialize(this);
}
catch (Exception arg)
{
Logger.LogError($"Unable to load Script Interpreter of type {ScriptInterpreterName}!\r\n{arg}");
throw;
IL_00d0:;
}
IsRunning = true;
IsRunning = false;
GameState = GameState.Normal;
curStateObj = new StateNormal();
IGameState obj = curStateObj;
Expand Down Expand Up @@ -293,11 +275,52 @@ private void Initialize()
{
Screen.SetResolution(640, 480, fullscreen: false);
}
Debug.Log("Starting compile thread...");
CompileThread = new Thread(CompileScripts)
{
IsBackground = true
};
CompileThread.Start();
if (Application.platform == RuntimePlatform.WindowsPlayer)
{
KeyHook = new KeyHook();
}
MainUIController.InitializeModMenuAndToaster(this);
}

public void StartScriptSystem()
{
Logger.Log("GameSystem: Starting ScriptInterpreter");
Type type = Type.GetType(ScriptInterpreterName);
if (type == null)
{
throw new Exception("Cannot find class " + ScriptInterpreterName + " through reflection!");
}
ScriptSystem = (Activator.CreateInstance(type) as IScriptInterpreter);
if (ScriptSystem == null)
{
throw new Exception("Failed to instantiate ScriptSystem!");
}
ScriptSystem.Initialize(this);
}

public void CompileScripts()
{
try
{
Debug.Log("Compiling!");
AssetManager.CompileIfNeeded();
}
catch (Exception arg)
{
Logger.LogError($"Unable to load Script Interpreter of type {ScriptInterpreterName}!\r\n{arg}");
throw;
}
}

public void PostLoading()
{
StartScriptSystem();
MainUIController.InitializeModMenu(this);
}

public void UpdateAspectRatio(float newratio)
Expand Down Expand Up @@ -844,72 +867,93 @@ private void LateUpdate()
}
}

private bool CheckInitialization()
{
if (!IsInitialized)
{
Initialize();
return false;
}
if (!IsRunning)
{
if (CompileThread == null || CompileThread.IsAlive)
{
if (AssetManager.MaxLoading > 0)
{
MODToaster.Show("Preparing scripts (" + AssetManager.CurrentLoading + " of " + AssetManager.MaxLoading + ")...", maybeSound: null);
}
return false;
}
IsRunning = true;
PostLoading();
}
if (SystemInit > 0)
{
return false;
}
return true;
}

private void Update()
{
if (SystemInit <= 0)
if (!CheckInitialization())
{
Logger.Update();
if (!IsInitialized)
return;
}
Logger.Update();
if (blockInputTime <= 0f)
{
if ((CanInput || GameState != GameState.Normal) && (MODIgnoreInputs() || inputHandler == null || !inputHandler()))
{
Initialize();
return;
}
else
}
else
{
blockInputTime -= Time.deltaTime;
}
if (IsRunning && CanAdvance)
{
UpdateWaits();
UpdateCarret();
try
{
if (blockInputTime <= 0f)
if (GameState == GameState.Normal)
{
if ((CanInput || GameState != GameState.Normal) && (MODIgnoreInputs() || inputHandler == null || !inputHandler()))
TextController.Update();
if (!IsSkipping)
{
return;
goto IL_0112;
}
}
else
{
blockInputTime -= Time.deltaTime;
}
if (IsRunning && CanAdvance)
{
UpdateWaits();
UpdateCarret();
try
skipWait -= Time.deltaTime;
if (!(skipWait <= 0f))
{
if (GameState == GameState.Normal)
{
TextController.Update();
if (!IsSkipping)
{
goto IL_0112;
}
skipWait -= Time.deltaTime;
if (!(skipWait <= 0f))
{
goto IL_0112;
}
if (!HasExistingWaits())
{
if (SkipModeDelay)
{
skipWait = 0.1f;
}
goto IL_0112;
}
ClearAllWaits();
}
goto end_IL_00a2;
IL_0112:
float num = Time.time + 0.01f;
while (!HasExistingWaits() && !(Time.time > num) && !HasExistingWaits() && CanAdvance)
{
ScriptSystem.Advance();
}
end_IL_00a2:;
goto IL_0112;
}
catch (Exception)
if (!HasExistingWaits())
{
IsRunning = false;
throw;
IL_0178:;
if (SkipModeDelay)
{
skipWait = 0.1f;
}
goto IL_0112;
}
ClearAllWaits();
}
goto end_IL_00a2;
IL_0112:
float num = Time.time + 0.01f;
while (!HasExistingWaits() && !(Time.time > num) && !HasExistingWaits() && CanAdvance)
{
ScriptSystem.Advance();
}
end_IL_00a2:;
}
catch (Exception)
{
IsRunning = false;
throw;
IL_0178:;
}
}
}
Expand Down
24 changes: 17 additions & 7 deletions Assets.Scripts.UI/MainUIController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -441,10 +441,10 @@ private void Update()
if (modMenu != null)
{
modMenu.Update();
}

// Handle mod keyboard shortcuts
MODKeyboardShortcuts.ModInputHandler();
// Handle mod keyboard shortcuts
MODKeyboardShortcuts.ModInputHandler();
}

int num = 402;
int num2 = 402;
Expand Down Expand Up @@ -517,10 +517,14 @@ public void TryRedrawTextWindowBackground(string windowFilterTextureName)
ui.bgLayer2.DrawLayer(windowFilterTextureName, 0, 0, 0, null, GameSystem.Instance.MessageWindowOpacity, /*isBustshot:*/ false, 0, 0f, /*isBlocking:*/ false);
}

// This must be called after BurikoScriptSystem has been initialized
public void InitializeModMenuAndToaster(GameSystem gameSystem)
public void InitializeToaster()
{
this.toaster = new MODToaster();
}

// This must be called after BurikoScriptSystem has been initialized
public void InitializeModMenu(GameSystem gameSystem)
{
this.modMenu = new MODMenu(gameSystem);

// On startup, display a toast indicating how many scripts were compiled (or failed to compile)
Expand Down Expand Up @@ -550,14 +554,20 @@ public void OnGUI()
return;
}

if(this.modMenu == null || this.toaster == null)
if(this.toaster == null)
{
return;
}

modMenu.OnGUIFragment();
toaster.OnGUIFragment();

if (this.modMenu == null)
{
return;
}

modMenu.OnGUIFragment();

// Helper Functions for processing flags
string boolDesc(string flag, string name)
{
Expand Down

0 comments on commit db65995

Please sign in to comment.