Fix memory leak in GameServer (#731)

* Convert entities to serializable after aggregating rather before

* Cache instances of CustomXmlSerializer and create readonly constants for reused settings

* Change CustomXmlSerializer and serializer cache to work with deserializer
This commit is contained in:
Josh 2023-04-02 18:45:19 -05:00 committed by GitHub
parent 0253864f5e
commit 2210541894
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
16 changed files with 125 additions and 147 deletions

View file

@ -1,6 +1,7 @@
#nullable enable #nullable enable
using LBPUnion.ProjectLighthouse.Configuration; using LBPUnion.ProjectLighthouse.Configuration;
using LBPUnion.ProjectLighthouse.Database; using LBPUnion.ProjectLighthouse.Database;
using LBPUnion.ProjectLighthouse.Extensions;
using LBPUnion.ProjectLighthouse.Servers.API.Responses; using LBPUnion.ProjectLighthouse.Servers.API.Responses;
using LBPUnion.ProjectLighthouse.Types.Entities.Level; using LBPUnion.ProjectLighthouse.Types.Entities.Level;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
@ -35,11 +36,10 @@ public class SlotEndpoints : ApiEndpointController
if (limit < 0) limit = 0; if (limit < 0) limit = 0;
limit = Math.Min(ServerStatics.PageSize, limit); limit = Math.Min(ServerStatics.PageSize, limit);
IEnumerable<ApiSlot> minimalSlots = await this.database.Slots.OrderByDescending(s => s.FirstUploaded) List<ApiSlot> minimalSlots = (await this.database.Slots.OrderByDescending(s => s.FirstUploaded)
.Skip(skip) .Skip(skip)
.Take(limit) .Take(limit)
.Select(s => ApiSlot.CreateFromEntity(s)) .ToListAsync()).ToSerializableList(ApiSlot.CreateFromEntity);
.ToListAsync();
return this.Ok(minimalSlots); return this.Ok(minimalSlots);
} }

View file

@ -1,5 +1,6 @@
#nullable enable #nullable enable
using LBPUnion.ProjectLighthouse.Database; using LBPUnion.ProjectLighthouse.Database;
using LBPUnion.ProjectLighthouse.Extensions;
using LBPUnion.ProjectLighthouse.Helpers; using LBPUnion.ProjectLighthouse.Helpers;
using LBPUnion.ProjectLighthouse.Servers.API.Responses; using LBPUnion.ProjectLighthouse.Servers.API.Responses;
using LBPUnion.ProjectLighthouse.Types.Entities.Profile; using LBPUnion.ProjectLighthouse.Types.Entities.Profile;
@ -63,13 +64,12 @@ public class UserEndpoints : ApiEndpointController
[ProducesResponseType(StatusCodes.Status404NotFound)] [ProducesResponseType(StatusCodes.Status404NotFound)]
public async Task<IActionResult> SearchUsers(string query) public async Task<IActionResult> SearchUsers(string query)
{ {
List<ApiUser> users = await this.database.Users List<ApiUser> users = (await this.database.Users
.Where(u => u.PermissionLevel != PermissionLevel.Banned && u.Username.Contains(query)) .Where(u => u.PermissionLevel != PermissionLevel.Banned && u.Username.Contains(query))
.Where(u => u.ProfileVisibility == PrivacyType.All) // TODO: change check for when user is logged in .Where(u => u.ProfileVisibility == PrivacyType.All) // TODO: change check for when user is logged in
.OrderByDescending(b => b.UserId) .OrderByDescending(b => b.UserId)
.Take(20) .Take(20)
.Select(u => ApiUser.CreateFromEntity(u)) .ToListAsync()).ToSerializableList(ApiUser.CreateFromEntity);
.ToListAsync();
if (!users.Any()) return this.NotFound(); if (!users.Any()) return this.NotFound();
return this.Ok(users); return this.Ok(users);

View file

@ -77,15 +77,14 @@ public class CommentController : ControllerBase
where blockedProfile.UserId == token.UserId where blockedProfile.UserId == token.UserId
select blockedProfile.BlockedUserId).ToListAsync(); select blockedProfile.BlockedUserId).ToListAsync();
List<GameComment> comments = await this.database.Comments.Where(p => p.TargetId == targetId && p.Type == type) List<GameComment> comments = (await this.database.Comments.Where(p => p.TargetId == targetId && p.Type == type)
.OrderByDescending(p => p.Timestamp) .OrderByDescending(p => p.Timestamp)
.Where(p => !blockedUsers.Contains(p.PosterUserId)) .Where(p => !blockedUsers.Contains(p.PosterUserId))
.Include(c => c.Poster) .Include(c => c.Poster)
.Where(p => p.Poster.PermissionLevel != PermissionLevel.Banned) .Where(p => p.Poster.PermissionLevel != PermissionLevel.Banned)
.Skip(Math.Max(0, pageStart - 1)) .Skip(Math.Max(0, pageStart - 1))
.Take(Math.Min(pageSize, 30)) .Take(Math.Min(pageSize, 30))
.Select(c => GameComment.CreateFromEntity(c, token.UserId)) .ToListAsync()).ToSerializableList(c => GameComment.CreateFromEntity(c, token.UserId));
.ToListAsync();
return this.Ok(new CommentListResponse(comments)); return this.Ok(new CommentListResponse(comments));
} }

View file

@ -167,13 +167,12 @@ public class PhotosController : ControllerBase
if (slotType == "developer") id = await SlotHelper.GetPlaceholderSlotId(this.database, id, SlotType.Developer); if (slotType == "developer") id = await SlotHelper.GetPlaceholderSlotId(this.database, id, SlotType.Developer);
List<GamePhoto> photos = await this.database.Photos.Include(p => p.PhotoSubjects) List<GamePhoto> photos = (await this.database.Photos.Include(p => p.PhotoSubjects)
.Where(p => p.SlotId == id) .Where(p => p.SlotId == id)
.OrderByDescending(s => s.Timestamp) .OrderByDescending(s => s.Timestamp)
.Skip(Math.Max(0, pageStart - 1)) .Skip(Math.Max(0, pageStart - 1))
.Take(Math.Min(pageSize, 30)) .Take(Math.Min(pageSize, 30))
.Select(p => GamePhoto.CreateFromEntity(p)) .ToListAsync()).ToSerializableList(GamePhoto.CreateFromEntity);
.ToListAsync();
return this.Ok(new PhotoListResponse(photos)); return this.Ok(new PhotoListResponse(photos));
} }
@ -186,13 +185,12 @@ public class PhotosController : ControllerBase
int targetUserId = await this.database.UserIdFromUsername(user); int targetUserId = await this.database.UserIdFromUsername(user);
if (targetUserId == 0) return this.NotFound(); if (targetUserId == 0) return this.NotFound();
List<GamePhoto> photos = await this.database.Photos.Include(p => p.PhotoSubjects) List<GamePhoto> photos = (await this.database.Photos.Include(p => p.PhotoSubjects)
.Where(p => p.CreatorId == targetUserId) .Where(p => p.CreatorId == targetUserId)
.OrderByDescending(s => s.Timestamp) .OrderByDescending(s => s.Timestamp)
.Skip(Math.Max(0, pageStart - 1)) .Skip(Math.Max(0, pageStart - 1))
.Take(Math.Min(pageSize, 30)) .Take(Math.Min(pageSize, 30))
.Select(p => GamePhoto.CreateFromEntity(p)) .ToListAsync()).ToSerializableList(GamePhoto.CreateFromEntity);
.ToListAsync();
return this.Ok(new PhotoListResponse(photos)); return this.Ok(new PhotoListResponse(photos));
} }
@ -204,13 +202,12 @@ public class PhotosController : ControllerBase
int targetUserId = await this.database.UserIdFromUsername(user); int targetUserId = await this.database.UserIdFromUsername(user);
if (targetUserId == 0) return this.NotFound(); if (targetUserId == 0) return this.NotFound();
List<GamePhoto> photos = await this.database.Photos.Include(p => p.PhotoSubjects) List<GamePhoto> photos = (await this.database.Photos.Include(p => p.PhotoSubjects)
.Where(p => p.PhotoSubjects.Any(ps => ps.UserId == targetUserId)) .Where(p => p.PhotoSubjects.Any(ps => ps.UserId == targetUserId))
.OrderByDescending(s => s.Timestamp) .OrderByDescending(s => s.Timestamp)
.Skip(Math.Max(0, pageStart - 1)) .Skip(Math.Max(0, pageStart - 1))
.Take(Math.Min(pageSize, 30)) .Take(Math.Min(pageSize, 30))
.Select(p => GamePhoto.CreateFromEntity(p)) .ToListAsync()).ToSerializableList(GamePhoto.CreateFromEntity);
.ToListAsync();
return this.Ok(new PhotoListResponse(photos)); return this.Ok(new PhotoListResponse(photos));
} }

View file

@ -37,9 +37,8 @@ public class CollectionController : ControllerBase
GameTokenEntity token = this.GetToken(); GameTokenEntity token = this.GetToken();
List<SlotBase> slots = await this.database.Slots.Where(s => targetPlaylist.SlotIds.Contains(s.SlotId)) List<SlotBase> slots = (await this.database.Slots.Where(s => targetPlaylist.SlotIds.Contains(s.SlotId)).ToListAsync())
.Select(s => SlotBase.CreateFromEntity(s, token)) .ToSerializableList(s => SlotBase.CreateFromEntity(s, token));
.ToListAsync();
int total = targetPlaylist.SlotIds.Length; int total = targetPlaylist.SlotIds.Length;
@ -101,9 +100,8 @@ public class CollectionController : ControllerBase
private async Task<PlaylistResponse> GetUserPlaylists(int userId) private async Task<PlaylistResponse> GetUserPlaylists(int userId)
{ {
List<GamePlaylist> playlists = await this.database.Playlists.Where(p => p.CreatorId == userId) List<GamePlaylist> playlists = (await this.database.Playlists.Where(p => p.CreatorId == userId)
.Select(p => GamePlaylist.CreateFromEntity(p)) .ToListAsync()).ToSerializableList(GamePlaylist.CreateFromEntity);
.ToListAsync();
int total = this.database.Playlists.Count(p => p.CreatorId == userId); int total = this.database.Playlists.Count(p => p.CreatorId == userId);
return new PlaylistResponse return new PlaylistResponse
@ -189,16 +187,16 @@ public class CollectionController : ControllerBase
if (category is CategoryWithUser categoryWithUser) if (category is CategoryWithUser categoryWithUser)
{ {
slots = categoryWithUser.GetSlots(this.database, user, pageStart, pageSize) slots = (await categoryWithUser.GetSlots(this.database, user, pageStart, pageSize)
.Select(s => SlotBase.CreateFromEntity(s, token)) .ToListAsync())
.ToList(); .ToSerializableList(s => SlotBase.CreateFromEntity(s, token));
totalSlots = categoryWithUser.GetTotalSlots(this.database, user); totalSlots = categoryWithUser.GetTotalSlots(this.database, user);
} }
else else
{ {
slots = category.GetSlots(this.database, pageStart, pageSize) slots = category.GetSlots(this.database, pageStart, pageSize)
.Select(s => SlotBase.CreateFromEntity(s, token)) .ToList()
.ToList(); .ToSerializableList(s => SlotBase.CreateFromEntity(s, token));
totalSlots = category.GetTotalSlots(this.database); totalSlots = category.GetTotalSlots(this.database);
} }

View file

@ -47,10 +47,11 @@ public class ListController : ControllerBase
if (pageSize <= 0) return this.BadRequest(); if (pageSize <= 0) return this.BadRequest();
List<SlotBase> queuedLevels = await this.filterListByRequest(gameFilterType, dateFilterType, token.GameVersion, username, ListFilterType.Queue) List<SlotBase> queuedLevels = (await this.filterListByRequest(gameFilterType, dateFilterType, token.GameVersion, username, ListFilterType.Queue)
.Skip(Math.Max(0, pageStart - 1)) .Skip(Math.Max(0, pageStart - 1))
.Take(Math.Min(pageSize, 30)) .Take(Math.Min(pageSize, 30))
.Select(s => SlotBase.CreateFromEntity(s, token)).ToListAsync(); .ToListAsync())
.ToSerializableList(s => SlotBase.CreateFromEntity(s, token));
int total = await this.database.QueuedLevels.CountAsync(q => q.UserId == token.UserId); int total = await this.database.QueuedLevels.CountAsync(q => q.UserId == token.UserId);
int start = pageStart + Math.Min(pageSize, 30); int start = pageStart + Math.Min(pageSize, 30);
@ -119,11 +120,11 @@ public class ListController : ControllerBase
UserEntity? targetUser = await this.database.Users.FirstOrDefaultAsync(u => u.Username == username); UserEntity? targetUser = await this.database.Users.FirstOrDefaultAsync(u => u.Username == username);
if (targetUser == null) return this.Forbid(); if (targetUser == null) return this.Forbid();
List<SlotBase> heartedLevels = await this.filterListByRequest(gameFilterType, dateFilterType, token.GameVersion, username, ListFilterType.FavouriteSlots) List<SlotBase> heartedLevels = (await this.filterListByRequest(gameFilterType, dateFilterType, token.GameVersion, username, ListFilterType.FavouriteSlots)
.Skip(Math.Max(0, pageStart - 1)) .Skip(Math.Max(0, pageStart - 1))
.Take(Math.Min(pageSize, 30)) .Take(Math.Min(pageSize, 30))
.Select(s => SlotBase.CreateFromEntity(s, token)) .ToListAsync()).ToSerializableList(s => SlotBase.CreateFromEntity(s, token));
.ToListAsync();
int total = await this.database.HeartedLevels.CountAsync(q => q.UserId == targetUser.UserId); int total = await this.database.HeartedLevels.CountAsync(q => q.UserId == targetUser.UserId);
int start = pageStart + Math.Min(pageSize, 30); int start = pageStart + Math.Min(pageSize, 30);
@ -191,13 +192,12 @@ public class ListController : ControllerBase
int targetUserId = await this.database.UserIdFromUsername(username); int targetUserId = await this.database.UserIdFromUsername(username);
if (targetUserId == 0) return this.Forbid(); if (targetUserId == 0) return this.Forbid();
List<GamePlaylist> heartedPlaylists = await this.database.HeartedPlaylists.Where(p => p.UserId == targetUserId) List<GamePlaylist> heartedPlaylists = (await this.database.HeartedPlaylists.Where(p => p.UserId == targetUserId)
.Include(p => p.Playlist) .Include(p => p.Playlist)
.Include(p => p.Playlist.Creator) .Include(p => p.Playlist.Creator)
.OrderByDescending(p => p.HeartedPlaylistId) .OrderByDescending(p => p.HeartedPlaylistId)
.Select(p => p.Playlist) .Select(p => p.Playlist)
.Select(p => GamePlaylist.CreateFromEntity(p)) .ToListAsync()).ToSerializableList(GamePlaylist.CreateFromEntity);
.ToListAsync();
int total = await this.database.HeartedPlaylists.CountAsync(p => p.UserId == targetUserId); int total = await this.database.HeartedPlaylists.CountAsync(p => p.UserId == targetUserId);
@ -250,15 +250,13 @@ public class ListController : ControllerBase
if (pageSize <= 0) return this.BadRequest(); if (pageSize <= 0) return this.BadRequest();
List<GameUser> heartedProfiles = await this.database.HeartedProfiles.Include List<GameUser> heartedProfiles = (await this.database.HeartedProfiles.Include(h => h.HeartedUser)
(h => h.HeartedUser)
.OrderBy(h => h.HeartedProfileId) .OrderBy(h => h.HeartedProfileId)
.Where(h => h.UserId == targetUser.UserId) .Where(h => h.UserId == targetUser.UserId)
.Select(h => h.HeartedUser) .Select(h => h.HeartedUser)
.Skip(Math.Max(0, pageStart - 1)) .Skip(Math.Max(0, pageStart - 1))
.Take(Math.Min(pageSize, 30)) .Take(Math.Min(pageSize, 30))
.Select(h => GameUser.CreateFromEntity(h, token.GameVersion)) .ToListAsync()).ToSerializableList(u => GameUser.CreateFromEntity(u, token.GameVersion));
.ToListAsync();
int total = await this.database.HeartedProfiles.CountAsync(h => h.UserId == targetUser.UserId); int total = await this.database.HeartedProfiles.CountAsync(h => h.UserId == targetUser.UserId);

View file

@ -153,14 +153,13 @@ public class ReviewController : ControllerBase
SlotEntity? slot = await this.database.Slots.FirstOrDefaultAsync(s => s.SlotId == slotId); SlotEntity? slot = await this.database.Slots.FirstOrDefaultAsync(s => s.SlotId == slotId);
if (slot == null) return this.BadRequest(); if (slot == null) return this.BadRequest();
List<GameReview> reviews = await this.database.Reviews.ByGameVersion(gameVersion, true) List<GameReview> reviews = (await this.database.Reviews.ByGameVersion(gameVersion, true)
.Where(r => r.SlotId == slotId) .Where(r => r.SlotId == slotId)
.OrderByDescending(r => r.ThumbsUp - r.ThumbsDown) .OrderByDescending(r => r.ThumbsUp - r.ThumbsDown)
.ThenByDescending(r => r.Timestamp) .ThenByDescending(r => r.Timestamp)
.Skip(Math.Max(0, pageStart - 1)) .Skip(Math.Max(0, pageStart - 1))
.Take(Math.Min(pageSize, 30)) .Take(Math.Min(pageSize, 30))
.Select(r => GameReview.CreateFromEntity(r, token)) .ToListAsync()).ToSerializableList(r => GameReview.CreateFromEntity(r, token));
.ToListAsync();
return this.Ok(new ReviewResponse(reviews, reviews.LastOrDefault()?.Timestamp ?? TimeHelper.TimestampMillis, pageStart + Math.Min(pageSize, 30))); return this.Ok(new ReviewResponse(reviews, reviews.LastOrDefault()?.Timestamp ?? TimeHelper.TimestampMillis, pageStart + Math.Min(pageSize, 30)));
@ -179,13 +178,12 @@ public class ReviewController : ControllerBase
if (targetUserId == 0) return this.BadRequest(); if (targetUserId == 0) return this.BadRequest();
List<GameReview> reviews = await this.database.Reviews.ByGameVersion(gameVersion, true) List<GameReview> reviews = (await this.database.Reviews.ByGameVersion(gameVersion, true)
.Where(r => r.ReviewerId == targetUserId) .Where(r => r.ReviewerId == targetUserId)
.OrderByDescending(r => r.Timestamp) .OrderByDescending(r => r.Timestamp)
.Skip(Math.Max(0, pageStart - 1)) .Skip(Math.Max(0, pageStart - 1))
.Take(Math.Min(pageSize, 30)) .Take(Math.Min(pageSize, 30))
.Select(r => GameReview.CreateFromEntity(r, token)) .ToListAsync()).ToSerializableList(r => GameReview.CreateFromEntity(r, token));
.ToListAsync();
return this.Ok(new ReviewResponse(reviews, reviews.LastOrDefault()?.Timestamp ?? TimeHelper.TimestampMillis, pageStart)); return this.Ok(new ReviewResponse(reviews, reviews.LastOrDefault()?.Timestamp ?? TimeHelper.TimestampMillis, pageStart));
} }

View file

@ -322,7 +322,7 @@ public class ScoreController : ControllerBase
// Paginated viewing: if not requesting pageStart, get results around user // Paginated viewing: if not requesting pageStart, get results around user
var pagedScores = rankedScores.Skip(options.PageStart != -1 || myScore == null ? options.PageStart - 1 : myScore.Rank - 3).Take(Math.Min(options.PageSize, 30)); var pagedScores = rankedScores.Skip(options.PageStart != -1 || myScore == null ? options.PageStart - 1 : myScore.Rank - 3).Take(Math.Min(options.PageSize, 30));
List<GameScore> gameScores = pagedScores.Select(ps => GameScore.CreateFromEntity(ps.Score, ps.Rank)).ToList(); List<GameScore> gameScores = pagedScores.ToSerializableList(ps => GameScore.CreateFromEntity(ps.Score, ps.Rank));
return new ScoreboardResponse(options.RootName, gameScores, rankedScores.Count, myScore?.Score.Points ?? 0, myScore?.Rank ?? 0); return new ScoreboardResponse(options.RootName, gameScores, rankedScores.Count, myScore?.Score.Points ?? 0, myScore?.Rank ?? 0);
} }

View file

@ -61,10 +61,10 @@ public class SearchController : ControllerBase
s.SlotId.ToString().Equals(keyword) s.SlotId.ToString().Equals(keyword)
); );
List<SlotBase> slots = await dbQuery.Skip(Math.Max(0, pageStart - 1)) List<SlotBase> slots = (await dbQuery.Skip(Math.Max(0, pageStart - 1))
.Take(Math.Min(pageSize, 30)) .Take(Math.Min(pageSize, 30))
.Select(s => SlotBase.CreateFromEntity(s, token)) .ToListAsync())
.ToListAsync(); .ToSerializableList(s => SlotBase.CreateFromEntity(s, token));
return this.Ok(new GenericSlotResponse(keyName, slots, await dbQuery.CountAsync(), 0)); return this.Ok(new GenericSlotResponse(keyName, slots, await dbQuery.CountAsync(), 0));
} }

View file

@ -40,12 +40,11 @@ public class SlotsController : ControllerBase
int usedSlots = this.database.Slots.Count(s => s.CreatorId == targetUserId); int usedSlots = this.database.Slots.Count(s => s.CreatorId == targetUserId);
List<SlotBase> slots = await this.database.Slots.Where(s => s.CreatorId == targetUserId) List<SlotBase> slots = (await this.database.Slots.Where(s => s.CreatorId == targetUserId)
.ByGameVersion(token.GameVersion, token.UserId == targetUserId) .ByGameVersion(token.GameVersion, token.UserId == targetUserId)
.Skip(Math.Max(0, pageStart - 1)) .Skip(Math.Max(0, pageStart - 1))
.Take(Math.Min(pageSize, usedSlots)) .Take(Math.Min(pageSize, usedSlots))
.Select(s => SlotBase.CreateFromEntity(s, token)) .ToListAsync()).ToSerializableList(s => SlotBase.CreateFromEntity(s, token));
.ToListAsync();
int start = pageStart + Math.Min(pageSize, usedSlots); int start = pageStart + Math.Min(pageSize, usedSlots);
int total = await this.database.Slots.CountAsync(s => s.CreatorId == targetUserId); int total = await this.database.Slots.CountAsync(s => s.CreatorId == targetUserId);
@ -158,12 +157,11 @@ public class SlotsController : ControllerBase
GameVersion gameVersion = token.GameVersion; GameVersion gameVersion = token.GameVersion;
List<SlotBase> slots = await this.database.Slots.ByGameVersion(gameVersion, false, true) List<SlotBase> slots = (await this.database.Slots.ByGameVersion(gameVersion, false, true)
.OrderByDescending(s => s.FirstUploaded) .OrderByDescending(s => s.FirstUploaded)
.Skip(Math.Max(0, pageStart - 1)) .Skip(Math.Max(0, pageStart - 1))
.Take(Math.Min(pageSize, 30)) .Take(Math.Min(pageSize, 30))
.Select(s => SlotBase.CreateFromEntity(s, token)) .ToListAsync()).ToSerializableList(s => SlotBase.CreateFromEntity(s, token));
.ToListAsync();
int start = pageStart + Math.Min(pageSize, ServerConfiguration.Instance.UserGeneratedContentLimits.EntitledSlots); int start = pageStart + Math.Min(pageSize, ServerConfiguration.Instance.UserGeneratedContentLimits.EntitledSlots);
int total = await StatisticsHelper.SlotCountForGame(this.database, token.GameVersion); int total = await StatisticsHelper.SlotCountForGame(this.database, token.GameVersion);
@ -192,13 +190,12 @@ public class SlotsController : ControllerBase
.Select(r => r.SlotId) .Select(r => r.SlotId)
.ToList(); .ToList();
List<SlotBase> slots = await this.database.Slots.ByGameVersion(gameVersion, false, true) List<SlotBase> slots = (await this.database.Slots.ByGameVersion(gameVersion, false, true)
.Where(s => slotIdsWithTag.Contains(s.SlotId)) .Where(s => slotIdsWithTag.Contains(s.SlotId))
.OrderByDescending(s => s.PlaysLBP1) .OrderByDescending(s => s.PlaysLBP1)
.Skip(Math.Max(0, pageStart - 1)) .Skip(Math.Max(0, pageStart - 1))
.Take(Math.Min(pageSize, 30)) .Take(Math.Min(pageSize, 30))
.Select(s => SlotBase.CreateFromEntity(s, token)) .ToListAsync()).ToSerializableList(s => SlotBase.CreateFromEntity(s, token));
.ToListAsync();
int start = pageStart + Math.Min(pageSize, ServerConfiguration.Instance.UserGeneratedContentLimits.EntitledSlots); int start = pageStart + Math.Min(pageSize, ServerConfiguration.Instance.UserGeneratedContentLimits.EntitledSlots);
int total = slotIdsWithTag.Count; int total = slotIdsWithTag.Count;
@ -215,13 +212,12 @@ public class SlotsController : ControllerBase
GameVersion gameVersion = token.GameVersion; GameVersion gameVersion = token.GameVersion;
List<SlotBase> slots = await this.database.Slots.ByGameVersion(gameVersion, false, true) List<SlotBase> slots = (await this.database.Slots.ByGameVersion(gameVersion, false, true)
.ToAsyncEnumerable() .ToAsyncEnumerable()
.OrderByDescending(s => s.RatingLBP1) .OrderByDescending(s => s.RatingLBP1)
.Skip(Math.Max(0, pageStart - 1)) .Skip(Math.Max(0, pageStart - 1))
.Take(Math.Min(pageSize, 30)) .Take(Math.Min(pageSize, 30))
.Select(s => SlotBase.CreateFromEntity(s, token)) .ToListAsync()).ToSerializableList(s => SlotBase.CreateFromEntity(s, token));
.ToListAsync();
int start = pageStart + Math.Min(pageSize, ServerConfiguration.Instance.UserGeneratedContentLimits.EntitledSlots); int start = pageStart + Math.Min(pageSize, ServerConfiguration.Instance.UserGeneratedContentLimits.EntitledSlots);
int total = await StatisticsHelper.SlotCount(this.database); int total = await StatisticsHelper.SlotCount(this.database);
@ -241,13 +237,12 @@ public class SlotsController : ControllerBase
.Select(s => s.SlotId) .Select(s => s.SlotId)
.ToListAsync(); .ToListAsync();
List<SlotBase> slots = await this.database.Slots.Where(s => slotIdsWithTag.Contains(s.SlotId)) List<SlotBase> slots = (await this.database.Slots.Where(s => slotIdsWithTag.Contains(s.SlotId))
.ByGameVersion(token.GameVersion, false, true) .ByGameVersion(token.GameVersion, false, true)
.OrderByDescending(s => s.PlaysLBP1) .OrderByDescending(s => s.PlaysLBP1)
.Skip(Math.Max(0, pageStart - 1)) .Skip(Math.Max(0, pageStart - 1))
.Take(Math.Min(pageSize, 30)) .Take(Math.Min(pageSize, 30))
.Select(s => SlotBase.CreateFromEntity(s, token)) .ToListAsync()).ToSerializableList(s => SlotBase.CreateFromEntity(s, token));
.ToListAsync();
int start = pageStart + Math.Min(pageSize, ServerConfiguration.Instance.UserGeneratedContentLimits.EntitledSlots); int start = pageStart + Math.Min(pageSize, ServerConfiguration.Instance.UserGeneratedContentLimits.EntitledSlots);
int total = slotIdsWithTag.Count; int total = slotIdsWithTag.Count;
@ -262,13 +257,12 @@ public class SlotsController : ControllerBase
if (pageSize <= 0) return this.BadRequest(); if (pageSize <= 0) return this.BadRequest();
List<SlotBase> slots = await this.database.Slots.Where(s => s.TeamPick) List<SlotBase> slots = (await this.database.Slots.Where(s => s.TeamPick)
.ByGameVersion(token.GameVersion, false, true) .ByGameVersion(token.GameVersion, false, true)
.OrderByDescending(s => s.LastUpdated) .OrderByDescending(s => s.LastUpdated)
.Skip(Math.Max(0, pageStart - 1)) .Skip(Math.Max(0, pageStart - 1))
.Take(Math.Min(pageSize, 30)) .Take(Math.Min(pageSize, 30))
.Select(s => SlotBase.CreateFromEntity(s, token)) .ToListAsync()).ToSerializableList(s => SlotBase.CreateFromEntity(s, token));
.ToListAsync();
int start = pageStart + Math.Min(pageSize, ServerConfiguration.Instance.UserGeneratedContentLimits.EntitledSlots); int start = pageStart + Math.Min(pageSize, ServerConfiguration.Instance.UserGeneratedContentLimits.EntitledSlots);
int total = await StatisticsHelper.TeamPickCountForGame(this.database, token.GameVersion); int total = await StatisticsHelper.TeamPickCountForGame(this.database, token.GameVersion);
@ -284,11 +278,10 @@ public class SlotsController : ControllerBase
GameVersion gameVersion = token.GameVersion; GameVersion gameVersion = token.GameVersion;
List<SlotBase> slots = await this.database.Slots.ByGameVersion(gameVersion, false, true) List<SlotBase> slots = (await this.database.Slots.ByGameVersion(gameVersion, false, true)
.OrderBy(_ => EF.Functions.Random()) .OrderBy(_ => EF.Functions.Random())
.Take(Math.Min(pageSize, 30)) .Take(Math.Min(pageSize, 30))
.Select(s => SlotBase.CreateFromEntity(s, token)) .ToListAsync()).ToSerializableList(s => SlotBase.CreateFromEntity(s, token));
.ToListAsync();
int start = pageStart + Math.Min(pageSize, ServerConfiguration.Instance.UserGeneratedContentLimits.EntitledSlots); int start = pageStart + Math.Min(pageSize, ServerConfiguration.Instance.UserGeneratedContentLimits.EntitledSlots);
int total = await StatisticsHelper.SlotCountForGame(this.database, token.GameVersion); int total = await StatisticsHelper.SlotCountForGame(this.database, token.GameVersion);
@ -313,14 +306,13 @@ public class SlotsController : ControllerBase
Random rand = new(); Random rand = new();
List<SlotBase> slots = await this.filterByRequest(gameFilterType, dateFilterType, token.GameVersion) List<SlotBase> slots = (await this.filterByRequest(gameFilterType, dateFilterType, token.GameVersion)
.AsAsyncEnumerable() .AsAsyncEnumerable()
.OrderByDescending(s => s.Thumbsup) .OrderByDescending(s => s.Thumbsup)
.ThenBy(_ => rand.Next()) .ThenBy(_ => rand.Next())
.Skip(Math.Max(0, pageStart - 1)) .Skip(Math.Max(0, pageStart - 1))
.Take(Math.Min(pageSize, 30)) .Take(Math.Min(pageSize, 30))
.Select(s => SlotBase.CreateFromEntity(s, token)) .ToListAsync()).ToSerializableList(s => SlotBase.CreateFromEntity(s, token));
.ToListAsync();
int start = pageStart + Math.Min(pageSize, ServerConfiguration.Instance.UserGeneratedContentLimits.EntitledSlots); int start = pageStart + Math.Min(pageSize, ServerConfiguration.Instance.UserGeneratedContentLimits.EntitledSlots);
int total = await StatisticsHelper.SlotCountForGame(this.database, token.GameVersion); int total = await StatisticsHelper.SlotCountForGame(this.database, token.GameVersion);
@ -345,7 +337,7 @@ public class SlotsController : ControllerBase
Random rand = new(); Random rand = new();
List<SlotBase> slots = await this.filterByRequest(gameFilterType, dateFilterType, token.GameVersion) List<SlotBase> slots = (await this.filterByRequest(gameFilterType, dateFilterType, token.GameVersion)
.AsAsyncEnumerable() .AsAsyncEnumerable()
.OrderByDescending( .OrderByDescending(
// probably not the best way to do this? // probably not the best way to do this?
@ -363,8 +355,7 @@ public class SlotsController : ControllerBase
.ThenBy(_ => rand.Next()) .ThenBy(_ => rand.Next())
.Skip(Math.Max(0, pageStart - 1)) .Skip(Math.Max(0, pageStart - 1))
.Take(Math.Min(pageSize, 30)) .Take(Math.Min(pageSize, 30))
.Select(s => SlotBase.CreateFromEntity(s, token)) .ToListAsync()).ToSerializableList(s => SlotBase.CreateFromEntity(s, token));
.ToListAsync();
int start = pageStart + Math.Min(pageSize, ServerConfiguration.Instance.UserGeneratedContentLimits.EntitledSlots); int start = pageStart + Math.Min(pageSize, ServerConfiguration.Instance.UserGeneratedContentLimits.EntitledSlots);
int total = await StatisticsHelper.SlotCountForGame(this.database, token.GameVersion); int total = await StatisticsHelper.SlotCountForGame(this.database, token.GameVersion);
@ -387,14 +378,13 @@ public class SlotsController : ControllerBase
if (pageSize <= 0) return this.BadRequest(); if (pageSize <= 0) return this.BadRequest();
List<SlotBase> slots = await this.filterByRequest(gameFilterType, dateFilterType, token.GameVersion) List<SlotBase> slots = (await this.filterByRequest(gameFilterType, dateFilterType, token.GameVersion)
.AsAsyncEnumerable() .AsAsyncEnumerable()
.OrderByDescending(s => s.Hearts) .OrderByDescending(s => s.Hearts)
.ThenBy(_ => RandomNumberGenerator.GetInt32(int.MaxValue)) .ThenBy(_ => RandomNumberGenerator.GetInt32(int.MaxValue))
.Skip(Math.Max(0, pageStart - 1)) .Skip(Math.Max(0, pageStart - 1))
.Take(Math.Min(pageSize, 30)) .Take(Math.Min(pageSize, 30))
.Select(s => SlotBase.CreateFromEntity(s, token)) .ToListAsync()).ToSerializableList(s => SlotBase.CreateFromEntity(s, token));
.ToListAsync();
int start = pageStart + Math.Min(pageSize, ServerConfiguration.Instance.UserGeneratedContentLimits.EntitledSlots); int start = pageStart + Math.Min(pageSize, ServerConfiguration.Instance.UserGeneratedContentLimits.EntitledSlots);
int total = await StatisticsHelper.SlotCountForGame(this.database, token.GameVersion); int total = await StatisticsHelper.SlotCountForGame(this.database, token.GameVersion);
@ -443,13 +433,12 @@ public class SlotsController : ControllerBase
foreach (int slotId in orderedPlayersBySlotId) foreach (int slotId in orderedPlayersBySlotId)
{ {
SlotBase? slot = await this.database.Slots.ByGameVersion(token.GameVersion, false, true) SlotEntity? slot = await this.database.Slots.ByGameVersion(token.GameVersion, false, true)
.Where(s => s.SlotId == slotId) .Where(s => s.SlotId == slotId)
.Select(s => SlotBase.CreateFromEntity(s, token))
.FirstOrDefaultAsync(); .FirstOrDefaultAsync();
if (slot == null) continue; // shouldn't happen ever unless the room is borked if (slot == null) continue; // shouldn't happen ever unless the room is borked
slots.Add(slot); slots.Add(SlotBase.CreateFromEntity(slot, token));
} }
int start = pageStart + Math.Min(pageSize, ServerConfiguration.Instance.UserGeneratedContentLimits.EntitledSlots); int start = pageStart + Math.Min(pageSize, ServerConfiguration.Instance.UserGeneratedContentLimits.EntitledSlots);

View file

@ -9,8 +9,8 @@ using System.Text.RegularExpressions;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Xml; using System.Xml;
using System.Xml.Serialization; using System.Xml.Serialization;
using LBPUnion.ProjectLighthouse.Helpers;
using LBPUnion.ProjectLighthouse.Logging; using LBPUnion.ProjectLighthouse.Logging;
using LBPUnion.ProjectLighthouse.Serialization;
using LBPUnion.ProjectLighthouse.Types.Entities.Token; using LBPUnion.ProjectLighthouse.Types.Entities.Token;
using LBPUnion.ProjectLighthouse.Types.Logging; using LBPUnion.ProjectLighthouse.Types.Logging;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
@ -118,7 +118,7 @@ public static partial class ControllerExtensions
} }
root = new XmlRootAttribute(rootElement); root = new XmlRootAttribute(rootElement);
} }
XmlSerializer serializer = new(typeof(T), root); XmlSerializer serializer = LighthouseSerializer.GetSerializer(typeof(T), root);
T? obj = (T?)serializer.Deserialize(new StringReader(bodyString)); T? obj = (T?)serializer.Deserialize(new StringReader(bodyString));
return obj; return obj;
} }

View file

@ -0,0 +1,11 @@
using System;
using System.Collections.Generic;
using System.Linq;
namespace LBPUnion.ProjectLighthouse.Extensions;
public static class QueryExtensions
{
public static List<T2> ToSerializableList<T, T2>(this IEnumerable<T> enumerable, Func<T, T2> selector)
=> enumerable.Select(selector).ToList();
}

View file

@ -2,7 +2,6 @@
using System.Collections; using System.Collections;
using System.Collections.Concurrent; using System.Collections.Concurrent;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO;
using System.Linq; using System.Linq;
using System.Linq.Expressions; using System.Linq.Expressions;
using System.Reflection; using System.Reflection;
@ -18,40 +17,12 @@ namespace LBPUnion.ProjectLighthouse.Serialization;
/// </summary> /// </summary>
public class CustomXmlSerializer : XmlSerializer public class CustomXmlSerializer : XmlSerializer
{ {
private readonly IServiceProvider provider; public CustomXmlSerializer(Type type, XmlRootAttribute rootAttribute) : base(type, rootAttribute)
{ }
public CustomXmlSerializer(Type type, IServiceProvider provider, XmlRootAttribute rootAttribute) : base(type, rootAttribute) public void Serialize(IServiceProvider provider, XmlWriter xmlWriter, object o, XmlSerializerNamespaces namespaces)
{ {
this.provider = provider; this.TriggerCallback(provider, o);
}
public new void Serialize(object o, XmlSerializationWriter xmlSerializationWriter)
{
this.TriggerCallback(o);
base.Serialize(o, xmlSerializationWriter);
}
public new void Serialize(Stream stream, object o)
{
this.TriggerCallback(o);
base.Serialize(stream, o);
}
public new void Serialize(TextWriter textWriter, object o)
{
this.TriggerCallback(o);
base.Serialize(textWriter, o);
}
public new void Serialize(XmlWriter xmlWriter, object o)
{
this.TriggerCallback(o);
base.Serialize(xmlWriter, o);
}
public new void Serialize(XmlWriter xmlWriter, object o, XmlSerializerNamespaces namespaces)
{
this.TriggerCallback(o);
base.Serialize(xmlWriter, o, namespaces); base.Serialize(xmlWriter, o, namespaces);
} }
@ -86,11 +57,12 @@ public class CustomXmlSerializer : XmlSerializer
/// <summary> /// <summary>
/// Recursively finds all properties of an object /// Recursively finds all properties of an object
/// </summary> /// </summary>
/// <param name="provider">The service provider from the ASP.NET request that is used to resolve dependencies</param>
/// <param name="obj">The object to recursively find all properties of</param> /// <param name="obj">The object to recursively find all properties of</param>
/// <param name="alreadyPrepared">A list of type references that have already been prepared to prevent duplicate preparing</param> /// <param name="alreadyPrepared">A list of type references that have already been prepared to prevent duplicate preparing</param>
/// <param name="recursionDepth">A number tracking how deep into the recursion call stack we are to prevent recursive loops</param> /// <param name="recursionDepth">A number tracking how deep into the recursion call stack we are to prevent recursive loops</param>
/// <returns>A list of object references of all properties of the object</returns> /// <returns>A list of object references of all properties of the object</returns>
public void RecursivelyPrepare(object obj, List<INeedsPreparationForSerialization> alreadyPrepared, int recursionDepth = 0) private void RecursivelyPrepare(IServiceProvider provider, object obj, ICollection<INeedsPreparationForSerialization> alreadyPrepared, int recursionDepth = 0)
{ {
if (recursionDepth > 5) return; if (recursionDepth > 5) return;
switch (obj) switch (obj)
@ -98,7 +70,7 @@ public class CustomXmlSerializer : XmlSerializer
case INeedsPreparationForSerialization needsPreparation: case INeedsPreparationForSerialization needsPreparation:
if (alreadyPrepared.Contains(needsPreparation)) break; if (alreadyPrepared.Contains(needsPreparation)) break;
this.PrepareForSerialization(needsPreparation); PrepareForSerialization(provider, needsPreparation);
alreadyPrepared.Add(needsPreparation); alreadyPrepared.Add(needsPreparation);
break; break;
case null: return; case null: return;
@ -131,32 +103,32 @@ public class CustomXmlSerializer : XmlSerializer
case IList list: case IList list:
foreach (object o in list) foreach (object o in list)
{ {
this.RecursivelyPrepare(o, alreadyPrepared, recursionDepth+1); this.RecursivelyPrepare(provider, o, alreadyPrepared, recursionDepth+1);
} }
break; break;
case INeedsPreparationForSerialization nP: case INeedsPreparationForSerialization nP:
if (alreadyPrepared.Contains(nP)) break; if (alreadyPrepared.Contains(nP)) break;
// Prepare object // Prepare object
this.PrepareForSerialization(nP); PrepareForSerialization(provider, nP);
alreadyPrepared.Add(nP); alreadyPrepared.Add(nP);
// Recursively find objects in this INeedsPreparationForSerialization object // Recursively find objects in this INeedsPreparationForSerialization object
this.RecursivelyPrepare(nP, alreadyPrepared, recursionDepth+1); this.RecursivelyPrepare(provider, nP, alreadyPrepared, recursionDepth+1);
break; break;
case ILbpSerializable serializable: case ILbpSerializable serializable:
// Recursively find objects in this ILbpSerializable object // Recursively find objects in this ILbpSerializable object
this.RecursivelyPrepare(serializable, alreadyPrepared, recursionDepth+1); this.RecursivelyPrepare(provider, serializable, alreadyPrepared, recursionDepth+1);
break; break;
} }
} }
} }
public void PrepareForSerialization(INeedsPreparationForSerialization obj) private static void PrepareForSerialization(IServiceProvider provider, INeedsPreparationForSerialization obj)
=> LighthouseSerializer.PrepareForSerialization(this.provider, obj); => LighthouseSerializer.PrepareForSerialization(provider, obj);
public void TriggerCallback(object o) private void TriggerCallback(IServiceProvider provider, object o)
{ {
this.RecursivelyPrepare(o, new List<INeedsPreparationForSerialization>()); this.RecursivelyPrepare(provider, o, new List<INeedsPreparationForSerialization>());
} }
} }

View file

@ -10,40 +10,56 @@ using LBPUnion.ProjectLighthouse.Extensions;
using LBPUnion.ProjectLighthouse.Logging; using LBPUnion.ProjectLighthouse.Logging;
using LBPUnion.ProjectLighthouse.Types.Logging; using LBPUnion.ProjectLighthouse.Types.Logging;
using LBPUnion.ProjectLighthouse.Types.Serialization; using LBPUnion.ProjectLighthouse.Types.Serialization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
namespace LBPUnion.ProjectLighthouse.Serialization; namespace LBPUnion.ProjectLighthouse.Serialization;
public static class LighthouseSerializer public static class LighthouseSerializer
{ {
private static readonly Dictionary<Type, CustomXmlSerializer> serializerCache = new();
private static readonly XmlSerializerNamespaces emptyNamespace = new(new[]
{
XmlQualifiedName.Empty,
});
private static readonly XmlWriterSettings defaultWriterSettings = new()
{
OmitXmlDeclaration = true,
CheckCharacters = false,
};
public static CustomXmlSerializer GetSerializer(Type type, XmlRootAttribute? rootAttribute = null)
{
if (serializerCache.TryGetValue(type, out CustomXmlSerializer? value)) return value;
CustomXmlSerializer serializer = new(type, rootAttribute);
serializerCache.Add(type, serializer);
return serializer;
}
public static string Serialize(IServiceProvider serviceProvider, ILbpSerializable? serializableObject) public static string Serialize(IServiceProvider serviceProvider, ILbpSerializable? serializableObject)
{ {
if (serializableObject == null) return ""; if (serializableObject == null) return "";
XmlRootAttribute? rootAttribute = null; XmlRootAttribute? rootAttribute = null;
if (serializableObject is IHasCustomRoot customRoot) rootAttribute = new XmlRootAttribute(customRoot.GetRoot()); if (serializableObject is IHasCustomRoot customRoot) rootAttribute = new XmlRootAttribute(customRoot.GetRoot());
// Required to omit the xml namespace CustomXmlSerializer serializer = GetSerializer(serializableObject.GetType(), rootAttribute);
XmlSerializerNamespaces namespaces = new();
namespaces.Add(string.Empty, string.Empty);
using StringWriter stringWriter = new(); using StringWriter stringWriter = new();
CustomXmlSerializer serializer = new(serializableObject.GetType(), serviceProvider, rootAttribute);
WriteFullClosingTagXmlWriter xmlWriter = new(stringWriter, WriteFullClosingTagXmlWriter xmlWriter = new(stringWriter, defaultWriterSettings);
new XmlWriterSettings
{ serializer.Serialize(serviceProvider, xmlWriter, serializableObject, emptyNamespace);
OmitXmlDeclaration = true,
CheckCharacters = false,
});
serializer.Serialize(xmlWriter, serializableObject, namespaces);
string finalResult = stringWriter.ToString(); string finalResult = stringWriter.ToString();
stringWriter.Dispose(); stringWriter.Dispose();
return finalResult; return finalResult;
} }
public static string Serialize(this ControllerBase controllerBase, ILbpSerializable serializableObject)
=> Serialize(controllerBase.Request.HttpContext.RequestServices, serializableObject);
public static void PrepareForSerialization(IServiceProvider serviceProvider, INeedsPreparationForSerialization serializableObject) public static void PrepareForSerialization(IServiceProvider serviceProvider, INeedsPreparationForSerialization serializableObject)
{ {
MethodInfo? methodInfo = serializableObject.GetType().GetMethod("PrepareSerialization"); MethodInfo? methodInfo = serializableObject.GetType().GetMethod("PrepareSerialization");

View file

@ -41,7 +41,7 @@ public abstract class Category
List<SlotBase> slots = new(); List<SlotBase> slots = new();
SlotEntity? previewSlot = this.GetPreviewSlot(database); SlotEntity? previewSlot = this.GetPreviewSlot(database);
if (previewSlot != null) if (previewSlot != null)
slots.Add(SlotBase.CreateFromEntity(this.GetPreviewSlot(database), GameVersion.LittleBigPlanet3, -1)); slots.Add(SlotBase.CreateFromEntity(previewSlot, GameVersion.LittleBigPlanet3, -1));
int totalSlots = this.GetTotalSlots(database); int totalSlots = this.GetTotalSlots(database);
return GameCategory.CreateFromEntity(this, new GenericSlotResponse(slots, totalSlots, 2)); return GameCategory.CreateFromEntity(this, new GenericSlotResponse(slots, totalSlots, 2));

View file

@ -4,7 +4,7 @@ using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Xml.Serialization; using System.Xml.Serialization;
using LBPUnion.ProjectLighthouse.Database; using LBPUnion.ProjectLighthouse.Database;
using LBPUnion.ProjectLighthouse.Serialization; using LBPUnion.ProjectLighthouse.Extensions;
using LBPUnion.ProjectLighthouse.Types.Entities.Profile; using LBPUnion.ProjectLighthouse.Types.Entities.Profile;
using LBPUnion.ProjectLighthouse.Types.Levels; using LBPUnion.ProjectLighthouse.Types.Levels;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
@ -102,7 +102,7 @@ public class GamePhoto : ILbpSerializable, INeedsPreparationForSerialization
MediumHash = entity.MediumHash, MediumHash = entity.MediumHash,
LargeHash = entity.MediumHash, LargeHash = entity.MediumHash,
PlanHash = entity.PlanHash, PlanHash = entity.PlanHash,
Subjects = entity.PhotoSubjects.Select(GamePhotoSubject.CreateFromEntity).ToList(), Subjects = entity.PhotoSubjects.ToSerializableList(GamePhotoSubject.CreateFromEntity),
}; };
} }