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_newgrf.cpp Implementation of ScriptNewGRF and friends. */
9 
10 #include "../../stdafx.h"
11 #include "script_newgrf.hpp"
12 #include "../../core/bitmath_func.hpp"
13 #include "../../newgrf_config.h"
14 #include "../../string_func.h"
15 
16 #include "../../safeguards.h"
17 
ScriptNewGRFList()18 ScriptNewGRFList::ScriptNewGRFList()
19 {
20 	for (auto c = _grfconfig; c != nullptr; c = c->next) {
21 		if (!HasBit(c->flags, GCF_STATIC)) {
22 			this->AddItem(BSWAP32(c->ident.grfid));
23 		}
24 	}
25 }
26 
IsLoaded(uint32 grfid)27 /* static */ bool ScriptNewGRF::IsLoaded(uint32 grfid)
28 {
29 	grfid = BSWAP32(grfid); // Match people's expectations.
30 
31 	for (auto c = _grfconfig; c != nullptr; c = c->next) {
32 		if (!HasBit(c->flags, GCF_STATIC) && c->ident.grfid == grfid) {
33 			return true;
34 		}
35 	}
36 
37 	return false;
38 }
39 
GetVersion(uint32 grfid)40 /* static */ uint32 ScriptNewGRF::GetVersion(uint32 grfid)
41 {
42 	grfid = BSWAP32(grfid); // Match people's expectations.
43 
44 	for (auto c = _grfconfig; c != nullptr; c = c->next) {
45 		if (!HasBit(c->flags, GCF_STATIC) && c->ident.grfid == grfid) {
46 			return c->version;
47 		}
48 	}
49 
50 	return 0;
51 }
52 
GetName(uint32 grfid)53 /* static */ char *ScriptNewGRF::GetName(uint32 grfid)
54 {
55 	grfid = BSWAP32(grfid); // Match people's expectations.
56 
57 	for (auto c = _grfconfig; c != nullptr; c = c->next) {
58 		if (!HasBit(c->flags, GCF_STATIC) && c->ident.grfid == grfid) {
59 			return ::stredup(c->GetName());
60 		}
61 	}
62 
63 	return nullptr;
64 }
65