1 /*
2  * This file is part of OpenTTD.
3  * 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.
4  * 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.
5  * 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/>.
6  */
7 
8 /** @file script_error.cpp Implementation of ScriptError. */
9 
10 #include "../../stdafx.h"
11 #include "script_error.hpp"
12 #include "../../core/bitmath_func.hpp"
13 #include "../../string_func.h"
14 #include "../../strings_func.h"
15 
16 #include "../../safeguards.h"
17 
18 ScriptError::ScriptErrorMap ScriptError::error_map = ScriptError::ScriptErrorMap();
19 ScriptError::ScriptErrorMapString ScriptError::error_map_string = ScriptError::ScriptErrorMapString();
20 
GetLastError()21 /* static */ ScriptErrorType ScriptError::GetLastError()
22 {
23 	return ScriptObject::GetLastError();
24 }
25 
GetLastErrorString()26 /* static */ char *ScriptError::GetLastErrorString()
27 {
28 	return stredup((*error_map_string.find(ScriptError::GetLastError())).second);
29 }
30 
StringToError(StringID internal_string_id)31 /* static */ ScriptErrorType ScriptError::StringToError(StringID internal_string_id)
32 {
33 	uint index = GetStringIndex(internal_string_id);
34 	switch (GetStringTab(internal_string_id)) {
35 		case TEXT_TAB_NEWGRF_START:
36 		case TEXT_TAB_GAMESCRIPT_START:
37 			return ERR_NEWGRF_SUPPLIED_ERROR; // NewGRF strings.
38 
39 		case TEXT_TAB_SPECIAL:
40 			if (index < 0xE4) break; // Player name
41 			FALLTHROUGH;
42 
43 		case TEXT_TAB_TOWN:
44 			if (index < 0xC0) break; // Town name
45 			/* These strings are 'random' and have no meaning.
46 			 * They actually shouldn't even be returned as error messages. */
47 			return ERR_UNKNOWN;
48 
49 		default:
50 			break;
51 	}
52 
53 	ScriptErrorMap::iterator it = error_map.find(internal_string_id);
54 	if (it == error_map.end()) return ERR_UNKNOWN;
55 	return (*it).second;
56 }
57 
RegisterErrorMap(StringID internal_string_id,ScriptErrorType ai_error_msg)58 /* static */ void ScriptError::RegisterErrorMap(StringID internal_string_id, ScriptErrorType ai_error_msg)
59 {
60 	error_map[internal_string_id] = ai_error_msg;
61 }
62 
RegisterErrorMapString(ScriptErrorType ai_error_msg,const char * message)63 /* static */ void ScriptError::RegisterErrorMapString(ScriptErrorType ai_error_msg, const char *message)
64 {
65 	error_map_string[ai_error_msg] = message;
66 }
67 
GetErrorCategory()68 /* static */ ScriptError::ErrorCategories ScriptError::GetErrorCategory()
69 {
70 	return (ScriptError::ErrorCategories)(GetLastError() >> (uint)ERR_CAT_BIT_SIZE);
71 }
72