Kemena3D
Loading...
Searching...
No Matches
kscriptmanager.h
Go to the documentation of this file.
1
16
17#ifndef KSCRIPTMANAGER_H
18#define KSCRIPTMANAGER_H
19
20#include "kexport.h"
21
22#include <cassert>
23#include <vector>
24#include <map>
25#include <cstdio>
26#include <iostream>
27
28#include <angelscript.h>
29#include <scriptstdstring/scriptstdstring.h>
30#include <scriptbuilder/scriptbuilder.h>
31
32#include "kdatatype.h"
33
34#define FUNCTION asFUNCTION
35
36namespace kemena
37{
38 class kObject;
39
48
67
70
86
98
107 {
111 asIScriptModule *module = nullptr;
112 asIScriptFunction *fn[K_SCRIPT_EVENT_COUNT] = {};
113 kObject *owner = nullptr;
114 bool awakeCalled = false;
115 bool startCalled = false;
116 bool valid = false;
117 };
118
125 class KEMENA3D_API kFileByteStream : public asIBinaryStream
126 {
127 public:
133 kFileByteStream(const kString &path, bool writing);
134
137
139 bool isOpen() const { return file != nullptr; }
140
147 int Read(void *ptr, asUINT size) override;
148
155 int Write(const void *ptr, asUINT size) override;
156
157 private:
158 std::FILE *file = nullptr;
159 };
160
176 {
177 public:
180
183
185 asIScriptEngine *getEngine() { return engine; }
186
187 // --- Script-asset registry ------------------------------------------
188
196 void registerScriptAsset(const kString &uuid, const kString &sourcePath,
198 const kString &bytecodePath = "");
199
202
205
207 void setBytecodePath(const kString &uuid, const kString &bytecodePath);
208
209 // --- Compilation -----------------------------------------------------
210
218 bool compileToBytecode(const kString &scriptUuid, const kString &outPath,
219 bool stripDebug = false);
220
228 bool compileFileToBytecode(const kString &sourcePath, const kString &outPath,
229 bool stripDebug = false);
230
231 // --- Per-object instances -------------------------------------------
232
245 const kString &scriptUuid);
246
248 kScriptInstance *getInstance(const kString &componentUuid);
249
251 void destroyInstance(const kString &componentUuid);
252
255
256 // --- Execution -------------------------------------------------------
257
264 void setActiveObject(kObject *obj) { activeObject = obj; }
265
267 kObject *getActiveObject() const { return activeObject; }
268
270 void setDeltaTime(float dt) { deltaTime = dt; }
271
273 float getDeltaTime() const { return deltaTime; }
274
276 void setFixedDeltaTime(float dt) { fixedDeltaTime = dt; }
277
279 float getFixedDeltaTime() const { return fixedDeltaTime; }
280
288
289 // --- Host bindings ---------------------------------------------------
290
296 void registerGlobalFunction(const kString &declaration, asSFuncPtr func);
297
299 void destroy();
300
301 protected:
302 private:
304 bool buildInstanceModule(kScriptInstance *inst, const kScriptAsset &asset);
306 void cacheInstanceFunctions(kScriptInstance *inst);
307
308 asIScriptEngine *engine = nullptr;
309 asIScriptContext *context = nullptr;
310 kObject *activeObject = nullptr;
311 unsigned int moduleCounter = 0;
312 float deltaTime = 0.0f;
313 float fixedDeltaTime = 0.0f;
314
315 std::map<kString, kScriptAsset> assets;
316 std::map<kString, kScriptInstance*> instances;
317 };
318}
319
320#endif // KSCRIPTMANAGER_H
~kFileByteStream() override
Closes the underlying file if it is open.
int Read(void *ptr, asUINT size) override
Reads size bytes from the file into ptr.
int Write(const void *ptr, asUINT size) override
Writes size bytes from ptr to the file.
kFileByteStream(const kString &path, bool writing)
Opens path for bytecode reading or writing.
bool isOpen() const
Returns true if the underlying file opened successfully.
Definition kscriptmanager.h:139
Base scene-graph node.
Definition kobject.h:43
bool compileFileToBytecode(const kString &sourcePath, const kString &outPath, bool stripDebug=false)
Compiles a source file to bytecode without touching the registry.
asIScriptEngine * getEngine()
Returns the underlying AngelScript engine (for binding setup).
Definition kscriptmanager.h:185
void setBytecodePath(const kString &uuid, const kString &bytecodePath)
Records the bytecode path for an already-registered asset.
void destroyInstance(const kString &componentUuid)
Destroys one instance and discards its module.
float getDeltaTime() const
Returns the current frame delta time.
Definition kscriptmanager.h:273
virtual ~kScriptManager()
Destroys all instances and shuts down the engine (see destroy()).
bool callEvent(kScriptInstance *inst, kScriptEvent evt)
Invokes one lifecycle function on an instance, if it defines it.
bool compileToBytecode(const kString &scriptUuid, const kString &outPath, bool stripDebug=false)
Compiles a registered asset's source to AngelScript bytecode.
void destroyAllInstances()
Destroys every live instance (e.g. when Play stops).
kObject * getActiveObject() const
Returns the object set by setActiveObject().
Definition kscriptmanager.h:267
void unregisterScriptAsset(const kString &uuid)
Removes a script asset from the registry.
void setActiveObject(kObject *obj)
Sets the object scripts see as their owner during dispatch.
Definition kscriptmanager.h:264
kScriptAsset * getScriptAsset(const kString &uuid)
Returns the asset for uuid, or nullptr if unregistered.
void registerGlobalFunction(const kString &declaration, asSFuncPtr func)
Registers a native C function callable from every script.
kScriptInstance * createInstance(const kString &componentUuid, const kString &scriptUuid)
Builds a private module instance for one object's script slot.
void destroy()
Shuts down the engine and releases all resources.
void setFixedDeltaTime(float dt)
Sets the fixed (physics) delta time exposed to scripts.
Definition kscriptmanager.h:276
kScriptInstance * getInstance(const kString &componentUuid)
Returns the instance for componentUuid, or nullptr.
void setDeltaTime(float dt)
Sets the frame delta time exposed to scripts via host bindings.
Definition kscriptmanager.h:270
void registerScriptAsset(const kString &uuid, const kString &sourcePath, kScriptSourceType type=K_SCRIPT_TEXT, const kString &bytecodePath="")
Registers (or updates) a script asset.
float getFixedDeltaTime() const
Returns the current fixed delta time.
Definition kscriptmanager.h:279
kScriptManager()
Creates and initialises the AngelScript engine and shared context.
Core type aliases, enumerations, structs, and utility functions used throughout the engine.
Symbol visibility / linkage macro for the Kemena3D library.
#define KEMENA3D_API
Definition kexport.h:35
Top-level Kemena3D engine namespace.
Definition kanimation.h:23
kScriptEvent
Lifecycle events dispatched to every active script each frame.
Definition kscriptmanager.h:56
@ K_SCRIPT_AWAKE
void Awake() — once, before any Start().
Definition kscriptmanager.h:57
@ K_SCRIPT_ON_ENABLE
void OnEnable() — when the component activates.
Definition kscriptmanager.h:62
@ K_SCRIPT_EVENT_COUNT
Sentinel — number of lifecycle events.
Definition kscriptmanager.h:65
@ K_SCRIPT_ON_DISABLE
void OnDisable() — when the component deactivates.
Definition kscriptmanager.h:63
@ K_SCRIPT_FIXED_UPDATE
void FixedUpdate() — every fixed (physics) step.
Definition kscriptmanager.h:60
@ K_SCRIPT_ON_DESTROY
void OnDestroy() — when the instance is torn down.
Definition kscriptmanager.h:64
@ K_SCRIPT_START
void Start() — once, on the first frame.
Definition kscriptmanager.h:58
@ K_SCRIPT_LATE_UPDATE
void LateUpdate() — every frame, after Update().
Definition kscriptmanager.h:61
@ K_SCRIPT_UPDATE
void Update() — every frame.
Definition kscriptmanager.h:59
std::string kString
Standard string alias.
Definition kdatatype.h:42
kScriptSourceType
How a script asset's source is authored.
Definition kscriptmanager.h:44
@ K_SCRIPT_NODEGRAPH
Visual node graph; sourcePath points at its generated .as.
Definition kscriptmanager.h:46
@ K_SCRIPT_TEXT
Hand-written AngelScript text (.as).
Definition kscriptmanager.h:45
KEMENA3D_API const char * kScriptEventDecl(kScriptEvent evt)
AngelScript declaration string for each kScriptEvent (indexed by enum).
A registered script asset: a compilable unit of script source.
Definition kscriptmanager.h:91
kString bytecodePath
Path to compiled bytecode, or empty.
Definition kscriptmanager.h:94
kScriptSourceType sourceType
Text vs node-graph origin.
Definition kscriptmanager.h:95
kString sourcePath
Path to the .as source.
Definition kscriptmanager.h:93
kString uuid
Script asset UUID.
Definition kscriptmanager.h:92
kString checksum
Source checksum at last compile.
Definition kscriptmanager.h:96
Per-object script component descriptor.
Definition kscriptmanager.h:79
kString checksum
Source checksum recorded at last compile.
Definition kscriptmanager.h:83
kString scriptUuid
UUID of the referenced script asset.
Definition kscriptmanager.h:81
kString uuid
UUID of this attachment (unique per object+slot).
Definition kscriptmanager.h:80
kString fileName
Source file path (editor convenience / fallback).
Definition kscriptmanager.h:82
bool isActive
Whether this script runs each frame.
Definition kscriptmanager.h:84
A live, per-object compiled script module.
Definition kscriptmanager.h:107
bool startCalled
Start() has run.
Definition kscriptmanager.h:115
bool awakeCalled
Awake() has run.
Definition kscriptmanager.h:114
kString scriptUuid
Script asset this was built from.
Definition kscriptmanager.h:109
kObject * owner
Object this instance belongs to.
Definition kscriptmanager.h:113
kString componentUuid
Matches the owning kScript::uuid.
Definition kscriptmanager.h:108
bool valid
Module compiled/loaded successfully.
Definition kscriptmanager.h:116
asIScriptFunction * fn[K_SCRIPT_EVENT_COUNT]
Cached lifecycle handles.
Definition kscriptmanager.h:112
kString moduleName
Private AngelScript module name.
Definition kscriptmanager.h:110