--- a/src/ai/ai_squirrel.cpp Fri Jun 13 19:57:25 2008 +0000
+++ b/src/ai/ai_squirrel.cpp Fri Jun 13 20:19:00 2008 +0000
@@ -17,6 +17,7 @@
#include "../squirrel.hpp"
#include "ai_info.hpp"
#include "ai_squirrel.hpp"
+#include "api/ai_controller.hpp"
void AISquirrel::ScanDir(const char *dirname, bool library_dir, char *library_depth)
{
@@ -132,7 +133,6 @@
AISquirrel::AISquirrel()
{
- this->library_instance_count = 0;
this->engine = new Squirrel();
/* Create the AIInfo class, and add the RegisterAI function */
@@ -162,7 +162,7 @@
delete this->engine;
}
-bool AISquirrel::ImportLibrary(const char *library, const char *class_name, int version, HSQUIRRELVM vm)
+bool AISquirrel::ImportLibrary(const char *library, const char *class_name, int version, HSQUIRRELVM vm, AIController *controller)
{
AILibraryList::iterator iter = this->library_list.find(library);
/* Check if the library exists */
@@ -181,28 +181,35 @@
return false;
}
- /* Create a fake internal name */
- char fake_class[1024];
- snprintf(fake_class, sizeof(fake_class), "_internalNA%d", ++this->library_instance_count);
-
/* Get the current table/class we belong to */
HSQOBJECT parent;
sq_getstackobj(vm, 1, &parent);
- /* Load the library in a 'fake' namespace, so we can link it to the name the user requested */
- sq_pushroottable(vm);
- sq_pushstring(vm, OTTD2FS(fake_class), -1);
- sq_newclass(vm, SQFalse);
- /* Load the library */
- if (!Squirrel::LoadScript(vm, (*iter).second->GetScriptName(), false)) {
- char error[1024];
- snprintf(error, sizeof(error), "there was a compile error when importing '%s'", library);
- sq_throwerror(vm, OTTD2FS(error));
- return false;
+ char fake_class[1024];
+ int next_number;
+ /* Check if we already have this library loaded.. if so, fill fake_class
+ * with the class-name it is nested in */
+ if (!controller->LoadedLibrary(library, &next_number, &fake_class[0], sizeof(fake_class))) {
+ /* Create a new fake internal name */
+ snprintf(fake_class, sizeof(fake_class), "_internalNA%d", next_number);
+
+ /* Load the library in a 'fake' namespace, so we can link it to the name the user requested */
+ sq_pushroottable(vm);
+ sq_pushstring(vm, OTTD2FS(fake_class), -1);
+ sq_newclass(vm, SQFalse);
+ /* Load the library */
+ if (!Squirrel::LoadScript(vm, (*iter).second->GetScriptName(), false)) {
+ char error[1024];
+ snprintf(error, sizeof(error), "there was a compile error when importing '%s'", library);
+ sq_throwerror(vm, OTTD2FS(error));
+ return false;
+ }
+ /* Create the fake class */
+ sq_newslot(vm, -3, SQFalse);
+ sq_pop(vm, 1);
+
+ controller->AddLoadedLibrary(library, fake_class);
}
- /* Create the fake class */
- sq_newslot(vm, -3, SQFalse);
- sq_pop(vm, 1);
/* Find the real class inside the fake class (like 'sets.Vector') */
sq_pushroottable(vm);