1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
| public class CoreValidationService { public enum CoreType { Error, Unknown, Client, ForgeInstaller, FabricInstaller, Forge, Fabric, Arclight, CatServer, CraftBukkit, Leaves, LightFall, Mohist, Paper, Vanilla, Velocity, } public static CoreType Validate(string? filePath, out string ErrorMessage) { ErrorMessage = ""; if (string.IsNullOrEmpty(filePath)) { ErrorMessage = "选定的路径为空"; return CoreType.Error; } if (!File.Exists(filePath)) { ErrorMessage = "选定的文件/路径不存在"; return CoreType.Error; } string? JarMainClass = GetMainClass(filePath); if (JarMainClass == null) return CoreType.Unknown; else if (JarMainClass.StartsWith("Access denied") || JarMainClass.StartsWith("Error")) { ErrorMessage = JarMainClass; return CoreType.Error; } else { return JarMainClass switch { "net.minecraft.server.MinecraftServer" => CoreType.Vanilla, "net.minecraft.bundler.Main" => CoreType.Vanilla, "net.minecraft.client.Main" => CoreType.Client, "net.minecraftforge.installer.SimpleInstaller" => CoreType.ForgeInstaller, "net.fabricmc.installer.Main" => CoreType.FabricInstaller, "net.fabricmc.installer.ServerLauncher" => CoreType.Fabric, "io.izzel.arclight.server.Launcher" => CoreType.Arclight, "catserver.server.CatServerLaunch" => CoreType.CatServer, "foxlaunch.FoxServerLauncher" => CoreType.CatServer, "org.bukkit.craftbukkit.Main" => CoreType.CraftBukkit, "org.bukkit.craftbukkit.bootstrap.Main" => CoreType.CraftBukkit, "io.papermc.paperclip.Main" => CoreType.Paper, "org.leavesmc.leavesclip.Main" => CoreType.Leaves, "net.md_5.bungee.Bootstrap" => CoreType.LightFall, "com.mohistmc.MohistMCStart" => CoreType.Mohist, "com.mohistmc.MohistMC" => CoreType.Mohist, "com.destroystokyo.paperclip.Paperclip" => CoreType.Paper, "com.velocitypowered.proxy.Velocity" => CoreType.Velocity, _ => CoreType.Unknown, }; } } public static string? GetMainClass(string jarFilePath) { try { using FileStream stream = new FileStream(jarFilePath, FileMode.Open); using ZipArchive archive = new ZipArchive(stream, ZipArchiveMode.Read); ZipArchiveEntry? manifestEntry = archive.Entries.FirstOrDefault(entry => entry.FullName == "META-INF/MANIFEST.MF"); if (manifestEntry != null) { using StreamReader reader = new StreamReader(manifestEntry.Open()); string manifestContent = reader.ReadToEnd(); return FindMainClassLine(manifestContent); } return null; } catch (UnauthorizedAccessException ex) { Console.WriteLine("Access denied: " + ex.Message); return "Access denied: " + ex.Message; } catch (IOException ex) { Console.WriteLine("IO error: " + ex.Message); return "Error reading file: " + ex.Message; } catch (Exception ex) { Console.WriteLine("Error reading jar file: " + ex.Message); return "Error reading jar file: " + ex.Message; } }
public static string? FindMainClassLine(string manifestContent) { string[] lines = manifestContent.Split(["\r\n", "\r", "\n"], StringSplitOptions.None); foreach (string line in lines) { if (line.StartsWith("Main-Class:")) { return line.Substring("Main-Class:".Length).Trim(); } } return null; } }
|