oopsk
oopsk 是一个 Skript 插件,旨在以非侵入式的方式为 Skript 添加有限的面向对象编程工具。
基础
当前的功能集围绕 structs(结构体)展开,这是一种将一组类型化字段组合在一起的简单对象。结构体通过结构体模板进行定义,这些顶层结构定义了它们的名称以及所包含的字段:
struct Message:
contents: string
sender: offline player
const timestamp: date = now
converts to:
string via this->contents
模板的名称和字段名不区分大小写。模板名的遵循与函数名相同的规则,而字段名只能由字母、下划线和空格组成。
每个字段都有一个名称和一个类型,以及一个可选的默认值。这个默认值可以是一个表达式,因为它是在创建结构体时求值的,而不是在注册模板时求值的。
在开头添加 const 或 constant 将防止字段在创建后被更改。
结构体还支持转换表达式,允许你定义如何将结构体转换为其他类型。语法为
<type> via <expression>,其中 <type> 是要转换到的类型,<expression> 是求值为该类型的表达式。
该表达式可以使用 this 来引用正在被转换的结构体。可以通过在 converts to: 部分下添加更多行来定义多个转换。
创建结构体涉及一个简单的表达式:
set {_a} to a message struct
{_a} 现在是 Message 模板的一个新实例,其 contents 和 sender 未设置,timestamp 设置为结构体创建的日期。可以使用字段访问表达式来访问和修改字段:
set field contents of {_a} to "hello world"
broadcast {_a}'s sender field
reset {_a}->timestamp
结构体也可以使用初始值创建:
set {_a} to a message struct:
sender: player
contents: "hello world"
自定义类型
结构体模板会自动使用模板名称 + struct 创建一个新的 Skript 类型。
在上述示例中,message struct 现在是一个有效的 Skript 类型,可以在函数参数和其他结构体字段中使用。
重新加载模板时要非常小心,因为如果模板被修改或删除,任何使用该类型的现有代码都可能会中断。
修改模板后,始终重新加载所有脚本以确保所有代码都被正确重新解析。
类型安全
oopsk 试图通过在解析时检查所有具有给定名称的字段来确保类型安全。这意味着在不同的结构体之间拥有唯一的字段名可以让 oopsk 提供更准确的解析错误,而共享字段名可能导致无效的代码在解析期间不会引发任何错误。
在解析过程中未被捕获的任何类型违规都会在运行时通过无法抑制的运行时错误来捕获。请注意,在一个脚本中解析的代码在另一个脚本中的结构体更新之前不会显示解析错误,直到它再次被重新加载,尽管它应该正确地发出运行时错误。
修改现有结构体的模板
对模板的任何修改都将反映在该模板之前创建的所有结构体中。这可能会导致数据丢失,如果重命名、删除或更改了字段的类型,因为现有结构体会将其修改后的字段要么移除,要么适当重新评估。
请注意,这意味着默认值需要重新评估,因此在创建结构体时不会被评估。如果由于模板更改导致任何现有结构体被修改,oopsk 将在控制台中打印警告。向模板添加字段或更改默认值不会修改现有结构体。
删除模板将使现有结构体“孤立”,它们将像模板仍然存在一样工作,尽管可能无法访问它们的信息,因为字段访问表达式可能无法识别字段名。如果添加了具有相同名称的新模板,这些现有结构体将被更新以匹配新模板。
如果你删除了一个模板,你应该注意移除依赖于它的结构体。
我建议将你的模板放在单独的脚本文件中,以便限制它们被意外修改或禁用的可能性。
路线图
Beta:
- 结构体和字段的反射表达式(获取字段、获取类型、获取字段是否为常量...)
- 动态从字符串访问字段的表达式(不安全)
- 结构体的序列化(不确定可行性)
可能性:
- 方法?
- 允许字段接受来自特定模板的结构体,而不是任何结构体。
文档
文档可在 skUnity 或 SkriptHub 上找到。
oopsk
oopsk is a Skript addon that aims to add limited object-oriented programming tools to Skript in a non-invasive manner.
Basics
The current feature set revolves around structs, simple objects that group together a set of typed fields. Structs are defined by struct templates, top-level structures that define their name and the fields they contain:
struct Message:
contents: string
sender: offline player
const timestamp: date = now
converts to:
string via this->contents
A template's name and field names are case-insensitive. The template name follows the same rules as a function name, while field names can only consist of letters, underscores, and spaces.
Each field has a name and a type, as well as an optional default value. This default value may be an expression, as it is evaluated when a struct is created, not when the template is registered.
Adding const or constant to the start will prevent the field from being changed after creation.
Structs also support conversion expressions, which allow you to define how a struct can be converted to other types. The syntax is
<type> via <expression>, where <type> is the type to convert to and <expression> is an expression that evaluates to that type.
The expression may use this to refer to the struct being converted. Multiple conversions can be defined by adding more lines under the converts to: section.
Creating a struct involves a simple expression:
set {_a} to a message struct
{_a} is now a new instance of the Message template, with contents and sender unset and timestamp set to the date the struct was created. Fields can be accessed and modified with the field access expression:
set field contents of {_a} to "hello world"
broadcast {_a}'s sender field
reset {_a}->timestamp
Structs can also be created with initial values:
set {_a} to a message struct:
sender: player
contents: "hello world"
Custom Types
Struct templates automatically create a new Skript type using the template's name + struct.
In the above example, message struct is now a valid Skript type that can be used in function parameters and other struct fields.
You should be very careful when reloading templates, as any existing code that used the type may break if the template was modified or removed.
ALWAYS reload all scripts after modifying templates to ensure all code is properly re-parsed.
Type Safety
oopsk attempts to ensure type safety at parse time via checking all fields with the given name. This means having unique field names across structs allows oopsk to give you more accurate parse errors, while sharing field names can result in invalid code not causing any errors during parsing.
Any type violations not caught during parsing should be caught at runtime via runtime errors that cannot be suppressed. Note that code parsed in one script prior to updates to a struct in another script will not show parse errors until it is reloaded again, though it should properly emit runtime errors.
Modifying the Template of Existing Structs
Any modifications to a template will be reflected in all structs that have been previously created from that template. This can result in data loss if a field is renamed, removed, or changes type, as existing structs will have their modified fields either removed or re-evaluated as appropriate.
Note that this means default values need to be re-evaluated and therefore will not have been evaluated when the struct was created. oopsk will print a warning in console if any existing structs were modified as a result of template changes. Adding fields to a template or changing default values will not modify existing structs.
Removing a template will 'orphan' existing structs, who will function as if the template still existed, though it may be impossible to access their information as the field access expression may not recognize the field names. If a new template is added with the same name, these existing structs will be updated to match the new template.
If you remove a template, you should take care to remove structs that depended on it.
I recommend putting your templates in a separate script file, so you can limit the chances of them being accidentally modified or disabled.
Roadmap
Beta:
- Reflective expressions for structs and field (get fields, get types, get whether a field is constant...)
- Expression to dynamically access a field from a string (unsafe)
- Serialization of structs (unsure on feasibility)
Possibilities:
- Methods?
- Allowing fields to accept structs from specific templates, rather than any struct.
Docs
Docs are available on skUnity or on SkriptHub.
评论 0
登录后参与评分
还没有评论,来抢沙发吧