From 74331a95ba2c4ae8896cb8120c28955fb7f8d1e6 Mon Sep 17 00:00:00 2001 From: annabunches Date: Sun, 9 Feb 2025 20:25:15 -0500 Subject: [PATCH] Add a utility functions library. --- Mixins/Utils/Utils.cs | 58 ++++++++++++++++++++++++++++++++++++ Mixins/Utils/Utils.projitems | 11 +++++++ Mixins/Utils/Utils.shproj | 19 ++++++++++++ 3 files changed, 88 insertions(+) create mode 100644 Mixins/Utils/Utils.cs create mode 100644 Mixins/Utils/Utils.projitems create mode 100644 Mixins/Utils/Utils.shproj diff --git a/Mixins/Utils/Utils.cs b/Mixins/Utils/Utils.cs new file mode 100644 index 0000000..2b6bfdf --- /dev/null +++ b/Mixins/Utils/Utils.cs @@ -0,0 +1,58 @@ +// The miscellaneous assortment of random methods that every lazy programmer +// inevitably creates. +// +// Do not rely on any of these functions hanging around long-term! +// See https://www.jeromethibaud.com/en/blog/utils-considered-harmful/ for why; +// when we're feeling less lazy these will probably get refactored, but this is +// just a fun personal project so, for now, YOLO. + +using Sandbox.ModAPI.Ingame; +using System.Collections.Generic; +using System.Text.RegularExpressions; + +namespace IngameScript +{ + partial class Program + { + public class Utils + { + public static List GetBlocksNameContains( + IMyGridTerminalSystem gridSystem, + string name) + { + List allBlocks = new List(); + List result = new List(); + gridSystem.GetBlocks(allBlocks); + + foreach (IMyTerminalBlock block in allBlocks) + { + if (block.CustomName.Contains(name)) result.Add(block); + } + return result; + } + + public static void GetBlocksOfTypeNameContains( + IMyGridTerminalSystem gridSystem, + List result, + string name) + where T : class + { + List allBlocks = new List(); + gridSystem.GetBlocksOfType(allBlocks); + + foreach (T block in allBlocks) + { + if (((IMyTerminalBlock)block).CustomName.Contains(name)) result.Add(block); + } + } + + public static string ExtractTag(IMyTerminalBlock block, string tag) + { + Regex r = new Regex($@"({tag}\w*?)\s"); + Match m = r.Match(block.CustomName); + if (m.Success) return m.Groups[0].Captures[0].ToString(); + return ""; + } + } + } +} \ No newline at end of file diff --git a/Mixins/Utils/Utils.projitems b/Mixins/Utils/Utils.projitems new file mode 100644 index 0000000..141cea5 --- /dev/null +++ b/Mixins/Utils/Utils.projitems @@ -0,0 +1,11 @@ + + + + $(MSBuildAllProjects);$(MSBuildThisFileFullPath) + true + 8a3cdcc5-4b55-4d87-a415-698a0e1ff06f + + + + + \ No newline at end of file diff --git a/Mixins/Utils/Utils.shproj b/Mixins/Utils/Utils.shproj new file mode 100644 index 0000000..998d504 --- /dev/null +++ b/Mixins/Utils/Utils.shproj @@ -0,0 +1,19 @@ + + + + 8a3cdcc5-4b55-4d87-a415-698a0e1ff06f + 14.0 + + + + + + + bin\Debug\ + + + bin\Release\ + + + +