mirror of
https://github.com/DaKerboul/perun.git
synced 2026-08-09 19:55:40 +02:00
v0.4.0 major code cleanup
This commit is contained in:
125
02_Windows_App/Perun_v1/01_Classes/DatabaseController.cs
Normal file
125
02_Windows_App/Perun_v1/01_Classes/DatabaseController.cs
Normal file
@@ -0,0 +1,125 @@
|
||||
// This class handles MySQL communication
|
||||
using MySql.Data.MySqlClient;
|
||||
using Newtonsoft.Json;
|
||||
using System;
|
||||
|
||||
internal class DatabaseController
|
||||
{
|
||||
public static string strMySQLConnectionString; // MySQL connection string
|
||||
|
||||
public static void SendToMySql(string strRawUDPFrame)
|
||||
{
|
||||
// Main function to send data to mysql
|
||||
dynamic strUDPFrame = JsonConvert.DeserializeObject(strRawUDPFrame); // Deserialize raw data
|
||||
string strUDPFrameType = strUDPFrame.type;
|
||||
string strUDPFrameTimestamp = strUDPFrame.timestamp;
|
||||
string strUDPFramePayload = "";
|
||||
string strSQLQueryTxt = "";
|
||||
|
||||
|
||||
// Some frames may come without timestamp, use database currrent timestampe then
|
||||
if (strUDPFrameTimestamp != null)
|
||||
{
|
||||
strUDPFrameTimestamp = "'" + strUDPFrameTimestamp + "'";
|
||||
}
|
||||
else
|
||||
{
|
||||
strUDPFrameTimestamp = "CURRENT_TIMESTAMP()";
|
||||
}
|
||||
|
||||
// Modify specific types
|
||||
if (strUDPFrameType == "1")
|
||||
{
|
||||
strUDPFrame.payload["v_win"] = "v" + Globals.strPerunVersion; // Inject app version information
|
||||
}
|
||||
|
||||
// Specific SQL
|
||||
if (strUDPFrameType == "50")
|
||||
{
|
||||
// Add entry to chat log
|
||||
strSQLQueryTxt = "INSERT INTO `pe_DataPlayers` (`pe_DataPlayers_ucid`) SELECT '" + strUDPFrame.payload.ucid + "' FROM DUAL WHERE NOT EXISTS (SELECT * FROM `pe_DataPlayers` where `pe_DataPlayers_ucid` = '" + strUDPFrame.payload.ucid + "' );";
|
||||
strSQLQueryTxt += "UPDATE `pe_DataPlayers` SET `pe_DataPlayers_updated` = " + strUDPFrameTimestamp + ",`pe_DataPlayers_lastname`='" + strUDPFrame.payload.player + "' WHERE `pe_DataPlayers_ucid`='" + strUDPFrame.payload.ucid + "' ;";
|
||||
strSQLQueryTxt += "INSERT INTO `pe_DataMissionHashes` (`pe_DataMissionHashes_hash`) SELECT '" + strUDPFrame.payload.missionhash + "' FROM DUAL WHERE NOT EXISTS (SELECT * FROM `pe_DataMissionHashes` where `pe_DataMissionHashes_hash` ='" + strUDPFrame.payload.missionhash + "' );";
|
||||
strSQLQueryTxt += "UPDATE `pe_DataMissionHashes` SET `pe_DataMissionHashes_datetime` = " + strUDPFrameTimestamp + " WHERE `pe_DataMissionHashes_hash` = '" + strUDPFrame.payload.missionhash + "';";
|
||||
strSQLQueryTxt += "INSERT INTO `pe_LogChat` (`pe_LogChat_id`,`pe_LogChat_datetime`, `pe_LogChat_playerid`, `pe_LogChat_msg`, `pe_LogChat_all`,`pe_LogChat_missionhash_id`) VALUES (NULL,'" + strUDPFrame.payload.datetime + "', (SELECT `pe_DataPlayers_id` from `pe_DataPlayers` WHERE `pe_DataPlayers_ucid` = '" + strUDPFrame.payload.ucid + "'), '" + strUDPFrame.payload.msg + "', '" + strUDPFrame.payload.all + "',(SELECT `pe_DataMissionHashes_id` FROM `pe_DataMissionHashes` WHERE `pe_DataMissionHashes_hash` = '" + strUDPFrame.payload.missionhash + "'));";
|
||||
}
|
||||
else if (strUDPFrameType == "51")
|
||||
{
|
||||
// Add entry to event log
|
||||
strSQLQueryTxt = "INSERT INTO `pe_DataMissionHashes` (`pe_DataMissionHashes_hash`) SELECT '" + strUDPFrame.payload.log_missionhash + "' FROM DUAL WHERE NOT EXISTS (SELECT * FROM `pe_DataMissionHashes` where `pe_DataMissionHashes_hash` = '" + strUDPFrame.payload.log_missionhash + "');";
|
||||
strSQLQueryTxt += "UPDATE `pe_DataMissionHashes` SET `pe_DataMissionHashes_datetime` = " + strUDPFrameTimestamp + " WHERE `pe_DataMissionHashes_hash` = '" + strUDPFrame.payload.log_missionhash + "';";
|
||||
strSQLQueryTxt += "INSERT INTO `pe_LogEvent` (`pe_LogEvent_id`, `pe_LogEvent_datetime`, `pe_LogEvent_type`, `pe_LogEvent_content`,`pe_LogEvent_missionhash_id`) VALUES ( NULL, '" + strUDPFrame.payload.log_datetime + "', '" + strUDPFrame.payload.log_type + "', '" + strUDPFrame.payload.log_content + "', (SELECT `pe_DataMissionHashes_id` FROM `pe_DataMissionHashes` WHERE `pe_DataMissionHashes_hash` = '" + strUDPFrame.payload.log_missionhash + "'));";
|
||||
}
|
||||
else if (strUDPFrameType == "52")
|
||||
{
|
||||
// Update user stats
|
||||
strUDPFramePayload = JsonConvert.SerializeObject(strUDPFrame.payload.stat_data); // Deserialize payload
|
||||
|
||||
strSQLQueryTxt = "INSERT INTO `pe_DataMissionHashes` (`pe_DataMissionHashes_hash`) SELECT '" + strUDPFrame.payload.stat_missionhash + "' FROM DUAL WHERE NOT EXISTS (SELECT * FROM `pe_DataMissionHashes` where `pe_DataMissionHashes_hash` = '" + strUDPFrame.payload.stat_missionhash + "');";
|
||||
strSQLQueryTxt += "UPDATE `pe_DataMissionHashes` SET `pe_DataMissionHashes_datetime` = " + strUDPFrameTimestamp + " WHERE `pe_DataMissionHashes_hash` = '" + strUDPFrame.payload.stat_missionhash + "';";
|
||||
strSQLQueryTxt += "INSERT INTO `pe_DataPlayers` (`pe_DataPlayers_ucid`) SELECT '" + strUDPFrame.payload.stat_ucid + "' FROM DUAL WHERE NOT EXISTS (SELECT * FROM `pe_DataPlayers` where `pe_DataPlayers_ucid` = '" + strUDPFrame.payload.stat_ucid + "');";
|
||||
strSQLQueryTxt += "UPDATE `pe_DataPlayers` SET `pe_DataPlayers_updated`=" + strUDPFrameTimestamp + " WHERE `pe_DataPlayers_ucid`='" + strUDPFrame.payload.stat_ucid + "';";
|
||||
strSQLQueryTxt += "INSERT INTO `pe_LogStats` (`pe_LogStats_playerid`,`pe_LogStats_missionhash_id`) SELECT (SELECT `pe_DataPlayers_id` from `pe_DataPlayers` WHERE `pe_DataPlayers_ucid` = '" + strUDPFrame.payload.stat_ucid + "'), (SELECT `pe_DataMissionHashes_id` FROM `pe_DataMissionHashes` WHERE `pe_DataMissionHashes_hash` = '" + strUDPFrame.payload.stat_missionhash + "') FROM DUAL WHERE NOT EXISTS (SELECT * FROM `pe_LogStats` WHERE `pe_LogStats_missionhash_id`=(SELECT `pe_DataMissionHashes_id` FROM `pe_DataMissionHashes` WHERE `pe_DataMissionHashes_hash` = '" + strUDPFrame.payload.stat_missionhash + "') AND `pe_LogStats_playerid` = (SELECT `pe_DataPlayers_id` from `pe_DataPlayers` WHERE `pe_DataPlayers_ucid` = '" + strUDPFrame.payload.stat_ucid + "') );";
|
||||
strSQLQueryTxt += "UPDATE `pe_LogStats` SET `pe_LogStats_datetime`='" + strUDPFrame.payload.stat_datetime + "',`pe_LogStats_playerid` = (SELECT `pe_DataPlayers_id` from `pe_DataPlayers` WHERE `pe_DataPlayers_ucid` = '" + strUDPFrame.payload.stat_ucid + "'),`pe_LogStats_debug`=JSON_QUOTE('" + strUDPFramePayload + "'),`pe_LogStats_missionhash_id`=(SELECT `pe_DataMissionHashes_id` FROM `pe_DataMissionHashes` WHERE `pe_DataMissionHashes_hash` = '" + strUDPFrame.payload.stat_missionhash + "') WHERE `pe_LogStats_missionhash_id`=(SELECT `pe_DataMissionHashes_id` FROM `pe_DataMissionHashes` WHERE `pe_DataMissionHashes_hash` = '" + strUDPFrame.payload.stat_missionhash + "') AND `pe_LogStats_playerid` = (SELECT `pe_DataPlayers_id` from `pe_DataPlayers` WHERE `pe_DataPlayers_ucid` = '" + strUDPFrame.payload.stat_ucid + "');";
|
||||
}
|
||||
else if (strUDPFrameType == "53")
|
||||
{
|
||||
// User logged in to DCS server
|
||||
|
||||
strSQLQueryTxt = "INSERT INTO `pe_DataPlayers` (`pe_DataPlayers_ucid`) SELECT '" + strUDPFrame.payload.login_ucid + "' FROM DUAL WHERE NOT EXISTS (SELECT * FROM `pe_DataPlayers` where pe_DataPlayers_ucid='" + strUDPFrame.payload.login_ucid + "');";
|
||||
strSQLQueryTxt += "UPDATE `pe_DataPlayers` SET pe_DataPlayers_lastip='" + strUDPFrame.payload.login_ipaddr + "', pe_DataPlayers_lastname='" + strUDPFrame.payload.login_name + "',pe_DataPlayers_updated='" + strUDPFrame.payload.login_datetime + "' WHERE `pe_DataPlayers_ucid`= '" + strUDPFrame.payload.login_ucid + "';";
|
||||
strSQLQueryTxt += "INSERT INTO `pe_LogLogins` (`pe_LogLogins_datetime`, `pe_LogLogins_playerid`, `pe_LogLogins_name`, `pe_LogLogins_ip`) VALUES ('" + strUDPFrame.payload.login_datetime + "', (SELECT pe_DataPlayers_id from pe_DataPlayers WHERE pe_DataPlayers_ucid = '" + strUDPFrame.payload.login_ucid + "'), '" + strUDPFrame.payload.login_name + "', '" + strUDPFrame.payload.login_ipaddr + "');";
|
||||
}
|
||||
else
|
||||
{
|
||||
// General definition used for 1-10 type packets
|
||||
strUDPFramePayload = JsonConvert.SerializeObject(strUDPFrame.payload); // Deserialize payload
|
||||
|
||||
strSQLQueryTxt = "INSERT INTO `pe_DataRaw` (`pe_dataraw_type`) SELECT '" + strUDPFrameType + "' FROM DUAL WHERE NOT EXISTS (SELECT * FROM `pe_DataRaw` WHERE pe_dataraw_type = '" + strUDPFrameType + "' );";
|
||||
strSQLQueryTxt += "UPDATE `pe_DataRaw` SET `pe_dataraw_payload` = JSON_QUOTE('" + strUDPFramePayload + "'), `pe_dataraw_updated`=" + strUDPFrameTimestamp + " WHERE `pe_dataraw_type`=" + strUDPFrameType + ";";
|
||||
}
|
||||
|
||||
// Connect to mysql and execute sql
|
||||
MySqlConnection connMySQL = new MySqlConnection(DatabaseController.strMySQLConnectionString);
|
||||
try
|
||||
{
|
||||
Console.WriteLine("Sending data to MySQL - Begin");
|
||||
connMySQL.Open();
|
||||
|
||||
MySqlCommand cmdMySQL = new MySqlCommand(strSQLQueryTxt, connMySQL);
|
||||
MySqlDataReader rdrMySQL = cmdMySQL.ExecuteReader();
|
||||
|
||||
while (rdrMySQL.Read())
|
||||
{
|
||||
Console.WriteLine(rdrMySQL[0] + " -- " + rdrMySQL[1]);
|
||||
}
|
||||
rdrMySQL.Close();
|
||||
PerunHelper.LogHistoryAdd(ref Globals.arrLogHistory, "MySQL updated, package type: " + strUDPFrameType);
|
||||
}
|
||||
catch (ArgumentException a_ex)
|
||||
{
|
||||
// General exception found
|
||||
Console.WriteLine(a_ex.ToString());
|
||||
PerunHelper.LogHistoryAdd(ref Globals.arrLogHistory, "ERROR MySQL - package type: " + strUDPFrameType);
|
||||
}
|
||||
catch (MySqlException m_ex)
|
||||
{
|
||||
// MySQL exception found
|
||||
PerunHelper.LogHistoryAdd(ref Globals.arrLogHistory, "ERROR MySQL - package type: " + strUDPFrameType);
|
||||
switch (m_ex.Number)
|
||||
{
|
||||
case 1042: // Unable to connect to any of the specified MySQL hosts (Check Server,Port)
|
||||
PerunHelper.LogHistoryAdd(ref Globals.arrLogHistory, "ERROR MySQL - unable to connect");
|
||||
break;
|
||||
case 0: // Access denied (Check DB name,username,password)
|
||||
PerunHelper.LogHistoryAdd(ref Globals.arrLogHistory, "ERROR MySQL - access denied");
|
||||
break;
|
||||
default:
|
||||
PerunHelper.LogHistoryAdd(ref Globals.arrLogHistory, "ERROR MySQL - " + m_ex.Number);
|
||||
break;
|
||||
}
|
||||
}
|
||||
connMySQL.Close();
|
||||
Console.WriteLine("Sending data to MySQL - Done");
|
||||
}
|
||||
}
|
||||
7
02_Windows_App/Perun_v1/01_Classes/Globals.cs
Normal file
7
02_Windows_App/Perun_v1/01_Classes/Globals.cs
Normal file
@@ -0,0 +1,7 @@
|
||||
// This class gathers all global variables
|
||||
|
||||
internal class Globals
|
||||
{
|
||||
public static string strPerunVersion = "DEBUG"; // Helper for pulling version definition
|
||||
public static string[] arrLogHistory = new string[10]; // Log history for GUI
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
// this class just wraps some Win32 stuff that we're going to use for blocking of multiple instances
|
||||
// This class just wraps some Win32 stuff that we're going to use for blocking of multiple instances
|
||||
using System;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
|
||||
26
02_Windows_App/Perun_v1/01_Classes/PerunHelper.cs
Normal file
26
02_Windows_App/Perun_v1/01_Classes/PerunHelper.cs
Normal file
@@ -0,0 +1,26 @@
|
||||
// This class gathers all helper functions
|
||||
using System;
|
||||
|
||||
internal class PerunHelper
|
||||
{
|
||||
public static void LogHistoryAdd(ref string[] arrLogHistory, string strEntryToAdd)
|
||||
{
|
||||
// Add entry to log history and rotate
|
||||
for (int i = 0; i < arrLogHistory.Length - 1; i++)
|
||||
{
|
||||
arrLogHistory[i] = arrLogHistory[i + 1]; // Shift one down
|
||||
}
|
||||
|
||||
arrLogHistory[arrLogHistory.Length - 1] = DateTime.Now.ToString("HH:mm:ss") + " > " + strEntryToAdd; // Add entry at the last position
|
||||
}
|
||||
public static string GetAppVersion(string strBeginning)
|
||||
{
|
||||
// Gets build version
|
||||
if (System.Deployment.Application.ApplicationDeployment.IsNetworkDeployed)
|
||||
{
|
||||
System.Deployment.Application.ApplicationDeployment cd = System.Deployment.Application.ApplicationDeployment.CurrentDeployment;
|
||||
Globals.strPerunVersion = cd.CurrentVersion.ToString();
|
||||
}
|
||||
return strBeginning+"v" + Globals.strPerunVersion;
|
||||
}
|
||||
}
|
||||
@@ -4,45 +4,44 @@ using System.Windows.Forms;
|
||||
|
||||
namespace Perun_v1
|
||||
{
|
||||
|
||||
static class Program
|
||||
{
|
||||
/// <summary>
|
||||
/// Główny punkt wejścia dla aplikacji.
|
||||
/// Main entry point to app
|
||||
/// </summary>
|
||||
|
||||
// Mutex to block multiple instances
|
||||
static Mutex mutex = new Mutex(true, "{5a710507-92e9-4664-9b91-918b8b82f107}");
|
||||
|
||||
[STAThread]
|
||||
|
||||
static void Main()
|
||||
{
|
||||
// Check if no other instances running
|
||||
if (mutex.WaitOne(TimeSpan.Zero, true))
|
||||
{
|
||||
// Only instance
|
||||
Application.EnableVisualStyles();
|
||||
Application.SetCompatibleTextRenderingDefault(false);
|
||||
Application.ApplicationExit += new EventHandler(Application_ApplicationExit);
|
||||
|
||||
void Application_ApplicationExit(object sender, EventArgs e)
|
||||
{
|
||||
Properties.Settings.Default.Save();
|
||||
Properties.Settings.Default.Save(); // We will save settings on exit
|
||||
}
|
||||
Application.Run(new form_Main());
|
||||
mutex.ReleaseMutex();
|
||||
Application.Run(new form_Main()); // Run main form
|
||||
mutex.ReleaseMutex(); // Release mutex
|
||||
}
|
||||
else
|
||||
{
|
||||
// send our Win32 message to make the currently running instance
|
||||
// jump on top of all the other windows
|
||||
// Multiple instance - send our Win32 message to make the currently running instance jump to front
|
||||
NativeMethods.PostMessage(
|
||||
(IntPtr)NativeMethods.HWND_BROADCAST,
|
||||
NativeMethods.WM_SHOWME,
|
||||
IntPtr.Zero,
|
||||
IntPtr.Zero);
|
||||
MessageBox.Show("Only one instance is allowed.");
|
||||
MessageBox.Show("Only one Perun instance is allowed.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
91
02_Windows_App/Perun_v1/01_Classes/UDPController.cs
Normal file
91
02_Windows_App/Perun_v1/01_Classes/UDPController.cs
Normal file
@@ -0,0 +1,91 @@
|
||||
// This class handles UDP communication
|
||||
using Newtonsoft.Json;
|
||||
using System;
|
||||
using System.Net;
|
||||
using System.Net.Sockets;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Windows.Forms;
|
||||
|
||||
internal class UDPController
|
||||
{
|
||||
// Main class for UDP listener
|
||||
public static int intListenPort; // Port to listen at
|
||||
public static bool boolDone; // Helper to exit main loop without killing thread
|
||||
public static UdpClient udpListener; // Listener object
|
||||
public static string[] arrLogHistory; // Log history for GUI
|
||||
public static string[] arrSendBuffer; // Mysql send buffer
|
||||
public static Thread thrUDPListener; // Seperate thread for UDP
|
||||
|
||||
public static void Create(int intListenPort, ref string[] arrLogHistory, ref string[] arrSendBuffer)
|
||||
{
|
||||
// Create class
|
||||
UDPController.intListenPort = intListenPort;
|
||||
UDPController.arrLogHistory = arrLogHistory;
|
||||
UDPController.arrSendBuffer = arrSendBuffer;
|
||||
}
|
||||
|
||||
public static void StopListen()
|
||||
{
|
||||
// FInish listening
|
||||
UDPController.boolDone = true;
|
||||
UDPController.udpListener.Close();
|
||||
UDPController.udpListener = null;
|
||||
|
||||
for (int i = 0; i < arrSendBuffer.Length - 1; i++)
|
||||
{
|
||||
arrSendBuffer[i] = null;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static void StartListen()
|
||||
{
|
||||
Console.WriteLine("UDP Listen start");
|
||||
try
|
||||
{
|
||||
// Start listening to UDP
|
||||
|
||||
udpListener = new UdpClient(intListenPort);
|
||||
IPEndPoint ipendpointGroupEP = new IPEndPoint(IPAddress.Loopback, intListenPort);
|
||||
string strReceivedData;
|
||||
byte[] arrReceiveByteArray;
|
||||
|
||||
// Start the main loop
|
||||
UDPController.boolDone = false;
|
||||
while (!UDPController.boolDone)
|
||||
{
|
||||
// Start listening
|
||||
Console.WriteLine("UDP: Waiting for packet");
|
||||
arrReceiveByteArray = udpListener.Receive(ref ipendpointGroupEP);
|
||||
strReceivedData = Encoding.ASCII.GetString(arrReceiveByteArray, 0, arrReceiveByteArray.Length);
|
||||
Console.WriteLine("Sender: {0} Payload: {1}", ipendpointGroupEP.ToString(), strReceivedData);
|
||||
|
||||
// Add to log history and rotate
|
||||
dynamic dynamicRawUDPFrame = JsonConvert.DeserializeObject(strReceivedData); // Deserialize received frame
|
||||
string strRawUDPFrameType = dynamicRawUDPFrame.type;
|
||||
PerunHelper.LogHistoryAdd(ref arrLogHistory, "UDP packet received, type: " + strRawUDPFrameType);
|
||||
|
||||
// Add to mySQL send buffer (find first empty slot)
|
||||
for (int i = 0; i < arrSendBuffer.Length - 1; i++)
|
||||
{
|
||||
if (arrSendBuffer[i] == null)
|
||||
{
|
||||
arrSendBuffer[i] = strReceivedData;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
udpListener.Close(); // Close port
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
// General exception found
|
||||
if (e.HResult != -2147467259) {
|
||||
Console.WriteLine(e.ToString());
|
||||
PerunHelper.LogHistoryAdd(ref arrLogHistory, "ERROR UDP - port may be in use");
|
||||
}
|
||||
}
|
||||
Console.WriteLine("UDP listen stop");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user