Skip to content

Commit

Permalink
💾 Feat(Installer, Dashboard): Merge pull request #80 from Crequency/d…
Browse files Browse the repository at this point in the history
…ev=main

[Pull Request] 导入包逻辑动工, Win平台安装包增加文件关联,增强异常捕获能力
  • Loading branch information
Dynesshely authored Aug 16, 2022
2 parents 8983e68 + c5081ea commit 6b07712
Show file tree
Hide file tree
Showing 11 changed files with 225 additions and 77 deletions.
29 changes: 29 additions & 0 deletions KitX Dashboard/Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
using FluentAvalonia.UI.Media;
using KitX_Dashboard.Data;
using LiteDB;
using System;
using System.Diagnostics;
using System.IO;
using System.Text.Json;
using System.Threading;
Expand Down Expand Up @@ -194,6 +196,33 @@ public static void InitEnvironment()
}).Start();
#endregion
}

/// <summary>
/// 导入插件
/// </summary>
/// <param name="kxpPath">.kxp Path</param>
public static void ImportPlugin(string kxpPath)
{
string? workbasef = Process.GetCurrentProcess().MainModule.FileName;
string? workbase = string.Empty;
if (workbasef == null)
throw new Exception("Can not get path of \"KitX Dashboard.exe\"");
else
{
workbase = Path.GetDirectoryName(workbasef);
if (workbase == null)
throw new Exception("Can not get path of \"KitX\"");
}
if (!File.Exists(kxpPath))
{
Console.WriteLine($"No this file: {kxpPath}");
throw new Exception("Plugin Package Doesn't Exist.");
}
else
{

}
}
}
}

Expand Down
45 changes: 26 additions & 19 deletions KitX Dashboard/KitX Dashboard.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@
<None Remove="FakesAssemblies\**" />
<AvaloniaResource Remove="Assets\avalonia-logo.ico" />
<AvaloniaResource Remove="Assets\KitX-Background.png" />
<AvaloniaResource Remove="Assets\KitX-Icon-128x.ico" />
<AvaloniaResource Remove="Assets\KitX-Icon-16x.ico" />
<AvaloniaResource Remove="Assets\KitX-Icon-256x.ico" />
<AvaloniaResource Remove="Assets\KitX-Icon-32x.ico" />
<AvaloniaResource Remove="Assets\KitX-Icon-48x.ico" />
<AvaloniaResource Remove="Assets\KitX-Icon-64x.ico" />
<AvaloniaResource Remove="Assets\KitX.Base64.txt" />
<AvaloniaResource Remove="Assets\ThirdPartLicense.md" />
<AvaloniaXaml Remove="Languages\en-us.axaml" />
Expand All @@ -55,7 +61,6 @@
<None Remove="Assets\SourceHanSans-VF.ttf" />
</ItemGroup>
<ItemGroup>
<Content Include="Assets\KitX-Icon-256x.ico" />
<Content Include="Languages\en-us.axaml">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
Expand All @@ -69,6 +74,26 @@
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="Assets\KitX-Icon-128x.ico">
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
</EmbeddedResource>
<EmbeddedResource Include="Assets\KitX-Icon-16x.ico">
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
</EmbeddedResource>
<EmbeddedResource Include="Assets\KitX-Icon-256x.ico">
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
</EmbeddedResource>
<EmbeddedResource Include="Assets\KitX-Icon-32x.ico">
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
</EmbeddedResource>
<EmbeddedResource Include="Assets\KitX-Icon-48x.ico">
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
</EmbeddedResource>
<EmbeddedResource Include="Assets\KitX-Icon-64x.ico">
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
</EmbeddedResource>
</ItemGroup>
<ItemGroup>
<!--This helps with theme dll-s trimming.
If you will publish your application in self-contained mode with p:PublishTrimmed=true and it will use Fluent theme Default theme will be trimmed from the output and vice versa.
Expand Down Expand Up @@ -110,24 +135,6 @@
<None Update="Assets\KitX-Background.png">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<AvaloniaResource Update="Assets\KitX-Icon-128x.ico">
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
</AvaloniaResource>
<AvaloniaResource Update="Assets\KitX-Icon-16x.ico">
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
</AvaloniaResource>
<AvaloniaResource Update="Assets\KitX-Icon-256x.ico">
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
</AvaloniaResource>
<AvaloniaResource Update="Assets\KitX-Icon-32x.ico">
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
</AvaloniaResource>
<AvaloniaResource Update="Assets\KitX-Icon-48x.ico">
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
</AvaloniaResource>
<AvaloniaResource Update="Assets\KitX-Icon-64x.ico">
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
</AvaloniaResource>
<AvaloniaResource Update="Assets\KitX.png">
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
</AvaloniaResource>
Expand Down
4 changes: 0 additions & 4 deletions KitX Dashboard/Models/Plugin.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
using KitX.Web.Rules;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace KitX_Dashboard.Models
{
Expand Down
27 changes: 27 additions & 0 deletions KitX Dashboard/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,33 @@ internal class Program
[STAThread]
public static void Main(string[] args)
{
try
{
for (int i = 0; i < args.Length; i++)
{
switch (args[i])
{
case "--import-plugin":
if (i != args.Length - 1)
try
{
Helper.ImportPlugin(args[i + 1]);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
else throw new Exception("No arguments for plugin location.");
break;
}
}
}
catch(Exception e)
{
Console.WriteLine(e.Message);
Environment.Exit(1);
}

try
{
if (File.Exists(Path.GetFullPath("./dump.log")))
Expand Down
2 changes: 1 addition & 1 deletion KitX Dashboard/ViewModels/Pages/RepoPageViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ internal async void ImportPlugin(object win)
Extensions = { "kxp" }
});
string[]? files = await ofd.ShowAsync(win as Window);
if(files?.Length > 0)
if (files?.Length > 0)
{

}
Expand Down
2 changes: 1 addition & 1 deletion KitX File Format Helper/KitX.KXP.Helper/Encoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public void Encode(string rootDir, string saveFolder, string filename)
md5.Dispose();

#if DEBUG
Console.Write($"Hash Code: {Encoding.UTF8.GetString(hash)}");
Console.Write($"Hash Code: {Encoding.UTF8.GetString(hash)}");
#endif

foreach (var item in hash)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,65 +170,151 @@ private void InstallProcess()

UpdatePro(60);

RegistryKey software = Registry.LocalMachine.OpenSubKey("SOFTWARE", true)
.OpenSubKey("Microsoft", true).OpenSubKey("Windows", true)
.OpenSubKey("CurrentVersion", true);
RegistryKey appPaths = software.OpenSubKey("App Paths", true);
RegistryKey uninstall = software.OpenSubKey("Uninstall", true);

RegistryKey appPaths_KitX = appPaths.CreateSubKey("KitX Dashboard.exe");
appPaths_KitX.SetValue("", targetPath);
appPaths_KitX.SetValue("Path", stfolder);
appPaths_KitX.Dispose();

UpdatePro(65);

Assembly assembly = Assembly.LoadFrom(modulePath);
string version = assembly.GetName().Version.ToString();

RegistryKey uninstall_KitX = uninstall.CreateSubKey("KitX");
uninstall_KitX.SetValue("DisplayName", "KitX Dashboard");
uninstall_KitX.SetValue("DisplayVersion", version);
uninstall_KitX.SetValue("DisplayIcon", targetPath);
uninstall_KitX.SetValue("Publisher", "Crequency Studio");
uninstall_KitX.SetValue("InstallLocation", stfolder);
uninstall_KitX.SetValue("UninstallString", uninstallString);
uninstall_KitX.SetValue("QuietUninstallString", $"{uninstallString} --silent");
uninstall_KitX.SetValue("HelpLink", helpLink);
uninstall_KitX.SetValue("URLInfoAbout", infoLink);
uninstall_KitX.SetValue("NoModify", 1, RegistryValueKind.DWord);
uninstall_KitX.SetValue("NoRepair", 1, RegistryValueKind.DWord);
uninstall_KitX.SetValue("EstimatedSize", GetDirectoryLength(stfolder) / 1000,
RegistryValueKind.DWord);
uninstall_KitX.Dispose();

UpdatePro(70);

appPaths.Dispose();
uninstall.Dispose();
UpdateTip("打开注册表 ...");
Thread.Sleep(200);

UpdateTip("更新快捷方式 ...");
try
{
RegistryKey software = Registry.LocalMachine.OpenSubKey("SOFTWARE", true)
.OpenSubKey("Microsoft", true).OpenSubKey("Windows", true)
.OpenSubKey("CurrentVersion", true);
RegistryKey appPaths = software.OpenSubKey("App Paths", true);
RegistryKey uninstall = software.OpenSubKey("Uninstall", true);

#region 更新 AppPaths
UpdateTip("写入注册表 App Paths ...");
Thread.Sleep(200);
{
RegistryKey appPaths_KitX = appPaths.CreateSubKey("KitX Dashboard.exe");
appPaths_KitX.SetValue("", targetPath);
appPaths_KitX.SetValue("Path", stfolder);
appPaths_KitX.Dispose();
}
#endregion

UpdatePro(65);

Assembly assembly = Assembly.LoadFrom(modulePath);
string version = assembly.GetName().Version.ToString();

#region 更新 控制面板中所对应的程序信息
UpdateTip("写入注册表 Uninstall ...");
Thread.Sleep(200);
{
RegistryKey uninstall_KitX = uninstall.CreateSubKey("KitX");
uninstall_KitX.SetValue("DisplayName", "KitX Dashboard");
uninstall_KitX.SetValue("DisplayVersion", version);
uninstall_KitX.SetValue("DisplayIcon", targetPath);
uninstall_KitX.SetValue("Publisher", "Crequency Studio");
uninstall_KitX.SetValue("InstallLocation", stfolder);
uninstall_KitX.SetValue("UninstallString", uninstallString);
uninstall_KitX.SetValue("QuietUninstallString", $"{uninstallString} --silent");
uninstall_KitX.SetValue("HelpLink", helpLink);
uninstall_KitX.SetValue("URLInfoAbout", infoLink);
uninstall_KitX.SetValue("NoModify", 1, RegistryValueKind.DWord);
uninstall_KitX.SetValue("NoRepair", 1, RegistryValueKind.DWord);
uninstall_KitX.SetValue("EstimatedSize", GetDirectoryLength(stfolder) / 1000,
RegistryValueKind.DWord);
uninstall_KitX.Dispose();
}
#endregion

UpdatePro(70);

#region 更新 文件关联
UpdateTip("写入注册表 文件关联 ...");
Thread.Sleep(200);
{
RegistryKey fileCon = Registry.ClassesRoot.CreateSubKey(".kxp");
fileCon.SetValue("", "KitX.kxp");
fileCon.SetValue("Content Type", "application/kitx-extensions-package");
RegistryKey filePro = Registry.ClassesRoot.CreateSubKey("KitX.kxp");
filePro.SetValue("", "KitX Extensions Package");
RegistryKey icon = filePro.CreateSubKey("DefaultIcon");
icon.SetValue("", $"{stfolder}\\Assets\\kxp.ico");
RegistryKey shell = filePro.CreateSubKey("Shell");
RegistryKey open = shell.CreateSubKey("Open");
open.SetValue("FriendlyAppName", "KitX");
RegistryKey com = open.CreateSubKey("Command");
com.SetValue("", $"\"{targetPath}\" --import-plugin \"%1\"");
com.Dispose();
open.Dispose();
shell.Dispose();
icon.Dispose();
filePro.Dispose();
fileCon.Dispose();
}
#endregion

appPaths.Dispose();
uninstall.Dispose();
}
catch (Exception e)
{
UpdateTip($"写入注册表失败: {e.Message}");
Invoke(new Action(() =>
{
MessageBox.Show("Cancel Setup! | 安装取消", "KitX",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}));
BeginCancel();
}

WshShell shell = new WshShell();
UpdatePro(75);
CreateShortCut(ref shell, $"{desktop}\\{shortcutName}", targetPath, stfolder, descr, targetPath);

UpdateTip("更新快捷方式 ...");
Thread.Sleep(200);

try
{
WshShell shell = new WshShell();
CreateShortCut(ref shell, $"{desktop}\\{shortcutName}",
targetPath, stfolder, descr, targetPath);
CreateShortCut(ref shell, $"{startmenu}\\{shortcutNameStartMenu}",
targetPath, stfolder, descr, targetPath);
}
catch (Exception e)
{
UpdateTip($"创建快捷方式失败: {e.Message}");
Invoke(new Action(() =>
{
MessageBox.Show("Cancel Setup! | 安装取消", "KitX",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}));
BeginCancel();
}

UpdatePro(80);
CreateShortCut(ref shell, $"{startmenu}\\{shortcutNameStartMenu}", targetPath, stfolder,
descr, targetPath);
UpdatePro(85);

UpdateTip("生成卸载程序 ...");
Thread.Sleep(200);

if (File.Exists(uninstallPath))
try
{
File.Delete(uninstallPath);
if (File.Exists(uninstallPath))
{
File.Delete(uninstallPath);
}
string me = Process.GetCurrentProcess().MainModule.FileName;
File.Copy(me, uninstallPath);
}
catch (Exception e)
{
UpdateTip($"生成卸载程序失败: {e.Message}");
Invoke(new Action(() =>
{
MessageBox.Show("Cancel Setup! | 安装取消", "KitX",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}));
BeginCancel();
}
string me = Process.GetCurrentProcess().MainModule.FileName;
File.Copy(me, uninstallPath);

UpdatePro(90);

Thread.Sleep(1000);

UpdatePro(100);

Invoke(new Action(() =>
{
MessageBox.Show("Install succeed! | 安装成功", "KitX",
Expand Down Expand Up @@ -285,12 +371,18 @@ private void CancelProcess()
ProgressBar_Installing.Value = 50;
}));

string stfolder = Path.GetFullPath(TextBox_InstallPath.Text);

UpdateTip("正在取消 ...");

Thread_Install.Abort();

while (Thread_Install.ThreadState != ThreadState.Aborted) { }

UpdateTip("正在回滚 ...");

Directory.Delete(stfolder, true);

Invoke(new Action(() =>
{
UpdateTip("等待用户操作 ...");
Expand Down
Loading

0 comments on commit 6b07712

Please sign in to comment.