OpenTTD Source: src/script/script_scanner.cpp Source File

OpenTTD Source 15.3

script_scanner.cpp
Go to the documentation of this file.
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 <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
10#include "../stdafx.h"
11#include "../debug.h"
12#include "../string_func.h"
13#include "../settings_type.h"
14
15#include "../script/squirrel.hpp"
16#include "script_scanner.hpp"
17#include "script_info.hpp"
18#include "script_fatalerror.hpp"
19
20#include "../network/network_content.h"
21#include "../3rdparty/md5/md5.h"
22#include "../tar_type.h"
23
24#include "../safeguards.h"
25
26 bool ScriptScanner::AddFile(const std::string &filename, size_t, const std::string &tar_filename)
27{
28 this->main_script = filename;
29 this->tar_file = tar_filename;
30
31 auto p = this->main_script.find_last_of(PATHSEPCHAR);
32 this->main_script.erase(p != std::string::npos ? p + 1 : 0);
33 this->main_script += "main.nut";
34
35 if (!FioCheckFileExists(filename, this->subdir) || !FioCheckFileExists(this->main_script, this->subdir)) return false;
36
37 this->ResetEngine();
38 try {
39 this->engine->LoadScript(filename);
40 } catch (Script_FatalError &e) {
41 Debug(script, 0, "Fatal error '{}' when trying to load the script '{}'.", e.GetErrorMessage(), filename);
42 return false;
43 }
44 return true;
45}
46
47ScriptScanner::ScriptScanner() = default;
48
49 void ScriptScanner::ResetEngine()
50{
51 this->engine->Reset();
52 this->engine->SetGlobalPointer(this);
53 this->RegisterAPI(*this->engine);
54}
55
56 void ScriptScanner::Initialize(std::string_view name)
57{
58 this->engine = std::make_unique<Squirrel>(name);
59
60 this->RescanDir();
61
62 this->ResetEngine();
63}
64
65ScriptScanner::~ScriptScanner()
66{
67 this->Reset();
68}
69
70 void ScriptScanner::RescanDir()
71{
72 /* Forget about older scans */
73 this->Reset();
74
75 /* Scan for scripts */
76 this->Scan(this->GetFileName(), this->GetDirectory());
77}
78
79 void ScriptScanner::Reset()
80{
81 this->info_list.clear();
82 this->info_single_list.clear();
83 this->info_vector.clear();
84}
85
86 void ScriptScanner::RegisterScript(std::unique_ptr<ScriptInfo> &&info)
87{
88 std::string script_original_name = this->GetScriptName(*info);
89 std::string script_name = fmt::format("{}.{}", script_original_name, info->GetVersion());
90
91 /* Check if GetShortName follows the rules */
92 if (info->GetShortName().size() != 4) {
93 Debug(script, 0, "The script '{}' returned a string from GetShortName() which is not four characters. Unable to load the script.", info->GetName());
94 return;
95 }
96
97 if (auto it = this->info_list.find(script_name); it != this->info_list.end()) {
98 /* This script was already registered */
99#ifdef _WIN32
100 /* Windows doesn't care about the case */
101 if (StrEqualsIgnoreCase(it->second->GetMainScript(), info->GetMainScript())) {
102#else
103 if (it->second->GetMainScript() == info->GetMainScript()) {
104#endif
105 return;
106 }
107
108 Debug(script, 1, "Registering two scripts with the same name and version");
109 Debug(script, 1, " 1: {}", it->second->GetMainScript());
110 Debug(script, 1, " 2: {}", info->GetMainScript());
111 Debug(script, 1, "The first is taking precedence.");
112
113 return;
114 }
115
116 ScriptInfo *script_info = this->info_vector.emplace_back(std::move(info)).get();
117 this->info_list[script_name] = script_info;
118
119 if (!script_info->IsDeveloperOnly() || _settings_client.gui.ai_developer_tools) {
120 /* Add the script to the 'unique' script list, where only the highest version
121 * of the script is registered. */
122 auto it = this->info_single_list.find(script_original_name);
123 if (it == this->info_single_list.end()) {
124 this->info_single_list[script_original_name] = script_info;
125 } else if (it->second->GetVersion() < script_info->GetVersion()) {
126 it->second = script_info;
127 }
128 }
129}
130
131 void ScriptScanner::GetConsoleList(std::back_insert_iterator<std::string> &output_iterator, bool newest_only) const
132{
133 fmt::format_to(output_iterator, "List of {}:\n", this->GetScannerName());
134 const ScriptInfoList &list = newest_only ? this->info_single_list : this->info_list;
135 for (const auto &item : list) {
136 ScriptInfo *i = item.second;
137 fmt::format_to(output_iterator, "{:>10} (v{:d}): {}\n", i->GetName(), i->GetVersion(), i->GetDescription());
138 }
139 fmt::format_to(output_iterator, "\n");
140}
141
143 struct ScriptFileChecksumCreator : FileScanner {
144 MD5Hash md5sum;
145 Subdirectory dir;
146
151 ScriptFileChecksumCreator(Subdirectory dir) : dir(dir) {}
152
153 /* Add the file and calculate the md5 sum. */
154 bool AddFile(const std::string &filename, size_t, const std::string &) override
155 {
156 Md5 checksum;
157 uint8_t buffer[1024];
158 size_t len, size;
159
160 /* Open the file ... */
161 auto f = FioFOpenFile(filename, "rb", this->dir, &size);
162 if (!f.has_value()) return false;
163
164 /* ... calculate md5sum... */
165 while ((len = fread(buffer, 1, (size > sizeof(buffer)) ? sizeof(buffer) : size, *f)) != 0 && size != 0) {
166 size -= len;
167 checksum.Append(buffer, len);
168 }
169
170 MD5Hash tmp_md5sum;
171 checksum.Finish(tmp_md5sum);
172
173 /* ... and xor it to the overall md5sum. */
174 this->md5sum ^= tmp_md5sum;
175
176 return true;
177 }
178};
179
188 static bool IsSameScript(const ContentInfo &ci, bool md5sum, const ScriptInfo &info, Subdirectory dir)
189{
190 uint32_t id = 0;
191 auto str = std::string_view{info.GetShortName()}.substr(0, 4);
192 for (size_t j = 0; j < str.size(); j++) id |= static_cast<uint8_t>(str[j]) << (8 * j);
193
194 if (id != ci.unique_id) return false;
195 if (!md5sum) return true;
196
197 ScriptFileChecksumCreator checksum(dir);
198 const auto &tar_filename = info.GetTarFile();
199 TarList::iterator iter;
200 if (!tar_filename.empty() && (iter = _tar_list[dir].find(tar_filename)) != _tar_list[dir].end()) {
201 /* The main script is in a tar file, so find all files that
202 * are in the same tar and add them to the MD5 checksumming. */
203 for (const auto &tar : _tar_filelist[dir]) {
204 /* Not in the same tar. */
205 if (tar.second.tar_filename != iter->first) continue;
206
207 /* Check the extension. */
208 auto ext = tar.first.rfind('.');
209 if (ext == std::string_view::npos || !StrEqualsIgnoreCase(tar.first.substr(ext), ".nut")) continue;
210
211 checksum.AddFile(tar.first, 0, tar_filename);
212 }
213 } else {
214 /* There'll always be at least 1 path separator character in a script
215 * main script name as the search algorithm requires the main script to
216 * be in a subdirectory of the script directory; so <dir>/<path>/main.nut. */
217 const std::string &main_script = info.GetMainScript();
218 std::string path = main_script.substr(0, main_script.find_last_of(PATHSEPCHAR));
219 checksum.Scan(".nut", path);
220 }
221
222 return ci.md5sum == checksum.md5sum;
223}
224
225 bool ScriptScanner::HasScript(const ContentInfo &ci, bool md5sum)
226{
227 for (const auto &item : this->info_list) {
228 if (IsSameScript(ci, md5sum, *item.second, this->GetDirectory())) return true;
229 }
230 return false;
231}
232
233std::optional<std::string_view> ScriptScanner::FindMainScript(const ContentInfo &ci, bool md5sum)
234{
235 for (const auto &item : this->info_list) {
236 if (IsSameScript(ci, md5sum, *item.second, this->GetDirectory())) return item.second->GetMainScript();
237 }
238 return std::nullopt;
239}
FileScanner
Helper for scanning for files with a given name.
Definition fileio_func.h:37
FileScanner::Scan
uint Scan(std::string_view extension, Subdirectory sd, bool tars=true, bool recursive=true)
Scan for files with the given extension in the given search path.
Definition fileio.cpp:1115
FileScanner::subdir
Subdirectory subdir
The current sub directory we are searching through.
Definition fileio_func.h:39
ScriptInfo
All static information from an Script like name, version, etc.
Definition script_info.hpp:30
ScriptInfo::GetName
const std::string & GetName() const
Get the Name of the script.
Definition script_info.hpp:40
ScriptInfo::GetMainScript
const std::string & GetMainScript() const
Get the filename of the main.nut script.
Definition script_info.hpp:75
ScriptInfo::GetShortName
const std::string & GetShortName() const
Get the 4 character long short name of the script.
Definition script_info.hpp:45
ScriptInfo::GetVersion
int GetVersion() const
Get the version of the script.
Definition script_info.hpp:55
ScriptInfo::GetTarFile
const std::string & GetTarFile() const
Get the filename of the tar the script is in.
Definition script_info.hpp:80
ScriptInfo::GetDescription
const std::string & GetDescription() const
Get the description of the script.
Definition script_info.hpp:50
ScriptInfo::IsDeveloperOnly
virtual bool IsDeveloperOnly() const
Can this script be selected by developers only?
Definition script_info.hpp:130
ScriptScanner::AddFile
bool AddFile(const std::string &filename, size_t basepath_length, const std::string &tar_filename) override
Add a file with the given filename.
Definition script_scanner.cpp:26
ScriptScanner::GetScannerName
virtual std::string_view GetScannerName() const =0
Get the type of the script, in plural.
ScriptScanner::tar_file
std::string tar_file
If, which tar file the script was in.
Definition script_scanner.hpp:89
ScriptScanner::RegisterAPI
virtual void RegisterAPI(class Squirrel &engine)=0
Register the API for this ScriptInfo.
ScriptScanner::Reset
void Reset()
Reset all allocated lists.
Definition script_scanner.cpp:79
ScriptScanner::GetScriptName
virtual std::string GetScriptName(ScriptInfo &info)=0
Get the script name how to store the script in memory.
ScriptScanner::RescanDir
void RescanDir()
Rescan the script dir.
Definition script_scanner.cpp:70
ScriptScanner::GetDirectory
virtual Subdirectory GetDirectory() const =0
Get the directory to scan in.
ScriptScanner::main_script
std::string main_script
The full path of the script.
Definition script_scanner.hpp:88
ScriptScanner::GetConsoleList
void GetConsoleList(std::back_insert_iterator< std::string > &output_iterator, bool newest_only) const
Get the list of registered scripts to print on the console.
Definition script_scanner.cpp:131
ScriptScanner::FindMainScript
std::optional< std::string_view > FindMainScript(const ContentInfo &ci, bool md5sum)
Find a script of a ContentInfo.
Definition script_scanner.cpp:233
ScriptScanner::GetFileName
virtual std::string_view GetFileName() const =0
Get the filename to scan for this type of script.
ScriptScanner::info_list
ScriptInfoList info_list
The list of all script.
Definition script_scanner.hpp:93
ScriptScanner::info_single_list
ScriptInfoList info_single_list
The list of all unique script. The best script (highest version) is shown.
Definition script_scanner.hpp:94
ScriptScanner::RegisterScript
void RegisterScript(std::unique_ptr< class ScriptInfo > &&info)
Register a ScriptInfo to the scanner.
Definition script_scanner.cpp:86
ScriptScanner::engine
std::unique_ptr< class Squirrel > engine
The engine we're scanning with.
Definition script_scanner.hpp:87
ScriptScanner::HasScript
bool HasScript(const struct ContentInfo &ci, bool md5sum)
Check whether we have a script with the exact characteristics as ci.
Definition script_scanner.cpp:225
ScriptScanner::ResetEngine
void ResetEngine()
Reset the engine to ensure a clean environment for further steps.
Definition script_scanner.cpp:49
Script_FatalError
A throw-class that is given when the script made a fatal error.
Definition script_fatalerror.hpp:16
Script_FatalError::GetErrorMessage
const std::string & GetErrorMessage() const
The error message associated with the fatal error.
Definition script_fatalerror.hpp:30
Debug
#define Debug(category, level, format_string,...)
Output a line of debugging information.
Definition debug.h:37
FioFOpenFile
std::optional< FileHandle > FioFOpenFile(std::string_view filename, std::string_view mode, Subdirectory subdir, size_t *filesize)
Opens a OpenTTD file somewhere in a personal or global directory.
Definition fileio.cpp:242
FioCheckFileExists
bool FioCheckFileExists(std::string_view filename, Subdirectory subdir)
Check whether the given file exists.
Definition fileio.cpp:121
Subdirectory
Subdirectory
The different kinds of subdirectories OpenTTD uses.
Definition fileio_type.h:88
script_fatalerror.hpp
The definition of Script_FatalError.
script_info.hpp
ScriptInfo keeps track of all information of a script, like Author, Description, ....
IsSameScript
static bool IsSameScript(const ContentInfo &ci, bool md5sum, const ScriptInfo &info, Subdirectory dir)
Check whether the script given in info is the same as in ci based on the shortname and md5 sum.
Definition script_scanner.cpp:188
script_scanner.hpp
Declarations of the class for the script scanner.
ScriptInfoList
std::map< std::string, class ScriptInfo *, CaseInsensitiveComparator > ScriptInfoList
Type for the list of scripts.
Definition script_scanner.hpp:16
_settings_client
ClientSettings _settings_client
The current settings for this game.
Definition settings.cpp:60
StrEqualsIgnoreCase
bool StrEqualsIgnoreCase(std::string_view str1, std::string_view str2)
Compares two string( view)s for equality, while ignoring the case of the characters.
Definition string.cpp:323
ClientSettings::gui
GUISettings gui
settings related to the GUI
Definition settings_type.h:650
ContentInfo
Container for all important information about a piece of content.
Definition tcp_content_type.h:56
ContentInfo::unique_id
uint32_t unique_id
Unique ID; either GRF ID or shortname.
Definition tcp_content_type.h:75
ContentInfo::md5sum
MD5Hash md5sum
The MD5 checksum.
Definition tcp_content_type.h:76
GUISettings::ai_developer_tools
bool ai_developer_tools
activate AI/GS developer tools
Definition settings_type.h:252
ScriptFileChecksumCreator
Helper for creating a MD5sum of all files within of a script.
Definition script_scanner.cpp:143
ScriptFileChecksumCreator::md5sum
MD5Hash md5sum
The final md5sum.
Definition script_scanner.cpp:144
ScriptFileChecksumCreator::AddFile
bool AddFile(const std::string &filename, size_t, const std::string &) override
Add a file with the given filename.
Definition script_scanner.cpp:154
ScriptFileChecksumCreator::dir
Subdirectory dir
The directory to look in.
Definition script_scanner.cpp:145
ScriptFileChecksumCreator::ScriptFileChecksumCreator
ScriptFileChecksumCreator(Subdirectory dir)
Initialise the md5sum to be all zeroes, so we can easily xor the data.
Definition script_scanner.cpp:151