Added version checking of database, dcs hook and windows app - in case of mismatch , app is terminating

This commit is contained in:
szporowolik
2019-10-22 17:36:36 +02:00
parent a0c9a249f6
commit 9770c26b93
4 changed files with 95 additions and 35 deletions

View File

@@ -39,6 +39,9 @@ public class DatabaseController
if (TCPFrameType == "1") if (TCPFrameType == "1")
{ {
TCPFrame.payload["v_win"] = "v" + Globals.VersionPerun; // Inject app version information TCPFrame.payload["v_win"] = "v" + Globals.VersionPerun; // Inject app version information
// Pull DCS hook version
Globals.VersionDCSHook = TCPFrame.payload.v_dcs_hook;
} }
// Specific SQL per each frame type // Specific SQL per each frame type
@@ -83,7 +86,7 @@ public class DatabaseController
else if (TCPFrameType == "-1") else if (TCPFrameType == "-1")
{ {
// Keep alive message // Keep alive message
SQLQueryTxt = "SELECT 1;"; SQLQueryTxt = "SELECT `pe_Config_payload` FROM `pe_Config` WHERE `pe_Config_id` = 1;";
} }
else else
{ {
@@ -106,41 +109,57 @@ public class DatabaseController
DatabaseStatus = true; DatabaseStatus = true;
MySqlCommand DatabaseCommand = new MySqlCommand(SQLQueryTxt, DatabaseConnection); MySqlCommand DatabaseCommand = new MySqlCommand(SQLQueryTxt, DatabaseConnection);
MySqlDataReader DatabaseReader = DatabaseCommand.ExecuteReader(); MySqlDataReader DatabaseReader = DatabaseCommand.ExecuteReader();
if (DatabaseReader.HasRows)
{
while (DatabaseReader.Read())
{
if (TCPFrameType == "-1")
{
Globals.VersionDatabase= DatabaseReader.GetString(0);
}
}
}
else
{
// Do nothing
}
DatabaseReader.Close(); DatabaseReader.Close();
switch (Int32.Parse(TCPFrameType)) switch (Int32.Parse(TCPFrameType))
{ {
case 1: case 1:
PerunHelper.GUILogHistoryAdd(ref Globals.AppLogHistory, "Connected players: " + TCPFrame.payload["c_players"], 1,0,"1"); PerunHelper.AddLog(ref Globals.AppLogHistory, "Connected players: " + TCPFrame.payload["c_players"], 1,0,"1");
break; break;
case 2: case 2:
PerunHelper.GUILogHistoryAdd(ref Globals.AppLogHistory, "Mission: \"" + TCPFrame.payload["mission"]["name"]+"\""+ ", time:" + TCPFrame.payload["mission"]["modeltime"], 1, 0, "2"); PerunHelper.AddLog(ref Globals.AppLogHistory, "Mission: \"" + TCPFrame.payload["mission"]["name"]+"\""+ ", time:" + TCPFrame.payload["mission"]["modeltime"], 1, 0, "2");
break; break;
case 3: case 3:
PerunHelper.GUILogHistoryAdd(ref Globals.AppLogHistory, "Slots data updated", 1, 0, "3"); PerunHelper.AddLog(ref Globals.AppLogHistory, "Slots data updated", 1, 0, "3");
break; break;
case 50: case 50:
PerunHelper.GUILogHistoryAdd(ref Globals.AppLogHistory, "Player's \""+ TCPFrame.payload.player + "\" chat message saved", 1, 0, "50"); PerunHelper.AddLog(ref Globals.AppLogHistory, "Player's \""+ TCPFrame.payload.player + "\" chat message saved", 1, 0, "50");
break; break;
case 51: case 51:
PerunHelper.GUILogHistoryAdd(ref Globals.AppLogHistory, "Game event: \""+ TCPFrame.payload.log_content +"\"", 1, 0, "51"); PerunHelper.AddLog(ref Globals.AppLogHistory, "Game event: \""+ TCPFrame.payload.log_content +"\"", 1, 0, "51");
break; break;
case 52: case 52:
PerunHelper.GUILogHistoryAdd(ref Globals.AppLogHistory, "Player's \""+ TCPFrame.payload.stat_name + "\" stats saved", 1, 0, "52"); PerunHelper.AddLog(ref Globals.AppLogHistory, "Player's \""+ TCPFrame.payload.stat_name + "\" stats saved", 1, 0, "52");
break; break;
case 53: case 53:
PerunHelper.GUILogHistoryAdd(ref Globals.AppLogHistory, "Player \""+ TCPFrame.payload.login_name + "\" logged in", 1, 0, "53"); PerunHelper.AddLog(ref Globals.AppLogHistory, "Player \""+ TCPFrame.payload.login_name + "\" logged in", 1, 0, "53");
break; break;
case 100: case 100:
PerunHelper.GUILogHistoryAdd(ref Globals.AppLogHistory, "SRS data send", 1,0,"100"); PerunHelper.AddLog(ref Globals.AppLogHistory, "SRS data send", 1,0,"100");
break; break;
case 101: case 101:
PerunHelper.GUILogHistoryAdd(ref Globals.AppLogHistory, "LotATC data send", 1,0,"101"); PerunHelper.AddLog(ref Globals.AppLogHistory, "LotATC data send", 1,0,"101");
break; break;
case -1: case -1:
break; break;
default: default:
PerunHelper.GUILogHistoryAdd(ref Globals.AppLogHistory, "Data send", 1,0, TCPFrameType); PerunHelper.AddLog(ref Globals.AppLogHistory, "Data send", 1,0, TCPFrameType);
break; break;
} }
@@ -149,7 +168,7 @@ public class DatabaseController
{ {
// General exception found // General exception found
Console.WriteLine(a_ex.ToString()); Console.WriteLine(a_ex.ToString());
PerunHelper.GUILogHistoryAdd(ref Globals.AppLogHistory, "ERROR MySQL - error: " + a_ex.Message,1,1, TCPFrameType); PerunHelper.AddLog(ref Globals.AppLogHistory, "ERROR MySQL - error: " + a_ex.Message,1,1, TCPFrameType);
Globals.ErrorsGame++; Globals.ErrorsGame++;
DatabaseStatus = false; DatabaseStatus = false;
ReturnValue = 0; ReturnValue = 0;
@@ -160,15 +179,15 @@ public class DatabaseController
switch (m_ex.Number) switch (m_ex.Number)
{ {
case 1042: // Unable to connect to any of the specified MySQL hosts (Check Server,Port) case 1042: // Unable to connect to any of the specified MySQL hosts (Check Server,Port)
PerunHelper.GUILogHistoryAdd(ref Globals.AppLogHistory, "ERROR MySQL - unable to connect, error: " + m_ex.Message,1,1, TCPFrameType); PerunHelper.AddLog(ref Globals.AppLogHistory, "ERROR MySQL - unable to connect, error: " + m_ex.Message,1,1, TCPFrameType);
break; break;
case 0: // Access denied (Check DB name,username,password) case 0: // Access denied (Check DB name,username,password)
PerunHelper.GUILogHistoryAdd(ref Globals.AppLogHistory, "ERROR MySQL - access denied, error: " + m_ex.Message,1,1, TCPFrameType); PerunHelper.AddLog(ref Globals.AppLogHistory, "ERROR MySQL - access denied, error: " + m_ex.Message,1,1, TCPFrameType);
break; break;
default: default:
PerunHelper.GUILogHistoryAdd(ref Globals.AppLogHistory, "ERROR MySQL - error id: " + m_ex.Number,1,1, TCPFrameType); PerunHelper.AddLog(ref Globals.AppLogHistory, "ERROR MySQL - error id: " + m_ex.Number,1,1, TCPFrameType);
PerunHelper.GUILogHistoryAdd(ref Globals.AppLogHistory, "ERROR MySQL - query: " + SQLQueryTxt, 1, 1, TCPFrameType); PerunHelper.AddLog(ref Globals.AppLogHistory, "ERROR MySQL - query: " + SQLQueryTxt, 1, 1, TCPFrameType);
PerunHelper.GUILogHistoryAdd(ref Globals.AppLogHistory, "ERROR MySQL - error: " + m_ex.Message,1,1, TCPFrameType); PerunHelper.AddLog(ref Globals.AppLogHistory, "ERROR MySQL - error: " + m_ex.Message,1,1, TCPFrameType);
break; break;
} }
Globals.ErrorsGame++; Globals.ErrorsGame++;
@@ -180,7 +199,7 @@ public class DatabaseController
} }
catch (ArgumentException x_ex) catch (ArgumentException x_ex)
{ {
PerunHelper.GUILogHistoryAdd(ref Globals.AppLogHistory, "ERROR MySQL - unable to connect, error: " + x_ex.Message,1,1, TCPFrameType); PerunHelper.AddLog(ref Globals.AppLogHistory, "ERROR MySQL - unable to connect, error: " + x_ex.Message,1,1, TCPFrameType);
ReturnValue = 0; ReturnValue = 0;
Globals.ErrorsGame++; Globals.ErrorsGame++;
} }

View File

@@ -1,10 +1,11 @@
// This class gathers all helper functions // This class gathers all helper functions
using System; using System;
using System.Reflection; using System.Reflection;
using System.Text.RegularExpressions;
internal class PerunHelper internal class PerunHelper
{ {
public static void GUILogHistoryAdd(ref string[] arrLogHistory, string strEntryToAdd, int intDirection = 0, int intMarker = 0, string strType = " ", bool bSkipGui = false) public static void AddLog(ref string[] arrLogHistory, string strEntryToAdd, int intDirection = 0, int intMarker = 0, string strType = " ", bool bSkipGui = false)
{ {
// Declare values // Declare values
string LogDirection; string LogDirection;
@@ -56,4 +57,38 @@ internal class PerunHelper
Globals.VersionPerun = Assembly.GetExecutingAssembly().GetName().Version.ToString(); Globals.VersionPerun = Assembly.GetExecutingAssembly().GetName().Version.ToString();
return strBeginning + "v" + Globals.VersionPerun; return strBeginning + "v" + Globals.VersionPerun;
} }
public static int CheckVersions()
{
// Checks the versions of APP, DCS Hook and MySQL database
Match match = Regex.Match(Globals.VersionPerun, @"^\d+.\d+.\d+", RegexOptions.Compiled | RegexOptions.IgnoreCase);
string VersionApp = "v" + match.Groups[0].Value;
int ReturnValue = 1;
if (!String.IsNullOrEmpty(Globals.VersionDatabase))
{
// Check database
if(VersionApp != Globals.VersionDatabase)
{
// Incorrect database version
PerunHelper.AddLog(ref Globals.AppLogHistory, "ERROR Incorrect database revision : "+ Globals.VersionDatabase, 1, 1, "?");
Globals.ErrorsDatabase++;
ReturnValue = 0;
}
}
if (!String.IsNullOrEmpty(Globals.VersionDCSHook))
{
// Check database
if (VersionApp != Globals.VersionDCSHook)
{
// Incorrect dcs script version
PerunHelper.AddLog(ref Globals.AppLogHistory, "ERROR Incorrect DCS hook revision : " + Globals.VersionDCSHook, 2, 1, "?");
Globals.ErrorsGame++;
ReturnValue = 0;
}
}
return ReturnValue;
}
} }

View File

@@ -126,7 +126,7 @@ public class TCPController
if (Int32.Parse(strRawTCPFrameType) != 0) if (Int32.Parse(strRawTCPFrameType) != 0)
{ {
// Add to mySQL send buffer (find first empty slot) // Add to mySQL send buffer (find first empty slot)
PerunHelper.GUILogHistoryAdd(ref arrGUILogHistory, "Packet received" , 2,0, strRawTCPFrameType,true); PerunHelper.AddLog(ref arrGUILogHistory, "Packet received" , 2,0, strRawTCPFrameType,true);
bool AddedDataToBuffer = false; bool AddedDataToBuffer = false;
for (int i = 0; i < arrMySQLSendBuffer.Length - 1; i++) for (int i = 0; i < arrMySQLSendBuffer.Length - 1; i++)
{ {
@@ -139,12 +139,12 @@ public class TCPController
} }
if (!AddedDataToBuffer) if (!AddedDataToBuffer)
{ {
PerunHelper.GUILogHistoryAdd(ref arrGUILogHistory, "ERROR package was dropped", 1, 1, strRawTCPFrameType); PerunHelper.AddLog(ref arrGUILogHistory, "ERROR package was dropped", 1, 1, strRawTCPFrameType);
} }
} else } else
{ {
// Keep alive // Keep alive
PerunHelper.GUILogHistoryAdd(ref arrGUILogHistory, "Keep-alive received", 2,0,"0",true); PerunHelper.AddLog(ref arrGUILogHistory, "Keep-alive received", 2,0,"0",true);
} }
} }
else else
@@ -156,7 +156,7 @@ public class TCPController
{ {
Globals.ErrorsGame++; Globals.ErrorsGame++;
Console.WriteLine(e.ToString()); Console.WriteLine(e.ToString());
PerunHelper.GUILogHistoryAdd(ref arrGUILogHistory, "ERROR while message parsing , error: " + e.Message,2,1,"?"); PerunHelper.AddLog(ref arrGUILogHistory, "ERROR while message parsing , error: " + e.Message,2,1,"?");
bTCPConnectionOnline = false; bTCPConnectionOnline = false;
} }
@@ -168,7 +168,7 @@ public class TCPController
catch (SocketException e) catch (SocketException e)
{ {
Console.WriteLine(e.ToString()); Console.WriteLine(e.ToString());
PerunHelper.GUILogHistoryAdd(ref arrGUILogHistory, "TCP ERROR cannot check connection, error: " + e.Message,2,1,"?"); PerunHelper.AddLog(ref arrGUILogHistory, "TCP ERROR cannot check connection, error: " + e.Message,2,1,"?");
} }
} }
@@ -182,7 +182,7 @@ public class TCPController
{ {
Globals.ErrorsGame++; Globals.ErrorsGame++;
Console.WriteLine(e.ToString()); Console.WriteLine(e.ToString());
PerunHelper.GUILogHistoryAdd(ref arrGUILogHistory, "TCP error - connection closed or port in use, error: " + e.Message,1,1,"?"); PerunHelper.AddLog(ref arrGUILogHistory, "TCP error - connection closed or port in use, error: " + e.Message,1,1,"?");
bTCPConnectionOnline = false; bTCPConnectionOnline = false;
} }

View File

@@ -183,7 +183,7 @@ namespace Perun_v1
DatabaseConnection.DatabaseConnectionString = "server=" + con_txt_mysql_server.Text + ";user=" + con_txt_mysql_username.Text + ";database=" + con_txt_mysql_database.Text + ";port=" + con_txt_mysql_port.Text + ";password=" + con_txt_mysql_password.Text; DatabaseConnection.DatabaseConnectionString = "server=" + con_txt_mysql_server.Text + ";user=" + con_txt_mysql_username.Text + ";database=" + con_txt_mysql_database.Text + ";port=" + con_txt_mysql_port.Text + ";password=" + con_txt_mysql_password.Text;
// Start listening // Start listening
PerunHelper.GUILogHistoryAdd(ref Globals.AppLogHistory, "Opening connections",0,1); PerunHelper.AddLog(ref Globals.AppLogHistory, "Opening connections",0,1);
TCPServer.Create(Int32.Parse(con_txt_dcs_server_port.Text), ref Globals.AppLogHistory, ref DatabaseSendBuffer); TCPServer.Create(Int32.Parse(con_txt_dcs_server_port.Text), ref Globals.AppLogHistory, ref DatabaseSendBuffer);
TCPServer.thrTCPListener = new Thread(TCPServer.StartListen); TCPServer.thrTCPListener = new Thread(TCPServer.StartListen);
TCPServer.thrTCPListener.Start(); TCPServer.thrTCPListener.Start();
@@ -203,7 +203,7 @@ namespace Perun_v1
{ {
// Stop listening // Stop listening
// Prepare GUI // Prepare GUI
PerunHelper.GUILogHistoryAdd(ref Globals.AppLogHistory, "Closing connections",0,1); PerunHelper.AddLog(ref Globals.AppLogHistory, "Closing connections",0,1);
con_Button_Listen_OFF.Enabled = false; con_Button_Listen_OFF.Enabled = false;
Tim_GUI_Tick(null, null); Tim_GUI_Tick(null, null);
this.Refresh(); this.Refresh();
@@ -221,7 +221,7 @@ namespace Perun_v1
} }
catch (Exception ex) catch (Exception ex)
{ {
PerunHelper.GUILogHistoryAdd(ref Globals.AppLogHistory, "TCP ERROR, error: " + ex.Message, 2, 1, "?"); PerunHelper.AddLog(ref Globals.AppLogHistory, "TCP ERROR, error: " + ex.Message, 2, 1, "?");
Console.WriteLine(ex.ToString()); Console.WriteLine(ex.ToString());
} }
@@ -239,7 +239,7 @@ namespace Perun_v1
con_img_srs.Image = (Image)Properties.Resources.ResourceManager.GetObject("status_disconnected"); con_img_srs.Image = (Image)Properties.Resources.ResourceManager.GetObject("status_disconnected");
// Display information about closed connections // Display information about closed connections
PerunHelper.GUILogHistoryAdd(ref Globals.AppLogHistory, "Connections closed",0,1); PerunHelper.AddLog(ref Globals.AppLogHistory, "Connections closed",0,1);
Tim_GUI_Tick(null, null); Tim_GUI_Tick(null, null);
// Set title bar // Set title bar
@@ -483,6 +483,12 @@ namespace Perun_v1
// Send ping to check for possible connection issues // Send ping to check for possible connection issues
DatabaseConnection.SendToMySql("", true); DatabaseConnection.SendToMySql("", true);
if (PerunHelper.CheckVersions()==0)
{
MessageBox.Show("Version mismatch detected - please check log files and update.\n\nPerun will now terminate.", "Perun ERROR!",MessageBoxButtons.OK,MessageBoxIcon.Error);
con_Button_Listen_OFF_Click(null, null);
}
// Take care of 3rd party stuff // Take care of 3rd party stuff
string ExtSRSJson = ""; string ExtSRSJson = "";
string ExtLotATCJson = ""; string ExtLotATCJson = "";
@@ -541,12 +547,12 @@ namespace Perun_v1
ExtSRSJson = "{'type':'100','instance':'" + Int32.Parse(con_txt_dcs_instance.Text) + "','payload':{'ignore':'false'}}"; // No SRS clients connected ExtSRSJson = "{'type':'100','instance':'" + Int32.Parse(con_txt_dcs_instance.Text) + "','payload':{'ignore':'false'}}"; // No SRS clients connected
} }
ExtSRSUseDefault = false; ExtSRSUseDefault = false;
PerunHelper.GUILogHistoryAdd(ref Globals.AppLogHistory, "SRS data loaded", 3, 0, "100", true); PerunHelper.AddLog(ref Globals.AppLogHistory, "SRS data loaded", 3, 0, "100", true);
ExtSRSStatus = true; ExtSRSStatus = true;
} }
catch (Exception exc_srs) catch (Exception exc_srs)
{ {
PerunHelper.GUILogHistoryAdd(ref Globals.AppLogHistory, "SRS data ERROR , error: " + exc_srs.Message, 3, 1, "100"); PerunHelper.AddLog(ref Globals.AppLogHistory, "SRS data ERROR , error: " + exc_srs.Message, 3, 1, "100");
ExtSRSStatus = false; ExtSRSStatus = false;
Globals.ErrorsSRS++; Globals.ErrorsSRS++;
} }
@@ -572,12 +578,12 @@ namespace Perun_v1
ExtLotATCJson = "{'type':'101','instance':'" + Int32.Parse(con_txt_dcs_instance.Text) + "','payload':'" + ExtLotATCJson + "'}"; ExtLotATCJson = "{'type':'101','instance':'" + Int32.Parse(con_txt_dcs_instance.Text) + "','payload':'" + ExtLotATCJson + "'}";
ExtLotATCUseDefault = false; ExtLotATCUseDefault = false;
PerunHelper.GUILogHistoryAdd(ref Globals.AppLogHistory, "LotATC data loaded", 3, 0, "101", true); PerunHelper.AddLog(ref Globals.AppLogHistory, "LotATC data loaded", 3, 0, "101", true);
ExtLotATCStatus = true; ExtLotATCStatus = true;
} }
catch (Exception exc_lotatc) catch (Exception exc_lotatc)
{ {
PerunHelper.GUILogHistoryAdd(ref Globals.AppLogHistory, "LotATC data ERROR, error: " + exc_lotatc.Message, 3, 1, "101"); PerunHelper.AddLog(ref Globals.AppLogHistory, "LotATC data ERROR, error: " + exc_lotatc.Message, 3, 1, "101");
ExtLotATCStatus = false; ExtLotATCStatus = false;
Globals.ErrorsLotATC++; Globals.ErrorsLotATC++;
} }
@@ -616,7 +622,7 @@ namespace Perun_v1
Globals.AppForceIconReload = true; Globals.AppForceIconReload = true;
// Add information // Add information
PerunHelper.GUILogHistoryAdd(ref Globals.AppLogHistory, "Resetted error counter",0,1); PerunHelper.AddLog(ref Globals.AppLogHistory, "Resetted error counter",0,1);
} }
else if (dialogResult == DialogResult.No) else if (dialogResult == DialogResult.No)
{ {
@@ -628,7 +634,7 @@ namespace Perun_v1
private void con_Button_Add_Marker_Click(object sender, EventArgs e) private void con_Button_Add_Marker_Click(object sender, EventArgs e)
{ {
// Added user marker // Added user marker
PerunHelper.GUILogHistoryAdd(ref Globals.AppLogHistory, "User Marker",0,1); PerunHelper.AddLog(ref Globals.AppLogHistory, "User Marker",0,1);
} }
} }
} }