Skip to content

Commit

Permalink
datastorer and some uncessary tools
Browse files Browse the repository at this point in the history
  • Loading branch information
Rundll86 committed Nov 16, 2024
1 parent 1f3d89a commit b26ca38
Show file tree
Hide file tree
Showing 13 changed files with 475 additions and 116 deletions.
132 changes: 83 additions & 49 deletions src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,78 +1,112 @@
import { ColorDefine } from "./fs-context/internal";
import { Block, Collaborator, Extension, Menu, Translator } from "./fs-context/structs";
let translator = new Translator();
//部署中文l10n表,其他语言同理
translator.store("zh-cn", {
import { Block, Collaborator, Extension, Menu, Translator, Version } from "./fs-context/structs";
import { GlobalContext, Unnecessary } from "./fs-context/tools";
//create方法创建一个Translator,不要直接new,参数为默认语言及其他所有语言下必须具有的键值对,之后可以调用store方法添加其他语言
//部署中文
let translator = Translator.create("zh-cn", {
name: "我的拓展",
des: "这是我的第一个拓展",
tanchuang: "弹窗",
towin: "到浏览器窗口",
suffix: "并使用后缀",
printRun: "打印runtime"
//积木文字,参数前带美元符号或下划线表示积木参数
alert: "向窗口中弹窗$sth,后缀为_suffix",
eat: "吃掉水果,$fruit",
eatenTip: "你吃了一个"
});
//英文
//部署英文
translator.store("en", {
name: "My Extension",
des: "This is my first extension",
tanchuang: "Alert",
towin: "to browser window",
suffix: "with suffix",
printRun: "Print Runtime"
alert: "alert $sth to window with suffix _suffix",
eat: "eat fruit,$fruit",
eatenTip: "You ate a(n)"
});
//只需要把拓展的类作为一个ES默认导出即可
//把拓展的类导出成default
export default class MyExtension extends Extension {
/*以下两个字段无需多言*/
id = "myextension";
displayName = translator.load("name");
//版本号,任意一种重载方式均可
version = new Version(1, 0, 0);
requires = {
//需求的拓展id及最低版本号,任意一种重载方式均可
/*witcat_filehelper: new Version("2.0.0")*/
}
//只有在非沙盒中运行,积木方法中才能使用this.runtime
allowSandboxed = false;
allowSandboxed = true;
blocks = [
Block.create(
//使用load方法加载当前语言的l10n
translator.load("tanchuang"),
//可以使用translator.load方法获取积木文字的翻译
translator.load("alert"),
{
//积木参数的name字段必须用$或_开头,否则ts将会编译不通过且方法中也不会有字段类型提示
name: "$content",
inputType: "string",
value: "something"
arguments: [
{
name: "$sth",
value: "Hello World",
inputType: "string"
},
{
name: "_suffix",
//菜单类型的积木参数有两种定义菜单的方式,一种就是下面这样,另一种是在拓展的menus字段中定义,这里填菜单的id
value: new Menu("suffixes", [
{ name: "已打印", value: "printed" },
{ name: "已输出", value: "output" },
{ name: "已显示", value: "displayed" }
]),
inputType: "menu"
}
],
},
translator.load("towin"),
translator.load("suffix"),
{
name: "$suffix",
inputType: "menu",
//inputType为menu时,value字段必须为一个已定义的Menu的名称
value: "suffix"
function alertSth(args) {
alert(args.$sth + " " + args._suffix);
dataStore.write("alertedSth", args.$sth.toString());
dataStore.write("lastSuffix", args._suffix.toString());
console.log(dataStore.read("tools"));
}
).config({
type: "command",
method(args) {
alert(args.$content + " " + args.$suffix);
},
//积木文字可能变动,手动设置opcode
opcode: "alertSth"
}),
),
Block.create(
translator.load("printRun"),
).config({
type: "command",
method() {
console.log("runtime", this.runtime);
translator.load("eat"),
{
arguments: [
{
name: "$fruit",
//第二种写法
value: "fruits",
inputType: "menu"
}
]
},
function eat(args) {
dataStore.write("fruitsEaten", args.$fruit.toString());
alert(`${translator.load("eatenTip")} ${args.$fruit}`);
}
//积木应该不会变动,不手动设置opcode,允许自动计算
})
)
];
menus = [
new Menu("suffix", [
{ name: "已打印", value: "printed" },
{ name: "已输出", value: "output" },
{ name: "已显示", value: "displayed" }
//这个字段中定义的菜单可以被积木参数所使用
new Menu("fruits", [
{ name: "苹果", value: "apple" },
{ name: "香蕉", value: "banana" },
{ name: "橙子", value: "orange" },
{ name: "西瓜", value: "watermelon" }
])
];
]
description = translator.load("des");
collaborators = [
//拓展的贡献者列表,可以不填
new Collaborator("FallingShrimp", "https://f-shrimp.solariix.com")
];
//三个颜色,可以直接填theme字段作为主题色,其他颜色能自动推断
colors: ColorDefine = {
theme: "#ff0000"
};
}
//是否自动推断颜色,如果为false,则colors字段中的颜色必须全部填写,block/inputer/menu
autoDeriveColors = true;
}
//在全局上下文中创建dataStore,不要直接new,dataStore可以跨拓展读取,不推荐对其他拓展的dataStore写入自己的数据
let dataStore = GlobalContext.createDataStore(MyExtension, {
alertedSth: [] as string[],
lastSuffix: "",
tools: Unnecessary,
fruitsEaten: [] as string[]
});
//若需要获取其他拓展的dataStore,使用getDataStore方法,即下面的注释部分
/*let dataFromOtherExtension = GlobalContext.getDataStore<dataStore_typeDefine>("ext_id");*/
3 changes: 2 additions & 1 deletion src/fs-context/declare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ declare interface Window {
__VUE_PROD_DEVTOOLS__: boolean;
__VUE_PROD_HYDRATION_MISMATCH_DETAILS__: boolean;
tempExt?: {
Extension: new () => any,
Extension: new (runtime: import("./internal").Scratch) => any,
info: {
extensionId: string,
name: string,
Expand All @@ -16,4 +16,5 @@ declare interface Window {
collaboratorList: import("./structs").Collaborator[]
}
}
_FSContext?: import("./internal").GlobalResourceMachine
}
6 changes: 4 additions & 2 deletions src/fs-context/entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ import { Extensions } from ".";
import Extension from "../extension";
import { ScratchWaterBoxed } from "./internal";
export default function load() {
//获取Scratch和加载拓展
let currentScratch = Extensions.getScratch() as ScratchWaterBoxed;
if (currentScratch) {
let extensionLoaded = Extensions.load(Extension);
extensionLoaded.debugPrint();
extensionLoaded.to("GandiIDE", "TurboWarp");
//若需要在控制台中打印调试信息,请取消注释下一行
/*extensionLoaded.debugPrint();*/
extensionLoaded.to("GandiIDE");
Extensions.isInWaterBoxed() ? currentScratch.loadTempExt() : null;
}
}
Expand Down
50 changes: 41 additions & 9 deletions src/fs-context/index.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,34 @@
import { Scratch, ScratchWaterBoxed } from "./internal";
import { Extension } from "./structs";
import { GlobalResourceMachine, Scratch, ScratchWaterBoxed } from "./internal";
import { Extension, Menu, Version } from "./structs";
export namespace Extensions {
const inputTypeCastToScratch: any = {
bool: "Boolean",
"hat-paramater": "ccw_hat_parameter"
}
function generateConstructor(extension: new () => Extension): any {
var constructor = new extension();
function ExtensionConstructor(this: any, runtime: Scratch) {
if (!runtime.extensions.unsandboxed && !constructor.allowSandboxed) {
var context = getFSContext();
function ExtensionConstructor(this: any, runtime: any) {
if (!runtime.extensions?.unsandboxed && !constructor.allowSandboxed) {
throw new Error(`FSExtension "${constructor.id}" must be supported unsandboxed.`)
}
if (!constructor.allowSandboxed) constructor.runtime = runtime;
for (let i in constructor.requires) {
if (!Object.keys(context.EXTENSIONS).includes(i)) {
throw new Error(`FSExtension "${constructor.id}" requires ${i} to be loaded.`)
}
if (Version.compare(context.EXTENSIONS[i], constructor.requires[i]) === constructor.requires[i]) {
throw new Error(`FSExtension "${constructor.id}" requires ${i} to be at least ${constructor.requires[i]}.`)
};
}
constructor.blocks.forEach(block => {
block.arguments.forEach((arg) => {
if (arg.inputType === "menu" && arg.value instanceof Menu) {
constructor.menus.push(arg.value);
arg.value = arg.value.name;
}
});
})
constructor.runtime = runtime;
let blocks: any[] = [];
for (let block of constructor.blocks) {
let args: any = {};
Expand All @@ -34,9 +51,7 @@ export namespace Extensions {
args[arg.content] = currentArg;
}
}
this[currentBlock.opcode] = (arg: any) => {
block.method.call(constructor, arg);
};
this[currentBlock.opcode] = (arg: any) => JSON.stringify(block.method.call(constructor, arg));
blocks.push(currentBlock);
}
let menus: any = {};
Expand Down Expand Up @@ -74,6 +89,15 @@ export namespace Extensions {
if (window.Scratch) return window.Scratch;
return null;
}
export function getFSContext(): GlobalResourceMachine {
if (!window._FSContext) {
window._FSContext = {
EXTENSIONS: {},
EXPORTED: {}
};
}
return window._FSContext;
}
export function load(extension: new () => Extension) {
let constructorPlain = extension
let constructorGenerated = generateConstructor(extension);
Expand All @@ -87,9 +111,13 @@ export namespace Extensions {
console.log(`Trying to load FSExtension "${objectPlain.id}" on platform "${platform}"...`);
if (platform === "TurboWarp") {
getScratch()?.extensions.register(objectGenerated);
if (isInWaterBoxed()) {
(getScratch() as ScratchWaterBoxed).currentExtension = objectGenerated;
}
getFSContext().EXTENSIONS[objectPlain.id] = objectPlain.version;
} else if (platform === "GandiIDE") {
window.tempExt = {
Extension: extension,
Extension: constructorGenerated,
info: {
extensionId: objectPlain.id,
name: objectPlain.displayName,
Expand All @@ -99,6 +127,10 @@ export namespace Extensions {
collaboratorList: objectPlain.collaborators
}
};
if (isInWaterBoxed()) {
(getScratch() as ScratchWaterBoxed).currentExtensionPlain = objectPlain;
}
getFSContext().EXTENSIONS[objectPlain.id] = objectPlain.version;
} else {
throw new Error(`Unknown platform "${platform}"`);
};
Expand Down
Loading

0 comments on commit b26ca38

Please sign in to comment.