Skip to content

Commit e7a45dc

Browse files
committed
Get Path Inputs from console instead of manual file writing.
1 parent 458f7d2 commit e7a45dc

File tree

1 file changed

+103
-2
lines changed

1 file changed

+103
-2
lines changed

ChessEngineAPI/Program.cs

Lines changed: 103 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,94 @@
1-
using System.Diagnostics;
1+
using System.ComponentModel;
2+
using System.Diagnostics;
23
using ChessEngineAPI.App;
34

45
AppDomain.CurrentDomain.ProcessExit += CloseHandler;
56

67
Console.WriteLine("Initializing application...\n");
78

9+
string PythonPath = "";
10+
11+
try
12+
{
13+
FileStream fs = File.OpenRead("PythonPath");
14+
StreamReader sreader = new StreamReader(fs);
15+
PythonPath = Convert.ToString(sreader.ReadToEnd());
16+
sreader.Close();
17+
fs.Close();
18+
}
19+
catch
20+
{
21+
Console.WriteLine("\nPython Path is invalid. Please enter new Path: ");
22+
PythonPath = Console.ReadLine();
23+
FileStream _fs = File.Create("PythonPath");
24+
StreamWriter swrite = new StreamWriter(_fs);
25+
swrite.WriteLine(PythonPath);
26+
swrite.Close();
27+
_fs.Close();
28+
}
29+
30+
Process process;
31+
32+
try
33+
{
34+
process = Process.Start(PythonPath);
35+
process.Kill(true);
36+
}
37+
catch
38+
{
39+
Console.WriteLine("\nPython Path is invalid. Please enter new Path: ");
40+
PythonPath = Console.ReadLine();
41+
FileStream _fs = File.Create("PythonPath");
42+
StreamWriter swrite = new StreamWriter(_fs);
43+
swrite.WriteLine(PythonPath);
44+
swrite.Close();
45+
_fs.Close();
46+
}
47+
48+
string StockFishPath = "";
49+
50+
try
51+
{
52+
FileStream fs2 = File.OpenRead("StockFishPath");
53+
StreamReader sreader2 = new StreamReader(fs2);
54+
StockFishPath = Convert.ToString(sreader2.ReadToEnd());
55+
sreader2.Close();
56+
fs2.Close();
57+
58+
}
59+
catch
60+
{
61+
Console.WriteLine("\nStockFish Path is invalid. Please enter new Path: ");
62+
StockFishPath = Console.ReadLine();
63+
FileStream _fs = File.Create("StockFishPath");
64+
StreamWriter swrite = new StreamWriter(_fs);
65+
swrite.WriteLine(StockFishPath);
66+
swrite.Close();
67+
_fs.Close();
68+
}
69+
70+
Process process2;
71+
72+
try
73+
{
74+
process2 = Process.Start(PythonPath);
75+
process2.Kill(true);
76+
}
77+
catch
78+
{
79+
Console.WriteLine("\nStockFish Path is invalid. Please enter new Path: ");
80+
StockFishPath = Console.ReadLine();
81+
FileStream _fs = File.Create("StockFishPath");
82+
StreamWriter swrite = new StreamWriter(_fs);
83+
swrite.WriteLine(StockFishPath);
84+
swrite.Close();
85+
_fs.Close();
86+
}
87+
88+
889
mainApp = new App();
990

10-
Console.WriteLine($"\nFinished initialization of application at port :{GlobalVars.Port}.");
91+
Console.WriteLine($"\n\nFinished initialization of application at port :{GlobalVars.Port}.");
1192

1293
while (ApplicationIsRunning)
1394
{
@@ -62,6 +143,26 @@ private static void PyProcessOutputHandler(object sender, DataReceivedEventArgs
62143
Console.WriteLine(sender.ToString() + " - " + args.Data.ToString());
63144
}
64145

146+
public static bool ExistsOnPath(string exeName)
147+
{
148+
try
149+
{
150+
using (Process p = new Process())
151+
{
152+
p.StartInfo.UseShellExecute = false;
153+
p.StartInfo.FileName = "where";
154+
p.StartInfo.Arguments = exeName;
155+
p.Start();
156+
p.WaitForExit();
157+
return p.ExitCode == 0;
158+
}
159+
}
160+
catch (Win32Exception)
161+
{
162+
throw new Exception("'where' command is not on path");
163+
}
164+
}
165+
65166
public static string OpenPY()
66167
{
67168
//Open python script that runs chess engine.

0 commit comments

Comments
 (0)