Skip to content

Commit

Permalink
Merge pull request #4 from sout233/dev
Browse files Browse the repository at this point in the history
WHAT A 0.3.0!!!!
  • Loading branch information
sout233 authored Dec 22, 2024
2 parents 1672933 + c54a5a6 commit 4163b9b
Show file tree
Hide file tree
Showing 43 changed files with 1,095 additions and 606 deletions.
4 changes: 4 additions & 0 deletions Nalai.CoreConnector.Sample/Nalai.CoreConnector.Sample.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,8 @@
<ProjectReference Include="..\Nalai.CoreConnector\Nalai.CoreConnector.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Hardcodet.NotifyIcon.Wpf" Version="2.0.1" />
</ItemGroup>

</Project>
58 changes: 29 additions & 29 deletions Nalai.CoreConnector.Sample/Program.cs
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
using Nalai.CoreConnector;


const string Url2 = "https://mirrors.tuna.tsinghua.edu.cn/debian/dists/Debian10.13/ChangeLog";
// var id = await preCore.StartAsync("https://mirrors.tuna.tsinghua.edu.cn/debian-cd/current/amd64/iso-cd/debian-12.7.0-amd64-netinst.iso","C:/download");
var id = await CoreService.SendStartMsgAsync(Url2, "C:/download","","");
Console.WriteLine(id.Id);

// Task.Run(async () =>
// {
// while (true)
// {
// var status = await PreCore.GetStatusAsync(id.Id);
// if (status.TotalBytes > 0)
// {
// var progress = (((float)status.DownloadedBytes / status.TotalBytes) * 100).ToString("0.00")+"%";
// Console.WriteLine($"Downloaded:{status.DownloadedBytes} Total:{status.TotalBytes} FileName:{status.FileName} Status:{status.StatusText} Progress:{progress}");
// }
// await Task.Delay(500);
// }
// });

await Task.Delay(5000);

var result = await CoreService.SendStopMsgAsync(id.Id);

Console.WriteLine(result.IsSuccess);

Environment.Exit(0);
// using Nalai.CoreConnector;
//
//
// const string Url2 = "https://mirrors.tuna.tsinghua.edu.cn/debian/dists/Debian10.13/ChangeLog";
// // var id = await preCore.StartAsync("https://mirrors.tuna.tsinghua.edu.cn/debian-cd/current/amd64/iso-cd/debian-12.7.0-amd64-netinst.iso","C:/download");
// var id = await CoreService.SendStartMsgAsync(Url2, "C:/download","","");
// Console.WriteLine(id.Id);
//
// // Task.Run(async () =>
// // {
// // while (true)
// // {
// // var status = await PreCore.GetStatusAsync(id.Id);
// // if (status.TotalBytes > 0)
// // {
// // var progress = (((float)status.DownloadedBytes / status.TotalBytes) * 100).ToString("0.00")+"%";
// // Console.WriteLine($"Downloaded:{status.DownloadedBytes} Total:{status.TotalBytes} FileName:{status.FileName} Status:{status.StatusText} Progress:{progress}");
// // }
// // await Task.Delay(500);
// // }
// // });
//
// await Task.Delay(5000);
//
// var result = await CoreService.SendStopMsgAsync(id.Id);
//
// // Console.WriteLine(result.IsSuccess);
//
// Environment.Exit(0);
15 changes: 9 additions & 6 deletions Nalai.CoreConnector/CoreService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public static Task StartAsync()
if (!isProcessRunning)
{
// 如果进程不存在,则启动它
var pathToExe = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Tools", "nalai_core-v0.1.3-windows-msvc.exe");
var pathToExe = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Tools", "nalai_core.exe");
var startInfo = new ProcessStartInfo
{
FileName = pathToExe,
Expand Down Expand Up @@ -85,24 +85,27 @@ public static void SendExitMsg()
}
}

public static Dictionary<string, NalaiCoreInfo>? GetAllInfo()
public static async Task<Dictionary<string, NalaiCoreInfo>?> GetAllInfo()
{
return MakeHttpRequestWithRetry(
return await MakeHttpRequestWithRetry(
() => HttpClient.GetAsync("http://localhost:13088/all_info"),
async response =>
{
var content = await response.Content.ReadAsStringAsync();
var result = JsonConvert.DeserializeObject<GetAllInfoResponse>(content);
return result?.Data;
}).Result;
});
}

public static async Task<NalaiCoreDownloadResult?> SendStartMsgAsync(string url, string saveDir, string fileName,
string id)
string id,Dictionary<string, string>? headers)
{
var headersJson = headers == null ? "{}" : JsonConvert.SerializeObject(headers);
var headersBase64 = Convert.ToBase64String(Encoding.UTF8.GetBytes(headersJson));

var uriBuilder = new UriBuilder("http://localhost:13088/download")
{
Query = $"url={url}&save_dir={saveDir}&file_name={fileName}&id={id}"
Query = $"url={url}&save_dir={saveDir}&file_name={fileName}&id={id}&headers={headersBase64}"
};
Console.WriteLine($"uriBuilder.Uri: {uriBuilder.Uri}");

Expand Down
57 changes: 55 additions & 2 deletions Nalai.CoreConnector/Models/DownloadStatus.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,64 @@
namespace Nalai.CoreConnector.Models;
using Newtonsoft.Json;

public enum DownloadStatus
namespace Nalai.CoreConnector.Models;

public enum DownloadStatusKind
{
NoStart,
Running,
Pending,
Error,
Finished,
Cancelled,
}

public class DownloadStatus
{
public DownloadStatus(DownloadStatusKind kind, string message)
{
Kind = kind;
Message = message;
}

[JsonIgnore]
public DownloadStatusKind Kind
{
get
{
var kind = KindRaw switch
{
"NoStart" => DownloadStatusKind.NoStart,
"Running" => DownloadStatusKind.Running,
"Error" => DownloadStatusKind.Error,
"Finished" => DownloadStatusKind.Finished,
"DownloadFinished" => DownloadStatusKind.Finished,
"Pending(Initializing)" => DownloadStatusKind.Pending,
"Pending(Starting)" => DownloadStatusKind.Pending,
"Pending(Stopping)" => DownloadStatusKind.Pending,
"Cancelled" => DownloadStatusKind.Cancelled,
_ => DownloadStatusKind.NoStart
};

return kind;
}
private set
{
KindRaw = value switch
{
DownloadStatusKind.NoStart => "NoStart",
DownloadStatusKind.Running => "Running",
DownloadStatusKind.Error => "Error",
DownloadStatusKind.Finished => "Finished",
DownloadStatusKind.Pending => "Pending(Initializing)",
DownloadStatusKind.Cancelled => "Cancelled",
_ => "NoStart"
};
}
}

[JsonProperty("kind")]
public string? KindRaw { get; set; }


[JsonProperty("message")] public string Message { get; set; }
}
25 changes: 2 additions & 23 deletions Nalai.CoreConnector/Models/GetInfoResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,36 +36,15 @@ public record NalaiCoreInfo
/// 下载状态,详情见core源代码
/// </summary>
[JsonProperty("status")]
public string StatusText
{
get => _statusText;
set
{
_statusText = value;
Status = value switch
{
"NoStart" => DownloadStatus.NoStart,
"Running" => DownloadStatus.Running,
"Error" => DownloadStatus.Error,
"Finished" => DownloadStatus.Finished,
"DownloadFinished" => DownloadStatus.Finished,
"Pending(Initializing)" => DownloadStatus.Pending,
"Pending(Starting)" => DownloadStatus.Pending,
"Pending(Stopping)" => DownloadStatus.Pending,
"Cancelled" => DownloadStatus.Cancelled,
_ => DownloadStatus.NoStart
};
}
}
public DownloadStatus Status { get; set; }


[JsonProperty("save_dir")]
public string SaveDirectory { get; set; }

[JsonProperty("create_time")]
public RustSystemTime CreatedTime { get;set; }

[JsonIgnore] public DownloadStatus Status { get; set; }

[JsonProperty("chunks")]
public List<ChunksItem> Chunks { get;set; }
}
Expand Down
4 changes: 3 additions & 1 deletion Nalai.CoreConnector/Models/NalaiResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ public class NalaiResponse<T>
[JsonProperty("success")] public bool Success { get; set; }

[JsonProperty("code")] public string Code { get; set; }

[JsonProperty("msg")] public string? Message { get; set; }

[JsonProperty("data")] public T Data { get; set; }
[JsonProperty("data")] public T? Data { get; set; }
}
2 changes: 1 addition & 1 deletion Nalai.CoreConnector/Models/PostDownloadResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace Nalai.CoreConnector.Models;

public class NalaiCoreDownloadResult
{
[JsonProperty("id")] public string? Id { get; set; }
[JsonProperty("id")] public required string Id { get; set; }
}

public class PostDownloadResponse : NalaiResponse<NalaiCoreDownloadResult>;
4 changes: 2 additions & 2 deletions Nalai.CoreConnector/Models/PostStopResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ namespace Nalai.CoreConnector.Models;

public class NalaiStopResult
{
[JsonProperty("success")]
public bool IsSuccess { get; set; }
// [JsonProperty("success")]
// public bool IsSuccess { get; set; }
}

public class PostStopResponse : NalaiResponse<NalaiStopResult>;
1 change: 1 addition & 0 deletions Nalai.CoreConnector/Nalai.CoreConnector.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Hardcodet.NotifyIcon.Wpf" Version="2.0.1" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
</ItemGroup>

Expand Down
19 changes: 18 additions & 1 deletion Nalai/App.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,32 @@
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:ui="http://schemas.lepo.co/wpfui/2022/xaml"
xmlns:tb="http://www.hardcodet.net/taskbar"
xmlns:resources="clr-namespace:Nalai.Resources"
DispatcherUnhandledException="OnDispatcherUnhandledException"
Exit="OnExit"
ShutdownMode="OnExplicitShutdown"
Startup="OnStartup">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ui:ThemesDictionary Theme="Light" />
<ui:ControlsDictionary />

</ResourceDictionary.MergedDictionaries>
<tb:TaskbarIcon x:Key="NalaiTrayIcon"
IconSource="pack://application:,,,/Nalai;component/Assets/nalai_icon_x1.png"
ToolTipText="{I18N {x:Static resources:LangKeys.Nalai_Title}}">
<tb:TaskbarIcon.ContextMenu>
<ContextMenu>
<MenuItem Header="Show Window" Click="ShowWindow" />
<MenuItem Header="Settings" Click="ShowSettings"></MenuItem>
<Separator />
<MenuItem Header="Exit" Click="ExitApplication" />
</ContextMenu>
</tb:TaskbarIcon.ContextMenu>
</tb:TaskbarIcon>
</ResourceDictionary>

</Application.Resources>
</Application>
</Application>
62 changes: 58 additions & 4 deletions Nalai/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@
using System.IO;
using System.Reflection;
using System.Windows.Threading;
using Antelcat.I18N.Attributes;
using Hardcodet.Wpf.TaskbarNotification;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Nalai.CoreConnector;
using Nalai.CoreConnector.Models;
using Nalai.Models;
using Nalai.Services;
using Nalai.ViewModels.Pages;
Expand Down Expand Up @@ -73,6 +72,8 @@ public static T GetService<T>()
return _host.Services.GetService(typeof(T)) as T;
}

private TaskbarIcon? _taskbarIcon;

/// <summary>
/// Occurs when the application is loading.
/// </summary>
Expand All @@ -95,13 +96,66 @@ private void OnStartup(object sender, StartupEventArgs e)
}
}


// 创建托盘图标
_taskbarIcon = (TaskbarIcon)FindResource("NalaiTrayIcon");


Task.Run(CoreTask.SyncAllTasksFromCore);
RunningStateChecker.Start();

Task.Run(EventApiService.Run);
}

private void ShowWindow(object sender, RoutedEventArgs e)
{
// 显示或激活主窗口
if (MainWindow == null)
{
var vm = GetService<MainWindowViewModel>();
var ps = GetService<IPageService>();
var ns = GetService<INavigationService>();
var window = new MainWindow(vm, ps, ns);
window.Show();
}
else
{
MainWindow.Show();
MainWindow.Activate();
}

if (MainWindow is INavigationWindow navWindow)
{
navWindow.Navigate(typeof(DashboardPage));
}
}

private void ShowSettings(object sender, RoutedEventArgs e)
{
if (MainWindow == null)
{
var vm = GetService<MainWindowViewModel>();
var ps = GetService<IPageService>();
var ns = GetService<INavigationService>();
var window = new MainWindow(vm, ps, ns);
window.Show();
}
else
{
MainWindow.Show();
MainWindow.Activate();
}

if (MainWindow is INavigationWindow navWindow)
{
navWindow.Navigate(typeof(SettingsPage));
}
}

private void ExitApplication(object sender, RoutedEventArgs e)
{
Current.Shutdown();
}

/// <summary>
/// Occurs when the application is closing.
/// </summary>
Expand All @@ -121,7 +175,7 @@ private async void OnExit(object sender, ExitEventArgs e)
CoreService.SendExitMsg();
}
}

await _host.StopAsync();
RunningStateChecker.Stop();
_host.Dispose();
Expand Down
Loading

0 comments on commit 4163b9b

Please sign in to comment.