aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/TEMP/Tango.Scripting/Tango.Scripting.IDE/Properties/AssemblyInfo.cs
blob: 3ac597e083e4fe3012b2cbf180144d91d05c009e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
using System.Reflection;
using System.Resources;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Windows;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("Tango.Scripting.IDE")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("Tango.Scripting.IDE")]
[assembly: AssemblyCopyright("Copyright ©  2019")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible 
// to COM components.  If you need to access a type in this assembly from 
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

//In order to begin building localizable applications, set
//<UICulture>CultureYouAreCodingWith</UICulture> in your .csproj file
//inside a <PropertyGroup>.  For example, if you are using US english
//in your source files, set the <UICulture> to en-US.  Then uncomment
//the NeutralResourceLanguage attribute below.  Update the "en-US" in
//the line below to match the UICulture setting in the project file.

//[assembly: NeutralResourcesLanguage("en-US", UltimateResourceFallbackLocation.Satellite)]


[assembly:ThemeInfo(
    ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located
                             //(used if a resource is not found in the page,
                             // or application resource dictionaries)
    ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located
                                      //(used if a resource is not found in the page,
                                      // app, or any theme specific resource dictionaries)
)]


// Version information for an assembly consists of the following four values:
//
//      Major Version
//      Minor Version
//      Build Number
//      Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
.highlight .py { color: #336699; font-weight: bold } /* Name.Property */ .highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */ .highlight .nv { color: #336699 } /* Name.Variable */ .highlight .ow { color: #008800 } /* Operator.Word */ .highlight .w { color: #bbbbbb } /* Text.Whitespace */ .highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */ .highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */ .highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */ .highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */ .highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */ .highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */ .highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */ .highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */ .highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */ .highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */ .highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */ .highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */ .highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */ .highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */ .highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */ .highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */ .highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */ .highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */ .highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */ .highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */ .highlight .vc { color: #336699 } /* Name.Variable.Class */ .highlight .vg { color: #dd7700 } /* Name.Variable.Global */ .highlight .vi { color: #3333bb } /* Name.Variable.Instance */ .highlight .vm { color: #336699 } /* Name.Variable.Magic */ .highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Threading;
using Tango.Core.Commands;
using Tango.Core.Helpers;
using Tango.MachineStudio.Common.Modules;
using Tango.MachineStudio.Common.Notifications;
using Tango.Scripting;
using Tango.SharedUI;

namespace Tango.MachineStudio.UI.Console
{
    public class ConsoleWindowVM : ViewModel
    {
        private IStudioModuleLoader _moduleLoader;
        private INotificationProvider _notificatrion;
        private TextBox _txtLog;
        private String _currentFile;
        private ScriptEngine _engine;


        /// <summary>
        /// Gets or sets the additional highlight C# types.
        /// </summary>
        public ObservableCollection<KeyValuePair<String, Type>> HighlightTypes { get; set; }

        internal void SetLogTextBox(TextBox txtLog)
        {
            _txtLog = txtLog;
        }

        /// <summary>
        /// Gets or sets the intellisense types.
        /// </summary>
        public ObservableCollection<KeyValuePair<String, Type>> IntellisenseTypes { get; set; }

        private String _code;
        /// <summary>
        /// Gets or sets the code.
        /// </summary>
        public String Code
        {
            get { return _code; }
            set { _code = value; RaisePropertyChangedAuto(); }
        }

        private bool _isRunning;
        /// <summary>
        /// Gets or sets a value indicating whether a stub is currently running.
        /// </summary>
        public bool IsRunning
        {
            get { return _isRunning; }
            set { _isRunning = value; RaisePropertyChanged(nameof(IsRunning)); InvalidateRelayCommands(); }
        }

        /// <summary>
        /// Gets or sets the run command.
        /// </summary>
        public RelayCommand RunCommand { get; set; }

        /// <summary>
        /// Gets or sets the stop command.
        /// </summary>
        public RelayCommand StopCommand { get; set; }

        /// <summary>
        /// Gets or sets the clear command.
        /// </summary>
        public RelayCommand ClearCommand { get; set; }

        /// <summary>
        /// Gets or sets the new command.
        /// </summary>
        public RelayCommand NewCommand { get; set; }

        /// <summary>
        /// Gets or sets the open command.
        /// </summary>
        public RelayCommand OpenCommand { get; set; }

        /// <summary>
        /// Gets or sets the save command.
        /// </summary>
        public RelayCommand SaveCommand { get; set; }

        /// <summary>
        /// Gets or sets the save as command.
        /// </summary>
        public RelayCommand SaveAsCommand { get; set; }

        public ConsoleWindowVM(IStudioModuleLoader moduleLoader, INotificationProvider notification)
        {
            _moduleLoader = moduleLoader;
            _notificatrion = notification;

            RunCommand = new RelayCommand(Run);
            StopCommand = new RelayCommand(Stop);

            HighlightTypes = new ObservableCollection<KeyValuePair<string, Type>>();
            IntellisenseTypes = new ObservableCollection<KeyValuePair<string, Type>>();

            IntellisenseTypes.Add(new KeyValuePair<string, Type>("manager", typeof(ConsoleManager)));

            foreach (var moduleType in moduleLoader.UserModules.SelectMany(x => x.MainViewType.Assembly.GetTypes()))
            {
                if (!moduleType.FullName.Contains("<") && !moduleType.FullName.Contains(">"))
                {
                    HighlightTypes.Add(new KeyValuePair<string, Type>(moduleType.FullName, moduleType));
                }
            }

            foreach (var type in this.GetType().Assembly.GetTypes())
            {
                if (!type.FullName.Contains("<") && !type.FullName.Contains(">"))
                {
                    HighlightTypes.Add(new KeyValuePair<string, Type>(type.Name, type));
                }
            }

            foreach (var type in typeof(INotificationProvider).Assembly.GetTypes())
            {
                if (!type.FullName.Contains("<") && !type.FullName.Contains(">"))
                {
                    HighlightTypes.Add(new KeyValuePair<string, Type>(type.Name, type));
                }
            }

            HighlightTypes.Add(new KeyValuePair<string, Type>("Thread", typeof(Thread)));
            HighlightTypes.Add(new KeyValuePair<string, Type>("DateTime", typeof(DateTime)));
            HighlightTypes.Add(new KeyValuePair<string, Type>("TimeSpan", typeof(TimeSpan)));
            HighlightTypes.Add(new KeyValuePair<string, Type>("Dispatcher", typeof(Dispatcher)));
            HighlightTypes.Add(new KeyValuePair<string, Type>("Task", typeof(Task)));
            HighlightTypes.Add(new KeyValuePair<string, Type>("List", typeof(IList<Object>)));
            HighlightTypes.Add(new KeyValuePair<string, Type>("int", typeof(Int32)));
            HighlightTypes.Add(new KeyValuePair<string, Type>("double", typeof(Double)));
            HighlightTypes.Add(new KeyValuePair<string, Type>("String", typeof(String)));
            HighlightTypes.Add(new KeyValuePair<string, Type>("string", typeof(String)));

            foreach (var item in HighlightTypes)
            {
                IntellisenseTypes.Add(item);
            }

            Code = EmbeddedResourceHelper.GetEmbeddedResourceText("Tango.MachineStudio.UI.Console.CodeTemplate.cs");

            NewCommand = new RelayCommand(CreateNew);
            OpenCommand = new RelayCommand(OpenFile);
            SaveCommand = new RelayCommand(SaveFile);
            SaveAsCommand = new RelayCommand(SaveAsFile);
            ClearCommand = new RelayCommand(ClearLog);
        }

        private void Stop()
        {
            if (_engine != null)
            {
                _engine.Stop();
            }
        }

        private async void Run()
        {
            _engine = new ScriptEngine(new ConsoleOnExecuteParameters(new ConsoleManager(WriteLine)));
            _engine.ReferencedAssemblies.Add(this.GetType());
            _engine.ReferencedAssemblies.Add(typeof(INotificationProvider));

            foreach (var module in _moduleLoader.AllModules)
            {
                _engine.ReferencedAssemblies.Add(module.GetType());
            }

            await _engine.Run(Code, null);
        }

        private void WriteLine(String text)
        {
            InvokeUI(() =>
            {
                _txtLog.AppendText(text);
            });
        }

        /// <summary>
        /// Clears the log.
        /// </summary>
        private void ClearLog()
        {
            _txtLog.Clear();
        }

        /// <summary>
        /// Saves the selected script file.
        /// </summary>
        private void SaveFile()
        {
            if (_currentFile == null)
            {
                SaveAsFile();
            }
            else
            {
                File.WriteAllText(_currentFile, Code);
            }
        }

        /// <summary>
        /// Saves the selected script file.
        /// </summary>
        private void SaveAsFile()
        {
            SaveFileDialog dlg = new SaveFileDialog();
            dlg.Filter = "C# Script Files|*.cs";
            dlg.DefaultExt = ".cs";
            if (dlg.ShowDialog().Value)
            {
                File.WriteAllText(dlg.FileName, Code);
                _currentFile = dlg.FileName;
            }
        }

        /// <summary>
        /// Opens a script from HD.
        /// </summary>
        private void OpenFile()
        {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.Filter = "C# Script Files|*.cs";
            if (dlg.ShowDialog().Value)
            {
                OpenFile(dlg.FileName);
            }
        }

        /// <summary>
        /// Opens the file.
        /// </summary>
        /// <param name="file">The file.</param>
        private void OpenFile(String file)
        {
            Code = File.ReadAllText(file);
            _currentFile = file;
        }

        private void CreateNew()
        {
            _txtLog.Clear();
            _currentFile = null;
            Code = String.Empty;
        }
    }
}