-
-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathGameClass.cs
More file actions
233 lines (186 loc) · 8.45 KB
/
Copy pathGameClass.cs
File metadata and controls
233 lines (186 loc) · 8.45 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
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
global using static TSMapEditor.Misc.Translator;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Rampastring.Tools;
using Rampastring.XNAUI;
using System;
using System.Globalization;
using System.IO;
using System.Text;
using System.Threading;
#if WINDOWS
using System.Windows.Forms;
using TSMapEditor.UI.IME;
#endif
using TSMapEditor.CCEngine;
using TSMapEditor.Misc;
using TSMapEditor.Settings;
using TSMapEditor.UI;
#if !DEBUG
using System.Windows.Forms;
#endif
namespace TSMapEditor.Rendering
{
public class GameClass : Microsoft.Xna.Framework.Game
{
private const double PowerSavingTime = 100.0;
private GraphicsDeviceManager graphics;
public GameClass()
{
#if !DEBUG
AppDomain.CurrentDomain.UnhandledException += (s, e) => HandleUnhandledException((Exception)e.ExceptionObject);
Application.ThreadException += (s, e) => HandleUnhandledException(e.Exception);
#endif
Program.DisableExceptionHandler();
Logger.WriteToConsole = true;
Logger.WriteLogFile = true;
Logger.Initialize(Environment.CurrentDirectory + "/", "MapEditorLog.log");
try
{
File.Delete(Environment.CurrentDirectory + "/MapEditorLog.log");
}
catch (IOException ex)
{
Logger.Log("Failed to delete log file! Returned error: " + ex.Message);
}
Logger.Log("C&C World-Altering Editor (WAE) build " + System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString());
Logger.Log("Release version: " + Constants.ReleaseVersion);
AutoLATType.InitArray();
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
TranslatorSetup.LoadTranslations();
Constants.Init();
new UserSettings();
TranslatorSetup.SetActiveTranslation(UserSettings.Instance.Language);
AutosaveTimer.Purge();
graphics = new GraphicsDeviceManager(this);
graphics.HardwareModeSwitch = false;
graphics.GraphicsProfile = GraphicsProfile.HiDef;
graphics.PreferredDepthStencilFormat = DepthFormat.Depth24Stencil8;
Content.RootDirectory = "Content";
graphics.SynchronizeWithVerticalRetrace = false;
Window.Title = "C&C World-Altering Editor (WAE)";
//IsFixedTimeStep = false;
SetTargetFPS();
}
private void HandleUnhandledException(Exception ex)
{
string exceptLogPath = Environment.CurrentDirectory + DSC + "except.txt";
File.Delete(exceptLogPath);
StringBuilder sb = new StringBuilder();
string fullName = typeof(GameClass).Assembly.FullName;
LogLineGenerate("World-Altering Editor (" + fullName + ")", sb, exceptLogPath);
LogLineGenerate("Release version: " + Constants.ReleaseVersion, sb, exceptLogPath);
LogLineGenerate("Unhandled exception! @ " + DateTime.Now.ToLongTimeString(), sb, exceptLogPath);
LogLineGenerate("Message: " + ex.Message, sb, exceptLogPath);
LogLineGenerate("Stack trace: " + ex.StackTrace, sb, exceptLogPath);
if (ex.InnerException != null)
{
LogLineGenerate("***************************", sb, exceptLogPath);
LogLineGenerate("InnerException information:", sb, exceptLogPath);
LogLineGenerate("Message: " + ex.InnerException.Message, sb, exceptLogPath);
LogLineGenerate("Stack trace: " + ex.InnerException.StackTrace, sb, exceptLogPath);
}
Logger.Log("Exiting.");
windowManager?.HideWindow();
System.Windows.Forms.MessageBox.Show("The map editor has crashed." + Environment.NewLine + Environment.NewLine +
"Exception information logged into except.txt:" + Environment.NewLine + Environment.NewLine +
sb.ToString());
Environment.Exit(255);
}
private void LogLineGenerate(string text, StringBuilder sb, string exceptLogPath)
{
sb.Append(text + Environment.NewLine);
Logger.ForceLog(text, exceptLogPath);
}
private WindowManager windowManager;
private bool wasActiveOnPreviousFrame;
private readonly char DSC = Path.DirectorySeparatorChar;
protected override void Initialize()
{
base.Initialize();
Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
Thread.CurrentThread.CurrentUICulture = CultureInfo.InvariantCulture;
AssetLoader.Initialize(GraphicsDevice, Content);
AssetLoader.AssetSearchPaths.Add(Path.Combine(Environment.CurrentDirectory, "Config", "Translations", TranslatorSetup.ActiveTranslationDirectory()));
AssetLoader.AssetSearchPaths.Add(Environment.CurrentDirectory + DSC + "Content" + DSC);
// // Hack: allow translations to override fonts
// int i = 0;
// while (true)
// {
// string spriteFontPath = Path.Combine("Translations", TranslatorSetup.ActiveTranslationDirectory(), "SpriteFont" + i + ".xnb");
// if (AssetLoader.AssetExists(Path.Combine(Environment.CurrentDirectory, "Config", spriteFontPath)))
// {
// var spriteFont = Content.Load<SpriteFont>(spriteFontPath);
// Renderer.GetFontList()[i] = spriteFont;
// }
// else
// {
// break;
// }
// }
windowManager = new WindowManager(this, graphics);
windowManager.Initialize(Content, Environment.CurrentDirectory + DSC + "Content" + DSC);
new Parser(windowManager);
const int menuRenderWidth = 800;
const int menuRenderHeight = 600;
int menuWidth = menuRenderWidth;
int menuHeight = menuRenderHeight;
int dpi = NativeMethods.GetScreenDPI();
double dpi_ratio = dpi / 96.0;
menuWidth = (int)(menuWidth * dpi_ratio);
menuHeight = (int)(menuHeight * dpi_ratio);
#if WINDOWS
// If the user has a very large display at 100% DPI, it's probably best to integer-upscale the main menu.
if (dpi_ratio <= 1.0)
{
const int minScaleRatio = 3;
if (Screen.PrimaryScreen.Bounds.Width >= menuRenderWidth * minScaleRatio &&
Screen.PrimaryScreen.Bounds.Height >= menuRenderHeight * minScaleRatio)
{
menuWidth = menuRenderWidth * 2;
menuHeight = menuRenderHeight * 2;
}
}
#endif
windowManager.InitGraphicsMode(menuWidth, menuHeight, false);
windowManager.SetRenderResolution(menuRenderWidth, menuRenderHeight);
windowManager.CenterOnScreen();
windowManager.Cursor.LoadNativeCursor(Environment.CurrentDirectory + DSC + "Content" + DSC + "cursor.cur");
windowManager.SetBorderlessMode(false);
Components.Add(windowManager);
EditorThemes.Initialize();
UISettings.ActiveSettings = new CustomUISettings()
{
PanelBackgroundColor = new Color(32, 32, 32),
PanelBorderColor = new Color(196, 196, 196)
};
#if WINDOWS
IMEHandler imeHandler = IMEHandler.Create(this);
windowManager.IMEHandler = imeHandler;
#endif
InitMainMenu();
}
private void InitMainMenu()
{
var mainMenu = new MainMenu(windowManager);
windowManager.AddAndInitializeControl(mainMenu);
windowManager.CenterControlOnScreen(mainMenu);
}
private void SetTargetFPS()
{
TargetElapsedTime = TimeSpan.FromMilliseconds(1000.0 / UserSettings.Instance.TargetFPS);
}
protected override void Update(GameTime gameTime)
{
if (IsActive != wasActiveOnPreviousFrame)
{
if (!IsActive)
TargetElapsedTime = TimeSpan.FromMilliseconds(PowerSavingTime); // Don't run at max FPS if we're not the active window
else
SetTargetFPS();
wasActiveOnPreviousFrame = IsActive;
}
base.Update(gameTime);
}
}
}