diff --git a/DMCompiler/Compiler/CompilerError.cs b/DMCompiler/Compiler/CompilerError.cs index dad0930c91..2209b23747 100644 --- a/DMCompiler/Compiler/CompilerError.cs +++ b/DMCompiler/Compiler/CompilerError.cs @@ -33,6 +33,7 @@ public enum WarningCode { FileAlreadyIncluded = 1000, MissingIncludedFile = 1001, InvalidWarningCode = 1002, + InvalidFileDirDefine = 1003, MisplacedDirective = 1100, UndefineMissingDirective = 1101, DefinedMissingParen = 1150, diff --git a/DMCompiler/Compiler/DMPreprocessor/DMPreprocessor.cs b/DMCompiler/Compiler/DMPreprocessor/DMPreprocessor.cs index de4ad91b44..82b9d0c09e 100644 --- a/DMCompiler/Compiler/DMPreprocessor/DMPreprocessor.cs +++ b/DMCompiler/Compiler/DMPreprocessor/DMPreprocessor.cs @@ -304,7 +304,7 @@ private void HandleDefineDirective(Token defineToken) { DMPreprocessorLexer currentLexer = _lexerStack.Peek(); string dir = Path.Combine(currentLexer.IncludeDirectory, dirTokenValue); - compiler.AddResourceDirectory(dir); + compiler.AddResourceDirectory(dir, dirToken.Location); // In BYOND it goes on to set the FILE_DIR macro's value to the added directory // I don't see any reason to do that diff --git a/DMCompiler/DMCompiler.cs b/DMCompiler/DMCompiler.cs index 25f6e9a5f3..3168999d35 100644 --- a/DMCompiler/DMCompiler.cs +++ b/DMCompiler/DMCompiler.cs @@ -21,12 +21,13 @@ namespace DMCompiler; public class DMCompiler { public int ErrorCount; public int WarningCount; - public HashSet UniqueEmissions = new(); + public readonly HashSet UniqueEmissions = new(); public DMCompilerSettings Settings; public IReadOnlyList ResourceDirectories => _resourceDirectories; private readonly DMCompilerConfiguration Config = new(); private readonly List _resourceDirectories = new(); + private string _codeDirectory; private DateTime _compileStartTime; internal readonly DMCodeTree DMCodeTree; @@ -97,8 +98,13 @@ public bool Compile(DMCompilerSettings settings) { return successfulCompile; } - public void AddResourceDirectory(string dir) { + public void AddResourceDirectory(string dir, Location loc) { dir = dir.Replace('\\', Path.DirectorySeparatorChar); + if (!Directory.Exists(dir)) { + Emit(WarningCode.InvalidFileDirDefine, loc, + $"Folder \"{Path.GetRelativePath(_codeDirectory, dir)}\" does not exist"); + return; + } _resourceDirectories.Add(dir); } @@ -129,7 +135,10 @@ public void AddResourceDirectory(string dir) { } // Adds the root of the DM project to FILE_DIR - compiler.AddResourceDirectory(Path.GetDirectoryName(files[0]) ?? "/"); + _codeDirectory = Path.GetDirectoryName(files[0]) ?? ""; + if (string.IsNullOrWhiteSpace(_codeDirectory)) + _codeDirectory = Path.GetFullPath("."); + compiler.AddResourceDirectory(_codeDirectory, Location.Internal); string compilerDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) ?? string.Empty; string dmStandardDirectory = Path.Join(compilerDirectory, "DMStandard"); diff --git a/DMCompiler/DMStandard/DefaultPragmaConfig.dm b/DMCompiler/DMStandard/DefaultPragmaConfig.dm index 103b84ab59..125a11f244 100644 --- a/DMCompiler/DMStandard/DefaultPragmaConfig.dm +++ b/DMCompiler/DMStandard/DefaultPragmaConfig.dm @@ -5,6 +5,7 @@ #pragma FileAlreadyIncluded warning #pragma MissingIncludedFile error #pragma InvalidWarningCode warning +#pragma InvalidFileDirDefine warning #pragma MisplacedDirective error #pragma UndefineMissingDirective warning #pragma DefinedMissingParen error