Skip to content

Commit

Permalink
Plugin now deletes the persistent gps of deleted objects
Browse files Browse the repository at this point in the history
  • Loading branch information
8vogt committed Feb 25, 2021
1 parent 2403263 commit 1d17564
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 55 deletions.
59 changes: 4 additions & 55 deletions SEWorldGenPlugin/Generator/MyStarSystemGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,6 @@ public override void Init(MyObjectBuilder_SessionComponent sessionComponent)
}

CheckIntegrityOfSystem();
AddPlanetDeleteAction();
}

/// <summary>
Expand Down Expand Up @@ -656,28 +655,6 @@ private void LoadPlanetDefinitions()
}
}

/// <summary>
/// Adds the action called, when planets are deleted
/// </summary>
private void AddPlanetDeleteAction()
{
List<MyEntityList.MyEntityListInfoItem> planets = MyEntityList.GetEntityList(MyEntityList.MyEntityTypeEnum.Planets);
foreach(var p in planets)
{
var e = MyEntities.GetEntityById(p.EntityId) as MyPlanet;
e.OnClose += OnPlanetClose;
}

MyEntities.OnEntityAdd += delegate (MyEntity entity)
{
if(entity is MyPlanet)
{
MyPluginLog.Debug("Planet created, adding close action");
entity.OnClose += OnPlanetClose;
}
};
}

/// <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
Expand All @@ -693,6 +670,7 @@ private void CheckIntegrityOfSystem()
{
MyPluginLog.Debug("Planet " + obj.Id + " does not exist anymore, deleting it", LogLevel.WARNING);
StarSystem.RemoveObject(obj.Id);
MyGPSManager.Static.RemovePersistentGps(obj.Id);
}
}
else if(obj is MySystemAsteroids)
Expand All @@ -706,45 +684,14 @@ private void CheckIntegrityOfSystem()
{
MyPluginLog.Debug("Asteroid instance " + obj.Id + " has no data attached, deleting it", LogLevel.WARNING);
MyAsteroidObjectsManager.Static.AsteroidObjectProviders[instance.AsteroidTypeName].RemoveInstance(instance);
MyGPSManager.Static.RemovePersistentGps(obj.Id);
}
}
}
}
MyPluginLog.Debug("Integrity check complete");
}

/// <summary>
/// Action for planet close
/// </summary>
/// <param name="entity">Entity that gets closed</param>
public static void OnPlanetClose(MyEntity entity)
{
MyPlanet e = null;
if (entity is MyPlanet)
{
e = entity as MyPlanet;
}
else return;

MyPluginLog.Log("Planet " + e.StorageName + " is getting removed. Removing its star system instance");
foreach (var obj in Static.StarSystem.GetAllObjects())
{
if (obj is MySystemPlanet)
{
var planet = obj as MySystemPlanet;

if (planet.EntityId == e.EntityId)
{
if (Static.StarSystem.RemoveObject(planet.Id))
{
MyPluginLog.Log("Star system instance of planet " + e.StorageName + " was removed");
}
break;
}
}
}
}

/// <summary>
/// Loads the system data xml file from the world folder, or,
/// if none exist, returns a new one.
Expand Down Expand Up @@ -772,6 +719,8 @@ public override void UpdateBeforeSimulation()

if (!MySettingsSession.Static.IsEnabled()) return;

CheckIntegrityOfSystem();

List<MyEntityList.MyEntityListInfoItem> planets = MyEntityList.GetEntityList(MyEntityList.MyEntityTypeEnum.Planets);
foreach (var p in planets)
{
Expand Down
31 changes: 31 additions & 0 deletions SEWorldGenPlugin/Session/MyGPSManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,34 @@ public bool PersistenGpsExists(Guid id)
return m_globalGpss.ContainsKey(id);
}

/// <summary>
/// Removes the given persistent gps if it exists
/// </summary>
/// <param name="id">Id of persistent gps</param>
public void RemovePersistentGps(Guid id)
{
if (PersistenGpsExists(id))
{

MyGps gps = new MyGps
{
Name = m_globalGpss[id].Name,
Coords = m_globalGpss[id].Position,
GPSColor = m_globalGpss[id].Color,
ShowOnHud = true,
AlwaysVisible = false,
DiscardAt = null
};

foreach (var playerId in m_globalGpss[id].Players)
{
MySession.Static.Gpss.SendDelete(playerId, gps.CalculateHash());
}

m_globalGpss.Remove(id);
}
}

/// <summary>
/// Adds a new Dynamic gps to the player
/// </summary>
Expand Down Expand Up @@ -236,6 +264,9 @@ public override void UpdateBeforeSimulation()
AlwaysVisible = false,
DiscardAt = null
};

gps.CalculateHash();

MySession.Static.Gpss.SendAddGps(p.Identity.IdentityId, ref gps, playSoundOnCreation: false);
m_globalGpss[entry].Players.Add(p.Identity.IdentityId);
}
Expand Down

0 comments on commit 1d17564

Please sign in to comment.