Avoid NRE in planet destruction (Fixes #116)

This commit is contained in:
Nick 2022-05-18 19:38:07 -04:00
parent 31592a042b
commit 10d9556b79
2 changed files with 82 additions and 62 deletions

View File

@ -69,81 +69,93 @@ namespace NewHorizons.Handlers
try try
{ {
if (ao.GetAstroObjectName() == AstroObject.Name.BrittleHollow) switch(ao._name)
{ {
RemoveBody(AstroObjectLocator.GetAstroObject(AstroObject.Name.WhiteHole.ToString()), delete, toDestroy); case AstroObject.Name.BrittleHollow:
} RemoveBody(AstroObjectLocator.GetAstroObject(AstroObject.Name.WhiteHole.ToString()), delete, toDestroy);
else if (ao.GetAstroObjectName() == AstroObject.Name.CaveTwin || ao.GetAstroObjectName() == AstroObject.Name.TowerTwin) break;
{ case AstroObject.Name.CaveTwin:
DisableBody(GameObject.Find("FocalBody"), delete); case AstroObject.Name.TowerTwin:
DisableBody(GameObject.Find("SandFunnel_Body"), delete); DisableBody(GameObject.Find("FocalBody"), delete);
} DisableBody(GameObject.Find("SandFunnel_Body"), delete);
else if (ao.GetAstroObjectName() == AstroObject.Name.MapSatellite) break;
{ case AstroObject.Name.MapSatellite:
DisableBody(GameObject.Find("MapSatellite_Body"), delete); DisableBody(GameObject.Find("MapSatellite_Body"), delete);
} break;
else if (ao.GetAstroObjectName() == AstroObject.Name.GiantsDeep) case AstroObject.Name.GiantsDeep:
{ foreach (var jelly in GameObject.FindObjectsOfType<JellyfishController>())
foreach (var jelly in GameObject.FindObjectsOfType<JellyfishController>()) {
{ DisableBody(jelly.gameObject, delete);
DisableBody(jelly.gameObject, delete); }
} // Else it will re-eanble the pieces
} // ao.GetComponent<OrbitalProbeLaunchController>()._realDebrisSectorProxies = null;
else if (ao.GetAstroObjectName() == AstroObject.Name.TimberHearth) break;
{ case AstroObject.Name.TimberHearth:
// Always just fucking kill this one to stop THE WARP BUG!!! // Always just fucking kill this one to stop THE WARP BUG!!!
DisableBody(GameObject.Find("StreamingGroup_TH"), true); DisableBody(GameObject.Find("StreamingGroup_TH"), true);
foreach (var obj in GameObject.FindObjectsOfType<DayNightTracker>()) foreach (var obj in GameObject.FindObjectsOfType<DayNightTracker>())
{ {
DisableBody(obj.gameObject, true); DisableBody(obj.gameObject, true);
} }
foreach (var obj in GameObject.FindObjectsOfType<VillageMusicVolume>()) foreach (var obj in GameObject.FindObjectsOfType<VillageMusicVolume>())
{ {
DisableBody(obj.gameObject, true); DisableBody(obj.gameObject, true);
} }
} break;
else if (ao.GetAstroObjectName() == AstroObject.Name.Sun) case AstroObject.Name.Sun:
{ var starController = ao.gameObject.GetComponent<StarController>();
var starController = ao.gameObject.GetComponent<StarController>(); StarLightController.RemoveStar(starController);
StarLightController.RemoveStar(starController); GameObject.Destroy(starController);
GameObject.Destroy(starController);
var audio = ao.GetComponentInChildren<SunSurfaceAudioController>(); var audio = ao.GetComponentInChildren<SunSurfaceAudioController>();
GameObject.Destroy(audio); GameObject.Destroy(audio);
foreach (var owAudioSource in ao.GetComponentsInChildren<OWAudioSource>()) foreach (var owAudioSource in ao.GetComponentsInChildren<OWAudioSource>())
{ {
owAudioSource.Stop(); owAudioSource.Stop();
GameObject.Destroy(owAudioSource); GameObject.Destroy(owAudioSource);
} }
foreach (var audioSource in ao.GetComponentsInChildren<AudioSource>()) foreach (var audioSource in ao.GetComponentsInChildren<AudioSource>())
{ {
audioSource.Stop(); audioSource.Stop();
GameObject.Destroy(audioSource); GameObject.Destroy(audioSource);
} }
foreach (var sunProxy in GameObject.FindObjectsOfType<SunProxy>()) foreach (var sunProxy in GameObject.FindObjectsOfType<SunProxy>())
{ {
Logger.Log($"Destroying SunProxy {sunProxy.gameObject.name}"); Logger.Log($"Destroying SunProxy {sunProxy.gameObject.name}");
GameObject.Destroy(sunProxy.gameObject); GameObject.Destroy(sunProxy.gameObject);
} }
// Stop the sun from breaking stuff when the supernova gets triggered // Stop the sun from breaking stuff when the supernova gets triggered
GlobalMessenger.RemoveListener("TriggerSupernova", ao.GetComponent<SunController>().OnTriggerSupernova); GlobalMessenger.RemoveListener("TriggerSupernova", ao.GetComponent<SunController>().OnTriggerSupernova);
break;
} }
// Just delete the children // Always delete the children
Logger.Log($"Removing Children of [{ao._name}], [{ao._customName}]");
foreach (var child in AstroObjectLocator.GetChildren(ao)) foreach (var child in AstroObjectLocator.GetChildren(ao))
{ {
if (child == null) continue;
Logger.Log($"Removing child [{child.name}] of [{ao._name}]");
// Ship starts as a child of TH but obvious we want to keep that
if (child.name == "Ship_Body") continue; if (child.name == "Ship_Body") continue;
DisableBody(child, true);
// Some children might be astro objects and as such can have children of their own
var childAO = child.GetComponent<AstroObject>();
if (childAO != null) RemoveBody(childAO, false, toDestroy);
else DisableBody(child, true);
} }
// Delete moons // Always delete moons
foreach (var obj in AstroObjectLocator.GetMoons(ao)) foreach (var obj in AstroObjectLocator.GetMoons(ao))
{ {
if (obj == null) continue;
RemoveBody(obj.GetComponent<AstroObject>(), false, toDestroy); RemoveBody(obj.GetComponent<AstroObject>(), false, toDestroy);
} }
} }
@ -188,7 +200,12 @@ namespace NewHorizons.Handlers
{ {
if (go == null) return; if (go == null) return;
if (delete) GameObject.Destroy(go); Logger.Log($"Removing [{go.name}]");
if (delete)
{
GameObject.Destroy(go);
}
else else
{ {
go.SetActive(false); go.SetActive(false);

View File

@ -94,7 +94,7 @@ namespace NewHorizons.Utility
if (primary == null) return new GameObject[0]; if (primary == null) return new GameObject[0];
var otherChildren = new List<GameObject>(); var otherChildren = new List<GameObject>();
switch (primary.GetAstroObjectName()) switch (primary._name)
{ {
case AstroObject.Name.TowerTwin: case AstroObject.Name.TowerTwin:
otherChildren.Add(GameObject.Find("TimeLoopRing_Body")); otherChildren.Add(GameObject.Find("TimeLoopRing_Body"));
@ -129,7 +129,10 @@ namespace NewHorizons.Utility
break; break;
// For some dumb reason the sun station doesn't use AstroObject.Name.SunStation // For some dumb reason the sun station doesn't use AstroObject.Name.SunStation
case AstroObject.Name.CustomString: case AstroObject.Name.CustomString:
if (primary._customName.Equals("Sun Station")) otherChildren.Add(GameObject.Find("SS_Debris_Body")); if (primary._customName.Equals("Sun Station"))
{
otherChildren.Add(GameObject.Find("SS_Debris_Body"));
}
break; break;
default: default:
break; break;