leejunyoung5819mc-paper-plugin-test-mcp-26.3

공개

마인크래프트 26.3 Paper 서버의 플러그인 테스트를 하려는 AI 에이전트를 위한 MCP 도구

121줄4.3 KB
1// Resolve configured plugin jar patterns to the exact jars to install.
2// Each jar's plugin.yml / paper-plugin.yml is read so that several builds of the SAME plugin
3// (old versions left in build/libs, shadow "-all" + plain jar) collapse to the newest one,
4// while a pattern like "../libs/*.jar" can still bring in many different plugins.
5import fs from 'node:fs';
6import path from 'node:path';
7import zlib from 'node:zlib';
8import YAML from 'yaml';
9import { expandGlob } from './config.ts';
10
11/** Read one entry from a zip/jar without dependencies. Returns null if missing or unreadable. */
12export function readZipEntry(file: string, wanted: string): Buffer | null {
13 let buf: Buffer;
14 try {
15 buf = fs.readFileSync(file);
16 } catch {
17 return null;
18 }
19 // End of central directory: signature 0x06054b50, within the last 64KB + 22 bytes.
20 let eocd = -1;
21 for (let i = buf.length - 22; i >= Math.max(0, buf.length - 65_557); i--) {
22 if (buf.readUInt32LE(i) === 0x06054b50) {
23 eocd = i;
24 break;
25 }
26 }
27 if (eocd < 0) return null;
28 const count = buf.readUInt16LE(eocd + 10);
29 let p = buf.readUInt32LE(eocd + 16);
30 for (let n = 0; n < count && p + 46 <= buf.length; n++) {
31 if (buf.readUInt32LE(p) !== 0x02014b50) return null;
32 const method = buf.readUInt16LE(p + 10);
33 const csize = buf.readUInt32LE(p + 20);
34 const nameLen = buf.readUInt16LE(p + 28);
35 const extraLen = buf.readUInt16LE(p + 30);
36 const commentLen = buf.readUInt16LE(p + 32);
37 const local = buf.readUInt32LE(p + 42);
38 const name = buf.subarray(p + 46, p + 46 + nameLen).toString('utf8');
39 if (name === wanted) {
40 const lNameLen = buf.readUInt16LE(local + 26);
41 const lExtraLen = buf.readUInt16LE(local + 28);
42 const start = local + 30 + lNameLen + lExtraLen;
43 const data = buf.subarray(start, start + csize);
44 try {
45 if (method === 0) return Buffer.from(data);
46 if (method === 8) return zlib.inflateRawSync(data);
47 } catch {
48 return null;
49 }
50 return null;
51 }
52 p += 46 + nameLen + extraLen + commentLen;
53 }
54 return null;
55}
56
57export interface PluginJar {
58 file: string;
59 pattern: string;
60 /** Plugin name from the descriptor, or the file name when the jar has none. */
61 plugin: string;
62 version: string | null;
63 descriptor: 'paper-plugin.yml' | 'plugin.yml' | null;
64 mtime: string;
65}
66
67export function describeJar(file: string, pattern = file): PluginJar {
68 let plugin = path.basename(file);
69 let version: string | null = null;
70 let descriptor: PluginJar['descriptor'] = null;
71 for (const d of ['paper-plugin.yml', 'plugin.yml'] as const) {
72 const raw = readZipEntry(file, d);
73 if (!raw) continue;
74 try {
75 const y = YAML.parse(raw.toString('utf8'));
76 if (y?.name) {
77 plugin = String(y.name);
78 version = y.version !== undefined ? String(y.version) : null;
79 descriptor = d;
80 break;
81 }
82 } catch {
83 /* fall through */
84 }
85 }
86 return { file, pattern, plugin, version, descriptor, mtime: fs.statSync(file).mtime.toISOString() };
87}
88
89export interface Resolution {
90 /** Jars that will be installed: one per plugin, the most recently built. */
91 selected: PluginJar[];
92 /** Other jars of an already-selected plugin (older builds, duplicates). */
93 skipped: (PluginJar & { reason: string })[];
94 /** Patterns that matched no file. */
95 unmatched: string[];
96}
97
98export function resolvePlugins(patterns: string[]): Resolution {
99 const all: PluginJar[] = [];
100 const unmatched: string[] = [];
101 for (const pattern of patterns) {
102 const files = expandGlob(pattern);
103 if (files.length === 0) unmatched.push(pattern);
104 for (const f of files) if (!all.some((j) => j.file === f)) all.push(describeJar(f, pattern));
105 }
106 const byPlugin = new Map<string, PluginJar[]>();
107 for (const j of all) {
108 const key = j.plugin.toLowerCase();
109 byPlugin.set(key, [...(byPlugin.get(key) ?? []), j]);
110 }
111 const selected: PluginJar[] = [];
112 const skipped: Resolution['skipped'] = [];
113 for (const group of byPlugin.values()) {
114 group.sort((a, b) => b.mtime.localeCompare(a.mtime));
115 selected.push(group[0]);
116 for (const j of group.slice(1)) {
117 skipped.push({ ...j, reason: `older build of plugin '${group[0].plugin}' (using ${path.basename(group[0].file)})` });
118 }
119 }
120 return { selected, skipped, unmatched };
121}