Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2817-BinPackingSpeedup/HeuristicLab.Scripting/3.3/Scripts/CSharp/CSharpScript.cs

Last change on this file was 16140, checked in by abeham, 6 years ago

#2817: updated to trunk r15680

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