Skip to content

Commit 645ef68

Browse files
committed
✨ feat: enhance PluginManager interface with additional loading methods
1 parent 1e10d75 commit 645ef68

File tree

5 files changed

+59
-1
lines changed

5 files changed

+59
-1
lines changed

packages/chili-core/src/plugin/manager.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,27 @@
11
// Part of the Chili3d Project, under the AGPL-3.0 License.
22
// See LICENSE file in the project root for full license information.
33

4-
import type { Plugin } from "../plugin";
4+
import type { Plugin } from "./plugin";
55

66
export interface IPluginManager {
7+
/**
8+
* Load plugins from a file, the plugin file must be a zip file
9+
* @param pluginFile the plugin file, must be a zip file.
10+
* It must have a manifest.json file inside,
11+
* manifest.json must be a valid plugin manifest see PluginManifest for more details
12+
*/
713
loadFromFile(pluginFile: File): Promise<void>;
14+
/**
15+
* Load plugins from a url, the plugin file must be a zip file
16+
* @param pluginUrl the plugin url
17+
*/
18+
loadFromUrl(pluginUrl: string): Promise<void>;
19+
/**
20+
* Load plugins from a folder, the index file is optional, if not specified, the default plugins.json will be used
21+
* @param folderUrl the folder url
22+
* @param indexName the index file name, default is plugins.json
23+
*/
24+
loadFromFolder(folderUrl: string, indexName?: string): Promise<void>;
825
unload(pluginName: string): Promise<void>;
926
unloadAll(): void;
1027
getPlugins(): Plugin[];

packages/chili/src/application.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,11 @@ export class Application implements IApplication {
7979
this.services.forEach((x) => x.start());
8080

8181
this.initWindowEvents();
82+
this.loadPluginsFromPublic();
83+
}
84+
85+
private async loadPluginsFromPublic() {
86+
await this.pluginManager.loadFromFolder("./plugins/");
8287
}
8388

8489
private initWindowEvents() {

packages/chili/src/pluginManager.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,37 @@ export class PluginManager implements IPluginManager {
3535
}
3636
}
3737

38+
async loadFromUrl(url: string) {
39+
const response = await fetch(url);
40+
if (!response.ok) {
41+
alert(`Failed to fetch plugin from ${url}: ${response.statusText}`);
42+
return;
43+
}
44+
45+
const arrayBuffer = await response.arrayBuffer();
46+
const blob = new Blob([arrayBuffer], { type: "application/zip" });
47+
const file = new File([blob], "plugin.chiliplugin");
48+
49+
await this.loadFromFile(file);
50+
}
51+
52+
async loadFromFolder(folderUrl: string, indexName: string = "plugins.json") {
53+
try {
54+
const response = await fetch(folderUrl + indexName);
55+
if (!response.ok) {
56+
return;
57+
}
58+
const config = await response.json();
59+
const plugins = config.plugins as string[];
60+
const baseUrl = folderUrl.endsWith("/") ? folderUrl : folderUrl + "/";
61+
for (const plugin of plugins ?? []) {
62+
await this.loadFromUrl(baseUrl + plugin);
63+
}
64+
} catch {
65+
Logger.warn(`Failed to load plugins from folder: ${folderUrl}`);
66+
}
67+
}
68+
3869
private async readManifest(zip: JSZip) {
3970
const manifestFile = zip.file("manifest.json");
4071
if (!manifestFile) {
1.59 KB
Binary file not shown.

public/plugins/plugins.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"plugins": [
3+
"helloworld-ts.chiliplugin"
4+
]
5+
}

0 commit comments

Comments
 (0)