12#include "../string_func.h"
13#include "../settings_type.h"
15#include "../script/squirrel.hpp"
20#include "../network/network_content.h"
21#include "../3rdparty/md5/md5.h"
22#include "../tar_type.h"
24#include "../safeguards.h"
31 auto p = this->
main_script.find_last_of(PATHSEPCHAR);
32 this->
main_script.erase(p != std::string::npos ? p + 1 : 0);
41 Debug(script, 0,
"Fatal error '{}' when trying to load the script '{}'.", e.
GetErrorMessage(), filename);
47ScriptScanner::ScriptScanner() :
59void ScriptScanner::Initialize(
const char *name)
68ScriptScanner::~ScriptScanner()
86 for (
const auto &item : this->
info_list) {
97 std::string script_name = fmt::format(
"{}.{}", script_original_name, info->
GetVersion());
101 Debug(script, 0,
"The script '{}' returned a string from GetShortName() which is not four characters. Unable to load the script.", info->
GetName());
106 if (this->
info_list.find(script_name) != this->info_list.end()) {
118 Debug(script, 1,
"Registering two scripts with the same name and version");
121 Debug(script, 1,
"The first is taking precedence.");
135 }
else if (it->second->GetVersion() < info->
GetVersion()) {
143 fmt::format_to(output_iterator,
"List of {}:\n", this->
GetScannerName());
145 for (
const auto &item : list) {
149 fmt::format_to(output_iterator,
"\n");
164 bool AddFile(
const std::string &filename,
size_t,
const std::string &)
override
167 uint8_t buffer[1024];
171 auto f =
FioFOpenFile(filename,
"rb", this->dir, &size);
172 if (!f.has_value())
return false;
175 while ((len = fread(buffer, 1, (size >
sizeof(buffer)) ?
sizeof(buffer) : size, *f)) != 0 && size != 0) {
177 checksum.Append(buffer, len);
181 checksum.Finish(tmp_md5sum);
184 this->md5sum ^= tmp_md5sum;
202 for (
int j = 0; j < 4 && *str !=
'\0'; j++, str++)
id |= *str << (8 * j);
205 if (!md5sum)
return true;
209 TarList::iterator iter;
210 if (!tar_filename.empty() && (iter = _tar_list[dir].find(tar_filename)) != _tar_list[dir].end()) {
213 for (
const auto &tar : _tar_filelist[dir]) {
215 if (tar.second.tar_filename != iter->first)
continue;
218 const char *ext = strrchr(tar.first.c_str(),
'.');
221 checksum.
AddFile(tar.first, 0, tar_filename);
229 checksum.
Scan(
".nut", path);
237 for (
const auto &item : this->
info_list) {
238 if (
IsSameScript(ci, md5sum, item.second, this->GetDirectory()))
return true;
245 for (
const auto &item : this->
info_list) {
246 if (
IsSameScript(ci, md5sum, item.second, this->GetDirectory()))
return item.second->GetMainScript().c_str();
Helper for scanning for files with a given name.
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.
Subdirectory subdir
The current sub directory we are searching through.
All static information from an Script like name, version, etc.
const std::string & GetName() const
Get the Name of the script.
const std::string & GetMainScript() const
Get the filename of the main.nut script.
const std::string & GetShortName() const
Get the 4 character long short name of the script.
int GetVersion() const
Get the version of the script.
const std::string & GetTarFile() const
Get the filename of the tar the script is in.
const std::string & GetDescription() const
Get the description of the script.
virtual bool IsDeveloperOnly() const
Can this script be selected by developers only?
bool HasScript(const struct ContentInfo *ci, bool md5sum)
Check whether we have a script with the exact characteristics as ci.
bool AddFile(const std::string &filename, size_t basepath_length, const std::string &tar_filename) override
Add a file with the given filename.
virtual const char * GetFileName() const =0
Get the filename to scan for this type of script.
const char * FindMainScript(const ContentInfo *ci, bool md5sum)
Find a script of a ContentInfo.
class Squirrel * engine
The engine we're scanning with.
std::string tar_file
If, which tar file the script was in.
void Reset()
Reset all allocated lists.
void RescanDir()
Rescan the script dir.
virtual const char * GetScannerName() const =0
Get the type of the script, in plural.
virtual Subdirectory GetDirectory() const =0
Get the directory to scan in.
std::string main_script
The full path of the script.
virtual void RegisterAPI(class Squirrel *engine)=0
Register the API for this ScriptInfo.
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.
virtual std::string GetScriptName(ScriptInfo *info)=0
Get the script name how to store the script in memory.
std::string GetMainScript()
Get the current main script the ScanDir is currently tracking.
ScriptInfoList info_list
The list of all script.
ScriptInfoList info_single_list
The list of all unique script. The best script (highest version) is shown.
void RegisterScript(class ScriptInfo *info)
Register a ScriptInfo to the scanner.
void ResetEngine()
Reset the engine to ensure a clean environment for further steps.
A throw-class that is given when the script made a fatal error.
const std::string & GetErrorMessage() const
The error message associated with the fatal error.
void Reset()
Completely reset the engine; start from scratch.
bool LoadScript(const std::string &script)
Load a script.
void SetGlobalPointer(void *ptr)
Sets a pointer in the VM that is reachable from where ever you are in SQ.
#define Debug(category, level, format_string,...)
Ouptut a line of debugging information.
bool FioCheckFileExists(const std::string &filename, Subdirectory subdir)
Check whether the given file exists.
std::optional< FileHandle > FioFOpenFile(const std::string &filename, const char *mode, Subdirectory subdir, size_t *filesize)
Opens a OpenTTD file somewhere in a personal or global directory.
Subdirectory
The different kinds of subdirectories OpenTTD uses.
The definition of Script_FatalError.
ScriptInfo keeps track of all information of a script, like Author, Description, ....
static bool IsSameScript(const ContentInfo *ci, bool md5sum, ScriptInfo *info, Subdirectory dir)
Check whether the script given in info is the same as in ci based on the shortname and md5 sum.
Declarations of the class for the script scanner.
std::map< std::string, class ScriptInfo *, CaseInsensitiveComparator > ScriptInfoList
Type for the list of scripts.
ClientSettings _settings_client
The current settings for this game.
bool StrEqualsIgnoreCase(const std::string_view str1, const std::string_view str2)
Compares two string( view)s for equality, while ignoring the case of the characters.
GUISettings gui
settings related to the GUI
Container for all important information about a piece of content.
uint32_t unique_id
Unique ID; either GRF ID or shortname.
MD5Hash md5sum
The MD5 checksum.
bool ai_developer_tools
activate AI/GS developer tools
Helper for creating a MD5sum of all files within of a script.
MD5Hash md5sum
The final md5sum.
bool AddFile(const std::string &filename, size_t, const std::string &) override
Add a file with the given filename.
Subdirectory dir
The directory to look in.
ScriptFileChecksumCreator(Subdirectory dir)
Initialise the md5sum to be all zeroes, so we can easily xor the data.