-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathProgram.cs
More file actions
147 lines (125 loc) · 4.88 KB
/
Program.cs
File metadata and controls
147 lines (125 loc) · 4.88 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using IronPython.Hosting;
using IronPython.Runtime;
using Microsoft.Scripting;
using Microsoft.Scripting.Hosting;
namespace MeteoInfo
{
static class Program
{
// /// <summary>
// /// The main entry point for the application.
// /// </summary>
// [STAThread]
// static void Main()
// {
// //AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(
// // (sender, e) =>
// // {
// // if (e.IsTerminating)
// // {
// // object o = e.ExceptionObject;
// // Console.WriteLine(o.ToString()); // use EventLog instead
// // }
// // }
// //);
// Application.EnableVisualStyles();
// Application.SetCompatibleTextRenderingDefault(false);
//#if DEBUG
// //don't catch unhandled exceptions if debuggin
// Application.Run(new frmMain());
//#else
// //enable unhandled exceptions trapping for release version
// try
// {
// Application.Run(new frmMain());
// }
// catch(Exception ex)
// {
// Console.WriteLine(ex.ToString());
// }
//#endif
// }
[DllImport("kernel32.dll")]
private static extern bool AllocConsole();
[DllImport("kernel32.dll")]
private static extern bool AttachConsole(int pid);
[DllImport("kernel32.dll", SetLastError = true)]
private static extern bool FreeConsole();
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
if (args.Length == 1)
{
if (args[0].Substring(args[0].Length - 3) == ".py")
{
// get console output
if (!AttachConsole(-1))
AllocConsole();
// Run script
Console.WriteLine("MeteoInfo Scripting");
frmMain aFrm = new frmMain(true);
string aFile = args[0];
ScriptEngine scriptEngine = Python.CreateEngine();
ScriptScope pyScope = scriptEngine.CreateScope();
//Set path
string path = Assembly.GetExecutingAssembly().Location;
string rootDir = Directory.GetParent(path).FullName;
List<string> paths = new List<string>();
paths.Add(rootDir);
paths.Add(Path.Combine(rootDir, "Lib"));
scriptEngine.SetSearchPaths(paths.ToArray());
pyScope.SetVariable("mipy", aFrm);
//Run python script
try
{
//ScriptSource sourceCode = scriptEngine.CreateScriptSourceFromString(text, SourceCodeKind.Statements);
ScriptSource sourceCode = scriptEngine.CreateScriptSourceFromFile(aFile);
sourceCode.Execute(pyScope);
//CompiledCode compiled = sourceCode.Compile();
//compiled.Execute(pyScope);
//sourceCode.ExecuteProgram();
}
catch (Exception e)
{
ExceptionOperations eo = scriptEngine.GetService<ExceptionOperations>();
Console.Write(eo.FormatException(e));
}
//aFrm.RunScript(aFile);
//aFrm.Dispose();
//aFrm.Show();
//Application.Run(aFrm);
//aFrm.Close();
FreeConsole(); // detach console
// get command prompt back
System.Windows.Forms.SendKeys.SendWait("{ENTER}");
}
else if (args[0].Substring(args[0].Length - 4) == ".mip")
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
frmMain myApp = new frmMain();
string pFile = args[0];
pFile = Path.Combine(System.Environment.CurrentDirectory, pFile);
myApp.OpenProjectFile(pFile);
Application.Run(myApp);
}
}
else
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new frmMain());
}
}
}
}