-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
64a5fd4
commit 5ea1ed8
Showing
9 changed files
with
123 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,54 +1,76 @@ | ||
using OneWare.SDK.Helpers; | ||
using Avalonia.Media; | ||
using OneWare.SDK.Enums; | ||
using OneWare.SDK.Helpers; | ||
using OneWare.SDK.NativeTools; | ||
using OneWare.SDK.Services; | ||
|
||
namespace OneWare.Core.Services; | ||
|
||
public class NativeToolService(IHttpService httpService, ISettingsService settingsService, IPaths paths, ILogger logger) : INativeToolService | ||
public class NativeToolService(IHttpService httpService, ISettingsService settingsService, IPaths paths, IApplicationStateService applicationStateService, ILogger logger) : INativeToolService | ||
{ | ||
private readonly Dictionary<PlatformId, Dictionary<string, NativeTool>> _nativeTools = new(); | ||
private readonly Dictionary<string, NativeToolContainer> _nativeTools = new(); | ||
|
||
public NativeTool Register(string id, string url, params PlatformId[] supportedPlatforms) | ||
public NativeToolContainer Register(string key) | ||
{ | ||
var nativeTool = new NativeTool(id, url, Path.Combine(paths.NativeToolsDirectory, id)); | ||
foreach (var platform in supportedPlatforms) | ||
{ | ||
_nativeTools.TryAdd(platform, new Dictionary<string, NativeTool>()); | ||
_nativeTools[platform].Add(id, nativeTool); | ||
} | ||
return nativeTool; | ||
_nativeTools[key] = new NativeToolContainer(key, Path.Combine(paths.NativeToolsDirectory, key)); | ||
return _nativeTools[key]; | ||
} | ||
|
||
public NativeTool? Get(string key) | ||
public NativeToolContainer? Get(string key) | ||
{ | ||
if (_nativeTools.TryGetValue(PlatformHelper.Platform, out var platformTools)) | ||
{ | ||
if (platformTools.TryGetValue(key, out var nativeTool)) | ||
{ | ||
return nativeTool; | ||
} | ||
} | ||
return null; | ||
return _nativeTools.GetValueOrDefault(key); | ||
} | ||
|
||
public async Task InstallAsync(NativeTool tool) | ||
public async Task<bool> InstallAsync(NativeToolContainer container) | ||
{ | ||
logger.Log($"Downloading {tool.Id}..."); | ||
var tool = container.GetPlatform(); | ||
|
||
if (tool == null) | ||
{ | ||
logger.Warning($"Tool {container.Id} not supported for {PlatformHelper.Platform.ToString()} yet"); | ||
return false; | ||
} | ||
|
||
logger.Log($"Downloading {container.Id} for {PlatformHelper.Platform.ToString()}...", ConsoleColor.Gray, true, Brushes.Gray); | ||
|
||
var state = applicationStateService.AddState($"Downloading {container.Id}...", AppState.Loading); | ||
|
||
var success = false; | ||
|
||
await DownloadNativeToolAsync(tool); | ||
try | ||
{ | ||
var progress = new Progress<float>(x => | ||
{ | ||
state.StatusMessage = $"Downloading {container.Id} {(int)(x*100)}%"; | ||
}); | ||
success = await DownloadNativeToolAsync(tool, progress); | ||
|
||
foreach (var shortCut in tool.ShortCuts) | ||
foreach (var shortCut in tool.ShortCuts) | ||
{ | ||
if(shortCut.Value.SettingKey != null) | ||
settingsService.SetSettingValue(shortCut.Value.SettingKey, Path.Combine(tool.FullPath, shortCut.Value.RelativePath)); | ||
} | ||
settingsService.Save(paths.SettingsPath); | ||
} | ||
catch (Exception e) | ||
{ | ||
if(shortCut.Value.SettingKey != null) | ||
settingsService.SetSettingValue(shortCut.Value.SettingKey, Path.Combine(tool.FullPath, shortCut.Value.RelativePath)); | ||
logger.Error(e.Message, e); | ||
return false; | ||
} | ||
settingsService.Save(paths.SettingsPath); | ||
|
||
logger.Log($"Download {tool.Id} finished!"); | ||
applicationStateService.RemoveState(state); | ||
|
||
if (success) | ||
{ | ||
logger.Log($"Download {container.Id} finished!", ConsoleColor.Gray, true, Brushes.Gray); | ||
return true; | ||
} | ||
logger.Warning($"Download {container.Id} failed!"); | ||
return false; | ||
} | ||
|
||
private async Task DownloadNativeToolAsync(NativeTool tool) | ||
private Task<bool> DownloadNativeToolAsync(NativeTool tool, IProgress<float> progress) | ||
{ | ||
await httpService.DownloadAndExtractArchiveAsync(tool.Url, Path.Combine(paths.NativeToolsDirectory, tool.Id)); | ||
return httpService.DownloadAndExtractArchiveAsync(tool.Url, tool.FullPath, progress); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
using OneWare.SDK.Helpers; | ||
|
||
namespace OneWare.SDK.NativeTools; | ||
|
||
public class NativeToolContainer(string id, string fullPath) | ||
{ | ||
public string Id { get; } = id; | ||
public string FullPath { get; } = fullPath; | ||
public Dictionary<PlatformId, NativeTool> Platforms { get; } = new(); | ||
|
||
public NativeTool AddPlatform(PlatformId platform, string url) | ||
{ | ||
Platforms[platform] = new NativeTool(url, Path.Combine(FullPath, platform.ToString())); | ||
return Platforms[platform]; | ||
} | ||
|
||
public NativeTool? GetPlatform(PlatformId? platform = null) | ||
{ | ||
var plt = platform ?? PlatformHelper.Platform; | ||
|
||
//If OsxArm64 is not available, use X64 to be used with Rosetta | ||
if (plt == PlatformId.OsxArm64 && !Platforms.ContainsKey(PlatformId.OsxArm64) && | ||
Platforms.ContainsKey(PlatformId.OsxX64)) | ||
{ | ||
plt = PlatformId.OsxX64; | ||
} | ||
return Platforms.GetValueOrDefault(plt); | ||
} | ||
|
||
public string GetShortcutPathOrEmpty(string shortcut, PlatformId? platform = null) | ||
{ | ||
return GetPlatform(platform)?.GetShortcutPath(shortcut) ?? string.Empty; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters