[10731] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[16140] | 3 | * Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[10731] | 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 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Linq;
|
---|
| 24 | using System.Reflection;
|
---|
| 25 | using HeuristicLab.Common;
|
---|
| 26 | using HeuristicLab.Core;
|
---|
| 27 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 28 |
|
---|
| 29 | namespace HeuristicLab.Scripting {
|
---|
| 30 | [Item("C# Script", "An empty C# script.")]
|
---|
[12504] | 31 | [Creatable(CreatableAttribute.Categories.Scripts, Priority = 100)]
|
---|
[10731] | 32 | [StorableClass]
|
---|
[13080] | 33 | public class CSharpScript : ExecutableScript, IStorableContent {
|
---|
[10731] | 34 | #region Fields & Properties
|
---|
[10857] | 35 | private CSharpScriptBase compiledScript;
|
---|
[15120] | 36 | public dynamic Instance {
|
---|
| 37 | get { return compiledScript; }
|
---|
| 38 | }
|
---|
[10731] | 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 |
|
---|
[13080] | 49 | #region Construction & Cloning
|
---|
[10731] | 50 | [StorableConstructor]
|
---|
| 51 | protected CSharpScript(bool deserializing) : base(deserializing) { }
|
---|
| 52 | protected CSharpScript(CSharpScript original, Cloner cloner)
|
---|
| 53 | : base(original, cloner) {
|
---|
[11058] | 54 | variableStore = cloner.Clone(original.variableStore);
|
---|
[10731] | 55 | }
|
---|
[13080] | 56 | public CSharpScript()
|
---|
[13218] | 57 | : base(ScriptTemplates.CSharpScriptTemplate) {
|
---|
[10731] | 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() {
|
---|
[10857] | 71 | if (compiledScript != null)
|
---|
| 72 | compiledScript.ConsoleOutputChanged += CompiledScriptOnConsoleOutputChanged;
|
---|
[10731] | 73 | }
|
---|
| 74 |
|
---|
| 75 | protected virtual void DeregisterScriptEvents() {
|
---|
[10857] | 76 | if (compiledScript != null)
|
---|
| 77 | compiledScript.ConsoleOutputChanged -= CompiledScriptOnConsoleOutputChanged;
|
---|
[10731] | 78 | }
|
---|
| 79 |
|
---|
| 80 | #region Compilation
|
---|
| 81 | public override Assembly Compile() {
|
---|
[10857] | 82 | DeregisterScriptEvents();
|
---|
| 83 | compiledScript = null;
|
---|
[10731] | 84 | var assembly = base.Compile();
|
---|
| 85 | var types = assembly.GetTypes();
|
---|
[10857] | 86 | compiledScript = (CSharpScriptBase)Activator.CreateInstance(types.Single(x => typeof(CSharpScriptBase).IsAssignableFrom(x)));
|
---|
[10731] | 87 | RegisterScriptEvents();
|
---|
| 88 | return assembly;
|
---|
| 89 | }
|
---|
| 90 | #endregion
|
---|
| 91 |
|
---|
[13080] | 92 | protected override void ExecuteCode() {
|
---|
[10857] | 93 | if (compiledScript == null) return;
|
---|
[10731] | 94 |
|
---|
[13080] | 95 | compiledScript.Execute(VariableStore);
|
---|
[10731] | 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 | }
|
---|