diff --git a/TSTSSESCores/Data/Scripts/Scripts/Specials/ShipClass/Hooks.cs b/TSTSSESCores/Data/Scripts/Scripts/Specials/ShipClass/Hooks.cs index 70869517..7a85c83c 100644 --- a/TSTSSESCores/Data/Scripts/Scripts/Specials/ShipClass/Hooks.cs +++ b/TSTSSESCores/Data/Scripts/Scripts/Specials/ShipClass/Hooks.cs @@ -12,30 +12,31 @@ using C = System.Func; using U = System.Collections.Generic.List; using L = System.Collections.Generic.IDictionary; -using VRage.Utils; namespace MIG.SpecCores { public class Hooks { + private static Action registerCustomLimitConsumer = RegisterCustomLimitConsumer; private static Func getMainSpecCore = GetMainSpecCore; private static Func getMainSpecCoreBlock = GetMainSpecCoreBlock; private static Action getSpecCoreLimits = GetSpecCoreLimits; private static Action getSpecCoreUpgrades = GetSpecCoreUpgrades; - private static Action setSpecCoreUpgrades = SetSpecCoreCustomValues; - + private static Action setSpecCoreUpgrades = SetSpecCoreCustomValues; + private static Func getSpecCoreBlock = GetSpecCoreBlock; - + private static Func>> getGridBlocksByType = GetGridBlocksByType; private static Func>> getGridBlocksById = GetGridBlocksById; - + private static Func getBlockSpecCore = GetBlockSpecCore; private static Func getLimitedBlock = GetLimitedBlock; private static Func getLimitedBlockBlock = GetLimitedBlockBlock; - - private static Action, float>> registerSpecCoreCurrentPointCustomFx = RegisterSpecCoreCurrentPointCustomFx; - + + private static Action , float>> registerSpecCoreCurrentPointCustomFx = RegisterSpecCoreCurrentPointCustomFx; + + private static Action, Dictionary>> addSpecCoreLimitsInterceptor = AddSpecCoreLimitsInterceptor; private static event Action, Dictionary> SpecCoreLimitsInterceptor = null; @@ -43,13 +44,15 @@ public static void InvokeLimitsInterceptor(ISpecBlock block, Dictionary, float>> SpecCoreCurrentCustom = new Dictionary, float>>(); - + + private static Func, string> CanSpecCoreWork; - + public static Dictionary HookedConsumerInfos = new Dictionary(); - + + public static event Action OnSpecBlockCreated; public static event Action OnSpecBlockDestroyed; public static event Action OnLimitedBlockCreated; @@ -81,143 +84,53 @@ public static void TriggerOnLimitedBlockDestroyed(ILimitedBlock block) { OnLimitedBlockDestroyed?.Invoke(block); } - + public static object GetSpecCoreBlock(IMyTerminalBlock block) { - if (block == null) - { - MyLog.Default.WriteLine("Hooks.GetSpecCoreBlock: block is null"); - return null; - } - var grid = block.CubeGrid; - if (grid == null) - { - MyLog.Default.WriteLine("Hooks.GetSpecCoreBlock: block.CubeGrid is null"); - return null; - } - var ship = grid.GetShip(); - if (ship == null) - { - MyLog.Default.WriteLine("Hooks.GetSpecCoreBlock: grid.GetShip() is null"); - return null; - } - + if (ship == null) return null; ISpecBlock specBlock; - if (ship.SpecBlocks.TryGetValue(block, out specBlock)) - { - return specBlock; - } - else - { - MyLog.Default.WriteLine("Hooks.GetSpecCoreBlock: ship.SpecBlocks does not contain the block"); - return null; - } + ship.SpecBlocks.TryGetValue(block, out specBlock); + return specBlock; } - + public static Dictionary> GetGridBlocksByType(IMyCubeGrid grid) { - if (grid == null) - { - MyLog.Default.WriteLine("Hooks.GetGridBlocksByType: grid is null"); - return null; - } - var ship = grid.GetShip(); return ship?.BlocksCache; } - + public static Dictionary> GetGridBlocksById(IMyCubeGrid grid) { - if (grid == null) - { - MyLog.Default.WriteLine("Hooks.GetGridBlocksById: grid is null"); - return null; - } - var ship = grid.GetShip(); return ship?.BlocksCacheByType; } public static IMyTerminalBlock GetBlockSpecCore(object block) { - if (block == null) - { - MyLog.Default.WriteLine("Hooks.GetBlockSpecCore: block is null"); - return null; - } - - var specBlock = block as ISpecBlock; - if (specBlock == null) - { - MyLog.Default.WriteLine($"Hooks.GetBlockSpecCore: block of type {block.GetType().FullName} does not implement ISpecBlock"); - return null; - } - - return specBlock.block; + return ((ISpecBlock) block).block; } - + public static object GetLimitedBlock(IMyTerminalBlock block) { - if (block == null) - { - MyLog.Default.WriteLine("Hooks.GetLimitedBlock: block is null"); - return null; - } - var grid = block.CubeGrid; - if (grid == null) - { - MyLog.Default.WriteLine("Hooks.GetLimitedBlock: block.CubeGrid is null"); - return null; - } - var ship = grid.GetShip(); - if (ship == null) - { - MyLog.Default.WriteLine("Hooks.GetLimitedBlock: grid.GetShip() is null"); - return null; - } - + if (ship == null) return null; ILimitedBlock limitedBlock; - if (ship.LimitedBlocks.TryGetValue(block, out limitedBlock)) - { - return limitedBlock; - } - else - { - MyLog.Default.WriteLine("Hooks.GetLimitedBlock: ship.LimitedBlocks does not contain the block"); - return null; - } + ship.LimitedBlocks.TryGetValue(block, out limitedBlock); + return limitedBlock; } public static IMyTerminalBlock GetLimitedBlockBlock(object block) { - if (block == null) - { - MyLog.Default.WriteLine("Hooks.GetLimitedBlockBlock: block is null"); - return null; - } + return ((ILimitedBlock) block).GetBlock(); + } - var limitedBlock = block as ILimitedBlock; - if (limitedBlock == null) - { - MyLog.Default.WriteLine($"Hooks.GetLimitedBlockBlock: block of type {block.GetType().FullName} does not implement ILimitedBlock"); - return null; - } - return limitedBlock.GetBlock(); - } public static object GetMainSpecCore(IMyCubeGrid grid) { - if (grid == null) - { - MyLog.Default.WriteLine("Hooks.GetMainSpecCore: grid is null"); - return null; - } - Ship ship; if (OriginalSpecCoreSession.Instance.gridToShip.TryGetValue(grid.EntityId, out ship)) { @@ -226,15 +139,9 @@ public static object GetMainSpecCore(IMyCubeGrid grid) return null; } - + public static IMyTerminalBlock GetMainSpecCoreBlock(IMyCubeGrid grid) { - if (grid == null) - { - MyLog.Default.WriteLine("Hooks.GetMainSpecCoreBlock: grid is null"); - return null; - } - Ship ship; if (OriginalSpecCoreSession.Instance.gridToShip.TryGetValue(grid.EntityId, out ship)) { @@ -243,15 +150,11 @@ public static IMyTerminalBlock GetMainSpecCoreBlock(IMyCubeGrid grid) return null; } - + public static void GetSpecCoreLimits(object specCore, IDictionary dictionary, int mode) { var specBlock = specCore as SpecBlock; - if (specBlock == null) - { - MyLog.Default.WriteLine("Hooks.GetSpecCoreLimits: specCore is not a SpecBlock"); - return; - } + if (specBlock == null) return; switch (mode) { @@ -262,51 +165,35 @@ public static void GetSpecCoreLimits(object specCore, IDictionary di case 5: dictionary.Sum(specBlock.Settings.CustomStatic); break; case 6: dictionary.Sum(specBlock.Settings.CustomDynamic); break; case 7: dictionary.Sum(specBlock.GetLimits()); break; - default: - MyLog.Default.WriteLine($"Hooks.GetSpecCoreLimits: Invalid mode {mode}"); - break; } } - + public static void GetSpecCoreUpgrades(object specCore, List copyTo, int mode) { var specBlock = specCore as SpecBlock; - if (specBlock == null) - { - MyLog.Default.WriteLine("Hooks.GetSpecCoreUpgrades: specCore is not a SpecBlock"); - return; - } - + if (specBlock == null) return; copyTo.AddRange(specBlock.Settings.Upgrades); } - + public static void GetSpecCoreUpgrades(object specCore, List copyTo) { var specBlock = specCore as SpecBlock; - if (specBlock == null) - { - MyLog.Default.WriteLine("Hooks.GetSpecCoreUpgrades: specCore is not a SpecBlock"); - return; - } - + if (specBlock == null) return; copyTo.AddRange(specBlock.Settings.Upgrades); } public static void SetSpecCoreCustomValues(object specCore, IDictionary staticValues, IDictionary dynamicValues) { var specBlock = specCore as SpecBlock; - if (specBlock == null) - { - MyLog.Default.WriteLine("Hooks.SetSpecCoreCustomValues: specCore is not a SpecBlock"); - return; - } - + if (specBlock == null) return; specBlock.Settings.CustomStatic.Sum(staticValues); specBlock.Settings.CustomDynamic.Sum(dynamicValues); specBlock.ApplyUpgrades(); specBlock.SaveSettings(); } + + /// /// Must be inited in LoadData of MySessionComponentBase /// @@ -315,7 +202,7 @@ public static void Init() ModConnection.Init(); TorchExtensions.Init(); - + ModConnection.SetValue("MIG.SpecCores.RegisterCustomLimitConsumer", registerCustomLimitConsumer); ModConnection.SetValue("MIG.SpecCores.GetMainSpecCore", getMainSpecCore); ModConnection.SetValue("MIG.SpecCores.GetMainSpecCoreBlock", getMainSpecCoreBlock); @@ -330,12 +217,12 @@ public static void Init() ModConnection.SetValue("MIG.SpecCores.GetBlockSpecCore", getBlockSpecCore); ModConnection.SetValue("MIG.SpecCores.GetLimitedBlock", getLimitedBlock); ModConnection.SetValue("MIG.SpecCores.GetLimitedBlockBlock", getLimitedBlockBlock); - + ModConnection.Subscribe("MIG.SpecCores.RegisterCustomLimitConsumer", registerCustomLimitConsumer, (x) => { registerCustomLimitConsumer = x; }); - + ModConnection.SetValue("MIG.SpecCores.RegisterSpecCorePointCustomFx", registerSpecCoreCurrentPointCustomFx); ModConnection.SetValue("MIG.SpecCores.AddSpecCoreLimitsInterceptor", addSpecCoreLimitsInterceptor); - + ModConnection.Subscribe("MIG.SpecCores.OnSpecBlockCreated", OnSpecBlockCreated, (a) => { OnSpecBlockCreated += a; }); ModConnection.Subscribe("MIG.SpecCores.OnSpecBlockDestroyed", OnSpecBlockDestroyed, (a) => { OnSpecBlockDestroyed += a; }); ModConnection.Subscribe("MIG.SpecCores.OnLimitedBlockCreated", OnLimitedBlockCreated, (a) => { OnLimitedBlockCreated += a; }); @@ -344,7 +231,7 @@ public static void Init() ModConnection.Subscribe("MIG.SpecCores.OnSpecBlockChanged", OnSpecCoreChanged, (a) => { OnSpecCoreChanged += a; }); ModConnection.Subscribe("MIG.SpecCores.CanSpecCoreWork", CanSpecCoreWork, (a) => { CanSpecCoreWork = a; }); } - + public static void Close() { ModConnection.Close(); @@ -360,31 +247,19 @@ public static void RegisterCustomLimitConsumer(string Id, C OnNewConsumerRegiste IsDrainingPoints = IsDrainingPoints, Disable = Disable, }; - } + public static void RegisterSpecCoreCurrentPointCustomFx(int id, Func, float> fx) { - if (fx == null) - { - MyLog.Default.WriteLine($"Hooks.RegisterSpecCoreCurrentPointCustomFx: fx is null for id {id}"); - return; - } - SpecCoreCurrentCustom[id] = fx; } - + public static void AddSpecCoreLimitsInterceptor(Action, Dictionary> fx) { - if (fx == null) - { - MyLog.Default.WriteLine("Hooks.AddSpecCoreLimitsInterceptor: fx is null"); - return; - } - SpecCoreLimitsInterceptor += fx; } - + public static string CanBeApplied(ISpecBlock specBlock, List grids) { return CanSpecCoreWork?.Invoke(specBlock, grids) ?? null; @@ -399,7 +274,7 @@ public static float GetCurrentPointValueForSpecCore(ISpecBlock specBlock, List grids, LimitPoint lp) { var fx = SpecCoreCurrentCustom.GetOr(lp.Id, null); diff --git a/TSTSSESCores/Data/Scripts/Scripts/Specials/ShipClass/LimitedBlock/LimitedBlockNetworking.cs b/TSTSSESCores/Data/Scripts/Scripts/Specials/ShipClass/LimitedBlock/LimitedBlockNetworking.cs index d1cc2eab..02fbd2f5 100644 --- a/TSTSSESCores/Data/Scripts/Scripts/Specials/ShipClass/LimitedBlock/LimitedBlockNetworking.cs +++ b/TSTSSESCores/Data/Scripts/Scripts/Specials/ShipClass/LimitedBlock/LimitedBlockNetworking.cs @@ -66,26 +66,20 @@ public static void Handler (ILimitedBlock block, LimitedBlockSettings settings, public void OnSettingsChanged() { } - public void NotifyAndSave(byte type = 255, bool forceSave = false) + public void NotifyAndSave(byte type=255, bool forceSave = false) { try { if (MyAPIGateway.Session.IsServer) { - lock (Settings) - { - Sync.SendMessageToOthers(Block.EntityId, Settings, type: type); - SaveSettings(forceSave); - } + Sync.SendMessageToOthers(Block.EntityId, Settings, type: type); + SaveSettings(forceSave); } else { if (Sync != null) { - lock (Settings) - { - Sync.SendMessageToServer(Block.EntityId, Settings, type: type); - } + Sync.SendMessageToServer(Block.EntityId, Settings, type: type); } } } @@ -94,8 +88,7 @@ public void NotifyAndSave(byte type = 255, bool forceSave = false) Log.ChatError($"NotifyAndSave {type} Exception {ex} {ex.StackTrace}"); } } - - + public void SaveSettings(bool forceSave = false) { if (MyAPIGateway.Session.IsServer) diff --git a/TSTSSESCores/Data/Scripts/Scripts/Specials/ShipClass/SpecBlock/SpecBlock.cs b/TSTSSESCores/Data/Scripts/Scripts/Specials/ShipClass/SpecBlock/SpecBlock.cs index f7ef1582..4aa7aa75 100644 --- a/TSTSSESCores/Data/Scripts/Scripts/Specials/ShipClass/SpecBlock/SpecBlock.cs +++ b/TSTSSESCores/Data/Scripts/Scripts/Specials/ShipClass/SpecBlock/SpecBlock.cs @@ -162,7 +162,6 @@ public void ApplyUpgrades() private static void Handler(SpecBlock block, UpgradableSpecBlockSettings settings, byte type, ulong userSteamId, bool isFromServer) { - Log.ChatError($"Handler called for block: {block.block.EntityId}, type: {type}, isFromServer: {isFromServer}"); if (isFromServer && !MyAPIGateway.Session.IsServer) { block.Settings = settings; diff --git a/TSTSSESCores/Data/Scripts/Scripts/Specials/ShipClass/SpecBlock/SpecBlockGui.cs b/TSTSSESCores/Data/Scripts/Scripts/Specials/ShipClass/SpecBlock/SpecBlockGui.cs index b97e6a65..6014ecdc 100644 --- a/TSTSSESCores/Data/Scripts/Scripts/Specials/ShipClass/SpecBlock/SpecBlockGui.cs +++ b/TSTSSESCores/Data/Scripts/Scripts/Specials/ShipClass/SpecBlock/SpecBlockGui.cs @@ -122,14 +122,13 @@ private static void RemoveUpgradeGui(SpecBlock x) x.block.RefreshDI(); UpdateControls(x); } - + private static void ApplyUpgradesGui(SpecBlock x) { var settings = new UpgradableSpecBlockSettings(); settings.Upgrades.AddRange(x.AddedUpgrades); x.AddedUpgrades.Clear(); x.SelectedUpgrades.Clear(); - Log.ChatError($"Applying upgrades: {string.Join(",", settings.Upgrades)} to block: {x.block.EntityId}"); Sync.SendMessageToServer(x.block.EntityId, settings, type: APPLY_SELECTED); FrameExecutor.addDelayedLogic(30, (xx) => UpdateControls(x)); //1/2 sec if (!MyAPIGateway.Session.IsServer) @@ -137,7 +136,7 @@ private static void ApplyUpgradesGui(SpecBlock x) x.Settings = settings; //Remove gui lag } } - + private static void ApplyRandomUpgradesGui(SpecBlock x) { x.AddedUpgrades.Clear();