Skip to content

Commit

Permalink
More profile cleaning null checks
Browse files Browse the repository at this point in the history
  • Loading branch information
doombubbles committed Apr 13, 2022
1 parent 7335e55 commit c5cfb7e
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions BloonsTD6 Mod Helper/Api/ProfileManagement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@ internal class ProfileManagement
private static string primaryHero;
private static readonly Dictionary<string, string> MapPrimaryHeroes = new Dictionary<string, string>();

private static readonly Dictionary<(string, int), string> MapPlayerHeroes =
new Dictionary<(string, int), string>();
private static readonly Dictionary<(string, int), string> MapPlayerHeroes = new Dictionary<(string, int), string>();

private static readonly Dictionary<string, string> SelectedTowerSkinData = new Dictionary<string, string>();

Expand All @@ -45,7 +44,7 @@ private static void CleanProfile(ProfileModel profile, IReadOnlyCollection<strin
MelonMain.PerformHook(mod => mod.PreCleanProfile(profile));

CleanHashSet(profile.unlockedTowers, Clean("unlockedTower", towers, current), UnlockedTowers);
CleanDictionary(profile.analyticsKonFuze.towersPlacedByBaseName,
CleanDictionary(profile.analyticsKonFuze?.towersPlacedByBaseName,
Clean("towerPlacedByBaseName", towers.Concat(heroes).ToList(), current), TowersPlacedByBaseName);
CleanDictionary(profile.towerXp, Clean("towerXp", towers, current), TowerXp);

Expand All @@ -59,13 +58,13 @@ private static void CleanProfile(ProfileModel profile, IReadOnlyCollection<strin
SeenNewHeroNotification);
CleanDictionary(profile.selectedTowerSkinData, Clean("selectedTowerSkinData", heroes, current),
SelectedTowerSkinData);
CleanDictionary(profile.analyticsKonFuze.heroesPlacedByName, Clean("heroPlacedByName", heroes, current),
CleanDictionary(profile.analyticsKonFuze?.heroesPlacedByName, Clean("heroPlacedByName", heroes, current),
HeroesPlacedByName);
CleanDictionary(profile.analyticsKonFuze.heroLevelsByName, Clean("heroLevelsByName", heroes, current),
CleanDictionary(profile.analyticsKonFuze?.heroLevelsByName, Clean("heroLevelsByName", heroes, current),
HeroLevelsByName);

SeenEvents.Clear();
profile.seenEvents.RemoveWhere(new Func<string, bool>(s =>
profile.seenEvents?.RemoveWhere(new Func<string, bool>(s =>
{
foreach (var paragonEvent in ParagonEvents)
{
Expand Down Expand Up @@ -236,7 +235,7 @@ internal static void UnCleanProfile(ProfileModel profile)

foreach (var ((map, player), hero) in MapPlayerHeroes)
{
if (profile.savedMaps.ContainsKey(map))
if (profile.savedMaps?.ContainsKey(map) == true)
{
var mapSaveDataModel = profile.savedMaps[map];
if (mapSaveDataModel.players.ContainsKey(player))
Expand All @@ -253,7 +252,7 @@ private static Func<string, bool> Clean(string name, IReadOnlyCollection<string>
{
return thing =>
{
if (string.IsNullOrEmpty(thing))
if (string.IsNullOrEmpty(thing) || things == null)
{
return false;
}
Expand All @@ -272,6 +271,10 @@ private static void CleanHashSet(Il2CppSystem.Collections.Generic.HashSet<string
Func<string, bool> clean, HashSet<string> storage)
{
storage.Clear();
if (hashSet == null)
{
return;
}

foreach (var thing in hashSet)
{
Expand All @@ -291,6 +294,11 @@ private static void CleanDictionary<T>(Il2CppSystem.Collections.Generic.Dictiona
Func<string, bool> clean, Dictionary<string, T> storage)
{
storage.Clear();
if (dictionary == null)
{
return;
}

foreach (var (thing, value) in dictionary)
{
if (clean(thing))
Expand Down

0 comments on commit c5cfb7e

Please sign in to comment.