00001 /* $Id$ */ 00002 00003 /* 00004 * This file is part of OpenTTD. 00005 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2. 00006 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 00007 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>. 00008 */ 00009 00012 #include "../../stdafx.h" 00013 #include "script_error.hpp" 00014 #include "../../core/bitmath_func.hpp" 00015 00016 ScriptError::ScriptErrorMap ScriptError::error_map = ScriptError::ScriptErrorMap(); 00017 ScriptError::ScriptErrorMapString ScriptError::error_map_string = ScriptError::ScriptErrorMapString(); 00018 00019 /* static */ ScriptErrorType ScriptError::GetLastError() 00020 { 00021 return ScriptObject::GetLastError(); 00022 } 00023 00024 /* static */ char *ScriptError::GetLastErrorString() 00025 { 00026 return strdup((*error_map_string.find(ScriptError::GetLastError())).second); 00027 } 00028 00029 /* static */ ScriptErrorType ScriptError::StringToError(StringID internal_string_id) 00030 { 00031 uint index = GB(internal_string_id, 11, 5); 00032 switch (GB(internal_string_id, 11, 5)) { 00033 case 26: case 28: case 29: case 30: // NewGRF strings. 00034 return ERR_NEWGRF_SUPPLIED_ERROR; 00035 00036 /* DO NOT SWAP case 14 and 4 because that will break StringToError due 00037 * to the index dependency that relies on FALL THROUGHs. */ 00038 case 14: if (index < 0xE4) break; // Player name 00039 case 4: if (index < 0xC0) break; // Town name 00040 case 15: // Custom name 00041 case 31: // Dynamic strings 00042 /* These strings are 'random' and have no meaning. 00043 * They actually shouldn't even be returned as error messages. */ 00044 return ERR_UNKNOWN; 00045 00046 default: 00047 break; 00048 } 00049 00050 ScriptErrorMap::iterator it = error_map.find(internal_string_id); 00051 if (it == error_map.end()) return ERR_UNKNOWN; 00052 return (*it).second; 00053 } 00054 00055 /* static */ void ScriptError::RegisterErrorMap(StringID internal_string_id, ScriptErrorType ai_error_msg) 00056 { 00057 error_map[internal_string_id] = ai_error_msg; 00058 } 00059 00060 /* static */ void ScriptError::RegisterErrorMapString(ScriptErrorType ai_error_msg, const char *message) 00061 { 00062 error_map_string[ai_error_msg] = message; 00063 } 00064 00065 /* static */ ScriptError::ErrorCategories ScriptError::GetErrorCategory() 00066 { 00067 return (ScriptError::ErrorCategories)(GetLastError() >> (uint)ERR_CAT_BIT_SIZE); 00068 }