160 lines
5.3 KiB
C#
Raw Permalink Normal View History

2025-07-16 20:15:42 +05:00
using System;
using System.Diagnostics;
using System.IO;
using System.IO.Compression;
using System.Net;
using System.Windows;
using System.Windows.Forms;
namespace HighGroundRoyaleLauncher
{
public partial class MainWindow : Window
{
2025-07-16 21:18:53 +05:00
private string remoteVersionURL = "http://147.93.30.25/SW/SWVersion.txt";
private string remoteZipURL = "http://147.93.30.25/SW/SeigeWorlds.zip";
2025-07-16 20:15:42 +05:00
private string installPath = "";
private string zipPath => Path.Combine(installPath, "game.zip");
private string versionPath => Path.Combine(installPath, "version.txt");
private string gameFolder => Path.Combine(installPath, "Game");
2025-07-16 21:18:53 +05:00
private string exePath => Path.Combine(gameFolder, "Siege Worlds.exe");
2025-07-17 22:59:20 +05:00
private BackgroundSlideshowManager _slideshow;
2025-07-16 20:15:42 +05:00
public MainWindow()
{
InitializeComponent();
2025-07-16 21:18:53 +05:00
installPathBox.Text = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles), "Siege worlds");
2025-07-17 22:59:20 +05:00
_slideshow = new BackgroundSlideshowManager(
SlideshowImage,
new[] { "img1.jpg", "img2.jpg", "img3.jpg" },
TimeSpan.FromSeconds(4)
);
_slideshow.Start();
2025-07-16 20:15:42 +05:00
}
private void Browse_Click(object sender, RoutedEventArgs e)
{
using (var dialog = new FolderBrowserDialog())
{
dialog.Description = "Select Installation Folder";
dialog.SelectedPath = installPathBox.Text;
if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
installPathBox.Text = dialog.SelectedPath;
}
}
}
private void InstallButton_Click(object sender, RoutedEventArgs e)
{
installPath = installPathBox.Text.Trim();
if (string.IsNullOrEmpty(installPath))
{
System.Windows.MessageBox.Show("Please select a valid installation path.");
return;
}
Directory.CreateDirectory(installPath);
CheckForUpdates();
}
private void CheckForUpdates()
{
statusText.Text = "Checking for updates...";
using (WebClient client = new WebClient())
{
client.DownloadStringCompleted += (s, e) =>
{
if (e.Error == null)
{
string remoteVersion = e.Result.Trim();
string localVersion = File.Exists(versionPath) ? File.ReadAllText(versionPath).Trim() : "";
if (remoteVersion != localVersion)
{
statusText.Text = "Update available. Downloading...";
DownloadGame(remoteVersion);
}
else
{
statusText.Text = "Up to date.";
LaunchGame();
}
}
else
{
statusText.Text = "Failed to check version.";
System.Windows.MessageBox.Show(e.Error.Message);
}
};
client.DownloadStringAsync(new Uri(remoteVersionURL));
}
}
private void DownloadGame(string newVersion)
{
using (WebClient client = new WebClient())
{
client.DownloadProgressChanged += (s, e) =>
{
progressBar.Value = e.ProgressPercentage;
};
client.DownloadFileCompleted += (s, e) =>
{
if (e.Error == null)
{
statusText.Text = "Download complete. Extracting...";
ExtractGame(newVersion);
}
else
{
statusText.Text = "Download failed.";
System.Windows.MessageBox.Show(e.Error.Message);
}
};
client.DownloadFileAsync(new Uri(remoteZipURL), zipPath);
}
}
private void ExtractGame(string newVersion)
{
try
{
if (Directory.Exists(gameFolder))
Directory.Delete(gameFolder, true);
ZipFile.ExtractToDirectory(zipPath, gameFolder);
File.WriteAllText(versionPath, newVersion);
File.Delete(zipPath);
statusText.Text = "Installation complete!";
LaunchGame();
}
catch (Exception ex)
{
statusText.Text = "Extraction failed.";
System.Windows.MessageBox.Show(ex.Message);
}
}
private void LaunchGame()
{
if (File.Exists(exePath))
{
Process.Start(exePath);
System.Windows.Application.Current.Shutdown();
}
else
{
System.Windows.MessageBox.Show("Game executable not found after install.");
}
}
}
}