Skip to content

Commit dddfee1

Browse files
committed
CnCNet#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: CnCNet#352 PR: CnCNet#358
1 parent 96d0a2e commit dddfee1

File tree

3 files changed

+28
-12
lines changed

3 files changed

+28
-12
lines changed

DXMainClient/DXGUI/Multiplayer/GameLobby/CnCNetGameLobby.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1595,7 +1595,7 @@ private void MapSharer_HandleMapDownloadComplete(SHA1EventArgs e)
15951595
{
15961596
string mapFileName = MapSharer.GetMapFileName(e.SHA1, e.MapName);
15971597
Logger.Log("Map " + mapFileName + " downloaded, parsing.");
1598-
string mapPath = MapLoader.CustomMapsDirectory + mapFileName;
1598+
string mapPath = SafePath.CombineFilePath(MapLoader.CustomMapsDirectory, mapFileName);
15991599
Map map = MapLoader.LoadCustomMap(mapPath, out string returnMessage);
16001600
if (map != null)
16011601
{

DXMainClient/DXGUI/Multiplayer/GameLobby/MultiplayerGameLobby.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -487,7 +487,7 @@ private void RollDiceCommand(string dieType)
487487
/// <param name="mapName">Name of the map given as a parameter, without file extension.</param>
488488
private void LoadCustomMap(string mapName)
489489
{
490-
Map map = MapLoader.LoadCustomMap($"{MapLoader.CustomMapsDirectory}{mapName}", out string resultMessage);
490+
Map map = MapLoader.LoadCustomMap(SafePath.CombineFilePath(MapLoader.CustomMapsDirectory, mapName), out string resultMessage);
491491
if (map != null)
492492
{
493493
AddNotice(resultMessage);

DXMainClient/Domain/Multiplayer/MapLoader.cs

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ namespace DTAClient.Domain.Multiplayer
1414
public class MapLoader
1515
{
1616
public const string MAP_FILE_EXTENSION = ".map";
17-
private const string CUSTOM_MAPS_DIRECTORY = "Maps/Custom";
17+
private const string CUSTOM_MAPS_DIRECTORY = "Maps\\Custom";
1818
private static readonly string CUSTOM_MAPS_CACHE = SafePath.CombineFilePath(ProgramConstants.ClientUserFilesPath, "custom_map_cache");
1919
private const string MultiMapsSection = "MultiMaps";
2020
private const string GameModesSection = "GameModes";
@@ -23,9 +23,9 @@ public class MapLoader
2323

2424
/// <summary>
2525
/// The relative path to the folder where custom maps are stored.
26-
/// This is the public version of CUSTOM_MAPS_DIRECTORY ending in a slash for convenience.
26+
/// This is the public version of CUSTOM_MAPS_DIRECTORY.
2727
/// </summary>
28-
public const string CustomMapsDirectory = CUSTOM_MAPS_DIRECTORY + "/";
28+
public const string CustomMapsDirectory = CUSTOM_MAPS_DIRECTORY;
2929

3030
/// <summary>
3131
/// List of game modes.
@@ -95,7 +95,7 @@ public void LoadMapsAsync()
9595
/// </summary>
9696
public void StartCustomMapFileWatcher()
9797
{
98-
customMapFileWatcher = new FileSystemWatcher($"{ProgramConstants.GamePath}{CustomMapsDirectory}");
98+
customMapFileWatcher = new FileSystemWatcher(SafePath.CombineDirectoryPath(ProgramConstants.GamePath, CustomMapsDirectory));
9999

100100
customMapFileWatcher.Filter = $"*{MAP_FILE_EXTENSION}";
101101
customMapFileWatcher.NotifyFilter = NotifyFilters.Attributes
@@ -127,8 +127,8 @@ public void HandleCustomMapFolder_Created(object sender, FileSystemEventArgs e)
127127
{
128128
// Get the map filename without the extension.
129129
// The extension gets added in LoadCustomMap so we need to excise it to avoid "file.map.map".
130-
string name = e.Name.EndsWith(MAP_FILE_EXTENSION) ? e.Name.Remove(e.Name.Length - MAP_FILE_EXTENSION.Length) : e.Name;
131-
string relativeMapPath = $"{CustomMapsDirectory}{name}";
130+
string name = Path.GetFileNameWithoutExtension(e.Name);
131+
string relativeMapPath = SafePath.CombineFilePath(CustomMapsDirectory, name);
132132
Map map = LoadCustomMap(relativeMapPath, out string result);
133133

134134
if (map == null)
@@ -149,9 +149,11 @@ public void HandleCustomMapFolder_Deleted(object sender, FileSystemEventArgs e)
149149
Logger.Log($"Map was deleted: map={e.Name}");
150150
// The way we're detecting the loaded map is hacky, but we don't
151151
// have the sha1 to work with.
152+
string name = Path.GetFileNameWithoutExtension(e.Name);
152153
foreach (GameMode gameMode in GameModes)
153154
{
154-
gameMode.Maps.RemoveAll(map => map.CompleteFilePath.EndsWith(e.Name));
155+
string test = Path.GetFileNameWithoutExtension(gameMode.Name);
156+
gameMode.Maps.RemoveAll(map => Path.GetFileNameWithoutExtension(map.CompleteFilePath).EndsWith(name));
155157
}
156158

157159
RemoveEmptyGameModesAndUpdateGameModeMaps();
@@ -170,15 +172,29 @@ public void HandleCustomMapFolder_Deleted(object sender, FileSystemEventArgs e)
170172
/// <param name="e"></param>
171173
public void HandleCustomMapFolder_Renamed(object sender, RenamedEventArgs e)
172174
{
173-
string name = e.Name.EndsWith(MAP_FILE_EXTENSION) ? e.Name.Remove(e.Name.Length - MAP_FILE_EXTENSION.Length) : e.Name;
174-
string relativeMapPath = $"{CustomMapsDirectory}{name}";
175+
string name = Path.GetFileNameWithoutExtension(e.Name);
176+
string relativeMapPath = SafePath.CombineFilePath(CustomMapsDirectory, name);
177+
bool oldPathIsMap = Path.GetExtension(e.OldName) == MAP_FILE_EXTENSION;
178+
bool newPathIsMap = Path.GetExtension(e.Name) == MAP_FILE_EXTENSION;
175179

176180
// Check if the user is renaming a non ".map" file.
177181
// This is just for logging to help debug.
178-
if (!e.OldName.EndsWith(MAP_FILE_EXTENSION))
182+
if (!oldPathIsMap && newPathIsMap)
179183
{
180184
Logger.Log($"Renaming file changed the file extension. User is likely renaming a '.yrm' from Final Alert 2: old={e.OldName}, new={e.Name}");
181185
}
186+
else if (oldPathIsMap && !newPathIsMap)
187+
{
188+
// A bit hacky, but this is a rare case.
189+
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}");
190+
HandleCustomMapFolder_Deleted(sender, e);
191+
}
192+
193+
if (!newPathIsMap)
194+
{
195+
Logger.Log($"Renaming file. New extension is not '{MAP_FILE_EXTENSION}', moving on: file={e.Name}");
196+
return;
197+
}
182198

183199
Map map = LoadCustomMap(relativeMapPath, out string result);
184200

0 commit comments

Comments
 (0)