初步完成基本功能

This commit is contained in:
DevWiki 2024-06-19 12:00:59 +08:00
parent 0a74334f13
commit 38fec1126d
12 changed files with 432 additions and 23 deletions

3
.gitignore vendored
View File

@ -1,3 +1,4 @@
/.idea /.idea
*/obj */obj
*/bin */bin
*/pack/output

View File

@ -0,0 +1,3 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:Boolean x:Key="/Default/AddReferences/RecentPaths/=D_003A_005CCode_005Cgitea_005CHarmonyDevTools_005Clibs_005Clog4net_002Edll/@EntryIndexedValue">True</s:Boolean>
<s:String x:Key="/Default/CodeInspection/Highlighting/AnalysisEnabled/@EntryValue">SOLUTION</s:String></wpf:ResourceDictionary>

View File

@ -11,5 +11,8 @@ namespace HarmonyDevTools
/// <summary> /// <summary>
/// Interaction logic for App.xaml /// Interaction logic for App.xaml
/// </summary> /// </summary>
public partial class App : Application { } public partial class App : Application
{
}
} }

View File

@ -7,4 +7,15 @@
<UseWPF>true</UseWPF> <UseWPF>true</UseWPF>
</PropertyGroup> </PropertyGroup>
<ItemGroup>
<Reference Include="log4net">
<HintPath>..\libs\log4net.dll</HintPath>
</Reference>
</ItemGroup>
<Target Name="PreBuild" BeforeTargets="PreBuildEvent">
<Exec Command="xcopy &quot;$(ProjectDir)..\toolchains&quot; &quot;$(TargetDir)toolchains&quot; /E /I /Y" />
</Target>
</Project> </Project>

View File

@ -4,9 +4,72 @@
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:HarmonyDevTools" xmlns:local="clr-namespace:HarmonyDevTools"
mc:Ignorable="d" xmlns:control="clr-namespace:HarmonyDevTools.control"
Title="MainWindow" Height="450" Width="800"> mc:Ignorable="d" ResizeMode="NoResize"
WindowStartupLocation="CenterScreen"
Title="HDC Tools" Height="600" Width="740" Margin="10">
<Grid> <Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="48"/>
<RowDefinition Height="48"/>
<RowDefinition Height="48"/>
<RowDefinition Height="48"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Button x:Name="ListBtn" Grid.Row="0" Grid.Column="0" Width="100" Height="36" Content="List Targets" Click="ListBtn_OnClick" />
<TextBox x:Name="TargetTb" Grid.Row="0" Grid.Column="1" Grid.ColumnSpan="2" Margin="10,0,10,0" VerticalContentAlignment="Center"
VerticalAlignment="Center" Height="36" control:TextBoxHelper.Placeholder="请输入connect key"/>
<Button x:Name="ConnectBtn" Grid.Row="0" Grid.Column="3" Width="100" Height="36" Content="Connect Device" Click="ConnectBtn_OnClick" />
<Button x:Name="KillBtn" Grid.Row="0" Grid.Column="4" Width="100" Height="36" Content="Kill Restart" Click="KillBtn_OnClick" />
<Button x:Name="CheckServerBtn" Grid.Row="0" Grid.Column="5" Width="100" Height="36" Content="Check Server" Click="CheckServerBtn_OnClick" />
<Border Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="3" BorderBrush="Red" BorderThickness="1" Margin="5,0,5,0">
<StackPanel Orientation="Horizontal" Margin="5,0,5,0">
<Button x:Name="InstallBtn" Width="100" Height="36" Content="Install"
Click="InstallOnClick" VerticalAlignment="Center" />
<CheckBox x:Name="ReplaceCb" Content="替换安装" IsChecked="False" VerticalAlignment="Center"
Margin="10,0,0,0"/>
<CheckBox x:Name="DowngradeCb" Content="允许降级" IsChecked="False" VerticalAlignment="Center"
Margin="10,0,0,0"/>
<CheckBox x:Name="DynamicCb" Content="动态授权" IsChecked="False" VerticalAlignment="Center"
Margin="10,0,0,0"/>
</StackPanel>
</Border>
<Border Grid.Row="1" Grid.Column="3" Grid.ColumnSpan="3" Margin="5,0,5,0" BorderBrush="Red" BorderThickness="1">
<StackPanel Orientation="Horizontal" Margin="5,0,5,0">
<TextBox x:Name="PackageNameTb" control:TextBoxHelper.Placeholder="输入包名" VerticalAlignment="Center" VerticalContentAlignment="Center"
Width="228" Height="36"/>
<Button x:Name="UninstallBtn" Width="100" Height="36" Content="Uninstall" Margin="10,0,0,0"
Click="UninstallBtn_OnClick" VerticalAlignment="Center" />
</StackPanel>
</Border>
<Button x:Name="GetImage" Grid.Row="3" Grid.Column="1" Width="100" Height="36" Content="Get Image" Click="GetImage_OnClick" />
<TextBox x:Name="CommandTb" Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="5" Margin="10,0,10,0"
Height="32" VerticalContentAlignment="Center" control:TextBoxHelper.Placeholder="输入命令不需要以 hdc 开头"/>
<Button x:Name="CommandBtn" Content="Execute" Grid.Row="2" Grid.Column="5" Width="100" Height="36" VerticalAlignment="Center"
Click="CommandBtn_OnClick" HorizontalAlignment="Center"/>
<TextBox Grid.Row="4" Grid.Column="0" Grid.ColumnSpan="6" x:Name="ResultTb" Margin="10" TextWrapping="Wrap" IsReadOnly="True"
VerticalScrollBarVisibility="Auto"/>
</Grid> </Grid>
</Window> </Window>

View File

@ -1,28 +1,130 @@
using System; using System;
using System.Collections.Generic; using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Windows; using System.Windows;
using System.Windows.Controls; using HarmonyDevTools.util;
using System.Windows.Data; using Microsoft.Win32;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace HarmonyDevTools namespace HarmonyDevTools;
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{ {
/// <summary> public MainWindow()
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{ {
public MainWindow() InitializeComponent();
}
private void InstallOnClick(object sender, RoutedEventArgs e)
{
OpenFileDialog dialog = new OpenFileDialog()
{ {
InitializeComponent(); Title = "Select a package",
Filter = "HarmonyNEXT(*.hap)|*.hap",
FilterIndex = 1,
RestoreDirectory = true
};
if (dialog.ShowDialog() == true)
{
// 获取所选文件的路径并显示在文本框中
string filePath = dialog.FileName;
AppendHintAndScrollToEnd($"select file: {filePath} \n");
string command = "install";
if (ReplaceCb.IsChecked ?? false)
{
command += " -r";
}
if (DowngradeCb.IsChecked ?? false)
{
command += " -d";
}
if (DynamicCb.IsChecked ?? false)
{
command += " -g";
}
CommandTb.Text = $"{command} {filePath}";
GetCommandAndExecute();
} }
} }
private void CommandBtn_OnClick(object sender, RoutedEventArgs e)
{
GetCommandAndExecute();
}
private void ListBtn_OnClick(object sender, RoutedEventArgs e)
{
CommandTb.Text = "list targets -v";
GetCommandAndExecute();
}
private async void GetCommandAndExecute()
{
string command = CommandTb.Text;
AppendHintAndScrollToEnd( $"execute command: {command} \n");
string result = await Task.Run(() => HdcUtil.RunCommand(command));
AppendHintAndScrollToEnd($"execute result: {result} \n");
}
private void CheckServerBtn_OnClick(object sender, RoutedEventArgs e)
{
CommandTb.Text = "checkserver";
GetCommandAndExecute();
}
private void ConnectBtn_OnClick(object sender, RoutedEventArgs e)
{
CommandTb.Text = $"-t {TargetTb.Text}";
GetCommandAndExecute();
}
private void KillBtn_OnClick(object sender, RoutedEventArgs e)
{
CommandTb.Text = "kill -r";
GetCommandAndExecute();
}
protected override void OnClosing(CancelEventArgs e)
{
e.Cancel = true;
KillHdcExe();
e.Cancel = false;
}
private async void KillHdcExe()
{
string result = await Task.Run(() => CmdUtil.RunCmd("taskkill /IM hdc.exe"));
AppendHintAndScrollToEnd(result + "\n");
}
private void UninstallBtn_OnClick(object sender, RoutedEventArgs e)
{
CommandTb.Text = $"uninstall {PackageNameTb.Text}";
GetCommandAndExecute();
}
private void AppendHintAndScrollToEnd(string hint)
{
ResultTb.Text += hint;
ResultTb.ScrollToEnd();
}
private async void GetImage_OnClick(object sender, RoutedEventArgs e)
{
ExportFile("/storage/media/100/local/files/Photo");
}
private async void ExportFile(string path)
{
var formattedDate = DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss");
CmdUtil.RunCmd($"mkdir {formattedDate}");
AppendHintAndScrollToEnd($"create dir: {formattedDate} \n");
AppendHintAndScrollToEnd($"export {path} to {formattedDate} \n");
string result = await Task.Run(() => HdcUtil.ExportFile(path));
AppendHintAndScrollToEnd($"export result: {result} \n");
}
} }

View File

@ -0,0 +1,57 @@
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
namespace HarmonyDevTools.control;
public static class TextBoxHelper
{
public static readonly DependencyProperty PlaceholderProperty =
DependencyProperty.RegisterAttached("Placeholder", typeof(string), typeof(TextBoxHelper),
new PropertyMetadata(default(string), OnPlaceholderChanged));
public static void SetPlaceholder(UIElement element, string value)
{
element.SetValue(PlaceholderProperty, value);
}
public static string GetPlaceholder(UIElement element)
{
return (string)element.GetValue(PlaceholderProperty);
}
private static void OnPlaceholderChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (d is TextBox textBox)
{
textBox.GotFocus -= RemovePlaceholder;
textBox.LostFocus -= ShowPlaceholder;
if (!string.IsNullOrEmpty((string)e.NewValue))
{
textBox.GotFocus += RemovePlaceholder;
textBox.LostFocus += ShowPlaceholder;
ShowPlaceholder(textBox, null);
}
}
}
private static void RemovePlaceholder(object sender, RoutedEventArgs e)
{
if (sender is TextBox textBox && textBox.Text == GetPlaceholder(textBox))
{
textBox.Text = "";
textBox.Foreground = Brushes.Black;
}
}
private static void ShowPlaceholder(object sender, RoutedEventArgs e)
{
if (sender is TextBox textBox && string.IsNullOrEmpty(textBox.Text))
{
textBox.Text = GetPlaceholder(textBox);
textBox.Foreground = Brushes.Gray;
}
}
}

View File

@ -0,0 +1,31 @@
set version=1.0.0
if not "%~1"=="" (
set version=%1
)
cd ../ && rmdir /s /q bin && rmdir /s /q obj
cd pack
if not exist "output" (
mkdir output
)
cd output
if exist "HarmonyTools-%version%.exe" (
del /q /s HarmonyDevTools-%version%.exe
)
cd ../
dotnet publish ../../HarmonyDevTools/HarmonyDevTools.csproj -c Release -r win-x86 /p:PublishSingleFile=true /p:IncludeNativeLibrariesForSelfExtract=true /p:SelfContained=false -o ./output
if NOT %errorlevel%==0 @goto :FailureOnBuild
cd output && move HarmonyDevTools.exe HarmonyDevTools-%version%.exe && move HarmonyDevTools.pdb HarmonyDevTools-%version%.pdb && cd ../
xcopy ..\..\toolchains .\output\toolchains /E /I /Y
cd ../bin && del /q /s Release && cd ../pack
goto :eof
:FailureOnBuild
echo build project failure

View File

@ -0,0 +1,119 @@
using System;
using System.Diagnostics;
using System.Text;
using log4net;
namespace HarmonyDevTools.util;
public static class CmdUtil
{
private static readonly ILog Log = LogManager.GetLogger(nameof(CmdUtil));
private const string CmdExe = "cmd.exe";
/**
* 使 cmd.exe
*/
public static string RunCmd(string parameter = "", bool withExit = true)
{
var output = new StringBuilder();
try
{
var pro = new Process
{
StartInfo =
{
FileName = CmdExe,
UseShellExecute = false,
RedirectStandardInput = true,
RedirectStandardOutput = true,
RedirectStandardError = true,
CreateNoWindow = true
}
};
if (parameter.Length > 0)
{
pro.StartInfo.Arguments = parameter;
}
pro.Start();
if (parameter.Length > 0)
{
pro.StandardInput.WriteLine(parameter + (withExit ? " &exit" : ""));
pro.StandardInput.AutoFlush = true;
}
while (!pro.StandardOutput.EndOfStream)
{
string? line = pro.StandardOutput.ReadLine();
if (!string.IsNullOrEmpty(line) && !line.StartsWith("(c) Microsoft") && !line.StartsWith("Microsoft Windows"))
{
output.AppendLine(line);
}
}
if (withExit)
{
pro.WaitForExit();
pro.Close();
}
}
catch (Exception e)
{
Log.Error(e);
}
return output.ToString();
}
/**
* 使 exe
*/
public static string RunExe(string exePath, string arguments = "")
{
var output = new StringBuilder();
try
{
var process = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = exePath,
Arguments = arguments,
UseShellExecute = false,
RedirectStandardInput = true,
RedirectStandardOutput = true,
RedirectStandardError = true,
CreateNoWindow = true
}
};
process.OutputDataReceived += (sender, args) =>
{
if (!string.IsNullOrEmpty(args.Data))
{
output.AppendLine(args.Data);
}
};
process.ErrorDataReceived += (sender, args) =>
{
if (!string.IsNullOrEmpty(args.Data))
{
output.AppendLine($"ERROR: {args.Data}");
}
};
process.Start();
process.BeginOutputReadLine();
process.BeginErrorReadLine();
process.WaitForExit();
process.Close();
}
catch (Exception e)
{
Log.Error(e);
}
return output.ToString();
}
}

View File

@ -0,0 +1,19 @@
using System;
namespace HarmonyDevTools.util;
public static class HdcUtil
{
public static string ExportFile(string originPath)
{
var formattedDate = DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss");
CmdUtil.RunCmd($"mkdir {formattedDate}");
var command = $"file recv {originPath} ./{formattedDate}";
return CmdUtil.RunExe("toolchains\\hdc.exe", command);
}
public static string RunCommand(string command)
{
return CmdUtil.RunExe("toolchains\\hdc.exe", command);
}
}

BIN
libs/log4net.dll Normal file

Binary file not shown.

BIN
toolchains/hdc.exe Normal file

Binary file not shown.