Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 14185 was 14185, checked in by swagner, 8 years ago

#2526: Updated year of copyrights in license headers

File size: 3.5 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 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 Fields & Properties
35    private CSharpScriptBase compiledScript;
36
37    public string Filename { get; set; }
38
39    [Storable]
40    private VariableStore variableStore;
41    public VariableStore VariableStore {
42      get { return variableStore; }
43    }
44    #endregion
45
46    #region Construction & Cloning
47    [StorableConstructor]
48    protected CSharpScript(bool deserializing) : base(deserializing) { }
49    protected CSharpScript(CSharpScript original, Cloner cloner)
50      : base(original, cloner) {
51      variableStore = cloner.Clone(original.variableStore);
52    }
53    public CSharpScript()
54      : base(ScriptTemplates.CSharpScriptTemplate) {
55      variableStore = new VariableStore();
56    }
57    public CSharpScript(string code)
58      : base(code) {
59      variableStore = new VariableStore();
60    }
61
62    public override IDeepCloneable Clone(Cloner cloner) {
63      return new CSharpScript(this, cloner);
64    }
65    #endregion
66
67    protected virtual void RegisterScriptEvents() {
68      if (compiledScript != null)
69        compiledScript.ConsoleOutputChanged += CompiledScriptOnConsoleOutputChanged;
70    }
71
72    protected virtual void DeregisterScriptEvents() {
73      if (compiledScript != null)
74        compiledScript.ConsoleOutputChanged -= CompiledScriptOnConsoleOutputChanged;
75    }
76
77    #region Compilation
78    public override Assembly Compile() {
79      DeregisterScriptEvents();
80      compiledScript = null;
81      var assembly = base.Compile();
82      var types = assembly.GetTypes();
83      compiledScript = (CSharpScriptBase)Activator.CreateInstance(types.Single(x => typeof(CSharpScriptBase).IsAssignableFrom(x)));
84      RegisterScriptEvents();
85      return assembly;
86    }
87    #endregion
88
89    protected override void ExecuteCode() {
90      if (compiledScript == null) return;
91
92      compiledScript.Execute(VariableStore);
93    }
94
95    protected virtual void CompiledScriptOnConsoleOutputChanged(object sender, EventArgs<string> e) {
96      OnConsoleOutputChanged(e.Value);
97    }
98
99    public event EventHandler<EventArgs<string>> ConsoleOutputChanged;
100    protected virtual void OnConsoleOutputChanged(string args) {
101      var handler = ConsoleOutputChanged;
102      if (handler != null) handler(this, new EventArgs<string>(args));
103    }
104  }
105}
Note: See TracBrowser for help on using the repository browser.