Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ver 0.12.0-RC1: Fix a lot of the problems. #6

Merged
merged 1 commit into from
Mar 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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("鏂版椂浠f瀬绠�鏈嶅姟鍣ㄩ潰鏉�")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("https://Hakuu.cc")]
[assembly: AssemblyCompany("https://nyat.icu")]
[assembly: AssemblyProduct("Hakuu - 鏂版椂浠f瀬绠�鏈嶅姟鍣ㄩ潰鏉�")]
[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 @@
{
PropertyOperator(fileName);
}

public void PropertyOperator(string fileName)
{
this.fileName = fileName;
Expand Down Expand Up @@ -70,8 +76,8 @@
{
while (sr.Peek() >= 0)
{
bufLine = sr.ReadLine();

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

View workflow job for this annotation

GitHub Actions / BuildForWindows (WPF)

Converting null literal or possible null value to non-nullable type.

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

View workflow job for this annotation

GitHub Actions / BuildForWindows (WPF)

Converting null literal or possible null value to non-nullable type.
limit = bufLine.Length;

Check warning on line 80 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 80 in Hakuu/Universal/Utils/PropertyOperation.cs

View workflow job for this annotation

GitHub Actions / BuildForWindows (WPF)

Dereference of a possibly null reference.
keyLen = 0;
valueStart = limit;
hasSep = false;
Expand Down Expand Up @@ -130,23 +136,63 @@
}
}
}
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)'.
StreamWriter sw = new StreamWriter(fileStream);
foreach (object item in list)
{
String key = (String)item;
String val = (String)this[key];

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

View workflow job for this annotation

GitHub Actions / BuildForWindows (WPF)

Converting null literal or possible null value to non-nullable type.
if (key.StartsWith("#"))
{
if (val == "")
Expand All @@ -165,6 +211,7 @@
}
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 @@
锘縰sing 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 @@

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
Loading