-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
291 lines (282 loc) · 13 KB
/
Program.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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.RegularExpressions;
using static DeSmuMAR.Native;
namespace DeSmuMAR
{
class Program
{
private static readonly string Home = Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location);
private static readonly string DesmumarSettingsFile = Path.Combine(Home, "DeSmuMAR.ini");
private static readonly string DesmumeLocation = Path.Combine(Home, "DeSmuME.exe");
private static readonly string DesmumeSettingsFile = Path.Combine(Home, "desmume.ini");
private static string[] Settings => File.Exists(DesmumarSettingsFile) ? File.ReadAllLines(DesmumarSettingsFile, Encoding.UTF8) : null;
static void Main(string[] args)
{
// Ensure DeSmuME is available
if (!File.Exists(DesmumeLocation))
{
Log("The \"DeSmuME.exe\" executable was not found. Please move DeSmuMAR's files next to DeSmuME's files.");
Log("DeSmuME must be v0.9.12 or newer. Make sure the file is named exactly DeSmuME.exe and not DeSmuME_0.9.11_x64.exe or such.");
_ = Console.ReadKey().Key;
return;
}
// Get active screen information
const int ENUM_CURRENT_SETTINGS = -1;
DEVMODE devMode = default;
devMode.dmSize = (short)Marshal.SizeOf(devMode);
EnumDisplaySettings(null, ENUM_CURRENT_SETTINGS, ref devMode); // null == current active monitor/screen
// Ensure the aspect ratio set won't escape the size constraints of the display
(int Width, int Height, int LCDLayout) = EnsureSizeConstraint(devMode.dmPelsWidth, devMode.dmPelsHeight);
// Force some DeSmuME settings that are necessary for custom aspect ratio use
if (!File.Exists(DesmumeSettingsFile))
{
Log("No DeSmuME configuration file available");
Log("Opening DeSmuME just for a second for it to create a new configuration file");
RunDesmume().Kill();
}
UpdateINI(DesmumeSettingsFile, new string[][] {
new string[] {"3D", "Renderer", "2"},
new string[] {"Console", "Show", "0"},
new string[] {"Display", "Show Toolbar", "0"},
new string[] {"Video", "Window Force Ratio", "0"},
new string[] {"Video", "LCDsLayout", LCDLayout.ToString()},
new string[] {"Video", "Window Lockdown", "0"},
new string[] {"Video", "Display Method", "1"},
new string[] {"Video", "Window width", "1"},
new string[] {"Video", "Window width", Width.ToString()},
new string[] {"Video", "Window height", Height.ToString()}
});
// Run DeSmuME with the first argument provided to DeSmuMAR (if available, this should be the ROM path)
RunDesmume(args.Length > 0 ? "\"" + args[0] + "\"" : null);
}
private static Process RunDesmume(string Argument = null)
{
Process p = new Process()
{
StartInfo = new ProcessStartInfo()
{
FileName = DesmumeLocation,
Arguments = Argument
}
};
p.Start();
p.WaitForInputIdle();
return p;
}
private static (int, int, int) EnsureSizeConstraint(int maxWidth, int maxHeight)
{
SettingsFlags flags = SettingsFlags.None;
bool WidthSafe = false;
bool HeightSafe = false;
int Width = -1;
int Height = -1;
int LCDLayout = -1;
while (!WidthSafe || !HeightSafe)
{
int[] AspectRatio = GetSetting<string>("AspectRatio", flags).Split(':').Select(int.Parse).ToArray();
LCDLayout = GetSetting<bool>("Screens", flags | SettingsFlags.BoolOnly) ? 1 : 2;
Height = GetSetting<int>("Resolution", flags | SettingsFlags.NumericOnly);
Width = Height / AspectRatio[1] * AspectRatio[0] * (LCDLayout == 1 ? 2 : 1);
WidthSafe = Width <= maxWidth;
HeightSafe = Height <= maxHeight;
if (!WidthSafe || !HeightSafe)
{
Log(
"Oh no! The Aspect Ratio and Resolution you chose would result in a DeSmuME window that is bigger than your display.\n" +
"To reduce the size, try reduce the resolution or use a smaller aspect ratio\n" +
((maxWidth / maxHeight) == (Width / Height) ? "Since you are trying to use the same aspect ratio for DeSmuME as your Screen, i'm assuming you want to go fullscreen, if this is the case, you should lower the resolution or set the screens amount to \"One LCD\" in DeSmuME as that will help lower the window size.\n" : string.Empty) +
"If your curious, you tried to resize DeSmuME to " + Width + "x" + Height + "\n" +
"That size is: " +
(!WidthSafe ? (Width - maxWidth).ToString() + " pixels wider" : string.Empty) +
(!WidthSafe && !HeightSafe ? " & " : string.Empty) +
(!HeightSafe ? (Height - maxHeight).ToString() + " pixels taller" : string.Empty) +
" than your display :O"
, LogTypes.Warning);
Log("Let's go ahead and reset the AspectRatio, Screens, and Resolution value's so you can enter new values.");
flags = SettingsFlags.ForceReset;
}
}
return (Width, Height, LCDLayout);
}
enum LogTypes
{
Info,
Warning,
Error
}
static void Log(string Message, LogTypes Type = LogTypes.Info)
{
if (Type == LogTypes.Info)
{
Console.ForegroundColor = ConsoleColor.White;
}
else if (Type == LogTypes.Warning)
{
Console.ForegroundColor = ConsoleColor.Yellow;
}
else if (Type == LogTypes.Error)
{
Console.ForegroundColor = ConsoleColor.Red;
}
else
{
throw new NotImplementedException("Log type must be Info, Warning, or Error");
}
Console.WriteLine(Message);
Console.ResetColor();
}
static int GCD(int a, int b)
{
return b == 0 ? Math.Abs(a) : GCD(b, a % b);
}
static void UpdateINI(string file, string[][] Settings)
{
List<string> CurrentSettings = File.Exists(file) ? File.ReadAllLines(file, Encoding.UTF8).ToList() : new List<string>();
bool EmptyFile = CurrentSettings.Count == 0;
foreach (string[] Setting in Settings.OrderBy(x => x[0]).Select(x => new string[] { "[" + x[0] + "]", x[1] + "=", x[2] }))
{
if (EmptyFile)
{
if (!CurrentSettings.Contains(Setting[0]))
{
CurrentSettings.Add(Setting[0]);
}
CurrentSettings.Add(Setting[1] + Setting[2]);
}
else
{
bool InSectionNow = false;
for (int i = 0; i < CurrentSettings.Count; i++)
{
string CurrentLine = CurrentSettings[i];
// We are now on a line of a starting section (e.g. [3D]) or at the end of the file
if (CurrentLine.StartsWith("[") && CurrentLine.EndsWith("]") || i == CurrentSettings.Count - 1)
{
// If the section is the one were trying to apply a setting in, mark it so our later code does checks
// If its not the one were trying to set to, but we were in it before, this must be the end of that section, meaning this wasnt in the INI yet, lets add it.
if (CurrentLine == Setting[0])
{
InSectionNow = true;
}
else if (InSectionNow)
{
CurrentSettings.Insert(i, Setting[1] + Setting[2]);
break;
}
}
if (InSectionNow && CurrentLine.StartsWith(Setting[1]))
{
CurrentSettings[i] = Setting[1] + Setting[2];
break;
}
}
}
}
File.WriteAllLines(file, CurrentSettings, Encoding.UTF8);
}
[Flags]
enum SettingsFlags
{
None = 0,
NumericOnly = 1,
AlphaOnly = 2,
AlphaNumericOnly = 4,
BoolOnly = 8,
Lowercase = 16,
UniqueOnly = 32,
ForceReset = 64
}
static T GetSetting<T>(string Setting, SettingsFlags Flags = SettingsFlags.None)
{
while (true)
{
if (!Flags.HasFlag(SettingsFlags.ForceReset) && Settings != null)
{
IEnumerable<string> KeysMatched = Settings.Where(x => x.StartsWith(Setting + "="));
if (KeysMatched != null && KeysMatched.Any())
{
try
{
return (T)Convert.ChangeType(KeysMatched.First().Split('=')[1], typeof(T));
}
catch
{
}
}
}
string Message = string.Empty;
Message = Setting switch
{
"AspectRatio" => "Which aspect ratio do you want (e.g. 4:3, 16:9, 21:9, e.t.c)",
"Screens" => "Do you want both screens to be shown with DeSmuME? (y/n)",
"Resolution" => "Which resolution do you want (e.g. 720, 1080, 1440, e.t.c)",
_ => "ERROR OCCURED!",
};
Console.WriteLine(Message + ":");
string Answer = Console.ReadLine();
// Apply changes to the input value based on the flag
if (Flags.HasFlag(SettingsFlags.BoolOnly))
{
Flags |= SettingsFlags.UniqueOnly | SettingsFlags.Lowercase;
}
if (Flags.HasFlag(SettingsFlags.NumericOnly))
{
Answer = Regex.Replace(Answer, "[^0-9]", string.Empty);
}
if (Flags.HasFlag(SettingsFlags.AlphaOnly))
{
Answer = Regex.Replace(Answer, "[^a-zA-Z]", string.Empty);
}
if (Flags.HasFlag(SettingsFlags.AlphaNumericOnly))
{
Answer = Regex.Replace(Answer, "[^a-zA-Z0-9]", string.Empty);
}
if (Flags.HasFlag(SettingsFlags.UniqueOnly))
{
Answer = new string(Answer.Distinct().ToArray());
}
if (Flags.HasFlag(SettingsFlags.Lowercase))
{
Answer = Answer.ToLowerInvariant();
}
if (Flags.HasFlag(SettingsFlags.BoolOnly))
{
Answer = Regex.Replace(Answer, "[^yn]", string.Empty).Replace("y", "true").Replace("n", "false");
}
// Run sanitization checks on the Answer based on Setting key
bool invalid = false;
switch (Setting)
{
case "AspectRatio":
if (!Answer.Contains(":") || !Answer.Replace(":", string.Empty).All(char.IsDigit))
{
Log("Invalid aspect ratio format.");
invalid = true;
}
break;
}
// If sanitization check passed, return the new answer, otherwise loop
if (!invalid)
{
UpdateINI(DesmumarSettingsFile, new string[][] {
new string[] {"General", Setting, Answer}
});
try
{
return (T)Convert.ChangeType(Answer, typeof(T));
}
catch (Exception ex)
{
Log(ex.Message.Replace("String was not recognized as a valid", "Invalid") + " Please type the expected response.", LogTypes.Warning);
}
}
}
}
}
}