Plugin Maneger #276
| @@ -8,6 +8,6 @@ module.exports = { | ||||
|     ignore: [], | ||||
|   }, | ||||
|   addModuleEntry: false, | ||||
|   addPackageJson: true, | ||||
|   addPackageJson: false, | ||||
|   filesWithShebang: [], | ||||
| }; | ||||
							
								
								
									
										5943
									
								
								package-lock.json
									
									
									
										generated
									
									
									
								
							
							
						
						
									
										5943
									
								
								package-lock.json
									
									
									
										generated
									
									
									
								
							
										
											
												File diff suppressed because it is too large
												Load Diff
											
										
									
								
							
							
								
								
									
										74
									
								
								src/PluginManeger.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										74
									
								
								src/PluginManeger.js
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,74 @@ | ||||
| const request = require("./lib/Requests"); | ||||
| const BdsSettings = require("./lib/BdsSettings"); | ||||
| const path = require("path"); | ||||
| const fs = require("fs"); | ||||
| const js_yaml = require("js-yaml"); | ||||
|  | ||||
| const RawGithubUrl = "https://raw.githubusercontent.com/The-Bds-Maneger/Plugins_Repository/main"; | ||||
|  | ||||
| async function PluginManeger(BdsPlatform = BdsSettings.GetPlatform()) { | ||||
|   const GetPluginsPath = async () => { | ||||
|     return (await request.GithubTree("The-Bds-Maneger/Plugins_Repository", "main")).tree.filter(Tree => Tree.path.startsWith(BdsPlatform)); | ||||
|   } | ||||
|   if ((await GetPluginsPath()).length === 0) throw new Error(`Bds Platform ${BdsPlatform} not found`); | ||||
|   const GetPlugin = async (pluginName = "") => { | ||||
|     if (!pluginName) throw new Error("Plugin name not found"); | ||||
|     const RepositoryPlugins = (await GetPluginsPath()); | ||||
|     if (RepositoryPlugins[0] === undefined) throw new Error(`Bds Platform ${BdsPlatform} not found`); | ||||
|     const PluginPath = `${BdsPlatform}/${pluginName.split("").filter(le => /[a-zA-Z0-9]/gi.test(le)).join("").charAt(0).toLocaleLowerCase()}/${pluginName}`; | ||||
|     const PluginArray = RepositoryPlugins.filter(Plugin => Plugin.path.startsWith(PluginPath)); | ||||
|     if (PluginArray[0] === undefined) throw new Error(`Plugin ${pluginName} not found`); | ||||
|     const ConfigFile = js_yaml.load(await request.text(`${RawGithubUrl}/${PluginPath}/${path.basename(PluginArray.filter(A => /config\.y[a]ml$/gi.test(A.path))[0].path)}`)); | ||||
|     console.log(ConfigFile); | ||||
|     const ParseConfigVersion = /* ConfigFile.revision.trim() || */ "v1" | ||||
|     if (fs.existsSync(path.resolve(__dirname, `./PluginManeger/revision/${ParseConfigVersion}/Config`))) throw new Error(`Plugin ${pluginName} is outdated`); | ||||
|     return require(`./PluginManeger/revision/${ParseConfigVersion}/Config`).Parse(PluginPath, BdsPlatform, ConfigFile); | ||||
|   } | ||||
|  | ||||
|   const InstallPlugin = async (PluginName = "", Version = "latest") => { | ||||
|     const Config = await GetPlugin(PluginName); | ||||
|     const Plugin = Config.versions.filter(Version => Version.version === Version)[0]; | ||||
|     if (Plugin === undefined) throw new Error(`Plugin ${PluginName} version ${Version} not found`); | ||||
|     if (BdsPlatform === "pocketmine") { | ||||
|       if (Config.type === "plugin") { | ||||
|         const PluginPath = path.join(BdsSettings.GetServerPaths("pocketmine"), "plugins"); | ||||
|         fs.writeFileSync(path.join(PluginPath, `${PluginName}.phar`), await request.buffer(Plugin.url)); | ||||
|       } else if (Config.type === "resourcepack") { | ||||
|         const PluginPath = path.join(BdsSettings.GetServerPaths("pocketmine"), "resourcepacks"); | ||||
|         fs.writeFileSync(path.join(PluginPath, `${PluginName}.zip`), await request.buffer(Plugin.url)); | ||||
|       } else throw new Error(`Plugin ${PluginName} type (${Config.type}) not supported`); | ||||
|     } else if (BdsPlatform === "bedrock") { | ||||
|       if (Config.type === "texture") { | ||||
|         if (Plugin.type === "texture_addon") { | ||||
|           throw new Error("not implemented"); | ||||
|         } else if (Plugin.type === "addon") { | ||||
|           throw new Error("not implemented"); | ||||
|         } else if (Plugin.type === "texture") { | ||||
|           throw new Error("not implemented"); | ||||
|         } else throw new Error(`Plugin ${PluginName} type (${Config.type}) not supported`); | ||||
|       } | ||||
|     } | ||||
|   } | ||||
|  | ||||
|   const PluginList = async () => { | ||||
|     const PluginList = (await GetPluginsPath()).filter(Plugin => /config\.y[a]ml$/gi.test(Plugin.path)).map(Plugin => Plugin.path.replace(/\/config\.y[a]ml$/gi, "").replace(RegExp(`^${BdsPlatform}/`), "")); | ||||
|     const NewObject = {}; | ||||
|     for (const PluginPath of PluginList) { | ||||
|       const [Letter] = PluginPath.split("/"); | ||||
|       if (NewObject[Letter] === undefined) NewObject[Letter] = []; | ||||
|       NewObject[Letter].push(PluginPath.replace(`${Letter}/`, "")) | ||||
|     } | ||||
|     console.log(NewObject); | ||||
|     return NewObject; | ||||
|   } | ||||
|  | ||||
|   return { | ||||
|     GetPlugin, | ||||
|     InstallPlugin, | ||||
|     PluginList | ||||
|   }; | ||||
| } | ||||
|  | ||||
| module.exports = { | ||||
|   PluginManeger | ||||
| }; | ||||
							
								
								
									
										85
									
								
								src/PluginManeger/revision/v1/Config.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										85
									
								
								src/PluginManeger/revision/v1/Config.js
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,85 @@ | ||||
| const RawGithubUrl = "https://raw.githubusercontent.com/The-Bds-Maneger/Plugins_Repository/main"; | ||||
| /** | ||||
|  * Parse /Config.y[a]ml/ to return a object with url, type and versions | ||||
|  */ | ||||
| function Parse(RepositoryPath = "", BdsPlatform = "pocketmine", Config = {}) { | ||||
|   for (let KeyArray of Object.keys(Config)) { | ||||
|     if (!(KeyArray === "revision" || KeyArray === "name" || KeyArray === "type" || KeyArray === "versions")) console.error(`${KeyArray} is not supported`); | ||||
|   } | ||||
|   if (Config.type === undefined) throw new Error("Config Error: type not found"); | ||||
|   const NewConfig = { | ||||
|     revision: "v1", | ||||
|     type: String(Config.type).toLowerCase(), | ||||
|     name: String(Config.name||""), | ||||
|     versions: [ | ||||
|       { | ||||
|         dependencies: [""], | ||||
|         version: "", | ||||
|         minimum: "", | ||||
|         url: "", | ||||
|       }, | ||||
|       { | ||||
|         dependencies: [], | ||||
|         version: 0, | ||||
|         minimum: 0, | ||||
|         url: "", | ||||
|       }, | ||||
|     ] | ||||
|   }; NewConfig.versions = []; | ||||
|   if (BdsPlatform === "pocketmine") { | ||||
|     for (const Version of Config.versions) { | ||||
|       let AddObj = false; | ||||
|       const { version, from, minimum, dependencies } = Version; | ||||
|       if (version === undefined) throw new Error("Config Error: version not found"); | ||||
|       if (from === undefined) throw new Error("Config Error: from not found"); | ||||
|       const ObjVersion = { | ||||
|         dependencies: dependencies || [], | ||||
|         version: version, | ||||
|         minimum: 0, | ||||
|         url: "", | ||||
|       } | ||||
|        | ||||
|       // Server Minimum version (0 is any version) | ||||
|       if (typeof minimum === "string") ObjVersion.minimum = minimum; | ||||
|       else if (typeof minimum === "number") ObjVersion.minimum = minimum; | ||||
|       else ObjVersion.minimum = 0; | ||||
|  | ||||
|       if (version !== undefined) { | ||||
|         // Pocketmine from poggit | ||||
|         if (from === "poggit_pmmp") { | ||||
|           if (typeof Config.name === "undefined") throw new Error("Config Error: name not found"); | ||||
|           const { poggit_id } = Version; | ||||
|           if (typeof poggit_id === "undefined") ObjVersion.url = `https://poggit.pmmp.io/get/${Config.name.trim()}/${version}`; | ||||
|           else ObjVersion.url = `https://poggit.pmmp.io/r/${typeof poggit_id === "number" ? parseInt(poggit_id) : poggit_id.trim()}/${Config.name}.phar`; | ||||
|           AddObj = true; | ||||
|         } else if (from === "file") { | ||||
|           const { file } = Version; | ||||
|           if (typeof RepositoryPath === "undefined") throw new Error("Config Error: RepositoryPath not found"); | ||||
|           if (typeof file === "string") { | ||||
|             ObjVersion.url = `${RawGithubUrl}/${RepositoryPath}/${file.replace("./", "")}`; | ||||
|             AddObj = true; | ||||
|           } else throw new Error("Config Error: file not found"); | ||||
|         } else if (from === "github_release") { | ||||
|           const { repository, file_name } = Version; | ||||
|           if (typeof repository === "undefined") throw new Error("Config Error: repository not found"); | ||||
|           if (typeof file_name === "undefined") console.error("Config Error: file_name not defined, using default"); | ||||
|           ObjVersion.url = `https://github.com/releases/download/${repository}/${file_name || Config.name}.phar`; | ||||
|           AddObj = true; | ||||
|         } else if (from === "url") { | ||||
|           const { url } = Version; | ||||
|           if (typeof url === "undefined") throw new Error("Config Error: url not found"); | ||||
|           if (/^http[s]?:\/\//.test(url.trim())) { | ||||
|             ObjVersion.url = url.trim(); | ||||
|             AddObj = true; | ||||
|           } | ||||
|         } else console.error(`Config Error: from ${from} not supported`); | ||||
|       } | ||||
|       if (AddObj) NewConfig.versions.push(ObjVersion); | ||||
|     } | ||||
|   } | ||||
|   return NewConfig; | ||||
| } | ||||
|  | ||||
| module.exports = { | ||||
|   Parse | ||||
| }; | ||||
| @@ -2,6 +2,7 @@ const fs = require("fs"); | ||||
| const path = require("path"); | ||||
| const BdsSettings = require("./BdsSettings"); | ||||
|  | ||||
| const ModD = {}; | ||||
| function LoadPlugins() { | ||||
|   return fs.readdirSync(BdsSettings.ExternalPlugins).filter(file => fs.fstatSync(path.join(BdsSettings.ExternalPlugins, file)).isFile()).map(files => { | ||||
|     try { | ||||
| @@ -12,10 +13,11 @@ function LoadPlugins() { | ||||
|     } | ||||
|   }).filter(plugin => plugin); | ||||
| } | ||||
| ModD.LoadPlugins = LoadPlugins; | ||||
|  | ||||
| module.exports.plugin = LoadPlugins(); | ||||
| ModD.plugin = LoadPlugins(); | ||||
| fs.watch(BdsSettings.ExternalPlugins, () => { | ||||
|   module.exports.plugin = LoadPlugins(); | ||||
|   ModD.plugin = LoadPlugins(); | ||||
| }); | ||||
|  | ||||
| module.exports.LoadPlugins = LoadPlugins; | ||||
| module.exports = ModD; | ||||
| @@ -1,10 +1,12 @@ | ||||
| if (typeof global.fetch !== "function") { | ||||
|   global.fetch = (...args) => import("node-fetch").then(m => m.default(...args)); | ||||
|   import("node-fetch").then(m => global.fetch = m.default); | ||||
| if (typeof global !== "undefined") { | ||||
|   if (typeof global.fetch === "undefined") { | ||||
|     global.fetch = (...args) => import("node-fetch").then(Fetch => Fetch.default(...args)); | ||||
|     import("node-fetch").then(Fetch => global.fetch = Fetch.default); | ||||
|   } | ||||
| } | ||||
|  | ||||
| async function BufferHTTP(url = "", args = {}) { | ||||
|   const res = await fetch(url, { | ||||
|   const Fetch = (await import("node-fetch")).default; | ||||
|   const res = await Fetch(url, { | ||||
|     mode: "cors", | ||||
|     ...args | ||||
|   }); | ||||
| @@ -24,6 +26,33 @@ async function TextHTTP(url = "", args = {}) { | ||||
|   return (await BufferHTTP(url, args)).toString(); | ||||
| } | ||||
|  | ||||
| async function GithubTree(repo = "The-Bds-Maneger/Plugins_Repository", branch = "main") { | ||||
|   let res = { | ||||
|     "sha": "db0e9588de141e033b62bb581ac65b89f5c57f5b", | ||||
|     "url": "https://api.github.com/repos/The-Bds-Maneger/Plugins_Repository/git/trees/db0e9588de141e033b62bb581ac65b89f5c57f5b", | ||||
|     "tree": [ | ||||
|       { | ||||
|         "path": ".gitignore", | ||||
|         "mode": "100644", | ||||
|         "type": "blob", | ||||
|         "sha": "40b878db5b1c97fc77049537a71bb2e249abe5dc", | ||||
|         "size": 13, | ||||
|         "url": "https://api.github.com/repos/The-Bds-Maneger/Plugins_Repository/git/blobs/40b878db5b1c97fc77049537a71bb2e249abe5dc" | ||||
|       }, | ||||
|       { | ||||
|         "path": "repository", | ||||
|         "mode": "040000", | ||||
|         "type": "tree", | ||||
|         "sha": "1dd3854f47e8c38200788f718058fac981a27227", | ||||
|         "url": "https://api.github.com/repos/The-Bds-Maneger/Plugins_Repository/git/trees/1dd3854f47e8c38200788f718058fac981a27227" | ||||
|       }, | ||||
|     ], | ||||
|     "truncated": false | ||||
|   } | ||||
|   res = await JsonHTTP(`https://api.github.com/repos/${repo}/git/trees/${branch}?recursive=true`); | ||||
|   return res; | ||||
| } | ||||
|  | ||||
| // Export Bds Request | ||||
| module.exports = { | ||||
|     // JSON | ||||
| @@ -36,5 +65,8 @@ module.exports = { | ||||
|      | ||||
|     // Buffer | ||||
|     BUFFER: BufferHTTP, | ||||
|     buffer: BufferHTTP | ||||
|     buffer: BufferHTTP, | ||||
|  | ||||
|     // Others | ||||
|     GithubTree | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user