Skip to content

Commit f7db2bb

Browse files
committed
Remove asset validation callback
1 parent 08bbe52 commit f7db2bb

4 files changed

Lines changed: 28 additions & 91 deletions

File tree

Refresh.Core/Helpers/ResourceValidationHelper.cs

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,38 +20,37 @@ public static ValidatedAssetResult ValidateReference(AssetValidationParameters p
2020
GameAsset? asset = null;
2121
bool existsInDataStore = false;
2222
bool isPSP = parameters.GameToUseIn == TokenGame.LittleBigPlanetPSP;
23-
Action<string>? onNewAssetRefCallback = parameters.OnNewAssetRefCallback;
2423

2524
if (parameters.AssetRef.IsBlankHash())
2625
{
27-
if (!parameters.MayBeBlank) return new(BadRequest, "0", $"The {assetTypeStr} must be set.", onNewAssetRefCallback);
28-
else return new(OK, "0", null, onNewAssetRefCallback);
26+
if (!parameters.MayBeBlank) return new(BadRequest, "0", $"The {assetTypeStr} must be set.");
27+
else return new(OK, "0", null);
2928
}
3029

3130
else if (parameters.AssetRef.StartsWith('g'))
3231
{
33-
if (!parameters.MayBeGuid) return new(BadRequest, null, $"The {assetTypeStr} may not be an in-game asset.", onNewAssetRefCallback);
34-
if (parameters.AssetRef.Length < 2) return new(BadRequest, null, $"The used in-game {assetTypeStr} is invalid (empty GUID).", onNewAssetRefCallback);
32+
if (!parameters.MayBeGuid) return new(BadRequest, null, $"The {assetTypeStr} may not be an in-game asset.");
33+
if (parameters.AssetRef.Length < 2) return new(BadRequest, null, $"The used in-game {assetTypeStr} is invalid (empty GUID).");
3534

3635
// This should only happen if the user is messing with mods/the API/beta builds, so give them a more detailed response
3736
bool canParseGuid = long.TryParse(parameters.AssetRef[1..], out long guid);
3837
if (!canParseGuid)
39-
return new(BadRequest, null, $"The used in-game {assetTypeStr} is invalid (badly formatted GUID).", onNewAssetRefCallback);
38+
return new(BadRequest, null, $"The used in-game {assetTypeStr} is invalid (badly formatted GUID).");
4039

4140
if (parameters.MustBeTexture && !parameters.GuidChecker.IsTextureGuid(parameters.GameToUseIn, guid))
42-
return new(BadRequest, null, $"The used in-game {assetTypeStr} was not a valid image (unknown GUID).", onNewAssetRefCallback);
41+
return new(BadRequest, null, $"The used in-game {assetTypeStr} was not a valid image (unknown GUID).");
4342
}
4443

4544
// At this point the reference is a hash
4645
else if (!parameters.MayBeHash)
4746
{
48-
return new(BadRequest, null, $"The {assetTypeStr} may not be a custom asset.", onNewAssetRefCallback);
47+
return new(BadRequest, null, $"The {assetTypeStr} may not be a custom asset.");
4948
}
5049

5150
else if (!CommonPatterns.Sha1Regex().IsMatch(parameters.AssetRef))
5251
{
5352
// This should only happen if a player is messing with mods/the API, so give them a more detailed response
54-
return new(BadRequest, null, $"The used {assetTypeStr} had an invalid hash.", onNewAssetRefCallback);
53+
return new(BadRequest, null, $"The used {assetTypeStr} had an invalid hash.");
5554
}
5655

5756
else
@@ -60,7 +59,7 @@ public static ValidatedAssetResult ValidateReference(AssetValidationParameters p
6059
if (disallowed != null)
6160
{
6261
logger.LogWarning(BunkumCategory.UserContent, $"{parameters.User} tried to use a manually disallowed {assetTypeStr}.");
63-
return new(Unauthorized, disallowanceInfo: disallowed, onNewAssetRefCallback: onNewAssetRefCallback);
62+
return new(Unauthorized, disallowanceInfo: disallowed);
6463
}
6564

6665
string filename = isPSP ? $"psp/{parameters.AssetRef}" : parameters.AssetRef;
@@ -71,7 +70,7 @@ public static ValidatedAssetResult ValidateReference(AssetValidationParameters p
7170
logger.LogDebug(BunkumCategory.UserContent, $"Referenced asset '{filename}' could not be found in data store.");
7271

7372
if (parameters.MustBeInDataStoreIfHash)
74-
return new(NotFound, null, $"The used {assetTypeStr} did not exist on the server.", onNewAssetRefCallback);
73+
return new(NotFound, null, $"The used {assetTypeStr} did not exist on the server.");
7574
}
7675

7776
asset = parameters.Cache.GetAssetInfo(parameters.AssetRef, parameters.Database);
@@ -88,15 +87,15 @@ public static ValidatedAssetResult ValidateReference(AssetValidationParameters p
8887
sw.Stop();
8988
logger.LogError(BunkumCategory.UserContent, $"Failed to read '{filename}' from data store!");
9089
logger.LogDebug(BunkumCategory.UserContent, $"Failed to get '{filename}' after {sw.ElapsedMilliseconds}ms.");
91-
return new(InternalServerError, null, $"Failed to read {assetTypeStr} internally. Please report this to the server owner.", onNewAssetRefCallback, existsInDataStore: existsInDataStore);
90+
return new(InternalServerError, null, $"Failed to read {assetTypeStr} internally. Please report this to the server owner.", existsInDataStore: existsInDataStore);
9291
}
9392

9493
asset = parameters.AssetImporter.ReadAndVerifyAsset(parameters.AssetRef, assetData, parameters.PlatformToUseIn, parameters.Database);
9594
if (asset == null)
9695
{
9796
sw.Stop();
9897
logger.LogDebug(BunkumCategory.UserContent, $"Failed to get '{filename}' after {sw.ElapsedMilliseconds}ms.");
99-
return new(BadRequest, null, $"The used {assetTypeStr} was invalid or corrupt.", onNewAssetRefCallback, existsInDataStore: existsInDataStore);
98+
return new(BadRequest, null, $"The used {assetTypeStr} was invalid or corrupt.", existsInDataStore: existsInDataStore);
10099
}
101100

102101
sw.Stop();
@@ -109,12 +108,12 @@ public static ValidatedAssetResult ValidateReference(AssetValidationParameters p
109108
bool isHashedTexture = (asset.AssetFlags & AssetFlags.Imagery) != 0;
110109

111110
if (parameters.MustBeTexture && !isHashedTexture)
112-
return new(BadRequest, null, $"The used {assetTypeStr} was not a valid custom image.", onNewAssetRefCallback, assetInfo: asset, existsInDataStore: existsInDataStore);
111+
return new(BadRequest, null, $"The used {assetTypeStr} was not a valid custom image.", assetInfo: asset, existsInDataStore: existsInDataStore);
113112

114113
// TODO: actually use AIPI to scan image if not null
115114
}
116115
}
117116

118-
return new(OK, parameters.AssetRef, null, onNewAssetRefCallback, assetInfo: asset, existsInDataStore: existsInDataStore);
117+
return new(OK, parameters.AssetRef, assetInfo: asset, existsInDataStore: existsInDataStore);
119118
}
120119
}

Refresh.Core/Types/Assets/Validation/AssetValidationParameters.cs

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -34,19 +34,11 @@ public struct AssetValidationParameters
3434
/// What the asset should be referred as in user-faced error messages and in logs, e.g. "planet asset" or "icon".
3535
/// If null, we will default to calling it "asset" or "image" depending on MustBeTexture.
3636
/// </summary>
37-
public string? AssetContextTypeStr { get; set; }
37+
public string? AssetContextTypeStr { get; set; } // TODO think of a better name
3838

39-
/// <summary>
40-
/// Callback which is called with the new asset reference as parameter when constructing <see cref="ValidatedAssetResult"/>;
41-
/// useful to update asset references of entities during validation without requiring the caller to manually reassign them after validation;
42-
/// this way similar attributes like photo images or face icons can simply be iterated.
43-
/// If null, this will be skipped.
44-
/// </summary>
45-
public Action<string>? OnNewAssetRefCallback { get; set; }
46-
47-
public AssetValidationParameters(string assetKey, DataContext dataContext, AssetImporter assetImporter, AipiService? aipi = null)
39+
public AssetValidationParameters(string assetRef, DataContext dataContext, AssetImporter assetImporter, AipiService? aipi = null)
4840
{
49-
this.AssetRef = assetKey;
41+
this.AssetRef = assetRef;
5042
this.User = dataContext.User;
5143
this.GameToUseIn = dataContext.Game;
5244
this.PlatformToUseIn = dataContext.Platform;

Refresh.Core/Types/Assets/Validation/ValidatedAssetResult.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public struct ValidatedAssetResult
2626
public DisallowedAsset? DisallowanceInfo { get; set; }
2727
public bool ExistsInDataStore { get; set; }
2828

29-
public ValidatedAssetResult(HttpStatusCode status, string? newAssetRef = null, string? errorMessage = null, Action<string>? onNewAssetRefCallback = null,
29+
public ValidatedAssetResult(HttpStatusCode status, string? newAssetRef = null, string? errorMessage = null,
3030
GameAsset? assetInfo = null, DisallowedAsset? disallowanceInfo = null, bool existsInDataStore = false)
3131
{
3232
this.Status = status;
@@ -35,7 +35,5 @@ public ValidatedAssetResult(HttpStatusCode status, string? newAssetRef = null, s
3535
this.AssetInfo = assetInfo;
3636
this.DisallowanceInfo = disallowanceInfo;
3737
this.ExistsInDataStore = existsInDataStore;
38-
39-
onNewAssetRefCallback?.Invoke(this.NewAssetRef);
4038
}
4139
}

0 commit comments

Comments
 (0)