8#include <simplesquirrel/simplesquirrel.hpp>
29 if (tableIdx < 0) tableIdx = sq_gettop(
vm) + tableIdx + 1;
30 sq_pushstring(
vm,
key, -1);
31 if (SQ_SUCCEEDED(sq_rawget(
vm, tableIdx))) {
38ssq::Table eveTableFromStack(
HSQUIRRELVM vm, SQInteger tableIdx) {
39 ssq::Object wrapped(
vm);
40 sq_getstackobj(
vm, tableIdx, &wrapped.getRaw());
41 sq_addref(
vm, &wrapped.getRaw());
42 return ssq::Table(wrapped);
45bool isPascalCaseName(
const char*
key) {
46 return key &&
key[0] >=
'A' &&
key[0] <=
'Z';
49bool isScriptEcsName(
const char*
key) {
50 if (!
key)
return false;
51 return std::strcmp(
key,
"Component") == 0 || std::strcmp(
key,
"Entity") == 0 ||
52 std::strcmp(
key,
"EntityContainer") == 0 || std::strcmp(
key,
"System") == 0 ||
53 std::strcmp(
key,
"ShaderSystem") == 0 || std::strcmp(
key,
"view") == 0 ||
54 std::strcmp(
key,
"ecsReady") == 0;
57void ensureScriptEcsInTable(ssq::Table& table) {
59 sq_pushobject(
vm,
table.getRaw());
60 const bool hasSystem = eveRawHas(
vm, -1,
"System");
61 const bool ready = eveRawHas(
vm, -1,
"Component") && eveRawHas(
vm, -1,
"Entity") && hasSystem;
62 if (!ready && hasSystem) {
66 sq_pushstring(
vm,
"System", -1);
67 sq_deleteslot(
vm, -2, SQFalse);
73template <
typename Info>
74void runExposer(
Info&
info, ssq::Table& table,
const char*
name) {
80 const auto t0 = std::chrono::steady_clock::now();
83 std::chrono::duration<double, std::milli>(std::chrono::steady_clock::now() - t0).count();
85 std::fprintf(stderr,
"[startup] bind %s: %.1f ms\n",
name ?
name :
"?", ms);
89void bindNamedModule(
const std::string&
name, ssq::Table& table) {
91 auto it = modules.find(
name);
92 if (it == modules.end())
return;
93 runExposer(it->second, table,
name.c_str());
96void bindUntilKeyExists(
HSQUIRRELVM vm, SQInteger tableIdx,
const char*
key, ssq::Table& table) {
97 if (eveRawHas(
vm, tableIdx,
key))
return;
99 std::vector<std::string>
pending;
100 pending.reserve(modules.size());
101 for (
auto& entry : modules) {
102 if (!entry.second.exposed && entry.second.exposer)
pending.push_back(entry.first);
106 auto it = modules.find(
name);
107 if (it == modules.end() || it->second.exposed)
continue;
108 runExposer(it->second, table,
name.c_str());
109 if (eveRawHas(
vm, tableIdx,
key))
return;
121 if (!eveRawHas(
vm, tableIdx,
key)) ensureScriptEcsInTable(table);
125 if (!
key || eveRawHas(
vm, tableIdx,
key))
return;
126 ssq::Table
table = eveTableFromStack(
vm, tableIdx);
127 if (isScriptEcsName(
key)) {
128 ensureScriptEcsInTable(table);
131 bindNamedModule(
key, table);
132 if (eveRawHas(
vm, tableIdx,
key))
return;
136 if (isPascalCaseName(
key)) bindUntilKeyExists(
vm, tableIdx,
key, table);
140 if (sq_gettop(
vm) < 2 || sq_gettype(
vm, 2) != OT_STRING)
141 return sq_throwerror(
vm, _SC(
"the index doesn't exist"));
142 const SQChar*
key =
nullptr;
143 if (SQ_FAILED(sq_getstring(
vm, 2, &
key)) || !
key)
144 return sq_throwerror(
vm, _SC(
"the index doesn't exist"));
154 bindOnDemand(
vm, 1,
name.c_str());
155 }
catch (
const std::exception&
error) {
156 return sq_throwerror(
vm,
error.what());
159 sq_pushstring(
vm,
name.c_str(),
static_cast<SQInteger
>(
name.size()));
160 if (SQ_FAILED(sq_rawget(
vm, -2))) {
161 const std::string
message =
"the index '" +
name +
"' does not exist";
162 return sq_throwerror(
vm,
message.c_str());
167void installLazyClassGet(ssq::Table& eveTable) {
169 sq_pushobject(
vm, eveTable.getRaw());
171 sq_pushstring(
vm, _SC(
"_get"), -1);
172 sq_newclosure(
vm, &eveTableGet, 0);
173 sq_newslot(
vm, -3, SQFalse);
174 sq_setdelegate(
vm, -2);
182 inst().state_->active_vm_ = &
vm;
183 for (
auto& entry :
inst().state_->registered_modules) entry.second.exposed =
false;
190 table =
vm.addTable(
"eve");
199 installLazyClassGet(table);
200 table.addFunc(
"_bindAllNativeClasses", []() {
return ModuleManager::exposeAllForCompatibility(); });
201 table.addFunc(
"_ensureScriptECS", []() {
return ModuleManager::ensureScriptEcs(); });
210 inst().state_->active_runtime_ =
nullptr;
223 auto stack =
active->guard();
224 ssq::Table table =
active->table(
"eve");
225 for (
auto& D :
inst().state_->registered_modules) {
226 if (D.second.exposed || !D.second.exposer)
228 D.second.exposer(table);
229 D.second.exposed =
true;
239int ModuleManager::ensureScriptEcs() {
244 auto stack =
active->guard();
245 ssq::Table table =
active->table(
"eve");
246 ensureScriptEcsInTable(table);
253int ModuleManager::exposeAllForCompatibility() {
256 if (ensureScriptEcs() < 0)
return -1;
EVEngine assertion entry point, backed by zeroerr.
std::vector< QuestEvent > pending
struct SQVM * HSQUIRRELVM
The single Squirrel projection for common Result, Status and Value.
static void exposeVM(ssq::VM &vm)
Expose vm.
static Runtime * runtime()
Active runtime associated with the last expose() call, or nullptr. @ownership Borrowed; the embedding...
static void expose(Runtime &runtime)
Installs the eve table and defers native class method bindings.
static int expose_pending()
Expose pending.
static ModuleManager & inst()
Singleton registry holding every registered engine module.
static ssq::VM * vm()
Script VM of the last expose call, including raw-VM embedders, or nullptr. @ownership Borrowed; Runti...
EVENGINE_API_FOUNDATION public API.
ssq::VM & vm() noexcept
Underlying SimpleSquirrel VM (requires a live, initialized runtime).
std::unordered_map< std::string, SkillDefinition > & table()
void exposeProviderBindings(ssq::Table &eveTable)
Expose eve.provider: script-published providers and capability seams.
void exposeResultBindings(ssq::Table &eveTable)
Expose the common script helpers under eve.result.
Build metadata (engine git commit, build time, third-party version).
void exposeECS(ssq::Table &table)
脚本侧 ECS:Entity / Component / System / ShaderSystem / view(见 ECS.cpp)。 C++ 游戏实体仍直接用 ECS....
void prepareEcsScriptInjection()
Resets per-VM script injection bookkeeping before a new VM is exposed.
void flushPostEcsHooks(ssq::Table &table)
Run post-ECS hooks that were registered after exposeECS already ran.