Add a utility functions library.

This commit is contained in:
Anna Rose 2025-02-09 20:25:15 -05:00
parent eac331b4ee
commit 74331a95ba
3 changed files with 88 additions and 0 deletions

58
Mixins/Utils/Utils.cs Normal file
View File

@ -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<IMyTerminalBlock> GetBlocksNameContains(
IMyGridTerminalSystem gridSystem,
string name)
{
List<IMyTerminalBlock> allBlocks = new List<IMyTerminalBlock>();
List<IMyTerminalBlock> result = new List<IMyTerminalBlock>();
gridSystem.GetBlocks(allBlocks);
foreach (IMyTerminalBlock block in allBlocks)
{
if (block.CustomName.Contains(name)) result.Add(block);
}
return result;
}
public static void GetBlocksOfTypeNameContains<T>(
IMyGridTerminalSystem gridSystem,
List<T> result,
string name)
where T : class
{
List<T> allBlocks = new List<T>();
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 "";
}
}
}
}

View File

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<MSBuildAllProjects Condition="'$(MSBuildVersion)' == '' Or '$(MSBuildVersion)' &lt; '16.0'">$(MSBuildAllProjects);$(MSBuildThisFileFullPath)</MSBuildAllProjects>
<HasSharedItems>true</HasSharedItems>
<SharedGUID>8a3cdcc5-4b55-4d87-a415-698a0e1ff06f</SharedGUID>
</PropertyGroup>
<ItemGroup>
<Compile Include="$(MSBuildThisFileDirectory)/**/*.cs" Visible=" '$(ShowCommonFiles)' == 'true' " />
</ItemGroup>
</Project>

19
Mixins/Utils/Utils.shproj Normal file
View File

@ -0,0 +1,19 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup Label="Globals">
<ProjectGuid>8a3cdcc5-4b55-4d87-a415-698a0e1ff06f</ProjectGuid>
<MinimumVisualStudioVersion>14.0</MinimumVisualStudioVersion>
</PropertyGroup>
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')"/>
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\CodeSharing\Microsoft.CodeSharing.Common.Default.props"/>
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\CodeSharing\Microsoft.CodeSharing.Common.props"/>
<PropertyGroup/>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<OutputPath>bin\Debug\</OutputPath>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<OutputPath>bin\Release\</OutputPath>
</PropertyGroup>
<Import Project="Utils.projitems" Label="Shared"/>
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\CodeSharing\Microsoft.CodeSharing.CSharp.targets"/>
</Project>