-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainForm.cs
230 lines (200 loc) · 7.44 KB
/
MainForm.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
/*
* File: NewLayoutForm.cs
* Date: 11/02/2024
* Author: NikoBK
*/
using flashback_emulator.UserControls;
using System.Diagnostics;
namespace flashback_emulator
{
public partial class MainForm : Form
{
// TODO: This should be based on some save data that fetches
// games from some directory of files.
public int GamesCount { get; private set; } = 0;
private AppData AppData { get; set; }
private UserControl? CurrentScreenView { get; set; }
// Game stats
private System.Windows.Forms.Timer _gameProcessTimer;
private DateTime _gameStartTime;
private TimeSpan _elapsedGameTime;
private Process _gameProcess;
private string _currentGameId;
public MainForm()
{
InitializeComponent();
LoadAppData();
CurrentScreenView = null;
_gameProcessTimer = new System.Windows.Forms.Timer();
_gameProcessTimer.Interval = 1000; // Update every second
_gameProcessTimer.Tick += GameTimer_Tick;
// Start parsing games for caching.
DataManager.Init();
if (!AppData.Registered)
{
// If the user for the existing appdata is not registered display the
// introductory view.
UpdateScreenView(new NewUserView(this));
}
else
{
if (AppData.Games.Count > 0)
{
UpdateScreenView(new LibraryView(this, AppData));
}
else
{
UpdateScreenView(new EmptyLibrary(this, AppData));
}
}
}
/// <summary>
/// Load the appdata for flashback using the <see cref="DataManager"/>.
/// </summary>
private void LoadAppData()
{
AppData = DataManager.LoadAppData(DataManager.GetAppDataPath());
}
/// <summary>
/// Update the current screenview by removing and disposing the old one
/// to then display the new screenview.
/// </summary>
/// <param name="screenView"></param>
private void UpdateScreenView(UserControl screenView)
{
// Remove and dispose the current screenview before updating.
if (CurrentScreenView != null)
{
screenViewPanel.Controls.Remove(CurrentScreenView);
CurrentScreenView.Dispose();
}
// Update the current screenview.
CurrentScreenView = screenView;
CurrentScreenView.Dock = DockStyle.Fill;
screenViewPanel.Controls.Add(CurrentScreenView);
}
/// <summary>
/// Function call for the <see cref="NewUserView"/> when the confirm button is clicked.
/// Loads Flashback's appdata and feeds it to the <see cref="LibraryView"/>.
/// </summary>
public void OpenLibrary()
{
LoadAppData();
if (AppData.Games.Count > 0)
{
UpdateScreenView(new LibraryView(this, AppData));
}
else
{
UpdateScreenView(new EmptyLibrary(this, AppData));
}
}
/// <summary>
/// Function call for the <see cref="LibraryView"/> wehn the 'ADD GAME' button is clicked.
/// Loads Flashback's appdata and feeds it to the <see cref="StoreView"/>.
/// </summary>
public void OpenStore()
{
LoadAppData();
UpdateScreenView(new StoreView(this, AppData));
}
public void ShowGameView(GameData data)
{
LoadAppData();
UpdateScreenView(new GameView(this, AppData, data));
}
public void AddGameToLibrary(GameData game)
{
// Get the latest application data.
LoadAppData();
// Add the game.
AppData.Games.Add(game);
// Save the addition of the game.
game.InLibrary = true;
DataManager.SaveData(AppData);
// Refresh the library.
OpenLibrary();
}
public void PlayGame(GameData game)
{
LoadAppData();
if (File.Exists(game.SWFPath))
{
// MessageBox.Show(game.SWFPath);
var pi = new ProcessStartInfo(game.SWFPath)
{
Arguments = Path.GetFileName(game.SWFPath),
UseShellExecute = true,
WorkingDirectory = Path.GetDirectoryName(game.SWFPath),
FileName = game.SWFPath,
Verb = "OPEN"
};
_gameProcess = Process.Start(pi);
// Record the start time.
_gameStartTime = DateTime.Now;
// Update last played.
DateTime today = DateTime.Today;
// Start the time.
_gameProcessTimer.Start();
foreach (var savedGame in AppData.Games)
{
if (savedGame.Id == game.Id)
{
savedGame.LastPlayed = today;
_currentGameId = savedGame.Id;
if (!savedGame.HasBeenPlayed)
{
savedGame.HasBeenPlayed = true;
}
var gameview = (CurrentScreenView as GameView);
if (gameview != null)
{
gameview.UpdateLastPlayed(savedGame.LastPlayed);
}
}
}
}
else
{
// This should not happen as we do a files check in the GameView for the game.
// Keeping this here just in case though.
// TODO: Check and remove if redundant.
MessageBox.Show($"Unable to find the game files for {game.Name}", "Game not found", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void GameTimer_Tick(object sender, EventArgs e)
{
// Calculate elapsed time.
TimeSpan elapsedTime = DateTime.Now - _gameStartTime;
_elapsedGameTime = elapsedTime;
}
/// <summary>
/// Kills the process of the current game that is being played.
/// </summary>
public void StopGame()
{
_gameProcessTimer.Stop();
// Stop the flashplayer process.
if (_gameProcess != null && !_gameProcess.HasExited)
{
_gameProcess.Kill(); // Close the process forcefully.
_gameProcess.Dispose(); // Release resources.
foreach (var game in AppData.Games)
{
if (game.Id == _currentGameId)
{
game.TimePlayedS += _elapsedGameTime;
var gameview = (CurrentScreenView as GameView);
if (gameview != null)
{
gameview.UpdateTimePlayed(game.TimePlayedS);
}
}
}
_currentGameId = string.Empty;
// Save game stats such as total time played and last played.
DataManager.SaveData(AppData);
}
}
}
}