Skip to content

Commit 0ec3482

Browse files
authored
Update favorite maps to use SHA1 instead of map name (CnCNet#839)
1 parent 61cdd9c commit 0ec3482

File tree

5 files changed

+91
-14
lines changed

5 files changed

+91
-14
lines changed

ClientCore/Settings/UserINISettings.cs

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -318,14 +318,15 @@ public int GetValue(string section, string key, int defaultValue)
318318
public bool IsGameFollowed(string gameName)
319319
=> SettingsIni.GetBooleanValue("Channels", gameName, false);
320320

321-
public bool ToggleFavoriteMap(string mapName, string gameModeName, bool isFavorite)
321+
public bool ToggleFavoriteMap(string mapSHA1, string gameModeName, bool isFavorite)
322322
{
323-
if (string.IsNullOrEmpty(mapName))
323+
if (string.IsNullOrEmpty(mapSHA1))
324324
return isFavorite;
325325

326-
string favoriteMapKey = FavoriteMapKey(mapName, gameModeName);
327-
isFavorite = IsFavoriteMap(mapName, gameModeName);
328-
if (isFavorite)
326+
string favoriteMapKey = FavoriteMapKey(mapSHA1, gameModeName);
327+
328+
bool isCurrentlyFavorite = FavoriteMaps.Contains(favoriteMapKey);
329+
if (isCurrentlyFavorite)
329330
FavoriteMaps.Remove(favoriteMapKey);
330331
else
331332
FavoriteMaps.Add(favoriteMapKey);
@@ -334,7 +335,7 @@ public bool ToggleFavoriteMap(string mapName, string gameModeName, bool isFavori
334335

335336
WriteFavoriteMaps();
336337

337-
return !isFavorite;
338+
return !isCurrentlyFavorite;
338339
}
339340

340341
private void LoadFavoriteMaps(IniFile iniFile)
@@ -349,7 +350,7 @@ private void LoadFavoriteMaps(IniFile iniFile)
349350
WriteFavoriteMaps();
350351
}
351352

352-
private void WriteFavoriteMaps()
353+
public void WriteFavoriteMaps()
353354
{
354355
var favoriteMapsSection = SettingsIni.GetOrAddSection(FAVORITE_MAPS);
355356
favoriteMapsSection.RemoveAllKeys();
@@ -361,12 +362,41 @@ private void WriteFavoriteMaps()
361362

362363
/// <summary>
363364
/// Checks if a specified map name and game mode name belongs to the favorite map list.
365+
/// Name-based favorites are migrated to SHA1.
364366
/// </summary>
365-
/// <param name="nameName">The name of the map.</param>
366-
/// <param name="gameModeName">The name of the game mode</param>
367-
public bool IsFavoriteMap(string nameName, string gameModeName) => FavoriteMaps.Contains(FavoriteMapKey(nameName, gameModeName));
367+
/// <param name="mapSHA1">The SHA1 hash of the map.</param>
368+
/// <param name="mapName">The name of the map.</param>
369+
/// <param name="gameModeName">The name of the game mode.</param>
370+
public bool IsFavoriteMap(string mapSHA1, string mapName, string gameModeName)
371+
{
372+
// SHA1-based lookup first
373+
if (!string.IsNullOrEmpty(mapSHA1) && FavoriteMaps.Contains(FavoriteMapKey(mapSHA1, gameModeName)))
374+
return true;
375+
376+
// Fallback to name-based
377+
string nameKey = FavoriteMapKey(mapName, gameModeName);
378+
if (FavoriteMaps.Contains(nameKey))
379+
{
380+
// Migrate to SHA1
381+
if (!string.IsNullOrEmpty(mapSHA1))
382+
{
383+
string sha1Key = FavoriteMapKey(mapSHA1, gameModeName);
384+
if (!FavoriteMaps.Contains(sha1Key))
385+
{
386+
FavoriteMaps.Add(sha1Key);
387+
WriteFavoriteMaps();
388+
}
389+
// Note: We don't remove the name-based entry here to allow other maps
390+
// with the same name to also migrate. The name-based entry will be
391+
// cleaned up when all maps with that name have been processed.
392+
}
393+
return true;
394+
}
395+
396+
return false;
397+
}
368398

369-
private string FavoriteMapKey(string nameName, string gameModeName) => $"{nameName}:{gameModeName}";
399+
private string FavoriteMapKey(string identifier, string gameModeName) => $"{identifier}:{gameModeName}";
370400

371401
public void ReloadSettings() => SettingsIni.Reload();
372402

DXMainClient/DXGUI/Multiplayer/GameLobby/GameLobbyBase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -783,7 +783,7 @@ protected virtual void ToggleFavoriteMap()
783783
{
784784
if (GameModeMap != null)
785785
{
786-
GameModeMap.IsFavorite = UserINISettings.Instance.ToggleFavoriteMap(Map.UntranslatedName, GameMode.Name, GameModeMap.IsFavorite);
786+
GameModeMap.IsFavorite = UserINISettings.Instance.ToggleFavoriteMap(Map.SHA1, GameMode.Name, GameModeMap.IsFavorite);
787787
MapPreviewBox.RefreshFavoriteBtn();
788788
}
789789
}

DXMainClient/DXGUI/Multiplayer/GameLobby/MapPreviewBox.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -523,7 +523,7 @@ private void UpdateMap()
523523

524524
public void RefreshFavoriteBtn()
525525
{
526-
bool isFav = UserINISettings.Instance.IsFavoriteMap(GameModeMap?.Map.UntranslatedName, GameModeMap?.GameMode.Name);
526+
bool isFav = UserINISettings.Instance.IsFavoriteMap(GameModeMap?.Map.SHA1, GameModeMap?.Map.UntranslatedName, GameModeMap?.GameMode.Name);
527527
var textureName = isFav ? "favActive.png" : "favInactive.png";
528528
var hoverTextureName = isFav ? "favActive_c.png" : "favInactive_c.png";
529529
var hoverTexture = AssetLoader.AssetExists(hoverTextureName) ? AssetLoader.LoadTexture(hoverTextureName) : null;

DXMainClient/Domain/Multiplayer/GameModeMapCollection.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ public class GameModeMapCollection : List<GameModeMap>
88
{
99
public GameModeMapCollection(IEnumerable<GameMode> gameModes) :
1010
base(gameModes.SelectMany(gm => gm.Maps.Select(map =>
11-
new GameModeMap(gm, map, UserINISettings.Instance.IsFavoriteMap(map.UntranslatedName, gm.Name)))).Distinct())
11+
new GameModeMap(gm, map, UserINISettings.Instance.IsFavoriteMap(map.SHA1, map.UntranslatedName, gm.Name)))).Distinct())
1212
{
1313
}
1414

DXMainClient/Domain/Multiplayer/MapLoader.cs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,9 @@ public void LoadMaps()
110110
GameModes.RemoveAll(g => g.Maps.Count < 1);
111111
GameModeMaps = new GameModeMapCollection(GameModes);
112112

113+
// Clean up any name-based favorite entries after migration (legacy: changed from name to sha1)
114+
CleanupMigratedFavorites();
115+
113116
MapLoadingComplete?.Invoke(this, EventArgs.Empty);
114117
}
115118

@@ -611,5 +614,49 @@ private void AddMapToGameModes(Map map, bool enableLogging)
611614
}
612615
}
613616
}
617+
618+
/// <summary>
619+
/// Removes any name-based favorite entries that have been successfully migrated to SHA1.
620+
/// This runs after all maps have been processed to ensure complete migration.
621+
/// </summary>
622+
private void CleanupMigratedFavorites()
623+
{
624+
var favoriteMaps = UserINISettings.Instance.FavoriteMaps;
625+
if (favoriteMaps == null || !favoriteMaps.Any())
626+
return;
627+
628+
var entriesToRemove = new List<string>();
629+
630+
foreach (string favoriteKey in favoriteMaps)
631+
{
632+
string[] parts = favoriteKey.Split(':');
633+
if (parts.Length != 2)
634+
continue;
635+
636+
string mapName = parts[0];
637+
string gameModeName = parts[1];
638+
639+
// Check if there's a corresponding SHA1-based entry for any map with this name
640+
var gameMode = GameModes.FirstOrDefault(gm => gm.Name == gameModeName);
641+
if (gameMode != null)
642+
{
643+
bool hasMigratedVersion = gameMode.Maps
644+
.Where(m => m.UntranslatedName == mapName)
645+
.Any(m => favoriteMaps.Contains($"{m.SHA1}:{gameModeName}"));
646+
647+
if (hasMigratedVersion)
648+
entriesToRemove.Add(favoriteKey);
649+
}
650+
}
651+
652+
// Remove the name-based entries
653+
if (entriesToRemove.Any())
654+
{
655+
foreach (string entry in entriesToRemove)
656+
favoriteMaps.Remove(entry);
657+
658+
UserINISettings.Instance.WriteFavoriteMaps();
659+
}
660+
}
614661
}
615662
}

0 commit comments

Comments
 (0)