Codechange: Optimize FlowsDown (#13262)
[openttd-github.git] / src / script / api / script_error.cpp
bloba3f0e48d967c9504c87b08668c3071e1ab1afc0a
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 */
8 /** @file script_error.cpp Implementation of ScriptError. */
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"
16 #include "../../safeguards.h"
18 ScriptError::ScriptErrorMap ScriptError::error_map = ScriptError::ScriptErrorMap();
19 ScriptError::ScriptErrorMapString ScriptError::error_map_string = ScriptError::ScriptErrorMapString();
21 /* static */ ScriptErrorType ScriptError::GetLastError()
23 return ScriptObject::GetLastError();
26 /* static */ std::optional<std::string> ScriptError::GetLastErrorString()
28 return (*error_map_string.find(ScriptError::GetLastError())).second;
31 /* static */ ScriptErrorType ScriptError::StringToError(StringID internal_string_id)
33 StringIndexInTab 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.
39 case TEXT_TAB_SPECIAL:
40 if (index < 0xE4) break; // Player name
41 [[fallthrough]];
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;
49 default:
50 break;
53 ScriptErrorMap::iterator it = error_map.find(internal_string_id);
54 if (it == error_map.end()) return ERR_UNKNOWN;
55 return (*it).second;
58 /* static */ void ScriptError::RegisterErrorMap(StringID internal_string_id, ScriptErrorType ai_error_msg)
60 error_map[internal_string_id] = ai_error_msg;
63 /* static */ void ScriptError::RegisterErrorMapString(ScriptErrorType ai_error_msg, const char *message)
65 error_map_string[ai_error_msg] = message;
68 /* static */ ScriptError::ErrorCategories ScriptError::GetErrorCategory()
70 return (ScriptError::ErrorCategories)(GetLastError() >> (uint)ERR_CAT_BIT_SIZE);