From 10e0e6c7ac2510ff583c4db626a2ef0c25402da2 Mon Sep 17 00:00:00 2001 From: Alex Lambson Date: Fri, 30 Sep 2022 19:58:41 -0600 Subject: [PATCH] #352: Add custom map file watcher - Address review fedback - Use safe path everywhere - Remove maps if they are renamed and their file extension changes Issue: https://github.com/CnCNet/xna-cncnet-client/issues/352 PR: https://github.com/CnCNet/xna-cncnet-client/pull/358/ --- .../Multiplayer/GameLobby/CnCNetGameLobby.cs | 2 +- .../GameLobby/MultiplayerGameLobby.cs | 2 +- DXMainClient/Domain/Multiplayer/MapLoader.cs | 36 +++++++++++++------ 3 files changed, 28 insertions(+), 12 deletions(-) diff --git a/DXMainClient/DXGUI/Multiplayer/GameLobby/CnCNetGameLobby.cs b/DXMainClient/DXGUI/Multiplayer/GameLobby/CnCNetGameLobby.cs index a196147ca..76f23210e 100644 --- a/DXMainClient/DXGUI/Multiplayer/GameLobby/CnCNetGameLobby.cs +++ b/DXMainClient/DXGUI/Multiplayer/GameLobby/CnCNetGameLobby.cs @@ -1595,7 +1595,7 @@ private void MapSharer_HandleMapDownloadComplete(SHA1EventArgs e) { string mapFileName = MapSharer.GetMapFileName(e.SHA1, e.MapName); Logger.Log("Map " + mapFileName + " downloaded, parsing."); - string mapPath = MapLoader.CustomMapsDirectory + mapFileName; + string mapPath = SafePath.CombineFilePath(MapLoader.CustomMapsDirectory, mapFileName); Map map = MapLoader.LoadCustomMap(mapPath, out string returnMessage); if (map != null) { diff --git a/DXMainClient/DXGUI/Multiplayer/GameLobby/MultiplayerGameLobby.cs b/DXMainClient/DXGUI/Multiplayer/GameLobby/MultiplayerGameLobby.cs index de248a390..0bd28dc62 100644 --- a/DXMainClient/DXGUI/Multiplayer/GameLobby/MultiplayerGameLobby.cs +++ b/DXMainClient/DXGUI/Multiplayer/GameLobby/MultiplayerGameLobby.cs @@ -487,7 +487,7 @@ private void RollDiceCommand(string dieType) /// Name of the map given as a parameter, without file extension. private void LoadCustomMap(string mapName) { - Map map = MapLoader.LoadCustomMap($"{MapLoader.CustomMapsDirectory}{mapName}", out string resultMessage); + Map map = MapLoader.LoadCustomMap(SafePath.CombineFilePath(MapLoader.CustomMapsDirectory, mapName), out string resultMessage); if (map != null) { AddNotice(resultMessage); diff --git a/DXMainClient/Domain/Multiplayer/MapLoader.cs b/DXMainClient/Domain/Multiplayer/MapLoader.cs index 588c72c21..db19aa6c3 100644 --- a/DXMainClient/Domain/Multiplayer/MapLoader.cs +++ b/DXMainClient/Domain/Multiplayer/MapLoader.cs @@ -14,7 +14,7 @@ namespace DTAClient.Domain.Multiplayer public class MapLoader { public const string MAP_FILE_EXTENSION = ".map"; - private const string CUSTOM_MAPS_DIRECTORY = "Maps/Custom"; + private const string CUSTOM_MAPS_DIRECTORY = "Maps\\Custom"; private static readonly string CUSTOM_MAPS_CACHE = SafePath.CombineFilePath(ProgramConstants.ClientUserFilesPath, "custom_map_cache"); private const string MultiMapsSection = "MultiMaps"; private const string GameModesSection = "GameModes"; @@ -23,9 +23,9 @@ public class MapLoader /// /// The relative path to the folder where custom maps are stored. - /// This is the public version of CUSTOM_MAPS_DIRECTORY ending in a slash for convenience. + /// This is the public version of CUSTOM_MAPS_DIRECTORY. /// - public const string CustomMapsDirectory = CUSTOM_MAPS_DIRECTORY + "/"; + public const string CustomMapsDirectory = CUSTOM_MAPS_DIRECTORY; /// /// List of game modes. @@ -95,7 +95,7 @@ public void LoadMapsAsync() /// public void StartCustomMapFileWatcher() { - customMapFileWatcher = new FileSystemWatcher($"{ProgramConstants.GamePath}{CustomMapsDirectory}"); + customMapFileWatcher = new FileSystemWatcher(SafePath.CombineDirectoryPath(ProgramConstants.GamePath, CustomMapsDirectory)); customMapFileWatcher.Filter = $"*{MAP_FILE_EXTENSION}"; customMapFileWatcher.NotifyFilter = NotifyFilters.Attributes @@ -127,8 +127,8 @@ public void HandleCustomMapFolder_Created(object sender, FileSystemEventArgs e) { // Get the map filename without the extension. // The extension gets added in LoadCustomMap so we need to excise it to avoid "file.map.map". - string name = e.Name.EndsWith(MAP_FILE_EXTENSION) ? e.Name.Remove(e.Name.Length - MAP_FILE_EXTENSION.Length) : e.Name; - string relativeMapPath = $"{CustomMapsDirectory}{name}"; + string name = Path.GetFileNameWithoutExtension(e.Name); + string relativeMapPath = SafePath.CombineFilePath(CustomMapsDirectory, name); Map map = LoadCustomMap(relativeMapPath, out string result); if (map == null) @@ -147,11 +147,13 @@ public void HandleCustomMapFolder_Created(object sender, FileSystemEventArgs e) public void HandleCustomMapFolder_Deleted(object sender, FileSystemEventArgs e) { Logger.Log($"Map was deleted: map={e.Name}"); + // Use the filename without the extension so we can remove maps that had their extension changed. + string name = Path.GetFileNameWithoutExtension(e.Name); // The way we're detecting the loaded map is hacky, but we don't // have the sha1 to work with. foreach (GameMode gameMode in GameModes) { - gameMode.Maps.RemoveAll(map => map.CompleteFilePath.EndsWith(e.Name)); + gameMode.Maps.RemoveAll(map => Path.GetFileNameWithoutExtension(map.CompleteFilePath).EndsWith(name)); } RemoveEmptyGameModesAndUpdateGameModeMaps(); @@ -170,15 +172,29 @@ public void HandleCustomMapFolder_Deleted(object sender, FileSystemEventArgs e) /// public void HandleCustomMapFolder_Renamed(object sender, RenamedEventArgs e) { - string name = e.Name.EndsWith(MAP_FILE_EXTENSION) ? e.Name.Remove(e.Name.Length - MAP_FILE_EXTENSION.Length) : e.Name; - string relativeMapPath = $"{CustomMapsDirectory}{name}"; + string name = Path.GetFileNameWithoutExtension(e.Name); + string relativeMapPath = SafePath.CombineFilePath(CustomMapsDirectory, name); + bool oldPathIsMap = Path.GetExtension(e.OldName) == MAP_FILE_EXTENSION; + bool newPathIsMap = Path.GetExtension(e.Name) == MAP_FILE_EXTENSION; // Check if the user is renaming a non ".map" file. // This is just for logging to help debug. - if (!e.OldName.EndsWith(MAP_FILE_EXTENSION)) + if (!oldPathIsMap && newPathIsMap) { Logger.Log($"Renaming file changed the file extension. User is likely renaming a '.yrm' from Final Alert 2: old={e.OldName}, new={e.Name}"); } + else if (oldPathIsMap && !newPathIsMap) + { + // A bit hacky, but this is a rare case. + Logger.Log($"Renaming file changed the file extension to no longer be '.map' for some reason, removing from map list: old={e.OldName}, new={e.Name}"); + HandleCustomMapFolder_Deleted(sender, e); + } + + if (!newPathIsMap) + { + Logger.Log($"Renaming file. New extension is not '{MAP_FILE_EXTENSION}', moving on: file={e.Name}"); + return; + } Map map = LoadCustomMap(relativeMapPath, out string result);