forked from SolarLiner/kOS-IDE
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GlobalOptions.cs
237 lines (195 loc) · 6.74 KB
/
GlobalOptions.cs
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
namespace kOS_IDE
{
public partial class GlobalOptions : Form
{
bool Is64OS;
[DllImport("kernel32.dll", SetLastError = true, CallingConvention = CallingConvention.Winapi)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool IsWow64Process([In] IntPtr hProcess, [Out] out bool wow64Process);
public GlobalOptions()
{
InitializeComponent();
Is64OS = (IntPtr.Size == 8) || InternalCheckIsWow64();
}
public static bool InternalCheckIsWow64()
{
if ((Environment.OSVersion.Version.Major == 5 && Environment.OSVersion.Version.Minor >= 1) ||
Environment.OSVersion.Version.Major >= 6)
{
using (Process p = Process.GetCurrentProcess())
{
bool retVal;
if (!IsWow64Process(p.Handle, out retVal))
{
return false;
}
return retVal;
}
}
else
{
return false;
}
}
public static string GetSteamInstall(bool Is64OS)
{
string pFiles = (Is64OS ? "Program Files (x86)" : "Program Files");
return @"C:\" + pFiles + @"\Steam\SteamApps\common\Kerbal Space Program";
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if (checkBox1.Checked)
{
KSPdir.Enabled = false;
FindKSP.Enabled = false;
KSPdir.Text = GetSteamInstall(Is64OS);
}
else
{
KSPdir.Enabled = true;
FindKSP.Enabled = true;
KSPdir.Text = "";
}
}
private void KSPdir_TextChanged(object sender, EventArgs e)
{
if (System.IO.Directory.Exists(KSPdir.Text)) AppOptions.KSPfolder = KSPdir.Text;
}
private void Connect_Click(object sender, EventArgs e)
{
Connect.Enabled = false;
Connect.Text = "Connecting ...";
// Do connection stuff here
if (true) // If success
{
AppOptions.Connection.Username = Username.Text;
AppOptions.Connection.Password = new Psswd(Password.Text);
Connect.Text = "Connected.";
}
}
private void ZoomBar_Scroll(object sender, EventArgs e)
{
float zm = (float)ZoomBar.Value;
if (zm < 0) zm = -(float)(1 / zm);
else zm /= 10.0f;
int add = (ZoomBar.Value < 0 ? 0 : 100);
ZoomLabel.Text = "Zoom: " + (100 * zm + add) + "%";
AppOptions.Zoom = ZoomBar.Value;
}
private void SmartIndent_CheckedChanged(object sender, EventArgs e)
{
AppOptions.SmartIndent = SmartIndent.Checked;
}
private void GlobalOptions_Load(object sender, EventArgs e)
{
AppOptions.Load("./config.cfg");
if (AppOptions.KSPfolder == GetSteamInstall(Is64OS))
checkBox1.Checked = true;
else if (!String.IsNullOrWhiteSpace(AppOptions.KSPfolder)) KSPdir.Text = AppOptions.KSPfolder;
}
private void GlobalOptions_FormClosing(object sender, FormClosingEventArgs e)
{
AppOptions.Save("./config.cfg");
}
}
public class Psswd
{
public string MD5hash;
public byte CharsCount;
public bool Ready;
public Psswd(string md5, byte Length)
{
MD5hash = md5;
CharsCount = Length;
Ready = true;
}
public Psswd(string input)
{
string result = "";
using (System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create())
{
byte[] data = md5.ComputeHash(Encoding.UTF8.GetBytes(input));
foreach (byte bit in data)
result += bit.ToString("X2");
}
MD5hash = result;
CharsCount = (byte)input.Length;
Ready = true;
}
public Psswd()
{
MD5hash = "";
CharsCount = 0;
Ready = false;
}
}
public static class AppOptions
{
public static string KSPfolder;
public static int Zoom;
public static bool SmartIndent;
public static class Connection
{
public static string Username;
public static bool Remember;
public static Psswd Password;
}
public static void Load(string filepath)
{
foreach (string s in System.IO.File.ReadAllLines(filepath))
{
string[] line = s.Split('=');
switch (line[0])
{
case "KSP":
KSPfolder = line[1];
continue;
case "User":
Connection.Username = line[1];
continue;
case "Password":
Connection.Remember = true;
string[] split = line[1].Split('|');
Psswd psswd = new Psswd(split[0], byte.Parse(split[1]));
continue;
case "SmartIndent":
SmartIndent = Boolean.Parse(line[1]);
continue;
case "Zoom":
int zm = int.Parse(line[1]);
Zoom = (zm > -5 && zm < 50 ? zm : 0);
continue;
default:
continue;
}
}
}
public static void Save(string filepath)
{
List<string> Lines = new List<string>();
Lines.Add("KSP=" + KSPfolder);
Lines.Add("User=" + Connection.Username);
Lines.Add("Password=" + Connection.Password.MD5hash + "|" + Connection.Password.CharsCount);
Lines.Add("SmartIndent=" + SmartIndent);
Lines.Add("Zoom=" + Zoom);
System.IO.File.WriteAllLines(filepath, Lines.ToArray<string>());
}
public static void Default()
{
KSPfolder = null;
SmartIndent = true;
Zoom = 0;
Connection.Password = new Psswd();
}
}
}