Last active
March 13, 2017 23:07
-
-
Save Dawnkeeper/09a1a5f1e93dae01026e0b207af57462 to your computer and use it in GitHub Desktop.
SpaceEngineers Scripting Helper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* SpaceEngineers Scripting Helper | |
* | |
* start with script filename as argument | |
* | |
* possible instructions: | |
* | |
* /**Begin copy here** | |
* /**End copy here** | |
* | |
* //#include(stringRelativeFilePath,boolMakeCompact) | |
* //#replace(what,withwhat) | |
* //#EnableRemoveLineComments | |
* //#DisableRemoveLineComments | |
* //#EnableDebug | |
* | |
*/ | |
using System.Collections.Generic; | |
namespace SpaceSharp | |
{ | |
using System; | |
using System.IO; | |
using System.Linq; | |
using System.Text; | |
using System.Windows.Forms; | |
class Program | |
{ | |
[STAThread] | |
static void Main(string[] aargh) | |
{ | |
if (!aargh.Any()) | |
{ | |
MessageBox.Show("missing argument"); | |
return; | |
} | |
Clipboard.SetText(new Parser(new FileInfo(aargh[0]).FullName, false, first: true).Parse()); | |
} | |
} | |
public class ParseRule | |
{ | |
public readonly Func<string, Parser, bool> Condition; // line, parser, return | |
public readonly Func<string, Parser, string> Action; // line, parser, return | |
public readonly string Name; | |
public readonly bool ContinueWithNextRule; | |
public readonly bool UseReturnValue; | |
public ParseRule (string name, Func<string, Parser, bool> condition, Func<string, Parser , string> action, bool continueWithNextRule = false, bool UseReturnValue = true ) | |
{ | |
this.Condition = condition; | |
this.Action = action; | |
this.Name = name; | |
this.ContinueWithNextRule = continueWithNextRule; | |
this.UseReturnValue = UseReturnValue; | |
} | |
} | |
public class Parser | |
{ | |
static List<ParseRule> rules; | |
static Parser () | |
{ | |
rules = new List<ParseRule>(); | |
rules.Add ( | |
new ParseRule ( | |
"EnableDebug", | |
(line, parser) =>line.Contains("//#EnableDebug"), | |
(line, parser) => | |
{ | |
parser.Options["DEBUG"] = true; | |
return String.Empty; | |
}, | |
true, | |
false | |
)); | |
rules.Add ( | |
new ParseRule ( | |
"EnableRemoveLineComment", | |
(line, parser) =>line.Contains("//#EnableRemoveLineComments"), | |
(line, parser) => | |
{ | |
parser.Options["EnableRemoveLineComments"] = true; | |
return String.Empty; | |
}, | |
true, | |
false | |
)); | |
rules.Add ( | |
new ParseRule ( | |
"DisableRemoveLineComment", | |
(line,parser) =>line.Contains("//#DisableRemoveLineComments"), | |
(line, parser) => | |
{ | |
parser.Options["EnableRemoveLineComments"] = false; | |
return String.Empty; | |
}, | |
true, | |
false | |
)); | |
rules.Add ( | |
new ParseRule ( | |
"Include", | |
(line,parser) => line.Trim ().StartsWith ("//#include"), | |
(line, parser) => | |
{ | |
var incl = | |
line.Trim () | |
.Substring (line.Trim ().IndexOf ('(') + 1, line.Trim ().IndexOf (')') - line.Trim ().IndexOf ('(') - 1) | |
.Split (new[] { ',' }); | |
return new Parser(new FileInfo (parser.fileName).Directory.FullName + "\\" + incl [0], bool.Parse (incl [1])).Parse(); | |
} | |
)); | |
rules.Add ( | |
new ParseRule ( | |
"replace", | |
(line,parser) =>line.Trim ().Contains ("//#replace"), | |
(line, parser) => | |
{ | |
var curr = line.Trim (); | |
var start = curr.IndexOf ("//#replace"); | |
var begin = curr.IndexOf ('(', start); | |
var replaceIn = curr.Substring (0, start); | |
var repWhat = | |
curr | |
.Substring (begin + 1, line.Trim ().IndexOf (')', begin) - begin - 1) | |
.Split (new[] { ',' }); | |
var result = replaceIn.Replace (repWhat [0], repWhat [1]); | |
return result; | |
} | |
)); | |
rules.Add ( | |
new ParseRule ( | |
"RemoveLineComments", | |
(line, parser) =>{ | |
if(!parser.Options.ContainsKey("EnableRemoveLineComments")) return false; | |
if(!parser.Options["EnableRemoveLineComments"]) return false; | |
return line.Trim ().Contains ("//"); | |
}, | |
(line, parser) => | |
{ | |
var pos = line.IndexOf("//"); | |
return line.Substring(0,pos); | |
} | |
)); | |
/** | |
* | |
* THIS SHOULD ALWAYS BE THE LAST RULE | |
* | |
* */ | |
rules.Add ( | |
new ParseRule ( | |
"addNonEmpty", | |
(line, parser) =>!string.IsNullOrWhiteSpace(line), | |
(line, parser) => | |
{ | |
return line.Trim(); | |
} | |
)); | |
} | |
public Dictionary<string,bool> Options = new Dictionary<string, bool> (); | |
StreamReader ReadMe; | |
bool MakeCompact; | |
int lineThreshold; | |
bool first; | |
string fileName; | |
public Parser (string fileName, bool makeCompact, int lineThreshold = -1, bool first = false) | |
{ | |
this.fileName = fileName; | |
this.ReadMe = new StreamReader (fileName); | |
this.MakeCompact = makeCompact; | |
this.lineThreshold = lineThreshold; | |
this.first = first; | |
} | |
public string Parse() | |
{ | |
try | |
{ | |
var addMe = new StringBuilder (); | |
if (!first) | |
addMe.AppendLine(); | |
string line; | |
var started = false; | |
var stop = false; | |
while ((line = ReadMe.ReadLine ()) != null && !stop) | |
{ | |
if (!started) | |
{ | |
if(line.Contains ("/**Begin copy here**")) | |
{ | |
started = true; | |
} | |
continue; | |
} | |
if (line.Contains ("/**End copy here**")) | |
{ | |
stop = true; | |
continue; | |
} | |
foreach (var rule in rules) | |
{ | |
if(rule.Condition(line,this)) | |
{ | |
try | |
{ | |
var result = rule.Action(line,this); | |
if(rule.UseReturnValue) | |
{ | |
if(result == null) | |
{ | |
MessageBox.Show ("Error during rule execution of '" + rule.Name + "' in file " + new FileInfo(fileName).Name); | |
Environment.Exit(1); | |
} | |
addMe.Append(result); | |
if(!MakeCompact) addMe.AppendLine(); | |
} | |
if(!rule.ContinueWithNextRule)break; | |
} | |
catch (Exception x) | |
{ | |
MessageBox.Show ("Error during rule execution of '" + rule.Name + "' in file " + new FileInfo(fileName).Name+"\n\n"+((Options.ContainsKey("DEBUG") && Options["DEBUG"])? x.ToString() : x.GetType().ToString())); | |
Environment.Exit (1); | |
} | |
} | |
} | |
} | |
//addMe.AppendLine(); | |
return addMe.ToString(); | |
} | |
catch (Exception x) | |
{ | |
MessageBox.Show ("Outer NOPE:\n" + x); | |
Environment.Exit (1); | |
} | |
return null; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment