|
| 1 | +# 插件基本接入方法 |
| 2 | +- version: 0.1 |
| 3 | + |
| 4 | +## 接入基本方法 |
| 5 | + |
| 6 | +1. 在 Plugin.cs 添加函数: |
| 7 | + |
| 8 | + ```csharp |
| 9 | + using ClassIsland.Core.Abstractions.Services; |
| 10 | + |
| 11 | + public static bool IsPluginInstalled(string pkgName) { |
| 12 | + return IPluginService.LoadedPlugins.Any(info => info.Manifest.Id == pkgName); |
| 13 | + } |
| 14 | + ``` |
| 15 | + |
| 16 | +2. Initialize 添加如下内容: |
| 17 | + |
| 18 | + ```csharp |
| 19 | + if (IsPluginInstalled("lrs2187.sai")) { |
| 20 | + AppBase.Current.AppStarted += (_, _) => { |
| 21 | + var saiServerService = IAppHost.GetService<ISaiServer>(); |
| 22 | + |
| 23 | + // 注册 sai 元素 |
| 24 | + saiServerService.RegisterBlocks("SuperAutoIsland", new RegisterData |
| 25 | + { |
| 26 | + Actions = [/* ... */], |
| 27 | + Rules = [/* ... */] |
| 28 | + }); |
| 29 | + }; |
| 30 | + } |
| 31 | + ``` |
| 32 | + |
| 33 | +3. 大功告成 |
| 34 | + |
| 35 | +## 积木示例 |
| 36 | + |
| 37 | +### 行动 |
| 38 | + |
| 39 | +```csharp |
| 40 | +var exampleActionBlock = new BlockMetadata |
| 41 | +{ |
| 42 | + Id = "sai.actions.example", |
| 43 | + Name = "示例积木", |
| 44 | + Icon = ("方块", "\uE326"), |
| 45 | + Args = new Dictionary<string, MetaArgsBase> |
| 46 | + { |
| 47 | + ["_string1"] = new CommonMetaArgs |
| 48 | + { |
| 49 | + Name = "示例字符串区域", |
| 50 | + Type = MetaType.dummy |
| 51 | + }, |
| 52 | + ["ExampleString"] = new CommonMetaArgs |
| 53 | + { |
| 54 | + Name = "示例字符串字段", |
| 55 | + Type = MetaType.text |
| 56 | + }, |
| 57 | + ["ExampleNumber"] = new CommonMetaArgs |
| 58 | + { |
| 59 | + Name = "示例数字字段", |
| 60 | + Type = MetaType.number |
| 61 | + }, |
| 62 | + ["ExampleBoolean"] = new CommonMetaArgs |
| 63 | + { |
| 64 | + Name = "示例布尔值字段", |
| 65 | + Type = MetaType.boolean |
| 66 | + }, |
| 67 | + ["ExampleCheckbox"] = new CheckboxMetaArgs |
| 68 | + { |
| 69 | + Name = "示例复选框字段", |
| 70 | + Type = MetaType.checkbox, |
| 71 | + DefaultValue = true |
| 72 | + }, |
| 73 | + ["ExampleDropdown"] = new DropDownMetaArgs |
| 74 | + { |
| 75 | + Name = "示例下拉框字段", |
| 76 | + Type = MetaType.dropdown, |
| 77 | + Options = [ |
| 78 | + ("字符串1", "值 val1"), |
| 79 | + ("字符串2", "val2"), |
| 80 | + ] |
| 81 | + } |
| 82 | + }, |
| 83 | + DropdownUseNumbers = false, // 下拉框传入参数是否为数字(其实是直接拼接到js...) |
| 84 | + InlineField = false, // 内行字段 |
| 85 | + InlineBlock = false, // 内行积木 |
| 86 | + IsRule = false |
| 87 | +}; |
| 88 | +``` |
| 89 | + |
| 90 | +### 规则集 |
| 91 | + |
| 92 | +仅需把 IsRule 改为 true 即可,其他与行动一致。 |
| 93 | + |
| 94 | +## 包装器注册 |
| 95 | + |
| 96 | +> 用于不同形状的积木转换为同一 ci 行动,或者改 Settings 之类的 |
| 97 | +
|
| 98 | +位于 `ISaiServe.RegisterWrapper`,下方为定义: |
| 99 | + |
| 100 | +```csharp |
| 101 | +using ClassIsland.Core.Models.Ruleset; |
| 102 | +using ClassIsland.Shared.Models.Automation; |
| 103 | + |
| 104 | +/// <summary> |
| 105 | +/// 行动 wrapper |
| 106 | +/// </summary> |
| 107 | +public delegate ActionItem ActionWrapper(ActionItem action); |
| 108 | + |
| 109 | +/// <summary> |
| 110 | +/// 规则 wrapper |
| 111 | +/// </summary> |
| 112 | +public delegate Rule RuleWrapper(Rule rule); |
| 113 | + |
| 114 | +/// <summary> |
| 115 | +/// 注册行动 wrapper |
| 116 | +/// </summary> |
| 117 | +/// <param name="id">行动 id</param> |
| 118 | +/// <param name="wrapper">wrapper 函数</param> |
| 119 | +public void RegisterWrapper(string id, ActionWrapper wrapper); |
| 120 | + |
| 121 | +/// <summary> |
| 122 | +/// 注册规则 wrapper |
| 123 | +/// </summary> |
| 124 | +/// <param name="id">规则 id</param> |
| 125 | +/// <param name="wrapper">wrapper 函数</param> |
| 126 | +public void RegisterWrapper(string id, RuleWrapper wrapper); |
| 127 | +``` |
| 128 | + |
| 129 | +下方为示例 |
| 130 | + |
| 131 | +```csharp |
| 132 | +/// <summary> |
| 133 | +/// "运行"行动设置。 |
| 134 | +/// </summary> |
| 135 | +public partial class RunActionSettings : ObservableRecipient { /* ... */ } |
| 136 | + |
| 137 | +/// <summary> |
| 138 | +/// 运行应用程序包装器 |
| 139 | +/// </summary> |
| 140 | +/// <param name="actionItem">行动项目</param> |
| 141 | +/// <returns>修改后的行动项目</returns> |
| 142 | +public static ActionItem RunActionProgramWrapper(ActionItem actionItem) |
| 143 | +{ |
| 144 | + var settingsJson = JsonSerializer.Serialize(actionItem.Settings); |
| 145 | + var settings = JsonSerializer.Deserialize<RunActionSettings>(settingsJson)!; |
| 146 | + settings.RunType = RunActionSettings.RunActionRunType.Application; |
| 147 | + settingsJson = JsonSerializer.Serialize(settings); |
| 148 | + |
| 149 | + return new ActionItem |
| 150 | + { |
| 151 | + Id = "classisland.os.run", |
| 152 | + Settings = JsonSerializer.Deserialize<object>(settingsJson) |
| 153 | + }; |
| 154 | +} |
| 155 | + |
| 156 | +saiServerService.RegisterWrapper("classisland.os.run.program", RunActionProgramWrapper); |
| 157 | +``` |
| 158 | + |
| 159 | +###### ~~垃圾文档集团 出品~~ |
0 commit comments