Code cleanup

This commit is contained in:
VladMordock
2021-02-17 14:13:18 +01:00
parent 9683e8e57e
commit f1b4fdcf9c
4 changed files with 38 additions and 14 deletions

View File

@@ -143,19 +143,23 @@ public class DatabaseController
// Add parameters - prevent SQL injection
if (TCPFrameType == "50")
{
// Chat entry
DatabaseCommand.Parameters.AddWithValue("@PAR_payload_player", TCPFrame.payload.player);
DatabaseCommand.Parameters.AddWithValue("@PAR_payload_msg", TCPFrame.payload.msg);
}
else if (TCPFrameType == "51")
{
// Event entry
DatabaseCommand.Parameters.AddWithValue("@PAR_log_content", TCPFrame.payload.log_content);
}
else if (TCPFrameType == "53")
{
// Stats entry
DatabaseCommand.Parameters.AddWithValue("@PAR_login_name", TCPFrame.payload.login_name);
}
else
{
// Others
TCPFramePayload = JsonConvert.SerializeObject(TCPFrame.payload); // Deserialize payload
DatabaseCommand.Parameters.AddWithValue("@PAR_TCPFramePayload", TCPFramePayload);
}
@@ -179,6 +183,7 @@ public class DatabaseController
DatabaseReader.Close();
// Add information to log
switch (Int32.Parse(TCPFrameType))
{
case 1:
@@ -230,13 +235,13 @@ public class DatabaseController
// MySQL exception found
switch (m_ex.Number)
{
case 1042: // Unable to connect to any of the specified MySQL hosts (Check Server,Port)
PerunHelper.LogError(ref Globals.AppLogHistory, "ERROR MySQL - unable to connect, error: " + m_ex.Message,1,1, TCPFrameType);
case 0: // Access denied (Check DB name,username,password)
PerunHelper.LogError(ref Globals.AppLogHistory, "ERROR MySQL - access denied, error: " + m_ex.Message, 1, 1, TCPFrameType);
DatabaseStatus = false;
ReturnValue = 0;
break;
case 0: // Access denied (Check DB name,username,password)
PerunHelper.LogError(ref Globals.AppLogHistory, "ERROR MySQL - access denied, error: " + m_ex.Message,1,1, TCPFrameType);
case 1042: // Unable to connect to any of the specified MySQL hosts (Check Server,Port)
PerunHelper.LogError(ref Globals.AppLogHistory, "ERROR MySQL - unable to connect, error: " + m_ex.Message,1,1, TCPFrameType);
DatabaseStatus = false;
ReturnValue = 0;
break;

View File

@@ -5,18 +5,22 @@
class CurrentMissionClass
{
public string Theatre = "";
public string Mission = "";
public string Pause = "";
public int PlayerCount = 0;
public string Theatre = ""; // Mission theathre
public string Mission = ""; // Mission name
public string Pause = ""; // Mission pause
public int PlayerCount = 0; // Actual player time
public string ToInfoString()
{
// Return mission information as string
if (this.Mission != "")
{
// Return mission information as string
return "Mission: " + this.Mission + "(" + this.Theatre + ") Pause:" + this.Pause + " Players: " + this.PlayerCount;
} else
{
// Mission information is not available
return "Mission: Unknown";
}
}
@@ -35,6 +39,7 @@ internal class Globals
public static bool StatusLotATC = false; // Historic lotatc connection status
public static bool StatusHistoryConnection = false; // Historic tcp connection status
public static bool StatusConnection = false; // Flag if is TCP connectionstill alive
public static int ErrorsDatabase = 0; // MySQL - Error counter
public static int ErrorsGame = 0; // TCP connection - Error counter
public static int ErrorsHistoryGame = 0; // TCP connection - historic value of Error counter
@@ -44,6 +49,7 @@ internal class Globals
public static string VersionDCSHook = ""; // Version - DCS hook
public static string VersionDatabase = ""; // Version - Database
public static string VersionPerun = "DEBUG"; // Version - Perun
public static CurrentMissionClass CurrentMission = new CurrentMissionClass(); // Mission - name
public static CurrentMissionClass CurrentMission = new CurrentMissionClass(); // Actual mission information
}

View File

@@ -1,39 +1,44 @@
using System;
using System.IO;
// Singleton class - log controller
class LogController
{
private static LogController _instance = new LogController();
private static LogController _instance = new LogController(); // Singleton instance
public static LogController instance
{
get
{
return _instance;
return _instance; // Return current instance
}
}
public int level
{
get;
set;
get; // Get debug level
set; // Set debug level
}
// Log error information
public void LogError(string strLog)
{
this.WriteLog(0, strLog);
}
// Log warning information
public void LogWarning(string strLog)
{
this.WriteLog(1, strLog);
}
// Log info information
public void LogInfo(string strLog)
{
this.WriteLog(2, strLog);
}
// Log debug information
public void LogDebug(string strLog)
{
this.WriteLog(3, strLog);
@@ -79,5 +84,7 @@ class LogController
{
// Do nothing
}
// TBD: log rotation
}
}

View File

@@ -5,26 +5,31 @@ using System.Text.RegularExpressions;
internal class PerunHelper
{
// Add error infomation
public static void LogError(ref string[] arrLogHistory, string strEntryToAdd, int intDirection = 0, int intMarker = 0, string strType = " ", bool bSkipGui = false)
{
AddLog(0, ref arrLogHistory, strEntryToAdd, intDirection, intMarker, strType, bSkipGui);
}
// Add warning information
public static void LogWarning(ref string[] arrLogHistory, string strEntryToAdd, int intDirection = 0, int intMarker = 0, string strType = " ", bool bSkipGui = false)
{
AddLog(1, ref arrLogHistory, strEntryToAdd, intDirection, intMarker, strType, bSkipGui);
}
// Add info information
public static void LogInfo(ref string[] arrLogHistory, string strEntryToAdd, int intDirection = 0, int intMarker = 0, string strType = " ", bool bSkipGui = false)
{
AddLog(2, ref arrLogHistory, strEntryToAdd, intDirection, intMarker, strType, bSkipGui);
}
// Add debug information
public static void LogDebug(ref string[] arrLogHistory, string strEntryToAdd, int intDirection = 0, int intMarker = 0, string strType = " ", bool bSkipGui = false)
{
AddLog(3, ref arrLogHistory, strEntryToAdd, intDirection, intMarker, strType, bSkipGui);
}
// Add log
private static void AddLog(int logLevel, ref string[] arrLogHistory, string strEntryToAdd, int intDirection = 0, int intMarker = 0, string strType = " ", bool bSkipGui = false)
{
// Declare values
@@ -71,15 +76,16 @@ internal class PerunHelper
LogController.instance.WriteLog(logLevel, DateTime.Now.ToString("yyyy-MM-dd ") + " " + DateTime.Now.ToString("HH:mm:ss") + " | Instance: "+ Globals.AppInstanceID + " | " + LogMarker + " | "+ LogDirection + " | "+ strType + " | " + strEntryToAdd);
}
// Gets build version
public static string GetAppVersion(string strBeginning)
{
// Gets build version
Globals.VersionPerun = Assembly.GetExecutingAssembly().GetName().Version.ToString();
return strBeginning + "v" + Globals.VersionPerun;
}
public static int CheckVersions()
{
// Checks if all versions (mysql, winapp, lua) are the same - ommit if run as debug build
#if !DEBUG
// Checks the versions of APP, DCS Hook and MySQL database
Match match = Regex.Match(Globals.VersionPerun, @"^\d+.\d+.\d+", RegexOptions.Compiled | RegexOptions.IgnoreCase);