2024-12-27 19:15:33 +03:00
|
|
|
using System;
|
2025-01-05 17:05:23 +03:00
|
|
|
using System.Diagnostics;
|
2025-01-14 22:10:16 +03:00
|
|
|
using System.IO;
|
2025-01-07 19:14:42 +03:00
|
|
|
using System.Text;
|
|
|
|
|
using System.Text.RegularExpressions;
|
2025-01-08 18:00:06 +03:00
|
|
|
using System.Threading.Tasks;
|
2025-01-07 19:14:42 +03:00
|
|
|
using Avalonia.Media;
|
2025-01-14 22:10:16 +03:00
|
|
|
using Nebula.Launcher.Services;
|
|
|
|
|
using Nebula.Launcher.ViewModels.Popup;
|
2025-01-05 17:05:23 +03:00
|
|
|
using Nebula.Shared.Models;
|
2025-01-07 17:01:00 +03:00
|
|
|
using Nebula.Shared.Services;
|
2024-12-27 19:15:33 +03:00
|
|
|
|
|
|
|
|
namespace Nebula.Launcher.ViewModels;
|
|
|
|
|
|
2025-01-14 22:10:16 +03:00
|
|
|
[ViewModelRegister(isSingleton: false)]
|
|
|
|
|
[ConstructGenerator]
|
2024-12-27 19:15:33 +03:00
|
|
|
public partial class ServerEntryModelView : ViewModelBase
|
|
|
|
|
{
|
2025-01-14 22:10:16 +03:00
|
|
|
private Process? _p;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public LogPopupModelView CurrLog;
|
|
|
|
|
[GenerateProperty] private AuthService AuthService { get; } = default!;
|
|
|
|
|
[GenerateProperty] private ContentService ContentService { get; } = default!;
|
|
|
|
|
[GenerateProperty] private CancellationService CancellationService { get; } = default!;
|
|
|
|
|
[GenerateProperty] private DebugService DebugService { get; } = default!;
|
|
|
|
|
[GenerateProperty] private RunnerService RunnerService { get; } = default!;
|
|
|
|
|
[GenerateProperty] private PopupMessageService PopupMessageService { get; } = default!;
|
|
|
|
|
[GenerateProperty] private ViewHelperService ViewHelperService { get; } = default!;
|
2025-01-07 17:01:00 +03:00
|
|
|
|
2025-01-08 18:00:06 +03:00
|
|
|
public bool RunVisible => Process == null;
|
2025-01-05 17:05:23 +03:00
|
|
|
|
2025-01-07 17:01:00 +03:00
|
|
|
public ServerHubInfo ServerHubInfo { get; set; } = default!;
|
2025-01-08 18:00:06 +03:00
|
|
|
|
|
|
|
|
private Process? Process
|
|
|
|
|
{
|
2025-01-14 22:10:16 +03:00
|
|
|
get => _p;
|
2025-01-08 18:00:06 +03:00
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
_p = value;
|
|
|
|
|
OnPropertyChanged(nameof(RunVisible));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-14 22:10:16 +03:00
|
|
|
protected override void InitialiseInDesignMode()
|
2025-01-05 17:05:23 +03:00
|
|
|
{
|
2025-01-14 22:10:16 +03:00
|
|
|
CurrLog = ViewHelperService.GetViewModel<LogPopupModelView>();
|
2025-01-05 17:05:23 +03:00
|
|
|
}
|
2024-12-27 19:15:33 +03:00
|
|
|
|
2025-01-14 22:10:16 +03:00
|
|
|
protected override void Initialise()
|
2025-01-05 17:05:23 +03:00
|
|
|
{
|
2025-01-14 22:10:16 +03:00
|
|
|
CurrLog = ViewHelperService.GetViewModel<LogPopupModelView>();
|
2025-01-07 19:14:42 +03:00
|
|
|
}
|
2025-01-07 17:01:00 +03:00
|
|
|
|
2025-01-08 18:00:06 +03:00
|
|
|
public void RunInstance()
|
2025-01-05 17:05:23 +03:00
|
|
|
{
|
2025-01-08 18:00:06 +03:00
|
|
|
Task.Run(RunAsync);
|
|
|
|
|
}
|
2025-01-07 17:01:00 +03:00
|
|
|
|
2025-01-08 18:00:06 +03:00
|
|
|
public async Task RunAsync()
|
|
|
|
|
{
|
2025-01-14 22:10:16 +03:00
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var authProv = AuthService.SelectedAuth;
|
|
|
|
|
|
|
|
|
|
var buildInfo =
|
|
|
|
|
await ContentService.GetBuildInfo(new RobustUrl(ServerHubInfo.Address), CancellationService.Token);
|
|
|
|
|
|
|
|
|
|
using (var loadingContext = ViewHelperService.GetViewModel<LoadingContextViewModel>())
|
|
|
|
|
{
|
|
|
|
|
loadingContext.LoadingName = "Loading instance...";
|
|
|
|
|
((ILoadingHandler)loadingContext).AppendJob();
|
|
|
|
|
|
|
|
|
|
PopupMessageService.Popup(loadingContext);
|
|
|
|
|
|
|
|
|
|
await RunnerService.PrepareRun(buildInfo, loadingContext, CancellationService.Token);
|
|
|
|
|
|
|
|
|
|
Process = Process.Start(new ProcessStartInfo
|
|
|
|
|
{
|
|
|
|
|
FileName = "dotnet.exe",
|
|
|
|
|
Arguments = "./Nebula.Runner.dll",
|
|
|
|
|
Environment =
|
|
|
|
|
{
|
|
|
|
|
{ "ROBUST_AUTH_USERID", authProv?.UserId.ToString() },
|
|
|
|
|
{ "ROBUST_AUTH_TOKEN", authProv?.Token.Token },
|
|
|
|
|
{ "ROBUST_AUTH_SERVER", authProv?.AuthLoginPassword.AuthServer },
|
|
|
|
|
{ "ROBUST_AUTH_PUBKEY", buildInfo.BuildInfo.Auth.PublicKey },
|
|
|
|
|
{ "GAME_URL", ServerHubInfo.Address },
|
|
|
|
|
{ "AUTH_LOGIN", authProv?.AuthLoginPassword.Login }
|
|
|
|
|
},
|
|
|
|
|
CreateNoWindow = true,
|
|
|
|
|
UseShellExecute = false,
|
|
|
|
|
RedirectStandardOutput = true,
|
|
|
|
|
RedirectStandardError = true,
|
|
|
|
|
StandardOutputEncoding = Encoding.UTF8
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
((ILoadingHandler)loadingContext).AppendResolvedJob();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (Process is null) return;
|
|
|
|
|
|
|
|
|
|
Process.EnableRaisingEvents = true;
|
|
|
|
|
|
|
|
|
|
Process.BeginOutputReadLine();
|
|
|
|
|
Process.BeginErrorReadLine();
|
|
|
|
|
|
|
|
|
|
Process.OutputDataReceived += OnOutputDataReceived;
|
|
|
|
|
Process.ErrorDataReceived += OnErrorDataReceived;
|
|
|
|
|
|
|
|
|
|
Process.Exited += OnExited;
|
|
|
|
|
}
|
|
|
|
|
catch (TaskCanceledException _)
|
|
|
|
|
{
|
|
|
|
|
PopupMessageService.Popup("Task canceled");
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
PopupMessageService.Popup(e);
|
|
|
|
|
}
|
2025-01-05 17:05:23 +03:00
|
|
|
}
|
2025-01-07 17:01:00 +03:00
|
|
|
|
|
|
|
|
private void OnExited(object? sender, EventArgs e)
|
|
|
|
|
{
|
2025-01-14 22:10:16 +03:00
|
|
|
if (Process is null) return;
|
|
|
|
|
|
2025-01-08 18:00:06 +03:00
|
|
|
Process.OutputDataReceived -= OnOutputDataReceived;
|
|
|
|
|
Process.ErrorDataReceived -= OnErrorDataReceived;
|
|
|
|
|
Process.Exited -= OnExited;
|
2025-01-14 22:10:16 +03:00
|
|
|
|
|
|
|
|
DebugService.Log("PROCESS EXIT WITH CODE " + Process.ExitCode);
|
|
|
|
|
|
2025-01-08 18:00:06 +03:00
|
|
|
Process.Dispose();
|
|
|
|
|
Process = null;
|
2025-01-07 17:01:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnErrorDataReceived(object sender, DataReceivedEventArgs e)
|
|
|
|
|
{
|
2025-01-07 19:14:42 +03:00
|
|
|
if (e.Data != null)
|
|
|
|
|
{
|
2025-01-14 22:10:16 +03:00
|
|
|
DebugService.Error(e.Data);
|
2025-01-07 19:14:42 +03:00
|
|
|
CurrLog.Append(e.Data);
|
|
|
|
|
}
|
2025-01-07 17:01:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnOutputDataReceived(object sender, DataReceivedEventArgs e)
|
|
|
|
|
{
|
2025-01-07 19:14:42 +03:00
|
|
|
if (e.Data != null)
|
|
|
|
|
{
|
2025-01-14 22:10:16 +03:00
|
|
|
DebugService.Log(e.Data);
|
2025-01-07 19:14:42 +03:00
|
|
|
CurrLog.Append(e.Data);
|
|
|
|
|
}
|
2025-01-07 17:01:00 +03:00
|
|
|
}
|
|
|
|
|
|
2024-12-27 19:15:33 +03:00
|
|
|
|
2025-01-05 17:05:23 +03:00
|
|
|
public void ReadLog()
|
2024-12-27 19:15:33 +03:00
|
|
|
{
|
2025-01-14 22:10:16 +03:00
|
|
|
PopupMessageService.Popup(CurrLog);
|
2024-12-27 19:15:33 +03:00
|
|
|
}
|
|
|
|
|
|
2025-01-05 17:05:23 +03:00
|
|
|
public void StopInstance()
|
2024-12-27 19:15:33 +03:00
|
|
|
{
|
2025-01-08 18:00:06 +03:00
|
|
|
Process?.CloseMainWindow();
|
2025-01-07 17:01:00 +03:00
|
|
|
}
|
2025-01-14 22:10:16 +03:00
|
|
|
|
|
|
|
|
private static string FindDotnetPath()
|
2025-01-07 17:01:00 +03:00
|
|
|
{
|
|
|
|
|
var pathEnv = Environment.GetEnvironmentVariable("PATH");
|
2025-01-14 22:10:16 +03:00
|
|
|
var paths = pathEnv?.Split(Path.PathSeparator);
|
2025-01-07 17:01:00 +03:00
|
|
|
if (paths != null)
|
|
|
|
|
foreach (var path in paths)
|
|
|
|
|
{
|
2025-01-14 22:10:16 +03:00
|
|
|
var dotnetPath = Path.Combine(path, "dotnet");
|
|
|
|
|
if (File.Exists(dotnetPath)) return dotnetPath;
|
2025-01-07 17:01:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
throw new Exception("Dotnet not found!");
|
2024-12-27 19:15:33 +03:00
|
|
|
}
|
2025-01-07 19:14:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public sealed class LogInfo
|
|
|
|
|
{
|
2025-01-14 22:10:16 +03:00
|
|
|
public string Category { get; set; } = "LOG";
|
2025-01-07 19:14:42 +03:00
|
|
|
public IBrush CategoryColor { get; set; } = Brush.Parse("#424242");
|
|
|
|
|
public string Message { get; set; } = "";
|
|
|
|
|
|
|
|
|
|
public static LogInfo FromString(string input)
|
|
|
|
|
{
|
|
|
|
|
var matches = Regex.Matches(input, @"(\[(?<c>.*)\] (?<m>.*))|(?<m>.*)");
|
2025-01-14 22:10:16 +03:00
|
|
|
var category = "All";
|
2025-01-07 19:14:42 +03:00
|
|
|
|
2025-01-14 22:10:16 +03:00
|
|
|
if (matches[0].Groups.TryGetValue("c", out var c)) category = c.Value;
|
2025-01-07 19:14:42 +03:00
|
|
|
|
|
|
|
|
var color = Brush.Parse("#444444");
|
|
|
|
|
|
|
|
|
|
switch (category)
|
|
|
|
|
{
|
|
|
|
|
case "DEBG":
|
|
|
|
|
color = Brush.Parse("#2436d4");
|
|
|
|
|
break;
|
|
|
|
|
case "ERRO":
|
|
|
|
|
color = Brush.Parse("#d42436");
|
|
|
|
|
break;
|
|
|
|
|
case "INFO":
|
|
|
|
|
color = Brush.Parse("#0ab3c9");
|
|
|
|
|
break;
|
|
|
|
|
}
|
2025-01-14 22:10:16 +03:00
|
|
|
|
2025-01-07 19:14:42 +03:00
|
|
|
var message = matches[0].Groups["m"].Value;
|
2025-01-14 22:10:16 +03:00
|
|
|
return new LogInfo
|
2025-01-07 19:14:42 +03:00
|
|
|
{
|
|
|
|
|
Category = category, Message = message, CategoryColor = color
|
|
|
|
|
};
|
|
|
|
|
}
|
2024-12-27 19:15:33 +03:00
|
|
|
}
|