forked from Turnerj/Borderless1942
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
134 lines (122 loc) · 3.9 KB
/
Program.cs
File metadata and controls
134 lines (122 loc) · 3.9 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
using Borderless1942;
using System.Diagnostics;
var monitorBounds = Win32Extensions.GetPrimaryMonitor().GetBounds();
var defaultWidth = monitorBounds.Width;
var defaultHeight = monitorBounds.Height;
var width = defaultWidth;
var height = defaultHeight;
var skipConfigEdits = false;
List<string> additionalArgs = new();
for (int i = 0; i < args.Length; i++)
{
if (args[i].Equals("-width", StringComparison.OrdinalIgnoreCase) && i + 1 < args.Length)
{
if (int.TryParse(args[i + 1], out int parsedWidth))
{
width = parsedWidth;
}
}
else if (args[i].Equals("-height", StringComparison.OrdinalIgnoreCase) && i + 1 < args.Length)
{
if (int.TryParse(args[i + 1], out int parsedHeight))
{
height = parsedHeight;
}
}
else if (args[i].Equals("-noedit", StringComparison.OrdinalIgnoreCase))
{
skipConfigEdits = true;
}
else
additionalArgs.Add(args[i]);
}
if (!skipConfigEdits)
{
// Update Video.con files if they exist
var modsPath = Path.Combine(Environment.CurrentDirectory, "Mods", "bf1942", "Settings");
if (Directory.Exists(modsPath))
{
// Update resolution in profile Video.con files
var videoConFiles = Directory.GetFiles(Path.Combine(modsPath, "Profiles"), "Video.con", SearchOption.AllDirectories);
foreach (var videoConFile in videoConFiles)
{
var lines = File.ReadAllLines(videoConFile);
for (int i = 0; i < lines.Length; i++)
{
if (lines[i].StartsWith("game.setGameDisplayMode"))
{
lines[i] = $"game.setGameDisplayMode {width} {height} 32 60";
}
}
File.WriteAllLines(videoConFile, lines);
Console.WriteLine($"[Borderless1942]: [{DateTime.Now:yyyy-MM-dd - hh:mm:ss tt}] [Updated resolution in {videoConFile} to {width}x{height}]");
}
// Update fullscreen setting in VideoDefault.con
var videoDefaultPath = Path.Combine(modsPath, "VideoDefault.con");
if (File.Exists(videoDefaultPath))
{
var lines = File.ReadAllLines(videoDefaultPath);
for (int i = 0; i < lines.Length; i++)
{
if (lines[i].StartsWith("renderer.setFullScreen"))
{
lines[i] = "renderer.setFullScreen 0";
}
}
File.WriteAllLines(videoDefaultPath, lines);
Console.WriteLine($"[Borderless1942]: [{DateTime.Now:yyyy-MM-dd - hh:mm:ss tt}] [Updated fullscreen setting in VideoDefault.con]");
}
}
}
// Start Process
var processStartInfo = new ProcessStartInfo
{
FileName = @"BF1942.exe",
};
foreach (var arg in additionalArgs)
processStartInfo.ArgumentList.Add(arg);
var process = Process.Start(processStartInfo)!;
var keepAlive = true;
Console.WriteLine($"[Borderless1942]: [{DateTime.Now:yyyy-MM-dd - hh:mm:ss tt}] [BF1942 Process Has Started] [{process.Id}]");
// Wait for initial window handle
var window = await process.WaitForMainWindowAsync();
window.RemoveBorders();
// Keep itself alive until detected otherwise
while (keepAlive)
{
MainLoop:
UpdateWindowPosition(window, width, height);
await Task.Delay(100);
if (process.HasExited)
{
var oldProcessId = process.Id;
var retryCount = 0;
while (retryCount < 3)
{
var matchingProcesses = Process.GetProcessesByName("BF1942");
if (matchingProcesses.Length == 1)
{
process = matchingProcesses[0];
if (process.HasExited)
{
break;
}
Console.WriteLine($"[Borderless1942]: [{DateTime.Now:yyyy-MM-dd - hh:mm:ss tt}] [BF1942 Process Has Changed] [{oldProcessId} -> {process.Id}]");
window = await process.WaitForMainWindowAsync();
window.RemoveBorders();
goto MainLoop;
}
retryCount++;
await Task.Delay(TimeSpan.FromSeconds(1));
}
keepAlive = false;
Console.WriteLine($"[Borderless1942]: [{DateTime.Now:yyyy-MM-dd - hh:mm:ss tt}] [BF1942 Process Has Exited]");
}
}
static void UpdateWindowPosition(Window window, int width, int height)
{
var monitorBounds = window.GetCurrentMonitor().GetBounds();
var x = monitorBounds.Left + (monitorBounds.Width - width) / 2;
var y = monitorBounds.Top + (monitorBounds.Height - height) / 2;
window.SetPosition(x, y, width, height);
}