Skip to content

Commit

Permalink
Merge pull request #275 from Founntain/unicode-titles
Browse files Browse the repository at this point in the history
Fixed Unicode handling in song titles and artists
  • Loading branch information
Founntain committed May 5, 2024
2 parents 96f6e19 + 674115b commit 06f1904
Show file tree
Hide file tree
Showing 12 changed files with 176 additions and 143 deletions.
21 changes: 2 additions & 19 deletions OsuPlayer.Data/DataModels/DbMapEntry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,16 @@ namespace OsuPlayer.Data.DataModels;
/// </summary>
public class DbMapEntry : DbMapEntryBase, IMapEntry
{
public string ArtistUnicode { get; init; } = string.Empty;
public string TitleUnicode { get; init; } = string.Empty;
public string AudioFileName { get; init; } = string.Empty;
public string FolderName { get; init; } = string.Empty;
public string FolderPath { get; init; } = string.Empty;
public string FullPath { get; init; } = string.Empty;
public bool UseUnicode { get; set; }

public async Task<string?> FindBackground()
{
var eventCount = 0;

if (string.IsNullOrEmpty(FolderPath))
if (string.IsNullOrWhiteSpace(FolderPath))
return null;

string[] files;
Expand Down Expand Up @@ -64,26 +61,12 @@ public class DbMapEntry : DbMapEntryBase, IMapEntry
break;
}

if (string.IsNullOrEmpty(background))
if (string.IsNullOrWhiteSpace(background))
return null;

var fileName = background.Split(',')[2].Replace("\"", string.Empty);
var path = Path.Combine(FolderPath, fileName);

return File.Exists(path) ? path : null;
}

public override string GetArtist()
{
if (UseUnicode)
return string.IsNullOrEmpty(ArtistUnicode) ? Artist : ArtistUnicode;
return Artist;
}

public override string GetTitle()
{
if (UseUnicode)
return string.IsNullOrEmpty(TitleUnicode) ? Title : TitleUnicode;
return Title;
}
}
13 changes: 9 additions & 4 deletions OsuPlayer.Data/DataModels/DbMapEntryBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,32 +14,37 @@ public class DbMapEntryBase : IMapEntryBase
public long DbOffset { get; init; }
public string? OsuPath { get; init; }
public string Artist { get; init; } = string.Empty;
public string ArtistUnicode { get; init; } = string.Empty;
public string Title { get; init; } = string.Empty;
public string TitleUnicode { get; init; } = string.Empty;
public string Hash { get; init; } = string.Empty;
public int BeatmapSetId { get; init; }
public int TotalTime { get; init; }
public string TotalTimeString => TimeSpan.FromMilliseconds(TotalTime).FormatTime();
public string SongName => GetSongName();
public string ArtistString => GetArtist();
public string TitleString => GetTitle();
public bool UseUnicode { get; set; }

/// <summary>
/// Gets the artist
/// <remarks>may be overridden for usage with <see cref="DbMapEntry.UseUnicode" /></remarks>
/// </summary>
/// <returns>the artist</returns>
public virtual string GetArtist()
public string GetArtist()
{
if (UseUnicode)
return string.IsNullOrWhiteSpace(ArtistUnicode) ? Artist : ArtistUnicode;
return Artist;
}

/// <summary>
/// Gets the title
/// <remarks>may be overridden for usage with <see cref="DbMapEntry.UseUnicode" /></remarks>
/// </summary>
/// <returns>the title</returns>
public virtual string GetTitle()
public string GetTitle()
{
if (UseUnicode)
return string.IsNullOrWhiteSpace(TitleUnicode) ? Title : TitleUnicode;
return Title;
}

Expand Down
3 changes: 0 additions & 3 deletions OsuPlayer.Data/DataModels/Interfaces/IMapEntry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,10 @@

public interface IMapEntry : IMapEntryBase
{
public string ArtistUnicode { get; }
public string TitleUnicode { get; }
public string AudioFileName { get; }
public string FolderName { get; }
public string FolderPath { get; }
public string FullPath { get; }
public bool UseUnicode { get; set; }

/// <summary>
/// Gets the background image of this <see cref="IMapEntry" />
Expand Down
4 changes: 4 additions & 0 deletions OsuPlayer.Data/DataModels/Interfaces/IMapEntryBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ public interface IMapEntryBase : IEquatable<IMapEntryBase>, IComparable<IMapEntr
public IDbReaderFactory DbReaderFactory { get; init; }

public string Artist { get; }
public string ArtistUnicode { get; }
public string Title { get; }
public string TitleUnicode { get; }
public string Hash { get; }
public int BeatmapSetId { get; }
public int TotalTime { get; }
Expand All @@ -16,6 +18,8 @@ public interface IMapEntryBase : IEquatable<IMapEntryBase>, IComparable<IMapEntr
public string ArtistString => GetArtist();
public string TitleString => GetTitle();

public bool UseUnicode { get; set; }

public string GetArtist();

public string GetTitle();
Expand Down
19 changes: 0 additions & 19 deletions OsuPlayer.Data/DataModels/RealmMapEntry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,10 @@ namespace OsuPlayer.Data.DataModels;
public class RealmMapEntry : RealmMapEntryBase, IMapEntry
{
public string BackgroundFileLocation { get; init; } = string.Empty;
public string ArtistUnicode { get; init; } = string.Empty;
public string TitleUnicode { get; init; } = string.Empty;
public string AudioFileName { get; init; } = string.Empty;
public string FolderName { get; init; } = string.Empty;
public string FolderPath { get; init; } = string.Empty;
public string FullPath { get; init; } = string.Empty;
public bool UseUnicode { get; set; }

public override string GetArtist()
{
if (UseUnicode)
return string.IsNullOrEmpty(ArtistUnicode) ? Artist : ArtistUnicode;

return Artist;
}

public override string GetTitle()
{
if (UseUnicode)
return string.IsNullOrEmpty(TitleUnicode) ? Title : TitleUnicode;

return Title;
}

public Task<string?> FindBackground()
{
Expand Down
23 changes: 20 additions & 3 deletions OsuPlayer.Data/DataModels/RealmMapEntryBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,39 @@ public class RealmMapEntryBase : IMapEntryBase
public Guid Id { get; init; }
public string? OsuPath { get; init; }
public string Artist { get; init; } = string.Empty;
public string ArtistUnicode { get; init; } = string.Empty;
public string Title { get; init; } = string.Empty;
public string TitleUnicode { get; init; } = string.Empty;
public string Hash { get; init; } = string.Empty;
public int BeatmapSetId { get; init; }
public int TotalTime { get; init; }
public string TotalTimeString => TimeSpan.FromMilliseconds(TotalTime).FormatTime();
public string SongName => GetSongName();
public string ArtistString => GetArtist();
public string TitleString => GetTitle();

public virtual string GetArtist()
public bool UseUnicode { get; set; }

/// <summary>
/// Gets the artist
/// <remarks>may be overridden for usage with <see cref="DbMapEntry.UseUnicode" /></remarks>
/// </summary>
/// <returns>the artist</returns>
public string GetArtist()
{
if (UseUnicode)
return string.IsNullOrWhiteSpace(ArtistUnicode) ? Artist : ArtistUnicode;
return Artist;
}

public virtual string GetTitle()
/// <summary>
/// Gets the title
/// <remarks>may be overridden for usage with <see cref="DbMapEntry.UseUnicode" /></remarks>
/// </summary>
/// <returns>the title</returns>
public string GetTitle()
{
if (UseUnicode)
return string.IsNullOrWhiteSpace(TitleUnicode) ? Title : TitleUnicode;
return Title;
}

Expand Down
31 changes: 22 additions & 9 deletions OsuPlayer.IO/DbReader/OsuDbReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using OsuPlayer.Data.DataModels;
using OsuPlayer.Data.DataModels.Interfaces;
using OsuPlayer.Interfaces.Service;
using OsuPlayer.IO.Storage.Config;
using OsuPlayer.Services;
using Splat;

Expand All @@ -27,11 +28,13 @@ public OsuDbReader(Stream input, string path, IDbReaderFactory readerFactory) :

public Task<List<IMapEntryBase>?> ReadBeatmaps()
{
using var config = new Config();

var minBeatMaps = new List<IMapEntryBase>();

var ver = ReadInt32();
_osuDbVersion = ver;
var flag = ver is >= 20160408 and < 20191107;
_osuDbVersion = ReadInt32();

var flag = _osuDbVersion is >= 20160408 and < 20191107;

ReadInt32();
ReadBoolean();
Expand Down Expand Up @@ -64,6 +67,8 @@ public OsuDbReader(Stream input, string path, IDbReaderFactory readerFactory) :

ReadFromStream(out var minBeatMap);

minBeatMap.UseUnicode = config.Container.UseSongNameUnicode;

prevId = minBeatMap.BeatmapSetId;
minBeatMaps.Add(minBeatMap);
}
Expand All @@ -78,9 +83,9 @@ public OsuDbReader(Stream input, string path, IDbReaderFactory readerFactory) :
{
var hashes = new Dictionary<string, int>();

var ver = ReadInt32();
_osuDbVersion = ver;
var flag = ver is >= 20160408 and < 20191107;
_osuDbVersion = ReadInt32();

var flag = _osuDbVersion is >= 20160408 and < 20191107;

ReadInt32();
ReadBoolean();
Expand Down Expand Up @@ -219,6 +224,8 @@ public OsuDbReader(Stream input, string path, IDbReaderFactory readerFactory) :
var fullPath = Path.Combine(path, "Songs", folderName, audioFileName);
var folderPath = Path.Combine(path, "Songs", folderName);

using var config = new Config();

return new DbMapEntry
{
DbReaderFactory = _readerFactory,
Expand All @@ -234,7 +241,8 @@ public OsuDbReader(Stream input, string path, IDbReaderFactory readerFactory) :
FolderPath = folderPath,
FullPath = fullPath,
Hash = mapEntryBase.Hash,
TotalTime = mapEntryBase.TotalTime
TotalTime = mapEntryBase.TotalTime,
UseUnicode = config.Container.UseSongNameUnicode
};
}
catch (Exception ex)
Expand All @@ -256,19 +264,22 @@ private void ReadFromStream(out DbMapEntryBase minBeatmap)
var dbOffset = BaseStream.Position;
var artist = string.Intern(ReadString());

string artistUnicode = string.Empty;
string titleUnicode = string.Empty;

if (artist.Length == 0)
artist = "Unknown Artist";

if (_osuDbVersion >= 20121008)
ReadString(true);
artistUnicode = ReadString();

var title = string.Intern(ReadString());

if (title.Length == 0)
title = "Unknown Title";

if (_osuDbVersion >= 20121008)
ReadString(true);
titleUnicode = ReadString();

ReadString(true);
ReadString(true); //Difficulty
Expand Down Expand Up @@ -319,7 +330,9 @@ private void ReadFromStream(out DbMapEntryBase minBeatmap)
DbReaderFactory = _readerFactory,
OsuPath = string.Intern(_path),
Artist = artist,
ArtistUnicode = artistUnicode,
Title = title,
TitleUnicode = titleUnicode,
BeatmapSetId = beatmapSetId,
DbOffset = dbOffset,
Hash = hash,
Expand Down
11 changes: 9 additions & 2 deletions OsuPlayer.IO/DbReader/RealmReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using OsuPlayer.Data.LazerModels.Beatmaps;
using OsuPlayer.Data.LazerModels.Collections;
using OsuPlayer.Data.LazerModels.Files;
using OsuPlayer.IO.Storage.Config;
using Realms;
using Realms.Dynamic;
using Splat;
Expand Down Expand Up @@ -103,6 +104,8 @@ public Task<List<OsuCollection>> GetCollections(string path)
var audioFolderName = Path.Combine($"{audioHash[0]}", $"{audioHash[0]}{audioHash[1]}");
var backgroundFolderName = Path.Combine($"{backgroundHash?[0]}", $"{backgroundHash?[0]}{backgroundHash?[1]}");

using var config = new Config();

var newMap = new RealmMapEntry
{
DbReaderFactory = _readerFactory,
Expand All @@ -120,7 +123,8 @@ public Task<List<OsuCollection>> GetCollections(string path)
BeatmapSetId = mapEntryBase.BeatmapSetId,
FolderName = audioFolderName,
FolderPath = Path.Combine("files", audioFolderName),
FullPath = Path.Combine(path, "files", audioFolderName, audioHash)
FullPath = Path.Combine(path, "files", audioFolderName, audioHash),
UseUnicode = config.Container.UseSongNameUnicode
};

_realm.Dispose();
Expand All @@ -130,6 +134,8 @@ public Task<List<OsuCollection>> GetCollections(string path)

public Task<List<IMapEntryBase>?> ReadBeatmaps()
{
using var config = new Config();

var minBeatMaps = new List<IMapEntryBase>();

var beatmaps = _realm.DynamicApi.All("BeatmapSet").ToList().OfType<DynamicRealmObject>().ToList();
Expand All @@ -156,7 +162,8 @@ public Task<List<OsuCollection>> GetCollections(string path)
BeatmapSetId = beatmapSetId,
Title = title,
TotalTime = (int) totalTime,
Id = id
Id = id,
UseUnicode = config.Container.UseSongNameUnicode
});
}

Expand Down
4 changes: 2 additions & 2 deletions OsuPlayer/Views/PlayerControlViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public string CurrentSongLength

public bool IsPlaying => _isPlaying.Value;

public string TitleText => CurrentSong.Value?.Title ?? "No song is playing";
public string TitleText => CurrentSong.Value?.GetTitle() ?? "No song is playing";

public RepeatMode IsRepeating
{
Expand All @@ -148,7 +148,7 @@ public RepeatMode IsRepeating
}
}

public string ArtistText => CurrentSong.Value?.Artist ?? "please select from song list";
public string ArtistText => CurrentSong.Value?.GetArtist() ?? "please select from song list";

public string SongText => $"{ArtistText} - {TitleText}";

Expand Down

0 comments on commit 06f1904

Please sign in to comment.