Skip to content

Commit

Permalink
Change: Make max script memory a setting
Browse files Browse the repository at this point in the history
  • Loading branch information
nielsmh committed Apr 20, 2019
1 parent ee4364f commit f55b811
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 3 deletions.
3 changes: 3 additions & 0 deletions src/lang/english.txt
Expand Up @@ -1483,6 +1483,9 @@ STR_CONFIG_SETTING_AI_IN_MULTIPLAYER :Allow AIs in mu
STR_CONFIG_SETTING_AI_IN_MULTIPLAYER_HELPTEXT :Allow AI computer players to participate in multiplayer games
STR_CONFIG_SETTING_SCRIPT_MAX_OPCODES :#opcodes before scripts are suspended: {STRING2}
STR_CONFIG_SETTING_SCRIPT_MAX_OPCODES_HELPTEXT :Maximum number of computation steps that a script can take in one turn
STR_CONFIG_SETTING_SCRIPT_MAX_MEMORY :Max memory usage per script: {STRING2}
STR_CONFIG_SETTING_SCRIPT_MAX_MEMORY_HELPTEXT :How much memory a single script may consume before it's forcibly terminated
STR_CONFIG_SETTING_SCRIPT_MAX_MEMORY_VALUE :{COMMA} MiB

STR_CONFIG_SETTING_SERVINT_ISPERCENT :Service intervals are in percents: {STRING2}
STR_CONFIG_SETTING_SERVINT_ISPERCENT_HELPTEXT :Choose whether servicing of vehicles is triggered by the time passed since last service or by reliability dropping by a certain percentage of the maximum reliability
Expand Down
1 change: 1 addition & 0 deletions src/saveload/saveload.h
Expand Up @@ -298,6 +298,7 @@ enum SaveLoadVersion : uint16 {
SLV_ROADVEH_PATH_CACHE, ///< 211 PR#7261 Add path cache for road vehicles.
SLV_REMOVE_OPF, ///< 212 PR#7245 Remove OPF.
SLV_TREES_WATER_CLASS, ///< 213 PR#7405 WaterClass update for tree tiles.
SLV_SCRIPT_MEMLIMIT, ///< 214 PR#7516 Limit on AI/GS memory consumption.

SL_MAX_VERSION, ///< Highest possible saveload version
};
Expand Down
7 changes: 4 additions & 3 deletions src/script/squirrel.cpp
Expand Up @@ -10,16 +10,17 @@
/** @file squirrel.cpp the implementation of the Squirrel class. It handles all Squirrel-stuff and gives a nice API back to work with. */

#include <stdarg.h>
#include <map>
#include "../stdafx.h"
#include "../debug.h"
#include "squirrel_std.hpp"
#include "../fileio_func.h"
#include "../string_func.h"
#include "../settings_type.h"
#include <sqstdaux.h>
#include <../squirrel/sqpcheader.h>
#include <../squirrel/sqvm.h>
#include "../core/alloc_func.hpp"
#include <map>

#include "../safeguards.h"

Expand Down Expand Up @@ -104,8 +105,8 @@ struct ScriptAllocator {
ScriptAllocator()
{
this->allocated_size = 0;
this->allocation_limit = 0x40000000; // 1 GiB
/* TODO: make limit configurable */
this->allocation_limit = static_cast<size_t>(_settings_game.script.script_max_memory_megabytes) << 20;
if (this->allocation_limit == 0) this->allocation_limit = 0x8000000; // 128 MiB
}

~ScriptAllocator()
Expand Down
1 change: 1 addition & 0 deletions src/settings_gui.cpp
Expand Up @@ -1758,6 +1758,7 @@ static SettingsContainer &GetSettingsTree()
{
npc->Add(new SettingEntry("script.settings_profile"));
npc->Add(new SettingEntry("script.script_max_opcode_till_suspend"));
npc->Add(new SettingEntry("script.script_max_memory_megabytes"));
npc->Add(new SettingEntry("difficulty.competitor_speed"));
npc->Add(new SettingEntry("ai.ai_in_multiplayer"));
npc->Add(new SettingEntry("ai.ai_disable_veh_train"));
Expand Down
1 change: 1 addition & 0 deletions src/settings_type.h
Expand Up @@ -339,6 +339,7 @@ struct AISettings {
struct ScriptSettings {
uint8 settings_profile; ///< difficulty profile to set initial settings of scripts, esp. random AIs
uint32 script_max_opcode_till_suspend; ///< max opcode calls till scripts will suspend
uint32 script_max_memory_megabytes; ///< limit on memory a single script instance may have allocated
};

/** Settings related to the new pathfinder. */
Expand Down
15 changes: 15 additions & 0 deletions src/table/settings.ini
Expand Up @@ -1555,6 +1555,21 @@ strhelp = STR_CONFIG_SETTING_SCRIPT_MAX_OPCODES_HELPTEXT
strval = STR_JUST_COMMA
cat = SC_EXPERT
[SDT_VAR]
base = GameSettings
var = script.script_max_memory_megabytes
type = SLE_UINT32
from = SLV_SCRIPT_MEMLIMIT
guiflags = SGF_NEWGAME_ONLY
def = 128
min = 8
max = 8192
interval = 8
str = STR_CONFIG_SETTING_SCRIPT_MAX_MEMORY
strhelp = STR_CONFIG_SETTING_SCRIPT_MAX_MEMORY_HELPTEXT
strval = STR_CONFIG_SETTING_SCRIPT_MAX_MEMORY_VALUE
cat = SC_EXPERT
##
[SDT_VAR]
base = GameSettings
Expand Down

0 comments on commit f55b811

Please sign in to comment.