00001
00002
00003
00004
00005
00006
00007
00008
00009
00012 #include "../stdafx.h"
00013 #include "../settings_type.h"
00014
00015 #include "squirrel_helper.hpp"
00016
00017 #include "script_info.hpp"
00018 #include "script_scanner.hpp"
00019
00020 ScriptInfo::~ScriptInfo()
00021 {
00022
00023 for (ScriptConfigItemList::iterator it = this->config_list.begin(); it != this->config_list.end(); it++) {
00024 free((*it).name);
00025 free((*it).description);
00026 if (it->labels != NULL) {
00027 for (LabelMapping::iterator it2 = (*it).labels->Begin(); it2 != (*it).labels->End(); it2++) {
00028 free(it2->second);
00029 }
00030 delete it->labels;
00031 }
00032 }
00033 this->config_list.clear();
00034
00035 free(this->author);
00036 free(this->name);
00037 free(this->short_name);
00038 free(this->description);
00039 free(this->date);
00040 free(this->instance_name);
00041 free(this->url);
00042 free(this->main_script);
00043 free(this->tar_file);
00044 free(this->SQ_instance);
00045 }
00046
00047 bool ScriptInfo::CheckMethod(const char *name) const
00048 {
00049 if (!this->engine->MethodExists(*this->SQ_instance, name)) {
00050 char error[1024];
00051 snprintf(error, sizeof(error), "your info.nut/library.nut doesn't have the method '%s'", name);
00052 this->engine->ThrowError(error);
00053 return false;
00054 }
00055 return true;
00056 }
00057
00058 SQInteger ScriptInfo::Constructor(HSQUIRRELVM vm, ScriptInfo *info)
00059 {
00060
00061 info->SQ_instance = MallocT<SQObject>(1);
00062 Squirrel::GetInstance(vm, info->SQ_instance, 2);
00063
00064 sq_addref(vm, info->SQ_instance);
00065
00066 info->scanner = (ScriptScanner *)Squirrel::GetGlobalPointer(vm);
00067 info->engine = info->scanner->GetEngine();
00068
00069
00070 static const char * const required_functions[] = {
00071 "GetAuthor",
00072 "GetName",
00073 "GetShortName",
00074 "GetDescription",
00075 "GetVersion",
00076 "GetDate",
00077 "CreateInstance",
00078 };
00079 for (size_t i = 0; i < lengthof(required_functions); i++) {
00080 if (!info->CheckMethod(required_functions[i])) return SQ_ERROR;
00081 }
00082
00083
00084 info->main_script = strdup(info->scanner->GetMainScript());
00085 const char *tar_name = info->scanner->GetTarFile();
00086 if (tar_name != NULL) info->tar_file = strdup(tar_name);
00087
00088
00089 if (!info->engine->CallStringMethodStrdup(*info->SQ_instance, "GetAuthor", &info->author, MAX_GET_OPS)) return SQ_ERROR;
00090 if (!info->engine->CallStringMethodStrdup(*info->SQ_instance, "GetName", &info->name, MAX_GET_OPS)) return SQ_ERROR;
00091 if (!info->engine->CallStringMethodStrdup(*info->SQ_instance, "GetShortName", &info->short_name, MAX_GET_OPS)) return SQ_ERROR;
00092 if (!info->engine->CallStringMethodStrdup(*info->SQ_instance, "GetDescription", &info->description, MAX_GET_OPS)) return SQ_ERROR;
00093 if (!info->engine->CallStringMethodStrdup(*info->SQ_instance, "GetDate", &info->date, MAX_GET_OPS)) return SQ_ERROR;
00094 if (!info->engine->CallIntegerMethod(*info->SQ_instance, "GetVersion", &info->version, MAX_GET_OPS)) return SQ_ERROR;
00095 if (!info->engine->CallStringMethodStrdup(*info->SQ_instance, "CreateInstance", &info->instance_name, MAX_CREATEINSTANCE_OPS)) return SQ_ERROR;
00096
00097
00098 if (info->engine->MethodExists(*info->SQ_instance, "GetURL")) {
00099 if (!info->engine->CallStringMethodStrdup(*info->SQ_instance, "GetURL", &info->url, MAX_GET_OPS)) return SQ_ERROR;
00100 }
00101
00102
00103 if (info->engine->MethodExists(*info->SQ_instance, "GetSettings")) {
00104 if (!info->GetSettings()) return SQ_ERROR;
00105 }
00106
00107 return 0;
00108 }
00109
00110 bool ScriptInfo::GetSettings()
00111 {
00112 return this->engine->CallMethod(*this->SQ_instance, "GetSettings", NULL, MAX_GET_SETTING_OPS);
00113 }
00114
00115 SQInteger ScriptInfo::AddSetting(HSQUIRRELVM vm)
00116 {
00117 ScriptConfigItem config;
00118 memset(&config, 0, sizeof(config));
00119 config.max_value = 1;
00120 config.step_size = 1;
00121 uint items = 0;
00122
00123
00124 sq_pushnull(vm);
00125 while (SQ_SUCCEEDED(sq_next(vm, -2))) {
00126 const SQChar *sqkey;
00127 if (SQ_FAILED(sq_getstring(vm, -2, &sqkey))) return SQ_ERROR;
00128 const char *key = SQ2OTTD(sqkey);
00129 ValidateString(key);
00130
00131 if (strcmp(key, "name") == 0) {
00132 const SQChar *sqvalue;
00133 if (SQ_FAILED(sq_getstring(vm, -1, &sqvalue))) return SQ_ERROR;
00134 char *name = strdup(SQ2OTTD(sqvalue));
00135 char *s;
00136 ValidateString(name);
00137
00138
00139
00140 while ((s = strchr(name, '=')) != NULL) *s = '_';
00141 while ((s = strchr(name, ',')) != NULL) *s = '_';
00142 config.name = name;
00143 items |= 0x001;
00144 } else if (strcmp(key, "description") == 0) {
00145 const SQChar *sqdescription;
00146 if (SQ_FAILED(sq_getstring(vm, -1, &sqdescription))) return SQ_ERROR;
00147 config.description = strdup(SQ2OTTD(sqdescription));
00148 ValidateString(config.description);
00149 items |= 0x002;
00150 } else if (strcmp(key, "min_value") == 0) {
00151 SQInteger res;
00152 if (SQ_FAILED(sq_getinteger(vm, -1, &res))) return SQ_ERROR;
00153 config.min_value = res;
00154 items |= 0x004;
00155 } else if (strcmp(key, "max_value") == 0) {
00156 SQInteger res;
00157 if (SQ_FAILED(sq_getinteger(vm, -1, &res))) return SQ_ERROR;
00158 config.max_value = res;
00159 items |= 0x008;
00160 } else if (strcmp(key, "easy_value") == 0) {
00161 SQInteger res;
00162 if (SQ_FAILED(sq_getinteger(vm, -1, &res))) return SQ_ERROR;
00163 config.easy_value = res;
00164 items |= 0x010;
00165 } else if (strcmp(key, "medium_value") == 0) {
00166 SQInteger res;
00167 if (SQ_FAILED(sq_getinteger(vm, -1, &res))) return SQ_ERROR;
00168 config.medium_value = res;
00169 items |= 0x020;
00170 } else if (strcmp(key, "hard_value") == 0) {
00171 SQInteger res;
00172 if (SQ_FAILED(sq_getinteger(vm, -1, &res))) return SQ_ERROR;
00173 config.hard_value = res;
00174 items |= 0x040;
00175 } else if (strcmp(key, "random_deviation") == 0) {
00176 SQInteger res;
00177 if (SQ_FAILED(sq_getinteger(vm, -1, &res))) return SQ_ERROR;
00178 config.random_deviation = res;
00179 items |= 0x200;
00180 } else if (strcmp(key, "custom_value") == 0) {
00181 SQInteger res;
00182 if (SQ_FAILED(sq_getinteger(vm, -1, &res))) return SQ_ERROR;
00183 config.custom_value = res;
00184 items |= 0x080;
00185 } else if (strcmp(key, "step_size") == 0) {
00186 SQInteger res;
00187 if (SQ_FAILED(sq_getinteger(vm, -1, &res))) return SQ_ERROR;
00188 config.step_size = res;
00189 } else if (strcmp(key, "flags") == 0) {
00190 SQInteger res;
00191 if (SQ_FAILED(sq_getinteger(vm, -1, &res))) return SQ_ERROR;
00192 config.flags = (ScriptConfigFlags)res;
00193 items |= 0x100;
00194 } else {
00195 char error[1024];
00196 snprintf(error, sizeof(error), "unknown setting property '%s'", key);
00197 this->engine->ThrowError(error);
00198 return SQ_ERROR;
00199 }
00200
00201 sq_pop(vm, 2);
00202 }
00203 sq_pop(vm, 1);
00204
00205
00206
00207 if ((items & 0x200) != 0 && (config.flags & SCRIPTCONFIG_RANDOM) != 0) {
00208 char error[1024];
00209 snprintf(error, sizeof(error), "Setting both random_deviation and SCRIPTCONFIG_RANDOM is not allowed");
00210 this->engine->ThrowError(error);
00211 return SQ_ERROR;
00212 }
00213
00214 items &= ~0x200;
00215
00216
00217 uint mask = (config.flags & SCRIPTCONFIG_BOOLEAN) ? 0x1F3 : 0x1FF;
00218 if (items != mask) {
00219 char error[1024];
00220 snprintf(error, sizeof(error), "please define all properties of a setting (min/max not allowed for booleans)");
00221 this->engine->ThrowError(error);
00222 return SQ_ERROR;
00223 }
00224
00225 this->config_list.push_back(config);
00226 return 0;
00227 }
00228
00229 SQInteger ScriptInfo::AddLabels(HSQUIRRELVM vm)
00230 {
00231 const SQChar *sq_setting_name;
00232 if (SQ_FAILED(sq_getstring(vm, -2, &sq_setting_name))) return SQ_ERROR;
00233 const char *setting_name = SQ2OTTD(sq_setting_name);
00234 ValidateString(setting_name);
00235
00236 ScriptConfigItem *config = NULL;
00237 for (ScriptConfigItemList::iterator it = this->config_list.begin(); it != this->config_list.end(); it++) {
00238 if (strcmp((*it).name, setting_name) == 0) config = &(*it);
00239 }
00240
00241 if (config == NULL) {
00242 char error[1024];
00243 snprintf(error, sizeof(error), "Trying to add labels for non-defined setting '%s'", setting_name);
00244 this->engine->ThrowError(error);
00245 return SQ_ERROR;
00246 }
00247 if (config->labels != NULL) return SQ_ERROR;
00248
00249 config->labels = new LabelMapping;
00250
00251
00252 sq_pushnull(vm);
00253 while (SQ_SUCCEEDED(sq_next(vm, -2))) {
00254 const SQChar *sq_key;
00255 const SQChar *sq_label;
00256 if (SQ_FAILED(sq_getstring(vm, -2, &sq_key))) return SQ_ERROR;
00257 if (SQ_FAILED(sq_getstring(vm, -1, &sq_label))) return SQ_ERROR;
00258
00259
00260 const char *key_string = SQ2OTTD(sq_key);
00261 int key = atoi(key_string + 1);
00262 const char *label = SQ2OTTD(sq_label);
00263 ValidateString(label);
00264
00265
00266 if (!config->labels->Contains(key)) config->labels->Insert(key, strdup(label));
00267
00268 sq_pop(vm, 2);
00269 }
00270 sq_pop(vm, 1);
00271
00272
00273 config->complete_labels = true;
00274 for (int value = config->min_value; value <= config->max_value; value++) {
00275 if (!config->labels->Contains(value)) {
00276 config->complete_labels = false;
00277 break;
00278 }
00279 }
00280
00281 return 0;
00282 }
00283
00284 const ScriptConfigItemList *ScriptInfo::GetConfigList() const
00285 {
00286 return &this->config_list;
00287 }
00288
00289 const ScriptConfigItem *ScriptInfo::GetConfigItem(const char *name) const
00290 {
00291 for (ScriptConfigItemList::const_iterator it = this->config_list.begin(); it != this->config_list.end(); it++) {
00292 if (strcmp((*it).name, name) == 0) return &(*it);
00293 }
00294 return NULL;
00295 }
00296
00297 int ScriptInfo::GetSettingDefaultValue(const char *name) const
00298 {
00299 for (ScriptConfigItemList::const_iterator it = this->config_list.begin(); it != this->config_list.end(); it++) {
00300 if (strcmp((*it).name, name) != 0) continue;
00301
00302 switch (GetGameSettings().script.settings_profile) {
00303 case SP_EASY: return (*it).easy_value;
00304 case SP_MEDIUM: return (*it).medium_value;
00305 case SP_HARD: return (*it).hard_value;
00306 case SP_CUSTOM: return (*it).custom_value;
00307 default: NOT_REACHED();
00308 }
00309 }
00310
00311
00312 return -1;
00313 }