Skip to content

Commit

Permalink
Added integrity check for system. If any object, like planets were de…
Browse files Browse the repository at this point in the history
…leted manually, it will be removed from the system
  • Loading branch information
8vogt committed Feb 21, 2021
1 parent 91d9031 commit 8711b33
Showing 1 changed file with 37 additions and 1 deletion.
38 changes: 37 additions & 1 deletion SEWorldGenPlugin/Generator/MyStarSystemGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ public override void Init(MyObjectBuilder_SessionComponent sessionComponent)
StarSystem = GenerateNewStarSystem();
}

CheckIntegrityOfSystem();
AddPlanetDeleteAction();
}

Expand Down Expand Up @@ -655,6 +656,41 @@ private void AddPlanetDeleteAction()
};
}

/// <summary>
/// Checks all planets of the star system, and whether they are still existent as objects
/// in the world. Just to clear up deleted objects from the system at world loading
/// </summary>
private void CheckIntegrityOfSystem()
{
MyPluginLog.Debug("Checking system integrity");
foreach(var obj in StarSystem.GetAllObjects())
{
if(obj is MySystemPlanet)
{
if(!MyEntities.EntityExists((obj as MySystemPlanet).EntityId) && (obj as MySystemPlanet).Generated)
{
MyPluginLog.Debug("Planet " + obj.Id + " does not exist anymore, deleting it", LogLevel.WARNING);
StarSystem.RemoveObject(obj.Id);
}
}
else if(obj is MySystemAsteroids)
{
var instance = obj as MySystemAsteroids;

if (MyAsteroidObjectsManager.Static.AsteroidObjectProviders.ContainsKey(instance.AsteroidTypeName))
{
var data = MyAsteroidObjectsManager.Static.AsteroidObjectProviders[instance.AsteroidTypeName].GetInstanceData(instance);
if(data == null)
{
MyPluginLog.Debug("Asteroid instance " + obj.Id + " has no data attached, deleting it", LogLevel.WARNING);
MyAsteroidObjectsManager.Static.AsteroidObjectProviders[instance.AsteroidTypeName].RemoveInstance(instance);
}
}
}
}
MyPluginLog.Debug("Integrity check complete");
}

/// <summary>
/// Action for planet close
/// </summary>
Expand All @@ -679,7 +715,7 @@ public static void OnPlanetClose(MyEntity entity)
{
if (Static.StarSystem.RemoveObject(planet.Id))
{
MyPluginLog.Log("Star system instance of planet " + e.StorageName + "was removed");
MyPluginLog.Log("Star system instance of planet " + e.StorageName + " was removed");
}
break;
}
Expand Down

0 comments on commit 8711b33

Please sign in to comment.