diff --git a/02_Windows_App/Perun_v1/01_Classes/DatabaseController.cs b/02_Windows_App/Perun_v1/01_Classes/DatabaseController.cs index c49e76f..e4e441c 100644 --- a/02_Windows_App/Perun_v1/01_Classes/DatabaseController.cs +++ b/02_Windows_App/Perun_v1/01_Classes/DatabaseController.cs @@ -39,6 +39,9 @@ public class DatabaseController if (TCPFrameType == "1") { 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 @@ -83,7 +86,7 @@ public class DatabaseController else if (TCPFrameType == "-1") { // Keep alive message - SQLQueryTxt = "SELECT 1;"; + SQLQueryTxt = "SELECT `pe_Config_payload` FROM `pe_Config` WHERE `pe_Config_id` = 1;"; } else { @@ -106,41 +109,57 @@ public class DatabaseController DatabaseStatus = true; MySqlCommand DatabaseCommand = new MySqlCommand(SQLQueryTxt, DatabaseConnection); MySqlDataReader DatabaseReader = DatabaseCommand.ExecuteReader(); + + if (DatabaseReader.HasRows) + { + while (DatabaseReader.Read()) + { + if (TCPFrameType == "-1") + { + Globals.VersionDatabase= DatabaseReader.GetString(0); + } + } + } + else + { + // Do nothing + } + DatabaseReader.Close(); switch (Int32.Parse(TCPFrameType)) { 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; 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; 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; 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; 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; 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; 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; 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; 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; case -1: break; default: - PerunHelper.GUILogHistoryAdd(ref Globals.AppLogHistory, "Data send", 1,0, TCPFrameType); + PerunHelper.AddLog(ref Globals.AppLogHistory, "Data send", 1,0, TCPFrameType); break; } @@ -149,7 +168,7 @@ public class DatabaseController { // General exception found 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++; DatabaseStatus = false; ReturnValue = 0; @@ -160,15 +179,15 @@ public class DatabaseController switch (m_ex.Number) { 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; 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; default: - PerunHelper.GUILogHistoryAdd(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.GUILogHistoryAdd(ref Globals.AppLogHistory, "ERROR MySQL - error: " + m_ex.Message,1,1, TCPFrameType); + PerunHelper.AddLog(ref Globals.AppLogHistory, "ERROR MySQL - error id: " + m_ex.Number,1,1, TCPFrameType); + PerunHelper.AddLog(ref Globals.AppLogHistory, "ERROR MySQL - query: " + SQLQueryTxt, 1, 1, TCPFrameType); + PerunHelper.AddLog(ref Globals.AppLogHistory, "ERROR MySQL - error: " + m_ex.Message,1,1, TCPFrameType); break; } Globals.ErrorsGame++; @@ -180,7 +199,7 @@ public class DatabaseController } 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; Globals.ErrorsGame++; } diff --git a/02_Windows_App/Perun_v1/01_Classes/PerunHelper.cs b/02_Windows_App/Perun_v1/01_Classes/PerunHelper.cs index 462d0d1..0617c05 100644 --- a/02_Windows_App/Perun_v1/01_Classes/PerunHelper.cs +++ b/02_Windows_App/Perun_v1/01_Classes/PerunHelper.cs @@ -1,10 +1,11 @@ // This class gathers all helper functions using System; using System.Reflection; +using System.Text.RegularExpressions; 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 string LogDirection; @@ -56,4 +57,38 @@ internal class PerunHelper Globals.VersionPerun = Assembly.GetExecutingAssembly().GetName().Version.ToString(); 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; + } } \ No newline at end of file diff --git a/02_Windows_App/Perun_v1/01_Classes/TCPController.cs b/02_Windows_App/Perun_v1/01_Classes/TCPController.cs index 3450b81..a92dfb6 100644 --- a/02_Windows_App/Perun_v1/01_Classes/TCPController.cs +++ b/02_Windows_App/Perun_v1/01_Classes/TCPController.cs @@ -126,7 +126,7 @@ public class TCPController if (Int32.Parse(strRawTCPFrameType) != 0) { // 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; for (int i = 0; i < arrMySQLSendBuffer.Length - 1; i++) { @@ -139,12 +139,12 @@ public class TCPController } 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 { // 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 @@ -156,7 +156,7 @@ public class TCPController { Globals.ErrorsGame++; 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; } @@ -168,7 +168,7 @@ public class TCPController catch (SocketException e) { 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++; 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; } diff --git a/02_Windows_App/Perun_v1/02_Forms/form_Main.cs b/02_Windows_App/Perun_v1/02_Forms/form_Main.cs index 96ea7a7..1b1baca 100644 --- a/02_Windows_App/Perun_v1/02_Forms/form_Main.cs +++ b/02_Windows_App/Perun_v1/02_Forms/form_Main.cs @@ -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; // 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.thrTCPListener = new Thread(TCPServer.StartListen); TCPServer.thrTCPListener.Start(); @@ -203,7 +203,7 @@ namespace Perun_v1 { // Stop listening // 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; Tim_GUI_Tick(null, null); this.Refresh(); @@ -221,7 +221,7 @@ namespace Perun_v1 } 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()); } @@ -239,7 +239,7 @@ namespace Perun_v1 con_img_srs.Image = (Image)Properties.Resources.ResourceManager.GetObject("status_disconnected"); // 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); // Set title bar @@ -483,6 +483,12 @@ namespace Perun_v1 // Send ping to check for possible connection issues 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 string ExtSRSJson = ""; 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 } 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; } 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; Globals.ErrorsSRS++; } @@ -572,12 +578,12 @@ namespace Perun_v1 ExtLotATCJson = "{'type':'101','instance':'" + Int32.Parse(con_txt_dcs_instance.Text) + "','payload':'" + ExtLotATCJson + "'}"; 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; } 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; Globals.ErrorsLotATC++; } @@ -616,7 +622,7 @@ namespace Perun_v1 Globals.AppForceIconReload = true; // 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) { @@ -628,7 +634,7 @@ namespace Perun_v1 private void con_Button_Add_Marker_Click(object sender, EventArgs e) { // Added user marker - PerunHelper.GUILogHistoryAdd(ref Globals.AppLogHistory, "User Marker",0,1); + PerunHelper.AddLog(ref Globals.AppLogHistory, "User Marker",0,1); } } }