Advertisement
PapaHarni

Untitled

Nov 14th, 2017
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.75 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using System.Timers;
  9. using MySql.Data.MySqlClient;
  10. using static System.Net.Mime.MediaTypeNames;
  11.  
  12. namespace MCStartUpper
  13. {
  14.     class MCStartUpper
  15.     {
  16.         private static MCStartUpper _instance;
  17.         private Configuration _conf = null;
  18.         private Database _db = null;
  19.         private Timer _t = null;
  20.  
  21.         private static void Main(string[] args)
  22.         {
  23.             _instance = new MCStartUpper();
  24.             _instance.Start();
  25.             Console.ReadLine();
  26.         }
  27.  
  28.         public void Start()
  29.         {
  30.             checkVariables();
  31.             checkTimer();
  32.             checkServer();
  33.         }
  34.  
  35.         private void checkTimer()
  36.         {
  37.             if (_t == null || !_t.Enabled)
  38.             {
  39.                 _t = new Timer();
  40.                 _t.AutoReset = true;
  41.                 _t.Interval = ((double)_conf.getInt("check.every.seconds") * 1000);
  42.                 _t.Elapsed += runTimer;
  43.                 _t.Start();
  44.             }
  45.         }
  46.  
  47.         private void checkVariables()
  48.         {
  49.             if (_conf == null)
  50.             {
  51.                 _conf = new Configuration();
  52.                 if (!_conf.ConfigurationTested())
  53.                     throw new Exception("Bitte bearbeite die config.ini und starte danach erneut.");
  54.             }
  55.  
  56.             if (_db == null || _db.Connection == null)
  57.             {
  58.                 _db = new Database();
  59.                 if (!_db.TestConnection())
  60.                     throw new Exception("Konnte keine Verbindung zur Datenbank aufbauen.");
  61.             }
  62.         }
  63.  
  64.         public void checkServer()
  65.         {
  66.             checkVariables();
  67.             Console.WriteLine("[" + DateTime.Now.ToString("HH:mm:ss") + "] Prüfe Server Status");
  68.  
  69.             MySqlCommand res = new MySqlCommand("SELECT * FROM `servers` WHERE `category` = @cat AND `autostart` = '1'", _db.Connection);
  70.             res.Parameters.AddWithValue("@cat", _conf.getString("check.start.category"));
  71.             MySqlDataReader read = res.ExecuteReader();
  72.  
  73.             while (read.Read())
  74.             {
  75.                 if (read.GetInt32("processid") > 0 && ProcessRunning(read.GetInt32("processid")))
  76.                     continue;
  77.  
  78.                 if (!File.Exists(read.GetString("java")))
  79.                 {
  80.                     Console.WriteLine("Konnte java auf dem angegebenen Pfad ( " + read.GetString("java") + " ) bei " + read.GetString("server") + " nicht finden.");
  81.                 }
  82.  
  83.                 //Set Process Informations
  84.                 ProcessStartInfo psi = new ProcessStartInfo(read.GetString("java"));
  85.                 psi.Arguments = read.GetString("parameter1") + " -jar " + read.GetString("jar") + " " + read.GetString("parameter2");
  86.                 psi.WindowStyle = ProcessWindowStyle.Minimized;
  87.  
  88.                 if (read.GetString("workMap") != "none")
  89.                 {
  90.                     File.Delete(read.GetString("path") + "\\" + read.GetString("workMap"));
  91.                     File.Copy(read.GetString("path") + "\\" + read.GetString("originalMap"), read.GetString("path") + "\\" + read.GetString("workMap"));
  92.                 }
  93.  
  94.                 psi.WorkingDirectory = read.GetString("path");
  95.                 psi.UseShellExecute = true;
  96.                 Process p = Process.Start(psi);
  97.  
  98.                 //Save Process Id to Database after start
  99.                 MySqlCommand saveId = new MySqlCommand("UPDATE `servers` SET `processid` = @id WHERE `server` = @server", _db.Connection);
  100.                 saveId.Parameters.AddWithValue("@id", p.Id);
  101.                 saveId.Parameters.AddWithValue("@server", read.GetString("server"));
  102.                 saveId.ExecuteNonQuery();
  103.                 Console.WriteLine("Server " + read.GetString("server") + " wurde gestartet.");
  104.             }
  105.         }
  106.  
  107.         public bool ProcessRunning(int id)
  108.         {
  109.             try
  110.             {
  111.                 Process pc = Process.GetProcessById(id);
  112.                 if (pc == null)
  113.                     return false;
  114.  
  115.                 if (pc.HasExited)
  116.                     return false;
  117.             }
  118.             catch (Exception ex)
  119.             {
  120.                 return false;
  121.             }
  122.             return true;
  123.         }
  124.  
  125.         public Configuration getConfig()
  126.         {
  127.             return _conf;
  128.         }
  129.  
  130.         public Database getDB()
  131.         {
  132.             return _db;
  133.         }
  134.  
  135.         public static MCStartUpper getInstance()
  136.         {
  137.             return _instance;
  138.         }
  139.  
  140.         private void runTimer(Object s, ElapsedEventArgs e)
  141.         {
  142.             checkServer();
  143.             checkTimer();
  144.         }
  145.     }
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement