12 #include "../stdafx.h" 
   14 #include "../saveload/saveload.h" 
   16 #include "../script/squirrel_class.hpp" 
   23 #include "api/script_controller.hpp" 
   24 #include "api/script_error.hpp" 
   25 #include "api/script_event.hpp" 
   26 #include "api/script_log.hpp" 
   28 #include "../company_base.h" 
   29 #include "../company_func.h" 
   30 #include "../fileio_func.h" 
   32 #include "../safeguards.h" 
   34 ScriptStorage::~ScriptStorage()
 
   37   if (
event_data != NULL) ScriptEventController::FreeEventPointer();
 
   38   if (
log_data != NULL) ScriptLog::FreeLogPointer();
 
   46 static void PrintFunc(
bool error_msg, 
const SQChar *message)
 
   49   ScriptController::Print(error_msg, message);
 
   60   is_save_data_on_stack(false),
 
   72   ScriptObject::ActiveInstance active(
this);
 
   74   this->
controller = 
new ScriptController(company);
 
   81     ScriptObject::SetAllowDoCommand(
false);
 
   83     if (strcmp(main_script, 
"%_dummy") == 0) {
 
   86       if (this->
engine->
IsSuspended()) ScriptLog::Error(
"This script took too long to load script. AI is not started.");
 
   92     this->
instance = MallocT<SQObject>(1);
 
   97     ScriptObject::SetAllowDoCommand(
true);
 
  114   char script_name[32];
 
  115   seprintf(script_name, 
lastof(script_name), 
"compat_%s.nut", api_version);
 
  119     FioAppendDirectory(buf, 
lastof(buf), sp, dir);
 
  125     ScriptLog::Error(
"Failed to load API compatibility script");
 
  126     DEBUG(script, 0, 
"Error compiling / running API compatibility script: %s", buf);
 
  130   ScriptLog::Warning(
"API compatibility script not found");
 
  134 ScriptInstance::~ScriptInstance()
 
  136   ScriptObject::ActiveInstance active(
this);
 
  153   DEBUG(script, 0, 
"The script died unexpectedly.");
 
  164   ScriptObject::ActiveInstance active(
this);
 
  166   if (this->
IsDead()) 
return;
 
  177   if (--this->
suspend > 0)  
return;          
 
  202       ScriptObject::SetAllowDoCommand(
false);
 
  206           if (this->
engine->
IsSuspended()) ScriptLog::Error(
"This script took too long to initialize. Script is not started.");
 
  212         if (this->
engine->
IsSuspended()) ScriptLog::Error(
"This script took too long in the Load function. Script is not started.");
 
  216       ScriptObject::SetAllowDoCommand(
true);
 
  258   instance->
engine->InsertResult(ScriptObject::GetLastCommandRes());
 
  263   instance->
engine->InsertResult(ScriptObject::GetNewVehicleID());
 
  268   instance->
engine->InsertResult(ScriptObject::GetNewSignID());
 
  273   instance->
engine->InsertResult(ScriptObject::GetNewGroupID());
 
  278   instance->
engine->InsertResult(ScriptObject::GetNewGoalID());
 
  283   instance->
engine->InsertResult(ScriptObject::GetNewStoryPageID());
 
  288   instance->
engine->InsertResult(ScriptObject::GetNewStoryPageElementID());
 
  298   ScriptObject::ActiveInstance active(
this);
 
  339   SLEG_VAR(_script_sl_byte, SLE_UINT8),
 
  345   if (max_depth == 0) {
 
  346     ScriptLog::Error(
"Savedata can only be nested to 25 deep. No data saved."); 
 
  350   switch (sq_gettype(vm, index)) {
 
  357       sq_getinteger(vm, index, &res);
 
  359         int value = (int)res;
 
  371       sq_getstring(vm, index, &buf);
 
  372       size_t len = strlen(buf) + 1;
 
  374         ScriptLog::Error(
"Maximum string length is 254 chars. No data saved.");
 
  378         _script_sl_byte = (byte)len;
 
  380         SlArray(const_cast<char *>(buf), len, SLE_CHAR);
 
  391       while (SQ_SUCCEEDED(sq_next(vm, index - 1))) {
 
  393         bool res = 
SaveObject(vm, -1, max_depth - 1, test);
 
  414       while (SQ_SUCCEEDED(sq_next(vm, index - 1))) {
 
  437       sq_getbool(vm, index, &res);
 
  439         _script_sl_byte = res ? 1 : 0;
 
  454       ScriptLog::Error(
"You tried to save an unsupported type. No data saved.");
 
  467   ScriptObject::ActiveInstance active(
this);
 
  487     bool backup_allow = ScriptObject::GetAllowDoCommand();
 
  488     ScriptObject::SetAllowDoCommand(
false);
 
  510     ScriptObject::SetAllowDoCommand(backup_allow);
 
  512     if (!sq_istable(savedata)) {
 
  513       ScriptLog::Error(this->
engine->
IsSuspended() ? 
"This script took too long to Save." : 
"Save function should return a table.");
 
  518     sq_pushobject(vm, savedata);
 
  529     ScriptLog::Warning(
"Save function is not implemented");
 
  557   switch (_script_sl_byte) {
 
  561       if (vm != NULL) sq_pushinteger(vm, (SQInteger)value);
 
  567       static char buf[256];
 
  568       SlArray(buf, _script_sl_byte, SLE_CHAR);
 
  569       if (vm != NULL) sq_pushstring(vm, buf, -1);
 
  574       if (vm != NULL) sq_newarray(vm, 0);
 
  576         if (vm != NULL) sq_arrayappend(vm, -2);
 
  583       if (vm != NULL) sq_newtable(vm);
 
  586         if (vm != NULL) sq_rawset(vm, -3);
 
  594       if (vm != NULL) sq_pushbool(vm, (SQBool)(_script_sl_byte != 0));
 
  599       if (vm != NULL) sq_pushnull(vm);
 
  607     default: NOT_REACHED();
 
  615   if (_script_sl_byte == 0) 
return;
 
  622   ScriptObject::ActiveInstance active(
this);
 
  624   if (this->
engine == NULL || version == -1) {
 
  632   if (_script_sl_byte == 0) 
return;
 
  634   sq_pushinteger(vm, version);
 
  648     ScriptLog::Warning(
"Loading failed: there was data for the script to load, but the script does not have a Load() function.");
 
  658   sq_pushstring(vm, 
"Load", -1);
 
  669   if (SQ_FAILED(sq_call(vm, 3, SQFalse, SQFalse, 
MAX_SL_OPS))) 
return false;
 
  683   ScriptObject::ActiveInstance active(
this);
 
  685   ScriptObject::SetLastCommandRes(result.
Succeeded());
 
  688     ScriptObject::SetLastError(ScriptError::StringToError(result.
GetErrorMessage()));
 
  690     ScriptObject::IncreaseDoCommandCosts(result.
GetCost());
 
  691     ScriptObject::SetLastCost(result.
GetCost());
 
  697   ScriptObject::ActiveInstance active(
this);