Skip to content

Commit

Permalink
#352: Add custom map file watcher
Browse files Browse the repository at this point in the history
- Address review fedback
- Use safe path everywhere
- Remove maps if they are renamed and their file extension changes

Issue: #352
PR: #358
  • Loading branch information
alexlambson committed Oct 1, 2022
1 parent 96d0a2e commit 10e0e6c
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,7 @@ private void RollDiceCommand(string dieType)
/// <param name="mapName">Name of the map given as a parameter, without file extension.</param>
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);
Expand Down
36 changes: 26 additions & 10 deletions DXMainClient/Domain/Multiplayer/MapLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -23,9 +23,9 @@ public class MapLoader

/// <summary>
/// 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.
/// </summary>
public const string CustomMapsDirectory = CUSTOM_MAPS_DIRECTORY + "/";
public const string CustomMapsDirectory = CUSTOM_MAPS_DIRECTORY;

/// <summary>
/// List of game modes.
Expand Down Expand Up @@ -95,7 +95,7 @@ public void LoadMapsAsync()
/// </summary>
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
Expand Down Expand Up @@ -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)
Expand All @@ -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();
Expand All @@ -170,15 +172,29 @@ public void HandleCustomMapFolder_Deleted(object sender, FileSystemEventArgs e)
/// <param name="e"></param>
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);

Expand Down

0 comments on commit 10e0e6c

Please sign in to comment.