Skip to content

Commit

Permalink
added better java handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Enn3Developer committed May 27, 2024
1 parent ebf8420 commit 28fb37a
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 44 deletions.
36 changes: 36 additions & 0 deletions Data/InstallProgress.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System.IO;
using System.Threading.Tasks;
using SharpCompress.Common;
using SharpCompress.Readers;

namespace NewRGB.Data;

public class InstallProgress(string dir, FileStream fileStream, IReader reader, string zip)
{
public static async Task<InstallProgress> Install(string dir, string zip)
{
if (!Directory.Exists(dir)) Directory.CreateDirectory(dir);
var fileStream = File.OpenRead(zip);
var reader = await Task.Run(() => ReaderFactory.Open(fileStream));
return new InstallProgress(dir, fileStream, reader, zip);
}

public async Task Run()
{
var opt = new ExtractionOptions
{
ExtractFullPath = true,
Overwrite = true
};
while (await Task.Run(reader.MoveToNextEntry))
if (!reader.Entry.IsDirectory)
await Task.Run(() => reader.WriteEntryToDirectory(dir, opt));
}

public async Task End()
{
reader.Dispose();
await fileStream.DisposeAsync();
File.Delete(zip);
}
}
32 changes: 2 additions & 30 deletions Data/Technic.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Net.Http;
using System.Text.Json.Nodes;
using System.Threading.Tasks;
Expand All @@ -15,7 +13,7 @@ public class Technic(string modpackName)
private const int BuildVersion = 69420;
private string _version = "";
private string _downloadUrl = "";
private string _modpackZipPath = Path.Combine(DataManager.Instance.DataPath, "modpack.zip");
private readonly string _modpackZipPath = Path.Combine(DataManager.Instance.DataPath, "modpack.zip");

public async Task Init()
{
Expand Down Expand Up @@ -53,32 +51,6 @@ public async Task<InstallProgress> InstallUpdate()
var modDir = Path.Combine(mcDir, "mods");
if (!Directory.Exists(mcDir)) Directory.CreateDirectory(mcDir);
if (Directory.Exists(modDir)) Directory.Delete(modDir, true);
var archive = await Task.Run(() => ZipFile.OpenRead(_modpackZipPath));
var fileList = new List<ZipArchiveEntry>(256);
fileList.AddRange(archive.Entries);
return new InstallProgress(fileList, mcDir, _modpackZipPath, archive);
}
}

public class InstallProgress(List<ZipArchiveEntry> fileList, string mcDir, string filePath, ZipArchive archive)
{
public int Length => fileList.Count;

public async Task Progress(int i)
{
if (i >= Length) return;
var file = fileList[i];
var path = Path.Combine(mcDir, file.FullName.Replace('/', Path.DirectorySeparatorChar));
if (path.EndsWith(Path.DirectorySeparatorChar))
Directory.CreateDirectory(path);
else
await Task.Run(() => file.ExtractToFile(path, true));
}

public void End()
{
fileList.Clear();
archive.Dispose();
File.Delete(filePath);
return await InstallProgress.Install(mcDir, _modpackZipPath);
}
}
1 change: 1 addition & 0 deletions NewRGB.csproj
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<Version>0.2.4</Version>
<OutputType>WinExe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
Expand Down
99 changes: 85 additions & 14 deletions ViewModels/MainViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
using System.Net.Http;
using System.Reactive;
using System.Runtime.InteropServices;
using System.Security.AccessControl;
using System.Text.Json;
using System.Threading.Tasks;
using mcswlib.ServerStatus;
using mcswlib.ServerStatus.Event;
using NewRGB.Data;
using ProjBobcat.Class.Model;
using ProjBobcat.Class.Model.LauncherProfile;
Expand All @@ -21,6 +21,7 @@
using ReactiveUI;
using Velopack;
using Velopack.Sources;
using FileInfo = System.IO.FileInfo;

namespace NewRGB.ViewModels;

Expand All @@ -36,6 +37,7 @@ public class MainViewModel : ViewModelBase
private readonly UpdateManager _updateManager;
private UpdateInfo? _updateInfo;
private string _serverInfo = "0/20";
private bool _needsJava;

public MainViewModel()
{
Expand All @@ -48,7 +50,8 @@ public MainViewModel()
"SOMETHING_WENT_WRONG_2";
else
Username = "SOMETHING_WENT_WRONG_3";
_updateManager = new UpdateManager(new GithubSource("https://github.com/rgbcraft/NewRGBLauncher/", null, false));
_updateManager =
new UpdateManager(new GithubSource("https://github.com/rgbcraft/NewRGBLauncher/", null, false));
}

public ReactiveCommand<Unit, Unit> LogoCommand { get; }
Expand Down Expand Up @@ -86,6 +89,22 @@ public string ServerInfo
private async Task<string?> CheckJava()
{
UpdateProgress(0.0f, "Checking Java version", false);
var possibleJavaPath =
Path.Combine(DataManager.Instance.DataPath, "runtime", "jdk-21.0.3+9-jre", "bin", "java");
if (File.Exists(possibleJavaPath))
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
new FileInfo(possibleJavaPath).GetAccessControl().AddAccessRule(new FileSystemAccessRule(
Environment.UserName, FileSystemRights.ReadAndExecute,
AccessControlType.Allow));
else
File.SetUnixFileMode(possibleJavaPath,
File.GetUnixFileMode(possibleJavaPath) | UnixFileMode.UserExecute);

if (await DataManager.IsValidJava(possibleJavaPath))
return possibleJavaPath;
}

var javaList = ProjBobcat.Class.Helper.SystemInfoHelper.FindJava();
var javaEnumerator = javaList.GetAsyncEnumerator();
var valid = false;
Expand All @@ -96,6 +115,7 @@ public string ServerInfo
break;
}

valid = false;
return valid ? javaEnumerator.Current : null;
}

Expand Down Expand Up @@ -286,7 +306,51 @@ private async Task Launch(string javaPath)
Console.WriteLine(result.Error.Cause);
}

private async Task OnPlayButton()
private async Task InstallJava()
{
UpdateProgress(0.0f, "Downloading Java");
DownloadProgress? progress = null;
string? path = null;
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
path = Path.Combine(DataManager.Instance.DataPath, "java.tar");
progress = await DownloadProgress.Download(
"https://github.com/adoptium/temurin21-binaries/releases/download/jdk-21.0.3%2B9/OpenJDK21U-jre_x64_linux_hotspot_21.0.3_9.tar.gz",
path);
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
{
path = Path.Combine(DataManager.Instance.DataPath, "java.tar");
progress = await DownloadProgress.Download(
"https://github.com/adoptium/temurin21-binaries/releases/download/jdk-21.0.3%2B9/OpenJDK21U-jre_aarch64_mac_hotspot_21.0.3_9.tar.gz",
path);
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
path = Path.Combine(DataManager.Instance.DataPath, "java.zip");
progress = await DownloadProgress.Download(
"https://github.com/adoptium/temurin21-binaries/releases/download/jdk-21.0.3%2B9/OpenJDK21U-jre_x64_windows_hotspot_21.0.3_9.zip",
path);
}

if (progress == null || path == null)
{
UpdateProgress(1.0f, "Can't download java");
return;
}

await DownloadAndProgress(progress, "Java");
UpdateProgress(0.0f, "Installing Java", false);
var installProgress =
await InstallProgress.Install(Path.Combine(DataManager.Instance.DataPath, "runtime"), path);
await installProgress.Run();
await installProgress.End();
UpdateProgress(1.0f, "Java installed");
_needsJava = false;
PlayText = "Play";
}

private async Task PlayButtonRun()
{
if (_gameProcess != null)
{
Expand All @@ -303,6 +367,10 @@ await _updateManager.DownloadUpdatesAsync(_updateInfo,
await Task.Delay(1000);
_updateManager.ApplyUpdatesAndRestart(_updateInfo);
}
else if (_needsJava)
{
await InstallJava();
}
else if (_needsUpdate)
{
await DownloadUpdate();
Expand All @@ -318,6 +386,8 @@ await _updateManager.DownloadUpdatesAsync(_updateInfo,
if (javaPath == null)
{
UpdateProgress(1.0f, "No valid java installation found");
PlayText = "Install";
_needsJava = true;
return;
}

Expand All @@ -333,6 +403,11 @@ await _updateManager.DownloadUpdatesAsync(_updateInfo,
}
}

private async Task OnPlayButton()
{
await PlayButtonRun();
}

private async Task DownloadUpdate()
{
UpdateProgress(0.0f, "Starting update", false);
Expand Down Expand Up @@ -367,24 +442,20 @@ private async Task DownloadAndProgress(DownloadProgress progress, string desc)
private async Task InstallUpdate()
{
var progress = await _technic.InstallUpdate();
UpdateProgress(0.0f, "Installing update");
for (var i = 0; i < progress.Length; i++)
{
await progress.Progress(i);
UpdateProgress((float)i / progress.Length, "Installing update");
}
UpdateProgress(0.0f, "Installing update", false);
await progress.Run();

progress.End();
await progress.End();

UpdateProgress(1.0f, "Installing update");
UpdateProgress(1.0f, "Update installed");
}

public async Task AsyncOnLoaded()
{
UpdateProgress(0.0f, "Checking for launcher updates", false);

var factory = new ServerStatusFactory();
factory.ServerChanged += (sender, e) =>
factory.ServerChanged += (sender, _) =>
{
var srv = (ServerStatus)sender!;
ServerInfo = $"{srv.PlayerCount}/{srv.MaxPlayerCount}";
Expand All @@ -401,9 +472,9 @@ public async Task AsyncOnLoaded()
return;
}
}
catch (Exception)
catch (Exception e)
{
// ignored
Console.Error.WriteLine(e);
}

UpdateProgress(0.0f, "Checking for updates", false);
Expand Down

0 comments on commit 28fb37a

Please sign in to comment.