Add: company mode enforcement checks to functions with command or company access
Command functions are those that call ScriptObject::Command, and functions with company access are any that call ScriptObject::GetCompany. This is a bit over-protective, but having the check everywhere makes it easier to validate that no check is missing automatically instead of by review.
This commit is contained in:
@@ -32,6 +32,7 @@
|
||||
|
||||
/* static */ ScriptGroup::GroupID ScriptGroup::CreateGroup(ScriptVehicle::VehicleType vehicle_type, GroupID parent_group_id)
|
||||
{
|
||||
EnforceCompanyModeValid(GROUP_INVALID);
|
||||
if (!ScriptObject::Command<CMD_CREATE_GROUP>::Do(&ScriptInstance::DoCommandReturnGroupID, (::VehicleType)vehicle_type, parent_group_id)) return GROUP_INVALID;
|
||||
|
||||
/* In case of test-mode, we return GroupID 0 */
|
||||
@@ -40,6 +41,7 @@
|
||||
|
||||
/* static */ bool ScriptGroup::DeleteGroup(GroupID group_id)
|
||||
{
|
||||
EnforceCompanyModeValid(false);
|
||||
EnforcePrecondition(false, IsValidGroup(group_id));
|
||||
|
||||
return ScriptObject::Command<CMD_DELETE_GROUP>::Do(group_id);
|
||||
@@ -56,6 +58,7 @@
|
||||
{
|
||||
CCountedPtr<Text> counter(name);
|
||||
|
||||
EnforceCompanyModeValid(false);
|
||||
EnforcePrecondition(false, IsValidGroup(group_id));
|
||||
EnforcePrecondition(false, name != nullptr);
|
||||
const std::string &text = name->GetDecodedText();
|
||||
@@ -75,6 +78,7 @@
|
||||
|
||||
/* static */ bool ScriptGroup::SetParent(GroupID group_id, GroupID parent_group_id)
|
||||
{
|
||||
EnforceCompanyModeValid(false);
|
||||
EnforcePrecondition(false, IsValidGroup(group_id));
|
||||
EnforcePrecondition(false, IsValidGroup(parent_group_id));
|
||||
|
||||
@@ -91,6 +95,7 @@
|
||||
|
||||
/* static */ bool ScriptGroup::EnableAutoReplaceProtection(GroupID group_id, bool enable)
|
||||
{
|
||||
EnforceCompanyModeValid(false);
|
||||
EnforcePrecondition(false, IsValidGroup(group_id));
|
||||
|
||||
return ScriptObject::Command<CMD_SET_GROUP_FLAG>::Do(group_id, GroupFlags::GF_REPLACE_PROTECTION, enable, false);
|
||||
@@ -105,6 +110,7 @@
|
||||
|
||||
/* static */ SQInteger ScriptGroup::GetNumEngines(GroupID group_id, EngineID engine_id)
|
||||
{
|
||||
EnforceCompanyModeValid(-1);
|
||||
if (!IsValidGroup(group_id) && group_id != GROUP_DEFAULT && group_id != GROUP_ALL) return -1;
|
||||
|
||||
return GetGroupNumEngines(ScriptObject::GetCompany(), group_id, engine_id);
|
||||
@@ -112,6 +118,7 @@
|
||||
|
||||
/* static */ SQInteger ScriptGroup::GetNumVehicles(GroupID group_id, ScriptVehicle::VehicleType vehicle_type)
|
||||
{
|
||||
EnforceCompanyModeValid(-1);
|
||||
bool valid_group = IsValidGroup(group_id);
|
||||
if (!valid_group && group_id != GROUP_DEFAULT && group_id != GROUP_ALL) return -1;
|
||||
if (!valid_group && (vehicle_type < ScriptVehicle::VT_RAIL || vehicle_type > ScriptVehicle::VT_AIR)) return -1;
|
||||
@@ -121,6 +128,7 @@
|
||||
|
||||
/* static */ bool ScriptGroup::MoveVehicle(GroupID group_id, VehicleID vehicle_id)
|
||||
{
|
||||
EnforceCompanyModeValid(false);
|
||||
EnforcePrecondition(false, IsValidGroup(group_id) || group_id == GROUP_DEFAULT);
|
||||
EnforcePrecondition(false, ScriptVehicle::IsPrimaryVehicle(vehicle_id));
|
||||
|
||||
@@ -129,6 +137,7 @@
|
||||
|
||||
/* static */ bool ScriptGroup::EnableWagonRemoval(bool enable_removal)
|
||||
{
|
||||
EnforceCompanyModeValid(false);
|
||||
if (HasWagonRemoval() == enable_removal) return true;
|
||||
|
||||
return ScriptObject::Command<CMD_CHANGE_COMPANY_SETTING>::Do("company.renew_keep_length", enable_removal ? 1 : 0);
|
||||
@@ -136,11 +145,13 @@
|
||||
|
||||
/* static */ bool ScriptGroup::HasWagonRemoval()
|
||||
{
|
||||
EnforceCompanyModeValid(false);
|
||||
return ::Company::Get(ScriptObject::GetCompany())->settings.renew_keep_length;
|
||||
}
|
||||
|
||||
/* static */ bool ScriptGroup::SetAutoReplace(GroupID group_id, EngineID engine_id_old, EngineID engine_id_new)
|
||||
{
|
||||
EnforceCompanyModeValid(false);
|
||||
EnforcePrecondition(false, IsValidGroup(group_id) || group_id == GROUP_DEFAULT || group_id == GROUP_ALL);
|
||||
EnforcePrecondition(false, ScriptEngine::IsBuildable(engine_id_new));
|
||||
|
||||
@@ -149,6 +160,7 @@
|
||||
|
||||
/* static */ EngineID ScriptGroup::GetEngineReplacement(GroupID group_id, EngineID engine_id)
|
||||
{
|
||||
EnforceCompanyModeValid(::INVALID_ENGINE);
|
||||
if (!IsValidGroup(group_id) && group_id != GROUP_DEFAULT && group_id != GROUP_ALL) return ::INVALID_ENGINE;
|
||||
|
||||
return ::EngineReplacementForCompany(Company::Get(ScriptObject::GetCompany()), engine_id, group_id);
|
||||
@@ -156,6 +168,7 @@
|
||||
|
||||
/* static */ bool ScriptGroup::StopAutoReplace(GroupID group_id, EngineID engine_id)
|
||||
{
|
||||
EnforceCompanyModeValid(false);
|
||||
EnforcePrecondition(false, IsValidGroup(group_id) || group_id == GROUP_DEFAULT || group_id == GROUP_ALL);
|
||||
|
||||
return ScriptObject::Command<CMD_SET_AUTOREPLACE>::Do(group_id, engine_id, ::INVALID_ENGINE, false);
|
||||
@@ -206,6 +219,7 @@
|
||||
|
||||
/* static */ bool ScriptGroup::SetPrimaryColour(GroupID group_id, ScriptCompany::Colours colour)
|
||||
{
|
||||
EnforceCompanyModeValid(false);
|
||||
EnforcePrecondition(false, IsValidGroup(group_id));
|
||||
|
||||
return ScriptObject::Command<CMD_SET_GROUP_LIVERY>::Do(group_id, true, (::Colours)colour);
|
||||
@@ -213,6 +227,7 @@
|
||||
|
||||
/* static */ bool ScriptGroup::SetSecondaryColour(GroupID group_id, ScriptCompany::Colours colour)
|
||||
{
|
||||
EnforceCompanyModeValid(false);
|
||||
EnforcePrecondition(false, IsValidGroup(group_id));
|
||||
|
||||
return ScriptObject::Command<CMD_SET_GROUP_LIVERY>::Do(group_id, false, (::Colours)colour);
|
||||
|
Reference in New Issue
Block a user