Refactor serialization system (#702)

* Initial work for serialization refactor

* Experiment with new naming conventions

* Mostly implement user and slot serialization.
Still needs to be fine tuned to match original implementation
Many things are left in a broken state like website features/api endpoints/lbp3 categories

* Fix release building

* Migrate scores, reviews, and more to new serialization system.
Many things are still broken but progress is steadily being made

* Fix Api responses and migrate serialization for most types

* Make serialization better and fix bugs
Fix recursive PrepareSerialization when recursive item is set during root item's PrepareSerialization, items, should be properly indexed in order but it's only tested to 1 level of recursion

* Fix review serialization

* Fix user serialization producing malformed SQL query

* Remove DefaultIfEmpty query

* MariaDB doesn't like double nested queries

* Fix LBP1 tag counter

* Implement lbp3 categories and add better deserialization handling

* Implement expression tree caching to speed up reflection and write new serializer tests

* Remove Game column from UserEntity and rename DatabaseContextModelSnapshot.cs back to DatabaseModelSnapshot.cs

* Make UserEntity username not required

* Fix recursive serialization of lists and add relevant unit tests

* Actually commit the migration

* Fix LocationTests to use new deserialization class

* Fix comments not serializing the right author username

* Replace all occurrences of StatusCode with their respective ASP.NET named result
instead of StatusCode(403) everything is now in the form of Forbid()

* Fix SlotBase.ConvertToEntity and LocationTests

* Fix compilation error

* Give Location a default value in GameUserSlot and GameUser

* Reimplement stubbed website functions

* Convert grief reports to new serialization system

* Update DatabaseModelSnapshot and bump dotnet tool version

* Remove unused directives

* Fix broken type reference

* Fix rated comments on website

* Don't include banned users in website comments

* Optimize score submission

* Fix slot id calculating in in-game comment posting

* Move serialization interfaces to types folder and add more documentation

* Allow uploading of versus scores
This commit is contained in:
Josh 2023-03-27 19:39:54 -05:00 committed by GitHub
parent 307b2135a3
commit 329ab66043
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
248 changed files with 4993 additions and 2896 deletions

View file

@ -5,12 +5,12 @@ using LBPUnion.ProjectLighthouse.Extensions;
using LBPUnion.ProjectLighthouse.Files;
using LBPUnion.ProjectLighthouse.Helpers;
using LBPUnion.ProjectLighthouse.Logging;
using LBPUnion.ProjectLighthouse.Serialization;
using LBPUnion.ProjectLighthouse.Types.Entities.Level;
using LBPUnion.ProjectLighthouse.Types.Entities.Profile;
using LBPUnion.ProjectLighthouse.Types.Entities.Token;
using LBPUnion.ProjectLighthouse.Types.Logging;
using LBPUnion.ProjectLighthouse.Types.Resources;
using LBPUnion.ProjectLighthouse.Types.Serialization;
using LBPUnion.ProjectLighthouse.Types.Users;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
@ -37,12 +37,12 @@ public class PublishController : ControllerBase
[HttpPost("startPublish")]
public async Task<IActionResult> StartPublish()
{
GameToken token = this.GetToken();
GameTokenEntity token = this.GetToken();
User? user = await this.database.UserFromGameToken(token);
if (user == null) return this.StatusCode(403, "");
UserEntity? user = await this.database.UserFromGameToken(token);
if (user == null) return this.Forbid();
Slot? slot = await this.DeserializeBody<Slot>();
GameUserSlot? slot = await this.DeserializeBody<GameUserSlot>();
if (slot == null)
{
Logger.Warn("Rejecting level upload, slot is null", LogArea.Publish);
@ -55,7 +55,7 @@ public class PublishController : ControllerBase
return this.BadRequest();
}
if (string.IsNullOrEmpty(slot.ResourceCollection)) slot.ResourceCollection = slot.RootLevel;
if (slot.Resources?.Length == 0) slot.Resources = new[]{slot.RootLevel,};
if (slot.Resources == null)
{
@ -63,10 +63,12 @@ public class PublishController : ControllerBase
return this.BadRequest();
}
int usedSlots = await this.database.Slots.CountAsync(s => s.CreatorId == token.UserId && s.GameVersion == token.GameVersion);
// Republish logic
if (slot.SlotId != 0)
{
Slot? oldSlot = await this.database.Slots.FirstOrDefaultAsync(s => s.SlotId == slot.SlotId);
SlotEntity? oldSlot = await this.database.Slots.FirstOrDefaultAsync(s => s.SlotId == slot.SlotId);
if (oldSlot == null)
{
Logger.Warn("Rejecting level republish, could not find old slot", LogArea.Publish);
@ -78,18 +80,18 @@ public class PublishController : ControllerBase
return this.BadRequest();
}
}
else if (user.GetUsedSlotsForGame(token.GameVersion) > user.EntitledSlots)
else if (usedSlots > user.EntitledSlots)
{
return this.StatusCode(403, "");
return this.Forbid();
}
slot.ResourceCollection += "," + slot.IconHash; // tells LBP to upload icon after we process resources here
HashSet<string> resources = new(slot.Resources)
{
slot.IconHash,
};
resources = resources.Where(hash => !FileHelper.ResourceExists(hash)).ToHashSet();
string resources = slot.Resources.Where
(hash => !FileHelper.ResourceExists(hash))
.Aggregate("", (current, hash) => current + LbpSerializer.StringElement("resource", hash));
return this.Ok(LbpSerializer.TaggedStringElement("slot", resources, "type", "user"));
return this.Ok(new SlotResourceResponse(resources.ToList()));
}
/// <summary>
@ -98,12 +100,12 @@ public class PublishController : ControllerBase
[HttpPost("publish")]
public async Task<IActionResult> Publish([FromQuery] string? game)
{
GameToken token = this.GetToken();
GameTokenEntity token = this.GetToken();
User? user = await this.database.UserFromGameToken(token);
if (user == null) return this.StatusCode(403, "");
UserEntity? user = await this.database.UserFromGameToken(token);
if (user == null) return this.Forbid();
Slot? slot = await this.DeserializeBody<Slot>();
GameUserSlot? slot = await this.DeserializeBody<GameUserSlot>();
if (slot == null)
{
@ -111,6 +113,12 @@ public class PublishController : ControllerBase
return this.BadRequest();
}
if (slot.Resources == null)
{
Logger.Warn("Rejecting level upload, resource list is null", LogArea.Publish);
return this.BadRequest();
}
slot.Description = CensorHelper.FilterMessage(slot.Description);
if (slot.Description.Length > 512)
@ -168,7 +176,7 @@ public class PublishController : ControllerBase
// Republish logic
if (slot.SlotId != 0)
{
Slot? oldSlot = await this.database.Slots.FirstOrDefaultAsync(s => s.SlotId == slot.SlotId);
SlotEntity? oldSlot = await this.database.Slots.FirstOrDefaultAsync(s => s.SlotId == slot.SlotId);
if (oldSlot == null)
{
Logger.Warn("Rejecting level republish, wasn't able to find old slot", LogArea.Publish);
@ -190,36 +198,32 @@ public class PublishController : ControllerBase
// Delete the useless rootLevel that lbp3 just uploaded
if (slotVersion == GameVersion.LittleBigPlanet3)
FileHelper.DeleteResource(slot.RootLevel);
slot.GameVersion = oldSlot.GameVersion;
slot.RootLevel = oldSlot.RootLevel;
slot.ResourceCollection = oldSlot.ResourceCollection;
else
{
oldSlot.GameVersion = slot.GameVersion;
oldSlot.RootLevel = slot.RootLevel;
oldSlot.Resources = slot.Resources;
}
}
}
slot.CreatorId = oldSlot.CreatorId;
slot.SlotId = oldSlot.SlotId;
oldSlot.Name = slot.Name;
oldSlot.Description = slot.Description;
oldSlot.Location = slot.Location;
oldSlot.IconHash = slot.IconHash;
oldSlot.BackgroundHash = slot.BackgroundHash;
oldSlot.AuthorLabels = slot.AuthorLabels;
oldSlot.Shareable = slot.IsShareable;
oldSlot.Resources = slot.Resources;
oldSlot.InitiallyLocked = slot.InitiallyLocked;
oldSlot.Lbp1Only = slot.IsLbp1Only;
oldSlot.IsAdventurePlanet = slot.IsAdventurePlanet;
oldSlot.LevelType = slot.LevelType;
oldSlot.SubLevel = slot.IsSubLevel;
oldSlot.MoveRequired = slot.IsMoveRequired;
oldSlot.CrossControllerRequired = slot.IsCrossControlRequired;
#region Set plays
slot.PlaysLBP1 = oldSlot.PlaysLBP1;
slot.PlaysLBP1Complete = oldSlot.PlaysLBP1Complete;
slot.PlaysLBP1Unique = oldSlot.PlaysLBP1Unique;
slot.PlaysLBP2 = oldSlot.PlaysLBP2;
slot.PlaysLBP2Complete = oldSlot.PlaysLBP2Complete;
slot.PlaysLBP2Unique = oldSlot.PlaysLBP2Unique;
slot.PlaysLBP3 = oldSlot.PlaysLBP3;
slot.PlaysLBP3Complete = oldSlot.PlaysLBP3Complete;
slot.PlaysLBP3Unique = oldSlot.PlaysLBP3Unique;
#endregion
slot.FirstUploaded = oldSlot.FirstUploaded;
slot.LastUpdated = TimeHelper.TimestampMillis;
slot.TeamPick = oldSlot.TeamPick;
oldSlot.LastUpdated = TimeHelper.TimestampMillis;
if (slot.MinimumPlayers == 0 || slot.MaximumPlayers == 0)
{
@ -227,20 +231,23 @@ public class PublishController : ControllerBase
slot.MaximumPlayers = 4;
}
slot.MinimumPlayers = Math.Clamp(slot.MinimumPlayers, 1, 4);
slot.MaximumPlayers = Math.Clamp(slot.MaximumPlayers, 1, 4);
oldSlot.MinimumPlayers = Math.Clamp(slot.MinimumPlayers, 1, 4);
oldSlot.MaximumPlayers = Math.Clamp(slot.MaximumPlayers, 1, 4);
this.database.Entry(oldSlot).CurrentValues.SetValues(slot);
await this.database.SaveChangesAsync();
return this.Ok(oldSlot.Serialize(token.GameVersion));
return this.Ok(SlotBase.CreateFromEntity(oldSlot, this.GetToken()));
}
if (user.GetUsedSlotsForGame(slotVersion) > user.EntitledSlots)
int usedSlots = await this.database.Slots.CountAsync(s => s.CreatorId == token.UserId && s.GameVersion == slotVersion);
if (usedSlots > user.EntitledSlots)
{
Logger.Warn("Rejecting level upload, too many published slots", LogArea.Publish);
return this.BadRequest();
}
SlotEntity slotEntity = SlotBase.ConvertToEntity(slot);
slot.CreatorId = user.UserId;
slot.FirstUploaded = TimeHelper.TimestampMillis;
slot.LastUpdated = TimeHelper.TimestampMillis;
@ -254,7 +261,7 @@ public class PublishController : ControllerBase
slot.MinimumPlayers = Math.Clamp(slot.MinimumPlayers, 1, 4);
slot.MaximumPlayers = Math.Clamp(slot.MaximumPlayers, 1, 4);
this.database.Slots.Add(slot);
this.database.Slots.Add(slotEntity);
await this.database.SaveChangesAsync();
if (user.LevelVisibility == PrivacyType.All)
@ -265,18 +272,18 @@ public class PublishController : ControllerBase
Logger.Success($"Successfully published level {slot.Name} (id: {slot.SlotId}) by {user.Username} (id: {user.UserId})", LogArea.Publish);
return this.Ok(slot.Serialize(token.GameVersion));
return this.Ok(SlotBase.CreateFromEntity(slotEntity, this.GetToken()));
}
[HttpPost("unpublish/{id:int}")]
public async Task<IActionResult> Unpublish(int id)
{
GameToken token = this.GetToken();
GameTokenEntity token = this.GetToken();
Slot? slot = await this.database.Slots.FirstOrDefaultAsync(s => s.SlotId == id);
SlotEntity? slot = await this.database.Slots.FirstOrDefaultAsync(s => s.SlotId == id);
if (slot == null) return this.NotFound();
if (slot.CreatorId != token.UserId) return this.StatusCode(403, "");
if (slot.CreatorId != token.UserId) return this.Forbid();
this.database.Slots.Remove(slot);