Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Timers;
- using MySql.Data.MySqlClient;
- using static System.Net.Mime.MediaTypeNames;
- namespace MCStartUpper
- {
- class MCStartUpper
- {
- private static MCStartUpper _instance;
- private Configuration _conf = null;
- private Database _db = null;
- private Timer _t = null;
- private static void Main(string[] args)
- {
- _instance = new MCStartUpper();
- _instance.Start();
- Console.ReadLine();
- }
- public void Start()
- {
- checkVariables();
- checkTimer();
- checkServer();
- }
- private void checkTimer()
- {
- if (_t == null || !_t.Enabled)
- {
- _t = new Timer();
- _t.AutoReset = true;
- _t.Interval = ((double)_conf.getInt("check.every.seconds") * 1000);
- _t.Elapsed += runTimer;
- _t.Start();
- }
- }
- private void checkVariables()
- {
- if (_conf == null)
- {
- _conf = new Configuration();
- if (!_conf.ConfigurationTested())
- throw new Exception("Bitte bearbeite die config.ini und starte danach erneut.");
- }
- if (_db == null || _db.Connection == null)
- {
- _db = new Database();
- if (!_db.TestConnection())
- throw new Exception("Konnte keine Verbindung zur Datenbank aufbauen.");
- }
- }
- public void checkServer()
- {
- checkVariables();
- Console.WriteLine("[" + DateTime.Now.ToString("HH:mm:ss") + "] Prüfe Server Status");
- MySqlCommand res = new MySqlCommand("SELECT * FROM `servers` WHERE `category` = @cat AND `autostart` = '1'", _db.Connection);
- res.Parameters.AddWithValue("@cat", _conf.getString("check.start.category"));
- MySqlDataReader read = res.ExecuteReader();
- while (read.Read())
- {
- if (read.GetInt32("processid") > 0 && ProcessRunning(read.GetInt32("processid")))
- continue;
- if (!File.Exists(read.GetString("java")))
- {
- Console.WriteLine("Konnte java auf dem angegebenen Pfad ( " + read.GetString("java") + " ) bei " + read.GetString("server") + " nicht finden.");
- }
- //Set Process Informations
- ProcessStartInfo psi = new ProcessStartInfo(read.GetString("java"));
- psi.Arguments = read.GetString("parameter1") + " -jar " + read.GetString("jar") + " " + read.GetString("parameter2");
- psi.WindowStyle = ProcessWindowStyle.Minimized;
- if (read.GetString("workMap") != "none")
- {
- File.Delete(read.GetString("path") + "\\" + read.GetString("workMap"));
- File.Copy(read.GetString("path") + "\\" + read.GetString("originalMap"), read.GetString("path") + "\\" + read.GetString("workMap"));
- }
- psi.WorkingDirectory = read.GetString("path");
- psi.UseShellExecute = true;
- Process p = Process.Start(psi);
- //Save Process Id to Database after start
- MySqlCommand saveId = new MySqlCommand("UPDATE `servers` SET `processid` = @id WHERE `server` = @server", _db.Connection);
- saveId.Parameters.AddWithValue("@id", p.Id);
- saveId.Parameters.AddWithValue("@server", read.GetString("server"));
- saveId.ExecuteNonQuery();
- Console.WriteLine("Server " + read.GetString("server") + " wurde gestartet.");
- }
- }
- public bool ProcessRunning(int id)
- {
- try
- {
- Process pc = Process.GetProcessById(id);
- if (pc == null)
- return false;
- if (pc.HasExited)
- return false;
- }
- catch (Exception ex)
- {
- return false;
- }
- return true;
- }
- public Configuration getConfig()
- {
- return _conf;
- }
- public Database getDB()
- {
- return _db;
- }
- public static MCStartUpper getInstance()
- {
- return _instance;
- }
- private void runTimer(Object s, ElapsedEventArgs e)
- {
- checkServer();
- checkTimer();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment