Skip to content

Commit

Permalink
Fix: Possible game database corruption during update
Browse files Browse the repository at this point in the history
  • Loading branch information
JosefNemec committed Sep 28, 2018
1 parent b1ddc80 commit 8030692
Showing 1 changed file with 79 additions and 19 deletions.
98 changes: 79 additions & 19 deletions source/Playnite/Database/GameDatabase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public bool IsOpen
get; private set;
}

public static readonly ushort DBVersion = 4;
public static readonly ushort DBVersion = 5;

public event PlatformsCollectionChangedEventHandler PlatformsCollectionChanged;
public event PlatformUpdatedEventHandler PlatformUpdated;
Expand Down Expand Up @@ -513,37 +513,97 @@ void MigrateGameAction(BsonDocument action, bool handleByPlugin)
action.Remove("IsPrimary");
action.Remove("IsBuiltIn");
action.Add("IsHandledByPlugin", handleByPlugin);
if (action["Type"].AsString == "Emulator")

var oldEmulator = action["EmulatorId"];
if (!oldEmulator.IsNull)
{
var oldEmulator = action["EmulatorId"];
if (!oldEmulator.IsNull)
if (conEmulators.TryGetValue(oldEmulator, out var newEmu))
{
if (conEmulators.TryGetValue(oldEmulator, out var newEmu))
{
action["EmulatorId"] = newEmu;
}
else
action["EmulatorId"] = newEmu;
}
else
{
action.Remove("EmulatorId");
}
}

var oldProfile = action["EmulatorProfileId"];
if (!oldProfile.IsNull)
{
if (conEmuProfiles.TryGetValue(oldProfile, out var newProf))
{
action["EmulatorProfileId"] = newProf;
}
else
{
action.Remove("EmulatorProfileId");
}
}
}

db.Engine.UserVersion = 4;
}

// 4 to 5
if (db.Engine.UserVersion == 4 && DBVersion > 4)
{
// Fix Game action that have invalid emualtor ids
var gameCol = db.GetCollection("games");
foreach (var game in gameCol.FindAll().ToList())
{
var fixApplied = false;
var playAction = game["PlayAction"];
if (!playAction.IsNull)
{
if (FixGameAction(playAction.AsDocument))
{
fixApplied = true;
}

}

var otherActions = game["OtherActions"];
if (!otherActions.IsNull)
{
foreach (BsonDocument task in otherActions.AsArray)
{
if (FixGameAction(task))
{
action.Remove("EmulatorId");
fixApplied = true;
}
}
}

if (fixApplied)
{
gameCol.Update(game);
}
}

bool FixGameAction(BsonDocument action)
{
var fixedAny = false;
if (action["Type"].AsString != "Emulator")
{
var oldEmulator = action["EmulatorId"];
if (!oldEmulator.IsNull)
{
action.Remove("EmulatorId");
fixedAny = true;
}

var oldProfile = action["EmulatorProfileId"];
if (!oldProfile.IsNull)
{
if (conEmuProfiles.TryGetValue(oldProfile, out var newProf))
{
action["EmulatorProfileId"] = newProf;
}
else
{
action.Remove("EmulatorProfileId");
}
action.Remove("EmulatorProfileId");
fixedAny = true;
}
}

return fixedAny;
}

db.Engine.UserVersion = 4;
db.Engine.UserVersion = 5;
}

trans.Commit();
Expand Down

0 comments on commit 8030692

Please sign in to comment.