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,28 +69,28 @@ namespace NewHorizons.Handlers
try try
{ {
if (ao.GetAstroObjectName() == AstroObject.Name.BrittleHollow) switch(ao._name)
{ {
case AstroObject.Name.BrittleHollow:
RemoveBody(AstroObjectLocator.GetAstroObject(AstroObject.Name.WhiteHole.ToString()), delete, toDestroy); RemoveBody(AstroObjectLocator.GetAstroObject(AstroObject.Name.WhiteHole.ToString()), delete, toDestroy);
} break;
else if (ao.GetAstroObjectName() == AstroObject.Name.CaveTwin || ao.GetAstroObjectName() == AstroObject.Name.TowerTwin) case AstroObject.Name.CaveTwin:
{ case AstroObject.Name.TowerTwin:
DisableBody(GameObject.Find("FocalBody"), delete); DisableBody(GameObject.Find("FocalBody"), delete);
DisableBody(GameObject.Find("SandFunnel_Body"), delete); DisableBody(GameObject.Find("SandFunnel_Body"), delete);
} break;
else if (ao.GetAstroObjectName() == AstroObject.Name.MapSatellite) 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
else if (ao.GetAstroObjectName() == AstroObject.Name.TimberHearth) // ao.GetComponent<OrbitalProbeLaunchController>()._realDebrisSectorProxies = null;
{ 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);
@ -102,9 +102,8 @@ namespace NewHorizons.Handlers
{ {
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);
@ -132,18 +131,31 @@ namespace NewHorizons.Handlers
// 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;