466 lines
21 KiB
C#
466 lines
21 KiB
C#
using Newtonsoft.Json.Linq;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel;
|
|
using System.Data;
|
|
using System.Diagnostics;
|
|
using System.Drawing;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Net.NetworkInformation;
|
|
using System.Runtime.InteropServices;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Windows.Forms;
|
|
|
|
namespace MxWManager
|
|
{
|
|
public partial class MainForm : Form
|
|
{
|
|
[DllImport("kernel32.dll", SetLastError = true)]
|
|
static extern bool GenerateConsoleCtrlEvent(int dwCtrlEvent, int dwProcessGroupId);
|
|
|
|
// P/Invoke declarations for Windows API functions
|
|
[DllImport("user32.dll")]
|
|
private static extern bool SetForegroundWindow(IntPtr hWnd);
|
|
|
|
[DllImport("user32.dll")]
|
|
private static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);
|
|
|
|
// Constant to restore a minimized window
|
|
private const int SW_RESTORE = 9;
|
|
|
|
public static string version = "1.0.0";
|
|
public Stopwatch stopwatch = new Stopwatch();
|
|
public TimeSpan ts;
|
|
public string elapsedTime = "";
|
|
|
|
private static string contextMenuEnv = "";
|
|
|
|
public MainForm()
|
|
{
|
|
InitializeComponent();
|
|
}
|
|
|
|
private void MainForm_Load(object sender, EventArgs e)
|
|
{
|
|
label_MainForm_Info.Text = $"Developped by mikx. Version: {version}";
|
|
dataGridView_MainForm_EnvList.BackgroundColor = Color.FromArgb(22,23,23);
|
|
textBox_MainForm_Console.BackColor = Color.FromArgb(22, 23, 23);
|
|
int envc = 0;
|
|
var env = Directory.GetDirectories(Directory.GetCurrentDirectory());
|
|
foreach (var dir in env)
|
|
{
|
|
bool envcheck = File.Exists($@"{dir}\env.json");
|
|
if (envcheck)
|
|
{
|
|
envc++;
|
|
}
|
|
|
|
}
|
|
MainConsole("System",$"MxWManager Launched with {envc} managed env.");
|
|
UpdateEnvList();
|
|
dataGridView_MainForm_EnvList.ClearSelection();
|
|
dataGridView_MainForm_EnvList.CurrentCell = null;
|
|
}
|
|
|
|
private void dataGridView_MainForm_EnvList_CellClick(object sender, DataGridViewCellEventArgs e)
|
|
{
|
|
if (dataGridView_MainForm_EnvList.Rows[e.RowIndex].Cells[e.ColumnIndex].Value != null)
|
|
{
|
|
string clickedButtonText = dataGridView_MainForm_EnvList.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
|
|
string envName = dataGridView_MainForm_EnvList.Rows[e.RowIndex].Cells[2].Value.ToString();
|
|
|
|
Process p = new Process();
|
|
|
|
switch (clickedButtonText)
|
|
{
|
|
case "Browse":
|
|
Process.Start(new ProcessStartInfo($@"{envName}"));
|
|
MainConsole($"{envName}", $"Browsing {envName}.");
|
|
break;
|
|
|
|
case "IDE":
|
|
MainConsole($"{envName}", $"Starting IDE for {envName}.");
|
|
p.StartInfo.FileName = "devenv.exe";
|
|
p.StartInfo.Arguments = $@"{envName}/build/AzerothCore.sln";
|
|
p.Start();
|
|
UpdateEnvList();
|
|
break;
|
|
|
|
case "Manage":
|
|
contextMenuStrip_MainForm_Manage.Items.Clear();
|
|
contextMenuEnv = envName;
|
|
contextMenuStrip_MainForm_Manage.Items.Add($@"CC|{envName}| Copy built Core");
|
|
var env = Directory.GetDirectories(Directory.GetCurrentDirectory());
|
|
foreach (var dir in env)
|
|
{
|
|
bool envcheck = File.Exists($@"{dir}\env.json");
|
|
if (envcheck && Path.GetFileName(dir) != envName && !envName.Contains("DEV"))
|
|
{
|
|
contextMenuStrip_MainForm_Manage.Items.Add($@"GS|{Path.GetFileName(dir)}| Get Sources From");
|
|
}
|
|
}
|
|
contextMenuStrip_MainForm_Manage.Show(Cursor.Position.X, Cursor.Position.Y);
|
|
break;
|
|
|
|
case "Config":
|
|
if ((Control.ModifierKeys & Keys.Shift) == Keys.Shift)
|
|
{
|
|
MainConsole($"{envName}", $"Opening {envName} env config...");
|
|
p.StartInfo.FileName = @"code";
|
|
p.StartInfo.Arguments = $@"{envName}\env.json";
|
|
p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
|
|
p.Start();
|
|
dataGridView_MainForm_EnvList.ClearSelection();
|
|
dataGridView_MainForm_EnvList.CurrentCell = null;
|
|
break;
|
|
}
|
|
else
|
|
{
|
|
stopwatch.Start();
|
|
p.StartInfo.FileName = "cmd.exe";
|
|
p.StartInfo.Arguments = $"/C cd {envName}/build && cmake ..\\sources -G \"Visual Studio 17 2022\" -A x64 -DWITH_WARNINGS=0 -DTOOLS_BUILD=none -DSCRIPTS=static -DMODULES=static -DMYSQL_LIBRARY=\"..\\..\\_DEPS\\mysql\\lib\\libmysql.lib\" -DMYSQL_INCLUDE_DIR=\"..\\..\\_DEPS\\mysql\\include\"";
|
|
p.Start();
|
|
MainConsole($"{envName}", $"Configuring {envName}...");
|
|
p.WaitForExit();
|
|
stopwatch.Stop();
|
|
ts = stopwatch.Elapsed;
|
|
elapsedTime = String.Format("{0:00}:{1:00}:{2:00}", ts.Hours, ts.Minutes, ts.Seconds);
|
|
MainConsole($"{envName}", $"Configured {envName} in {elapsedTime}.");
|
|
UpdateEnvList();
|
|
break;
|
|
}
|
|
|
|
case "Clean":
|
|
p.StartInfo.FileName = "cmd.exe";
|
|
p.StartInfo.Arguments = $"/C cd {envName}/build && cmake --build . --target clean --config RelWithDebInfo";
|
|
MainConsole($"{envName}", $"Cleaned {envName}.");
|
|
p.Start();
|
|
break;
|
|
|
|
case "Build":
|
|
stopwatch.Start();
|
|
p.StartInfo.FileName = "cmd.exe";
|
|
p.StartInfo.Arguments = $"/C cd {envName}/build && cmake --build . --config RelWithDebInfo";
|
|
p.Start();
|
|
MainConsole($"{envName}", $"Building {envName}...");
|
|
p.WaitForExit();
|
|
stopwatch.Stop();
|
|
ts = stopwatch.Elapsed;
|
|
elapsedTime = String.Format("{0:00}:{1:00}:{2:00}", ts.Hours, ts.Minutes, ts.Seconds);
|
|
MainConsole($"{envName}", $"Built {envName} in {elapsedTime}.");
|
|
stopwatch.Reset();
|
|
UpdateEnvList();
|
|
break;
|
|
case "Auth":
|
|
int apid = GetEnvProcessID(envName, "auth");
|
|
if (apid == 0 || !ProcessExists(apid))
|
|
{
|
|
p.StartInfo.FileName = "cmd.exe";
|
|
p.StartInfo.Arguments = $@"/C cd {Directory.GetCurrentDirectory()}\{envName}\core && authserver.exe";
|
|
p.Start();
|
|
SetEnvProcessID(envName, "auth", p.Id);
|
|
MainConsole($"{envName}", $"Started Auth with pid {p.Id}.");
|
|
UpdateEnvList();
|
|
} else
|
|
{
|
|
MainConsole($"{envName}", $"Auth is running with pid {apid}, focusing.");
|
|
SetActiveWindowByProcessId(apid);
|
|
}
|
|
break;
|
|
case "World":
|
|
int wpid = GetEnvProcessID(envName, "world");
|
|
if (wpid == 0 || !ProcessExists(wpid))
|
|
{
|
|
p.StartInfo.FileName = "cmd.exe";
|
|
p.StartInfo.Arguments = $@"/C cd {Directory.GetCurrentDirectory()}\{envName}\core && worldserver.exe";
|
|
p.Start();
|
|
SetEnvProcessID(envName, "world", p.Id);
|
|
MainConsole($"{envName}", $"Started World with pid {p.Id}.");
|
|
UpdateEnvList();
|
|
} else
|
|
{
|
|
MainConsole($"{envName}", $"World is running with pid {wpid}, focusing.");
|
|
SetActiveWindowByProcessId(wpid);
|
|
}
|
|
break;
|
|
case "Configs":
|
|
contextMenuStrip_MainForm_ConfList.Items.Clear();
|
|
contextMenuEnv = envName;
|
|
var configs = Directory.GetFiles($@"{envName}\core\configs", "*.conf");
|
|
foreach (var c in configs)
|
|
{
|
|
contextMenuStrip_MainForm_ConfList.Items.Add(Path.GetFileName(c));
|
|
}
|
|
contextMenuStrip_MainForm_ConfList.Show(Cursor.Position.X, Cursor.Position.Y);
|
|
break;
|
|
|
|
}
|
|
}
|
|
}
|
|
|
|
public void UpdateEnvList()
|
|
{
|
|
dataGridView_MainForm_EnvList.Rows.Clear();
|
|
dataGridView_MainForm_EnvList.Columns.Clear();
|
|
DataGridViewImageColumn imgColumn = new DataGridViewImageColumn();
|
|
dataGridView_MainForm_EnvList.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.DisplayedCells;
|
|
dataGridView_MainForm_EnvList.RowTemplate.Height = 31;
|
|
dataGridView_MainForm_EnvList.ReadOnly = true;
|
|
//dataGridView_MainForm_EnvList.Columns.Add(new DataGridViewImageColumn());
|
|
dataGridView_MainForm_EnvList.Columns.Add(new DataGridViewImageColumn());
|
|
dataGridView_MainForm_EnvList.Columns.Add(new DataGridViewTextBoxColumn());
|
|
dataGridView_MainForm_EnvList.Columns.Add(new DataGridViewTextBoxColumn());
|
|
dataGridView_MainForm_EnvList.Columns.Add(new DataGridViewButtonColumn());
|
|
dataGridView_MainForm_EnvList.Columns.Add(new DataGridViewButtonColumn());
|
|
dataGridView_MainForm_EnvList.Columns.Add(new DataGridViewButtonColumn());
|
|
dataGridView_MainForm_EnvList.Columns.Add(new DataGridViewButtonColumn());
|
|
dataGridView_MainForm_EnvList.Columns.Add(new DataGridViewButtonColumn());
|
|
dataGridView_MainForm_EnvList.Columns.Add(new DataGridViewButtonColumn());
|
|
dataGridView_MainForm_EnvList.Columns.Add(new DataGridViewImageColumn());
|
|
dataGridView_MainForm_EnvList.Columns.Add(new DataGridViewButtonColumn());
|
|
dataGridView_MainForm_EnvList.Columns.Add(new DataGridViewImageColumn());
|
|
dataGridView_MainForm_EnvList.Columns.Add(new DataGridViewButtonColumn());
|
|
dataGridView_MainForm_EnvList.Columns.Add(new DataGridViewButtonColumn());
|
|
|
|
var env = Directory.GetDirectories(Directory.GetCurrentDirectory());
|
|
foreach (var dir in env)
|
|
{
|
|
bool envcheck = File.Exists($@"{dir}\env.json");
|
|
if (envcheck)
|
|
{
|
|
Image imgSource = Image.FromFile(@"_DATA\img\Source_Off.png");
|
|
if (Directory.GetFileSystemEntries($@"{dir}\sources").Length == 0)
|
|
{
|
|
imgSource = Image.FromFile(@"_DATA\img\Source_Off.png");
|
|
}
|
|
else
|
|
{
|
|
imgSource = Image.FromFile(@"_DATA\img\Source_On.png");
|
|
}
|
|
|
|
Image imgBuild = Image.FromFile(@"_DATA\img\Build_Off.png");
|
|
if (Directory.GetFileSystemEntries($@"{dir}\build\bin").Length == 0)
|
|
{
|
|
imgBuild = Image.FromFile(@"_DATA\img\Build_Off.png");
|
|
}
|
|
else
|
|
{
|
|
imgBuild = Image.FromFile(@"_DATA\img\Build_On.png");
|
|
}
|
|
|
|
Image imgAuth = Image.FromFile(@"_DATA\img\Auth_Off.png");
|
|
int authpid = GetEnvProcessID(Path.GetFileName(dir), "auth");
|
|
if (authpid != 0 && ProcessExists(authpid))
|
|
{
|
|
imgAuth = Image.FromFile(@"_DATA\img\Auth_On.png");
|
|
} else
|
|
{
|
|
imgAuth = Image.FromFile(@"_DATA\img\Auth_Off.png");
|
|
}
|
|
|
|
Image imgWorld = Image.FromFile(@"_DATA\img\World_Off.png");
|
|
int worldpid = GetEnvProcessID(Path.GetFileName(dir), "world");
|
|
if (worldpid != 0 && ProcessExists(worldpid))
|
|
{
|
|
imgWorld = Image.FromFile(@"_DATA\img\World_On.png");
|
|
}
|
|
else
|
|
{
|
|
imgWorld = Image.FromFile(@"_DATA\img\World_Off.png");
|
|
}
|
|
|
|
// Add env to datagridview
|
|
DataGridViewRow rowEnv = new DataGridViewRow();
|
|
//rowEnv.Cells.Add(new DataGridViewImageCell { Value = imgSource });
|
|
rowEnv.Cells.Add(new DataGridViewImageCell { Value = imgBuild });
|
|
rowEnv.Cells.Add(new DataGridViewTextBoxCell { Value = $"{GetEnvTag(Path.GetFileName(dir))[0]}:{GetEnvTag(Path.GetFileName(dir))[1]}" });
|
|
rowEnv.Cells.Add(new DataGridViewTextBoxCell { Value = Path.GetFileName(dir) });
|
|
rowEnv.Cells.Add(new DataGridViewButtonCell { Value = "Browse" });
|
|
rowEnv.Cells.Add(new DataGridViewButtonCell { Value = "IDE" });
|
|
rowEnv.Cells.Add(new DataGridViewButtonCell { Value = "Manage" });
|
|
rowEnv.Cells.Add(new DataGridViewButtonCell { Value = "Config" });
|
|
rowEnv.Cells.Add(new DataGridViewButtonCell { Value = "Clean" });
|
|
rowEnv.Cells.Add(new DataGridViewButtonCell { Value = "Build" });
|
|
rowEnv.Cells.Add(new DataGridViewImageCell { Value = imgAuth });
|
|
rowEnv.Cells.Add(new DataGridViewButtonCell { Value = "Auth" });
|
|
rowEnv.Cells.Add(new DataGridViewImageCell { Value = imgWorld });
|
|
rowEnv.Cells.Add(new DataGridViewButtonCell { Value = "World" });
|
|
rowEnv.Cells.Add(new DataGridViewButtonCell { Value = "Configs" });
|
|
dataGridView_MainForm_EnvList.Rows.Add(rowEnv);
|
|
}
|
|
}
|
|
}
|
|
|
|
public static bool ProcessExists(int pid)
|
|
{
|
|
try
|
|
{
|
|
Process.GetProcessById(pid);
|
|
return true;
|
|
}
|
|
catch (ArgumentException)
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public static int GetEnvProcessID(string env, string type)
|
|
{
|
|
string jsonFilePath = $@"{env}/env.json";
|
|
string jsonContent = File.ReadAllText(jsonFilePath);
|
|
JObject jsonObject = JObject.Parse(jsonContent);
|
|
if (type == "auth") { return jsonObject["authpid"].Value<int>(); }
|
|
if (type == "world") { return jsonObject["worldpid"].Value<int>(); }
|
|
return 0;
|
|
}
|
|
|
|
public static string[] GetEnvTag(string env)
|
|
{
|
|
string jsonFilePath = $@"{env}/env.json";
|
|
string jsonContent = File.ReadAllText(jsonFilePath);
|
|
JObject jsonObject = JObject.Parse(jsonContent);
|
|
string[] tags = { jsonObject["tag"].ToString().Split('|')[0], jsonObject["tag"].ToString().Split('|')[1] };
|
|
return tags;
|
|
}
|
|
|
|
public static void SetEnvProcessID(string env, string type, int pid)
|
|
{
|
|
string jsonFilePath = $@"{env}/env.json";
|
|
string jsonContent = File.ReadAllText(jsonFilePath);
|
|
JObject jsonObject = JObject.Parse(jsonContent);
|
|
if (type == "auth") { jsonObject["authpid"] = pid; }
|
|
if (type == "world") { jsonObject["worldpid"] = pid; }
|
|
string updatedJson = jsonObject.ToString(Newtonsoft.Json.Formatting.Indented);
|
|
File.WriteAllText(jsonFilePath, updatedJson);
|
|
}
|
|
|
|
public static void SetActiveWindowByProcessId(int processId)
|
|
{
|
|
try
|
|
{
|
|
Process process = Process.GetProcessById(processId);
|
|
|
|
// Check if the process has a main window
|
|
if (process.MainWindowHandle == IntPtr.Zero)
|
|
{
|
|
Console.WriteLine($"Process with ID {processId} has no main window.");
|
|
return;
|
|
}
|
|
|
|
IntPtr handle = process.MainWindowHandle;
|
|
|
|
// Restore the window if it's minimized
|
|
ShowWindowAsync(handle, SW_RESTORE);
|
|
|
|
// Bring the window to the foreground and activate it
|
|
SetForegroundWindow(handle);
|
|
|
|
Console.WriteLine($"Window for process with ID {processId} has been activated.");
|
|
}
|
|
catch (ArgumentException)
|
|
{
|
|
Console.WriteLine($"Error: A process with ID {processId} does not exist.");
|
|
}
|
|
}
|
|
|
|
private void timer_MainForm_ProcessWatchDog_Tick(object sender, EventArgs e)
|
|
{
|
|
var env = Directory.GetDirectories(Directory.GetCurrentDirectory());
|
|
foreach (var dir in env)
|
|
{
|
|
bool envcheck = File.Exists($@"{dir}\env.json");
|
|
if (envcheck)
|
|
{
|
|
string envName = Path.GetFileName(dir);
|
|
int apid = GetEnvProcessID(Path.GetFileName(dir), "auth");
|
|
if (apid == 0 || !ProcessExists(apid))
|
|
{
|
|
SetEnvProcessID(envName, "auth", 0);
|
|
}
|
|
|
|
int wpid = GetEnvProcessID(Path.GetFileName(dir), "world");
|
|
if (wpid == 0 || !ProcessExists(wpid))
|
|
{
|
|
SetEnvProcessID(envName, "world", 0);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private void contextMenuStrip_MainForm_ConfList_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
|
|
{
|
|
ToolStripItem item = e.ClickedItem;
|
|
MainConsole($"{contextMenuEnv}", $"Opening {item.Text}...");
|
|
Process p = new Process();
|
|
p.StartInfo.FileName = @"code";
|
|
p.StartInfo.Arguments = $@"{contextMenuEnv}\core\configs\{item.Text}";
|
|
p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
|
|
p.Start();
|
|
}
|
|
|
|
private void contextMenuStrip_MainForm_Manage_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
|
|
{
|
|
ToolStripItem item = e.ClickedItem;
|
|
string clickedType = item.Text.Split('|')[0];
|
|
string clickedEnv = item.Text.Split('|')[1];
|
|
switch (clickedType)
|
|
{
|
|
case "CC":
|
|
File.Copy($@"{clickedEnv}\build\bin\RelWithDebInfo\worldserver.exe", $@"{clickedEnv}\core\worldserver.exe", true);
|
|
break;
|
|
case "GS":
|
|
// Get sources from another env
|
|
Process p = new Process();
|
|
p.StartInfo.FileName = "cmd.exe";
|
|
p.StartInfo.Arguments = $@"/C xcopy {Directory.GetCurrentDirectory()}\{clickedEnv}\sources\src {Directory.GetCurrentDirectory()}\{contextMenuEnv}\sources\src /S /E /H /Y && xcopy {Directory.GetCurrentDirectory()}\{clickedEnv}\sources\modules {Directory.GetCurrentDirectory()}\{contextMenuEnv}\sources\modules /S /E /H /Y";
|
|
p.Start();
|
|
p.WaitForExit();
|
|
break;
|
|
}
|
|
}
|
|
|
|
public void MainConsole(string env, string msg)
|
|
{
|
|
string seconds = "";
|
|
string minutes = "";
|
|
string hours = "";
|
|
if (DateTime.Now.Second < 10)
|
|
{
|
|
seconds = String.Format("0{0}", DateTime.Now.Second);
|
|
}
|
|
else
|
|
{
|
|
seconds = DateTime.Now.Second.ToString();
|
|
}
|
|
|
|
if (DateTime.Now.Minute < 10)
|
|
{
|
|
minutes = String.Format("0{0}", DateTime.Now.Minute);
|
|
}
|
|
else
|
|
{
|
|
minutes = DateTime.Now.Minute.ToString();
|
|
}
|
|
|
|
if (DateTime.Now.Hour < 10)
|
|
{
|
|
hours = String.Format("0{0}", DateTime.Now.Hour);
|
|
}
|
|
else
|
|
{
|
|
hours = DateTime.Now.Hour.ToString();
|
|
}
|
|
if (textBox_MainForm_Console.Text == "")
|
|
{
|
|
textBox_MainForm_Console.Text = $"[{hours}:{minutes}:{seconds}][{env}] {msg}";
|
|
}
|
|
else
|
|
{
|
|
textBox_MainForm_Console.AppendText("\r\n" + $"[{hours}:{minutes}:{seconds}][{env}] {msg}");
|
|
}
|
|
}
|
|
}
|
|
}
|