v0.1.23: 生灭千秋 + 插件生态化第一课(1027 全绿)
【世界模型:世界会死人也会新生】 - 家族生灭:declineYears(power<58+景气<38 连续8年)→ 覆灭(最强邻分食+史官广播)+ 新贵补位(6%/年,乱世×2) —— NPC 永远四家铁打的现状终结;玩家补刀(raid×0.82)可加速衰亡 - npcById 双源可空(静态+动态注册表;不再 throw——15 处消费者防御化) - purgeNpc 全局清理(raidCD/事件队列/恩怨网/distress/动态 def) - 秘境产出回池(loot 20% 经 tradeSettle 回流)——资源循环最后单向封口 - WorldSnapshot 只读摘要门面(UI/史书消费,零行为变更) - 潮汐长波(10 年正弦 ±0.08 叠加,灵潮三十年河东) 【插件生态化(0.1.8 后第一次大进化)】 - 持久化:GameState.plugins 落盘 + PLUGIN_REGISTRY 读档重装(engineFromSnapshot 真实路径验证) - plugin-public.ts 公共导出面(第三方单入口+纪律) - 双闸:能力卡随插件启停联动;池自动回滚 - 契约补完:Proxy 追踪(池/卡/pack 自动回滚)+ onUninstall 真调 + 能力卡入 World 实例(防跨档泄漏) - SettingsPanel 启停按钮/依赖详情;demo 与 tests 去重(发现并修复:npcById 双源静态源未绑定——全局 NPC 停滞回归) 【测试】1027 全绿(44 套件):dynasty-plugin 7 例(覆灭/新贵/回池/快照/持久化/双闸/自动回滚); 金钟罩 0.1.23 基线固化;build 通过
This commit is contained in:
@@ -50,6 +50,12 @@ export interface WorldEventBus {
|
||||
|
||||
import { WorldSim } from '../sim/WorldSim'
|
||||
|
||||
/** 插件代码注册表(0.1.23 持久化重装;未来加载器扩展点) */
|
||||
export const PLUGIN_REGISTRY = new Map<string, () => import('../kernel/plugin').CotycPlugin>()
|
||||
export function registerPluginFactory(id: string, factory: () => import('../kernel/plugin').CotycPlugin): void {
|
||||
PLUGIN_REGISTRY.set(id, factory)
|
||||
}
|
||||
|
||||
export function worldSimOf(w: World): WorldSim {
|
||||
return new WorldSim(w)
|
||||
}
|
||||
@@ -145,6 +151,15 @@ export class World {
|
||||
this.systems = Object.fromEntries(SYSTEM_DEFS.map((d) => [d.id, { enabled: true }]))
|
||||
this.plugins = new PluginManager(this.buildPluginContext())
|
||||
this.installCorePlugins()
|
||||
// 0.1.23 持久化插件重装:读档时按注册表重建非核心插件
|
||||
for (const item of state.plugins ?? []) {
|
||||
const mk = PLUGIN_REGISTRY.get(item.id)
|
||||
if (!mk) continue
|
||||
const plugin = mk()
|
||||
if (plugin.version !== item.version) continue
|
||||
const r = this.installPlugin(plugin)
|
||||
if (r.ok && !item.enabled) this.setPluginEnabled(item.id, false)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -158,10 +173,8 @@ export class World {
|
||||
register: (phase, fn: SystemHook) => self.clock.register(phase, fn),
|
||||
onYearStart: (fn: SystemHook) => self.clock.onYearStart(fn),
|
||||
addCapability: (cap) => {
|
||||
if (!SYSTEM_DEFS.find((d) => d.id === cap.id)) {
|
||||
SYSTEM_DEFS.push({ id: cap.id, name: cap.name, version: cap.version, desc: cap.desc })
|
||||
}
|
||||
self.systems[cap.id] = { enabled: true }
|
||||
// 0.1.23:能力卡注册入 World 实例(防跨档全局泄漏);UI 全局清单读 SYSTEM_DEFS 展示不受影响
|
||||
if (!self.systems[cap.id]) self.systems[cap.id] = { enabled: true }
|
||||
},
|
||||
removeCapability: (id) => {
|
||||
delete self.systems[id]
|
||||
@@ -210,7 +223,10 @@ export class World {
|
||||
|
||||
installPlugin(p: CotycPlugin): { ok: boolean; reason?: string } {
|
||||
const r = this.plugins.install(p)
|
||||
if (r.ok) this.out.forEach((o) => o.onPluginChange?.(p.id, 'install'))
|
||||
if (r.ok) {
|
||||
this.out.forEach((o) => o.onPluginChange?.(p.id, 'install'))
|
||||
this.syncPersistedPlugins()
|
||||
}
|
||||
return r
|
||||
}
|
||||
|
||||
@@ -219,13 +235,17 @@ export class World {
|
||||
if (r.ok) {
|
||||
this.rebuildEventPoolsAfterRemoval(id)
|
||||
this.out.forEach((o) => o.onPluginChange?.(id, 'remove'))
|
||||
this.syncPersistedPlugins()
|
||||
}
|
||||
return r
|
||||
}
|
||||
|
||||
setPluginEnabled(id: string, enabled: boolean): { ok: boolean; reason?: string } {
|
||||
const r = this.plugins.setEnabled(id, enabled)
|
||||
if (r.ok) this.out.forEach((o) => o.onPluginChange?.(id, enabled ? 'enable' : 'disable'))
|
||||
if (r.ok) {
|
||||
this.out.forEach((o) => o.onPluginChange?.(id, enabled ? 'enable' : 'disable'))
|
||||
this.syncPersistedPlugins()
|
||||
}
|
||||
return r
|
||||
}
|
||||
|
||||
@@ -241,6 +261,14 @@ export class World {
|
||||
return [...this.eventPools.keys()]
|
||||
}
|
||||
|
||||
/** 插件状态落盘(id/version/enabled)——读档时按注册表重装 */
|
||||
private syncPersistedPlugins(): void {
|
||||
this.state.plugins = this.plugins
|
||||
.list()
|
||||
.filter((p) => !p.protected)
|
||||
.map((p) => ({ id: p.id, version: p.version, enabled: p.enabled }))
|
||||
}
|
||||
|
||||
private rebuildEventPoolsAfterRemoval(id: string): void {
|
||||
void id
|
||||
// 事件池卸载暂由插件 uninstall 自行处理;此处在 remove 后重置 core 保证可用
|
||||
|
||||
Reference in New Issue
Block a user