From d1ab8fc161d33b035b8eec775ed62dec6f055ea8 Mon Sep 17 00:00:00 2001 From: wixoa Date: Wed, 5 Feb 2025 19:02:21 -0500 Subject: [PATCH] Disallow var defines on /world and /list (#2206) --- DMCompiler/DM/DMCodeTree.Vars.cs | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/DMCompiler/DM/DMCodeTree.Vars.cs b/DMCompiler/DM/DMCodeTree.Vars.cs index f7ff73d79c..bc8f31e653 100644 --- a/DMCompiler/DM/DMCodeTree.Vars.cs +++ b/DMCompiler/DM/DMCodeTree.Vars.cs @@ -105,7 +105,7 @@ public override bool TryDefineVar(DMCompiler compiler, int pass) { if (!compiler.DMObjectTree.TryGetDMObject(owner, out var dmObject)) return false; - if (AlreadyExists(compiler, dmObject)) { + if (CheckCantDefine(compiler, dmObject)) { _defined = true; return true; } @@ -162,12 +162,23 @@ private bool HandleInstanceVar(DMCompiler compiler, DMObject dmObject) { return true; } - private bool AlreadyExists(DMCompiler compiler, DMObject dmObject) { - // "type" and "tag" can only be defined in DMStandard - if (VarName is "type" or "tag" && !varDef.Location.InDMStandard) { - compiler.Emit(WarningCode.InvalidVarDefinition, varDef.Location, - $"Cannot redefine built-in var \"{VarName}\""); - return true; + private bool CheckCantDefine(DMCompiler compiler, DMObject dmObject) { + if (!compiler.Settings.NoStandard) { + var inStandard = varDef.Location.InDMStandard; + + // "type" and "tag" can only be defined in DMStandard + if (VarName is "type" or "tag" && !inStandard) { + compiler.Emit(WarningCode.InvalidVarDefinition, varDef.Location, + $"Cannot redefine built-in var \"{VarName}\""); + return true; + } + + // Vars on /world and /list can only be defined in DMStandard + if ((dmObject.Path == DreamPath.World || dmObject.Path == DreamPath.List) && !inStandard) { + compiler.Emit(WarningCode.InvalidVarDefinition, varDef.Location, + $"Cannot define a var on type {dmObject.Path}"); + return true; + } } //DMObjects store two bundles of variables; the statics in GlobalVariables and the non-statics in Variables.