Skip to content

Commit

Permalink
add warning for clarity when a runtime migration fails
Browse files Browse the repository at this point in the history
  • Loading branch information
Pathoschild committed Feb 15, 2024
1 parent 4caad1c commit 5b1ad78
Show file tree
Hide file tree
Showing 11 changed files with 118 additions and 51 deletions.
13 changes: 11 additions & 2 deletions ContentPatcher/Framework/Migrations/BaseRuntimeMigration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,18 @@ public virtual bool TryApplyEditPatch<T>(IPatch patch, IAssetData asset, out str
{
if (migrator.AppliesTo(patch.TargetAssetBeforeRedirection ?? asset.Name))
{
if (migrator.TryApplyEditPatch<T>(editPatch, asset, out error))
return true;
// log warning if runtime migration has issues
bool hasLoggedWarning = false;
void OnWarning(string warning, IMonitor monitor)
{
if (!hasLoggedWarning)
monitor.Log($"Data patch \"{patch.Path}\" reported warnings when applying runtime migration {this.Version}. (For the mod author: see https://smapi.io/cp-migrate to avoid runtime migrations.)", LogLevel.Warn);
hasLoggedWarning = true;
}

// apply
if (migrator.TryApplyEditPatch<T>(editPatch, asset, OnWarning, out error))
return true;
if (error != null)
return false;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Diagnostics.CodeAnalysis;
using ContentPatcher.Framework.Conditions;
using ContentPatcher.Framework.Patches;
Expand All @@ -19,6 +20,10 @@ internal interface IEditAssetMigrator
bool TryApplyLoadPatch<T>(LoadPatch patch, IAssetName assetName, [NotNullWhen(true)] ref T? asset, out string? error);

/// <inheritdoc cref="IRuntimeMigration.TryApplyEditPatch{T}" />
bool TryApplyEditPatch<T>(EditDataPatch patch, IAssetData asset, out string? error);
/// <param name="patch" />
/// <param name="asset" />
/// <param name="onWarning">A callback to invoke before logging a warning message. The warning message is still logged after calling it.</param>
/// <param name="error" />
bool TryApplyEditPatch<T>(EditDataPatch patch, IAssetData asset, Action<string, IMonitor> onWarning, out string? error);
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using ContentPatcher.Framework.Migrations.Internal;
Expand Down Expand Up @@ -54,12 +55,12 @@ public bool TryApplyLoadPatch<T>(LoadPatch patch, IAssetName assetName, [NotNull
}

/// <inheritdoc />
public bool TryApplyEditPatch<T>(EditDataPatch patch, IAssetData asset, out string? error)
public bool TryApplyEditPatch<T>(EditDataPatch patch, IAssetData asset, Action<string, IMonitor> onWarning, out string? error)
{
var data = asset.GetData<Dictionary<string, BigCraftableData>>();
Dictionary<string, string> tempData = this.GetOldFormat(data);
Dictionary<string, string> tempDataBackup = new(tempData);
patch.Edit<Dictionary<string, string>>(new FakeAssetData(asset, this.GetOldAssetName(asset.Name), tempData));
patch.Edit<Dictionary<string, string>>(new FakeAssetData(asset, this.GetOldAssetName(asset.Name), tempData), onWarning);
this.MergeIntoNewFormat(data, tempData, tempDataBackup);

error = null;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Text;
Expand Down Expand Up @@ -55,12 +56,12 @@ public bool TryApplyLoadPatch<T>(LoadPatch patch, IAssetName assetName, [NotNull
}

/// <inheritdoc />
public bool TryApplyEditPatch<T>(EditDataPatch patch, IAssetData asset, out string? error)
public bool TryApplyEditPatch<T>(EditDataPatch patch, IAssetData asset, Action<string, IMonitor> onWarning, out string? error)
{
var data = asset.GetData<Dictionary<string, BuildingData>>();
Dictionary<string, string> tempData = this.GetOldFormat(data);
Dictionary<string, string> tempDataBackup = new(tempData);
patch.Edit<Dictionary<string, string>>(new FakeAssetData(asset, this.GetOldAssetName(asset.Name), tempData));
patch.Edit<Dictionary<string, string>>(new FakeAssetData(asset, this.GetOldAssetName(asset.Name), tempData), onWarning);
this.MergeIntoNewFormat(data, tempData, tempDataBackup);

error = null;
Expand Down
5 changes: 3 additions & 2 deletions ContentPatcher/Framework/Migrations/Migration_2_0.ForBoots.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using ContentPatcher.Framework.Migrations.Internal;
Expand Down Expand Up @@ -45,10 +46,10 @@ public bool TryApplyLoadPatch<T>(LoadPatch patch, IAssetName assetName, [NotNull
}

/// <inheritdoc />
public bool TryApplyEditPatch<T>(EditDataPatch patch, IAssetData asset, out string? error)
public bool TryApplyEditPatch<T>(EditDataPatch patch, IAssetData asset, Action<string, IMonitor> onWarning, out string? error)
{
var data = (Dictionary<string, string>)asset.Data;
patch.Edit<Dictionary<string, string>>(asset);
patch.Edit<Dictionary<string, string>>(asset, onWarning);
this.MigrateData(data);

error = null;
Expand Down
5 changes: 3 additions & 2 deletions ContentPatcher/Framework/Migrations/Migration_2_0.ForCrops.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
Expand Down Expand Up @@ -51,12 +52,12 @@ public bool TryApplyLoadPatch<T>(LoadPatch patch, IAssetName assetName, [NotNull
}

/// <inheritdoc />
public bool TryApplyEditPatch<T>(EditDataPatch patch, IAssetData asset, out string? error)
public bool TryApplyEditPatch<T>(EditDataPatch patch, IAssetData asset, Action<string, IMonitor> onWarning, out string? error)
{
var data = asset.GetData<Dictionary<string, CropData>>();
Dictionary<string, string> tempData = this.GetOldFormat(data);
Dictionary<string, string> tempDataBackup = new(tempData);
patch.Edit<Dictionary<string, string>>(new FakeAssetData(asset, asset.Name, tempData));
patch.Edit<Dictionary<string, string>>(new FakeAssetData(asset, asset.Name, tempData), onWarning);
this.MergeIntoNewFormat(data, tempData, tempDataBackup);

error = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,13 @@ public bool TryApplyLoadPatch<T>(LoadPatch patch, IAssetName assetName, [NotNull
}

/// <inheritdoc />
public bool TryApplyEditPatch<T>(EditDataPatch patch, IAssetData asset, out string? error)
public bool TryApplyEditPatch<T>(EditDataPatch patch, IAssetData asset, Action<string, IMonitor> onWarning, out string? error)
{
var assetData = asset.GetData<Dictionary<string, LocationData>>();
Dictionary<string, string> tempData = this.GetOldFormat(assetData, out HashSet<string> skippedDueToNoData);
Dictionary<string, string> tempDataBackup = new(tempData);

patch.Edit<Dictionary<string, string>>(new FakeAssetData(asset, asset.Name, tempData));
patch.Edit<Dictionary<string, string>>(new FakeAssetData(asset, asset.Name, tempData), onWarning);
this.MergeIntoNewFormat(assetData, tempData, tempDataBackup, skippedDueToNoData, patch.ContentPack.Manifest.UniqueID);

error = null;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Text;
Expand Down Expand Up @@ -55,12 +56,12 @@ public bool TryApplyLoadPatch<T>(LoadPatch patch, IAssetName assetName, [NotNull
}

/// <inheritdoc />
public bool TryApplyEditPatch<T>(EditDataPatch patch, IAssetData asset, out string? error)
public bool TryApplyEditPatch<T>(EditDataPatch patch, IAssetData asset, Action<string, IMonitor> onWarning, out string? error)
{
var data = asset.GetData<Dictionary<string, CharacterData>>();
Dictionary<string, string> tempData = this.GetOldFormat(data);
Dictionary<string, string> tempDataBackup = new(tempData);
patch.Edit<Dictionary<string, string>>(new FakeAssetData(asset, this.GetOldAssetName(asset.Name), tempData));
patch.Edit<Dictionary<string, string>>(new FakeAssetData(asset, this.GetOldAssetName(asset.Name), tempData), onWarning);
this.MergeIntoNewFormat(data, tempData, tempDataBackup);

error = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,12 @@ public bool TryApplyLoadPatch<T>(LoadPatch patch, IAssetName assetName, [NotNull
}

/// <inheritdoc />
public bool TryApplyEditPatch<T>(EditDataPatch patch, IAssetData asset, out string? error)
public bool TryApplyEditPatch<T>(EditDataPatch patch, IAssetData asset, Action<string, IMonitor> onWarning, out string? error)
{
var data = asset.GetData<Dictionary<string, ObjectData>>();
Dictionary<string, string> tempData = this.GetOldFormat(data);
Dictionary<string, string> tempDataBackup = new(tempData);
patch.Edit<Dictionary<string, string>>(new FakeAssetData(asset, this.GetOldAssetName(asset.Name), tempData));
patch.Edit<Dictionary<string, string>>(new FakeAssetData(asset, this.GetOldAssetName(asset.Name), tempData), onWarning);
this.MergeIntoNewFormat(data, tempData, tempDataBackup, patch.ContentPack.Manifest.UniqueID);

error = null;
Expand Down
Loading

0 comments on commit 5b1ad78

Please sign in to comment.