2024-12-27 19:15:33 +03:00
|
|
|
using System;
|
2025-01-07 19:14:42 +03:00
|
|
|
using System.Collections.ObjectModel;
|
2025-01-05 17:05:23 +03:00
|
|
|
using System.Diagnostics;
|
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;
|
2024-12-27 19:15:33 +03:00
|
|
|
using CommunityToolkit.Mvvm.ComponentModel;
|
2025-01-05 17:05:23 +03:00
|
|
|
using Nebula.Launcher.ViewHelper;
|
2025-01-07 19:14:42 +03:00
|
|
|
using Nebula.Launcher.Views.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-05 17:05:23 +03:00
|
|
|
[ViewModelRegister(isSingleton:false)]
|
2024-12-27 19:15:33 +03:00
|
|
|
public partial class ServerEntryModelView : ViewModelBase
|
|
|
|
|
{
|
2025-01-07 17:01:00 +03:00
|
|
|
private readonly AuthService _authService = default!;
|
|
|
|
|
private readonly ContentService _contentService = default!;
|
|
|
|
|
private readonly CancellationService _cancellationService = default!;
|
|
|
|
|
private readonly DebugService _debugService = default!;
|
2025-01-07 19:14:42 +03:00
|
|
|
private readonly RunnerService _runnerService = default!;
|
|
|
|
|
private readonly PopupMessageService _popupMessageService;
|
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? _p;
|
|
|
|
|
private Process? Process
|
|
|
|
|
{
|
|
|
|
|
get { return _p; }
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
_p = value;
|
|
|
|
|
OnPropertyChanged(nameof(RunVisible));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2025-01-07 19:14:42 +03:00
|
|
|
|
|
|
|
|
public LogPopupModelView CurrLog;
|
2025-01-05 17:05:23 +03:00
|
|
|
|
|
|
|
|
public ServerEntryModelView() : base()
|
|
|
|
|
{
|
2025-01-07 19:14:42 +03:00
|
|
|
CurrLog = GetViewModel<LogPopupModelView>();
|
2025-01-05 17:05:23 +03:00
|
|
|
}
|
2024-12-27 19:15:33 +03:00
|
|
|
|
2025-01-07 17:01:00 +03:00
|
|
|
public ServerEntryModelView(
|
|
|
|
|
IServiceProvider serviceProvider,
|
|
|
|
|
AuthService authService,
|
|
|
|
|
ContentService contentService,
|
|
|
|
|
CancellationService cancellationService,
|
|
|
|
|
DebugService debugService,
|
2025-01-07 19:14:42 +03:00
|
|
|
RunnerService runnerService, PopupMessageService popupMessageService
|
2025-01-07 17:01:00 +03:00
|
|
|
) : base(serviceProvider)
|
2025-01-05 17:05:23 +03:00
|
|
|
{
|
2025-01-07 17:01:00 +03:00
|
|
|
_authService = authService;
|
|
|
|
|
_contentService = contentService;
|
|
|
|
|
_cancellationService = cancellationService;
|
|
|
|
|
_debugService = debugService;
|
|
|
|
|
_runnerService = runnerService;
|
2025-01-07 19:14:42 +03:00
|
|
|
_popupMessageService = popupMessageService;
|
2025-01-05 17:05:23 +03:00
|
|
|
|
2025-01-07 19:14:42 +03:00
|
|
|
CurrLog = GetViewModel<LogPopupModelView>();
|
|
|
|
|
}
|
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()
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var authProv = _authService.SelectedAuth;
|
|
|
|
|
|
|
|
|
|
var buildInfo =
|
|
|
|
|
await _contentService.GetBuildInfo(new RobustUrl(ServerHubInfo.Address), _cancellationService.Token);
|
|
|
|
|
|
|
|
|
|
using (var loadingContext = 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 e)
|
|
|
|
|
{
|
|
|
|
|
_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-08 18:00:06 +03:00
|
|
|
if (Process is null)
|
2025-01-07 17:01:00 +03:00
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-08 18:00:06 +03:00
|
|
|
Process.OutputDataReceived -= OnOutputDataReceived;
|
|
|
|
|
Process.ErrorDataReceived -= OnErrorDataReceived;
|
|
|
|
|
Process.Exited -= OnExited;
|
2025-01-07 17:01:00 +03:00
|
|
|
|
2025-01-08 18:00:06 +03:00
|
|
|
_debugService.Log("PROCESS EXIT WITH CODE " + Process.ExitCode);
|
2025-01-07 17:01:00 +03:00
|
|
|
|
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)
|
|
|
|
|
{
|
|
|
|
|
_debugService.Error(e.Data);
|
|
|
|
|
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)
|
|
|
|
|
{
|
|
|
|
|
_debugService.Log(e.Data);
|
|
|
|
|
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-07 19:14:42 +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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static string FindDotnetPath()
|
|
|
|
|
{
|
|
|
|
|
var pathEnv = Environment.GetEnvironmentVariable("PATH");
|
|
|
|
|
var paths = pathEnv?.Split(System.IO.Path.PathSeparator);
|
|
|
|
|
if (paths != null)
|
|
|
|
|
{
|
|
|
|
|
foreach (var path in paths)
|
|
|
|
|
{
|
|
|
|
|
var dotnetPath = System.IO.Path.Combine(path, "dotnet");
|
|
|
|
|
if (System.IO.File.Exists(dotnetPath))
|
|
|
|
|
{
|
|
|
|
|
return dotnetPath;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
{
|
|
|
|
|
public string Category { get; set; } = "LOG";
|
|
|
|
|
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>.*)");
|
|
|
|
|
string category = "All";
|
|
|
|
|
|
|
|
|
|
if( matches[0].Groups.TryGetValue("c", out var c))
|
|
|
|
|
{
|
|
|
|
|
category = c.Value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var message = matches[0].Groups["m"].Value;
|
|
|
|
|
return new LogInfo()
|
|
|
|
|
{
|
|
|
|
|
Category = category, Message = message, CategoryColor = color
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[ViewModelRegister(typeof(LogPopupView), false)]
|
|
|
|
|
public sealed class LogPopupModelView : PopupViewModelBase
|
|
|
|
|
{
|
|
|
|
|
public LogPopupModelView() : base()
|
|
|
|
|
{
|
|
|
|
|
Logs.Add(new LogInfo()
|
|
|
|
|
{
|
|
|
|
|
Category = "DEBG", Message = "MEOW MEOW TEST"
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
Logs.Add(new LogInfo()
|
|
|
|
|
{
|
|
|
|
|
Category = "ERRO", Message = "MEOW MEOW TEST 11\naaaaa"
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public LogPopupModelView(IServiceProvider serviceProvider) : base(serviceProvider)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override string Title => "LOG";
|
2025-01-12 15:15:01 +03:00
|
|
|
public override bool IsClosable => true;
|
2025-01-07 19:14:42 +03:00
|
|
|
|
|
|
|
|
public ObservableCollection<LogInfo> Logs { get; } = new();
|
|
|
|
|
|
|
|
|
|
public void Append(string str)
|
|
|
|
|
{
|
|
|
|
|
Logs.Add(LogInfo.FromString(str));
|
|
|
|
|
}
|
2024-12-27 19:15:33 +03:00
|
|
|
}
|