2020-09-11 15:20:30 +08:00
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
using System ;
using System.IO ;
using System.Linq ;
using System.Threading.Tasks ;
using NUnit.Framework ;
using osu.Framework.Allocation ;
using osu.Framework.Platform ;
using osu.Game.IO.Archives ;
using osu.Game.Skinning ;
using SharpCompress.Archives.Zip ;
namespace osu.Game.Tests.Skins.IO
{
2020-09-18 17:05:33 +08:00
public class ImportSkinTest : ImportTest
2020-09-11 15:20:30 +08:00
{
[Test]
public async Task TestBasicImport ( )
{
2020-09-17 12:01:34 +08:00
using ( HeadlessGameHost host = new CleanRunHeadlessGameHost ( nameof ( ImportSkinTest ) ) )
2020-09-11 15:20:30 +08:00
{
try
{
2020-09-18 17:05:33 +08:00
var osu = LoadOsuIntoHost ( host ) ;
2020-09-11 15:20:30 +08:00
2020-09-18 17:05:33 +08:00
var imported = await loadSkinIntoOsu ( osu , new ZipArchiveReader ( createOsk ( "test skin" , "skinner" ) , "skin.osk" ) ) ;
2020-09-11 15:20:30 +08:00
Assert . That ( imported . Name , Is . EqualTo ( "test skin" ) ) ;
Assert . That ( imported . Creator , Is . EqualTo ( "skinner" ) ) ;
}
finally
{
host . Exit ( ) ;
}
}
}
[Test]
2021-10-20 14:22:47 +08:00
public async Task TestImportTwiceWithSameMetadataAndFilename ( )
2020-09-11 15:20:30 +08:00
{
2020-09-17 12:01:34 +08:00
using ( HeadlessGameHost host = new CleanRunHeadlessGameHost ( nameof ( ImportSkinTest ) ) )
2020-09-11 15:20:30 +08:00
{
try
{
2020-09-18 17:05:33 +08:00
var osu = LoadOsuIntoHost ( host ) ;
2020-09-11 15:20:30 +08:00
2020-09-18 17:05:33 +08:00
var imported = await loadSkinIntoOsu ( osu , new ZipArchiveReader ( createOsk ( "test skin" , "skinner" ) , "skin.osk" ) ) ;
2021-10-20 14:22:47 +08:00
var imported2 = await loadSkinIntoOsu ( osu , new ZipArchiveReader ( createOsk ( "test skin" , "skinner" ) , "skin.osk" ) ) ;
2020-09-11 15:20:30 +08:00
2021-10-20 14:22:47 +08:00
Assert . That ( imported2 . ID , Is . EqualTo ( imported . ID ) ) ;
2021-08-17 18:21:22 +08:00
Assert . That ( osu . Dependencies . Get < SkinManager > ( ) . GetAllUserSkins ( true ) . Count , Is . EqualTo ( 1 ) ) ;
2020-09-11 15:20:30 +08:00
// the first should be overwritten by the second import.
2021-10-20 14:22:47 +08:00
Assert . That ( imported . Files . First ( ) . FileInfoID , Is . EqualTo ( imported2 . Files . First ( ) . FileInfoID ) ) ;
}
finally
{
host . Exit ( ) ;
}
}
}
[Test]
public async Task TestImportTwiceWithSameMetadataButDifferentFilename ( )
{
using ( HeadlessGameHost host = new CleanRunHeadlessGameHost ( nameof ( ImportSkinTest ) ) )
{
try
{
var osu = LoadOsuIntoHost ( host ) ;
var imported = await loadSkinIntoOsu ( osu , new ZipArchiveReader ( createOsk ( "test skin" , "skinner" ) , "skin.osk" ) ) ;
var imported2 = await loadSkinIntoOsu ( osu , new ZipArchiveReader ( createOsk ( "test skin" , "skinner" ) , "skin2.osk" ) ) ;
Assert . That ( imported2 . ID , Is . Not . EqualTo ( imported . ID ) ) ;
Assert . That ( osu . Dependencies . Get < SkinManager > ( ) . GetAllUserSkins ( true ) . Count , Is . EqualTo ( 2 ) ) ;
// skin.ini will be rewritten and therefore not match.
Assert . That ( imported . Files . First ( ) . FileInfoID , Is . Not . EqualTo ( imported2 . Files . First ( ) . FileInfoID ) ) ;
2020-09-11 15:20:30 +08:00
}
finally
{
host . Exit ( ) ;
}
}
}
2020-09-11 15:29:14 +08:00
[Test]
public async Task TestImportTwiceWithNoMetadata ( )
{
2020-09-17 12:01:34 +08:00
using ( HeadlessGameHost host = new CleanRunHeadlessGameHost ( nameof ( ImportSkinTest ) ) )
2020-09-11 15:29:14 +08:00
{
try
{
2020-09-18 17:05:33 +08:00
var osu = LoadOsuIntoHost ( host ) ;
2020-09-11 15:29:14 +08:00
// if a user downloads two skins that do have skin.ini files but don't have any creator metadata in the skin.ini, they should both import separately just for safety.
2020-09-18 17:05:33 +08:00
var imported = await loadSkinIntoOsu ( osu , new ZipArchiveReader ( createOsk ( string . Empty , string . Empty ) , "download.osk" ) ) ;
var imported2 = await loadSkinIntoOsu ( osu , new ZipArchiveReader ( createOsk ( string . Empty , string . Empty ) , "download.osk" ) ) ;
2020-09-11 15:29:14 +08:00
Assert . That ( imported2 . ID , Is . Not . EqualTo ( imported . ID ) ) ;
2021-08-17 18:21:22 +08:00
Assert . That ( osu . Dependencies . Get < SkinManager > ( ) . GetAllUserSkins ( true ) . Count , Is . EqualTo ( 2 ) ) ;
2020-09-11 15:29:14 +08:00
2021-08-17 18:21:22 +08:00
Assert . That ( osu . Dependencies . Get < SkinManager > ( ) . GetAllUserSkins ( true ) . First ( ) . Files . First ( ) . FileInfoID , Is . EqualTo ( imported . Files . First ( ) . FileInfoID ) ) ;
Assert . That ( osu . Dependencies . Get < SkinManager > ( ) . GetAllUserSkins ( true ) . Last ( ) . Files . First ( ) . FileInfoID , Is . EqualTo ( imported2 . Files . First ( ) . FileInfoID ) ) ;
2020-09-11 15:29:14 +08:00
}
finally
{
host . Exit ( ) ;
}
}
}
2020-09-11 15:20:30 +08:00
[Test]
public async Task TestImportTwiceWithDifferentMetadata ( )
{
2020-09-17 12:01:34 +08:00
using ( HeadlessGameHost host = new CleanRunHeadlessGameHost ( nameof ( ImportSkinTest ) ) )
2020-09-11 15:20:30 +08:00
{
try
{
2020-09-18 17:05:33 +08:00
var osu = LoadOsuIntoHost ( host ) ;
2020-09-11 15:20:30 +08:00
2020-09-18 17:05:33 +08:00
var imported = await loadSkinIntoOsu ( osu , new ZipArchiveReader ( createOsk ( "test skin v2" , "skinner" ) , "skin.osk" ) ) ;
var imported2 = await loadSkinIntoOsu ( osu , new ZipArchiveReader ( createOsk ( "test skin v2.1" , "skinner" ) , "skin2.osk" ) ) ;
2020-09-11 15:20:30 +08:00
Assert . That ( imported2 . ID , Is . Not . EqualTo ( imported . ID ) ) ;
2021-08-17 18:21:22 +08:00
Assert . That ( osu . Dependencies . Get < SkinManager > ( ) . GetAllUserSkins ( true ) . Count , Is . EqualTo ( 2 ) ) ;
2020-09-11 15:20:30 +08:00
2021-08-17 18:21:22 +08:00
Assert . That ( osu . Dependencies . Get < SkinManager > ( ) . GetAllUserSkins ( true ) . First ( ) . Files . First ( ) . FileInfoID , Is . EqualTo ( imported . Files . First ( ) . FileInfoID ) ) ;
Assert . That ( osu . Dependencies . Get < SkinManager > ( ) . GetAllUserSkins ( true ) . Last ( ) . Files . First ( ) . FileInfoID , Is . EqualTo ( imported2 . Files . First ( ) . FileInfoID ) ) ;
2020-09-11 15:20:30 +08:00
}
finally
{
host . Exit ( ) ;
}
}
}
2021-03-25 03:55:15 +08:00
[Test]
public async Task TestImportUpperCasedOskArchive ( )
{
using ( HeadlessGameHost host = new CleanRunHeadlessGameHost ( nameof ( ImportSkinTest ) ) )
{
try
{
var osu = LoadOsuIntoHost ( host ) ;
var imported = await loadSkinIntoOsu ( osu , new ZipArchiveReader ( createOsk ( "name 1" , "author 1" ) , "skin1.OsK" ) ) ;
Assert . That ( imported . Name , Is . EqualTo ( "name 1" ) ) ;
Assert . That ( imported . Creator , Is . EqualTo ( "author 1" ) ) ;
var imported2 = await loadSkinIntoOsu ( osu , new ZipArchiveReader ( createOsk ( "name 1" , "author 1" ) , "skin1.oSK" ) ) ;
Assert . That ( imported2 . Hash , Is . EqualTo ( imported . Hash ) ) ;
}
finally
{
host . Exit ( ) ;
}
}
}
2021-08-23 19:25:46 +08:00
[Test]
public async Task TestSameMetadataNameDifferentFolderName ( )
{
using ( HeadlessGameHost host = new CleanRunHeadlessGameHost ( nameof ( ImportSkinTest ) ) )
{
try
{
var osu = LoadOsuIntoHost ( host ) ;
var imported = await loadSkinIntoOsu ( osu , new ZipArchiveReader ( createOsk ( "name 1" , "author 1" , false ) , "my custom skin 1" ) ) ;
2021-08-23 19:37:19 +08:00
Assert . That ( imported . Name , Is . EqualTo ( "name 1 [my custom skin 1]" ) ) ;
Assert . That ( imported . Creator , Is . EqualTo ( "author 1" ) ) ;
2021-08-23 19:25:46 +08:00
var imported2 = await loadSkinIntoOsu ( osu , new ZipArchiveReader ( createOsk ( "name 1" , "author 1" , false ) , "my custom skin 2" ) ) ;
2021-08-23 19:37:19 +08:00
Assert . That ( imported2 . Name , Is . EqualTo ( "name 1 [my custom skin 2]" ) ) ;
Assert . That ( imported2 . Creator , Is . EqualTo ( "author 1" ) ) ;
2021-08-23 19:25:46 +08:00
Assert . That ( imported2 . Hash , Is . Not . EqualTo ( imported . Hash ) ) ;
}
finally
{
host . Exit ( ) ;
}
}
}
private MemoryStream createOsk ( string name , string author , bool makeUnique = true )
2020-09-11 15:20:30 +08:00
{
var zipStream = new MemoryStream ( ) ;
using var zip = ZipArchive . Create ( ) ;
2021-08-23 19:25:46 +08:00
zip . AddEntry ( "skin.ini" , generateSkinIni ( name , author , makeUnique ) ) ;
2020-09-11 15:20:30 +08:00
zip . SaveTo ( zipStream ) ;
return zipStream ;
}
2021-08-23 19:25:46 +08:00
private MemoryStream generateSkinIni ( string name , string author , bool makeUnique = true )
2020-09-11 15:20:30 +08:00
{
var stream = new MemoryStream ( ) ;
var writer = new StreamWriter ( stream ) ;
writer . WriteLine ( "[General]" ) ;
writer . WriteLine ( $"Name: {name}" ) ;
writer . WriteLine ( $"Author: {author}" ) ;
2021-08-23 19:25:46 +08:00
if ( makeUnique )
{
writer . WriteLine ( ) ;
writer . WriteLine ( $"# unique {Guid.NewGuid()}" ) ;
}
2020-09-11 15:20:30 +08:00
writer . Flush ( ) ;
return stream ;
}
2020-09-18 17:05:33 +08:00
private async Task < SkinInfo > loadSkinIntoOsu ( OsuGameBase osu , ArchiveReader archive = null )
2020-09-11 15:20:30 +08:00
{
var skinManager = osu . Dependencies . Get < SkinManager > ( ) ;
2021-09-30 18:33:12 +08:00
return ( await skinManager . Import ( archive ) ) . Value ;
2020-09-11 15:20:30 +08:00
}
}
}