Skip to content

Commit

Permalink
Ver 0.12.0-RC1: Fix a lot of the problems.
Browse files Browse the repository at this point in the history
  • Loading branch information
Shiroiame-Kusu committed Mar 29, 2024
1 parent f75b97a commit 555cc61
Show file tree
Hide file tree
Showing 14 changed files with 471 additions and 94 deletions.
2 changes: 1 addition & 1 deletion Hakuu/Universal/Global.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ internal static class Global
/// <summary>
/// 版本号
/// </summary>
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();

/// <summary>
Expand Down
4 changes: 2 additions & 2 deletions Hakuu/Universal/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
2 changes: 2 additions & 0 deletions Hakuu/Universal/Settings/Server.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,7 @@ internal class Server
public string? JavaPath;

public bool AutoJVMOptimization = true;
public bool isCustomizedTitleEnabled = false;
public string CustomizedTitle = "";
}
}
179 changes: 179 additions & 0 deletions Hakuu/Universal/Utils/FileOccupationCheck.cs
Original file line number Diff line number Diff line change
@@ -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;

Check warning on line 18 in Hakuu/Universal/Utils/FileOccupationCheck.cs

View workflow job for this annotation

GitHub Actions / BuildForWindows (WPF)

Non-nullable field 'ProcessName' must contain a non-null value when exiting constructor. Consider declaring the field as nullable.
[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<Process> GetProcessesUsingFile(string filePath)
{
var processes = new List<Process>();
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);
}
}
49 changes: 48 additions & 1 deletion Hakuu/Universal/Utils/PropertyOperation.cs
Original file line number Diff line number Diff line change
@@ -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
{
Expand All @@ -21,6 +26,7 @@ public PropertyOperation(string fileName)
{
PropertyOperator(fileName);
}

public void PropertyOperator(string fileName)
{
this.fileName = fileName;
Expand Down Expand Up @@ -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))

Check warning on line 149 in Hakuu/Universal/Utils/PropertyOperation.cs

View workflow job for this annotation

GitHub Actions / BuildForWindows (WPF)

Dereference of a possibly null reference.

Check warning on line 149 in Hakuu/Universal/Utils/PropertyOperation.cs

View workflow job for this annotation

GitHub Actions / BuildForWindows (WPF)

Dereference of a possibly null reference.
{
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;

}
/// <summary>
/// �����ļ�
/// </summary>
/// <param name="filePath">Ҫ������ļ���·��</param>
public void Save()
public bool Save()
{
string? filePath = this.fileName;
if (File.Exists(filePath))
{
int PID;

Check warning on line 174 in Hakuu/Universal/Utils/PropertyOperation.cs

View workflow job for this annotation

GitHub Actions / BuildForWindows (WPF)

The variable 'PID' is declared but never used
string ProcessName;

Check warning on line 175 in Hakuu/Universal/Utils/PropertyOperation.cs

View workflow job for this annotation

GitHub Actions / BuildForWindows (WPF)

The variable 'ProcessName' is declared but never used
/*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);

Check warning on line 190 in Hakuu/Universal/Utils/PropertyOperation.cs

View workflow job for this annotation

GitHub Actions / BuildForWindows (WPF)

Possible null reference argument for parameter 'path' in 'FileStream File.Create(string path)'.
Expand All @@ -165,6 +211,7 @@ public void Save()
}
sw.Close();
fileStream.Close();
return true;
}
}
}
30 changes: 28 additions & 2 deletions Hakuu/WPF/App.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using Hakuu.Utils;
using System.Runtime.InteropServices;
using System.Windows;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.TreeView;

namespace Hakuu
{
Expand All @@ -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(); // 关闭控制台

Check warning on line 44 in Hakuu/WPF/App.xaml.cs

View workflow job for this annotation

GitHub Actions / BuildForWindows (WPF)

Unreachable code detected

Check warning on line 44 in Hakuu/WPF/App.xaml.cs

View workflow job for this annotation

GitHub Actions / BuildForWindows (WPF)

Unreachable code detected

Check warning on line 44 in Hakuu/WPF/App.xaml.cs

View workflow job for this annotation

GitHub Actions / BuildForWindows (WPF)

Unreachable code detected

Check warning on line 44 in Hakuu/WPF/App.xaml.cs

View workflow job for this annotation

GitHub Actions / BuildForWindows (WPF)

Unreachable code detected

Check warning on line 44 in Hakuu/WPF/App.xaml.cs

View workflow job for this annotation

GitHub Actions / BuildForWindows (WPF)

Unreachable code detected

Check warning on line 44 in Hakuu/WPF/App.xaml.cs

View workflow job for this annotation

GitHub Actions / BuildForWindows (WPF)

Unreachable code detected
}
}
/*public App()
{
Runtime.Init();
DispatcherUnhandledException += (_, e) => CrashInterception.ShowException(e.Exception);
}*/
}
}
1 change: 1 addition & 0 deletions Hakuu/WPF/Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<assembly alias="System.Windows.Forms" name="System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<data name="BuildInfo" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\..\Universal\buildinfo.info;System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;gb2312</value>
</data>
Expand Down
5 changes: 3 additions & 2 deletions Hakuu/WPF/Windows/Pages/Server/Panel.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -142,14 +142,15 @@
Click="Restart_Click"
Content="重启"
Icon="ArrowClockwise24" />
<Button
<ui:Button
Grid.Row="1"
Grid.Column="1"
Margin="3"
HorizontalContentAlignment="Stretch"
VerticalContentAlignment="Stretch"
Click="Kill_Click"
Content="结束进程" />
Content="终止"
Icon="Stop24"/>
</Grid>
</GroupBox>
<GroupBox
Expand Down
Loading

0 comments on commit 555cc61

Please sign in to comment.