Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Scripting/3.3/Scripts/CSharp/CSharpScript.cs @ 13080

Last change on this file since 13080 was 13080, checked in by jkarder, 7 years ago

#2298: added ExecutableScript and ExecutableScriptView

File size: 3.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Linq;
24using System.Reflection;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28
29namespace HeuristicLab.Scripting {
30  [Item("C# Script", "An empty C# script.")]
31  [Creatable(CreatableAttribute.Categories.Scripts, Priority = 100)]
32  [StorableClass]
33  public class CSharpScript : ExecutableScript, IStorableContent {
34    #region Constants
35    protected override string CodeTemplate { get { return ScriptTemplates.CSharpScriptTemplate; } }
36    #endregion
37
38    #region Fields & Properties
39    private CSharpScriptBase compiledScript;
40
41    public string Filename { get; set; }
42
43    [Storable]
44    private VariableStore variableStore;
45    public VariableStore VariableStore {
46      get { return variableStore; }
47    }
48    #endregion
49
50    #region Construction & Cloning
51    [StorableConstructor]
52    protected CSharpScript(bool deserializing) : base(deserializing) { }
53    protected CSharpScript(CSharpScript original, Cloner cloner)
54      : base(original, cloner) {
55      variableStore = cloner.Clone(original.variableStore);
56    }
57    public CSharpScript()
58      : base() {
59      variableStore = new VariableStore();
60    }
61    public CSharpScript(string code)
62      : base(code) {
63      variableStore = new VariableStore();
64    }
65
66    public override IDeepCloneable Clone(Cloner cloner) {
67      return new CSharpScript(this, cloner);
68    }
69    #endregion
70
71    protected virtual void RegisterScriptEvents() {
72      if (compiledScript != null)
73        compiledScript.ConsoleOutputChanged += CompiledScriptOnConsoleOutputChanged;
74    }
75
76    protected virtual void DeregisterScriptEvents() {
77      if (compiledScript != null)
78        compiledScript.ConsoleOutputChanged -= CompiledScriptOnConsoleOutputChanged;
79    }
80
81    #region Compilation
82    public override Assembly Compile() {
83      DeregisterScriptEvents();
84      compiledScript = null;
85      var assembly = base.Compile();
86      var types = assembly.GetTypes();
87      compiledScript = (CSharpScriptBase)Activator.CreateInstance(types.Single(x => typeof(CSharpScriptBase).IsAssignableFrom(x)));
88      RegisterScriptEvents();
89      return assembly;
90    }
91    #endregion
92
93    protected override void ExecuteCode() {
94      if (compiledScript == null) return;
95
96      compiledScript.Execute(VariableStore);
97    }
98
99    protected virtual void CompiledScriptOnConsoleOutputChanged(object sender, EventArgs<string> e) {
100      OnConsoleOutputChanged(e.Value);
101    }
102
103    public event EventHandler<EventArgs<string>> ConsoleOutputChanged;
104    protected virtual void OnConsoleOutputChanged(string args) {
105      var handler = ConsoleOutputChanged;
106      if (handler != null) handler(this, new EventArgs<string>(args));
107    }
108  }
109}
Note: See TracBrowser for help on using the repository browser.