10 #include "../stdafx.h" 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" 33 if (tar_filename !=
nullptr) {
35 if (this->
tar_file ==
nullptr)
return false;
57 DEBUG(script, 0,
"Fatal error '%s' when trying to load the script '%s'.", e.
GetErrorMessage(), filename);
63 ScriptScanner::ScriptScanner() :
77 void ScriptScanner::Initialize(
const char *name)
86 ScriptScanner::~ScriptScanner()
106 ScriptInfoList::iterator it = this->
info_list.begin();
107 for (; it != this->
info_list.end(); it++) {
122 char script_original_name[1024];
126 char script_name[1024];
131 DEBUG(script, 0,
"The script '%s' returned a string from GetShortName() which is not four characaters. Unable to load the script.", info->
GetName());
148 DEBUG(script, 1,
"Registering two scripts with the same name and version");
151 DEBUG(script, 1,
"The first is taking precedence.");
174 ScriptInfoList::const_iterator it = list.begin();
175 for (; it != list.end(); it++) {
196 memset(this->md5sum, 0,
sizeof(this->md5sum));
200 virtual bool AddFile(
const char *filename,
size_t basepath_length,
const char *tar_filename)
208 FILE *f =
FioFOpenFile(filename,
"rb", this->dir, &size);
209 if (f ==
nullptr)
return false;
212 while ((len = fread(buffer, 1, (size >
sizeof(buffer)) ?
sizeof(buffer) : size, f)) != 0 && size != 0) {
214 checksum.Append(buffer, len);
216 checksum.Finish(tmp_md5sum);
221 for (uint i = 0; i <
sizeof(md5sum); i++) this->md5sum[i] ^= tmp_md5sum[i];
239 for (
int j = 0; j < 4 && *str !=
'\0'; j++, str++)
id |= *str << (8 * j);
242 if (!md5sum)
return true;
245 const char *tar_filename = info->
GetTarFile();
246 TarList::iterator iter;
247 if (tar_filename !=
nullptr && (iter = _tar_list[dir].find(tar_filename)) != _tar_list[dir].end()) {
250 TarFileList::iterator tar;
251 FOR_ALL_TARS(tar, dir) {
253 if (tar->second.tar_filename != iter->first)
continue;
256 const char *ext = strrchr(tar->first.c_str(),
'.');
257 if (ext ==
nullptr || strcasecmp(ext,
".nut") != 0)
continue;
259 checksum.
AddFile(tar->first.c_str(), 0, tar_filename);
267 *strrchr(path, PATHSEPCHAR) =
'\0';
268 checksum.
Scan(
".nut", path);
276 for (ScriptInfoList::iterator it = this->
info_list.begin(); it != this->
info_list.end(); it++) {
277 if (
IsSameScript(ci, md5sum, (*it).second, this->GetDirectory()))
return true;
284 for (ScriptInfoList::iterator it = this->
info_list.begin(); it != this->
info_list.end(); it++) {
285 if (
IsSameScript(ci, md5sum, (*it).second, this->GetDirectory()))
return (*it).second->GetMainScript();
int GetVersion() const
Get the version of the script.
const char * GetMainScript()
Get the current main script the ScanDir is currently tracking.
bool AddFile(const char *filename, size_t basepath_length, const char *tar_filename) override
Add a file with the given filename.
uint32 unique_id
Unique ID; either GRF ID or shortname.
virtual void RegisterAPI(class Squirrel *engine)=0
Register the API for this ScriptInfo.
Subdirectory
The different kinds of subdirectories OpenTTD uses.
void FioFCloseFile(FILE *f)
Close a file in a safe way.
int CDECL seprintf(char *str, const char *last, const char *format,...)
Safer implementation of snprintf; same as snprintf except:
uint Scan(const char *extension, Subdirectory sd, bool tars=true, bool recursive=true)
Scan for files with the given extension in the given search path.
const char * GetName() const
Get the Name of the script.
std::map< const char *, class ScriptInfo *, StringCompare > ScriptInfoList
A list that maps AI names to their AIInfo object.
The definition of Script_FatalError.
const char * GetShortName() const
Get the 4 character long short name of the script.
A throw-class that is given when the script made a fatal error.
#define lastof(x)
Get the last element of an fixed size array.
bool strtolower(char *str)
Convert a given ASCII string to lowercase.
char * tar_file
If, which tar file the script was in.
Helper for scanning for files with a given name.
const char * GetErrorMessage()
The error message associated with the fatal error.
Helper for creating a MD5sum of all files within of a script.
const char * GetMainScript() const
Get the filename of the main.nut script.
bool HasScript(const struct ContentInfo *ci, bool md5sum)
Check whether we have a script with the exact characteristics as ci.
void RegisterScript(class ScriptInfo *info)
Register a ScriptInfo to the scanner.
Subdirectory dir
The directory to look in.
virtual Subdirectory GetDirectory() const =0
Get the directory to scan in.
All static information from an Script like name, version, etc.
ClientSettings _settings_client
The current settings for this game.
FILE * FioFOpenFile(const char *filename, const char *mode, Subdirectory subdir, size_t *filesize)
Opens a OpenTTD file somewhere in a personal or global directory.
char * main_script
The full path of the script.
virtual bool AddFile(const char *filename, size_t basepath_length, const char *tar_filename)
Add a file with the given filename.
virtual const char * GetFileName() const =0
Get the filename to scan for this type of script.
char * stredup(const char *s, const char *last)
Create a duplicate of the given string.
virtual void GetScriptName(ScriptInfo *info, char *name, const char *last)=0
Get the script name how to store the script in memory.
ScriptInfoList info_list
The list of all script.
bool ai_developer_tools
activate AI developer tools
bool FioCheckFileExists(const char *filename, Subdirectory subdir)
Check whether the given file exists.
byte md5sum[16]
The MD5 checksum.
void RescanDir()
Rescan the script dir.
const char * GetDescription() const
Get the description of the script.
#define DEBUG(name, level,...)
Output a line of debugging information.
Subdirectory subdir
The current sub directory we are searching through.
GUISettings gui
settings related to the GUI
void Reset()
Completely reset the engine; start from scratch.
void SetGlobalPointer(void *ptr)
Sets a pointer in the VM that is reachable from where ever you are in SQ.
void Reset()
Reset all allocated lists.
const char * FindMainScript(const ContentInfo *ci, bool md5sum)
Find a script of a ContentInfo.
Declarations of the class for the script scanner.
char * GetConsoleList(char *p, const char *last, bool newest_only) const
Get the list of registered scripts to print on the console.
ScriptFileChecksumCreator(Subdirectory dir)
Initialise the md5sum to be all zeroes, so we can easily xor the data.
char * strecpy(char *dst, const char *src, const char *last)
Copies characters from one buffer to another.
void ResetEngine()
Reset the engine to ensure a clean environment for further steps.
ScriptInfoList info_single_list
The list of all unique script. The best script (highest version) is shown.
static void free(const void *ptr)
Version of the standard free that accepts const pointers.
bool LoadScript(const char *script)
Load a script.
const char * GetTarFile() const
Get the filename of the tar the script is in.
virtual bool IsDeveloperOnly() const
Can this script be selected by developers only?
byte md5sum[16]
The final md5sum.
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...
Container for all important information about a piece of content.
virtual const char * GetScannerName() const =0
Get the type of the script, in plural.
class Squirrel * engine
The engine we're scanning with.
ScriptInfo keeps track of all information of a script, like Author, Description, ...