Wrap Main.Instance.ModHelper.Events.Unity into Delay

This commit is contained in:
Nick 2022-07-13 23:48:43 -04:00
parent 64b12ecb00
commit 5c5b4e0764
19 changed files with 43 additions and 31 deletions

View File

@ -44,9 +44,9 @@ namespace NewHorizons.Builder.Body
cloakSectorController.EnableCloak();
// To cloak from the start
Main.Instance.ModHelper.Events.Unity.FireOnNextUpdate(cloakSectorController.OnPlayerExit);
Main.Instance.ModHelper.Events.Unity.FireOnNextUpdate(hasCustomAudio ? cloakSectorController.TurnOnMusic : cloakSectorController.TurnOffMusic);
Main.Instance.ModHelper.Events.Unity.FireOnNextUpdate(keepReferenceFrame ? cloakSectorController.EnableReferenceFrameVolume : cloakSectorController.DisableReferenceFrameVolume);
Delay.FireOnNextUpdate(cloakSectorController.OnPlayerExit);
Delay.FireOnNextUpdate(hasCustomAudio ? cloakSectorController.TurnOnMusic : cloakSectorController.TurnOffMusic);
Delay.FireOnNextUpdate(keepReferenceFrame ? cloakSectorController.EnableReferenceFrameVolume : cloakSectorController.DisableReferenceFrameVolume);
}
}
}

View File

@ -159,7 +159,7 @@ namespace NewHorizons.Builder.Body
funnelSizeController.anchor = planetGO.transform;
// Finish up next tick
Main.Instance.ModHelper.Events.Unity.FireOnNextUpdate(() => PostMake(funnelGO, funnelSizeController, module));
Delay.FireOnNextUpdate(() => PostMake(funnelGO, funnelSizeController, module));
}
private static void PostMake(GameObject funnelGO, FunnelController funnelSizeController, FunnelModule module)

View File

@ -55,7 +55,7 @@ namespace NewHorizons.Builder.Body
OLC._ambientLight = GDOLC._ambientLight; // this needs to be set or else is black
// trigger sector enter
Main.Instance.ModHelper.Events.Unity.FireOnNextUpdate(() =>
Delay.FireOnNextUpdate(() =>
{
OEC._active = true;
OEC.enabled = true;

View File

@ -1,5 +1,6 @@
using NewHorizons.Components.Orbital;
using NewHorizons.External.Configs;
using NewHorizons.Utility;
using UnityEngine;
using Logger = NewHorizons.Utility.Logger;
namespace NewHorizons.Builder.General
@ -51,7 +52,7 @@ namespace NewHorizons.Builder.General
{
Logger.Log($"Setting center of universe to {config.name}");
Main.Instance.ModHelper.Events.Unity.RunWhen(
Delay.RunWhen(
() => Locator._centerOfTheUniverse != null,
() => Locator._centerOfTheUniverse._staticReferenceFrame = astroObject.GetComponent<OWRigidbody>()
);

View File

@ -78,7 +78,7 @@ namespace NewHorizons.Builder.General
if ((Main.Instance.IsWarpingFromVessel || (!Main.Instance.IsWarpingFromShip && module.startWithSuit)) && !suitUpQueued)
{
suitUpQueued = true;
Main.Instance.ModHelper.Events.Unity.RunWhen(() => Main.IsSystemReady, () => SuitUp());
Delay.RunWhen(() => Main.IsSystemReady, () => SuitUp());
}
Logger.Log($"Made spawnpoint on [{planetGO.name}]");

View File

@ -78,7 +78,7 @@ namespace NewHorizons.Builder.Orbital
orbitLine._numVerts = (int)Mathf.Clamp(config.Orbit.semiMajorAxis / 1000f, numVerts, 4096);
Main.Instance.ModHelper.Events.Unity.FireOnNextUpdate(orbitLine.InitializeLineRenderer);
Delay.FireOnNextUpdate(orbitLine.InitializeLineRenderer);
return orbitLine;
}

View File

@ -125,7 +125,7 @@ namespace NewHorizons.Builder.Props
// Enable all children or something
// BUG doesnt work because enabled is a property, not a field
var enabledField = component?.GetType()?.GetField("enabled");
if (enabledField != null && enabledField.FieldType == typeof(bool)) Main.Instance.ModHelper.Events.Unity.FireOnNextUpdate(() => enabledField.SetValue(component, true));
if (enabledField != null && enabledField.FieldType == typeof(bool)) Events.FireOnNextUpdate(() => enabledField.SetValue(component, true));
*/
// Fix a bunch of sector stuff
@ -167,7 +167,7 @@ namespace NewHorizons.Builder.Props
var sectorField = component?.GetType()?.GetField("_sector");
if (sectorField != null && sectorField.FieldType == typeof(Sector))
{
Main.Instance.ModHelper.Events.Unity.FireOnNextUpdate(() => sectorField.SetValue(component, sector));
Events.FireOnNextUpdate(() => sectorField.SetValue(component, sector));
}
}
*/
@ -227,7 +227,7 @@ namespace NewHorizons.Builder.Props
}
// Fix a bunch of stuff when done loading
Main.Instance.ModHelper.Events.Unity.RunWhen(() => Main.IsSystemReady, () =>
Delay.RunWhen(() => Main.IsSystemReady, () =>
{
try
{

View File

@ -32,7 +32,7 @@ namespace NewHorizons.Builder.Props
var geyserFluidVolume = geyserGO.GetComponentInChildren<GeyserFluidVolume>();
// Do this after awake
Main.Instance.ModHelper.Events.Unity.FireOnNextUpdate(() => geyserFluidVolume._maxHeight = 1);
Delay.FireOnNextUpdate(() => geyserFluidVolume._maxHeight = 1);
geyserFluidVolume.enabled = true;
geyserGO.transform.Find("FluidVolume_Geyser").GetComponent<CapsuleShape>().enabled = true;

View File

@ -165,7 +165,7 @@ namespace NewHorizons.Builder.Props
customScroll.SetActive(true);
// Enable the collider and renderer
Main.Instance.ModHelper.Events.Unity.RunWhen(
Delay.RunWhen(
() => Main.IsSystemReady,
() =>
{

View File

@ -29,7 +29,7 @@ namespace NewHorizons.Builder.Props
{
heightMapTexture = ImageUtilities.GetTexture(mod, heightMap.heightMap);
// defer remove texture to next frame
Main.Instance.ModHelper.Events.Unity.FireOnNextUpdate(() => Object.Destroy(heightMapTexture));
Delay.FireOnNextUpdate(() => Object.Destroy(heightMapTexture));
}
catch (Exception) { }
if (heightMapTexture == null)

View File

@ -118,7 +118,7 @@ namespace NewHorizons.Builder.Props
// Resize the distance it can be heard from to match roughly with the size
var maxDistance = info.audioDistance;
if (maxDistance <= 0) maxDistance = scale * 10f;
Main.Instance.ModHelper.Events.Unity.FireOnNextUpdate(() =>
Delay.FireOnNextUpdate(() =>
{
audioSource.maxDistance = maxDistance;
audioSource.minDistance = maxDistance / 10f;

View File

@ -37,7 +37,7 @@ namespace NewHorizons.Builder.Props
launcherGO.SetActive(true);
// Have to null check else it breaks on reload configs
Main.Instance.ModHelper.Events.Unity.RunWhen(() => Main.IsSystemReady && meteorLauncher._meteorPool != null, () =>
Delay.RunWhen(() => Main.IsSystemReady && meteorLauncher._meteorPool != null, () =>
{
foreach (var meteor in meteorLauncher._meteorPool)
{

View File

@ -114,7 +114,7 @@ namespace NewHorizons.Components
if (name.Length > 17) textComponent.fontSize = 10;
// Do it next frame
var fontPath = "Ship_Body/Module_Cabin/Systems_Cabin/ShipLogPivot/ShipLog/ShipLogPivot/ShipLogCanvas/DetectiveMode/ScaleRoot/PanRoot/TH_VILLAGE/EntryCardRoot/NameBackground/Name";
Main.Instance.ModHelper.Events.Unity.FireOnNextUpdate(() => textComponent.font = SearchUtilities.Find(fontPath).GetComponent<Text>().font);
Delay.FireOnNextUpdate(() => textComponent.font = SearchUtilities.Find(fontPath).GetComponent<Text>().font);
newCard.SetActive(true);
newCard.transform.name = uniqueID;

View File

@ -121,7 +121,7 @@ namespace NewHorizons.Components
{
if (_isWarpingIn && LateInitializerManager.isDoneInitializing)
{
Main.Instance.ModHelper.Events.Unity.FireInNUpdates(() => StartWarpInEffect(), 1);
Delay.FireInNUpdates(() => StartWarpInEffect(), 1);
_isWarpingIn = false;
}
@ -139,7 +139,7 @@ namespace NewHorizons.Components
// Idk whats making this work but now it works and idc
if (_waitingToBeSeated && PlayerState.IsInsideShip() && _eyesOpen)
{
Main.Instance.ModHelper.Events.Unity.FireInNUpdates(() => FinishWarpIn(), 1);
Delay.FireInNUpdates(() => FinishWarpIn(), 1);
_waitingToBeSeated = false;
}
}
@ -174,7 +174,7 @@ namespace NewHorizons.Components
Logger.LogVerbose("Finishing warp");
Locator.GetShipBody().GetComponentInChildren<ShipCockpitController>().OnPressInteract();
_waitingToBeSeated = false;
Main.Instance.ModHelper.Events.Unity.FireInNUpdates(() => _whitehole.Collapse(), 30);
Delay.FireInNUpdates(() => _whitehole.Collapse(), 30);
var resources = Locator.GetPlayerTransform().GetComponent<PlayerResources>();

View File

@ -19,7 +19,7 @@ namespace NewHorizons.Handlers
_customAudioTypes = new Dictionary<string, AudioType>();
_audioEntries = new List<AudioLibrary.AudioEntry>();
Main.Instance.ModHelper.Events.Unity.RunWhen(
Delay.RunWhen(
() => Locator.GetAudioManager()?._libraryAsset != null,
PostInit
);

View File

@ -138,7 +138,7 @@ namespace NewHorizons.Handlers
Logger.Log("Done loading bodies");
// Main.Instance.ModHelper.Events.Unity.FireOnNextUpdate(PlanetDestroyer.RemoveAllProxies);
// Events.FireOnNextUpdate(PlanetDestroyer.RemoveAllProxies);
if (Main.SystemDict[Main.Instance.CurrentStarSystem].Config.destroyStockPlanets) PlanetDestructionHandler.RemoveSolarSystem();
}
@ -164,8 +164,8 @@ namespace NewHorizons.Handlers
if (body.Config.destroy)
{
var ao = existingPlanet.GetComponent<AstroObject>();
if (ao != null) Main.Instance.ModHelper.Events.Unity.FireInNUpdates(() => PlanetDestructionHandler.RemoveBody(ao), 2);
else Main.Instance.ModHelper.Events.Unity.FireInNUpdates(() => existingPlanet.SetActive(false), 2);
if (ao != null) Delay.FireInNUpdates(() => PlanetDestructionHandler.RemoveBody(ao), 2);
else Delay.FireInNUpdates(() => existingPlanet.SetActive(false), 2);
}
else if (body.Config.isQuantumState)
{
@ -279,7 +279,7 @@ namespace NewHorizons.Handlers
{
// We purposefully use GameObject.Find here because we don't want to find inactive things.
// If you were to try and disable two children with the same name, if we were finding inactive then we'd disable the first one twice
Main.Instance.ModHelper.Events.Unity.FireInNUpdates(() => GameObject.Find(go.name + "/" + child)?.SetActive(false), 2);
Delay.FireInNUpdates(() => GameObject.Find(go.name + "/" + child)?.SetActive(false), 2);
}
}
@ -417,7 +417,7 @@ namespace NewHorizons.Handlers
if (body.Config.Orbit.showOrbitLine && !body.Config.Orbit.isStatic)
{
Main.Instance.ModHelper.Events.Unity.FireOnNextUpdate(() => OrbitlineBuilder.Make(body.Object, ao as NHAstroObject, body.Config.Orbit.isMoon, body.Config));
Delay.FireOnNextUpdate(() => OrbitlineBuilder.Make(body.Object, ao as NHAstroObject, body.Config.Orbit.isMoon, body.Config));
}
if (!body.Config.Orbit.isStatic)
@ -429,7 +429,7 @@ namespace NewHorizons.Handlers
if (!(body.Config.Cloak != null && body.Config.Cloak.radius != 0f))
{
Main.Instance.ModHelper.Events.Unity.FireOnNextUpdate(() =>
Delay.FireOnNextUpdate(() =>
{
ProxyBuilder.Make(go, body);
});

View File

@ -54,12 +54,12 @@ namespace NewHorizons.Handlers
foreach (var name in _solarSystemBodies)
{
var ao = AstroObjectLocator.GetAstroObject(name);
if (ao != null) Main.Instance.ModHelper.Events.Unity.FireInNUpdates(() => RemoveBody(ao, false), 2);
if (ao != null) Delay.FireInNUpdates(() => RemoveBody(ao, false), 2);
else Logger.LogError($"Couldn't find [{name}]");
}
// Bring the sun back because why not
Main.Instance.ModHelper.Events.Unity.FireInNUpdates(() => { if (Locator.GetAstroObject(AstroObject.Name.Sun).gameObject.activeInHierarchy) { sunVolumes.SetActive(true); } }, 3);
Delay.FireInNUpdates(() => { if (Locator.GetAstroObject(AstroObject.Name.Sun).gameObject.activeInHierarchy) { sunVolumes.SetActive(true); } }, 3);
}
public static void RemoveBody(AstroObject ao, bool delete = false, List<AstroObject> toDestroy = null)
@ -198,7 +198,7 @@ namespace NewHorizons.Handlers
}
RemoveProxy(ao.name.Replace("_Body", ""));
Main.Instance.ModHelper.Events.Unity.RunWhen(() => Main.IsSystemReady, () => DisableBody(ao.gameObject, delete));
Delay.RunWhen(() => Main.IsSystemReady, () => DisableBody(ao.gameObject, delete));
foreach (ProxyBody proxy in GameObject.FindObjectsOfType<ProxyBody>())
{

View File

@ -30,7 +30,7 @@ namespace NewHorizons.Handlers
if (!string.IsNullOrEmpty(system.Config.travelAudio))
{
Main.Instance.ModHelper.Events.Unity.FireOnNextUpdate(() => AudioUtilities.SetAudioClip(Locator.GetGlobalMusicController()._travelSource, system.Config.travelAudio, system.Mod));
Delay.FireOnNextUpdate(() => AudioUtilities.SetAudioClip(Locator.GetGlobalMusicController()._travelSource, system.Config.travelAudio, system.Mod));
}
}
}

View File

@ -0,0 +1,11 @@
using System;
namespace NewHorizons.Utility
{
public static class Delay
{
public static void RunWhen(Func<bool> predicate, Action action) => Main.Instance.ModHelper.Events.Unity.RunWhen(predicate, action);
public static void FireInNUpdates(Action action, int n) => Main.Instance.ModHelper.Events.Unity.FireInNUpdates(action, n);
public static void FireOnNextUpdate(Action action) => Main.Instance.ModHelper.Events.Unity.FireOnNextUpdate(action);
}
}