-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
178 lines (152 loc) · 6.11 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
using Flappy_Bird_Windows.Data;
using Flappy_Bird_Windows.Data.Config;
using Flappy_Bird_Windows.Forms;
using Flappy_Bird_Windows.Repository.Bird;
using Flappy_Bird_Windows.Repository.Pipe;
using Flappy_Bird_Windows.Service.BirdManager;
using Flappy_Bird_Windows.Service.PipeManager;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing.Text;
using System.Globalization;
using System.Reflection;
namespace Flappy_Bird_Windows;
internal static class Program
{
public static ServiceProvider? Services { get; private set; }
public static PrivateFontCollection Fonts { get; private set; } = new();
public static ControlsConfig ControlsConfig { get; private set; } = new();
public static GameplayConfig GameplayConfig { get; private set; } = new();
public static ProgramConfig ProgramConfig { get; private set; } = new();
[STAThread]
static void Main()
{
Process.GetCurrentProcess().PriorityClass = ProcessPriorityClass.RealTime;
RegisterServices();
Fonts.AddFontFile(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "font.ttf"));
LoadConfig();
ApplicationConfiguration.Initialize();
Application.Run(new GameForm());
}
public static void RegisterServices()
{
var serviceCollection = new ServiceCollection();
serviceCollection.AddSingleton<IBirdRepository, BirdRepository>();
serviceCollection.AddSingleton<IPipeRepository, PipeRepository>();
serviceCollection.AddSingleton<IBirdManagerService, BirdManagerService>();
serviceCollection.AddSingleton<IPipeManagerService, PipeManagerService>();
serviceCollection.AddTransient<IPipePair, PipePair>();
Services = serviceCollection.BuildServiceProvider();
}
public static void LoadConfig()
{
var configFile = "config.ini";
IConfiguration? config = null;
try
{
config = new ConfigurationBuilder().AddIniFile(configFile).Build();
}
catch (Exception ex)
{
MessageBox.Show($"Could not find config file.{Environment.NewLine}{Environment.NewLine}{ex.Message}", "Config Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
var configProperties = typeof(Program).GetProperties(BindingFlags.Static | BindingFlags.Public)
.Where(p => p.PropertyType.GetCustomAttributes(typeof(ConfigSectionAttribute), true).Length > 0);
foreach (var property in configProperties)
{
var configObject = property.GetValue(null);
var configType = property.PropertyType;
var sectionAttr = (ConfigSectionAttribute)configType.GetCustomAttributes(typeof(ConfigSectionAttribute), true)[0];
var sectionName = sectionAttr.SectionName;
try
{
config.GetSection(sectionName).Bind(configObject);
}
catch (Exception ex)
{
MessageBox.Show($"Could not load config section '{sectionName}'. Using default config instead.{Environment.NewLine}{Environment.NewLine}{ex.Message}", "Config Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
public static bool SaveConfig()
{
try
{
WriteConfigToFile("config.ini");
return true;
}
catch (UnauthorizedAccessException)
{
var tempFile = Path.GetTempFileName();
WriteConfigToFile(tempFile);
var startInfo = new ProcessStartInfo
{
FileName = "cmd.exe",
Arguments = $"/c move /Y \"{tempFile}\" \"{Path.GetFullPath("config.ini")}\"",
Verb = "runas",
UseShellExecute = true,
CreateNoWindow = true
};
try
{
var process = Process.Start(startInfo);
process?.WaitForExit();
if (process?.ExitCode != 0)
{
MessageBox.Show("Could not save config. Please try again later.", "Save Config", MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
}
}
catch (Win32Exception)
{
MessageBox.Show("Aborted.", "Save Config", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return false;
}
return true;
}
}
private static void WriteConfigToFile(string configFile)
{
using var writer = new StreamWriter(configFile);
var configProperties = typeof(Program).GetProperties(BindingFlags.Static | BindingFlags.Public)
.Where(p => p.PropertyType.GetCustomAttributes(typeof(ConfigSectionAttribute), true).Length > 0);
foreach (var property in configProperties)
{
var configObject = property.GetValue(null);
var configType = property.PropertyType;
var sectionAttr = (ConfigSectionAttribute)configType.GetCustomAttributes(typeof(ConfigSectionAttribute), true)[0];
var sectionName = sectionAttr.SectionName;
writer.WriteLine($"[{sectionName}]");
var properties = configType.GetProperties();
foreach (var prop in properties)
{
var propName = prop.Name;
var propValue = prop.GetValue(configObject);
writer.WriteLine($"{propName}={ConvertObjectToString(propValue)}");
}
writer.WriteLine();
}
}
public static void ResetConfig()
{
ControlsConfig = new ControlsConfig();
GameplayConfig = new GameplayConfig();
ProgramConfig = new ProgramConfig();
SaveConfig();
}
private static string? ConvertObjectToString(object? obj)
{
if (obj is bool)
{
return obj.ToString()?.ToLower();
}
else if (obj is float or double or decimal)
{
return Convert.ToString(obj, CultureInfo.InvariantCulture);
}
return obj?.ToString();
}
}