diff --git a/Source/Tools/Flax.Build/Bindings/BindingsGenerator.Cache.cs b/Source/Tools/Flax.Build/Bindings/BindingsGenerator.Cache.cs index d1aba9162..72896f186 100644 --- a/Source/Tools/Flax.Build/Bindings/BindingsGenerator.Cache.cs +++ b/Source/Tools/Flax.Build/Bindings/BindingsGenerator.Cache.cs @@ -253,7 +253,7 @@ namespace Flax.Build.Bindings { // Version writer.Write(CacheVersion); - writer.Write(File.GetLastWriteTime(Assembly.GetExecutingAssembly().Location).Ticks); + writer.Write(FileCache.GetLastWriteTime(Assembly.GetExecutingAssembly().Location).Ticks); // Build options writer.Write(moduleOptions.IntermediateFolder); @@ -270,7 +270,7 @@ namespace Flax.Build.Bindings { var headerFile = headerFiles[i]; writer.Write(headerFile); - writer.Write(File.GetLastWriteTime(headerFile).Ticks); + writer.Write(FileCache.GetLastWriteTime(headerFile).Ticks); } // Info @@ -281,7 +281,7 @@ namespace Flax.Build.Bindings private static bool LoadCache(ref ModuleInfo moduleInfo, BuildOptions moduleOptions, List headerFiles) { var path = GetCachePath(moduleInfo.Module, moduleOptions); - if (!File.Exists(path)) + if (!FileCache.Exists(path)) return false; try { @@ -292,7 +292,7 @@ namespace Flax.Build.Bindings var version = reader.ReadInt32(); if (version != CacheVersion) return false; - if (File.GetLastWriteTime(Assembly.GetExecutingAssembly().Location).Ticks != reader.ReadInt64()) + if (FileCache.GetLastWriteTime(Assembly.GetExecutingAssembly().Location).Ticks != reader.ReadInt64()) return false; // Build options @@ -320,7 +320,7 @@ namespace Flax.Build.Bindings var headerFile = headerFiles[i]; if (headerFile != reader.ReadString()) return false; - if (File.GetLastWriteTime(headerFile).Ticks > reader.ReadInt64()) + if (FileCache.GetLastWriteTime(headerFile).Ticks > reader.ReadInt64()) return false; } diff --git a/Source/Tools/Flax.Build/Build/FileCache.cs b/Source/Tools/Flax.Build/Build/FileCache.cs new file mode 100644 index 000000000..a11b53415 --- /dev/null +++ b/Source/Tools/Flax.Build/Build/FileCache.cs @@ -0,0 +1,41 @@ +using System; +using System.Collections.Generic; +using System.IO; + +namespace Flax.Build +{ + /// + /// Cache filesystem related queries like File.Exists and File.GetLastWriteTime. + /// + public static class FileCache + { + private static Dictionary fileInfoCache = new Dictionary(); + + public static void FileRemoveFromCache(string path) + { + //fileInfoCache[path].Refresh(); + fileInfoCache.Remove(path); + } + + public static bool Exists(string path) + { + if (fileInfoCache.TryGetValue(path, out var fileInfo)) + return fileInfo.Exists; + + fileInfo = new FileInfo(path); + fileInfoCache.Add(path, fileInfo); + return fileInfo.Exists; + } + + public static DateTime GetLastWriteTime(string path) + { + + if (fileInfoCache.TryGetValue(path, out var fileInfo)) + return fileInfo.LastWriteTime; + + fileInfo = new FileInfo(path); + fileInfoCache.Add(path, fileInfo); + return fileInfo.LastWriteTime; + } + } +} diff --git a/Source/Tools/Flax.Build/Build/NativeCpp/IncludesCache.cs b/Source/Tools/Flax.Build/Build/NativeCpp/IncludesCache.cs index eecd1479f..ec0138883 100644 --- a/Source/Tools/Flax.Build/Build/NativeCpp/IncludesCache.cs +++ b/Source/Tools/Flax.Build/Build/NativeCpp/IncludesCache.cs @@ -17,8 +17,6 @@ namespace Flax.Build.NativeCpp private static Dictionary AllIncludesCache = new(); private static Dictionary DirectIncludesTimestampCache = new(); private static Dictionary AllIncludesTimestampCache = new(); - private static Dictionary FileExistsCache = new(); - private static Dictionary FileTimestampCache = new(); private static readonly string IncludeToken = "include"; private static string CachePath; @@ -141,26 +139,6 @@ namespace Flax.Build.NativeCpp } } - private static bool FileExists(string path) - { - if (FileExistsCache.TryGetValue(path, out bool result)) - return result; - - result = File.Exists(path); - FileExistsCache.Add(path, result); - return result; - } - - private static DateTime FileLastWriteTime(string path) - { - if (FileTimestampCache.TryGetValue(path, out DateTime result)) - return result; - - result = File.GetLastWriteTime(path); - FileTimestampCache.Add(path, result); - return result; - } - /// /// Finds all included files by the source file (including dependencies). /// @@ -176,7 +154,7 @@ namespace Flax.Build.NativeCpp { if (AllIncludesTimestampCache.TryGetValue(sourceFile, out var cachedTimestamp)) { - lastModified = FileLastWriteTime(sourceFile); + lastModified = FileCache.GetLastWriteTime(sourceFile); if (lastModified == cachedTimestamp) return result; } @@ -185,7 +163,7 @@ namespace Flax.Build.NativeCpp AllIncludesTimestampCache.Remove(sourceFile); } - if (!FileExists(sourceFile)) + if (!FileCache.Exists(sourceFile)) throw new Exception(string.Format("Cannot scan file \"{0}\" for includes because it does not exist.", sourceFile)); //using (new ProfileEventScope("FindAllIncludedFiles")) @@ -231,7 +209,7 @@ namespace Flax.Build.NativeCpp private static string[] GetDirectIncludes(string sourceFile) { - if (!FileExists(sourceFile)) + if (!FileCache.Exists(sourceFile)) return Array.Empty(); DateTime? lastModified = null; @@ -241,7 +219,7 @@ namespace Flax.Build.NativeCpp { if (DirectIncludesTimestampCache.TryGetValue(sourceFile, out var cachedTimestamp)) { - lastModified = FileLastWriteTime(sourceFile); + lastModified = FileCache.GetLastWriteTime(sourceFile); if (lastModified == cachedTimestamp) return result; } @@ -325,11 +303,11 @@ namespace Flax.Build.NativeCpp // Relative to the workspace root var includedFilePath = Path.Combine(Globals.Root, "Source", includedFile); - if (!FileExists(includedFilePath)) + if (!FileCache.Exists(includedFilePath)) { // Relative to the source file includedFilePath = Path.Combine(sourceFileFolder, includedFile); - if (!FileExists(includedFilePath)) + if (!FileCache.Exists(includedFilePath)) { // Relative to any of the included project workspaces var project = Globals.Project; @@ -337,7 +315,7 @@ namespace Flax.Build.NativeCpp foreach (var reference in project.References) { includedFilePath = Path.Combine(reference.Project.ProjectFolderPath, "Source", includedFile); - if (FileExists(includedFilePath)) + if (FileCache.Exists(includedFilePath)) { isValid = true; break; @@ -348,7 +326,7 @@ namespace Flax.Build.NativeCpp if (!isValid && isLibraryInclude) { includedFilePath = Path.Combine(Globals.Root, "Source", "ThirdParty", includedFile); - if (FileExists(includedFilePath)) + if (FileCache.Exists(includedFilePath)) { isValid = true; } @@ -375,7 +353,7 @@ namespace Flax.Build.NativeCpp result = includedFiles.ToArray(); DirectIncludesCache.Add(sourceFile, result); if (!DirectIncludesTimestampCache.ContainsKey(sourceFile)) - DirectIncludesTimestampCache.Add(sourceFile, lastModified ?? FileLastWriteTime(sourceFile)); + DirectIncludesTimestampCache.Add(sourceFile, lastModified ?? FileCache.GetLastWriteTime(sourceFile)); return result; } }