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/>.
8 /** @file script_scanner.cpp Allows scanning for scripts. */
10 #include "../stdafx.h"
12 #include "../string_func.h"
13 #include "../settings_type.h"
15 #include "../script/squirrel.hpp"
16 #include "script_scanner.hpp"
17 #include "script_info.hpp"
18 #include "script_fatalerror.hpp"
20 #include "../network/network_content.h"
21 #include "../3rdparty/md5/md5.h"
22 #include "../tar_type.h"
24 #include "../safeguards.h"
26 bool ScriptScanner::AddFile(const std::string
&filename
, size_t basepath_length
, const std::string
&tar_filename
)
28 this->main_script
= filename
;
29 this->tar_file
= tar_filename
;
31 auto p
= this->main_script
.rfind(PATHSEPCHAR
);
32 this->main_script
.erase(p
!= std::string::npos
? p
+ 1 : 0);
33 this->main_script
+= "main.nut";
35 if (!FioCheckFileExists(filename
, this->subdir
) || !FioCheckFileExists(this->main_script
, this->subdir
)) return false;
39 this->engine
->LoadScript(filename
.c_str());
40 } catch (Script_FatalError
&e
) {
41 Debug(script
, 0, "Fatal error '{}' when trying to load the script '{}'.", e
.GetErrorMessage(), filename
);
47 ScriptScanner::ScriptScanner() :
52 void ScriptScanner::ResetEngine()
54 this->engine
->Reset();
55 this->engine
->SetGlobalPointer(this);
56 this->RegisterAPI(this->engine
);
59 void ScriptScanner::Initialize(const char *name
)
61 this->engine
= new Squirrel(name
);
68 ScriptScanner::~ScriptScanner()
75 void ScriptScanner::RescanDir()
77 /* Forget about older scans */
80 /* Scan for scripts */
81 this->Scan(this->GetFileName(), this->GetDirectory());
84 void ScriptScanner::Reset()
86 for (const auto &item
: this->info_list
) {
90 for (const auto &item
: this->info_single_list
) {
94 this->info_list
.clear();
95 this->info_single_list
.clear();
98 void ScriptScanner::RegisterScript(ScriptInfo
*info
)
100 char script_original_name
[1024];
101 this->GetScriptName(info
, script_original_name
, lastof(script_original_name
));
102 strtolower(script_original_name
);
104 char script_name
[1024];
105 seprintf(script_name
, lastof(script_name
), "%s.%d", script_original_name
, info
->GetVersion());
107 /* Check if GetShortName follows the rules */
108 if (strlen(info
->GetShortName()) != 4) {
109 Debug(script
, 0, "The script '{}' returned a string from GetShortName() which is not four characaters. Unable to load the script.", info
->GetName());
114 if (this->info_list
.find(script_name
) != this->info_list
.end()) {
115 /* This script was already registered */
117 /* Windows doesn't care about the case */
118 if (strcasecmp(this->info_list
[script_name
]->GetMainScript(), info
->GetMainScript()) == 0) {
120 if (strcmp(this->info_list
[script_name
]->GetMainScript(), info
->GetMainScript()) == 0) {
126 Debug(script
, 1, "Registering two scripts with the same name and version");
127 Debug(script
, 1, " 1: {}", this->info_list
[script_name
]->GetMainScript());
128 Debug(script
, 1, " 2: {}", info
->GetMainScript());
129 Debug(script
, 1, "The first is taking precedence.");
135 this->info_list
[stredup(script_name
)] = info
;
137 if (!info
->IsDeveloperOnly() || _settings_client
.gui
.ai_developer_tools
) {
138 /* Add the script to the 'unique' script list, where only the highest version
139 * of the script is registered. */
140 if (this->info_single_list
.find(script_original_name
) == this->info_single_list
.end()) {
141 this->info_single_list
[stredup(script_original_name
)] = info
;
142 } else if (this->info_single_list
[script_original_name
]->GetVersion() < info
->GetVersion()) {
143 this->info_single_list
[script_original_name
] = info
;
148 std::string
ScriptScanner::GetConsoleList(bool newest_only
) const
151 p
+= fmt::format("List of {}:\n", this->GetScannerName());
152 const ScriptInfoList
&list
= newest_only
? this->info_single_list
: this->info_list
;
153 for (const auto &item
: list
) {
154 ScriptInfo
*i
= item
.second
;
155 p
+= fmt::format("{:>10} (v{:d}): {}\n", i
->GetName(), i
->GetVersion(), i
->GetDescription());
162 /** Helper for creating a MD5sum of all files within of a script. */
163 struct ScriptFileChecksumCreator
: FileScanner
{
164 byte md5sum
[16]; ///< The final md5sum.
165 Subdirectory dir
; ///< The directory to look in.
168 * Initialise the md5sum to be all zeroes,
169 * so we can easily xor the data.
171 ScriptFileChecksumCreator(Subdirectory dir
)
174 memset(this->md5sum
, 0, sizeof(this->md5sum
));
177 /* Add the file and calculate the md5 sum. */
178 virtual bool AddFile(const std::string
&filename
, size_t basepath_length
, const std::string
&tar_filename
)
185 /* Open the file ... */
186 FILE *f
= FioFOpenFile(filename
, "rb", this->dir
, &size
);
187 if (f
== nullptr) return false;
189 /* ... calculate md5sum... */
190 while ((len
= fread(buffer
, 1, (size
> sizeof(buffer
)) ? sizeof(buffer
) : size
, f
)) != 0 && size
!= 0) {
192 checksum
.Append(buffer
, len
);
194 checksum
.Finish(tmp_md5sum
);
198 /* ... and xor it to the overall md5sum. */
199 for (uint i
= 0; i
< sizeof(md5sum
); i
++) this->md5sum
[i
] ^= tmp_md5sum
[i
];
206 * Check whether the script given in info is the same as in ci based
207 * on the shortname and md5 sum.
208 * @param ci The information to compare to.
209 * @param md5sum Whether to check the MD5 checksum.
210 * @param info The script to get the shortname and md5 sum from.
211 * @return True iff they're the same.
213 static bool IsSameScript(const ContentInfo
*ci
, bool md5sum
, ScriptInfo
*info
, Subdirectory dir
)
216 const char *str
= info
->GetShortName();
217 for (int j
= 0; j
< 4 && *str
!= '\0'; j
++, str
++) id
|= *str
<< (8 * j
);
219 if (id
!= ci
->unique_id
) return false;
220 if (!md5sum
) return true;
222 ScriptFileChecksumCreator
checksum(dir
);
223 auto tar_filename
= info
->GetTarFile();
224 TarList::iterator iter
;
225 if (!tar_filename
.empty() && (iter
= _tar_list
[dir
].find(tar_filename
)) != _tar_list
[dir
].end()) {
226 /* The main script is in a tar file, so find all files that
227 * are in the same tar and add them to the MD5 checksumming. */
228 for (const auto &tar
: _tar_filelist
[dir
]) {
229 /* Not in the same tar. */
230 if (tar
.second
.tar_filename
!= iter
->first
) continue;
232 /* Check the extension. */
233 const char *ext
= strrchr(tar
.first
.c_str(), '.');
234 if (ext
== nullptr || strcasecmp(ext
, ".nut") != 0) continue;
236 checksum
.AddFile(tar
.first
, 0, tar_filename
);
240 strecpy(path
, info
->GetMainScript(), lastof(path
));
241 /* There'll always be at least 1 path separator character in a script
242 * main script name as the search algorithm requires the main script to
243 * be in a subdirectory of the script directory; so <dir>/<path>/main.nut. */
244 *strrchr(path
, PATHSEPCHAR
) = '\0';
245 checksum
.Scan(".nut", path
);
248 return memcmp(ci
->md5sum
, checksum
.md5sum
, sizeof(ci
->md5sum
)) == 0;
251 bool ScriptScanner::HasScript(const ContentInfo
*ci
, bool md5sum
)
253 for (const auto &item
: this->info_list
) {
254 if (IsSameScript(ci
, md5sum
, item
.second
, this->GetDirectory())) return true;
259 const char *ScriptScanner::FindMainScript(const ContentInfo
*ci
, bool md5sum
)
261 for (const auto &item
: this->info_list
) {
262 if (IsSameScript(ci
, md5sum
, item
.second
, this->GetDirectory())) return item
.second
->GetMainScript();