using System;
using System.Diagnostics;
using System.IO;
using JXHelper.Common;
using JXHelper.Config;
using JXHelper.Core;
namespace JXHelper.Platform;
///
/// Bộ khởi chạy tự động Client game Võ Lâm và AutoVLBS (SSoT GameLauncher).
///
public static class GameLauncher
{
public const string DefaultGameExe = @"C:\JXVCTK\JxVoCongTruyenKy\Game.exe";
public const string DefaultGameDir = @"C:\JXVCTK\JxVoCongTruyenKy";
public const string DefaultAutoVlbsExe = @"C:\JXVCTK\JxVoCongTruyenKy\_AutoVLBS\VLBS19\AutoVLBS.exe";
public const string DefaultAutoVlbsDir = @"C:\JXVCTK\JxVoCongTruyenKy\_AutoVLBS\VLBS19";
public const string DefaultLauncherExe = @"C:\JXVCTK\JxVoCongTruyenKy\AutoUpdateVCTK.exe";
///
/// Khởi chạy game client cho tài khoản chỉ định.
///
public static bool LaunchGameClient(AccountEntry? account = null)
{
try
{
string dir = DefaultGameDir;
// 1. Ưu tiên khởi chạy qua Desktop Shortcut (tìm linh hoạt không phụ thuộc bảng mã Unicode của Windows)
var desktopDirs = new[]
{
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), "OneDrive", "Desktop"),
Environment.GetFolderPath(Environment.SpecialFolder.Desktop),
Environment.GetFolderPath(Environment.SpecialFolder.CommonDesktopDirectory)
};
foreach (var d in desktopDirs)
{
if (!Directory.Exists(d)) continue;
try
{
var foundLnk = Directory.GetFiles(d, "*.lnk")
.FirstOrDefault(f =>
(Path.GetFileName(f).Contains("Truy", StringComparison.OrdinalIgnoreCase) ||
Path.GetFileName(f).Contains("VCTK", StringComparison.OrdinalIgnoreCase) ||
Path.GetFileName(f).Contains("Vo Cong", StringComparison.OrdinalIgnoreCase) ||
Path.GetFileName(f).Contains("Võ Công", StringComparison.OrdinalIgnoreCase)) &&
!Path.GetFileName(f).Contains("Auto Train", StringComparison.OrdinalIgnoreCase));
if (foundLnk != null)
{
TelemetryLogger.LogAction($"[LAUNCH] Khởi chạy Game qua Desktop Shortcut: '{foundLnk}' cho '{account?.CharacterName ?? "Default"}'...");
var lnkStart = new ProcessStartInfo
{
FileName = "cmd.exe",
Arguments = $"/c start \"\" \"{foundLnk}\"",
UseShellExecute = false,
CreateNoWindow = true
};
var p = Process.Start(lnkStart);
if (p != null) return true;
}
}
catch {}
}
// 2. Khởi chạy qua AutoUpdateVCTK.exe launcher
if (File.Exists(DefaultLauncherExe))
{
TelemetryLogger.LogAction($"[LAUNCH] Khởi chạy Game qua Launcher: '{DefaultLauncherExe}'...");
var lStart = new ProcessStartInfo
{
FileName = DefaultLauncherExe,
WorkingDirectory = dir,
UseShellExecute = true,
Verb = "open"
};
var p = Process.Start(lStart);
if (p != null) return true;
}
// 3. Fallback khởi chạy Game.exe trực tiếp
string exe = DefaultGameExe;
if (!File.Exists(exe))
{
string altExe = Path.Combine(dir, "vggame.exe");
if (File.Exists(altExe)) exe = altExe;
}
var startInfo = new ProcessStartInfo
{
FileName = exe,
WorkingDirectory = dir,
UseShellExecute = true,
Verb = "runas"
};
TelemetryLogger.LogAction($"[LAUNCH] Khởi chạy Game trực tiếp: {exe} cho '{account?.CharacterName ?? "Default"}'...");
var proc = Process.Start(startInfo);
return proc != null;
}
catch (Exception ex)
{
TelemetryLogger.LogAction($"[LAUNCH ERROR] Ngoại lệ khởi chạy Game: {ex.Message}");
return false;
}
}
///
/// Khởi chạy AutoVLBS nếu chưa chạy trên hệ thống.
///
public static bool LaunchAutoVLBS()
{
try
{
var existing = Process.GetProcessesByName("AutoVLBS");
if (existing.Length > 0)
{
TelemetryLogger.LogAction($"[LAUNCH] AutoVLBS da dang chay san tren he thong (PID: {existing[0].Id})");
return true;
}
string exe = DefaultAutoVlbsExe;
string dir = DefaultAutoVlbsDir;
if (!File.Exists(exe))
{
TelemetryLogger.LogAction($"[LAUNCH ERROR] Khong tim thay AutoVLBS.exe tai: {exe}");
return false;
}
var startInfo = new ProcessStartInfo
{
FileName = exe,
WorkingDirectory = dir,
UseShellExecute = true,
Verb = "open"
};
TelemetryLogger.LogAction($"[LAUNCH] Dang khoi chay AutoVLBS: {exe}...");
var proc = Process.Start(startInfo);
if (proc != null)
{
TelemetryLogger.LogAction($"[LAUNCH] AutoVLBS da khoi dong thanh cong (PID: {proc.Id})");
return true;
}
return false;
}
catch (Exception ex)
{
TelemetryLogger.LogAction($"[LAUNCH ERROR] Ngoai le khoi chay AutoVLBS: {ex.Message}");
return false;
}
}
///
/// Kiểm tra xem tiến trình game có đang chạy hay không.
///
public static bool IsAnyGameRunning()
{
var p1 = Process.GetProcessesByName("vggame");
var p2 = Process.GetProcessesByName("game");
var p3 = Process.GetProcessesByName("Game");
return p1.Length > 0 || p2.Length > 0 || p3.Length > 0;
}
///
/// Tự động thực thi chu trình đăng nhập toàn diện: Bắt đầu trò chơi -> Nhập TK/MK -> Chọn nhân vật -> Vào game.
///
public static async System.Threading.Tasks.Task AutoFillLoginCredentialsAsync(IntPtr hWnd, AccountEntry account)
{
return await AutoLoginEngine.ExecuteAutoLoginAsync(hWnd, account);
}
}