12 #include "../stdafx.h" 14 #include "../string_func.h" 15 #include "../settings_type.h" 17 #include "../script/squirrel.hpp" 21 #if defined(ENABLE_NETWORK) 22 #include "../network/network_content.h" 23 #include "../3rdparty/md5/md5.h" 24 #include "../tar_type.h" 27 #include "../safeguards.h" 36 if (tar_filename != NULL) {
38 if (this->
tar_file == NULL)
return false;
62 ScriptScanner::ScriptScanner() :
76 void ScriptScanner::Initialize(
const char *name)
85 ScriptScanner::~ScriptScanner()
105 ScriptInfoList::iterator it = this->
info_list.begin();
106 for (; it != this->
info_list.end(); it++) {
121 char script_original_name[1024];
125 char script_name[1024];
130 DEBUG(script, 0,
"The script '%s' returned a string from GetShortName() which is not four characaters. Unable to load the script.", info->
GetName());
147 DEBUG(script, 1,
"Registering two scripts with the same name and version");
150 DEBUG(script, 1,
"The first is taking precedence.");
173 ScriptInfoList::const_iterator it = list.begin();
174 for (; it != list.end(); it++) {
183 #if defined(ENABLE_NETWORK) 197 memset(this->md5sum, 0,
sizeof(this->md5sum));
201 virtual bool AddFile(
const char *filename,
size_t basepath_length,
const char *tar_filename)
209 FILE *f =
FioFOpenFile(filename,
"rb", this->dir, &size);
210 if (f == NULL)
return false;
213 while ((len = fread(buffer, 1, (size >
sizeof(buffer)) ?
sizeof(buffer) : size, f)) != 0 && size != 0) {
215 checksum.Append(buffer, len);
217 checksum.Finish(tmp_md5sum);
222 for (uint i = 0; i <
sizeof(md5sum); i++) this->md5sum[i] ^= tmp_md5sum[i];
240 for (
int j = 0; j < 4 && *str !=
'\0'; j++, str++)
id |= *str << (8 * j);
243 if (!md5sum)
return true;
246 const char *tar_filename = info->
GetTarFile();
247 TarList::iterator iter;
248 if (tar_filename != NULL && (iter = _tar_list[dir].find(tar_filename)) != _tar_list[dir].end()) {
251 TarFileList::iterator tar;
252 FOR_ALL_TARS(tar, dir) {
254 if (tar->second.tar_filename != iter->first)
continue;
257 const char *ext = strrchr(tar->first.c_str(),
'.');
258 if (ext == NULL || strcasecmp(ext,
".nut") != 0)
continue;
260 checksum.
AddFile(tar->first.c_str(), 0, tar_filename);
268 *strrchr(path, PATHSEPCHAR) =
'\0';
269 checksum.
Scan(
".nut", path);
277 for (ScriptInfoList::iterator it = this->
info_list.begin(); it != this->
info_list.end(); it++) {
278 if (
IsSameScript(ci, md5sum, (*it).second, this->GetDirectory()))
return true;
285 for (ScriptInfoList::iterator it = this->
info_list.begin(); it != this->
info_list.end(); it++) {
286 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.
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.
const char * GetShortName() const
Get the 4 character long short name of the script.
#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.
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.
bool AddFile(const char *filename, size_t basepath_length, const char *tar_filename)
Add a file with the given filename.
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, ...