uox3/source/scriptc.cpp
Xoduz 166d3b0119 Numerous changes with focus on feature and stability parity across Windows, Linux and MacOS platforms
Added new Makefile that handles compiling UOX3 and Spidermonkey on both Linux and MacOS (punt)
Added VS solution (SpiderMonkey.sln) and VC++ project files for compiling SpiderMonkey on Windows (Xuri)
Updated SpiderMonkey from v1.6.0 to v1.7.0
Added optimization flag -O2 for release build in CMakeLists.txt in project root (Xuri)
Added StringUtility file with general common string manipulation functions (punt)
Updated RandomNum function to use a "seedless" random number generator from C++11 instead of the old C rand() function. (punt)
Overall code cleanup to remove/replace platform-dependent code (punt)
Replaced potentially unsafe usage of C string stuff like sprintf, vsprintf, vsnprintf, strncat, strcpy and strlen throughout the code with calls to a format() function containing only a single, safe use of vsnprintf, ensuring there's a single place of failure if there's anything to fix and and making it easy to potentially replace this with std::format from C++20 when the time comes. (punt)
Replaced usage of char in many places with std::string (punt)
Started process of replacing UString usage with functions provided through StringUtility instead (punt)
Replaced platform specific fileIO handling in Windows/Linux with cross-platform C++17 standard std::filesystem (punt)
Replaced platform specific time handling by using cross-platform chrono library (punt)
Removed UOX namespace to reduce complexity (punt)
Elimitated template code approach to singletons for more modern c++ constructs, removing dependices in the process (punt)
Removed ODBC support; it hasn't been touched since the initial implementation in 2008, and there is a lack of someone to maintain the code. (punt)
Removed some platform specific files like uoxlinux.h, which is no longer required (punt)
Removed legacy crash protection code, and removed support for cluox (punt)
Removed legacy "support" for Borland compiler (punt)
Removed legacy VC++ 6 Workspace/Project files, VS2005 Solution/Project files, as these are no longer supported (Xuri)
Removed legacy BUILD folder with outdated compilation instructions (Xuri)
Removed old Changelog file (merged into Changelog.txt) (Xuri)
Fixed a pointer bug for items in multis on world load (punt)
Fixed a dictionary related issue that caused segmentation faults on Linux/MacOS (punt)
Fixed an issue with callbacks to JS scripts that caused segmentation faults on Linux/MacOS (punt)
Paths in uox.ini should now load properly on all platforms regardless of whether those paths use slashes or backslashes, and whether or not they end in a slash/backslash. (punt)
Moved to c++17 style of threading, and got rid of threadsafeobject.cpp/h (punt)
Replaced parsing of UOX.INI tags with a system that's more easily maintainable (punt)
Added some new commands to js/commands/custom/misc-cmd.js (Xuri)
	cont		// Targeted item will be made a container, set to nondecay and movable 2
	endfight	// Targeted character (and character being fought) will stop fighting
	getmulti	// Get multiObject for targeted item
	finditem	// Find item at layer X
	movespeed	// Set movement speed of target player (0x0 Normal, 0x1 Mounted, 0x2 Slow (walk only), 0x3 Hybrid ("jog"?), 0x4 Frozen)
Added new HTML based documentation in docs folder, and removed many old legacy docs that are either incorporated into this document already or no longer relevant for the current UOX3 version (Xuri)

Co-Authored-By: Charles Kerr <punt1959@users.noreply.github.com>
2020-09-07 18:09:55 +08:00

224 lines
8.9 KiB
C++

//
// Cache script section locations
// Reads through the contents of the file and saves location of each
// SECTION XYZ entry
// Calling Script::find() will then seek to that location directly rather
// than having to parse through all of the script
//
#include "uox3.h"
#include "ssection.h"
#include "scriptc.h"
#include "StringUtility.hpp"
//o-----------------------------------------------------------------------------------------------o
//| Function - bool get_modification_date( const std::string& filename, time_t* mod_time )
//o-----------------------------------------------------------------------------------------------o
//| Purpose - Returns true if the file's stats can be found (testing its existence)
//o-----------------------------------------------------------------------------------------------o
bool get_modification_date( const std::string& filename, time_t* mod_time )
{
struct stat stat_buf;
if( stat( filename.c_str(), &stat_buf ) )
return false;
*mod_time = stat_buf.st_mtime;
return true;
}
//o-----------------------------------------------------------------------------------------------o
//| Function - void reload( bool disp )
//o-----------------------------------------------------------------------------------------------o
//| Purpose - Reload's the scripts data. If disp is true, then information is output to
//| the console (# of sections). Will not run if the script is in an erroneous state
//o-----------------------------------------------------------------------------------------------o
void Script::reload( bool disp )
{
if( !errorState )
{
// Clear the map, we are starting from scratch;
deleteMap();
char line[2048];
input.open( filename.c_str(), std::ios_base::in );
if( input.is_open() )
{
UString sLine;
SI32 count = 0;
while( !input.eof() && !input.fail() )
{
input.getline( line, 2048 );
sLine = line;
sLine = sLine.removeComment().stripWhiteSpace();
if( !sLine.empty() )
{
// We have some real data
// see if in a section
if( sLine.substr( 0, 1 ) == "[" && sLine.substr( sLine.size() - 1 ) == "]" )
{
// Ok a section is starting here, get the name
UString sectionname = sLine.substr( 1, sLine.size() - 2 );
sectionname = sectionname.simplifyWhiteSpace().upper();
// Now why we look for a {, no idea, but we do - Because we want to make sure that were IN a block not before the block. At least this makes sure that were inside the {}'s of a block...
while( !input.eof() && sLine.substr( 0, 1 ) != "{" && !input.fail() )
{
input.getline( line, 2048 );
sLine = line;
sLine = sLine.removeComment().stripWhiteSpace();
}
// We are finally in the actual section!
// We waited until now to create it, incase a total invalid file
lastModTime = 0;
defEntries[sectionname] = new ScriptSection( input, dfnCat );
++count;
}
}
}
input.close();
}
else
{
std::cerr<< "Cannot open "<< filename<<": "<< std::string(strerror( errno )) <<std::endl;
errorState = true;
}
}
if( disp ){
Console.print( format("Reloading %-15s: ", filename.c_str()) );
}
fflush( stdout );
}
//o-----------------------------------------------------------------------------------------------o
//| Function - Script( const std::string& _filename, DEFINITIONCATEGORIES d, bool disp ) : errorState( false ), dfnCat( d )
//o-----------------------------------------------------------------------------------------------o
//| Purpose - Builds the script, reading in the information from the script file.
//o-----------------------------------------------------------------------------------------------o
Script::Script( const std::string& _filename, DEFINITIONCATEGORIES d, bool disp ) : errorState( false ), dfnCat( d )
{
filename = _filename;
if( !get_modification_date( filename, &last_modification ) )
{
std::cerr<< "Cannot open "<< filename<< ": "<< std::string(strerror( errno )) << std::endl;;
errorState = true;
}
reload( disp );
}
//o-----------------------------------------------------------------------------------------------o
//| Function - ~Script()
//o-----------------------------------------------------------------------------------------------o
//| Purpose - Destroys any memory that has been allocated
//o-----------------------------------------------------------------------------------------------o
Script::~Script()
{
deleteMap();
}
//o-----------------------------------------------------------------------------------------------o
//| Function - bool isin( const std::string& section )
//o-----------------------------------------------------------------------------------------------o
//| Purpose - Returns true if the section named section is in the script
//o-----------------------------------------------------------------------------------------------o
bool Script::isin( const std::string& section )
{
UString temp( section );
SSMAP::const_iterator iSearch = defEntries.find( temp.upper() );
if( iSearch != defEntries.end() )
return true;
return false;
}
//o-----------------------------------------------------------------------------------------------o
//| Function - ScriptSection *FindEntry( const std::string& section )
//o-----------------------------------------------------------------------------------------------o
//| Purpose - Returns a ScriptSection * to the section named "section"
//| if it exists, otherwise returning NULL
//o-----------------------------------------------------------------------------------------------o
ScriptSection *Script::FindEntry( const std::string& section )
{
ScriptSection *rvalue = NULL;
SSMAP::const_iterator iSearch = defEntries.find( section );
if( iSearch != defEntries.end() )
rvalue = iSearch->second;
return rvalue;
}
//o-----------------------------------------------------------------------------------------------o
//| Function - ScriptSection *FindEntrySubStr( const std::string& section )
//o-----------------------------------------------------------------------------------------------o
//| Purpose - Find the first ScriptSection * (if any) that has the
//| string section in the section name
//o-----------------------------------------------------------------------------------------------o
ScriptSection *Script::FindEntrySubStr( const std::string& section )
{
ScriptSection *rvalue = NULL;
auto usection = std::string( section );
usection = str_toupper(usection);
for( SSMAP::const_iterator iSearch = defEntries.begin(); iSearch != defEntries.end(); ++iSearch )
{
if( iSearch->first.find( usection ) != std::string::npos ) // FOUND IT!
{
rvalue = iSearch->second;
break;
}
}
return rvalue;
}
//o-----------------------------------------------------------------------------------------------o
//| Function - ScriptSection *FirstEntry( void )
//o-----------------------------------------------------------------------------------------------o
//| Purpose - Returns the first ScriptSection in the Script (if any)
//o-----------------------------------------------------------------------------------------------o
ScriptSection *Script::FirstEntry( void )
{
ScriptSection *rvalue = NULL;
iSearch = defEntries.begin();
if( iSearch != defEntries.end() )
rvalue = iSearch->second;
return rvalue;
}
//o-----------------------------------------------------------------------------------------------o
//| Function - ScriptSection *NextEntry( void )
//o-----------------------------------------------------------------------------------------------o
//| Purpose - Returns the next ScriptSection (if any) in the Script
//o-----------------------------------------------------------------------------------------------o
ScriptSection *Script::NextEntry( void )
{
ScriptSection *rvalue = NULL;
if( iSearch != defEntries.end() )
{
++iSearch;
if( iSearch != defEntries.end() )
rvalue = iSearch->second;
}
return rvalue;
}
//o-----------------------------------------------------------------------------------------------o
//| Function - deleteMap( void )
//o-----------------------------------------------------------------------------------------------o
//| Purpose - Destroys any memory that has been allocated
//o-----------------------------------------------------------------------------------------------o
void Script::deleteMap( void )
{
for( SSMAP::const_iterator iTest = defEntries.begin(); iTest != defEntries.end(); ++iTest )
delete iTest->second;
defEntries.clear();
}
//o-----------------------------------------------------------------------------------------------o
//| Function - std::string EntryName( void )
//o-----------------------------------------------------------------------------------------------o
//| Purpose - Returns the section name for the current entry (if any)
//o-----------------------------------------------------------------------------------------------o
std::string Script::EntryName( void )
{
std::string rvalue;
if( iSearch != defEntries.end() )
rvalue = iSearch->first;
return rvalue;
}