From 555cc61fbeac67009f6e3d973095d8db223b9b2a Mon Sep 17 00:00:00 2001 From: Shiroiame-Kusu Date: Fri, 29 Mar 2024 14:08:25 +0800 Subject: [PATCH] Ver 0.12.0-RC1: Fix a lot of the problems. --- Hakuu/Universal/Global.cs | 2 +- Hakuu/Universal/Properties/AssemblyInfo.cs | 4 +- Hakuu/Universal/Settings/Server.cs | 2 + Hakuu/Universal/Utils/FileOccupationCheck.cs | 179 ++++++++++++++++++ Hakuu/Universal/Utils/PropertyOperation.cs | 49 ++++- Hakuu/WPF/App.xaml.cs | 30 ++- Hakuu/WPF/Properties/Resources.resx | 1 + Hakuu/WPF/Windows/Pages/Server/Panel.xaml | 5 +- Hakuu/WPF/Windows/Pages/Server/Panel.xaml.cs | 34 +++- .../WPF/Windows/Pages/Server/Properties.xaml | 44 ++--- .../Windows/Pages/Server/Properties.xaml.cs | 131 ++++++++----- Hakuu/WPF/Windows/Pages/Settings/Serein.xaml | 21 ++ .../WPF/Windows/Pages/Settings/Serein.xaml.cs | 32 ++++ Hakuu/WPF/Windows/Pages/Settings/Server.xaml | 31 ++- 14 files changed, 471 insertions(+), 94 deletions(-) create mode 100644 Hakuu/Universal/Utils/FileOccupationCheck.cs diff --git a/Hakuu/Universal/Global.cs b/Hakuu/Universal/Global.cs index 807bd340..2c3a9ab6 100644 --- a/Hakuu/Universal/Global.cs +++ b/Hakuu/Universal/Global.cs @@ -23,7 +23,7 @@ internal static class Global /// /// 版本号 /// - public const string VERSION = "v0.12.0-B2"; + public const string VERSION = "v0.12.0-RC1"; //public static readonly string VERSION = FileVersionInfo.GetVersionInfo(Assembly.GetExecutingAssembly().Location).FileVersion.ToString(); /// diff --git a/Hakuu/Universal/Properties/AssemblyInfo.cs b/Hakuu/Universal/Properties/AssemblyInfo.cs index ffb0475a..427194f3 100644 --- a/Hakuu/Universal/Properties/AssemblyInfo.cs +++ b/Hakuu/Universal/Properties/AssemblyInfo.cs @@ -4,11 +4,11 @@ [assembly: AssemblyTitle("Hakuu")] [assembly: AssemblyDescription("新时代极简服务器面板")] [assembly: AssemblyConfiguration("")] -[assembly: AssemblyCompany("https://Hakuu.cc")] +[assembly: AssemblyCompany("https://nyat.icu")] [assembly: AssemblyProduct("Hakuu - 新时代极简服务器面板")] [assembly: AssemblyCopyright("Copyright © Zaitonn & Shiroiame-Kusu")] [assembly: AssemblyTrademark("")] [assembly: AssemblyCulture("")] [assembly: ComVisible(false)] [assembly: AssemblyVersion("0.12.0.1")] -[assembly: AssemblyFileVersion("0.12.0-B2")] +[assembly: AssemblyFileVersion("0.12.0-RC1")] diff --git a/Hakuu/Universal/Settings/Server.cs b/Hakuu/Universal/Settings/Server.cs index 647f2d78..ef733d36 100644 --- a/Hakuu/Universal/Settings/Server.cs +++ b/Hakuu/Universal/Settings/Server.cs @@ -40,5 +40,7 @@ internal class Server public string? JavaPath; public bool AutoJVMOptimization = true; + public bool isCustomizedTitleEnabled = false; + public string CustomizedTitle = ""; } } diff --git a/Hakuu/Universal/Utils/FileOccupationCheck.cs b/Hakuu/Universal/Utils/FileOccupationCheck.cs new file mode 100644 index 00000000..90b0c381 --- /dev/null +++ b/Hakuu/Universal/Utils/FileOccupationCheck.cs @@ -0,0 +1,179 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.IO; +using System.Linq; +using System.Runtime.InteropServices; +using System.Text.RegularExpressions; +using System.Threading.Tasks; +using System.Windows.Media.Media3D; +using Hakuu.Properties; +using Microsoft.Win32.SafeHandles; + +namespace Hakuu.Utils +{ + public class FileOccupationCheck + { + public static int PID; + public static string ProcessName; + [DllImport("kernel32.dll", SetLastError = true)] + static extern uint GetFinalPathNameByHandle(IntPtr hFile, [MarshalAs(UnmanagedType.LPTStr)] System.Text.StringBuilder lpszFilePath, uint cchFilePath, uint dwFlags); + + [DllImport("kernel32.dll", SetLastError = true)] + static extern IntPtr OpenProcess(uint dwDesiredAccess, bool bInheritHandle, int dwProcessId); + + [DllImport("kernel32.dll", SetLastError = true)] + static extern bool CloseHandle(IntPtr hObject); + + [Flags] + enum FileAccessFlags : uint + { + FILE_READ_DATA = 0x0001, + FILE_WRITE_DATA = 0x0002, + FILE_APPEND_DATA = 0x0004, + FILE_READ_EA = 0x0008, + FILE_WRITE_EA = 0x0010, + FILE_EXECUTE = 0x0020, + FILE_READ_ATTRIBUTES = 0x0080, + FILE_WRITE_ATTRIBUTES = 0x0100, + FILE_ALL_ACCESS = 0x1F01FF, + FILE_GENERIC_READ = 0x120089, + FILE_GENERIC_WRITE = 0x120116, + FILE_GENERIC_EXECUTE = 0x1200A0 + } + + [Flags] + enum ProcessAccessFlags : uint + { + PROCESS_QUERY_INFORMATION = 0x0400, + PROCESS_VM_READ = 0x0010 + } + + public static bool Check(string filePath, out string ProcessName,out int? PID) + { + ProcessName = ""; + PID = 0; + // ļǷ + if (File.Exists(filePath)) + { + try + { + // ȡռøļн + var processes = GetProcessesUsingFile(filePath); + + if (processes.Count > 0) + { + Console.WriteLine("½ʹļ:"); + foreach (var process in processes) + { + Console.WriteLine($": {process.ProcessName}, PID: {process.Id}"); + ProcessName = process.ProcessName; + PID = process.Id; + if (PID == 0) + { + return true; + } + return false; + } + } + else + { + Console.WriteLine("ûнʹļ."); + } + } + catch (Exception ex) + { + Console.WriteLine($"쳣: {ex.Message}"); + return false; + } + return true; + } + else + { + Console.WriteLine("ļ."); + return false; + } + } + + static List GetProcessesUsingFile(string filePath) + { + var processes = new List(); + IntPtr handle = IntPtr.Zero; + + try + { + // ļ + handle = OpenFile(filePath); + + // ļɹ + if (handle != IntPtr.Zero) + { + // ȡļĽID + uint processId = GetProcessIdFromHandle(handle); + + // ȡ̶ + Process process = Process.GetProcessById((int)processId); + + // ̶Ϊգӵб + if (process != null) + { + processes.Add(process); + } + } + } + finally + { + // رļ + if (handle != IntPtr.Zero) + { + CloseHandle(handle); + } + } + + return processes; + } + + static IntPtr OpenFile(string filePath) + { + // ļ + IntPtr handle = IntPtr.Zero; + + try + { + // ļ + handle = CreateFile(filePath, FileAccessFlags.FILE_READ_ATTRIBUTES, FileShare.ReadWrite | FileShare.Delete, IntPtr.Zero, FileMode.Open, 0, IntPtr.Zero); + } + catch + { + // Դļʧܵ쳣 + } + + return handle; + } + + static uint GetProcessIdFromHandle(IntPtr handle) + { + // ȡID + uint processId = 0; + GetWindowThreadProcessId(handle, out processId); + return processId; + } + + const uint FILE_SHARE_READ = 0x00000001; + const uint FILE_SHARE_WRITE = 0x00000002; + const uint OPEN_EXISTING = 3; + + [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)] + static extern IntPtr CreateFile( + string lpFileName, + FileAccessFlags dwDesiredAccess, + FileShare dwShareMode, + IntPtr lpSecurityAttributes, + FileMode dwCreationDisposition, + uint dwFlagsAndAttributes, + IntPtr hTemplateFile); + + [DllImport("user32.dll", SetLastError = true)] + static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint processId); + } +} \ No newline at end of file diff --git a/Hakuu/Universal/Utils/PropertyOperation.cs b/Hakuu/Universal/Utils/PropertyOperation.cs index afc27e57..ea843d94 100644 --- a/Hakuu/Universal/Utils/PropertyOperation.cs +++ b/Hakuu/Universal/Utils/PropertyOperation.cs @@ -1,12 +1,17 @@ using System; using System.Collections; +using System.Diagnostics; using System.IO; +using System.Threading.Tasks; +using System.Windows.Threading; namespace Hakuu.Utils { public class PropertyOperation : System.Collections.Hashtable { private string? fileName; + public static int? FileOccupiedProcessPID; + public static string? FileOccupiedProcessName; private ArrayList list = new ArrayList(); public ArrayList List { @@ -21,6 +26,7 @@ public PropertyOperation(string fileName) { PropertyOperator(fileName); } + public void PropertyOperator(string fileName) { this.fileName = fileName; @@ -130,15 +136,55 @@ private void Load(string filePath) } } } + public bool FileCheck(string filePath) + { + Process[] processes = Process.GetProcesses(); + foreach (Process process in processes) + { + try + { + // 获取进程打开的文件句柄 + foreach (ProcessModule module in process.Modules) + { + if (module.FileName.Equals(filePath, StringComparison.OrdinalIgnoreCase)) + { + Console.WriteLine($"Process Name: {process.ProcessName}, PID: {process.Id}"); + return false; + } + } + } + catch (Exception ex) + { + // 忽略访问拒绝或其他异常 + Console.WriteLine($"Error accessing process {process.ProcessName}: {ex.Message}"); + } + } + return true; + + } /// /// �����ļ� /// /// Ҫ������ļ���·�� - public void Save() + public bool Save() { string? filePath = this.fileName; if (File.Exists(filePath)) { + int PID; + string ProcessName; + /*if (FileOccupationCheck.Check(filePath,out FileOccupiedProcessName,out FileOccupiedProcessPID)) + { + File.Delete(filePath); + + } + else + { + //FileOccupiedProcessPID = PID; + //FileOccupiedProcessName = ProcessName; + + return false; + }*/ File.Delete(filePath); } FileStream fileStream = File.Create(filePath); @@ -165,6 +211,7 @@ public void Save() } sw.Close(); fileStream.Close(); + return true; } } } \ No newline at end of file diff --git a/Hakuu/WPF/App.xaml.cs b/Hakuu/WPF/App.xaml.cs index 6e4cd57f..4fa76a32 100644 --- a/Hakuu/WPF/App.xaml.cs +++ b/Hakuu/WPF/App.xaml.cs @@ -1,5 +1,7 @@ using Hakuu.Utils; +using System.Runtime.InteropServices; using System.Windows; +using static System.Windows.Forms.VisualStyles.VisualStyleElement.TreeView; namespace Hakuu { @@ -19,10 +21,34 @@ namespace Hakuu public partial class App : Application { - public App() - { + public static string? Username = null; + public static string? Password = null; + [DllImport("kernel32.dll", SetLastError = true)] + [return: MarshalAs(UnmanagedType.Bool)] + static extern bool AllocConsole(); + + [DllImport("kernel32.dll", SetLastError = true)] + [return: MarshalAs(UnmanagedType.Bool)] + static extern bool FreeConsole(); + protected override void OnStartup(StartupEventArgs e) + { + base.OnStartup(e); Runtime.Init(); DispatcherUnhandledException += (_, e) => CrashInterception.ShowException(e.Exception); + if (Global.BRANCH != "Release" || Global.BRANCH != "ReleaseCandidate") + { + AllocConsole(); // 打开控制台 + } + else + { + FreeConsole(); // 关闭控制台 + } } + /*public App() + { + Runtime.Init(); + DispatcherUnhandledException += (_, e) => CrashInterception.ShowException(e.Exception); + + }*/ } } diff --git a/Hakuu/WPF/Properties/Resources.resx b/Hakuu/WPF/Properties/Resources.resx index 8a40a091..cadc2149 100644 --- a/Hakuu/WPF/Properties/Resources.resx +++ b/Hakuu/WPF/Properties/Resources.resx @@ -117,6 +117,7 @@ System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + ..\..\Universal\buildinfo.info;System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;gb2312 diff --git a/Hakuu/WPF/Windows/Pages/Server/Panel.xaml b/Hakuu/WPF/Windows/Pages/Server/Panel.xaml index c27a728a..238547ff 100644 --- a/Hakuu/WPF/Windows/Pages/Server/Panel.xaml +++ b/Hakuu/WPF/Windows/Pages/Server/Panel.xaml @@ -142,14 +142,15 @@ Click="Restart_Click" Content="重启" Icon="ArrowClockwise24" /> -