Skip to content

Entry script SPI methods ​

KODE-CFG-012 · KB-001 engine mechanics. The engine reaches the entry module only through getEntryScript(); this page is the complete method table for that interface. Sources: kbe/src at /home/cui/workspaces/kbengine (commit c2a3b8f), official templates under kbe/res/sdk_templates/server/python_assets/scripts/. Companion pages: Fixed directories and path resolution, Startup flow and stack.

The entry module is entryScriptFile, a module name whose default is kbemain (see fixed directories). Everything the engine calls on it goes through EntityApp::getEntryScript() (kbe/src/lib/server/entity_app.h:89), PythonApp::getEntryScript() (kbe/src/lib/server/python_app.h:51) or ClientApp::getEntryScript() (kbe/src/lib/client_lib/clientapp.h:83).

How to read the tables ​

Three call forms exist, and they differ in what happens when the method is missing:

FormMissing method behaviourWhere
PyObject_CallMethod(...) directlyreturns NULL, AttributeError printed by SCRIPT_ERROR_CHECK()e.g. kbe/src/server/loginapp/loginapp.cpp:174
SCRIPT_OBJECT_CALL_ARGSx(...) macromacro runs PyObject_HasAttrString first; skipped silently unless GETERROR is trueguard at kbe/src/lib/pyscript/py_macros.h:665 (ARGS0), :699 (ARGS1), :736 (ARGS2), :774 (ARGS3); AttributeError formatting at :681
explicit PyObject_HasAttrString guard, then callnever called when absent — safe to omite.g. kbe/src/server/baseapp/entity_autoloader.cpp:90 then :92

A full scan of getEntryScript() across kbe/src yields 56 call sites covering 36 distinct method names: 32 are invoked unconditionally (the call site carries no guard of its own) and 4 sit behind an explicit PyObject_HasAttrString.

The 4 optional methods ​

MethodGuard lineCall lineFormatComponents
onAutoLoadEntityCreatekbe/src/server/baseapp/entity_autoloader.cpp:90:92sKbaseapp
onLogWrotekbe/src/server/tools/logger/logger.cpp:287:290y#logger
onReadyForLoginkbe/src/server/baseapp/initprogress_handler.cpp:201, kbe/src/server/cellapp/initprogress_handler.cpp:153:206, :156O (baseapp), i (cellapp)baseapp, cellapp
onReadyForShutDownkbe/src/server/baseapp/baseapp.cpp:275, kbe/src/server/dbmgr/dbmgr.cpp:78, kbe/src/server/tools/logger/logger.cpp:176:278, :81, :179""baseapp, dbmgr, logger

A subtlety for the remaining 32: 14 of them go through the SCRIPT_OBJECT_CALL_ARGSx macro, so they are also skipped when the attribute is absent (py_macros.h:665, :699, :736, :774) — the difference is only whether an AttributeError is printed. The other 18 are called directly and raise a Python AttributeError on every hit if you delete them from kbemain.py.

Per-component tables ​

Repeated methods (shared by several components) appear in each relevant table.

baseapp — 12 methods ​

MethodRequiredFormatCall siteTrigger
onInityesi (0 at startup, 1 on reload)kbe/src/server/baseapp/baseapp.cpp:3601Baseapp::onDbmgrInitCompleted (baseapp.cpp:3581), first callback after scripts load
onBaseAppReadyyesOkbe/src/server/baseapp/initprogress_handler.cpp:185InitProgressHandler::process (initprogress_handler.cpp:132)
onReadyForLoginoptionalOinitprogress_handler.cpp:201 guard, :206 callpolled each tick until the return value is ≥ 0.9999 (initprogress_handler.cpp:244)
onGlobalDatayes (macro)OOkbe/src/lib/server/entity_app.h:1308EntityApp::onBroadcastGlobalDataChanged (entity_app.h:1261)
onGlobalDataDelyes (macro)Oentity_app.h:1290same broadcast path
onBaseAppDatayes (macro)OOkbe/src/server/baseapp/baseapp.cpp:3668Baseapp::onBroadcastBaseAppDataChanged (baseapp.cpp:3618)
onBaseAppDataDelyes (macro)Obaseapp.cpp:3649same
onCellAppDeathyesObaseapp.cpp:696Baseapp::onCellAppDeath (baseapp.cpp:673)
onLoseChargeCByesOOOObaseapp.cpp:3563Baseapp::onChargeCB (baseapp.cpp:3510)
onAutoLoadEntityCreateoptionalsKentity_autoloader.cpp:90 guard, :92 callEntityAutoLoader::onEntityAutoLoadCBFromDBMgr (entity_autoloader.cpp:52)
onReadyForShutDownoptional""baseapp.cpp:275 guard, :278 callBaseapp::canShutdown (baseapp.cpp:258)
onBaseAppShutDownyes (macro)i = 0 / 1 / 2baseapp.cpp:327, :342, :389onShutdownBegin (:321) → onShutdown(first) (:334) → onShutdownEnd (:383)

cellapp — 9 methods ​

MethodRequiredFormatCall siteTrigger
onInityesi (0)kbe/src/server/cellapp/initprogress_handler.cpp:137InitProgressHandler::process (initprogress_handler.cpp:102) — cellapp uses onInit(0) as its ready callback
onGlobalDatayes (macro)OOkbe/src/lib/server/entity_app.h:1308shared with baseapp
onGlobalDataDelyes (macro)Oentity_app.h:1290shared with baseapp
onCellAppDatayes (macro)OOkbe/src/server/cellapp/cellapp.cpp:864Cellapp::onBroadcastCellAppDataChanged (cellapp.cpp:816)
onCellAppDataDelyes (macro)Ocellapp.cpp:844same
onSpaceDatayes (macro)Iss (set) / IsO (del)kbe/src/server/cellapp/spacememory.cpp:609, :615SpaceMemory::onSpaceDataChanged (spacememory.cpp:603)
onSpaceGeometryLoadedyes (macro)Isspacememory.cpp:308SpaceMemory::onLoadedSpaceGeometryMapping (spacememory.cpp:299) — not implemented in any template
onAllSpaceGeometryLoadedyes (macro)Iisspacememory.cpp:336SpaceMemory::onAllSpaceGeometryLoaded (spacememory.cpp:331)
onReadyForLoginoptionalicellapp/initprogress_handler.cpp:153 guard, :156 callsame polling loop as baseapp

loginapp — 7 methods ​

MethodRequiredFormatCall siteTrigger
onLoginAppReadyyes""kbe/src/server/loginapp/loginapp.cpp:174Loginapp::initializeEnd (loginapp.cpp:163)
onRequestLoginyesssby#loginapp.cpp:1013Loginapp::login (loginapp.cpp:877)
onLoseLoginyessloginapp.cpp:130Loginapp::onChannelDeregister (loginapp.cpp:102)
onRequestCreateAccountyesssy#loginapp.cpp:368Loginapp::_createAccount (loginapp.cpp:285)
onCreateAccountCallbackFromDByessHy#loginapp.cpp:590Loginapp::onReqCreateAccountResult (loginapp.cpp:578)
onLoginCallbackFromDByesssHy#loginapp.cpp:1231Loginapp::onLoginAccountQueryResultFromDbmgr (loginapp.cpp:1166)
onLoginAppShutDownyes (macro)no argsloginapp.cpp:62Loginapp::onShutdownBegin (loginapp.cpp:56)

dbmgr — 4 methods ​

MethodRequiredFormatCall siteTrigger
onDBMgrReadyyes""kbe/src/server/dbmgr/dbmgr.cpp:309Dbmgr::initializeEnd (dbmgr.cpp:283)
onSelectAccountDBInterfaceyessdbmgr.cpp:1242Dbmgr::selectAccountDBInterfaceName (dbmgr.cpp:1236)
onReadyForShutDownoptional""dbmgr.cpp:78 guard, :81 callDbmgr::canShutdown (dbmgr.cpp:76)
onDBMgrShutDownyes (macro)no argsdbmgr.cpp:156Dbmgr::onShutdownBegin (dbmgr.cpp:150)

interfaces — 5 methods ​

MethodRequiredFormatCall siteTrigger
onInterfaceAppReadyyes""kbe/src/server/tools/interfaces/interfaces.cpp:153Interfaces::initializeEnd (interfaces.cpp:137)
onRequestCreateAccountyesssy#interfaces.cpp:525Interfaces::reqCreateAccount (interfaces.cpp:488)
onRequestAccountLoginyesssy#interfaces.cpp:637Interfaces::onAccountLogin (interfaces.cpp:605)
onRequestChargeyessKy#interfaces.cpp:752Interfaces::charge (interfaces.cpp:717)
onInterfaceAppShutDownyes (macro)no argsinterfaces.cpp:79Interfaces::onShutdownBegin (interfaces.cpp:73)

logger — 4 methods ​

MethodRequiredFormatCall siteTrigger
onLoggerAppReadyyes""kbe/src/server/tools/logger/logger.cpp:131Logger::initializeEnd (logger.cpp:116)
onReadyForShutDownoptional""logger.cpp:176 guard, :179 callLogger::canShutdown (logger.cpp:174)
onLoggerAppShutDownyes (macro)no argslogger.cpp:218Logger::onShutdownBegin (logger.cpp:210)
onLogWroteoptionaly#logger.cpp:287 guard, :290 callLogger::writeLog (logger.cpp:229); the guard is a static bool, evaluated once per process (logger.cpp:287)

client / bots — 3 methods ​

MethodRequiredFormatCall siteTrigger
onInityesi (0)kbe/src/lib/client_lib/clientapp.cpp:131, kbe/src/server/tools/bots/bots.cpp:102ClientApp::initializeEnd (clientapp.cpp:128), Bots::initializeEnd (bots.cpp:88)
onFinishyes""clientapp.cpp:303, bots.cpp:124Bots::finalise (bots.cpp:121) — client/bots only, not a server callback
onTargetChangedyesiclientapp.cpp:458client entity target change — client only

36 engine calls vs 36 template functions ​

The seven official templates define 44 function definitions / 36 distinct names (base 12, login 8, cell 7, interface 6, logger 4, db 4, bots 3 — names shared between components are counted once each in the distinct total). The engine calls 36 distinct names. The two sets overlap on 33 names:

CategoryCountNamesEvidence
Both engine and template33the intersection—
Engine calls, no template implements it3onAutoLoadEntityCreate, onSpaceGeometryLoaded, onTargetChangedguards/paths: entity_autoloader.cpp:90, py_macros.h:736, clientapp.cpp:458
Template defines it, engine never calls it3onFini, onStart, onTicksee below

34 vs 36: both numbers are real, they measure different scopes ​

An earlier ledger note recorded this table as 34 methods. A programmatic re-scan of kbe/src (at c2a3b8f, re-verified unchanged at 3082ab5) confirms 36 distinct names invoked through getEntryScript() — nothing was over-counted here. The two numbers measure different scopes:

  • 36 = every method the engine invokes on the entry-script object, across all components including the client/bots pair onFinish (clientapp.cpp:303, bots.cpp:124) and onTargetChanged (clientapp.cpp:458) that no server-side kbemain.py ever receives.
  • 34 = the server-component scope: 36 minus that client/bots-only pair. This is the number that matters when authoring a server entry script, and it is the scope downstream consumers (e.g. SPI CodeLens recognition) should match against.

Both counts exclude onFini, onStart and onTick — those three are not engine calls at all, as established below.

The three template-only names, corrected ​

  • onFini — template vs implementation mismatch, 待证伪. No call site exists anywhere in kbe/src: grepping "onFini" and onFini\b returns nothing, and the adjacent-string form ("on" "Fini") does not appear either. The template still labels it a KBEngine method (kbe/res/sdk_templates/server/python_assets/scripts/base/kbemain.py:56, INFO_MSG('onFini()') at :61). Actual shutdown goes through onXxxShutDown(state) instead — the three-stage sequence is in startup flow and stack.
  • onTick — not an engine callback. No "onTick" string exists in kbe/src. It is the template's own timer: the onInit body carries the (commented-out) line #KBEngine.addTimer(0.01, 1.0, onTick) at kbe/res/sdk_templates/server/python_assets/scripts/db/kbemain.py:17 with def onTick(timerID) at :19, and the same pair at interface/kbemain.py:44 and :47. When enabled it fires from the script timer machinery, never from getEntryScript().
  • onStart — no call site, 待证伪. kbe/src has no script-callback invocation of it (the only onStart hits are unrelated C++ such as KBEConcurrency::onStartMainThreadIdling). Template: kbe/res/sdk_templates/server/python_assets/scripts/bots/kbemain.py:14.
  • onFinish is component-specific, not general: the only call sites are kbe/src/lib/client_lib/clientapp.cpp:303 and kbe/src/server/tools/bots/bots.cpp:124.

Lifecycle diagram ​

KBEngine entry script lifecycle

Seven phases (P0 environment paths → P7 shutdown), runtime split into per-component lanes, shutdown drawn as the three-stage state = 0 → 1 → 2 sequence. Required callbacks are blue, HasAttr-guarded ones amber, the three non-engine names dashed. The same picture with per-phase stack frames is on Startup flow and stack.

Where to attach debugpy ​

Best injection point: P1, the module import — not onInit.

  • onInit fires after the engine has already commented 「所有脚本都加载完毕」 (kbe/src/lib/server/entity_app.h:1459, kbe/src/lib/server/python_app.cpp:708), which is after installPyScript / installPyModules have run (entity_app.h:281, entity_app.h:284, python_app.cpp:74, python_app.cpp:77). Any import-time error in an earlier module is already past you by then.
  • The wrapper module's top-level code runs inside PyImport_Import(entryScriptFile) (kbe/src/lib/server/entity_app.h:509, python_app.cpp:340, bots.cpp:218, clientapp.cpp:272), i.e. before entity defs, before every callback, and while process/port identity is already known (g_kbeSrvConfig.updateInfos(...) at kbe/src/lib/server/kbemain.h:190, components registered at :198). Putting debugpy.listen() / debugpy.wait_for_client() at the top of kbemain.py stops the process at the earliest point where Python runs at all.
  • Fallback: onInit(isReload) if you only need business-layer breakpoints — it is the first engine callback (baseapp.cpp:3601, cellapp/initprogress_handler.cpp:137).

Decorators work. The engine resolves the attribute at call time through PyObject_CallMethod, so the attribute may be a decorated function or an instance with __call__. Two constraints:

  1. The format string must match what the engine passes — onInit uses "i" (baseapp.cpp:3603), onSpaceGeometryLoaded uses "Is" (spacememory.cpp:309), onLoginCallbackFromDB uses "ssHy#" (loginapp.cpp:1233), and onReadyForLogin is "O" on baseapp but "i" on cellapp (initprogress_handler.cpp:208 vs :158).
  2. The wrapper must forward *args, **kwargs unchanged and return what the engine expects — onReadyForLogin must return Py_True for the login gate to open (initprogress_handler.cpp:213) — letting exceptions propagate rather than swallowing them.

Next: Fixed directories and path resolution · Startup flow and stack · Entity lifecycle & reference release

Kode documentation