Changeset 10401 for trunk/sources/HeuristicLab.HLScript/3.3
- Timestamp:
- 01/27/14 14:13:02 (11 years ago)
- Location:
- trunk/sources/HeuristicLab.HLScript/3.3
- Files:
-
- 1 edited
- 2 moved
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.HLScript/3.3/HeuristicLab.HLScript-3.3.csproj
r10391 r10401 87 87 <Compile Include="VariableStore.cs" /> 88 88 <None Include="Plugin.cs.frame" /> 89 <Compile Include=" HLScript.cs" />90 <Compile Include=" HLScriptGeneration.cs" />89 <Compile Include="Script.cs" /> 90 <Compile Include="UserScriptBase.cs" /> 91 91 <Compile Include="Plugin.cs" /> 92 92 <Compile Include="Properties\AssemblyInfo.cs" /> -
trunk/sources/HeuristicLab.HLScript/3.3/Script.cs
r10400 r10401 17 17 18 18 namespace HeuristicLab.HLScript { 19 [Item(" HLScript", "A HeuristicLab script.")]19 [Item("Script", "A HeuristicLab script.")] 20 20 [Creatable("Scripts")] 21 21 [StorableClass] 22 public sealed class HLScript : NamedItem, IStorableContent {22 public sealed class Script : NamedItem, IStorableContent { 23 23 #region Constants 24 private const string ScriptNamespaceName = "HeuristicLab.HLScript";25 24 private const string ExecuteMethodName = "Execute"; 26 25 private const string CodeTemplate = … … 29 28 using System; 30 29 31 public override void Main() { 32 // type your code here 33 } 34 35 // further classes and methods"; 30 namespace UserScripts { 31 public class UserScript : HeuristicLab.HLScript.UserScriptBase { 32 public override void Main() { 33 // type your code here 34 } 35 36 // further classes and methods 37 38 } 39 }"; 36 40 #endregion 37 41 38 42 #region Fields & Properties 39 private HLScriptGeneration compiledHLScript;43 private UserScriptBase compiledScript; 40 44 41 45 public string Filename { get; set; } … … 58 62 if (value == code) return; 59 63 code = value; 60 compiled HLScript = null;64 compiledScript = null; 61 65 OnCodeChanged(); 62 66 } … … 80 84 #region Construction & Initialization 81 85 [StorableConstructor] 82 private HLScript(bool deserializing) : base(deserializing) { }83 private HLScript(HLScript original, Cloner cloner)86 private Script(bool deserializing) : base(deserializing) { } 87 private Script(Script original, Cloner cloner) 84 88 : base(original, cloner) { 85 89 code = original.code; … … 90 94 } 91 95 92 public HLScript()93 : base(" HLScript", "A HeuristicLab script.") {96 public Script() 97 : base("Script", "A HeuristicLab script.") { 94 98 code = CodeTemplate; 95 99 variableStore = new VariableStore(); … … 97 101 98 102 public override IDeepCloneable Clone(Cloner cloner) { 99 return new HLScript(this, cloner);103 return new Script(this, cloner); 100 104 } 101 105 #endregion 102 106 103 107 private void RegisterScriptEvents() { 104 if (compiled HLScript == null) return;105 compiled HLScript.ConsoleOutputChanged += compiledHLScript_ConsoleOutputChanged;108 if (compiledScript == null) return; 109 compiledScript.ConsoleOutputChanged += compiledScript_ConsoleOutputChanged; 106 110 } 107 111 108 112 private void DeregisterScriptEvents() { 109 if (compiled HLScript == null) return;110 compiled HLScript.ConsoleOutputChanged -= compiledHLScript_ConsoleOutputChanged;113 if (compiledScript == null) return; 114 compiledScript.ConsoleOutputChanged -= compiledScript_ConsoleOutputChanged; 111 115 } 112 116 … … 143 147 public void Compile() { 144 148 var results = DoCompile(); 145 compiled HLScript = null;149 compiledScript = null; 146 150 CompileErrors = results.Errors; 147 151 if (results.Errors.HasErrors) { … … 160 164 var types = assembly.GetTypes(); 161 165 DeregisterScriptEvents(); 162 compiled HLScript = (HLScriptGeneration)Activator.CreateInstance(types[0]);166 compiledScript = (UserScriptBase)Activator.CreateInstance(types[0]); 163 167 RegisterScriptEvents(); 164 168 } … … 181 185 } 182 186 183 private readonly Regex LineSplitter = new Regex(@"\r\n|\r|\n");184 187 private readonly Regex SafeTypeNameCharRegex = new Regex("[_a-zA-Z0-9]+"); 185 188 private readonly Regex SafeTypeNameRegex = new Regex("[_a-zA-Z][_a-zA-Z0-9]*"); 186 private readonly Regex NamespaceDeclarationRegex = new Regex(@"using\s+(@?[a-z_A-Z]\w+(?:\s*\.\s*@?[a-z_A-Z]\w*)*)\s*;");187 private readonly Regex NamespaceRegex = new Regex(@"(@?[a-z_A-Z]\w+(?:\s*\.\s*@?[a-z_A-Z]\w*)*)");188 private readonly Regex CommentRegex = new Regex(@"((/\*)[^/]+(\*/))|(//.*)");189 189 190 190 private CodeCompileUnit CreateCompilationUnit() { 191 var ns = new CodeNamespace(ScriptNamespaceName); 192 ns.Types.Add(CreateScriptClass()); 193 ns.Imports.AddRange( 194 GetNamespaces() 195 .Select(n => new CodeNamespaceImport(n)) 196 .ToArray()); 197 var unit = new CodeCompileUnit(); 198 unit.Namespaces.Add(ns); 191 var unit = new CodeSnippetCompileUnit(code); 199 192 return unit; 200 }201 202 private IEnumerable<string> GetNamespaces() {203 var strings = NamespaceDeclarationRegex.Matches(CommentRegex.Replace(code, string.Empty))204 .Cast<Match>()205 .Select(m => m.Value);206 foreach (var s in strings) {207 var match = NamespaceRegex.Match(s.Replace("using", string.Empty));208 yield return match.Value;209 }210 }211 212 private IEnumerable<string> GetCodeLines() {213 var lines = LineSplitter.Split(code);214 foreach (var line in lines) {215 string trimmedLine = line.Trim();216 if (!NamespaceDeclarationRegex.IsMatch(trimmedLine))217 yield return trimmedLine;218 }219 193 } 220 194 … … 230 204 } 231 205 } 232 233 private CodeTypeDeclaration CreateScriptClass() {234 var typeDecl = new CodeTypeDeclaration(CompiledTypeName) {235 IsClass = true,236 TypeAttributes = TypeAttributes.Public,237 };238 typeDecl.BaseTypes.Add(typeof(HLScriptGeneration));239 typeDecl.Members.Add(new CodeSnippetTypeMember(string.Join(Environment.NewLine, GetCodeLines())));240 return typeDecl;241 }242 206 #endregion 243 207 244 208 private Thread scriptThread; 245 209 public void Execute() { 246 if (compiled HLScript == null) return;247 var executeMethod = typeof( HLScriptGeneration).GetMethod(ExecuteMethodName, BindingFlags.NonPublic | BindingFlags.Instance);210 if (compiledScript == null) return; 211 var executeMethod = typeof(UserScriptBase).GetMethod(ExecuteMethodName, BindingFlags.NonPublic | BindingFlags.Instance); 248 212 if (executeMethod != null) { 249 213 scriptThread = new Thread(() => { 250 214 try { 251 215 OnScriptExecutionStarted(); 252 executeMethod.Invoke(compiled HLScript, new[] { VariableStore });216 executeMethod.Invoke(compiledScript, new[] { VariableStore }); 253 217 } finally { 254 218 OnScriptExecutionFinished(); … … 264 228 } 265 229 266 private void compiled HLScript_ConsoleOutputChanged(object sender, EventArgs<string> e) {230 private void compiledScript_ConsoleOutputChanged(object sender, EventArgs<string> e) { 267 231 OnConsoleOutputChanged(e.Value); 268 232 } -
trunk/sources/HeuristicLab.HLScript/3.3/UserScriptBase.cs
r10400 r10401 7 7 8 8 namespace HeuristicLab.HLScript { 9 public abstract class HLScriptGeneration{9 public abstract class UserScriptBase { 10 10 protected dynamic vars; 11 11 … … 15 15 } 16 16 17 protected HLScriptGeneration() {17 protected UserScriptBase() { 18 18 console = new EventWriter(this); 19 19 } … … 56 56 57 57 protected class EventWriter : TextWriter { 58 private readonly HLScriptGeneration hlsg;58 private readonly UserScriptBase usb; 59 59 60 public EventWriter( HLScriptGeneration hlsg) {61 this. hlsg = hlsg;60 public EventWriter(UserScriptBase usb) { 61 this.usb = usb; 62 62 } 63 63 … … 68 68 #region Write/WriteLine Overrides 69 69 #region Write 70 public override void Write(bool value) { hlsg.OnConsoleOutputChanged(value.ToString()); }71 public override void Write(char value) { hlsg.OnConsoleOutputChanged(value.ToString()); }72 public override void Write(char[] buffer) { hlsg.OnConsoleOutputChanged(new string(buffer)); }73 public override void Write(char[] buffer, int index, int count) { hlsg.OnConsoleOutputChanged(new string(buffer, index, count)); }74 public override void Write(decimal value) { hlsg.OnConsoleOutputChanged(value.ToString()); }75 public override void Write(double value) { hlsg.OnConsoleOutputChanged(value.ToString()); }76 public override void Write(float value) { hlsg.OnConsoleOutputChanged(value.ToString()); }77 public override void Write(int value) { hlsg.OnConsoleOutputChanged(value.ToString()); }78 public override void Write(long value) { hlsg.OnConsoleOutputChanged(value.ToString()); }79 public override void Write(object value) { hlsg.OnConsoleOutputChanged(value.ToString()); }80 public override void Write(string value) { hlsg.OnConsoleOutputChanged(value); }81 public override void Write(string format, object arg0) { hlsg.OnConsoleOutputChanged(string.Format(format, arg0)); }82 public override void Write(string format, object arg0, object arg1) { hlsg.OnConsoleOutputChanged(string.Format(format, arg0, arg0)); }83 public override void Write(string format, object arg0, object arg1, object arg2) { hlsg.OnConsoleOutputChanged(string.Format(format, arg0, arg1, arg2)); }84 public override void Write(string format, params object[] arg) { hlsg.OnConsoleOutputChanged(string.Format(format, arg)); }85 public override void Write(uint value) { hlsg.OnConsoleOutputChanged(value.ToString()); }86 public override void Write(ulong value) { hlsg.OnConsoleOutputChanged(value.ToString()); }70 public override void Write(bool value) { usb.OnConsoleOutputChanged(value.ToString()); } 71 public override void Write(char value) { usb.OnConsoleOutputChanged(value.ToString()); } 72 public override void Write(char[] buffer) { usb.OnConsoleOutputChanged(new string(buffer)); } 73 public override void Write(char[] buffer, int index, int count) { usb.OnConsoleOutputChanged(new string(buffer, index, count)); } 74 public override void Write(decimal value) { usb.OnConsoleOutputChanged(value.ToString()); } 75 public override void Write(double value) { usb.OnConsoleOutputChanged(value.ToString()); } 76 public override void Write(float value) { usb.OnConsoleOutputChanged(value.ToString()); } 77 public override void Write(int value) { usb.OnConsoleOutputChanged(value.ToString()); } 78 public override void Write(long value) { usb.OnConsoleOutputChanged(value.ToString()); } 79 public override void Write(object value) { usb.OnConsoleOutputChanged(value.ToString()); } 80 public override void Write(string value) { usb.OnConsoleOutputChanged(value); } 81 public override void Write(string format, object arg0) { usb.OnConsoleOutputChanged(string.Format(format, arg0)); } 82 public override void Write(string format, object arg0, object arg1) { usb.OnConsoleOutputChanged(string.Format(format, arg0, arg0)); } 83 public override void Write(string format, object arg0, object arg1, object arg2) { usb.OnConsoleOutputChanged(string.Format(format, arg0, arg1, arg2)); } 84 public override void Write(string format, params object[] arg) { usb.OnConsoleOutputChanged(string.Format(format, arg)); } 85 public override void Write(uint value) { usb.OnConsoleOutputChanged(value.ToString()); } 86 public override void Write(ulong value) { usb.OnConsoleOutputChanged(value.ToString()); } 87 87 #endregion 88 88 89 89 #region WriteLine 90 public override void WriteLine() { hlsg.OnConsoleOutputChanged(Environment.NewLine); }91 public override void WriteLine(bool value) { hlsg.OnConsoleOutputChanged(value + Environment.NewLine); }92 public override void WriteLine(char value) { hlsg.OnConsoleOutputChanged(value + Environment.NewLine); }93 public override void WriteLine(char[] buffer) { hlsg.OnConsoleOutputChanged(new string(buffer) + Environment.NewLine); }94 public override void WriteLine(char[] buffer, int index, int count) { hlsg.OnConsoleOutputChanged(new string(buffer, index, count) + Environment.NewLine); }95 public override void WriteLine(decimal value) { hlsg.OnConsoleOutputChanged(value + Environment.NewLine); }96 public override void WriteLine(double value) { hlsg.OnConsoleOutputChanged(value + Environment.NewLine); }97 public override void WriteLine(float value) { hlsg.OnConsoleOutputChanged(value + Environment.NewLine); }98 public override void WriteLine(int value) { hlsg.OnConsoleOutputChanged(value + Environment.NewLine); }99 public override void WriteLine(long value) { hlsg.OnConsoleOutputChanged(value + Environment.NewLine); }100 public override void WriteLine(object value) { hlsg.OnConsoleOutputChanged(value + Environment.NewLine); }101 public override void WriteLine(string value) { hlsg.OnConsoleOutputChanged(value + Environment.NewLine); }102 public override void WriteLine(string format, object arg0) { hlsg.OnConsoleOutputChanged(string.Format(format, arg0) + Environment.NewLine); }103 public override void WriteLine(string format, object arg0, object arg1) { hlsg.OnConsoleOutputChanged(string.Format(format, arg0, arg1) + Environment.NewLine); }104 public override void WriteLine(string format, object arg0, object arg1, object arg2) { hlsg.OnConsoleOutputChanged(string.Format(format, arg0, arg1, arg2) + Environment.NewLine); }105 public override void WriteLine(string format, params object[] arg) { hlsg.OnConsoleOutputChanged(string.Format(format, arg) + Environment.NewLine); }106 public override void WriteLine(uint value) { hlsg.OnConsoleOutputChanged(value + Environment.NewLine); }107 public override void WriteLine(ulong value) { hlsg.OnConsoleOutputChanged(value + Environment.NewLine); }90 public override void WriteLine() { usb.OnConsoleOutputChanged(Environment.NewLine); } 91 public override void WriteLine(bool value) { usb.OnConsoleOutputChanged(value + Environment.NewLine); } 92 public override void WriteLine(char value) { usb.OnConsoleOutputChanged(value + Environment.NewLine); } 93 public override void WriteLine(char[] buffer) { usb.OnConsoleOutputChanged(new string(buffer) + Environment.NewLine); } 94 public override void WriteLine(char[] buffer, int index, int count) { usb.OnConsoleOutputChanged(new string(buffer, index, count) + Environment.NewLine); } 95 public override void WriteLine(decimal value) { usb.OnConsoleOutputChanged(value + Environment.NewLine); } 96 public override void WriteLine(double value) { usb.OnConsoleOutputChanged(value + Environment.NewLine); } 97 public override void WriteLine(float value) { usb.OnConsoleOutputChanged(value + Environment.NewLine); } 98 public override void WriteLine(int value) { usb.OnConsoleOutputChanged(value + Environment.NewLine); } 99 public override void WriteLine(long value) { usb.OnConsoleOutputChanged(value + Environment.NewLine); } 100 public override void WriteLine(object value) { usb.OnConsoleOutputChanged(value + Environment.NewLine); } 101 public override void WriteLine(string value) { usb.OnConsoleOutputChanged(value + Environment.NewLine); } 102 public override void WriteLine(string format, object arg0) { usb.OnConsoleOutputChanged(string.Format(format, arg0) + Environment.NewLine); } 103 public override void WriteLine(string format, object arg0, object arg1) { usb.OnConsoleOutputChanged(string.Format(format, arg0, arg1) + Environment.NewLine); } 104 public override void WriteLine(string format, object arg0, object arg1, object arg2) { usb.OnConsoleOutputChanged(string.Format(format, arg0, arg1, arg2) + Environment.NewLine); } 105 public override void WriteLine(string format, params object[] arg) { usb.OnConsoleOutputChanged(string.Format(format, arg) + Environment.NewLine); } 106 public override void WriteLine(uint value) { usb.OnConsoleOutputChanged(value + Environment.NewLine); } 107 public override void WriteLine(ulong value) { usb.OnConsoleOutputChanged(value + Environment.NewLine); } 108 108 #endregion 109 109 #endregion
Note: See TracChangeset
for help on using the changeset viewer.