Skip to content

Commit eb6e1dc

Browse files
committed
feat: delay instruction
1 parent 0ec571e commit eb6e1dc

18 files changed

+727
-372
lines changed

SequentialScript.Test/CustomData/Resources.Designer.cs

Lines changed: 237 additions & 214 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

SequentialScript.Test/CustomData/Resources.resx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,9 @@
124124
<data name="TestComments" type="System.Resources.ResXFileRef, System.Windows.Forms">
125125
<value>TestComments.txt;System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;utf-8</value>
126126
</data>
127+
<data name="TestDelay" type="System.Resources.ResXFileRef, System.Windows.Forms">
128+
<value>TestDelay.txt;System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;utf-8</value>
129+
</data>
127130
<data name="TestDependences" type="System.Resources.ResXFileRef, System.Windows.Forms">
128131
<value>TestDependences.txt;System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;utf-8</value>
129132
</data>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[OPEN]
2+
run
3+
Block -> Some Action /NoCheck /Key1:Value /Key2:"Value with spaces" /Key3:"Value with special characters :/"
4+
Delay 3000
5+
as @action

SequentialScript.Test/CustomData/TestHydrogen.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ as @thrusters_disabled
66
run
77
Hydrogen Example (Status LCD) -> Set /Index:-1 /Background:YELLOW /Text:Connecting...
88
Hydrogen Example (Piston) -> Extend
9-
as piston_extended
9+
as @piston_extended
1010

1111
when @piston_extended
1212
run

SequentialScript.Test/InstructionParserTest.cs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,5 +192,43 @@ public void TestHydrogen()
192192
commands = IngameScript.InstructionParser.Parse(CustomData.Resources.TestHydrogen);
193193
commands.ToString();
194194
}
195+
196+
[TestMethod]
197+
public void TestDelay()
198+
{
199+
IDictionary<string, IngameScript.ICommandInstruction> commands;
200+
var expected = new Dictionary<string, IngameScript.ICommandInstruction>()
201+
{
202+
{ "OPEN", new IngameScript.InstructionCommand {
203+
CommandName = "OPEN",
204+
Body = new Dictionary<string, IngameScript.InstructionBlock> {
205+
{ "@action", new IngameScript.InstructionBlock {
206+
Alias = "@action",
207+
PreviousAlias = new string[] { },
208+
Instructions = new []{ new IngameScript.Instruction {
209+
BlockName = "Block",
210+
ActionName = "Some Action",
211+
Arguments = new Dictionary<string, string> {
212+
{ "NoCheck", "" },
213+
{ "Key1", "Value" },
214+
{ "Key2", "Value with spaces" },
215+
{ "Key3", "Value with special characters :/" }
216+
},
217+
IsValid = true,
218+
}, new IngameScript.Instruction {
219+
BlockName = null,
220+
ActionName = "DELAY",
221+
Arguments = new Dictionary<string, string> { { "TIME", "3000" } },
222+
IsValid = true,
223+
}},
224+
} }
225+
}.Values
226+
}}
227+
};
228+
229+
commands = IngameScript.InstructionParser.Parse(CustomData.Resources.TestDelay);
230+
InstrucionAssert.AreEqual(expected, commands);
231+
}
232+
195233
}
196234
}

SequentialScript.Test/SequentialScript.Test.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
</ProjectReference>
7272
</ItemGroup>
7373
<ItemGroup>
74+
<Content Include="CustomData\TestDelay.txt" />
7475
<Content Include="CustomData\TestHydrogen.txt" />
7576
<Content Include="CustomData\TestArguments.txt" />
7677
<Content Include="CustomData\TestComments.txt" />
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
R e a d m e
22
-----------
33

4-
V1.1-alpha.6
4+
V1.2-alpha
55
See more information in the following link:
66
https://github.com/space-engineers-hf/SequentialScript

SequentialScript/Instructions/InstructionParser.cs

Lines changed: 82 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -125,40 +125,52 @@ private static InstructionCommand CreateCommand(System.Text.RegularExpressions.M
125125
.Where(line => !string.IsNullOrWhiteSpace(line) && !line.StartsWith("//")) // Ignore empty lines and comments.
126126
.Select((line, index) =>
127127
{
128+
Instruction instruction = null;
129+
128130
// Remove comments
129131
var lineComments = line.Split(new[] { "//" }, StringSplitOptions.None);
130-
var lineWithoutComments = lineComments.First();
131-
132-
// Take BlockName and ActionName
133-
var lineItems = lineWithoutComments.Trim().Split(new[] { "->" }, StringSplitOptions.RemoveEmptyEntries);
134-
string blockName = null, actionName = null;
135-
bool isValid;
132+
var lineWithoutComments = lineComments.First().Trim();
136133

137-
if (lineItems.Length > 0)
134+
// Create instruction.
135+
if (lineWithoutComments.Contains("->"))
138136
{
139-
blockName = lineItems[0].Trim();
137+
// Instruction of IMyTerminalBlock -> Action
138+
instruction = CreateInstruction(lineWithoutComments);
140139
}
141-
if (lineItems.Length > 1)
140+
else
142141
{
143-
actionName = lineItems[1].Trim();
144-
}
145-
isValid = lineItems.Length == 2;
146-
147-
// Arguments
148-
var arguments = GetArguments(actionName);
142+
// Script reserved instructions.
143+
var lineItems = lineWithoutComments.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
144+
var command = lineItems.FirstOrDefault();
149145

146+
if (command == null)
147+
{
148+
instruction = null;
149+
}
150+
else if (command.Equals("DELAY", StringComparison.OrdinalIgnoreCase))
151+
{
152+
instruction = CreateInstructionDelay(lineItems);
153+
}
154+
if (instruction == null)
155+
{
156+
instruction = new Instruction
157+
{
158+
BlockName = null,
159+
ActionName = null,
160+
Arguments = null,
161+
IsValid = false
162+
};
163+
}
164+
}
150165
return new
151166
{
152167
Line = index + 1,
153-
BlockName = blockName,
154-
ActionName = arguments[""].Trim(),
155-
Arguments = arguments.Where(x => !string.IsNullOrEmpty(x.Key)).ToDictionary(x => x.Key, x => x.Value, StringComparer.OrdinalIgnoreCase),
156-
IsValid = isValid
168+
Instruction = instruction,
157169
};
158170
})
159171
;
160172

161-
var invalid = actions.Where(x => !x.IsValid);
173+
var invalid = actions.Where(x => !x.Instruction.IsValid);
162174
if (invalid.Any())
163175
{
164176
throw new SyntaxException(
@@ -197,13 +209,7 @@ private static InstructionCommand CreateCommand(System.Text.RegularExpressions.M
197209
{
198210
Alias = alias,
199211
PreviousAlias = previousActionsMatch.Select(x => x.Alias),
200-
Instructions = actions.Select(x => new Instruction
201-
{
202-
BlockName = x.BlockName,
203-
ActionName = x.ActionName,
204-
Arguments = x.Arguments,
205-
IsValid = x.IsValid
206-
})
212+
Instructions = actions.Select(x => x.Instruction)
207213
});
208214
}
209215
}
@@ -237,6 +243,55 @@ private static InstructionCommand CreateCommand(System.Text.RegularExpressions.M
237243
return result;
238244
}
239245

246+
private static Instruction CreateInstruction(string lineWithoutComments)
247+
{
248+
// Take BlockName and ActionName
249+
var lineItems = lineWithoutComments.Split(new[] { "->" }, StringSplitOptions.RemoveEmptyEntries);
250+
string blockName = null, actionName = null;
251+
bool isValid;
252+
253+
if (lineItems.Length > 0)
254+
{
255+
blockName = lineItems[0].Trim();
256+
}
257+
if (lineItems.Length > 1)
258+
{
259+
actionName = lineItems[1].Trim();
260+
}
261+
isValid = lineItems.Length == 2;
262+
263+
// Arguments
264+
var arguments = GetArguments(actionName);
265+
266+
// Return
267+
return new Instruction
268+
{
269+
BlockName = blockName,
270+
ActionName = arguments[""].Trim(),
271+
Arguments = arguments.Where(x => !string.IsNullOrEmpty(x.Key)).ToDictionary(x => x.Key, x => x.Value, StringComparer.OrdinalIgnoreCase),
272+
IsValid = isValid
273+
};
274+
}
275+
276+
private static Instruction CreateInstructionDelay(string[] lineItems)
277+
{
278+
string timeString = null;
279+
280+
if (lineItems.Length >= 1)
281+
{
282+
timeString = lineItems[1];
283+
}
284+
return new Instruction
285+
{
286+
BlockName = null,
287+
ActionName = "DELAY",
288+
Arguments = new Dictionary<string, string>() {
289+
{ "TIME", timeString }
290+
},
291+
IsValid = true
292+
};
293+
}
294+
240295
private static ConditionCommandInstruction CreateConditionCommand(System.Text.RegularExpressions.Match commandMatch)
241296
{
242297
ConditionCommandInstruction result = null;

SequentialScript/MDK/MDK.options.props

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66
-->
77
<MDKVersion>1.4.14</MDKVersion>
88
<MDKTrimTypes>
9-
<Enabled>no</Enabled>
9+
<Enabled>yes</Enabled>
1010
</MDKTrimTypes>
1111
<MDKMinify>
12-
<Level>StripComments</Level>
12+
<Level>Lite</Level>
1313
</MDKMinify>
1414
<MDKIgnore>
1515
<Folder>mdk</Folder>

SequentialScript/Program.cs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ partial class Program : MyGridProgram
3030
static readonly UpdateFrequency UPDATE_FREQUENCY = UpdateFrequency.Update10; // Update1, Update10, Update100
3131
static readonly int UPDATE_TICKS = 0; // Very slow mode multiplier (for debug)
3232

33+
34+
/* ----------------------------------------------------------------------------------- */
35+
/* --- ¡¡¡IMPORTANT!!! Do not change anything below this line. --- */
36+
/* ----------------------------------------------------------------------------------- */
37+
3338
#endregion
3439

3540
DateTime _momento;
@@ -76,7 +81,9 @@ public void Main(string argument, UpdateType updateSource)
7681
.OfType<InstructionCommand>()
7782
.SelectMany(cmd => cmd.Body)
7883
.SelectMany(body => body.Instructions)
79-
.Select(instruction => instruction.BlockName);
84+
.Where(instruction => instruction.BlockName != null)
85+
.Select(instruction => instruction.BlockName)
86+
.Distinct();
8087

8188
AdvancedEcho($"Building dictionary", append: true);
8289
_blocksDictionary = Helper.CreateBlockDictionary(blockNames, _terminalBlocks, _terminalGroups);
@@ -283,7 +290,7 @@ void EndCycle()
283290

284291
void AdvancedEchoReset()
285292
{
286-
_momento = DateTime.Now;
293+
_momento = DateTime.UtcNow;
287294
}
288295

289296
void AdvancedEcho(string message, bool append = false)
@@ -297,7 +304,7 @@ void AdvancedEcho(string message, bool append = false)
297304
builder.Append(value);
298305
message = builder.ToString();
299306
}
300-
message = $"| Elapsed {(DateTime.Now - _momento).TotalMilliseconds:00}ms |\n{message}";
307+
message = $"| Elapsed {(DateTime.UtcNow - _momento).TotalMilliseconds:00}ms |\n{message}";
301308

302309
Echo(message);
303310
if (DEBUG_IN_SCREEN)

0 commit comments

Comments
 (0)