Replace ConfigParser with SDK-provided MyIni
This commit is contained in:
parent
37511ac473
commit
e82303ff00
|
@ -24,7 +24,6 @@
|
||||||
<AdditionalFiles Include="thumb.png" />
|
<AdditionalFiles Include="thumb.png" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<Import Project="..\Mixins\Console\Console.projitems" Label="shared" />
|
<Import Project="..\Mixins\Console\Console.projitems" Label="shared" />
|
||||||
<Import Project="..\Mixins\ConfigParser\ConfigParser.projitems" Label="shared" />
|
|
||||||
<Import Project="..\Mixins\Sequencer\Sequencer.projitems" Label="shared" />
|
<Import Project="..\Mixins\Sequencer\Sequencer.projitems" Label="shared" />
|
||||||
<Import Project="..\Mixins\Utils\Utils.projitems" Label="shared" />
|
<Import Project="..\Mixins\Utils\Utils.projitems" Label="shared" />
|
||||||
</Project>
|
</Project>
|
|
@ -8,6 +8,7 @@ namespace IngameScript
|
||||||
public partial class Program : MyGridProgram
|
public partial class Program : MyGridProgram
|
||||||
{
|
{
|
||||||
private MyCommandLine _cli;
|
private MyCommandLine _cli;
|
||||||
|
private MyIni _ini;
|
||||||
private Console _console;
|
private Console _console;
|
||||||
private List<IEnumerator<bool>> _jobs;
|
private List<IEnumerator<bool>> _jobs;
|
||||||
private Dictionary<string, Sequencer> _doors;
|
private Dictionary<string, Sequencer> _doors;
|
||||||
|
@ -17,13 +18,14 @@ namespace IngameScript
|
||||||
{
|
{
|
||||||
_tickCount = 0;
|
_tickCount = 0;
|
||||||
_cli = new MyCommandLine();
|
_cli = new MyCommandLine();
|
||||||
_console = new Console(this);
|
_ini = new MyIni();
|
||||||
|
_console = new Console(this, _ini);
|
||||||
_jobs = new List<IEnumerator<bool>>();
|
_jobs = new List<IEnumerator<bool>>();
|
||||||
|
|
||||||
_doors = new Dictionary<string, Sequencer>();
|
_doors = new Dictionary<string, Sequencer>();
|
||||||
|
|
||||||
List<IMyTerminalBlock> doorParts = Utils.GetBlocksNameContains(GridTerminalSystem, "!Door");
|
List<IMyTerminalBlock> doorBlocks = new List<IMyTerminalBlock>();
|
||||||
foreach (IMyTerminalBlock block in doorParts)
|
GridTerminalSystem.GetBlocksOfType<IMyTerminalBlock>(doorBlocks, block => block.CustomName.Contains("!Door"));
|
||||||
|
foreach (IMyTerminalBlock block in doorBlocks)
|
||||||
{
|
{
|
||||||
string doorName = Utils.ExtractTag(block, "!Door");
|
string doorName = Utils.ExtractTag(block, "!Door");
|
||||||
|
|
||||||
|
@ -36,7 +38,7 @@ namespace IngameScript
|
||||||
// Add the part; the Door object handles typing and sequencing.
|
// Add the part; the Door object handles typing and sequencing.
|
||||||
int defaultStep = 1;
|
int defaultStep = 1;
|
||||||
if (block is IMyShipMergeBlock) defaultStep = 0;
|
if (block is IMyShipMergeBlock) defaultStep = 0;
|
||||||
ISequenceable wrapped = SequenceableFactory.MakeSequenceable(block, defaultStep);
|
ISequenceable wrapped = SequenceableFactory.MakeSequenceable(block, _ini, defaultStep);
|
||||||
if (wrapped == null) { _console.Print($"Tried to add incompatible block '{block.CustomName}'"); continue; }
|
if (wrapped == null) { _console.Print($"Tried to add incompatible block '{block.CustomName}'"); continue; }
|
||||||
_doors[doorName].AddBlock(wrapped);
|
_doors[doorName].AddBlock(wrapped);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,47 +0,0 @@
|
||||||
using Sandbox.ModAPI.Ingame;
|
|
||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
|
|
||||||
namespace IngameScript
|
|
||||||
{
|
|
||||||
partial class Program
|
|
||||||
{
|
|
||||||
public class ConfigParser
|
|
||||||
{
|
|
||||||
private Dictionary<string, string> _config;
|
|
||||||
private IMyTerminalBlock _input;
|
|
||||||
|
|
||||||
public ConfigParser(IMyTerminalBlock input)
|
|
||||||
{
|
|
||||||
_input = input;
|
|
||||||
_config = new Dictionary<string, string>();
|
|
||||||
Parse();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get a config value, or a default value.
|
|
||||||
// that also does type inference, but the type of `defaultValue` must contain a `Parse()` method.
|
|
||||||
public T GetValue<T>(string key, T defaultValue)
|
|
||||||
{
|
|
||||||
if (!_config.ContainsKey(key))
|
|
||||||
{
|
|
||||||
return defaultValue;
|
|
||||||
|
|
||||||
}
|
|
||||||
return (T)Convert.ChangeType(_config[key], typeof(T));
|
|
||||||
}
|
|
||||||
|
|
||||||
// Only call this method manually if you are monitoring CustomData for changes.
|
|
||||||
public void Parse()
|
|
||||||
{
|
|
||||||
_config.Clear();
|
|
||||||
string[] lines = _input.CustomData.Split('\n');
|
|
||||||
foreach (string line in lines)
|
|
||||||
{
|
|
||||||
string[] tokens = line.Split('=');
|
|
||||||
if (tokens.Length != 2) continue;
|
|
||||||
_config[tokens[0]] = tokens[1];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,11 +0,0 @@
|
||||||
<?xml version="1.0" encoding="utf-8"?>
|
|
||||||
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
|
||||||
<PropertyGroup>
|
|
||||||
<MSBuildAllProjects Condition="'$(MSBuildVersion)' == '' Or '$(MSBuildVersion)' < '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>
|
|
|
@ -1,19 +0,0 @@
|
||||||
<?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="ConfigParser.projitems" Label="Shared"/>
|
|
||||||
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\CodeSharing\Microsoft.CodeSharing.CSharp.targets"/>
|
|
||||||
</Project>
|
|
|
@ -5,6 +5,7 @@
|
||||||
using Sandbox.ModAPI.Ingame;
|
using Sandbox.ModAPI.Ingame;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
using VRage.Game.ModAPI.Ingame.Utilities;
|
||||||
|
|
||||||
namespace IngameScript
|
namespace IngameScript
|
||||||
{
|
{
|
||||||
|
@ -24,14 +25,14 @@ namespace IngameScript
|
||||||
|
|
||||||
private const int DefaultMaxLines = 10;
|
private const int DefaultMaxLines = 10;
|
||||||
|
|
||||||
public Console(MyGridProgram program)
|
public Console(MyGridProgram program, MyIni ini)
|
||||||
{
|
{
|
||||||
_program = program;
|
_program = program;
|
||||||
_buffer = new List<string>();
|
_buffer = new List<string>();
|
||||||
|
|
||||||
// Check the PB's custom data for a maxlines directive.
|
// Check the PB's custom data for a maxlines directive.
|
||||||
ConfigParser config = new ConfigParser(_program.Me);
|
ini.TryParse(program.Me.CustomData);
|
||||||
_maxLines = config.GetValue("ConsoleMaxLines", DefaultMaxLines);
|
_maxLines = ini.Get("console", "maxLines").ToInt32(DefaultMaxLines);
|
||||||
}
|
}
|
||||||
|
|
||||||
public PrefixedConsole CreatePrefixedConsole(string prefix)
|
public PrefixedConsole CreatePrefixedConsole(string prefix)
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
using Sandbox.ModAPI.Ingame;
|
using Sandbox.ModAPI.Ingame;
|
||||||
using SpaceEngineers.Game.ModAPI.Ingame;
|
using SpaceEngineers.Game.ModAPI.Ingame;
|
||||||
|
using VRage.Game.ModAPI.Ingame.Utilities;
|
||||||
|
|
||||||
namespace IngameScript
|
namespace IngameScript
|
||||||
{
|
{
|
||||||
|
@ -9,11 +10,14 @@ namespace IngameScript
|
||||||
{
|
{
|
||||||
public class SequenceableFactory
|
public class SequenceableFactory
|
||||||
{
|
{
|
||||||
public static ISequenceable MakeSequenceable(IMyTerminalBlock block, int step = 0)
|
public static ISequenceable MakeSequenceable(
|
||||||
|
IMyTerminalBlock block,
|
||||||
|
MyIni ini,
|
||||||
|
int step = 0)
|
||||||
{
|
{
|
||||||
if (block is IMyMotorStator)
|
if (block is IMyMotorStator)
|
||||||
{
|
{
|
||||||
return new SequenceableRotor(block as IMyMotorStator, step);
|
return new SequenceableRotor(block as IMyMotorStator, ini, step);
|
||||||
}
|
}
|
||||||
if (block is IMyPistonBase)
|
if (block is IMyPistonBase)
|
||||||
{
|
{
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
using Sandbox.ModAPI.Ingame;
|
using Sandbox.ModAPI.Ingame;
|
||||||
using System.Collections.Generic;
|
|
||||||
using System;
|
using System;
|
||||||
|
using VRage.Game.ModAPI.Ingame.Utilities;
|
||||||
|
|
||||||
namespace IngameScript
|
namespace IngameScript
|
||||||
{
|
{
|
||||||
|
@ -19,15 +19,18 @@ namespace IngameScript
|
||||||
private float _closedAngle;
|
private float _closedAngle;
|
||||||
private IMyMotorStator _rotor;
|
private IMyMotorStator _rotor;
|
||||||
|
|
||||||
public SequenceableRotor(IMyMotorStator rotor, int defaultStep = 0)
|
public SequenceableRotor(
|
||||||
|
IMyMotorStator rotor,
|
||||||
|
MyIni ini,
|
||||||
|
int defaultStep = 0)
|
||||||
{
|
{
|
||||||
_rotor = rotor;
|
_rotor = rotor;
|
||||||
|
|
||||||
ConfigParser config = new ConfigParser(_rotor);
|
ini.TryParse(rotor.CustomData);
|
||||||
_openAngle = config.GetValue("OpenAngle", 90F);
|
_openAngle = ini.Get("sequencer", "openAngle").ToSingle(90F);
|
||||||
_closedAngle = config.GetValue("ClosedAngle", 0F);
|
_closedAngle = ini.Get("sequencer", "closedAngle").ToSingle(0F);
|
||||||
_velocity = config.GetValue("Velocity", 5F);
|
_velocity = ini.Get("sequencer", "velocity").ToSingle(5F);
|
||||||
Step = config.GetValue("Step", defaultStep);
|
Step = ini.Get("sequencer", "step").ToInt32(defaultStep);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Start(bool reverse)
|
public void Start(bool reverse)
|
||||||
|
|
|
@ -48,9 +48,9 @@ namespace IngameScript
|
||||||
|
|
||||||
public static string ExtractTag(IMyTerminalBlock block, string tag)
|
public static string ExtractTag(IMyTerminalBlock block, string tag)
|
||||||
{
|
{
|
||||||
Regex r = new Regex($@"({tag}\w*?)\s");
|
foreach (string token in block.CustomName.Split(' ')) {
|
||||||
Match m = r.Match(block.CustomName);
|
if (token.StartsWith(tag)) return token;
|
||||||
if (m.Success) return m.Groups[0].Captures[0].ToString();
|
}
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user