public static void Start()
{
// Can't start if we are already running!
if (IsRunning) return;
// === Try to connect to the database
using (StatsDatabase Database = new StatsDatabase())
{
if (!Database.TablesExist)
{
string message = "In order to use the Private Stats feature of this program, we need to setup a database. "
+ "You may choose to do this later by clicking \"Cancel\". Would you like to setup the database now?";
DialogResult R = MessageBox.Show(message, "Stats Database Setup", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (R == DialogResult.Yes)
SetupManager.ShowDatabaseSetupForm(DatabaseMode.Stats, MainForm.Instance);
// Call the stopped event to Re-enable the main form's buttons
Stopped(null, EventArgs.Empty);
return;
}
// Initialize the stats manager
StatsManager.Load(Database);
// Drop the SQLite ip2nation country tables (old table versions)
if (Database.DatabaseEngine == DatabaseEngine.Sqlite)
{
string query = "SELECT COUNT(*) FROM sqlite_master WHERE type='table' AND name='ip2nation'";
if (Database.ExecuteScalar<bool>(query)) // 0 count converts to false
{
Database.Execute("DROP TABLE IF EXISTS 'ip2nation';");
Database.Execute("DROP TABLE IF EXISTS 'ip2nationcountries';");
Database.Execute("VACUUM;");
}
}
}
// === Compile our templates
string path = Path.Combine(Program.RootPath, "Web", "Bf2Stats", "Views");
foreach (string file in Directory.EnumerateFiles(path, "*.cshtml"))
{
// If this template file is loaded already, then skip
string fileName = Path.GetFileName(file);
if (Engine.Razor.IsTemplateCached(fileName, ModelType))
continue;
// Open the file, and compile it
try
{
using (FileStream stream = File.OpenRead(file))
using (StreamReader reader = new StreamReader(stream))
Engine.Razor.Compile(reader.ReadToEnd(), fileName, ModelType);
}
catch (TemplateCompilationException e)
{
// Show the Exception form so the user can view
DialogResult Res = ExceptionForm.ShowTemplateError(e, file);
// If the user clicked "Quit", we stop
if (Res == DialogResult.Abort) return;
}
}
// === Load XML stats and awards files
Bf2StatsData.Load();
BackendAwardData.BuildAwardData();
// Start the Listener and accept new connections
try
{
Listener.Start();
Listener.BeginGetContext(HandleRequest, Listener);
}
catch (ObjectDisposedException)
{
// If we are disposed (happens when port 80 was in use already before, and we tried to start)
// Then we need to start over with a new Listener
CreateHttpListener();
Listener.Start();
Listener.BeginGetContext(HandleRequest, Listener);
}
// Fire Startup Event
Started(null, EventArgs.Empty);
}