Improve UX when the first thing a user does is start singleplayer (download Java, set everything up)

This commit is contained in:
dginovker 2023-11-05 13:48:28 -05:00
parent 61053cc1f1
commit 65db9e9c70
6 changed files with 40 additions and 14 deletions

View file

@ -6,7 +6,7 @@ namespace Saradomin.Infrastructure.Services
{
public interface IJavaUpdateService : IService
{
event EventHandler<float> JavaDownloadProgressChanged;
event EventHandler<Tuple<float, bool>> JavaDownloadProgressChanged;
Task DownloadAndSetJava11(ISettingsService settingsService);
}
}

View file

@ -10,7 +10,7 @@ namespace Saradomin.Infrastructure.Services
{
public class JavaUpdateService : IJavaUpdateService
{
public event EventHandler<float> JavaDownloadProgressChanged;
public event EventHandler<Tuple<float, bool>> JavaDownloadProgressChanged;
public async Task DownloadAndSetJava11(ISettingsService settingsService)
{
@ -47,13 +47,13 @@ namespace Saradomin.Infrastructure.Services
await fileStream.WriteAsync(buffer, 0, bytesRead);
var progress = (float)totalRead / contentLength;
JavaDownloadProgressChanged?.Invoke(this, progress);
JavaDownloadProgressChanged?.Invoke(this, new Tuple<float, bool>(progress, false));
} while (bytesRead > 0);
}
}
}
JavaDownloadProgressChanged?.Invoke(this, 1f);
JavaDownloadProgressChanged?.Invoke(this, new Tuple<float, bool>(1f, false));
if (Directory.Exists(extractedPath)) Directory.Delete(extractedPath, true);
@ -97,6 +97,7 @@ namespace Saradomin.Infrastructure.Services
);
}
settingsService.SaveAll();
JavaDownloadProgressChanged?.Invoke(this, new Tuple<float, bool>(1f, true));
}
}
}

View file

@ -91,7 +91,7 @@ public static class SingleplayerManagement
var singleplayerHome = CrossPlatform.GetSingleplayerHome();
// Get all backup directories
var backupDirectories = Directory.GetDirectories(backupHome);
string[] backupDirectories = Directory.Exists(backupHome) ? Directory.GetDirectories(backupHome) : Array.Empty<string>();
string mostRecentBackupDirName = FindMostRecentBackupDirectory(backupDirectories);
@ -132,10 +132,10 @@ public static class SingleplayerManagement
}
private static Dictionary<string, string> _confCache;
private static string ConfPath => Path.Combine(CrossPlatform.GetSaradominHome(), "game", "worldprops", "default.conf");
public static Dictionary<string, string> GrabConfCache()
{
var configPath = CrossPlatform.GetSingleplayerHome() + "/game/worldprops/default.conf";
return File.ReadLines(configPath)
return File.ReadLines(ConfPath)
.Where(line => line.Contains('=') && !line.TrimStart().StartsWith("#"))
.Select(l => l.Split(new[] { '#', '=' }, 3))
.ToDictionary(parts => parts[0].Trim(), parts => parts[1].Trim());
@ -143,12 +143,18 @@ public static class SingleplayerManagement
public static T ParseConf<T>(string key, T defaultValue = default)
{
Console.WriteLine($"Getting key {key}..");
if (!File.Exists(ConfPath)) return defaultValue;
_confCache ??= GrabConfCache();
if (!_confCache.TryGetValue(key, out var value)) return defaultValue;
if (!_confCache.TryGetValue(key, out var value))
{
Console.WriteLine($"Cache not hit - returning default {value}");
return defaultValue;
}
Console.WriteLine($"Cache hit - returning {value}");
return (T)Convert.ChangeType(value, typeof(T));
}
public static void WriteConf(string key, object value)
{
Console.WriteLine($"Writing {key}");

View file

@ -64,8 +64,10 @@ public static class Windows
return;
}
log("Starting process");
Process serverProcess = StartJavaProcess(javaExecutableLocation, serverJar, "2G", log, null);
log("waiting process");
while (!IsPortInUse(43595)) Thread.Sleep(1000);
StartJavaProcess(javaExecutableLocation, clientJar, "1G", null, () =>

View file

@ -17,6 +17,7 @@ using Glitonea.Mvvm;
using Saradomin.Infrastructure.Services;
using Saradomin.Model.Settings.Launcher;
using Saradomin.Utilities;
using Saradomin.View.Windows;
using static Saradomin.Utilities.SingleplayerManagement;
namespace Saradomin.ViewModel.Controls;
@ -25,6 +26,7 @@ public class SingleplayerViewModel : ViewModelBase
{
private readonly ISingleplayerUpdateService _singleplayerUpdateService;
private readonly ISettingsService _settingsService;
private readonly IJavaUpdateService _javaUpdateService;
public string SingleplayerDownloadText { get; private set; } =
Directory.Exists(CrossPlatform.GetSingleplayerHome()) ? "Update Singleplayer" : "Download Singleplayer";
@ -34,9 +36,11 @@ public class SingleplayerViewModel : ViewModelBase
public LauncherSettings Launcher => _settingsService.Launcher;
public SingleplayerViewModel(ISettingsService settingsService,
IJavaUpdateService javaUpdateService,
ISingleplayerUpdateService iSingleplayerUpdateService)
{
_settingsService = settingsService;
_javaUpdateService = javaUpdateService;
_singleplayerUpdateService = iSingleplayerUpdateService;
_singleplayerUpdateService.SingleplayerDownloadProgressChanged += OnSingleplayerDownloadProgressChanged;
@ -117,11 +121,19 @@ public class SingleplayerViewModel : ViewModelBase
if (IsServerTerminationLog(message)) CanLaunch = true;
}
public void LaunchSingleplayer()
public async void LaunchSingleplayer()
{
string javaVersionOutput = CrossPlatform.RunCommandAndGetOutput(
$"\"{Launcher.JavaExecutableLocation}\" -version"
);
if (!javaVersionOutput.Contains("11"))
{
await _javaUpdateService.DownloadAndSetJava11(_settingsService);
}
CanLaunch = false;
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
PrintLog("Starting windowsssssss");
new Task(() => Utilities.Singleplayer.Windows.WindowsLaunchServerAndClient(Launcher.JavaExecutableLocation, PrintLog)).Start();
}
else

View file

@ -265,7 +265,7 @@ namespace Saradomin.ViewModel.Windows
private bool IsJavaVersion11()
{
string javaVersionOutput = CrossPlatform.RunCommandAndGetOutput(
$"\"{_settingsService.Launcher.JavaExecutableLocation}\" -version"
$"\"{Launcher.JavaExecutableLocation}\" -version"
);
return javaVersionOutput.Contains("11");
}
@ -274,14 +274,19 @@ namespace Saradomin.ViewModel.Windows
{
LaunchText = $"Updating... (Downloading client - {e * 100:F2}%)";
}
private void OnJavaDownloadProgressUpdated(object sender, float e)
private void OnJavaDownloadProgressUpdated(object sender, Tuple<float, bool> e)
{
if (e >= 0.999f)
if (e.Item2)
{
LaunchText = "Play!";
return;
}
if (e.Item1 >= 0.999f)
{
LaunchText = "Updating... (Extracting Java 11)";
return;
}
LaunchText = $"Updating... (Downloading Java 11 - {e * 100:F2}%)";
LaunchText = $"Updating... (Downloading Java 11 - {e.Item1 * 100:F2}%)";
}
}
}