using System; using System.Collections.Generic; using Sandbox.ModAPI.Ingame; namespace XFEExtension.SpaceEngineers.ScriptingHelper; /// /// 提供一个基类,用于在基于网格的环境中编写可编程脚本,使其能够响应系统事件执行自定义逻辑。
/// Provides a base class for programmable scripts in a grid-based environment, enabling custom logic execution in /// response to system events. ///
/// Inherit from ProgramBase to implement script behavior for grid systems. Override the Main method to /// define how your script responds to arguments and update events. This class is intended for use in environments where /// scripts are triggered by various update sources, such as timers or user actions. public abstract class ProgramBase : MyGridProgram { /// /// Retrieves a block of the specified type with the given name. /// /// The type of block to retrieve. /// The name of the block. /// The block instance if found; otherwise, null. public T GetBlock(string name) where T : class, IMyTerminalBlock { return GridTerminalSystem.GetBlockWithName(name) as T; } /// /// Retrieves the first block of the specified type that matches the optional filter. /// /// The type of block to retrieve. /// An optional filter function. /// The block instance if found; otherwise, null. public T GetBlock(Func collect = null) where T : class, IMyTerminalBlock { var list = new List(); GridTerminalSystem.GetBlocksOfType(list, collect); return list.Count > 0 ? list[0] : null; } /// /// Retrieves a list of blocks of the specified type, optionally filtered. /// /// The type of blocks to retrieve. /// An optional filter function. /// A list of blocks matching the criteria. public List GetBlocks(Func collect = null) where T : class, IMyTerminalBlock { var list = new List(); GridTerminalSystem.GetBlocksOfType(list, collect); return list; } /// /// 当游戏内脚本执行时调用。根据传入的参数和更新源执行相应的逻辑。
/// Executes the main entry point for the program, processing the specified argument based on the provided update /// source. ///
/// 需要处理的输入字符串。不能为空;表示要处理的命令或数据。
The input string to be processed. Cannot be null; represents the command or data to handle. /// 触发执行的更新源。决定如何解释和处理参数。
The source of the update triggering the execution. Determines how the argument is interpreted and processed. public abstract void Main(string argument, UpdateType updateSource); /// /// 当游戏内脚本需要保存其状态时调用。可以在此方法中实现任何必要的逻辑来持久化数据,以便在脚本重新加载时恢复状态。
/// Called when the program needs to save its state. ///
public abstract void Save(); }