-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBus.cs
More file actions
530 lines (453 loc) · 15.5 KB
/
Copy pathBus.cs
File metadata and controls
530 lines (453 loc) · 15.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
#region Header
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Concurrent;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Globalization;
using Sandbox.ModAPI.Ingame;
using SpaceEngineers.Game.ModAPI.Ingame;
using VRage.Game.ModAPI.Ingame;
using VRageMath;
namespace SpaceEngineersScripting
{
public class Wrapper
{
static void Main()
{
}
}
class Program : MyGridProgram
{
#endregion
#region CodeEditor
//Configuration
//--------------------
//Definitions
//--------------------
//Types
//--------------------
public struct Bus
{
//The bus stores a series of records as a string, each ended by the terminator
// *It is not checked that records do not contain the terminator
// so make sure that you do not corrupt it by using this character
//Records have one of two Record Types:
//-Static: non-volatile; read or overwritten only
//-Temporary: appended on write (duplicates allowed), destructive read (FIFO)
//e.g. use static records for public status/exported data
//e.g. use temporary records to issue commands / directional data transfer
//Records have an Id allowing basic discrimination
//(e.g. source and/or destination and/or data as needed)
//Additionally, records all have a Data Type to allow for basic type checking.
//Different types make a record unique, even if they share an Id.
//(new types may be easily added, so long as they can be encoded/decoded
// from a string, and ensured not to contain the record terminator)
//Static records are stored at the head of the store
//Temporary records are appended to the end of the store
//(this simplifies searching)
//FORMAT
//<Record> ::= <RecordBody><recordTerminator>
//<RecordBody> ::= <RecordStatic> | <RecordTemporary>
//<RecordStatic> ::= <recordTypeStatic><id><Data>
//<RecordTemporary> ::= <recordTypeTemporary><id><Data>
//<id> ::= <string-lengthId>
//<Data> ::= <DataInt> | <DataFloat> | <DataString>
//<DataInt> ::= <dataTypeInt><int>
//<DataFloat> ::= <dataTypeFloat><float>
//<DataString> ::= <dataTypeFloat><string>
//e.g. Static float record 'Altitude' = 100.0f
//SAltitude________F100\n
//e.g. Temporary string record 'Cmd.Clock' = "reset"
//TCmd.Clock_______Sreset\n
//Configuration
const char
recordTerminator = '\n';//'\x1E'; //Record separator
const char
recordTypeStatic = 'S',
recordTypeTemp = 'T';
const char
dataTypeInt = 'I',
dataTypeFloat = 'F',
dataTypeString = 'S';
const int
lengthId = 16;
//The source of the storage
public IMyTerminalBlock
bus;
//Internal storage interface
private string Store{
get { return bus.CustomData; }
set { bus.CustomData = value; }
}
private void Append(string value){
bus.CustomData += value;
}
//Utility definitions
private const int
offsetRecordData = lengthId +2; //recordType +id +dataType
//Internal Implementation
/// <summary>
/// Returns a copy of the raw Data section of the given record (static or temporary).
/// </summary>
/// <returns>The raw Data section of the record referenced</returns>
/// <param name="store">The storage string to extract from.</param>
/// <param name="indexRecordStart">The index the record starts at.</param>
/// <remarks>
/// This procedure assumes the data structure of <paramref name="storage"/>
/// is correct; no checks are performed when trying to access it.
/// </remarks>
private static string ExtractRecordData(ref string store, int indexRecordStart){
int
indexDataStart = indexRecordStart +offsetRecordData,
indexDataEnd = indexDataStart;
//Data segment continues until the end of the record
while (store[indexDataEnd] != recordTerminator)
indexDataEnd++;
return store.Substring(indexDataStart, indexDataEnd -indexDataStart);
}
/// <summary>
/// Finds the static record meeting all of the requirements.
/// </summary>
/// <returns>The index of the start of the static record.</returns>
/// <param name="store">The storage string to search in.</param>
/// <param name="id">The record id to match.</param>
/// <param name="dataType">The record data type constant to match.</param>
/// <param name="startIndex">The index to advance from.</param>
/// <remarks>
/// This procedure assumes the data structure of <paramref name="storage"/>
/// is correct; no checks are performed when trying to access it.
/// </remarks>
private static int FindStaticRecord(ref string store, ref string id, char dataType, int startIndex=0){
//Find the index of the start of the record matching id and dataType
//-otherwise return -1
int i = startIndex;
while (i < store.Length) {
//Examine the current record
//-log the start index in case we need to return in
//-check that we are still examining static records
// >otherwise stop searching
//-check that the ids match
//-check that data types match
//-if they match, return the start index
// >otherwise, skip data and continue with the next record
int indexRecordStart = i;
//check record type
if (store[i++] != recordTypeStatic) {
//static records should all be at the beginning
//stop if we encounter another type
return -1;
} else {
//compare ids (without allocating more memory for strings)
bool matchId = true;
for (int ii=0; ii<lengthId; ii++) {
if (store[i++] != id[ii]) {
matchId = false;
//jump to the start of the data field
i = startIndex +offsetRecordData;
break;
}
}
//check id and if necessary data type
if (matchId && store[i++] == dataType) {
//record type, id and data type all match this record
return indexRecordStart;
} else {
//move on to next record
while (store[i++] != recordTerminator) {};
}
}
}
//if we run out of records, it was not found
return -1;
}
/// <summary>
/// WriteStatic archetype; overwrite/add raw string as a static record.
/// </summary>
private void WriteStaticData(ref string id, char dataType, ref string data){
string
store = Store;
int
indexRecordStart = FindStaticRecord(ref store, ref id, dataType);
if (indexRecordStart < 0) {
//the record was not found
//add the record to the start
Store =
recordTypeStatic +id +dataType +data +recordTerminator
+store;
} else {
//overwrite the existing record in-place
//-find the start of the next record
//-insert new record between end of previous record and next record
int indexRecordNext = indexRecordStart +offsetRecordData;
while (store[indexRecordNext++] != recordTerminator) {};
Store =
store.Substring (0, indexRecordStart)
+recordTypeStatic +id +dataType +data +recordTerminator
+store.Substring (indexRecordNext, store.Length -indexRecordNext);
}
}
/// <summary>
/// ReadStatic archetype; finds and returns the raw string from a static record.
/// </summary>
/// <returns>true if the record was found, otherwise false</returns>
private bool ReadStaticData(ref string id, char dataType, out string data){
string
store = Store;
int
indexRecordStart = FindStaticRecord(ref store, ref id, dataType);
if (indexRecordStart < 0) {
//the record was not found
data = null;
return false;
} else {
data = ExtractRecordData(ref store, indexRecordStart);
return true;
}
}
/// <summary>
/// DeleteStatic archetype; deletes a static record of the given Data Type.
/// </summary>
/// <returns>true if the record was found, otherwise false</returns>
private bool DeleteStaticData(ref string id, char dataType){
string
store = Store;
int
indexRecordStart = FindStaticRecord(ref store, ref id, dataType);
if (indexRecordStart < 0) {
//the record was not found
return false;
} else {
//delete the existing record
//-find the length of this record
//-remove that many characters from the store
int indexRecordNext = indexRecordStart +offsetRecordData;
while (store[indexRecordNext++] != recordTerminator) {};
Store = store.Remove(indexRecordStart, indexRecordNext -indexRecordStart);
return true;
}
}
/// <summary>
/// Finds the temporary record meeting all of the requirements.
/// </summary>
/// <returns>The index of the start of the temporary record.</returns>
/// <param name="store">The storage string to search in.</param>
/// <param name="id">The record id to match.</param>
/// <param name="dataType">The record data type constant to match.</param>
/// <param name="startIndex">The index to advance from.</param>
/// <remarks>
/// This procedure assumes the data structure of <paramref name="storage"/>
/// is correct; no checks are performed when trying to access it.
/// </remarks>
private static int FindTemporaryRecord(ref string store, ref string id, char dataType, int startIndex=0){
//Find the index of the start of the record matching id and dataType
//-otherwise return -1
int i = startIndex;
while (i < store.Length) {
//Examine the current record
//-log the start index in case we need to return in
//-check that we are examining a temporary record
// >otherwise skip past it
//-check that the ids match
//-check that data types match
//-if they match, return the start index
// >otherwise, skip data and continue with the next record
int indexRecordStart = i;
//check record type
if (store[i++] != recordTypeTemp) {
//skip any non-temporary records
//-jump to data field
//-skip until end of record
i += (offsetRecordData -1);
} else {
//compare ids (without allocating more memory for strings)
bool matchId = true;
for (int ii=0; ii<lengthId; ii++) {
if (store[i++] != id[ii]) {
matchId = false;
//jump to the start of the data field
i = startIndex +offsetRecordData;
break;
}
}
//check id and if necessary data type
if (matchId && store[i++] == dataType) {
//record type, id and data type all match this record
return indexRecordStart;
}
//else skip until end of record
}
//move on to next record
while (store[i++] != recordTerminator) {};
}
//if we run out of records, it was not found
return -1;
}
/// <summary>
/// AppendTemporary archetype; append raw string as a temporary record.
/// </summary>
private void AppendTemporaryData(ref string id, char dataType, ref string data){
Append(recordTypeTemp +id +dataType +data +recordTerminator);
}
/// <summary>
/// ReadTemporary archetype; finds and returns the raw string from a temporary record.
/// </summary>
/// <returns>true if the record was found, otherwise false</returns>
private bool ReadTemporaryData(ref string id, char dataType, out string data){
string
store = Store;
int
indexRecordStart = FindTemporaryRecord(ref store, ref id, dataType);
if (indexRecordStart < 0) {
//the record was not found
data = null;
return false;
} else {
data = ExtractRecordData(ref store, indexRecordStart);
//remove the record from the store
//-derive the total record length and remove that many characters
Store = store.Remove(indexRecordStart, offsetRecordData +data.Length +1);
return true;
}
}
//PUBLIC INTERFACE
public Bus(IMyTerminalBlock bus){
this.bus = bus;
}
public static string ExtendId(string id){
return id.PadRight(lengthId, ' ');
}
public void WriteStaticInt(string id, int value){
string data = value.ToString ();
WriteStaticData(ref id, dataTypeInt, ref data);
}
public bool ReadStaticInt(string id, out int value){
string data;
if ( ReadStaticData(ref id, dataTypeInt, out data) ) {
//Record found
//Validity depends on ability to parse the data
return int.TryParse(data, out value);
} else {
//The record could not be found
value = 0;
return false;
}
}
public bool DeleteStaticInt(string id){
return DeleteStaticData(ref id, dataTypeInt);
}
public void WriteStaticFloat(string id, float value){
string data = value.ToString ("R");
WriteStaticData(ref id, dataTypeFloat, ref data);
}
public bool ReadStaticFloat(string id, out float value){
string data;
if ( ReadStaticData(ref id, dataTypeFloat, out data) ) {
//Record found
//Validity depends on ability to parse the data
return float.TryParse(data, out value);
} else {
//The record could not be found
value = float.NaN;
return false;
}
}
public bool DeleteStaticFloat(string id){
return DeleteStaticData(ref id, dataTypeFloat);
}
public void WriteStaticString(string id, string value){
WriteStaticData(ref id, dataTypeString, ref value);
}
public bool ReadStaticString(string id, out string value){
if ( ReadStaticData(ref id, dataTypeString, out value) ) {
//Record found
return true;
} else {
//The record could not be found
value = null;
return false;
}
}
public bool DeleteStaticString(string id){
return DeleteStaticData(ref id, dataTypeString);
}
public void AppendTemporaryInt(string id, int value){
string data = value.ToString ();
AppendTemporaryData(ref id, dataTypeInt, ref data);
}
public bool ReadTemporaryInt(string id, out int value){
string data;
if ( ReadTemporaryData(ref id, dataTypeInt, out data) ) {
//Record found
//Validity depends on ability to parse the data
return int.TryParse(data, out value);
} else {
//The record could not be found
value = 0;
return false;
}
}
public void AppendTemporaryFloat(string id, float value){
string data = value.ToString ("R");
AppendTemporaryData(ref id, dataTypeFloat, ref data);
}
public bool ReadTemporaryFloat(string id, out float value){
string data;
if ( ReadTemporaryData(ref id, dataTypeFloat, out data) ) {
//Record found
//Validity depends on ability to parse the data
return float.TryParse(data, out value);
} else {
//The record could not be found
value = float.NaN;
return false;
}
}
public void AppendTemporaryString(string id, string value){
AppendTemporaryData(ref id, dataTypeString, ref value);
}
public bool ReadTemporaryString(string id, out string value){
if ( ReadTemporaryData(ref id, dataTypeString, out value) ) {
//Record found
return true;
} else {
//The record could not be found
value = null;
return false;
}
}
}
//Global variables
//--------------------
bool restarted = true;
IMyTextPanel text;
Bus bus;
readonly string
aId = Bus.ExtendId ("a"),
bId = Bus.ExtendId ("b");
float
a;
//Program
//--------------------
public void Main(string argument)
{
if (restarted){
text = (IMyTextPanel)GridTerminalSystem.GetBlockWithName ("Text Panel");
bus = new Bus (text);
restarted = false;
if ( !bus.ReadStaticFloat (aId, out a) ) {
a = 0.25f;
};
}
a *= 2.0f;
bus.WriteStaticFloat (aId, a);
bus.WriteStaticFloat(bId, (float)Math.Sqrt(a) );
text.WritePublicText (text.CustomData);
}
#endregion
#region footer
}
}
#endregion