summaryrefslogtreecommitdiff
path: root/src/script
diff options
context:
space:
mode:
authortruebrain <truebrain@openttd.org>2011-11-29 22:24:30 +0000
committertruebrain <truebrain@openttd.org>2011-11-29 22:24:30 +0000
commit00877dd6d36ca52b1a8720cbbd4a013c958f7fad (patch)
treeac1c05499e12228624c695b93368246217425469 /src/script
parent96249564fd219f8747c9cd54bc934215fb1b5e44 (diff)
downloadopenttd-00877dd6d36ca52b1a8720cbbd4a013c958f7fad.tar.xz
(svn r23352) -Add: support dynamically adding an API prefix when returning a C++ class to Squirrel
Diffstat (limited to 'src/script')
-rw-r--r--src/script/script_scanner.cpp2
-rw-r--r--src/script/squirrel.cpp30
-rw-r--r--src/script/squirrel.hpp13
3 files changed, 32 insertions, 13 deletions
diff --git a/src/script/script_scanner.cpp b/src/script/script_scanner.cpp
index 258a05643..049790196 100644
--- a/src/script/script_scanner.cpp
+++ b/src/script/script_scanner.cpp
@@ -52,7 +52,7 @@ bool ScriptScanner::AddFile(const char *filename, size_t basepath_length, const
ScriptScanner::ScriptScanner()
{
- this->engine = new Squirrel();
+ this->engine = new Squirrel("Scanner");
/* Mark this class as global pointer */
this->engine->SetGlobalPointer(this);
diff --git a/src/script/squirrel.cpp b/src/script/squirrel.cpp
index a3697dcb4..837e86987 100644
--- a/src/script/squirrel.cpp
+++ b/src/script/squirrel.cpp
@@ -273,22 +273,33 @@ bool Squirrel::CallBoolMethod(HSQOBJECT instance, const char *method_name, bool
return true;
}
-/* static */ bool Squirrel::CreateClassInstanceVM(HSQUIRRELVM vm, const char *class_name, void *real_instance, HSQOBJECT *instance, SQRELEASEHOOK release_hook)
+/* static */ bool Squirrel::CreateClassInstanceVM(HSQUIRRELVM vm, const char *class_name, void *real_instance, HSQOBJECT *instance, SQRELEASEHOOK release_hook, bool prepend_API_name)
{
+ Squirrel *engine = (Squirrel *)sq_getforeignptr(vm);
+
int oldtop = sq_gettop(vm);
/* First, find the class */
sq_pushroottable(vm);
- sq_pushstring(vm, OTTD2SQ(class_name), -1);
+
+ if (prepend_API_name) {
+ char *class_name2 = (char *)alloca(strlen(class_name) + strlen(engine->GetAPIName()) + 1);
+ sprintf(class_name2, "%s%s", engine->GetAPIName(), class_name);
+
+ sq_pushstring(vm, OTTD2SQ(class_name2), -1);
+ } else {
+ sq_pushstring(vm, OTTD2SQ(class_name), -1);
+ }
+
if (SQ_FAILED(sq_get(vm, -2))) {
- DEBUG(misc, 0, "[squirrel] Failed to find class by the name '%s'", class_name);
+ DEBUG(misc, 0, "[squirrel] Failed to find class by the name '%s%s'", prepend_API_name ? engine->GetAPIName() : "", class_name);
sq_settop(vm, oldtop);
return false;
}
/* Create the instance */
if (SQ_FAILED(sq_createinstance(vm, -1))) {
- DEBUG(misc, 0, "[squirrel] Failed to create instance for class '%s'", class_name);
+ DEBUG(misc, 0, "[squirrel] Failed to create instance for class '%s%s'", prepend_API_name ? engine->GetAPIName() : "", class_name);
sq_settop(vm, oldtop);
return false;
}
@@ -316,13 +327,14 @@ bool Squirrel::CreateClassInstance(const char *class_name, void *real_instance,
return Squirrel::CreateClassInstanceVM(this->vm, class_name, real_instance, instance, NULL);
}
-Squirrel::Squirrel()
+Squirrel::Squirrel(const char *APIName) :
+ global_pointer(NULL),
+ print_func(NULL),
+ crashed(false),
+ overdrawn_ops(0),
+ APIName(APIName)
{
this->vm = sq_open(1024);
- this->print_func = NULL;
- this->global_pointer = NULL;
- this->crashed = false;
- this->overdrawn_ops = 0;
/* Handle compile-errors ourself, so we can display it nicely */
sq_setcompilererrorhandler(this->vm, &Squirrel::CompileError);
diff --git a/src/script/squirrel.hpp b/src/script/squirrel.hpp
index 44ab21650..e4aaf6ae3 100644
--- a/src/script/squirrel.hpp
+++ b/src/script/squirrel.hpp
@@ -23,11 +23,12 @@ class Squirrel {
private:
typedef void (SQPrintFunc)(bool error_msg, const SQChar *message);
- HSQUIRRELVM vm; ///< The VirtualMachine instnace for squirrel
+ HSQUIRRELVM vm; ///< The VirtualMachine instance for squirrel
void *global_pointer; ///< Can be set by who ever initializes Squirrel
SQPrintFunc *print_func; ///< Points to either NULL, or a custom print handler
bool crashed; ///< True if the squirrel script made an error.
int overdrawn_ops; ///< The amount of operations we have overdrawn.
+ const char *APIName; ///< Name of the API used for this squirrel.
/**
* The internal RunError handler. It looks up the real error and calls RunError with it.
@@ -39,6 +40,11 @@ private:
*/
HSQUIRRELVM GetVM() { return this->vm; }
+ /**
+ * Get the API name.
+ */
+ const char *GetAPIName() { return this->APIName; }
+
protected:
/**
* The CompileError handler.
@@ -66,7 +72,7 @@ public:
friend class AIController;
friend void squirrel_register_std(Squirrel *engine);
- Squirrel();
+ Squirrel(const char *APIName);
~Squirrel();
/**
@@ -165,9 +171,10 @@ public:
* @param real_instance The instance to the real class, if it represents a real class.
* @param instance Returning value with the pointer to the instance.
* @param release_hook Optional param to give a release hook.
+ * @param prepend_API_name Optional parameter; if true, the class_name is prefixed with the current API name.
* @return False if creating failed.
*/
- static bool CreateClassInstanceVM(HSQUIRRELVM vm, const char *class_name, void *real_instance, HSQOBJECT *instance, SQRELEASEHOOK release_hook);
+ static bool CreateClassInstanceVM(HSQUIRRELVM vm, const char *class_name, void *real_instance, HSQOBJECT *instance, SQRELEASEHOOK release_hook, bool prepend_API_name = false);
/**
* Exactly the same as CreateClassInstanceVM, only callable without instance of Squirrel.