OpenTTD Source
12.0-beta2
|
Go to the documentation of this file.
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"
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);
47 ScriptScanner::ScriptScanner() :
59 void ScriptScanner::Initialize(
const char *name)
68 ScriptScanner::~ScriptScanner()
86 for (
const auto &item : this->
info_list) {
94 this->info_list.clear();
95 this->info_single_list.clear();
100 char script_original_name[1024];
104 char script_name[1024];
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()) {
126 Debug(script, 1,
"Registering two scripts with the same name and version");
129 Debug(script, 1,
"The first is taking precedence.");
140 if (this->
info_single_list.find(script_original_name) == this->info_single_list.end()) {
152 for (
const auto &item : list) {
173 memset(this->md5sum, 0,
sizeof(this->md5sum));
177 virtual bool AddFile(
const std::string &filename,
size_t basepath_length,
const std::string &tar_filename)
185 FILE *f =
FioFOpenFile(filename,
"rb", this->dir, &size);
186 if (f ==
nullptr)
return false;
189 while ((len = fread(buffer, 1, (size >
sizeof(buffer)) ?
sizeof(buffer) : size, f)) != 0 && size != 0) {
191 checksum.Append(buffer, len);
193 checksum.Finish(tmp_md5sum);
198 for (uint i = 0; i <
sizeof(md5sum); i++) this->md5sum[i] ^= tmp_md5sum[i];
216 for (
int j = 0; j < 4 && *str !=
'\0'; j++, str++)
id |= *str << (8 * j);
219 if (!md5sum)
return true;
223 TarList::iterator iter;
224 if (!tar_filename.empty() && (iter = _tar_list[dir].find(tar_filename)) != _tar_list[dir].end()) {
227 for (
const auto &tar : _tar_filelist[dir]) {
229 if (tar.second.tar_filename != iter->first)
continue;
232 const char *ext = strrchr(tar.first.c_str(),
'.');
233 if (ext ==
nullptr || strcasecmp(ext,
".nut") != 0)
continue;
235 checksum.
AddFile(tar.first, 0, tar_filename);
243 *strrchr(path, PATHSEPCHAR) =
'\0';
244 checksum.
Scan(
".nut", path);
252 for (
const auto &item : this->
info_list) {
253 if (
IsSameScript(ci, md5sum, item.second, this->GetDirectory()))
return true;
260 for (
const auto &item : this->
info_list) {
261 if (
IsSameScript(ci, md5sum, item.second, this->GetDirectory()))
return item.second->GetMainScript();
virtual void GetScriptName(ScriptInfo *info, char *name, const char *last)=0
Get the script name how to store the script in memory.
byte md5sum[16]
The final md5sum.
const char * GetMainScript() const
Get the filename of the main.nut script.
Helper for creating a MD5sum of all files within of a script.
bool strtolower(char *str)
Convert a given ASCII string to lowercase.
class Squirrel * engine
The engine we're scanning with.
ScriptFileChecksumCreator(Subdirectory dir)
Initialise the md5sum to be all zeroes, so we can easily xor the data.
std::string GetMainScript()
Get the current main script the ScanDir is currently tracking.
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.
virtual void RegisterAPI(class Squirrel *engine)=0
Register the API for this ScriptInfo.
ClientSettings _settings_client
The current settings for this game.
std::string tar_file
If, which tar file the script was in.
void Reset()
Completely reset the engine; start from scratch.
const std::string & GetErrorMessage() const
The error message associated with the fatal error.
std::string GetTarFile() const
Get the filename of the tar the script is in.
char * GetConsoleList(char *p, const char *last, bool newest_only) const
Get the list of registered scripts to print on the console.
const char * GetDescription() const
Get the description of the script.
virtual bool IsDeveloperOnly() const
Can this script be selected by developers only?
void RescanDir()
Rescan the script dir.
void ResetEngine()
Reset the engine to ensure a clean environment for further steps.
bool LoadScript(const char *script)
Load a script.
bool ai_developer_tools
activate AI developer tools
std::map< const char *, class ScriptInfo *, StringCompare > ScriptInfoList
A list that maps AI names to their AIInfo object.
virtual const char * GetFileName() const =0
Get the filename to scan for this type of script.
FILE * FioFOpenFile(const std::string &filename, const char *mode, Subdirectory subdir, size_t *filesize)
Opens a OpenTTD file somewhere in a personal or global directory.
Container for all important information about a piece of content.
bool AddFile(const std::string &filename, size_t basepath_length, const std::string &tar_filename) override
Add a file with the given filename.
void SetGlobalPointer(void *ptr)
Sets a pointer in the VM that is reachable from where ever you are in SQ.
const char * GetShortName() const
Get the 4 character long short name of the script.
byte md5sum[16]
The MD5 checksum.
Subdirectory subdir
The current sub directory we are searching through.
virtual bool AddFile(const std::string &filename, size_t basepath_length, const std::string &tar_filename)
Add a file with the given filename.
std::string main_script
The full path of the script.
bool HasScript(const struct ContentInfo *ci, bool md5sum)
Check whether we have a script with the exact characteristics as ci.
ScriptInfoList info_list
The list of all script.
bool FioCheckFileExists(const std::string &filename, Subdirectory subdir)
Check whether the given file exists.
virtual const char * GetScannerName() const =0
Get the type of the script, in plural.
void RegisterScript(class ScriptInfo *info)
Register a ScriptInfo to the scanner.
int GetVersion() const
Get the version of the script.
void Reset()
Reset all allocated lists.
virtual Subdirectory GetDirectory() const =0
Get the directory to scan in.
int CDECL seprintf(char *str, const char *last, const char *format,...)
Safer implementation of snprintf; same as snprintf except:
Subdirectory
The different kinds of subdirectories OpenTTD uses.
char * stredup(const char *s, const char *last)
Create a duplicate of the given string.
A throw-class that is given when the script made a fatal error.
uint32 unique_id
Unique ID; either GRF ID or shortname.
Subdirectory dir
The directory to look in.
#define Debug(name, level, format_string,...)
Ouptut a line of debugging information.
Helper for scanning for files with a given name.
const char * GetName() const
Get the Name of the script.
const char * FindMainScript(const ContentInfo *ci, bool md5sum)
Find a script of a ContentInfo.
char * strecpy(char *dst, const char *src, const char *last)
Copies characters from one buffer to another.
static void free(const void *ptr)
Version of the standard free that accepts const pointers.
All static information from an Script like name, version, etc.
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.
#define lastof(x)
Get the last element of an fixed size array.
ScriptInfoList info_single_list
The list of all unique script. The best script (highest version) is shown.
GUISettings gui
settings related to the GUI
void FioFCloseFile(FILE *f)
Close a file in a safe way.