diff --git a/WarpDrivePlugin/Core.cs b/WarpDrivePlugin/Core.cs index b1a4258..069f5b6 100644 --- a/WarpDrivePlugin/Core.cs +++ b/WarpDrivePlugin/Core.cs @@ -35,6 +35,7 @@ public class Core : PluginBase, ICubeBlockEventHandler private static float m_warpDelay; protected Dictionary m_warpEngineMap; + protected DateTime m_lastFullScan; #endregion @@ -185,6 +186,23 @@ public float WarpDelay set { m_warpDelay = value; } } + [Category("Warp Drive Plugin")] + [Browsable(true)] + [ReadOnly(true)] + public List WarpEngines + { + get + { + List cubeGridNameList = new List(); + foreach (var key in m_warpEngineMap.Keys) + { + cubeGridNameList.Add(key.Name); + } + + return cubeGridNameList; + } + } + #endregion #region "Methods" @@ -206,25 +224,44 @@ public override void Update() warpEngine.StartWarp(); } } + + TimeSpan timeSinceLastFullScan = DateTime.Now - m_lastFullScan; + if (timeSinceLastFullScan.TotalMilliseconds > 30000) + { + LogManager.APILog.WriteLine("Scanning all entities for valid warp drives ..."); + + //Run cleanup + CleanUpEngineMap(); + + //Run full scan + FullScan(); + } } public void OnCubeBlockCreated(CubeBlockEntity cubeBlock) { - if (cubeBlock.Parent.GridSizeEnum != MyCubeSize.Large) + CubeGridEntity cubeGrid = cubeBlock.Parent; + + if (cubeGrid.GridSizeEnum != MyCubeSize.Large) return; - WarpEngine dummyEngine = new WarpEngine(null); - if (dummyEngine.IsDefinitionMatch(cubeBlock)) + if (m_warpEngineMap.ContainsKey(cubeGrid)) { - if (m_warpEngineMap.ContainsKey(cubeBlock.Parent)) + if (CheckEngineForRemoval(m_warpEngineMap[cubeGrid])) + { + LogManager.APILog.WriteLineAndConsole("Removing warp engine on cube grid '" + cubeGrid.Name + "' (on create)"); + m_warpEngineMap.Remove(cubeGrid); + } + else + { return; + } + } - if(SandboxGameAssemblyWrapper.IsDebugging) - LogManager.APILog.WriteLineAndConsole("Created warp engine on cube grid '" + cubeBlock.Parent.Name + "'"); - - WarpEngine warpEngine = new WarpEngine(cubeBlock.Parent); - warpEngine.LoadBlocksFromAnchor(cubeBlock.Min); - m_warpEngineMap.Add(cubeBlock.Parent, warpEngine); + WarpEngine dummyEngine = new WarpEngine(null); + if (dummyEngine.IsDefinitionMatch(cubeBlock)) + { + CreateWarpEngine(cubeBlock); } } @@ -233,32 +270,128 @@ public void OnCubeBlockDeleted(CubeBlockEntity cubeBlock) if (cubeBlock.Parent.GridSizeEnum != MyCubeSize.Large) return; - CleanUpEngineMap(cubeBlock); + CubeGridEntity cubeGrid = cubeBlock.Parent; + + if (cubeGrid == null || !m_warpEngineMap.ContainsKey(cubeGrid)) + return; + + WarpEngine warpEngine = m_warpEngineMap[cubeGrid]; + bool shouldRemove = false; + foreach (CubeBlockEntity block in warpEngine.Blocks) + { + if (block == null || block == cubeBlock || block.IsDisposed) + { + shouldRemove = true; + break; + } + } + if (cubeGrid.IsDisposed) + shouldRemove = true; + + if (shouldRemove) + { + RemoveWarpEngine(cubeGrid); + } } #endregion - protected void CleanUpEngineMap(CubeBlockEntity deletedCubeBlock) + protected void RemoveWarpEngine(CubeGridEntity cubeGrid) { - CubeGridEntity parent = deletedCubeBlock.Parent; - - if (!m_warpEngineMap.ContainsKey(parent)) + if (!m_warpEngineMap.ContainsKey(cubeGrid)) return; - WarpEngine warpEngine = m_warpEngineMap[parent]; - bool shouldRemove = false; + WarpEngine engine = m_warpEngineMap[cubeGrid]; + LogManager.APILog.WriteLineAndConsole("Removing warp engine on cube grid '" + engine.Parent.Name + "'"); + m_warpEngineMap.Remove(engine.Parent); - foreach (CubeBlockEntity cubeBlock in warpEngine.Blocks) + engine.Dispose(); + } + + protected void CreateWarpEngine(CubeBlockEntity cubeBlock) + { + CubeGridEntity cubeGrid = cubeBlock.Parent; + + LogManager.APILog.WriteLineAndConsole("Created warp engine on cube grid '" + cubeGrid.Name + "'"); + + WarpEngine warpEngine = new WarpEngine(cubeGrid); + warpEngine.LoadBlocksFromAnchor(cubeBlock.Min); + + if(!m_warpEngineMap.ContainsKey(cubeGrid)) + m_warpEngineMap.Add(cubeGrid, warpEngine); + } + + protected bool CheckEngineForRemoval(WarpEngine engine) + { + bool shouldBeRemoved = false; + List blocks = engine.Blocks; + foreach (var block in blocks) { - if (cubeBlock == deletedCubeBlock || cubeBlock.IsDisposed) + if (block == null || block.IsDisposed || block.Parent == null || block.Parent.IsDisposed) { - shouldRemove = true; + shouldBeRemoved = true; break; } } - if(shouldRemove || parent.IsDisposed) - m_warpEngineMap.Remove(parent); + return shouldBeRemoved; + } + + protected void CleanUpEngineMap() + { + foreach (WarpEngine engine in m_warpEngineMap.Values) + { + if (CheckEngineForRemoval(engine)) + { + RemoveWarpEngine(engine.Parent); + } + } + } + + protected void FullScan() + { + m_lastFullScan = DateTime.Now; + foreach (BaseEntity entity in SectorObjectManager.Instance.GetTypedInternalData()) + { + try + { + //Skip if not cube grid + if (!(entity is CubeGridEntity)) + continue; + + CubeGridEntity cubeGrid = (CubeGridEntity)entity; + + //Skip if not large cube grid + if (cubeGrid.GridSizeEnum != MyCubeSize.Large) + continue; + + //Force a cube block refresh + List cubeBlocks = cubeGrid.CubeBlocks; + + //Skip if cube grid already has engine + if (m_warpEngineMap.ContainsKey(cubeGrid)) + continue; + + //Scan cube grid for engines + WarpEngine dummyEngine = new WarpEngine(null); + List matches = dummyEngine.GetDefinitionMatches(cubeGrid); + if (matches.Count > 0) + { + //If there was a match, create a new engine + CubeBlockEntity cubeBlock = cubeGrid.GetCubeBlock(matches[0]); + CreateWarpEngine(cubeBlock); + } + else + { + //If there was no match, remove the existing engine + RemoveWarpEngine(cubeGrid); + } + } + catch (Exception ex) + { + LogManager.GameLog.WriteLine(ex); + } + } } #endregion diff --git a/WarpDrivePlugin/Properties/AssemblyInfo.cs b/WarpDrivePlugin/Properties/AssemblyInfo.cs index 51e0e30..aad9db9 100644 --- a/WarpDrivePlugin/Properties/AssemblyInfo.cs +++ b/WarpDrivePlugin/Properties/AssemblyInfo.cs @@ -29,5 +29,5 @@ // Build Number // Revision // -[assembly: AssemblyVersion("0.1.3.3")] -[assembly: AssemblyFileVersion("0.1.3.3")] +[assembly: AssemblyVersion("0.1.3.11")] +[assembly: AssemblyFileVersion("0.1.3.11")] diff --git a/WarpDrivePlugin/WarpEngine.cs b/WarpDrivePlugin/WarpEngine.cs index c800549..aa60078 100644 --- a/WarpDrivePlugin/WarpEngine.cs +++ b/WarpDrivePlugin/WarpEngine.cs @@ -17,7 +17,7 @@ namespace WarpDrivePlugin { - public class WarpEngine : MultiblockStructure + public class WarpEngine : MultiblockStructure, IDisposable { #region "Attributes" @@ -26,6 +26,7 @@ public class WarpEngine : MultiblockStructure private bool m_isAtWarpSpeed; private bool m_isSpeedingUp; private bool m_isSlowingDown; + private bool m_isDisposed; private float m_warpFuelRequired; private float m_accelerationFactor; @@ -56,6 +57,7 @@ public WarpEngine(CubeGridEntity parent) m_isAtWarpSpeed = false; m_isSpeedingUp = false; m_isSlowingDown = false; + m_isDisposed = false; m_accelerationFactor = 2; @@ -118,6 +120,11 @@ public bool CanWarp } } + public bool IsDisposed + { + get { return m_isDisposed; } + } + protected BeaconEntity Beacon { get @@ -170,9 +177,16 @@ protected List FuelItems #region "Methods" + public void Dispose() + { + m_isDisposed = true; + } + public override Dictionary GetMultiblockDefinition() { Dictionary def = new Dictionary(); + if (IsDisposed) + return def; def.Add(new Vector3I(0, 0, 0), typeof(ReactorEntity)); def.Add(new Vector3I(1, 0, 1), typeof(ReactorEntity)); @@ -197,10 +211,20 @@ public override Dictionary GetMultiblockDefinition() public void Update() { + if (IsDisposed) + return; + try { - if (!IsFunctional) + try + { + if (!this.IsFunctional) + return; + } + catch + { return; + } m_timeSinceLastUpdate = DateTime.Now - m_lastUpdate; m_lastUpdate = DateTime.Now; @@ -265,6 +289,9 @@ public void Update() public void StartWarp() { + if (IsDisposed) + return; + if (m_isStartingWarp) return;