Fix letting multiple mods editing one planet

This commit is contained in:
Nick 2022-05-10 18:44:36 -04:00
parent 9506c3dee5
commit 6f34dc71d5
3 changed files with 86 additions and 67 deletions

View File

@ -4,6 +4,7 @@ using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using UnityEngine; using UnityEngine;
using Logger = NewHorizons.Utility.Logger;
namespace NewHorizons.Components.Orbital namespace NewHorizons.Components.Orbital
{ {
@ -59,6 +60,8 @@ namespace NewHorizons.Components.Orbital
} }
public override void Update() public override void Update()
{
try
{ {
AstroObject primary = _astroObject?.GetPrimaryBody(); AstroObject primary = _astroObject?.GetPrimaryBody();
@ -91,6 +94,12 @@ namespace NewHorizons.Components.Orbital
_lineRenderer.widthMultiplier = widthMultiplier; _lineRenderer.widthMultiplier = widthMultiplier;
_lineRenderer.startColor = new Color(_color.r, _color.g, _color.b, num3 * num3); _lineRenderer.startColor = new Color(_color.r, _color.g, _color.b, num3 * num3);
} }
catch(Exception ex)
{
Logger.LogError($"Exception in OrbitLine for [{_astroObject?.name}] : {ex.Message}, {ex.StackTrace}");
enabled = false;
}
}
private float CalcProjectedAngleToCenter(Vector3 foci, Vector3 semiMajorAxis, Vector3 semiMinorAxis, Vector3 point) private float CalcProjectedAngleToCenter(Vector3 foci, Vector3 semiMajorAxis, Vector3 semiMinorAxis, Vector3 point)
{ {

View File

@ -4,6 +4,7 @@ using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using UnityEngine; using UnityEngine;
using Logger = NewHorizons.Utility.Logger;
namespace NewHorizons.Components.Orbital namespace NewHorizons.Components.Orbital
{ {
@ -57,6 +58,8 @@ namespace NewHorizons.Components.Orbital
} }
public override void Update() public override void Update()
{
try
{ {
var primary = _astroObject.GetPrimaryBody(); var primary = _astroObject.GetPrimaryBody();
Vector3 origin = primary == null ? Locator.GetRootTransform().position : primary.transform.position; Vector3 origin = primary == null ? Locator.GetRootTransform().position : primary.transform.position;
@ -85,6 +88,12 @@ namespace NewHorizons.Components.Orbital
_lineRenderer.widthMultiplier = widthMultiplier; _lineRenderer.widthMultiplier = widthMultiplier;
_lineRenderer.startColor = new Color(_color.r, _color.g, _color.b, num3 * num3); _lineRenderer.startColor = new Color(_color.r, _color.g, _color.b, num3 * num3);
} }
catch (Exception ex)
{
Logger.LogError($"Exception in OrbitLine for [{_astroObject?.name}] : {ex.Message}, {ex.StackTrace}");
enabled = false;
}
}
private float DistanceToTrackingOrbitLine(Vector3 point) private float DistanceToTrackingOrbitLine(Vector3 point)
{ {

View File

@ -83,14 +83,25 @@ namespace NewHorizons.Handlers
foreach (var node in planetGraph) foreach (var node in planetGraph)
{ {
LoadBody(node.body); LoadBody(node.body);
toLoad.Remove(node.body);
if (node is PlanetGraphHandler.FocalPointNode focal) if (node is PlanetGraphHandler.FocalPointNode focal)
{ {
LoadBody(focal.primary.body); LoadBody(focal.primary.body);
LoadBody(focal.secondary.body); LoadBody(focal.secondary.body);
toLoad.Remove(focal.primary.body);
toLoad.Remove(focal.secondary.body);
} }
} }
} }
// Are there more?
foreach(var body in toLoad)
{
LoadBody(body);
}
Logger.Log("Loading Deferred Bodies"); Logger.Log("Loading Deferred Bodies");
// Make a copy of the next pass of bodies so that the array can be edited while we load them // Make a copy of the next pass of bodies so that the array can be edited while we load them
@ -169,27 +180,17 @@ namespace NewHorizons.Handlers
var sector = go.GetComponentInChildren<Sector>(); var sector = go.GetComponentInChildren<Sector>();
var rb = go.GetAttachedOWRigidbody(); var rb = go.GetAttachedOWRigidbody();
// Did we already generate the rest of the body
var justUpdateOrbit = go.GetComponent<NHAstroObject>() != null && ExistingAOConfigs.ContainsKey(go.GetComponent<NHAstroObject>());
// Since orbits are always there just check if they set a semi major axis // Since orbits are always there just check if they set a semi major axis
if (body.Config.Orbit != null && body.Config.Orbit.SemiMajorAxis != 0f) if (body.Config.Orbit != null && body.Config.Orbit.SemiMajorAxis != 0f)
{ {
// If we aren't able to update the orbit wait until later UpdateBodyOrbit(body, go);
if (!UpdateBodyOrbit(body, go))
{
NextPassBodies.Add(body);
return null;
} }
}
if (justUpdateOrbit) return go;
if (body.Config.ChildrenToDestroy != null && body.Config.ChildrenToDestroy.Length > 0) if (body.Config.ChildrenToDestroy != null && body.Config.ChildrenToDestroy.Length > 0)
{ {
foreach (var child in body.Config.ChildrenToDestroy) foreach (var child in body.Config.ChildrenToDestroy)
{ {
Main.Instance.ModHelper.Events.Unity.FireInNUpdates(() => GameObject.Find(go.name + "/" + child).SetActive(false), 2); Main.Instance.ModHelper.Events.Unity.FireInNUpdates(() => GameObject.Find(go.name + "/" + child)?.SetActive(false), 2);
} }
} }
@ -401,7 +402,7 @@ namespace NewHorizons.Handlers
return go; return go;
} }
public static bool UpdateBodyOrbit(NewHorizonsBody body, GameObject go) public static void UpdateBodyOrbit(NewHorizonsBody body, GameObject go)
{ {
Logger.Log($"Updating orbit of [{body.Config.Name}]"); Logger.Log($"Updating orbit of [{body.Config.Name}]");
@ -421,7 +422,7 @@ namespace NewHorizons.Handlers
{ {
// If we can't find the new one we want to try again later (return false) // If we can't find the new one we want to try again later (return false)
primary = AstroObjectLocator.GetAstroObject(body.Config.Orbit.PrimaryBody); primary = AstroObjectLocator.GetAstroObject(body.Config.Orbit.PrimaryBody);
if (primary == null) return false; if (primary == null) return;
} }
// Just destroy the existing AO after copying everything over // Just destroy the existing AO after copying everything over
@ -505,10 +506,10 @@ namespace NewHorizons.Handlers
catch (Exception ex) catch (Exception ex)
{ {
Logger.LogError($"Couldn't update orbit of [{body.Config.Name}]: {ex.Message}, {ex.StackTrace}"); Logger.LogError($"Couldn't update orbit of [{body.Config.Name}]: {ex.Message}, {ex.StackTrace}");
// If it doesn't here there's no point trying again so we'll still return true // If it doesn't work here there's no point trying again so we'll still return true
} }
return true; return;
} }
private static void UpdatePosition(GameObject go, NewHorizonsBody body, AstroObject primaryBody, AstroObject secondaryBody) private static void UpdatePosition(GameObject go, NewHorizonsBody body, AstroObject primaryBody, AstroObject secondaryBody)