diff --git a/src/Zilf/Interpreter/Context.cs b/src/Zilf/Interpreter/Context.cs index 019304075df1693c49214fd9c147dbc53b60cce8..ba16e2c6e903f561132035d777c7c31cf12df5e0 100644 --- a/src/Zilf/Interpreter/Context.cs +++ b/src/Zilf/Interpreter/Context.cs @@ -708,30 +708,59 @@ namespace Zilf.Interpreter public void HandleError(ZilErrorBase ex) => DiagnosticManager.Handle(ex.Diagnostic ?? throw new ArgumentException("Missing diagnostic", nameof(ex))); + private static readonly string[] IncludeFileExtensions = { ".zil", ".mud", ".ZIL", ".MUD" }; + /// The file wasn't found in any include path. public string FindIncludeFile(string name) { - // ReSharper disable once ForeachCanBePartlyConvertedToQueryUsingAnotherGetEnumerator foreach (var path in IncludePaths) { - var combined = Path.Combine(path, name); + foreach (var nameVariant in GetIncludeFileNameVariants(name)) + { + var combined = Path.Combine(path, nameVariant); - if (FileExists(combined)) - return combined; + if (FileExists(combined)) + return combined; + } + } - if (!string.IsNullOrEmpty(Path.GetExtension(combined))) - continue; + throw new FileNotFoundException(); + } + + [SuppressMessage("Globalization", "CA1308:Normalize strings to uppercase", + Justification = "This method looks for a lowercase filename when the source specifies an uppercase filename.")] + private static IEnumerable GetIncludeFileNameVariants(string name) + { + // try the name as-is + yield return name; - combined = Path.ChangeExtension(combined, ".zil"); - if (FileExists(combined)) - return combined; + // try adding a .zil or .mud extension + bool hasExtension = !string.IsNullOrEmpty(Path.GetExtension(name)); - combined = Path.ChangeExtension(combined, ".mud"); - if (FileExists(combined)) - return combined; + if (!hasExtension) + { + foreach (var ext in IncludeFileExtensions) + { + yield return Path.ChangeExtension(name, ext); + } } - throw new FileNotFoundException(); + // try lowercasing the name + var lower = name.ToLowerInvariant(); + + if (lower != name) + { + yield return lower; + + // try adding a .zil or .mud extension + if (!hasExtension) + { + foreach (var ext in IncludeFileExtensions) + { + yield return Path.ChangeExtension(lower, ext); + } + } + } } ///