Skip to content

Commit

Permalink
Update to v0.1.0-beta4 (#53)
Browse files Browse the repository at this point in the history
* Remove redundant Clear method from NotificationProcedures

The Clear method was redundant and not being utilized anywhere in the codebase. Its removal streamlines the class and ensures consistency in the notification handling procedures.

* Refactor whitelist methods in ProfileProcedures

Converted async methods for adding and removing files and folders to non-blocking tasks. Also added bulk operations for adding and removing folders in IProfileProcedures interface to improve performance and maintainability.

* Update submodule link CmlLib.Core.Installer.Forge

* Update Minecraft server addresses in unit tests

Replaced old server addresses with new ones in multiple unit tests to reflect the current server IPs. Commented out a redundant assertion in the GmlManager test and added a TODO note to fix the endpoint.

* Refactor unit tests to use dynamic profile names

Replaced hardcoded profile names with a dynamic variable `name` to make the tests more flexible and maintainable. Additionally, commented out a redundant assertion in the installation test method to clean up the code.

* #52 Use case-insensitive comparison for profile names

Replaced the exact match check with a case-insensitive comparison when adding new profiles. This prevents duplicate profiles with names that differ only by case.

* Handle DirectoryNotFoundException in GameDownloader

Add specific catch block for DirectoryNotFoundException to provide a detailed warning message and log the unsupported operating system profile creation attempt. This ensures clearer feedback and better error handling for unsupported systems.

* Add support for game-specific arguments in process creation

Enhanced the profile and game downloader procedures to include game-specific arguments. Updated model interfaces and implementations to support these new arguments and ensured backward compatibility with existing JVM arguments.

* Add support for game arguments in profile procedures

Extended profile management functionality to include game arguments. Updated method signatures and relevant handlers to accept and process the new game arguments parameter. This ensures profiles can now be configured with specific game arguments alongside existing JVM arguments.

* Add GameArguments property to GameProfileInfo

This new property is meant to store game-specific arguments separately from JVM arguments. It increases the clarity and flexibility of the GameProfileInfo class.

* Add GameArguments property to GameProfileInfo

This new property is meant to store game-specific arguments separately from JVM arguments. It increases the clarity and flexibility of the GameProfileInfo class.

* Ensure file existence in LocalStorage verification

Previously, the code only checked for a non-null localFileInfo. The update adds an additional check to confirm that the file actually exists on the filesystem before proceeding. This helps prevent potential runtime errors and ensures that file operations are only attempted on valid files.

* Optimize game arguments handling in profile procedures

Updated the game arguments addition to use AddRange with the split method. This ensures that individual words in the arguments string are correctly separated and added, improving the handling of game arguments in the profile procedures.

* Add cache restoration flag to GetAllProfileFiles method

Updated the GetAllProfileFiles method across multiple classes to include an optional needRestoreCache parameter. This change enables conditional cache restoration and improves cache handling during file retrieval processes.

* Update submodule link CmlLib.Core.Installer.Forge

---------

Co-authored-by: Gru <[email protected]>
Co-authored-by: akemiko <[email protected]>
  • Loading branch information
3 people authored Aug 11, 2024
1 parent c7301d7 commit 13e198c
Show file tree
Hide file tree
Showing 11 changed files with 73 additions and 51 deletions.
2 changes: 1 addition & 1 deletion src/CmlLib.Core.Installer.Forge
3 changes: 2 additions & 1 deletion src/Gml.Core.Interfaces/Launcher/IGameProfile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public interface IGameProfile : IDisposable
List<IProfileServer> Servers { get; set; }
DateTimeOffset CreateDate { get; set; }
string? JvmArguments { get; set; }
string? GameArguments { get; set; }
ProfileState State { get; set; }

Task<bool> ValidateProfile();
Expand All @@ -47,6 +48,6 @@ public interface IGameProfile : IDisposable
void RemoveServer(IProfileServer server);
Task CreateModsFolder();
Task<IEnumerable<IFileInfo>> GetProfileFiles(string osName, string osArchitecture);
Task<IFileInfo[]> GetAllProfileFiles();
Task<IFileInfo[]> GetAllProfileFiles(bool needRestoreCache);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ public interface IGameDownloaderProcedures

Task<string> DownloadGame(string version, string? launchVersion, GameLoader loader,
IBootstrapProgram? bootstrapProgram);
Task<Process> CreateProcess(IStartupOptions startupOptions, IUser user, bool needDownload, string[] jvmArguments);
Task<IFileInfo[]> GetAllFiles();
Task<Process> CreateProcess(IStartupOptions startupOptions, IUser user, bool needDownload,
string[] jvmArguments, string[] gameArguments);
Task<IFileInfo[]> GetAllFiles(bool needRestoreCache);
bool GetLauncher(string launcherKey, out object launcher);
Task<IEnumerable<IFileInfo>> GetLauncherFiles(string osName, string osArchitecture);
}
Expand Down
4 changes: 2 additions & 2 deletions src/Gml.Core.Interfaces/Procedures/IProfileProcedures.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@ public interface IProfileProcedures
Task RemoveFileFromWhiteList(IGameProfile profile, IFileInfo file);
Task UpdateProfile(IGameProfile profile, string newProfileName, Stream? icon, Stream? backgroundImage,
string updateDtoDescription, bool isEnabled,
string jvmArguments);
string jvmArguments, string gameArguments);
Task<string[]> InstallAuthLib(IGameProfile profile);
Task<IGameProfileInfo?> GetCacheProfile(IGameProfile baseProfile);
Task SetCacheProfile(IGameProfileInfo profile);
Task CreateModsFolder(IGameProfile profile);
Task<IEnumerable<IFileInfo>> GetProfileFiles(IGameProfile profile, string osName, string osArchitecture);
Task<IFileInfo[]> GetAllProfileFiles(IGameProfile baseProfile);
Task<IFileInfo[]> GetAllProfileFiles(IGameProfile baseProfile, bool needRestoreCache);
Task<IEnumerable<string>> GetAllowVersions(GameLoader result, string? minecraftVersion);
Task ChangeBootstrapProgram(IGameProfile testGameProfile, IBootstrapProgram version);
Task AddFolderToWhiteList(IGameProfile profile, IFolderInfo folder);
Expand Down
2 changes: 1 addition & 1 deletion src/Gml.Core/Core/Helpers/Files/FileStorageProcedures.cs
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ public async Task<string> LoadFile(Stream fileStream,
case StorageType.LocalStorage:
var localFileInfo = await _storage.GetAsync<LocalFileInfo>(fileHash).ConfigureAwait(false);

if (localFileInfo is not null)
if (localFileInfo is not null && File.Exists(localFileInfo.FullPath))
{
// If it's an additional file
if (Path.GetFileName(localFileInfo.FullPath) is { } name && Guid.TryParse(name, out _))
Expand Down
12 changes: 11 additions & 1 deletion src/Gml.Core/Core/Helpers/Game/GameDownloader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,7 @@ private async Task CheckBuildJava()
}

public async Task<Process> GetProcessAsync(IStartupOptions startupOptions, IUser user, bool needDownload,
string[] jvmArguments)
string[] jvmArguments, string[] gameArguments)
{
if (!_launchers.TryGetValue($"{startupOptions.OsName}/{startupOptions.OsArch}", out var launcher))
{
Expand All @@ -457,9 +457,18 @@ await _notifications.SendMessage("Ошибка", "Выбранная опера
ServerIp = startupOptions.ServerIp,
ServerPort = startupOptions.ServerPort,
Session = session,
ExtraGameArguments = gameArguments.Select(c => new MArgument(c)),
ExtraJvmArguments = jvmArguments.Select(c => new MArgument(c))
}).AsTask();
}
catch (DirectoryNotFoundException exception)
{
var message =
$"Пропущено создание профиля {_profile.Name}, для OS: {anyLauncher.RulesContext.OS.Name}, {anyLauncher.RulesContext.OS.Arch}. Данная система не поддерживается.";
await _notifications.SendMessage(message, NotificationType.Warn);
_exception.OnNext(exception);
_loadLog.OnNext(message);
}
catch (Exception exception)
{
var message =
Expand All @@ -480,6 +489,7 @@ await _notifications.SendMessage("Ошибка", "Выбранная опера
ServerPort = startupOptions.ServerPort,
Session = session,
PathSeparator = startupOptions.OsName == "windows" ? ";" : ":",
ExtraGameArguments = gameArguments.Select(c => new MArgument(c)),
ExtraJvmArguments = jvmArguments.Select(c => new MArgument(c))
}).AsTask();
}
Expand Down
14 changes: 8 additions & 6 deletions src/Gml.Core/Core/Helpers/Game/GameDownloaderProcedures.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,14 @@ public Task<string> DownloadGame(string version, string? launchVersion, GameLoad
}

public async Task<Process> CreateProcess(IStartupOptions startupOptions, IUser user, bool needDownload,
string[] jvmArguments)
string[] jvmArguments, string[] gameArguments)
{
Process process = await _gameLoader.GetProcessAsync(startupOptions, user, needDownload, jvmArguments);
var process = await _gameLoader.GetProcessAsync(startupOptions, user, needDownload, jvmArguments, gameArguments);

return process;
}

public async Task<IFileInfo[]> GetAllFiles()
public async Task<IFileInfo[]> GetAllFiles(bool needRestoreCache = false)
{
List<string> directoryFiles = new List<string>();
var anyLauncher = _gameLoader.AnyLauncher;
Expand Down Expand Up @@ -88,7 +88,8 @@ public async Task<IFileInfo[]> GetAllFiles()

directoryFiles.AddRange(allFiles);

var localFilesInfo = await GetHashFiles(directoryFiles, []);
var localFilesInfo = await GetHashFiles(directoryFiles, [], needRestoreCache);

localFilesInfo = localFilesInfo
.GroupBy(c => c.Hash)
.Select(c => c.First())
Expand Down Expand Up @@ -205,12 +206,13 @@ private static bool GetJavaRuntimeFolder(string osName, string osArchitecture, M
return !string.IsNullOrEmpty(runtimeFolder);
}

private async Task<LocalFileInfo[]> GetHashFiles(IEnumerable<string> files, string[] additionalPath)
private async Task<LocalFileInfo[]> GetHashFiles(IEnumerable<string> files, string[] additionalPath,
bool needRestoreCache = false)
{
var localFilesInfo = await Task.WhenAll(files.AsParallel().Select(c =>
{
string hash;
if (_fileHashCache.TryGetValue(c, out var value))
if (!needRestoreCache && _fileHashCache.TryGetValue(c, out var value))
{
hash = value;
}
Expand Down
31 changes: 19 additions & 12 deletions src/Gml.Core/Core/Helpers/Profiles/ProfileProcedures.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public async Task AddProfile(IGameProfile? profile)

public async Task<bool> CanAddProfile(string name, string version, string loaderVersion, GameLoader dtoGameLoader)
{
if (_gameProfiles.Any(c => c.Name == name))
if (_gameProfiles.Any(c => string.Equals(c.Name, name, StringComparison.OrdinalIgnoreCase)))
return false;

var versions = await GetAllowVersions(dtoGameLoader, version);
Expand Down Expand Up @@ -301,27 +301,29 @@ public async Task<IEnumerable<IFileInfo>> GetProfileFiles(IGameProfile baseProfi
var relativePath = Path.Combine("clients", profileName);

var jvmArgs = new List<string>();
var gameArguments = new List<string>();

if (profile.JvmArguments is not null)
{
jvmArgs.Add(profile.JvmArguments);
}

var files =
await profile.GetProfileFiles(startupOptions.OsName, startupOptions.OsArch);

if (files!.Any(c => c.Name == Path.GetFileName(AuthLibUrl)))
if (files.Any(c => c.Name == Path.GetFileName(AuthLibUrl)))
{
var authLibRelativePath = Path.Combine(profile.ClientPath, "libraries", "custom", Path.GetFileName(AuthLibUrl));
jvmArgs.Add($"-javaagent:{authLibRelativePath}={{authEndpoint}}");
}

if (profile.GameArguments is not null)
gameArguments.AddRange(profile.GameArguments.Split(' '));

Process? process = default;

try
{
process = await profile.GameLoader.CreateProcess(startupOptions, user, false,
jvmArgs.ToArray());
jvmArgs.ToArray(), gameArguments.ToArray());
}
catch (Exception exception)
{
Expand All @@ -345,6 +347,7 @@ public async Task<IEnumerable<IFileInfo>> GetProfileFiles(IGameProfile baseProfi
Description = profile.Description,
IconBase64 = profile.IconBase64,
JvmArguments = profile.JvmArguments,
GameArguments = profile.GameArguments,
HasUpdate = profile.State != ProfileState.Loading,
Arguments = arguments,
JavaPath = javaPath,
Expand Down Expand Up @@ -389,7 +392,7 @@ public async Task<IEnumerable<IFileInfo>> GetProfileFiles(IGameProfile baseProfi
var authLibArguments = await profile.InstallAuthLib();
await profile.CreateModsFolder();
var process =
await profile.GameLoader.CreateProcess(StartupOptions.Empty, Core.User.User.Empty, true, authLibArguments);
await profile.GameLoader.CreateProcess(StartupOptions.Empty, Core.User.User.Empty, true, authLibArguments, []);

var files = (await GetProfileFiles(profile)).ToList();
var files2 = GetWhiteListFilesProfileFiles(files);
Expand Down Expand Up @@ -419,7 +422,7 @@ public async Task<IEnumerable<IFileInfo>> GetProfileFiles(IGameProfile baseProfi

public async Task PackProfile(IGameProfile profile)
{
var fileInfos = await profile.GetAllProfileFiles();
var fileInfos = await profile.GetAllProfileFiles(true);
var totalFiles = fileInfos.Length;
var processed = 0;

Expand Down Expand Up @@ -523,7 +526,8 @@ public async Task UpdateProfile(IGameProfile profile,
Stream? backgroundImage,
string updateDtoDescription,
bool isEnabled,
string jvmArguments)
string jvmArguments,
string gameArguments)
{
var directory =
new DirectoryInfo(Path.Combine(_launcherInfo.InstallationDirectory, "clients", profile.Name));
Expand All @@ -544,7 +548,7 @@ public async Task UpdateProfile(IGameProfile profile,
: await _gmlManager.Files.LoadFile(backgroundImage, "profile-backgrounds");

await UpdateProfile(profile, newProfileName, iconBase64, backgroundKey, updateDtoDescription,
needRenameFolder, directory, newDirectory, isEnabled, jvmArguments);
needRenameFolder, directory, newDirectory, isEnabled, jvmArguments, gameArguments);
}

private async Task<string> ConvertStreamToBase64Async(Stream stream)
Expand All @@ -560,14 +564,17 @@ private async Task<string> ConvertStreamToBase64Async(Stream stream)
private async Task UpdateProfile(IGameProfile profile, string newProfileName, string newIcon,
string backgroundImageKey,
string newDescription, bool needRenameFolder, DirectoryInfo directory, DirectoryInfo newDirectory,
bool isEnabled, string jvmArguments)
bool isEnabled,
string jvmArguments,
string gameArguments)
{
profile.Name = newProfileName;
profile.IconBase64 = newIcon;
profile.BackgroundImageKey = backgroundImageKey;
profile.Description = newDescription;
profile.IsEnabled = isEnabled;
profile.JvmArguments = jvmArguments;
profile.GameArguments = gameArguments;

profile.GameLoader = new GameDownloaderProcedures(_launcherInfo, _storageService, profile, _notifications);

Expand Down Expand Up @@ -640,9 +647,9 @@ public Task<IEnumerable<IFileInfo>> GetProfileFiles(
return profile.GameLoader.GetLauncherFiles(osName, osArchitecture);
}

public Task<IFileInfo[]> GetAllProfileFiles(IGameProfile baseProfile)
public Task<IFileInfo[]> GetAllProfileFiles(IGameProfile baseProfile, bool needRestoreCache = false)
{
return baseProfile.GameLoader.GetAllFiles();
return baseProfile.GameLoader.GetAllFiles(needRestoreCache);
}

public async Task<IEnumerable<string>> GetAllowVersions(GameLoader gameLoader, string? minecraftVersion)
Expand Down
1 change: 1 addition & 0 deletions src/Gml.Core/Core/Launcher/GameProfileInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,6 @@ public class GameProfileInfo : IGameProfileInfo
public string LaunchVersion { get; set; }
public string Arguments { get; set; }
public string JvmArguments { get; set; }
public string GameArguments { get; set; }
}
}
26 changes: 13 additions & 13 deletions src/Gml.Core/Models/BaseProfile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using System.Threading.Tasks;
using Gml.Models.Converters;
using Gml.Models.Enums;
using Gml.Web.Api.Domains.System;
using GmlCore.Interfaces.Enums;
using GmlCore.Interfaces.Launcher;
using GmlCore.Interfaces.Procedures;
Expand Down Expand Up @@ -50,7 +49,8 @@ internal BaseProfile(string name, string gameVersion, GameLoader loader)
public string BackgroundImageKey { get; set; }
public string Description { get; set; }

public string JvmArguments { get; set; }
public string? JvmArguments { get; set; }
public string? GameArguments { get; set; }
public ProfileState State { get; set; }

[JsonConverter(typeof(LocalFileInfoConverter))]
Expand Down Expand Up @@ -84,25 +84,25 @@ public Task<Process> CreateProcess(IStartupOptions startupOptions, IUser user)
{
CheckDispose();

return GameLoader.CreateProcess(startupOptions, user, false, []);
return GameLoader.CreateProcess(startupOptions, user, false, [], []);
}

public async Task<bool> CheckClientExists()
public Task<bool> CheckClientExists()
{
CheckDispose();

//ToDo: Доделать
// return GameLoader.CheckClientExists(this);
return true;
return Task.FromResult(true);
}

public async Task<bool> CheckOsTypeLoaded(IStartupOptions startupOptions)
public Task<bool> CheckOsTypeLoaded(IStartupOptions startupOptions)
{
CheckDispose();

//ToDo: Доделать
// return GameLoader.CheckOsTypeLoaded(this, startupOptions);
return true;
return Task.FromResult(true);
}

public Task<string[]> InstallAuthLib()
Expand All @@ -124,14 +124,14 @@ public Task<IProfileServer> AddMinecraftServer(string serverName, string address
return ServerProcedures.AddMinecraftServer(this, serverName, address, port);
}

public async Task<bool> CheckIsFullLoaded(IStartupOptions startupOptions)
public Task<bool> CheckIsFullLoaded(IStartupOptions startupOptions)
{
CheckDispose();

//ToDo: Доделать
// return await GameLoader.IsFullLoaded(this, startupOptions);

return true;
return Task.FromResult(true);
}

public async Task Remove()
Expand All @@ -151,7 +151,7 @@ public void Dispose()
IsDisposed = true;
}

public async Task<bool> CheckIsLoaded()
public Task<bool> CheckIsLoaded()
{
CheckDispose();

Expand All @@ -160,7 +160,7 @@ public async Task<bool> CheckIsLoaded()
// ? NullableBool.True
// : NullableBool.False;

return IsValidProfile == NullableBool.True;
return Task.FromResult(IsValidProfile == NullableBool.True);
}

private void CheckDispose()
Expand Down Expand Up @@ -200,9 +200,9 @@ public Task<IEnumerable<IFileInfo>> GetProfileFiles(string osName, string osArch
return ProfileProcedures.GetProfileFiles(this, osName, osArchitecture);
}

public Task<IFileInfo[]> GetAllProfileFiles()
public Task<IFileInfo[]> GetAllProfileFiles(bool needRestoreCache = false)
{
return ProfileProcedures.GetAllProfileFiles(this);
return ProfileProcedures.GetAllProfileFiles(this, needRestoreCache);
}
}
}
Loading

0 comments on commit 13e198c

Please sign in to comment.