forked from cbluoss/OpenHacknet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Game1.cs
276 lines (258 loc) · 10 KB
/
Game1.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
using System;
using System.Globalization;
using System.Threading;
using System.Windows.Forms;
using Hacknet.Effects;
using Hacknet.PlatformAPI.Storage;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Point = System.Drawing.Point;
namespace Hacknet
{
public class Game1 : Game
{
private static Game1 singleton;
public static bool threadsExiting;
public static CultureInfo culture;
private bool CanLoadContent;
public GraphicsDeviceManager graphics;
public GraphicsDeviceInformation graphicsInfo;
private readonly EventHandler<PreparingDeviceSettingsEventArgs> graphicsPreparedHandler;
private bool HasLoadedContent;
private bool IsDrawing;
private bool resolutionSet = true;
private ScreenManager sman;
private SpriteBatch spriteBatch;
public Game1()
{
culture = new CultureInfo("en-US");
Thread.CurrentThread.CurrentCulture = culture;
Thread.CurrentThread.CurrentUICulture = culture;
threadsExiting = false;
graphics = new GraphicsDeviceManager(this);
graphics.GraphicsProfile = GraphicsProfile.HiDef;
graphics.PreferMultiSampling = true;
graphics.DeviceDisposing += graphics_DeviceDisposing;
graphics.DeviceResetting += graphics_DeviceResetting;
graphics.DeviceReset += graphics_DeviceReset;
SettingsLoader.checkStatus();
if (SettingsLoader.didLoad)
{
CanLoadContent = true;
graphics.PreferredBackBufferWidth = Math.Min(SettingsLoader.resWidth, 4096);
graphics.PreferredBackBufferHeight = Math.Min(SettingsLoader.resHeight, 4096);
graphics.IsFullScreen = SettingsLoader.isFullscreen;
}
else if (Settings.windowed)
{
graphics.PreferredBackBufferWidth = 1280;
graphics.PreferredBackBufferHeight = 800;
CanLoadContent = true;
}
else
{
graphicsPreparedHandler = graphics_PreparingDeviceSettings;
graphics.PreparingDeviceSettings += graphicsPreparedHandler;
graphics.PreferredBackBufferWidth = 1280;
graphics.PreferredBackBufferHeight = 800;
graphics.IsFullScreen = true;
}
IsMouseVisible = true;
IsFixedTimeStep = false;
Content.RootDirectory = "Content";
singleton = this;
Exiting += handleExit;
PlatformAPISettings.InitPlatformAPI();
StatsManager.InitStats();
}
private void graphics_DeviceReset(object sender, EventArgs e)
{
Program.GraphicsDeviceResetLog = Program.GraphicsDeviceResetLog + "Reset at " +
DateTime.Now.ToShortTimeString();
Console.WriteLine("Graphics Device Reset Complete");
Utils.white.Dispose();
Utils.white = null;
LoadRegenSafeContent();
HasLoadedContent = true;
}
private void graphics_DeviceResetting(object sender, EventArgs e)
{
HasLoadedContent = false;
}
private void graphics_DeviceDisposing(object sender, EventArgs e)
{
HasLoadedContent = false;
}
private void graphics_PreparingDeviceSettings(object sender, PreparingDeviceSettingsEventArgs e)
{
var currentDisplayMode = GraphicsAdapter.DefaultAdapter.CurrentDisplayMode;
var width = currentDisplayMode.Width;
var height = currentDisplayMode.Height;
graphics.PreferredBackBufferWidth = Math.Min(width, 4096);
graphics.PreferredBackBufferHeight = Math.Min(height, 4096);
graphics.PreferMultiSampling = true;
resolutionSet = false;
graphics.PreparingDeviceSettings -= graphicsPreparedHandler;
CanLoadContent = true;
}
public void setNewGraphics()
{
graphics.ApplyChanges();
var screens = sman.GetScreens();
var flag = false;
string str1 = null;
string str2 = null;
for (var index = 0; index < screens.Length; ++index)
{
var os = screens[index] as OS;
if (os != null)
{
os.threadedSaveExecute();
flag = true;
str1 = os.SaveGameUserName;
str2 = os.SaveUserAccountName;
break;
}
}
Components.Remove(sman);
sman = new ScreenManager(this);
Components.Add(sman);
LoadGraphicsContent();
if (flag)
{
OS.WillLoadSave = true;
var os = new OS();
os.SaveGameUserName = str1;
os.SaveUserAccountName = str2;
MainMenu.resetOS();
sman.AddScreen(os, sman.controllingPlayer);
}
GuiData.spriteBatch = sman.SpriteBatch;
if (sman.GetScreens().Length != 0)
return;
LoadInitialScreens();
}
public void setWindowPosition(Vector2 pos)
{
if (SettingsLoader.isFullscreen)
return;
var form = (Form) Control.FromHandle(Window.Handle);
form.Location = new Point(form.Location.X - 5, form.Location.Y - 5);
Control.FromHandle(Window.Handle).FindForm().FormBorderStyle = FormBorderStyle.None;
}
protected override void Initialize()
{
sman = new ScreenManager(this);
Components.Add(sman);
graphics.PreferMultiSampling = true;
NameGenerator.init();
Helpfile.init();
FileEntry.init(Content);
PatternDrawer.init(Content);
ProgramList.init();
Cube3D.Initilize(graphics.GraphicsDevice);
base.Initialize();
}
private void handleExit(object sender, EventArgs e)
{
threadsExiting = true;
MusicManager.stop();
}
private void LoadRegenSafeContent()
{
Utils.white = new Texture2D(graphics.GraphicsDevice, 1, 1);
var data = new Color[1]
{
Color.White
};
Utils.white.SetData(data);
var content = Content;
Utils.gradient = content.Load<Texture2D>("Gradient");
Utils.gradientLeftRight = content.Load<Texture2D>("GradientHorizontal");
}
protected override void LoadContent()
{
if (!CanLoadContent)
return;
PortExploits.populate();
sman.controllingPlayer = PlayerIndex.One;
if (Settings.isConventionDemo)
setWindowPosition(new Vector2(200f, 200f));
LoadGraphicsContent();
LoadRegenSafeContent();
var content = Content;
GuiData.font = content.Load<SpriteFont>("Font23");
GuiData.titlefont = content.Load<SpriteFont>("Kremlin");
GuiData.smallfont = Content.Load<SpriteFont>("Font12");
GuiData.UISmallfont = GuiData.smallfont;
GuiData.tinyfont = Content.Load<SpriteFont>("Font10");
GuiData.UITinyfont = GuiData.tinyfont;
GuiData.detailfont = Content.Load<SpriteFont>("Font7");
GuiData.spriteBatch = sman.SpriteBatch;
GuiData.font = content.Load<SpriteFont>("Font23");
GuiData.titlefont = content.Load<SpriteFont>("Kremlin");
GuiData.smallfont = Content.Load<SpriteFont>("Font12");
GuiData.UISmallfont = GuiData.smallfont;
GuiData.tinyfont = Content.Load<SpriteFont>("Font10");
GuiData.UITinyfont = GuiData.tinyfont;
GuiData.detailfont = Content.Load<SpriteFont>("Font7");
GuiData.init(Window);
GuiData.InitFontOptions(Content);
VehicleInfo.init();
WorldLocationLoader.init();
ThemeManager.init(content);
MissionGenerationParser.init();
MissionGenerator.init(content);
UsernameGenerator.init();
MusicManager.init(content);
SFX.init(content);
OldSystemSaveFileManifest.Load();
SaveFileManager.Init();
HasLoadedContent = true;
LoadInitialScreens();
}
protected void LoadGraphicsContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
PostProcessor.init(graphics.GraphicsDevice, spriteBatch, Content);
WebRenderer.init(graphics.GraphicsDevice);
}
protected void LoadInitialScreens()
{
if (Settings.MenuStartup)
sman.AddScreen(new MainMenu(), sman.controllingPlayer);
else
sman.AddScreen(new OS(), sman.controllingPlayer);
}
protected override void UnloadContent()
{
}
protected override void Update(GameTime gameTime)
{
if (!HasLoadedContent)
LoadContent();
if (!resolutionSet)
{
setNewGraphics();
resolutionSet = true;
}
PatternDrawer.update((float) gameTime.ElapsedGameTime.TotalSeconds);
GuiData.setTimeStep((float) gameTime.ElapsedGameTime.TotalSeconds);
MusicManager.Update((float) gameTime.ElapsedGameTime.TotalSeconds);
ThemeManager.Update((float) gameTime.ElapsedGameTime.TotalSeconds);
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
if (!HasLoadedContent)
return;
IsDrawing = true;
base.Draw(gameTime);
IsDrawing = false;
}
public static Game1 getSingleton()
{
return singleton;
}
}
}