Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
01/27/14 14:13:02 (11 years ago)
Author:
jkarder
Message:

#2136:

  • renamed some HLScript files
  • fixed a bug where variables could be renamed while a script was running
  • the complete source code is now always visible
  • removed "show generated code" feature
File:
1 moved

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.HLScript/3.3/Script.cs

    r10400 r10401  
    1717
    1818namespace HeuristicLab.HLScript {
    19   [Item("HL Script", "A HeuristicLab script.")]
     19  [Item("Script", "A HeuristicLab script.")]
    2020  [Creatable("Scripts")]
    2121  [StorableClass]
    22   public sealed class HLScript : NamedItem, IStorableContent {
     22  public sealed class Script : NamedItem, IStorableContent {
    2323    #region Constants
    24     private const string ScriptNamespaceName = "HeuristicLab.HLScript";
    2524    private const string ExecuteMethodName = "Execute";
    2625    private const string CodeTemplate =
     
    2928using System;
    3029
    31 public override void Main() {
    32   // type your code here
    33 }
    34 
    35 // further classes and methods";
     30namespace 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}";
    3640    #endregion
    3741
    3842    #region Fields & Properties
    39     private HLScriptGeneration compiledHLScript;
     43    private UserScriptBase compiledScript;
    4044
    4145    public string Filename { get; set; }
     
    5862        if (value == code) return;
    5963        code = value;
    60         compiledHLScript = null;
     64        compiledScript = null;
    6165        OnCodeChanged();
    6266      }
     
    8084    #region Construction & Initialization
    8185    [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)
    8488      : base(original, cloner) {
    8589      code = original.code;
     
    9094    }
    9195
    92     public HLScript()
    93       : base("HL Script", "A HeuristicLab script.") {
     96    public Script()
     97      : base("Script", "A HeuristicLab script.") {
    9498      code = CodeTemplate;
    9599      variableStore = new VariableStore();
     
    97101
    98102    public override IDeepCloneable Clone(Cloner cloner) {
    99       return new HLScript(this, cloner);
     103      return new Script(this, cloner);
    100104    }
    101105    #endregion
    102106
    103107    private void RegisterScriptEvents() {
    104       if (compiledHLScript == null) return;
    105       compiledHLScript.ConsoleOutputChanged += compiledHLScript_ConsoleOutputChanged;
     108      if (compiledScript == null) return;
     109      compiledScript.ConsoleOutputChanged += compiledScript_ConsoleOutputChanged;
    106110    }
    107111
    108112    private void DeregisterScriptEvents() {
    109       if (compiledHLScript == null) return;
    110       compiledHLScript.ConsoleOutputChanged -= compiledHLScript_ConsoleOutputChanged;
     113      if (compiledScript == null) return;
     114      compiledScript.ConsoleOutputChanged -= compiledScript_ConsoleOutputChanged;
    111115    }
    112116
     
    143147    public void Compile() {
    144148      var results = DoCompile();
    145       compiledHLScript = null;
     149      compiledScript = null;
    146150      CompileErrors = results.Errors;
    147151      if (results.Errors.HasErrors) {
     
    160164        var types = assembly.GetTypes();
    161165        DeregisterScriptEvents();
    162         compiledHLScript = (HLScriptGeneration)Activator.CreateInstance(types[0]);
     166        compiledScript = (UserScriptBase)Activator.CreateInstance(types[0]);
    163167        RegisterScriptEvents();
    164168      }
     
    181185    }
    182186
    183     private readonly Regex LineSplitter = new Regex(@"\r\n|\r|\n");
    184187    private readonly Regex SafeTypeNameCharRegex = new Regex("[_a-zA-Z0-9]+");
    185188    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(@"((/\*)[^/]+(\*/))|(//.*)");
    189189
    190190    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);
    199192      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       }
    219193    }
    220194
     
    230204      }
    231205    }
    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     }
    242206    #endregion
    243207
    244208    private Thread scriptThread;
    245209    public void Execute() {
    246       if (compiledHLScript == 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);
    248212      if (executeMethod != null) {
    249213        scriptThread = new Thread(() => {
    250214          try {
    251215            OnScriptExecutionStarted();
    252             executeMethod.Invoke(compiledHLScript, new[] { VariableStore });
     216            executeMethod.Invoke(compiledScript, new[] { VariableStore });
    253217          } finally {
    254218            OnScriptExecutionFinished();
     
    264228    }
    265229
    266     private void compiledHLScript_ConsoleOutputChanged(object sender, EventArgs<string> e) {
     230    private void compiledScript_ConsoleOutputChanged(object sender, EventArgs<string> e) {
    267231      OnConsoleOutputChanged(e.Value);
    268232    }
Note: See TracChangeset for help on using the changeset viewer.