Timekeeping Improvements and Small Optimizations (#544)

* make BeatActions coroutines instead of componentrs

* pooled scheduled sounds

implement S' entity seek

* remove debug prints from last two changes

* implement absolute time tracking

implement DSP time resyncing

* optimize GameManager

* update TMPro

* update IDE packages

* fix dsp sync making the drift worse

* fix issue with the JSL dll

* relocate debug print

* make scheduled pitch setter functional

* any cpu
This commit is contained in:
minenice55
2023-09-11 18:28:04 -04:00
committed by GitHub
parent 0acaafbebd
commit 60d29f19c6
157 changed files with 49650 additions and 2194 deletions

View File

@ -3,6 +3,7 @@ using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.Pool;
using Starpelly;
using Jukebox;
@ -14,10 +15,13 @@ namespace HeavenStudio
{
public class GameManager : MonoBehaviour
{
const int SoundPoolSizeMin = 32;
const int SoundPoolSizeMax = 32;
[Header("Lists")]
[NonSerialized] public RiqBeatmap Beatmap = new();
private List<GameObject> preloadedGames = new List<GameObject>();
[NonSerialized] public List<GameObject> SoundObjects = new List<GameObject>();
private List<GameObject> preloadedGames = new();
[NonSerialized] public ObjectPool<Sound> SoundObjects;
[Header("Components")]
[NonSerialized] public Camera GameCamera, CursorCam, OverlayCamera, StaticCamera;
@ -34,7 +38,8 @@ namespace HeavenStudio
[NonSerialized] public string txt = null;
[NonSerialized] public string ext = null;
[NonSerialized] public int currentEvent, currentTempoEvent, currentVolumeEvent, currentSectionEvent,
[NonSerialized]
public int currentEvent, currentTempoEvent, currentVolumeEvent, currentSectionEvent,
currentPreEvent, currentPreSwitch, currentPreSequence;
[NonSerialized] public double endBeat;
[NonSerialized] public float startOffset;
@ -49,6 +54,8 @@ namespace HeavenStudio
bool AudioLoadDone;
bool ChartLoadError;
List<double> eventBeats, tempoBeats, volumeBeats, sectionBeats;
public event Action<double> onBeatChanged;
public event Action<RiqEntity> onSectionChange;
@ -109,7 +116,7 @@ namespace HeavenStudio
{
AudioLoadDone = false;
ChartLoadError = false;
currentPreEvent= 0;
currentPreEvent = 0;
currentPreSwitch = 0;
currentPreSequence = 0;
@ -138,7 +145,9 @@ namespace HeavenStudio
GoForAPerfect.instance.Disable();
/////
SoundObjects = new ObjectPool<Sound>(CreatePooledSound, OnTakePooledSound, OnReturnPooledSound, OnDestroyPooledSound, true, SoundPoolSizeMin, SoundPoolSizeMax);
if (preLoaded)
{
@ -168,8 +177,42 @@ namespace HeavenStudio
}
}
Sound CreatePooledSound()
{
GameObject oneShot = new GameObject($"Pooled Scheduled Sound");
oneShot.transform.SetParent(transform);
AudioSource audioSource = oneShot.AddComponent<AudioSource>();
audioSource.playOnAwake = false;
Sound snd = oneShot.AddComponent<Sound>();
oneShot.SetActive(false);
return snd;
}
// Called when an item is returned to the pool using Release
void OnReturnPooledSound(Sound snd)
{
snd.Stop();
}
// Called when an item is taken from the pool using Get
void OnTakePooledSound(Sound snd)
{
snd.gameObject.SetActive(true);
}
// If the pool capacity is reached then any items returned will be destroyed.
void OnDestroyPooledSound(Sound snd)
{
snd.Stop();
Destroy(snd.gameObject);
}
public void NewRemix()
{
{
AudioLoadDone = false;
Beatmap = new("1", "HeavenStudio");
Beatmap.data.properties = Minigames.propertiesModel;
@ -260,7 +303,7 @@ namespace HeavenStudio
if (Beatmap.data.riqOrigin != "HeavenStudio")
{
string origin = Beatmap.data.riqOrigin?.DisplayName() ?? "Unknown Origin";
GlobalGameManager.ShowErrorMessage("Warning",
GlobalGameManager.ShowErrorMessage("Warning",
$"This chart came from\n<alpha=#AA>{origin}</color>\nand uses content not included in Heaven Studio.\n\n<color=\"yellow\">You may be able to edit this chart in Heaven Studio to be used in its original program.</color>");
}
}
@ -275,7 +318,7 @@ namespace HeavenStudio
{
SkillStarManager.instance.KillStar();
}
if (SkillStarManager.instance.IsEligible && !skillStarCollected && accuracy >= 1f)
{
if (SkillStarManager.instance.DoStarJust())
@ -289,16 +332,26 @@ namespace HeavenStudio
public void SeekAheadAndPreload(double start, float seekTime = 8f)
{
List<RiqEntity> entitiesAtSameBeat = ListPool<RiqEntity>.Get();
List<RiqEntity> gameSwitchs = ListPool<RiqEntity>.Get();
Minigames.Minigame inf;
//seek ahead to preload games that have assetbundles
//check game switches first
var gameSwitchs = Beatmap.Entities.FindAll(c => c.datamodel.Split(1) == "switchGame");
foreach (RiqEntity entity in Beatmap.Entities)
{
if (entity.datamodel.Split(1) == "switchGame")
{
gameSwitchs.Add(entity);
}
}
if (currentPreSwitch < gameSwitchs.Count && currentPreSwitch >= 0)
{
if (start + seekTime >= gameSwitchs[currentPreSwitch].beat)
{
string gameName = gameSwitchs[currentPreSwitch].datamodel.Split(2);
var inf = GetGameInfo(gameName);
if (inf != null && inf.usesAssetBundle && !inf.AssetsLoaded)
inf = GetGameInfo(gameName);
if (inf != null && inf.usesAssetBundle && !inf.AssetsLoaded)
{
Debug.Log($"ASYNC loading assetbundles for game {gameName}");
StartCoroutine(inf.LoadCommonAssetBundleAsync());
@ -308,18 +361,23 @@ namespace HeavenStudio
}
}
//then check game entities
List<double> entities = Beatmap.Entities.Select(c => c.beat).ToList();
if (currentPreEvent < Beatmap.Entities.Count && currentPreEvent >= 0)
{
if (start + seekTime >= entities[currentPreEvent])
if (start + seekTime >= eventBeats[currentPreEvent])
{
var entitiesAtSameBeat = Beatmap.Entities.FindAll(c => c.beat == Beatmap.Entities[currentPreEvent].beat && !EventCaller.FXOnlyGames().Contains(EventCaller.instance.GetMinigame(c.datamodel.Split('/')[0])));
foreach (RiqEntity entity in Beatmap.Entities)
{
if (entity.beat == Beatmap.Entities[currentPreEvent].beat && !EventCaller.FXOnlyGames().Contains(eventCaller.GetMinigame(entity.datamodel.Split('/')[0])))
{
entitiesAtSameBeat.Add(entity);
}
}
SortEventsByPriority(entitiesAtSameBeat);
foreach (RiqEntity entity in entitiesAtSameBeat)
{
string gameName = entity.datamodel.Split('/')[0];
var inf = GetGameInfo(gameName);
if (inf != null && inf.usesAssetBundle && !inf.AssetsLoaded)
inf = GetGameInfo(gameName);
if (inf != null && inf.usesAssetBundle && !inf.AssetsLoaded)
{
Debug.Log($"ASYNC loading assetbundles for game {gameName}");
StartCoroutine(inf.LoadCommonAssetBundleAsync());
@ -329,29 +387,38 @@ namespace HeavenStudio
}
}
}
ListPool<RiqEntity>.Release(entitiesAtSameBeat);
ListPool<RiqEntity>.Release(gameSwitchs);
}
public void SeekAheadAndDoPreEvent(double start)
{
List<double> entities = Beatmap.Entities.Select(c => c.beat).ToList();
if (currentPreSequence < Beatmap.Entities.Count && currentPreSequence >= 0)
{
var seekEntity = Beatmap.Entities[currentPreSequence];
List<RiqEntity> entitiesAtSameBeat = ListPool<RiqEntity>.Get();
RiqEntity seekEntity = Beatmap.Entities[currentPreSequence];
float seekTime = EventCaller.instance.GetGameAction(
EventCaller.instance.GetMinigame(seekEntity.datamodel.Split(0)), seekEntity.datamodel.Split(1)).preFunctionLength;
if (start + seekTime >= entities[currentPreSequence])
foreach (RiqEntity entity in Beatmap.Entities)
{
if (entity.beat == seekEntity.beat)
{
entitiesAtSameBeat.Add(entity);
}
}
SortEventsByPriority(entitiesAtSameBeat);
float seekTime = eventCaller.GetGameAction(
eventCaller.GetMinigame(seekEntity.datamodel.Split(0)), seekEntity.datamodel.Split(1)).preFunctionLength;
if (start + seekTime >= eventBeats[currentPreSequence])
{
double beat = seekEntity.beat;
var entitiesAtSameBeat = Beatmap.Entities.FindAll(c => c.beat == seekEntity.beat);
SortEventsByPriority(entitiesAtSameBeat);
foreach (RiqEntity entity in entitiesAtSameBeat)
{
currentPreSequence++;
string gameName = entity.datamodel.Split('/')[0];
var inf = GetGameInfo(gameName);
if (inf != null && inf.usesAssetBundle && inf.AssetsLoaded && !inf.SequencesPreloaded)
if (inf != null && inf.usesAssetBundle && inf.AssetsLoaded && !inf.SequencesPreloaded)
{
Debug.Log($"Preloading game {gameName}");
PreloadGameSequences(gameName);
@ -359,6 +426,7 @@ namespace HeavenStudio
eventCaller.CallPreEvent(entity);
}
}
ListPool<RiqEntity>.Release(entitiesAtSameBeat);
}
}
@ -369,33 +437,28 @@ namespace HeavenStudio
if (!Conductor.instance.isPlaying)
return;
Conductor cond = Conductor.instance;
List<double> entities = Beatmap.Entities.Select(c => c.beat).ToList();
List<double> tempoChanges = Beatmap.TempoChanges.Select(c => c.beat).ToList();
if (currentTempoEvent < Beatmap.TempoChanges.Count && currentTempoEvent >= 0)
{
if (cond.songPositionInBeatsAsDouble >= tempoChanges[currentTempoEvent])
if (cond.songPositionInBeatsAsDouble >= tempoBeats[currentTempoEvent])
{
cond.SetBpm(Beatmap.TempoChanges[currentTempoEvent]["tempo"]);
currentTempoEvent++;
}
}
List<double> volumeChanges = Beatmap.VolumeChanges.Select(c => c.beat).ToList();
if (currentVolumeEvent < Beatmap.VolumeChanges.Count && currentVolumeEvent >= 0)
{
if (cond.songPositionInBeatsAsDouble >= volumeChanges[currentVolumeEvent])
if (cond.songPositionInBeatsAsDouble >= volumeBeats[currentVolumeEvent])
{
cond.SetVolume(Beatmap.VolumeChanges[currentVolumeEvent]["volume"]);
currentVolumeEvent++;
}
}
List<double> chartSections = Beatmap.SectionMarkers.Select(c => c.beat).ToList();
if (currentSectionEvent < Beatmap.SectionMarkers.Count && currentSectionEvent >= 0)
{
if (cond.songPositionInBeatsAsDouble >= chartSections[currentSectionEvent])
if (cond.songPositionInBeatsAsDouble >= sectionBeats[currentSectionEvent])
{
Debug.Log("Section " + Beatmap.SectionMarkers[currentSectionEvent]["sectionName"] + " started");
currentSection = Beatmap.SectionMarkers[currentSectionEvent];
@ -416,19 +479,35 @@ namespace HeavenStudio
if (currentEvent < Beatmap.Entities.Count && currentEvent >= 0)
{
if (cond.songPositionInBeatsAsDouble >= entities[currentEvent])
if (cond.songPositionInBeatsAsDouble >= eventBeats[currentEvent])
{
List<RiqEntity> entitiesAtSameBeat = ListPool<RiqEntity>.Get();
List<RiqEntity> fxEntities = ListPool<RiqEntity>.Get();
// allows for multiple events on the same beat to be executed on the same frame, so no more 1-frame delay
var entitiesAtSameBeat = Beatmap.Entities.FindAll(c => c.beat == Beatmap.Entities[currentEvent].beat && !EventCaller.FXOnlyGames().Contains(EventCaller.instance.GetMinigame(c.datamodel.Split('/')[0])));
var fxEntities = Beatmap.Entities.FindAll(c => c.beat == Beatmap.Entities[currentEvent].beat && EventCaller.FXOnlyGames().Contains(EventCaller.instance.GetMinigame(c.datamodel.Split('/')[0])));
using (PooledObject<List<RiqEntity>> pool = ListPool<RiqEntity>.Get(out List<RiqEntity> currentBeatEntities))
{
currentBeatEntities = Beatmap.Entities.FindAll(c => c.beat == Beatmap.Entities[currentEvent].beat);
foreach (RiqEntity entity in currentBeatEntities)
{
if (EventCaller.FXOnlyGames().Contains(eventCaller.GetMinigame(entity.datamodel.Split('/')[0])))
{
fxEntities.Add(entity);
}
else
{
entitiesAtSameBeat.Add(entity);
}
}
}
SortEventsByPriority(fxEntities);
SortEventsByPriority(entitiesAtSameBeat);
// FX entities should ALWAYS execute before gameplay entities
for (int i = 0; i < fxEntities.Count; i++)
foreach (RiqEntity entity in fxEntities)
{
eventCaller.CallEvent(fxEntities[i], true);
eventCaller.CallEvent(entity, true);
currentEvent++;
}
@ -447,6 +526,9 @@ namespace HeavenStudio
// Thank you to @shshwdr for bring this to my attention
currentEvent++;
}
ListPool<RiqEntity>.Release(entitiesAtSameBeat);
ListPool<RiqEntity>.Release(fxEntities);
}
}
@ -465,7 +547,8 @@ namespace HeavenStudio
}
}
private void LateUpdate() {
private void LateUpdate()
{
OverlaysManager.instance.TogleOverlaysVisibility(Editor.Editor.instance == null || Editor.Editor.instance.fullscreen || ((PersistentDataManager.gameSettings.overlaysInEditor) && (!Editor.Editor.instance.fullscreen)) || HeavenStudio.Editor.GameSettings.InPreview);
}
@ -514,7 +597,7 @@ namespace HeavenStudio
{
Util.SoundByte.UnpauseOneShots();
}
else
else
{
Conductor.instance.SetBpm(Beatmap.TempoChanges[0]["tempo"]);
Conductor.instance.SetVolume(Beatmap.VolumeChanges[0]["volume"]);
@ -544,7 +627,6 @@ namespace HeavenStudio
Conductor.instance.Stop(beat);
SetCurrentEventToClosest(beat);
onBeatChanged?.Invoke(beat);
KillAllSounds();
// I feel like I should standardize the names
SkillStarManager.instance.KillStar();
@ -555,7 +637,7 @@ namespace HeavenStudio
// pass this data to rating screen + stats
Debug.Log($"== Playthrough statistics of {Beatmap["remixtitle"]} (played at {System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}) ==");
Debug.Log($"Average input offset for playthrough: {averageInputOffset}ms");
Debug.Log($"Accuracy for playthrough: {(PlayerAccuracy * 100) : 0.00}");
Debug.Log($"Accuracy for playthrough: {(PlayerAccuracy * 100): 0.00}");
Debug.Log($"Cleared {clearedSections.FindAll(c => c).Count} sections out of {Beatmap.SectionMarkers.Count}");
if (SkillStarManager.instance.IsCollected)
Debug.Log($"Skill Star collected");
@ -563,7 +645,8 @@ namespace HeavenStudio
Debug.Log($"Skill Star not collected");
if (GoForAPerfect.instance.perfect)
Debug.Log($"Perfect Clear!");
KillAllSounds();
if (playOnStart || restart)
{
Play(0, restartDelay);
@ -573,12 +656,16 @@ namespace HeavenStudio
private IEnumerator WaitReadyAndPlayCo(double beat)
{
WaitUntil yieldOverlays = new WaitUntil(() => OverlaysManager.OverlaysReady);
WaitUntil yieldBeatmap = new WaitUntil(() => Beatmap != null && Beatmap.Entities.Count > 0);
WaitUntil yieldAudio = new WaitUntil(() => AudioLoadDone || (ChartLoadError && !GlobalGameManager.IsShowingDialog));
// wait for overlays to be ready
yield return new WaitUntil(() => OverlaysManager.OverlaysReady);
yield return yieldOverlays;
// wait for first game to be loaded
yield return new WaitUntil(() => Beatmap != null && Beatmap.Entities.Count > 0);
yield return yieldBeatmap;
//wait for audio clip to be loaded
yield return new WaitUntil(() => AudioLoadDone || (ChartLoadError && !GlobalGameManager.IsShowingDialog));
yield return yieldAudio;
SkillStarManager.instance.KillStar();
TimingAccuracyDisplay.instance.StopStarFlash();
@ -588,13 +675,12 @@ namespace HeavenStudio
GlobalGameManager.UpdateDiscordStatus(Beatmap["remixtitle"], false, true);
Play(beat, 1f);
yield break;
}
public void KillAllSounds()
{
for (int i = 0; i < SoundObjects.Count; i++)
Destroy(SoundObjects[i].gameObject);
Debug.Log("Killing all sounds");
SoundObjects.Clear();
Util.SoundByte.KillOneShots();
}
@ -609,15 +695,21 @@ namespace HeavenStudio
Beatmap.TempoChanges.Sort((x, y) => x.beat.CompareTo(y.beat));
Beatmap.VolumeChanges.Sort((x, y) => x.beat.CompareTo(y.beat));
Beatmap.SectionMarkers.Sort((x, y) => x.beat.CompareTo(y.beat));
eventBeats = Beatmap.Entities.Select(c => c.beat).ToList();
tempoBeats = Beatmap.TempoChanges.Select(c => c.beat).ToList();
volumeBeats = Beatmap.VolumeChanges.Select(c => c.beat).ToList();
sectionBeats = Beatmap.SectionMarkers.Select(c => c.beat).ToList();
}
void SortEventsByPriority(List<RiqEntity> entities)
{
entities.Sort((x, y) => {
Minigames.Minigame xGame = EventCaller.instance.GetMinigame(x.datamodel.Split(0));
Minigames.GameAction xAction = EventCaller.instance.GetGameAction(xGame, x.datamodel.Split(1));
Minigames.Minigame yGame = EventCaller.instance.GetMinigame(y.datamodel.Split(0));
Minigames.GameAction yAction = EventCaller.instance.GetGameAction(yGame, y.datamodel.Split(1));
entities.Sort((x, y) =>
{
Minigames.Minigame xGame = eventCaller.GetMinigame(x.datamodel.Split(0));
Minigames.GameAction xAction = eventCaller.GetGameAction(xGame, x.datamodel.Split(1));
Minigames.Minigame yGame = eventCaller.GetMinigame(y.datamodel.Split(0));
Minigames.GameAction yAction = eventCaller.GetGameAction(yGame, y.datamodel.Split(1));
return yAction.priority.CompareTo(xAction.priority);
});
@ -632,25 +724,57 @@ namespace HeavenStudio
return double.MinValue;
}
public static int GetIndexAfter(List<double> list, double compareTo)
{
list.Sort();
if (list.Count > 0)
{
foreach (double item in list)
{
if (item >= compareTo)
{
return Math.Max(list.IndexOf(item), 0);
}
}
return list.Count;
}
return 0;
}
public static int GetIndexBefore(List<double> list, double compareTo)
{
list.Sort();
if (list.Count > 0)
{
foreach (double item in list)
{
if (item >= compareTo)
{
return Math.Max(list.IndexOf(item) - 1, 0);
}
}
return list.Count - 1;
}
return 0;
}
public void SetCurrentEventToClosest(double beat)
{
SortEventsList();
onBeatChanged?.Invoke(beat);
if (Beatmap.Entities.Count > 0)
{
List<double> entities = Beatmap.Entities.Select(c => c.beat).ToList();
currentEvent = entities.IndexOf(GetClosestInList(entities, beat));
currentPreEvent = entities.IndexOf(GetClosestInList(entities, beat));
currentPreSequence = entities.IndexOf(GetClosestInList(entities, beat));
currentEvent = GetIndexAfter(eventBeats, beat);
currentPreEvent = GetIndexAfter(eventBeats, beat);
currentPreSequence = GetIndexAfter(eventBeats, beat);
var gameSwitchs = Beatmap.Entities.FindAll(c => c.datamodel.Split(1) == "switchGame");
string newGame = Beatmap.Entities[currentEvent].datamodel.Split(0);
string newGame = Beatmap.Entities[Math.Min(currentEvent, eventBeats.Count - 1)].datamodel.Split(0);
if (gameSwitchs.Count > 0)
{
int index = gameSwitchs.FindIndex(c => c.beat == GetClosestInList(gameSwitchs.Select(c => c.beat).ToList(), beat));
int index = GetIndexBefore(gameSwitchs.Select(c => c.beat).ToList(), beat);
currentPreSwitch = index;
var closestGameSwitch = gameSwitchs[index];
if (closestGameSwitch.beat <= beat)
@ -698,13 +822,12 @@ namespace HeavenStudio
if (Beatmap.TempoChanges.Count > 0)
{
currentTempoEvent = 0;
List<double> tempoChanges = Beatmap.TempoChanges.Select(c => c.beat).ToList();
//for tempo changes, just go over all of em until the last one we pass
for (int t = 0; t < tempoChanges.Count; t++)
for (int t = 0; t < tempoBeats.Count; t++)
{
// Debug.Log("checking tempo event " + t + " against beat " + beat + "( tc beat " + tempoChanges[t] + ")");
if (tempoChanges[t] > beat)
if (tempoBeats[t] > beat)
{
break;
}
@ -716,11 +839,10 @@ namespace HeavenStudio
if (Beatmap.VolumeChanges.Count > 0)
{
currentVolumeEvent = 0;
List<double> volumeChanges = Beatmap.VolumeChanges.Select(c => c.beat).ToList();
for (int t = 0; t < volumeChanges.Count; t++)
for (int t = 0; t < volumeBeats.Count; t++)
{
if (volumeChanges[t] > beat)
if (volumeBeats[t] > beat)
{
break;
}
@ -733,11 +855,10 @@ namespace HeavenStudio
if (Beatmap.SectionMarkers.Count > 0)
{
currentSectionEvent = 0;
List<double> beatmapSections = Beatmap.SectionMarkers.Select(c => c.beat).ToList();
for (int t = 0; t < beatmapSections.Count; t++)
for (int t = 0; t < sectionBeats.Count; t++)
{
if (beatmapSections[t] > beat)
if (sectionBeats[t] > beat)
{
break;
}
@ -774,7 +895,7 @@ namespace HeavenStudio
if (miniGame != null)
miniGame.OnGameSwitch(beat);
while(beat + 0.25 > Conductor.instance.songPositionInBeats)
while (beat + 0.25 > Conductor.instance.songPositionInBeats)
{
if (!Conductor.instance.isPlaying)
{
@ -831,6 +952,7 @@ namespace HeavenStudio
if (gameInfo.usesAssetBundle)
{
//game is packed in an assetbundle, load from that instead
// this is fucked!! figure out a way to make this async
return gameInfo.GetCommonAssetBundle().LoadAsset<GameObject>(name);
}
name = gameInfo.LoadableName;
@ -841,7 +963,7 @@ namespace HeavenStudio
public Minigames.Minigame GetGameInfo(string name)
{
return EventCaller.instance.minigames.Find(c => c.name == name);
return eventCaller.minigames.Find(c => c.name == name);
}
public void SetCurrentGame(string game, bool useMinigameColor = true)
@ -864,7 +986,7 @@ namespace HeavenStudio
private void SetAmbientGlowToCurrentMinigameColor()
{
if (GetGameInfo(currentGame) != null)
if (GetGameInfo(currentGame) != null)
HeavenStudio.StaticCamera.instance.SetAmbientGlowColour(Colors.Hex2RGB(GetGameInfo(currentGame).color), true);
}