Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00012 #include "../stdafx.h"
00013
00014 #include "squirrel_helper.hpp"
00015
00016 #include "script_info.hpp"
00017 #include "script_scanner.hpp"
00018
00020 static const int MAX_GET_OPS = 1000;
00022 static const int MAX_CREATEINSTANCE_OPS = 100000;
00023
00024 ScriptFileInfo::~ScriptFileInfo()
00025 {
00026 free((void *)this->author);
00027 free((void *)this->name);
00028 free((void *)this->short_name);
00029 free((void *)this->description);
00030 free((void *)this->date);
00031 free((void *)this->instance_name);
00032 free((void *)this->url);
00033 free(this->main_script);
00034 free(this->SQ_instance);
00035 }
00036
00037 bool ScriptFileInfo::CheckMethod(const char *name) const
00038 {
00039 if (!this->engine->MethodExists(*this->SQ_instance, name)) {
00040 char error[1024];
00041 snprintf(error, sizeof(error), "your info.nut/library.nut doesn't have the method '%s'", name);
00042 this->engine->ThrowError(error);
00043 return false;
00044 }
00045 return true;
00046 }
00047
00048 SQInteger ScriptFileInfo::Constructor(HSQUIRRELVM vm, ScriptFileInfo *info)
00049 {
00050
00051 info->SQ_instance = MallocT<SQObject>(1);
00052 Squirrel::GetInstance(vm, info->SQ_instance, 2);
00053
00054 sq_addref(vm, info->SQ_instance);
00055 ScriptScanner *scanner = (ScriptScanner *)Squirrel::GetGlobalPointer(vm);
00056 info->engine = scanner->GetEngine();
00057
00058 static const char * const required_functions[] = {
00059 "GetAuthor",
00060 "GetName",
00061 "GetShortName",
00062 "GetDescription",
00063 "GetVersion",
00064 "GetDate",
00065 "CreateInstance",
00066 };
00067 for (size_t i = 0; i < lengthof(required_functions); i++) {
00068 if (!info->CheckMethod(required_functions[i])) return SQ_ERROR;
00069 }
00070
00071 info->main_script = strdup(scanner->GetMainScript());
00072
00073
00074 if (!info->engine->CallStringMethodStrdup(*info->SQ_instance, "GetAuthor", &info->author, MAX_GET_OPS)) return SQ_ERROR;
00075 if (!info->engine->CallStringMethodStrdup(*info->SQ_instance, "GetName", &info->name, MAX_GET_OPS)) return SQ_ERROR;
00076 if (!info->engine->CallStringMethodStrdup(*info->SQ_instance, "GetShortName", &info->short_name, MAX_GET_OPS)) return SQ_ERROR;
00077 if (!info->engine->CallStringMethodStrdup(*info->SQ_instance, "GetDescription", &info->description, MAX_GET_OPS)) return SQ_ERROR;
00078 if (!info->engine->CallStringMethodStrdup(*info->SQ_instance, "GetDate", &info->date, MAX_GET_OPS)) return SQ_ERROR;
00079 if (!info->engine->CallIntegerMethod(*info->SQ_instance, "GetVersion", &info->version, MAX_GET_OPS)) return SQ_ERROR;
00080 if (!info->engine->CallStringMethodStrdup(*info->SQ_instance, "CreateInstance", &info->instance_name, MAX_CREATEINSTANCE_OPS)) return SQ_ERROR;
00081
00082
00083 if (info->engine->MethodExists(*info->SQ_instance, "GetURL")) {
00084 if (!info->engine->CallStringMethodStrdup(*info->SQ_instance, "GetURL", &info->url, MAX_GET_OPS)) return SQ_ERROR;
00085 }
00086
00087 return 0;
00088 }