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
00130 if (strcmp(key, "name") == 0) {
00131 const SQChar *sqvalue;
00132 if (SQ_FAILED(sq_getstring(vm, -1, &sqvalue))) return SQ_ERROR;
00133 char *name = strdup(SQ2OTTD(sqvalue));
00134 char *s;
00135
00136
00137 while ((s = strchr(name, '=')) != NULL) *s = '_';
00138 while ((s = strchr(name, ',')) != NULL) *s = '_';
00139 config.name = name;
00140 items |= 0x001;
00141 } else if (strcmp(key, "description") == 0) {
00142 const SQChar *sqdescription;
00143 if (SQ_FAILED(sq_getstring(vm, -1, &sqdescription))) return SQ_ERROR;
00144 config.description = strdup(SQ2OTTD(sqdescription));
00145 items |= 0x002;
00146 } else if (strcmp(key, "min_value") == 0) {
00147 SQInteger res;
00148 if (SQ_FAILED(sq_getinteger(vm, -1, &res))) return SQ_ERROR;
00149 config.min_value = res;
00150 items |= 0x004;
00151 } else if (strcmp(key, "max_value") == 0) {
00152 SQInteger res;
00153 if (SQ_FAILED(sq_getinteger(vm, -1, &res))) return SQ_ERROR;
00154 config.max_value = res;
00155 items |= 0x008;
00156 } else if (strcmp(key, "easy_value") == 0) {
00157 SQInteger res;
00158 if (SQ_FAILED(sq_getinteger(vm, -1, &res))) return SQ_ERROR;
00159 config.easy_value = res;
00160 items |= 0x010;
00161 } else if (strcmp(key, "medium_value") == 0) {
00162 SQInteger res;
00163 if (SQ_FAILED(sq_getinteger(vm, -1, &res))) return SQ_ERROR;
00164 config.medium_value = res;
00165 items |= 0x020;
00166 } else if (strcmp(key, "hard_value") == 0) {
00167 SQInteger res;
00168 if (SQ_FAILED(sq_getinteger(vm, -1, &res))) return SQ_ERROR;
00169 config.hard_value = res;
00170 items |= 0x040;
00171 } else if (strcmp(key, "random_deviation") == 0) {
00172 SQInteger res;
00173 if (SQ_FAILED(sq_getinteger(vm, -1, &res))) return SQ_ERROR;
00174 config.random_deviation = res;
00175 items |= 0x200;
00176 } else if (strcmp(key, "custom_value") == 0) {
00177 SQInteger res;
00178 if (SQ_FAILED(sq_getinteger(vm, -1, &res))) return SQ_ERROR;
00179 config.custom_value = res;
00180 items |= 0x080;
00181 } else if (strcmp(key, "step_size") == 0) {
00182 SQInteger res;
00183 if (SQ_FAILED(sq_getinteger(vm, -1, &res))) return SQ_ERROR;
00184 config.step_size = res;
00185 } else if (strcmp(key, "flags") == 0) {
00186 SQInteger res;
00187 if (SQ_FAILED(sq_getinteger(vm, -1, &res))) return SQ_ERROR;
00188 config.flags = (ScriptConfigFlags)res;
00189 items |= 0x100;
00190 } else {
00191 char error[1024];
00192 snprintf(error, sizeof(error), "unknown setting property '%s'", key);
00193 this->engine->ThrowError(error);
00194 return SQ_ERROR;
00195 }
00196
00197 sq_pop(vm, 2);
00198 }
00199 sq_pop(vm, 1);
00200
00201
00202
00203 if ((items & 0x200) != 0 && (config.flags & SCRIPTCONFIG_RANDOM) != 0) {
00204 char error[1024];
00205 snprintf(error, sizeof(error), "Setting both random_deviation and SCRIPTCONFIG_RANDOM is not allowed");
00206 this->engine->ThrowError(error);
00207 return SQ_ERROR;
00208 }
00209
00210 items &= ~0x200;
00211
00212
00213 uint mask = (config.flags & SCRIPTCONFIG_BOOLEAN) ? 0x1F3 : 0x1FF;
00214 if (items != mask) {
00215 char error[1024];
00216 snprintf(error, sizeof(error), "please define all properties of a setting (min/max not allowed for booleans)");
00217 this->engine->ThrowError(error);
00218 return SQ_ERROR;
00219 }
00220
00221 this->config_list.push_back(config);
00222 return 0;
00223 }
00224
00225 SQInteger ScriptInfo::AddLabels(HSQUIRRELVM vm)
00226 {
00227 const SQChar *sq_setting_name;
00228 if (SQ_FAILED(sq_getstring(vm, -2, &sq_setting_name))) return SQ_ERROR;
00229 const char *setting_name = SQ2OTTD(sq_setting_name);
00230
00231 ScriptConfigItem *config = NULL;
00232 for (ScriptConfigItemList::iterator it = this->config_list.begin(); it != this->config_list.end(); it++) {
00233 if (strcmp((*it).name, setting_name) == 0) config = &(*it);
00234 }
00235
00236 if (config == NULL) {
00237 char error[1024];
00238 snprintf(error, sizeof(error), "Trying to add labels for non-defined setting '%s'", setting_name);
00239 this->engine->ThrowError(error);
00240 return SQ_ERROR;
00241 }
00242 if (config->labels != NULL) return SQ_ERROR;
00243
00244 config->labels = new LabelMapping;
00245
00246
00247 sq_pushnull(vm);
00248 while (SQ_SUCCEEDED(sq_next(vm, -2))) {
00249 const SQChar *sq_key;
00250 const SQChar *sq_label;
00251 if (SQ_FAILED(sq_getstring(vm, -2, &sq_key))) return SQ_ERROR;
00252 if (SQ_FAILED(sq_getstring(vm, -1, &sq_label))) return SQ_ERROR;
00253
00254
00255 const char *key_string = SQ2OTTD(sq_key);
00256 int key = atoi(key_string + 1);
00257 const char *label = SQ2OTTD(sq_label);
00258
00259
00260 if (!config->labels->Contains(key)) config->labels->Insert(key, strdup(label));
00261
00262 sq_pop(vm, 2);
00263 }
00264 sq_pop(vm, 1);
00265
00266 return 0;
00267 }
00268
00269 const ScriptConfigItemList *ScriptInfo::GetConfigList() const
00270 {
00271 return &this->config_list;
00272 }
00273
00274 const ScriptConfigItem *ScriptInfo::GetConfigItem(const char *name) const
00275 {
00276 for (ScriptConfigItemList::const_iterator it = this->config_list.begin(); it != this->config_list.end(); it++) {
00277 if (strcmp((*it).name, name) == 0) return &(*it);
00278 }
00279 return NULL;
00280 }
00281
00282 int ScriptInfo::GetSettingDefaultValue(const char *name) const
00283 {
00284 for (ScriptConfigItemList::const_iterator it = this->config_list.begin(); it != this->config_list.end(); it++) {
00285 if (strcmp((*it).name, name) != 0) continue;
00286
00287 switch (GetGameSettings().difficulty.diff_level) {
00288 case 0: return (*it).easy_value;
00289 case 1: return (*it).medium_value;
00290 case 2: return (*it).hard_value;
00291 case 3: return (*it).custom_value;
00292 default: NOT_REACHED();
00293 }
00294 }
00295
00296
00297 return -1;
00298 }