From 102e55091e1fa7e258b129f3d7a33133619f8a06 Mon Sep 17 00:00:00 2001 From: InvalidArgument3 Date: Tue, 4 Jun 2024 16:05:05 -0500 Subject: [PATCH] forking epic --- .../ScriptsAddon/Specials/SpecBlockHooks.cs | 132 +++++++++++++----- 1 file changed, 94 insertions(+), 38 deletions(-) diff --git a/TSTSSESCoresAddon/Data/Scripts/ScriptsAddon/Specials/SpecBlockHooks.cs b/TSTSSESCoresAddon/Data/Scripts/ScriptsAddon/Specials/SpecBlockHooks.cs index 30830e94..87cff80d 100644 --- a/TSTSSESCoresAddon/Data/Scripts/ScriptsAddon/Specials/SpecBlockHooks.cs +++ b/TSTSSESCoresAddon/Data/Scripts/ScriptsAddon/Specials/SpecBlockHooks.cs @@ -207,67 +207,123 @@ public static object GetLimitedBlock(IMyTerminalBlock block) public static IMyTerminalBlock GetLimitedBlockBlock(object block) { - return getLimitedBlockBlock?.Invoke(block); + if (getLimitedBlockBlock == null) + { + MyLog.Default.WriteLine("SpecBlockHooks: getLimitedBlockBlock is null"); + return null; + } + + if (block == null) + { + MyLog.Default.WriteLine("SpecBlockHooks: block is null"); + return null; + } + + try + { + var limitedBlock = block as ILimitedBlock; + if (limitedBlock == null) + { + MyLog.Default.WriteLine($"SpecBlockHooks: block of type {block.GetType().FullName} does not implement ILimitedBlock"); + return null; + } + + return getLimitedBlockBlock.Invoke(block); + } + catch (InvalidCastException) + { + MyLog.Default.WriteLine($"SpecBlockHooks: Failed to cast block of type {block.GetType().FullName} to ILimitedBlock"); + return null; + } + catch (Exception e) + { + MyLog.Default.WriteLine($"SpecBlockHooks: Error in GetLimitedBlockBlock: {e.Message}"); + return null; + } } public static void RegisterCustomLimitConsumer(string Id, Func creator) { - SpecBlockHooks.RegisterCustomLimitConsumer(Id, + SpecBlockHooks.RegisterCustomLimitConsumer(Id, creator, - (logic)=> + (logic) => { - ((ILimitedBlock) logic).CanWork(); + var limitedBlock = logic as ILimitedBlock; + if (limitedBlock != null) limitedBlock.CanWork(); }, (block, logic) => { - return ((ILimitedBlock) logic).CheckConditions(block); + var limitedBlock = logic as ILimitedBlock; + return limitedBlock != null && limitedBlock.CheckConditions(block); }, - (logic)=> + (logic) => { - return ((ILimitedBlock) logic).CanBeDisabled(); + var limitedBlock = logic as ILimitedBlock; + return limitedBlock != null && limitedBlock.CanBeDisabled(); }, - (logic)=> + (logic) => { - return ((ILimitedBlock) logic).IsDrainingPoints(); + var limitedBlock = logic as ILimitedBlock; + return limitedBlock != null && limitedBlock.IsDrainingPoints(); }, - (logic)=> + (logic) => { - ((ILimitedBlock) logic).Disable(); + var limitedBlock = logic as ILimitedBlock; + if (limitedBlock != null) limitedBlock.Disable(); }); } - + public static Dictionary> GetGridBlocksByType(IMyCubeGrid grid) { + if (grid == null) + { + MyLog.Default.WriteLine("SpecBlockHooks: grid is null in GetGridBlocksByType"); + return null; + } return getGridBlocksByType?.Invoke(grid) ?? null; } - + public static Dictionary> GetGridBlocksById(IMyCubeGrid grid) { + if (grid == null) + { + MyLog.Default.WriteLine("SpecBlockHooks: grid is null in GetGridBlocksById"); + return null; + } return getGridBlocksById?.Invoke(grid) ?? null; } - - public static void HandleDamage(object target, ref MyDamageInformation damage) - { //Paralell - try - { - var slimBlock = target as IMySlimBlock; - if (slimBlock != null) - { - var cubeGrid = slimBlock.CubeGrid; - var core = SpecBlockHooks.GetMainSpecCore(slimBlock.CubeGrid); - var stats = new Dictionary(); - SpecBlockHooks.GetSpecCoreLimits(core, stats, SpecBlockHooks.GetSpecCoreLimitsEnum.CurrentStaticOrDynamic); - if(stats.ContainsKey(-33) && stats[-33]!=0) - { - damage.Amount *= stats [-33]; - } - } - } - catch(Exception e) - { - - } - } - - } + + public static void HandleDamage(object target, ref MyDamageInformation damage) + { //Parallel + try + { + var slimBlock = target as IMySlimBlock; + if (slimBlock == null) + { + MyLog.Default.WriteLine("SpecBlockHooks: target is not IMySlimBlock in HandleDamage"); + return; + } + + var cubeGrid = slimBlock.CubeGrid; + var core = SpecBlockHooks.GetMainSpecCore(cubeGrid); + if (core == null) + { + MyLog.Default.WriteLine("SpecBlockHooks: core is null in HandleDamage"); + return; + } + + var stats = new Dictionary(); + SpecBlockHooks.GetSpecCoreLimits(core, stats, SpecBlockHooks.GetSpecCoreLimitsEnum.CurrentStaticOrDynamic); + if (stats != null && stats.ContainsKey(-33) && stats[-33] != 0) + { + damage.Amount *= stats[-33]; + } + } + catch (Exception e) + { + MyLog.Default.WriteLine($"SpecBlockHooks: Error in HandleDamage: {e.Message}"); + } + } + + } } \ No newline at end of file