Files
NebulaLauncher/Nebula.Launcher/ViewModels/ServerEntryModelView.cs

235 lines
6.6 KiB
C#
Raw Normal View History

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;
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-05 17:05:23 +03:00
[ObservableProperty] private bool _runVisible = true;
2025-01-07 17:01:00 +03:00
public ServerHubInfo ServerHubInfo { get; set; } = default!;
2025-01-07 19:14:42 +03:00
private Process? _process;
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
public async void RunInstance()
2025-01-05 17:05:23 +03:00
{
2025-01-07 17:01:00 +03:00
var authProv = _authService.SelectedAuth;
var buildInfo = await _contentService.GetBuildInfo(new RobustUrl(ServerHubInfo.Address), _cancellationService.Token);
await _runnerService.PrepareRun(buildInfo, _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 } ,
},
2025-01-07 19:14:42 +03:00
CreateNoWindow = true,
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true, StandardOutputEncoding = Encoding.UTF8
2025-01-07 17:01:00 +03:00
});
2025-01-07 19:14:42 +03:00
2025-01-07 17:01:00 +03:00
if (_process is null)
{
return;
}
2025-01-07 19:14:42 +03:00
_process.BeginOutputReadLine();
_process.BeginErrorReadLine();
RunVisible = false;
2025-01-07 17:01:00 +03:00
_process.OutputDataReceived += OnOutputDataReceived;
_process.ErrorDataReceived += OnErrorDataReceived;
_process.Exited += OnExited;
2025-01-05 17:05:23 +03:00
}
2025-01-07 17:01:00 +03:00
private void OnExited(object? sender, EventArgs e)
{
if (_process is null)
{
return;
}
_process.OutputDataReceived -= OnOutputDataReceived;
_process.ErrorDataReceived -= OnErrorDataReceived;
_process.Exited -= OnExited;
_debugService.Log("PROCESS EXIT WITH CODE " + _process.ExitCode);
_process.Dispose();
_process = null;
2025-01-07 19:14:42 +03:00
RunVisible = true;
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-07 17:01:00 +03:00
_process?.Close();
}
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";
public ObservableCollection<LogInfo> Logs { get; } = new();
public void Append(string str)
{
Logs.Add(LogInfo.FromString(str));
}
2024-12-27 19:15:33 +03:00
}