now with animations lol
This commit is contained in:
parent
7a7cefe7b5
commit
243224a720
6 changed files with 192 additions and 43 deletions
|
|
@ -1,10 +1,18 @@
|
|||
using MetaforceInstaller.Core.Models;
|
||||
using MetaforceInstaller.Core.Models;
|
||||
|
||||
namespace MetaforceInstaller.Core.Intefaces;
|
||||
|
||||
public interface IAdbService
|
||||
{
|
||||
public void InstallApk(string apkPath);
|
||||
public void CopyFile(string localPath, string remotePath);
|
||||
public DeviceInfo GetDeviceInfo();
|
||||
event EventHandler<ProgressInfo>? ProgressChanged;
|
||||
event EventHandler<string>? StatusChanged;
|
||||
|
||||
Task InstallApkAsync(string apkPath, IProgress<ProgressInfo>? progress = null, CancellationToken cancellationToken = default);
|
||||
Task CopyFileAsync(string localPath, string remotePath, IProgress<ProgressInfo>? progress = null, CancellationToken cancellationToken = default);
|
||||
DeviceInfo GetDeviceInfo();
|
||||
|
||||
// Синхронные версии для обратной совместимости
|
||||
void InstallApk(string apkPath);
|
||||
void CopyFile(string localPath, string remotePath);
|
||||
}
|
||||
|
|
@ -12,8 +12,14 @@
|
|||
<EmbeddedResource Include="adb\AdbWinUsbApi.dll" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="8.0.0" />
|
||||
<PackageReference Include="AdvancedSharpAdbClient" Version="3.4.14" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="AdvancedSharpAdbClient" Version="3.4.14" />
|
||||
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="8.0.3" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
|
|
|||
19
MetaforceInstaller.Core/Models/ProgressInfo.cs
Normal file
19
MetaforceInstaller.Core/Models/ProgressInfo.cs
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
namespace MetaforceInstaller.Core.Models;
|
||||
|
||||
public class ProgressInfo
|
||||
{
|
||||
public int PercentageComplete { get; set; }
|
||||
public long BytesTransferred { get; set; }
|
||||
public long TotalBytes { get; set; }
|
||||
public string? Message { get; set; }
|
||||
public string? CurrentFile { get; set; }
|
||||
public ProgressType Type { get; set; }
|
||||
}
|
||||
|
||||
public enum ProgressType
|
||||
{
|
||||
Installation,
|
||||
FileCopy,
|
||||
Extraction,
|
||||
General
|
||||
}
|
||||
|
|
@ -1,33 +1,42 @@
|
|||
using System.Reflection;
|
||||
using AdvancedSharpAdbClient;
|
||||
using AdvancedSharpAdbClient.DeviceCommands;
|
||||
using AdvancedSharpAdbClient.Logs;
|
||||
using AdvancedSharpAdbClient.Models;
|
||||
using MetaforceInstaller.Core.Intefaces;
|
||||
using MetaforceInstaller.Core.Models;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Logging.Abstractions;
|
||||
|
||||
namespace MetaforceInstaller.Core.Services;
|
||||
|
||||
public class AdbService : IAdbService
|
||||
{
|
||||
private ILogger<AdbService> _logger;
|
||||
private readonly ILogger<AdbService> _logger;
|
||||
private readonly AdbClient _adbClient;
|
||||
private DeviceData _deviceData;
|
||||
|
||||
public AdbService()
|
||||
public event EventHandler<ProgressInfo>? ProgressChanged;
|
||||
public event EventHandler<string>? StatusChanged;
|
||||
|
||||
public AdbService(ILogger<AdbService>? logger = null)
|
||||
{
|
||||
_logger = logger ?? new NullLogger<AdbService>();
|
||||
var adbPath = GetAdbPath();
|
||||
var server = new AdbServer();
|
||||
var serverStatus = server.StartServer(adbPath, restartServerIfNewer: false);
|
||||
_adbClient = new AdbClient();
|
||||
var devices = _adbClient.GetDevices();
|
||||
foreach (var device in devices)
|
||||
{
|
||||
Console.WriteLine(device.Model);
|
||||
}
|
||||
_deviceData = devices.FirstOrDefault();
|
||||
}
|
||||
|
||||
private void ExtractResource(string resourceName, string outputPath)
|
||||
{
|
||||
_logger.LogInformation($"Extracting resource: {resourceName} to {outputPath}");
|
||||
using var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName);
|
||||
using var stream = Assembly.GetAssembly(typeof(AdbService)).GetManifestResourceStream(resourceName);
|
||||
using var fileStream = File.Create(outputPath);
|
||||
stream.CopyTo(fileStream);
|
||||
_logger.LogInformation($"Resource extracted: {resourceName} to {outputPath}");
|
||||
|
|
@ -41,14 +50,30 @@ public class AdbService : IAdbService
|
|||
var adbPath = Path.Combine(tempDir, "adb.exe");
|
||||
|
||||
if (File.Exists(adbPath)) return adbPath;
|
||||
ExtractResource("MetaforceInstaller.Cli.adb.adb.exe", adbPath);
|
||||
ExtractResource("MetaforceInstaller.Cli.adb.AdbWinApi.dll", Path.Combine(tempDir, "AdbWinApi.dll"));
|
||||
ExtractResource("MetaforceInstaller.Cli.adb.AdbWinUsbApi.dll", Path.Combine(tempDir, "AdbWinUsbApi.dll"));
|
||||
ExtractResource("MetaforceInstaller.Core.adb.adb.exe", adbPath);
|
||||
ExtractResource("MetaforceInstaller.Core.adb.AdbWinApi.dll", Path.Combine(tempDir, "AdbWinApi.dll"));
|
||||
ExtractResource("MetaforceInstaller.Core.adb.AdbWinUsbApi.dll", Path.Combine(tempDir, "AdbWinUsbApi.dll"));
|
||||
|
||||
return adbPath;
|
||||
}
|
||||
|
||||
private void OnProgressChanged(ProgressInfo progressInfo)
|
||||
{
|
||||
ProgressChanged?.Invoke(this, progressInfo);
|
||||
}
|
||||
|
||||
private void OnStatusChanged(string status)
|
||||
{
|
||||
StatusChanged?.Invoke(this, status);
|
||||
}
|
||||
|
||||
public void InstallApk(string apkPath)
|
||||
{
|
||||
InstallApkAsync(apkPath).Wait();
|
||||
}
|
||||
|
||||
public async Task InstallApkAsync(string apkPath, IProgress<ProgressInfo>? progress = null,
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
try
|
||||
{
|
||||
|
|
@ -58,11 +83,37 @@ public class AdbService : IAdbService
|
|||
return;
|
||||
}
|
||||
|
||||
OnStatusChanged("Начинаем установку APK...");
|
||||
_logger.LogInformation($"Installing APK: {apkPath}");
|
||||
|
||||
var packageManager = new PackageManager(_adbClient, _deviceData);
|
||||
packageManager.InstallPackage(apkPath, o => { });
|
||||
progress?.Report(new ProgressInfo
|
||||
{
|
||||
PercentageComplete = 0,
|
||||
Message = "Подготовка к установке APK...",
|
||||
Type = ProgressType.Installation,
|
||||
CurrentFile = Path.GetFileName(apkPath)
|
||||
});
|
||||
|
||||
var packageManager = new PackageManager(_adbClient, _deviceData);
|
||||
|
||||
await Task.Run(() =>
|
||||
{
|
||||
packageManager.InstallPackage(apkPath, installProgress =>
|
||||
{
|
||||
var progressInfo = new ProgressInfo
|
||||
{
|
||||
PercentageComplete = (int)installProgress.UploadProgress,
|
||||
Message = $"Установка APK: {installProgress.UploadProgress:F1}%",
|
||||
Type = ProgressType.Installation,
|
||||
CurrentFile = Path.GetFileName(apkPath)
|
||||
};
|
||||
|
||||
progress?.Report(progressInfo);
|
||||
OnProgressChanged(progressInfo);
|
||||
});
|
||||
}, cancellationToken);
|
||||
|
||||
OnStatusChanged("APK успешно установлен!");
|
||||
_logger.LogInformation("APK successfully installed!");
|
||||
}
|
||||
catch (Exception ex)
|
||||
|
|
@ -73,6 +124,12 @@ public class AdbService : IAdbService
|
|||
}
|
||||
|
||||
public void CopyFile(string localPath, string remotePath)
|
||||
{
|
||||
CopyFileAsync(localPath, remotePath).Wait();
|
||||
}
|
||||
|
||||
public async Task CopyFileAsync(string localPath, string remotePath, IProgress<ProgressInfo>? progress = null,
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
try
|
||||
{
|
||||
|
|
@ -82,14 +139,44 @@ public class AdbService : IAdbService
|
|||
return;
|
||||
}
|
||||
|
||||
OnStatusChanged("Начинаем копирование файла...");
|
||||
_logger.LogInformation($"Copying file: {localPath} to {remotePath}");
|
||||
|
||||
using var fileStream = File.OpenRead(localPath);
|
||||
var syncService = new SyncService(_adbClient, _deviceData);
|
||||
var fileInfo = new FileInfo(localPath);
|
||||
|
||||
syncService.Push(fileStream, remotePath, UnixFileStatus.DefaultFileMode, DateTime.Now,
|
||||
new Action<SyncProgressChangedEventArgs>(progress => { }));
|
||||
progress?.Report(new ProgressInfo
|
||||
{
|
||||
PercentageComplete = 0,
|
||||
Message = "Подготовка к копированию файла...",
|
||||
Type = ProgressType.FileCopy,
|
||||
CurrentFile = Path.GetFileName(localPath),
|
||||
TotalBytes = fileInfo.Length
|
||||
});
|
||||
|
||||
await Task.Run(() =>
|
||||
{
|
||||
using var fileStream = File.OpenRead(localPath);
|
||||
var syncService = new SyncService(_adbClient, _deviceData);
|
||||
|
||||
syncService.Push(fileStream, remotePath, UnixFileStatus.DefaultFileMode, DateTime.Now,
|
||||
copyProgress =>
|
||||
{
|
||||
var progressInfo = new ProgressInfo
|
||||
{
|
||||
PercentageComplete = (int)copyProgress.ProgressPercentage,
|
||||
BytesTransferred = copyProgress.ReceivedBytesSize,
|
||||
TotalBytes = copyProgress.TotalBytesToReceive,
|
||||
Message = $"Копирование: {copyProgress.ProgressPercentage:F1}%",
|
||||
Type = ProgressType.FileCopy,
|
||||
CurrentFile = Path.GetFileName(localPath)
|
||||
};
|
||||
|
||||
progress?.Report(progressInfo);
|
||||
OnProgressChanged(progressInfo);
|
||||
});
|
||||
}, cancellationToken);
|
||||
|
||||
OnStatusChanged("Файл успешно скопирован!");
|
||||
_logger.LogInformation("File successfully copied!");
|
||||
}
|
||||
catch (Exception ex)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue