ModelingDebug/Mod.cs
2024-08-25 17:55:05 +08:00

204 lines
5.5 KiB
C#

using Debug;
using Microsoft.VisualBasic;
using Microsoft.Win32;
/// <summary>
/// 模组搜索引擎操作
/// </summary>
public class Mod
{
private static DateTime StartTime;
/// <summary>
/// 结束时间
/// </summary>
private static void EndTime()
{
Console.WriteLine("结束" + (DateTime.Now - StartTime).TotalMilliseconds + "ms");
}
private readonly bool Isdot = true;
private readonly List<string> ExcludeFile = [
"Android", "androidword", "Java",
"Linux", "linuxword", "Mac",
"Windows", "Windows 10", "Steam",
"Genshin Impact Game", "Godot", "Gradle",
"WeGame", "Mental Omega", "WeGameApps",
"ramboplayGame", "QQ", "QQWORD","Microsoft SDKs","Creator","editors","AndroidSDK","SDK","Pr","PR","PS","Ps","WE",
"androidstudioword","xshell7"
];
public string FileDir = string.Empty;
public Mod()
{
Console.Clear();
var SearchMode = 1;
do
{
Program.printfLine("输入 1.默认搜索 2.只执行steam搜索 3.只执行全盘搜索 0.返回主页");
var v = Console.ReadLine();
if (int.TryParse(v, out int i))
{
SearchMode = i;
if (SearchMode == 0)
{
Console.Clear();
break;
}
// StartTime = DateTime.Now;
if (SearchMode == 1 || SearchMode == 2)
{
steam();//先看看steam中有没有安装铁锈战争
//steam中没有安装铁锈战争扫全盘
}
if (SearchMode == 1 || SearchMode == 3)
{
if (FileDir == string.Empty)
{
init();
}
}
if (!string.IsNullOrEmpty(FileDir))
{
Program.printfLine("找到了:" + FileDir);
}
}
} while (true);
}
private void steam()
{
var steamPath = GetSteamInstallPath();
if (!string.IsNullOrEmpty(steamPath))
{
string v = Path.Combine(steamPath, "steamapps\\common\\Rusted Warfare");
// Program.printfLine("找到了:" + v);
if (Directory.Exists(v))
{
FileDir = v;
// EndTime();
}
}
}
private void init()
{
var allDrives = DriveInfo.GetDrives();
foreach (var d in allDrives)
{
Console.WriteLine();
if (d.IsReady == true)
{
var name = d.Name;
if (ExcludeFile.Contains(name))
{
continue;
}
if (name.Equals("C:\\"))
{
FileSearch(name);
}
else
{
if (Directory.Exists(name + "/Rusted Warfare"))
{
EndTime();
break;
}
else
{
FileSearch(name);
}
}
}
}
}
public bool IsSearch = true;
private void FileSearch(string path)
{
try
{
foreach (var file in Directory.EnumerateDirectories(path))
{
if (!IsSearch)
{
return;
}
var name = Path.GetFileName(file);
if (Isdot && name.StartsWith("."))
{
continue;
}
if (ExcludeFile.Contains(name))
{
continue;
}
if (name.Equals("Rusted Warfare"))
{
FileDir = file;
EndTime();
break;
}
else
{
FileSearch(file);
}
}
}
catch (UnauthorizedAccessException)
{
Console.WriteLine($"Access denied for: {path}");
}
catch (Exception ex)
{
Console.WriteLine($"Error accessing directory '{path}': {ex.Message}");
}
}
private static string GetSteamInstallPath()
{
var installPath = GetInstallPathFromKey(@"Software\Valve\Steam");
if (string.IsNullOrEmpty(installPath))
{
installPath = GetInstallPathFromKey(@"SOFTWARE\Wow6432Node\Valve\Steam");
}
return installPath;
string GetInstallPathFromKey(string subKey)
{
using (RegistryKey key = Registry.CurrentUser.OpenSubKey(subKey))
{
if (key != null)
{
object path = key.GetValue("InstallPath");
if (path != null)
{
return (string)path;
}
}
}
using (RegistryKey key = Registry.LocalMachine.OpenSubKey(subKey))
{
if (key != null)
{
var path = key.GetValue("InstallPath");
if (path != null)
{
return (string)path;
}
}
}
return string.Empty;
}
}
}