Merge branch 'LBPUnion:main' into main

This commit is contained in:
LumaLivy 2021-11-18 19:34:53 -05:00 committed by GitHub
commit 42f25fd2cc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 74 additions and 19 deletions

View file

@ -60,13 +60,13 @@ jobs:
- name: Process Test Results (Control) - name: Process Test Results (Control)
if: ${{ matrix.os.prettyName == 'Linux' }} if: ${{ matrix.os.prettyName == 'Linux' }}
uses: im-open/process-dotnet-test-results@v2.0.0 uses: im-open/process-dotnet-test-results@v2.0.1
with: with:
github-token: ${{ secrets.GITHUB_TOKEN }} github-token: ${{ secrets.GITHUB_TOKEN }}
- name: Process Test Results - name: Process Test Results
if: ${{ matrix.os.prettyName != 'Linux' }} if: ${{ matrix.os.prettyName != 'Linux' }}
uses: im-open/process-dotnet-test-results@v2.0.0 uses: im-open/process-dotnet-test-results@v2.0.1
with: with:
github-token: ${{ secrets.GITHUB_TOKEN }} github-token: ${{ secrets.GITHUB_TOKEN }}
create-status-check: false create-status-check: false

View file

@ -1,6 +1,7 @@
using System.Diagnostics.CodeAnalysis; using System.Diagnostics.CodeAnalysis;
using System.IO; using System.IO;
using System.Net.Http; using System.Net.Http;
using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Xml.Serialization; using System.Xml.Serialization;
using LBPUnion.ProjectLighthouse.Helpers; using LBPUnion.ProjectLighthouse.Helpers;
@ -55,6 +56,14 @@ namespace LBPUnion.ProjectLighthouse.Tests
return this.Client.SendAsync(requestMessage); return this.Client.SendAsync(requestMessage);
} }
public async Task<HttpResponseMessage> UploadFileEndpointRequest(string filePath)
{
byte[] bytes = Encoding.UTF8.GetBytes(await File.ReadAllTextAsync(filePath));
string hash = HashHelper.Sha1Hash(bytes);
return await this.Client.PostAsync($"/LITTLEBIGPLANETPS3_XML/upload/{hash}", new ByteArrayContent(bytes));
}
public async Task<HttpResponseMessage> UploadFileRequest(string endpoint, string filePath) public async Task<HttpResponseMessage> UploadFileRequest(string endpoint, string filePath)
=> await this.Client.PostAsync(endpoint, new StringContent(await File.ReadAllTextAsync(filePath))); => await this.Client.PostAsync(endpoint, new StringContent(await File.ReadAllTextAsync(filePath)));

View file

@ -17,13 +17,18 @@ namespace LBPUnion.ProjectLighthouse.Tests
User userA = await database.CreateUser("unitTestUser0"); User userA = await database.CreateUser("unitTestUser0");
User userB = await database.CreateUser("unitTestUser1"); User userB = await database.CreateUser("unitTestUser1");
Location l = new(); Location l = new()
{
X = 0,
Y = 0,
};
database.Locations.Add(l); database.Locations.Add(l);
await database.SaveChangesAsync(); await database.SaveChangesAsync();
Slot slotA = new() Slot slotA = new()
{ {
Creator = userA, Creator = userA,
CreatorId = userA.UserId,
Name = "slotA", Name = "slotA",
Location = l, Location = l,
LocationId = l.Id, LocationId = l.Id,
@ -33,6 +38,7 @@ namespace LBPUnion.ProjectLighthouse.Tests
Slot slotB = new() Slot slotB = new()
{ {
Creator = userB, Creator = userB,
CreatorId = userB.UserId,
Name = "slotB", Name = "slotB",
Location = l, Location = l,
LocationId = l.Id, LocationId = l.Id,
@ -49,8 +55,10 @@ namespace LBPUnion.ProjectLighthouse.Tests
LoginResult loginResult = await this.Authenticate(); LoginResult loginResult = await this.Authenticate();
HttpResponseMessage respMessageA = await this.AuthenticatedRequest("LITTLEBIGPLANETPS3_XML/slots/by?u=unitTestUser0", loginResult.AuthTicket); HttpResponseMessage respMessageA = await this.AuthenticatedRequest
HttpResponseMessage respMessageB = await this.AuthenticatedRequest("LITTLEBIGPLANETPS3_XML/slots/by?u=unitTestUser1", loginResult.AuthTicket); ("LITTLEBIGPLANETPS3_XML/slots/by?u=unitTestUser0&pageStart=1&pageSize=1", loginResult.AuthTicket);
HttpResponseMessage respMessageB = await this.AuthenticatedRequest
("LITTLEBIGPLANETPS3_XML/slots/by?u=unitTestUser1&pageStart=1&pageSize=1", loginResult.AuthTicket);
Assert.True(respMessageA.IsSuccessStatusCode); Assert.True(respMessageA.IsSuccessStatusCode);
Assert.True(respMessageB.IsSuccessStatusCode); Assert.True(respMessageB.IsSuccessStatusCode);

View file

@ -17,35 +17,35 @@ namespace LBPUnion.ProjectLighthouse.Tests
[Fact] [Fact]
public async Task ShouldNotAcceptScript() public async Task ShouldNotAcceptScript()
{ {
HttpResponseMessage response = await this.UploadFileRequest("/LITTLEBIGPLANETPS3_XML/upload/scriptTest", "ExampleFiles/TestScript.ff"); HttpResponseMessage response = await this.UploadFileEndpointRequest("ExampleFiles/TestScript.ff");
Assert.False(response.IsSuccessStatusCode); Assert.False(response.IsSuccessStatusCode);
} }
[Fact] [Fact]
public async Task ShouldNotAcceptFarc() public async Task ShouldNotAcceptFarc()
{ {
HttpResponseMessage response = await this.UploadFileRequest("/LITTLEBIGPLANETPS3_XML/upload/farcTest", "ExampleFiles/TestFarc.farc"); HttpResponseMessage response = await this.UploadFileEndpointRequest("ExampleFiles/TestFarc.farc");
Assert.False(response.IsSuccessStatusCode); Assert.False(response.IsSuccessStatusCode);
} }
[Fact] [Fact]
public async Task ShouldNotAcceptGarbage() public async Task ShouldNotAcceptGarbage()
{ {
HttpResponseMessage response = await this.UploadFileRequest("/LITTLEBIGPLANETPS3_XML/upload/garbageTest", "ExampleFiles/TestGarbage.bin"); HttpResponseMessage response = await this.UploadFileEndpointRequest("ExampleFiles/TestGarbage.bin");
Assert.False(response.IsSuccessStatusCode); Assert.False(response.IsSuccessStatusCode);
} }
[Fact] [Fact]
public async Task ShouldAcceptTexture() public async Task ShouldAcceptTexture()
{ {
HttpResponseMessage response = await this.UploadFileRequest("/LITTLEBIGPLANETPS3_XML/upload/textureTest", "ExampleFiles/TestTexture.tex"); HttpResponseMessage response = await this.UploadFileEndpointRequest("ExampleFiles/TestTexture.tex");
Assert.True(response.IsSuccessStatusCode); Assert.True(response.IsSuccessStatusCode);
} }
[Fact] [Fact]
public async Task ShouldAcceptLevel() public async Task ShouldAcceptLevel()
{ {
HttpResponseMessage response = await this.UploadFileRequest("/LITTLEBIGPLANETPS3_XML/upload/levelTest", "ExampleFiles/TestLevel.lvl"); HttpResponseMessage response = await this.UploadFileEndpointRequest("ExampleFiles/TestLevel.lvl");
Assert.True(response.IsSuccessStatusCode); Assert.True(response.IsSuccessStatusCode);
} }
} }

View file

@ -90,6 +90,19 @@ namespace LBPUnion.ProjectLighthouse.Controllers
return this.Ok(); return this.Ok();
} }
[HttpPost("lolcatftw/clear")]
public async Task<IActionResult> ClearQueuedLevels()
{
User? user = await this.database.UserFromRequest(this.Request);
if (user == null) return this.StatusCode(403, "");
this.database.QueuedLevels.RemoveRange(this.database.QueuedLevels.Where(q => q.UserId == user.UserId));
await this.database.SaveChangesAsync();
return this.Ok();
}
#endregion #endregion
#region Hearted Levels #region Hearted Levels

View file

@ -54,7 +54,6 @@ namespace LBPUnion.ProjectLighthouse.Controllers
[AllowSynchronousIo] [AllowSynchronousIo]
public async Task<IActionResult> UploadResource(string hash) public async Task<IActionResult> UploadResource(string hash)
{ {
string assetsDirectory = FileHelper.ResourcePath; string assetsDirectory = FileHelper.ResourcePath;
string path = FileHelper.GetResourcePath(hash); string path = FileHelper.GetResourcePath(hash);
@ -70,6 +69,12 @@ namespace LBPUnion.ProjectLighthouse.Controllers
return this.UnprocessableEntity(); return this.UnprocessableEntity();
} }
if (HashHelper.Sha1Hash(file.Data) != hash)
{
Logger.Log($"File hash does not match the uploaded file! (hash: {hash}, type: {file.FileType})", LoggerLevelResources.Instance);
return this.Conflict();
}
Logger.Log($"File is OK! (hash: {hash}, type: {file.FileType})", LoggerLevelResources.Instance); Logger.Log($"File is OK! (hash: {hash}, type: {file.FileType})", LoggerLevelResources.Instance);
await IOFile.WriteAllBytesAsync(path, file.Data); await IOFile.WriteAllBytesAsync(path, file.Data);
return this.Ok(); return this.Ok();

View file

@ -78,6 +78,18 @@ namespace LBPUnion.ProjectLighthouse.Helpers
public static bool ResourceExists(string hash) => File.Exists(GetResourcePath(hash)); public static bool ResourceExists(string hash) => File.Exists(GetResourcePath(hash));
public static int ResourceSize(string hash)
{
try
{
return (int)new FileInfo(GetResourcePath(hash)).Length;
}
catch
{
return 0;
}
}
public static void EnsureDirectoryCreated(string path) public static void EnsureDirectoryCreated(string path)
{ {
if (!Directory.Exists(path)) Directory.CreateDirectory(path ?? throw new ArgumentNullException(nameof(path))); if (!Directory.Exists(path)) Directory.CreateDirectory(path ?? throw new ArgumentNullException(nameof(path)));

View file

@ -11,7 +11,7 @@ namespace LBPUnion.ProjectLighthouse.Helpers
[SuppressMessage("ReSharper", "UnusedMember.Global")] [SuppressMessage("ReSharper", "UnusedMember.Global")]
public static class HashHelper public static class HashHelper
{ {
// private static readonly SHA1 sha1 = SHA1.Create(); private static readonly SHA1 sha1 = SHA1.Create();
private static readonly SHA256 sha256 = SHA256.Create(); private static readonly SHA256 sha256 = SHA256.Create();
private static readonly Random random = new(); private static readonly Random random = new();
@ -67,11 +67,11 @@ namespace LBPUnion.ProjectLighthouse.Helpers
public static string Sha256Hash(string str) => Sha256Hash(Encoding.UTF8.GetBytes(str)); public static string Sha256Hash(string str) => Sha256Hash(Encoding.UTF8.GetBytes(str));
public static string Sha256Hash(byte[] bytes) public static string Sha256Hash(byte[] bytes) => BitConverter.ToString(sha256.ComputeHash(bytes)).Replace("-", "");
{
byte[] hash = sha256.ComputeHash(bytes); public static string Sha1Hash(string str) => Sha1Hash(Encoding.UTF8.GetBytes(str));
return Encoding.UTF8.GetString(hash, 0, hash.Length);
} public static string Sha1Hash(byte[] bytes) => BitConverter.ToString(sha1.ComputeHash(bytes)).Replace("-", "");
public static string BCryptHash(string str) => BCrypt.Net.BCrypt.HashPassword(str); public static string BCryptHash(string str) => BCrypt.Net.BCrypt.HashPassword(str);

View file

@ -3,6 +3,7 @@ using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema; using System.ComponentModel.DataAnnotations.Schema;
using System.Linq; using System.Linq;
using System.Xml.Serialization; using System.Xml.Serialization;
using LBPUnion.ProjectLighthouse.Helpers;
using LBPUnion.ProjectLighthouse.Serialization; using LBPUnion.ProjectLighthouse.Serialization;
using LBPUnion.ProjectLighthouse.Types.Profiles; using LBPUnion.ProjectLighthouse.Types.Profiles;
@ -195,7 +196,8 @@ namespace LBPUnion.ProjectLighthouse.Types.Levels
public string SerializeResources() public string SerializeResources()
{ {
return this.Resources.Aggregate("", (current, resource) => current + LbpSerializer.StringElement("resource", resource)); return this.Resources.Aggregate("", (current, resource) => current + LbpSerializer.StringElement("resource", resource)) +
LbpSerializer.StringElement("sizeOfResources", this.Resources.Sum(FileHelper.ResourceSize));
} }
public string Serialize(RatedLevel? yourRatingStats = null, VisitedLevel? yourVisitedStats = null) public string Serialize(RatedLevel? yourRatingStats = null, VisitedLevel? yourVisitedStats = null)

View file

@ -62,7 +62,7 @@ Finally, take a break. Chances are that took a while.
## Contributing Tips ## Contributing Tips
### Database ### Database migrations
Some modifications may require updates to the database schema. You can automatically create a migration file by: Some modifications may require updates to the database schema. You can automatically create a migration file by:
@ -72,6 +72,12 @@ Some modifications may require updates to the database schema. You can automatic
you're doing. you're doing.
4. Running `dotnet ef migrations add <NameOfMigrationInPascalCase> --project ProjectLighthouse`. 4. Running `dotnet ef migrations add <NameOfMigrationInPascalCase> --project ProjectLighthouse`.
### Running tests
You can run tests either through your IDE or by running `dotnet tests`.
Keep in mind while running database tests you need to have `LIGHTHOUSE_DB_CONNECTION_STRING` set.
## Compatibility across games and platforms ## Compatibility across games and platforms
| Game | Console (PS3/Vita) | Emulator (RPCS3) | Next-Gen (PS4/PS5) | | Game | Console (PS3/Vita) | Emulator (RPCS3) | Next-Gen (PS4/PS5) |