Skip to content

Commit

Permalink
Merge pull request #46 from Gml-Launcher/develop
Browse files Browse the repository at this point in the history
Update to v1.0.4
  • Loading branch information
GamerVII-NET authored Jan 26, 2025
2 parents d2cf4da + 053effa commit 1ca314c
Show file tree
Hide file tree
Showing 43 changed files with 776 additions and 300 deletions.
37 changes: 37 additions & 0 deletions src/Gml.Client/GmlClientManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
using Gml.Client.Extensions;
using Gml.Client.Helpers;
using Gml.Web.Api.Domains.System;
using Gml.Web.Api.Dto.Files;
using Gml.Web.Api.Dto.Messages;
using Gml.Web.Api.Dto.Mods;
using Gml.Web.Api.Dto.Profile;
using GmlCore.Interfaces.Storage;
using GmlCore.Interfaces.User;
Expand Down Expand Up @@ -68,6 +70,31 @@ public Task<ResponseMessage<List<ProfileReadDto>>> GetProfiles()
return _apiProcedures.GetProfiles();
}

public Task<ResponseMessage<List<ModsDetailsInfoDto>>> GetOptionalModsInfo(string accessToken)
{
return _apiProcedures.GetOptionalModsInfo(accessToken);
}

public Task<ResponseMessage<List<ModReadDto>>> GetOptionalMods(string profileName, string accessToken)
{
return _apiProcedures.GetOptionalMods(profileName, accessToken);
}

public bool ToggleOptionalMod(string path, bool isEnebled)
{
try
{
var newFileName = _apiProcedures.ToggleOptionalMod(path, isEnebled);
File.Move(path, newFileName);
}
catch
{
return false;
}

return true;
}

public Task<ResponseMessage<List<ProfileReadDto>>> GetProfiles(string accessToken)
{
return _apiProcedures.GetProfiles(accessToken);
Expand Down Expand Up @@ -139,6 +166,12 @@ public async Task DownloadNotInstalledFiles(ProfileReadInfoDto profileInfo,
await _apiProcedures.DownloadFiles(InstallationDirectory, updateFiles.ToArray(), 60, cancellationToken);
}

public async Task DownloadFiles(ProfileFileReadDto[] profileInfo,
CancellationToken cancellationToken = default)
{
await _apiProcedures.DownloadFiles(InstallationDirectory, profileInfo.ToArray(), 60, cancellationToken);
}

public async Task<(IUser User, string Message, IEnumerable<string> Details)> Auth(string login, string password,
string hwid)
{
Expand Down Expand Up @@ -177,7 +210,11 @@ public async Task OpenServerConnection(IUser user)
{
_profilesChangedEvent?.Dispose();
if (_launchBackendConnection is not null)
{
await _launchBackendConnection.DisposeAsync();
_profilesChangedEvent?.Dispose();
_profilesChangedEvent = null;
}

_launchBackendConnection = new SignalRConnect($"{_webSocketAddress}/ws/launcher", user);
_profilesChangedEvent ??= _launchBackendConnection.ProfilesChanges.Subscribe(_profilesChanged);
Expand Down
83 changes: 81 additions & 2 deletions src/Gml.Client/Helpers/ApiProcedures.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using Gml.Web.Api.Dto.Files;
using Gml.Web.Api.Dto.Integration;
using Gml.Web.Api.Dto.Messages;
using Gml.Web.Api.Dto.Mods;
using Gml.Web.Api.Dto.Player;
using Gml.Web.Api.Dto.Profile;
using Gml.Web.Api.Dto.Texture;
Expand Down Expand Up @@ -207,15 +208,14 @@ private void ChangeProcessRules(string startInfoFileName, OsType profileDtoOsTyp
case OsType.Undefined:
break;
case OsType.Linux:
case OsType.OsX:
var chmodStartInfo = new ProcessStartInfo
{
FileName = "/bin/bash",
Arguments = $"-c \"chmod +x {startInfoFileName}\""
};
Process.Start(chmodStartInfo);
break;
case OsType.OsX:
break;
case OsType.Windows:
break;
default:
Expand Down Expand Up @@ -320,6 +320,7 @@ public static async Task<string> GetSentryLink(string hostUrl)
if (response.IsSuccessStatusCode && dto != null)
{
authUser.Uuid = dto.Data!.Uuid;
authUser.Name = dto.Data.Name;
authUser.AccessToken = dto.Data!.AccessToken;
authUser.Has2Fa = false;
authUser.ExpiredDate = dto.Data!.ExpiredDate;
Expand Down Expand Up @@ -473,6 +474,11 @@ private async Task DownloadFile(string installationDirectory, ProfileFileReadDto
file.Directory.TrimStart(Path.DirectorySeparatorChar).TrimStart('\\'));
await EnsureDirectoryExists(localPath);

if (IsOptionalMod(localPath))
{
localPath = ToggleOptionalMod(localPath);
}

var url = $"{_httpClient.BaseAddress.AbsoluteUri}api/v1/file/{file.Hash}";

await using (var fs = new FileStream(localPath, FileMode.OpenOrCreate))
Expand Down Expand Up @@ -511,6 +517,21 @@ private async Task DownloadFile(string installationDirectory, ProfileFileReadDto
}
}

public string ToggleOptionalMod(string localPath)
{
if (IsOptionalMod(localPath))
{
return $"{localPath}.disabled";
}

return localPath.Replace(".disabled", string.Empty);
}

public static bool IsOptionalMod(string localPath)
{
return localPath.Contains("-optional-mod.jar");
}

private Task EnsureDirectoryExists(string localPath)
{
var directory = Path.GetDirectoryName(localPath);
Expand Down Expand Up @@ -708,4 +729,62 @@ private string GetOsName(OsType osType, Architecture osArch)
#endif
return versionBuilder.ToString();
}

public async Task<ResponseMessage<List<ModsDetailsInfoDto>>> GetOptionalModsInfo(string accessToken)
{
#if DEBUG
Debug.WriteLine("Calling GetOptionalMods()");
#endif
Debug.Write("Load profiles: ");
if (!_httpClient.DefaultRequestHeaders.TryGetValues("Authorization", out _))
_httpClient.DefaultRequestHeaders.Add("Authorization", $"Bearer {accessToken}");

var response = await _httpClient.GetAsync("/api/v1/mods/details").ConfigureAwait(false);

Debug.WriteLine(response.IsSuccessStatusCode ? "Success load" : "Failed load");

if (!response.IsSuccessStatusCode)
return new ResponseMessage<List<ModsDetailsInfoDto>>();

var content = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
#if DEBUG
Debug.WriteLine(response.IsSuccessStatusCode
? $"Mods loaded successfully: {content}"
: "Failed to load profiles.");
#endif
return JsonConvert.DeserializeObject<ResponseMessage<List<ModsDetailsInfoDto>>>(content)
?? new ResponseMessage<List<ModsDetailsInfoDto>>();
}

public async Task<ResponseMessage<List<ModReadDto>>> GetOptionalMods(string profileName, string accessToken)
{
#if DEBUG
Debug.WriteLine("Calling GetOptionalMods()");
#endif
Debug.Write("Load profiles: ");
if (!_httpClient.DefaultRequestHeaders.TryGetValues("Authorization", out _))
_httpClient.DefaultRequestHeaders.Add("Authorization", $"Bearer {accessToken}");

var response = await _httpClient.GetAsync($"/api/v1/profiles/{profileName}/mods/optionals")
.ConfigureAwait(false);

Debug.WriteLine(response.IsSuccessStatusCode ? "Success load" : "Failed load");

if (!response.IsSuccessStatusCode)
return new ResponseMessage<List<ModReadDto>>();

var content = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
#if DEBUG
Debug.WriteLine(response.IsSuccessStatusCode
? $"Mods loaded successfully: {content}"
: "Failed to load profiles.");
#endif
return JsonConvert.DeserializeObject<ResponseMessage<List<ModReadDto>>>(content)
?? new ResponseMessage<List<ModReadDto>>();
}

public string ToggleOptionalMod(string localPath, bool isEnabled)
{
return isEnabled ? localPath.Replace(".disabled", string.Empty) : $"{localPath}.disabled";
}
}
27 changes: 25 additions & 2 deletions src/Gml.Client/Helpers/SystemIOProcedures.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,19 @@ public Task RemoveFiles(ProfileReadInfoDto profileInfo)
.Select(wf => Path.GetFullPath(GetRealFilePath(_installationDirectory, wf))))
.ToHashSet();

bool IsNeedRemove(FileInfo f)
bool IsNeedRemove(FileInfo fileInfo)
{
return f.Length == 0 || !hashSet.Contains(f.FullName) && !allowedPaths.Any(path => f.FullName.StartsWith(path));
if (fileInfo.Length == 0)
{
return true;
}

if (!hashSet.Contains(fileInfo.FullName) && !allowedPaths.Any(path => fileInfo.FullName.StartsWith(path)))
{
return CompareHashOptionalMods(profileInfo, fileInfo);
}

return CompareHashOptionalMods(profileInfo, fileInfo);
}

foreach (var file in localFiles.Where(IsNeedRemove))
Expand All @@ -116,6 +126,19 @@ bool IsNeedRemove(FileInfo f)
return Task.CompletedTask;
}

private static bool CompareHashOptionalMods(ProfileReadInfoDto profileInfo, FileInfo fileInfo)
{
if (!ApiProcedures.IsOptionalMod(fileInfo.FullName))
{
return profileInfo.Files.Any(c => c.Name == fileInfo.Name) == false;
}

using var hash = new SHA256Managed();
var fileHash = SystemHelper.CalculateFileHash(fileInfo.FullName, hash);

return profileInfo.Files.All(c => c.Hash != fileHash);
}

private string GetRealFilePath(string installationDirectory, ProfileFileReadDto file)
{
if (_osType == OsType.Windows)
Expand Down
8 changes: 8 additions & 0 deletions src/Gml.Client/IGmlClientManager.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using System.Diagnostics;
using System.Runtime.InteropServices;
using Gml.Web.Api.Domains.System;
using Gml.Web.Api.Dto.Files;
using Gml.Web.Api.Dto.Messages;
using Gml.Web.Api.Dto.Mods;
using Gml.Web.Api.Dto.Profile;
using GmlCore.Interfaces.Storage;
using GmlCore.Interfaces.User;
Expand All @@ -19,6 +21,7 @@ public interface IGmlClientManager : IDisposable
string InstallationDirectory { get; }
bool SkipUpdate { get; set; }
Task<ResponseMessage<List<ProfileReadDto>>> GetProfiles();
Task<ResponseMessage<List<ModsDetailsInfoDto>>> GetOptionalModsInfo(string accessToken);
Task<ResponseMessage<List<ProfileReadDto>>> GetProfiles(string accessToken);
Task<ResponseMessage<ProfileReadInfoDto?>?> GetProfileInfo(ProfileCreateInfoDto profileDto);
public Task<Process> GetProcess(ProfileReadInfoDto profileDto, OsType osType);
Expand All @@ -36,4 +39,9 @@ Task UpdateCurrentLauncher((IVersionFile? ActualVersion, bool IsActuallVersion)
Task OpenServerConnection(IUser user);
void ChangeInstallationFolder(string installationDirectory);
Task<IPlayerTexture?> GetTexturesByName(string userName);
Task<ResponseMessage<List<ModReadDto>>> GetOptionalMods(string profileName, string accessToken);
bool ToggleOptionalMod(string path, bool isEnebled);

Task DownloadFiles(ProfileFileReadDto[] profileInfo,
CancellationToken cancellationToken = default);
}
14 changes: 8 additions & 6 deletions src/Gml.Core.Interfaces/Auth/IAuthServiceInfo.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
using Gml.Web.Api.Domains.System;
using GmlCore.Interfaces.Enums;

namespace GmlCore.Interfaces.Auth;

public interface IAuthServiceInfo
namespace GmlCore.Interfaces.Auth
{
public string Name { get; set; }
public AuthType AuthType { get; set; }
string Endpoint { get; set; }
public interface IAuthServiceInfo
{
public string Name { get; set; }
public AuthType AuthType { get; set; }
string Endpoint { get; set; }
}
}
2 changes: 1 addition & 1 deletion src/Gml.Core.Interfaces/Enums/AuthType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ public enum AuthType
EasyCabinet = 4,
UnicoreCMS = 5,
CustomEndpoint = 6,
NamelessMC = 7
NamelessMC = 7,
}
20 changes: 11 additions & 9 deletions src/Gml.Core.Interfaces/Enums/GameLoader.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
namespace GmlCore.Interfaces.Enums;

public enum GameLoader
namespace GmlCore.Interfaces.Enums
{
Undefined = 0,
Vanilla = 1,
Forge = 2,
Fabric = 3,
LiteLoader = 4,
NeoForge = 5
public enum GameLoader
{
Undefined = 0,
Vanilla = 1,
Forge = 2,
Fabric = 3,
LiteLoader = 4,
NeoForge = 5,
Quilt = 6,
}
}
11 changes: 6 additions & 5 deletions src/Gml.Core.Interfaces/Enums/StorageType.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
namespace GmlCore.Interfaces.Enums;

public enum StorageType
namespace GmlCore.Interfaces.Enums
{
LocalStorage = 0,
S3 = 1
public enum StorageType
{
LocalStorage = 0,
S3 = 1
}
}
4 changes: 2 additions & 2 deletions src/Gml.Core.Interfaces/Gml.Core.Interfaces.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Http.Features" Version="5.0.17" />
<PackageReference Include="System.Text.Json" Version="9.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Http.Features" Version="2.2.0" />
<PackageReference Include="System.Text.Json" Version="8.0.5" />
</ItemGroup>

</Project>
26 changes: 14 additions & 12 deletions src/Gml.Core.Interfaces/IGmlManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,19 @@
using GmlCore.Interfaces.Launcher;
using GmlCore.Interfaces.Procedures;

namespace GmlCore.Interfaces;

public interface IGmlManager
namespace GmlCore.Interfaces
{
public ILauncherInfo LauncherInfo { get; }
public IBugTrackerProcedures BugTracker { get; }
public IProfileProcedures Profiles { get; }
public IFileStorageProcedures Files { get; }
public IServicesIntegrationProcedures Integrations { get; }
public IUserProcedures Users { get; }
public ILauncherProcedures Launcher { get; }
IProfileServersProcedures Servers { get; }
INotificationProcedures Notifications { get; }
public interface IGmlManager
{
public ILauncherInfo LauncherInfo { get; }
public IBugTrackerProcedures BugTracker { get; }
public IProfileProcedures Profiles { get; }
public IFileStorageProcedures Files { get; }
public IServicesIntegrationProcedures Integrations { get; }
public IUserProcedures Users { get; }
public ILauncherProcedures Launcher { get; }
IProfileServersProcedures Servers { get; }
INotificationProcedures Notifications { get; }
IModsProcedures Mods { get; }
}
}
Loading

0 comments on commit 1ca314c

Please sign in to comment.