Skip to content

Commit

Permalink
Merge branch 'release/v1.6.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
tor4kichi committed Dec 14, 2023
2 parents 1b63c1e + 70ba846 commit a0359d8
Show file tree
Hide file tree
Showing 28 changed files with 604 additions and 297 deletions.
67 changes: 67 additions & 0 deletions Hohoema.Core/Helpers/NumberToKMGTPEZYStringHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

#nullable enable
namespace Hohoema.Helpers;

public static class NumberToKMGTPEZYStringHelper
{
const string KMGTPEZY = "KMGTPEZY";

public static string ToKMGTPEZY(double number)
{
int divCount = -1;
while (number >= 1000.0d)
{
number /= 1000.0d;
divCount++;
}

if (divCount >= KMGTPEZY.Length)
{
throw new NotSupportedException("ヨタより大きい桁数は対応してない");
}
else if (divCount >= 2 /* G 以上なら */)
{
return number.ToString("F2") + KMGTPEZY[divCount];
}
else if (divCount >= 0 /* K 以上なら */)
{
return number.ToString("F0") + KMGTPEZY[divCount];
}
else
{
return number.ToString("F0");
}
}

public static string ToKMGTPEZY(int number)
{
int divCount = -1;
while (number >= 1000)
{
number /= 1000;
divCount++;
}

if (divCount >= KMGTPEZY.Length)
{
throw new NotSupportedException("ヨタより大きい桁数は対応してない");
}
else if (divCount >= 2 /* G 以上なら */)
{
return number.ToString("F2") + KMGTPEZY[divCount];
}
else if (divCount >= 0 /* K 以上なら */)
{
return number.ToString("F0") + KMGTPEZY[divCount];
}
else
{
return number.ToString("F0");
}
}
}
4 changes: 3 additions & 1 deletion Hohoema.Core/Hohoema.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@
<Compile Include="Contracts\Subscriptions\SubscriptionGroupMessages.cs" />
<Compile Include="Contracts\Subscriptions\SubscriptionMessages.cs" />
<Compile Include="Contracts\Subscriptions\SusbcriptionId.cs" />
<Compile Include="Helpers\NumberToKMGTPEZYStringHelper.cs" />
<Compile Include="Helpers\ObservableObjectDebugHelper.cs" />
<Compile Include="IsExternalInit.cs" />
<Compile Include="MessengerObservableExtensions.cs" />
Expand Down Expand Up @@ -197,6 +198,7 @@
<Compile Include="Models\Niconico\Video\NvapiVideoContent.cs" />
<Compile Include="Models\Niconico\User\UserVideoPlaylist.cs" />
<Compile Include="Models\Niconico\User\UserVideoPlaylistSortOption.cs" />
<Compile Include="Models\Player\Video\VideoStreamingSession\DomandStreamingSession.cs" />
<Compile Include="Models\Playlist\BufferedPlaylistItemsSource.cs" />
<Compile Include="Services\Playlist\PlaylistFactory\SubscriptionGroupPlaylistFactory.cs" />
<Compile Include="Services\Playlist\PlaylistFactory\SubscriptionPlaylistFactory.cs" />
Expand Down Expand Up @@ -376,7 +378,7 @@
<Version>2.7.2</Version>
</PackageReference>
<PackageReference Include="NiconicoToolkit">
<Version>0.4.11</Version>
<Version>0.5.3</Version>
</PackageReference>
<PackageReference Include="ReactiveProperty">
<Version>7.12.0</Version>
Expand Down
9 changes: 3 additions & 6 deletions Hohoema.Core/Models/Application/BackupManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ public async Task BackupAsync(StorageFile storageFile, CancellationToken ct = de

PlayerSetting = new PlayerSettingBackupEntry
{
DefaultQuality = _playerSettings.DefaultVideoQuality.ToString(),
DefaultQualityId = _playerSettings.DefaultVideoQualityId?.ToString(),
DefaultLiveQuality = _playerSettings.DefaultLiveQuality.ToString(),
LiveQualityLimit = _playerSettings.LiveQualityLimit.ToString(),
LiveWatchWithLowLatency = _playerSettings.LiveWatchWithLowLatency,
Expand Down Expand Up @@ -488,10 +488,7 @@ public void RestorePlayerSettings(BackupContainer backup)

PlayerSettingBackupEntry p = backup.PlayerSetting;

if (Enum.TryParse<NicoVideoQuality>(p.DefaultQuality, out NicoVideoQuality videoQuality))
{
_playerSettings.DefaultVideoQuality = videoQuality;
}
_playerSettings.DefaultVideoQualityId = p.DefaultQualityId;

if (Enum.TryParse<LiveQualityType>(p.DefaultLiveQuality, out LiveQualityType liveQuality))
{
Expand Down Expand Up @@ -808,7 +805,7 @@ public sealed class NicoRepoSettingsBackupEntry
public sealed class PlayerSettingBackupEntry
{
[JsonPropertyName("defaultQuality")]
public string DefaultQuality { get; set; }
public string DefaultQualityId { get; set; }

[JsonPropertyName("defaultLiveQuality")]
public string DefaultLiveQuality { get; set; }
Expand Down
36 changes: 27 additions & 9 deletions Hohoema.Core/Models/PageNavigation/RestoreNavigationManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

using Hohoema.Infra;
using Hohoema.Models.Playlist;
using NiconicoToolkit.Video;
using System;
using System.Collections.Generic;
using System.Linq;
Expand Down Expand Up @@ -31,14 +32,19 @@ public void SetCurrentPlayerEntry(PlayerEntry entry)
_navigationStackRepository.SetCurrentPlayerEntry(entry);
}


public void SetCurrentPlayerEntry(VideoId videoId, TimeSpan position, PlaylistItemsSourceOrigin? playlistOrigin, string? playlistId)
{
_navigationStackRepository.SetCurrentPlayerEntry(new PlayerEntry(videoId, position, playlistOrigin, playlistId));
}


public void ClearCurrentPlayerEntry()
{
_navigationStackRepository.ClearCurrentPlayerEntry();
}


public PlayerEntry GetCurrentPlayerEntry()
public PlayerEntry? GetCurrentPlayerEntry()
{
return _navigationStackRepository.GetCurrentPlayerContent();
}
Expand All @@ -49,7 +55,8 @@ public void SetCurrentNavigationEntry(PageEntry pageEntry)
_navigationStackRepository.SetCurrentNavigationEntry(pageEntry);
}




public PageEntry GetCurrentNavigationEntry()
{
return _navigationStackRepository.GetCurrentNavigationEntry();
Expand Down Expand Up @@ -83,8 +90,6 @@ public Task<PageEntry[]> GetForwardNavigationEntriesAsync()
return _navigationStackRepository.GetForwardNavigationEntriesAsync();
}



internal class NavigationStackRepository : FlagsRepositoryBase
{

Expand All @@ -106,9 +111,9 @@ public NavigationStackRepository()
};


public PlayerEntry GetCurrentPlayerContent()
public PlayerEntry? GetCurrentPlayerContent()
{
byte[] json = Read<byte[]>(null, CurrentPlayerEntryName);
byte[]? json = Read<byte[]?>(null, CurrentPlayerEntryName);
return json == null ? null : JsonSerializer.Deserialize<PlayerEntry>(json, _options);
}

Expand Down Expand Up @@ -171,12 +176,25 @@ public async Task SetForwardNavigationEntriesAsync(PageEntry[] entries)

public class PlayerEntry
{
public string ContentId { get; set; }
public PlayerEntry()
{
ContentId = "";
}

public PlayerEntry(string contentId, TimeSpan position, PlaylistItemsSourceOrigin? playlistOrigin, string? playlistId)
{
ContentId = contentId;
Position = position;
PlaylistOrigin = playlistOrigin;
PlaylistId = playlistId;
}

public string ContentId { get; set; }

public TimeSpan Position { get; set; }

public PlaylistItemsSourceOrigin? PlaylistOrigin { get; set; }
public string PlaylistId { get; set; }
public string? PlaylistId { get; set; }
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public interface INiconicoVideoSessionProvider
{
VideoId ContentId { get; }
ImmutableArray<NicoVideoQualityEntity> AvailableQualities { get; }
Task<IStreamingSession> CreateVideoSessionAsync(NicoVideoQuality quality);
Task<IStreamingSession> CreateVideoSessionAsync(NicoVideoQualityEntity quality);
}

public class Quality
Expand Down
20 changes: 12 additions & 8 deletions Hohoema.Core/Models/Player/PlayerSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class PlayerSettings : FlagsRepositoryBase

public PlayerSettings()
{
_DefaultVideoQuality = Read(NicoVideoQuality.Midium, nameof(DefaultVideoQuality));
_DefaultVideoQualityId = Read(default(string?), nameof(DefaultVideoQualityId));

_DefaultLiveQuality = Read(LiveQualityType.Normal, nameof(DefaultLiveQuality));
_LiveQualityLimit = Read(LiveQualityLimitType.SuperHigh, nameof(LiveQualityLimit));
Expand Down Expand Up @@ -66,14 +66,11 @@ public PlayerSettings()
}



private NicoVideoQuality _DefaultVideoQuality;


public NicoVideoQuality DefaultVideoQuality
private string? _DefaultVideoQualityId;
public string? DefaultVideoQualityId
{
get => _DefaultVideoQuality;
set => SetProperty(ref _DefaultVideoQuality, value);
get => _DefaultVideoQualityId;
set => SetProperty(ref _DefaultVideoQualityId, value);
}


Expand Down Expand Up @@ -411,4 +408,11 @@ public bool AutoMoveNextVideoOnPlaylistEmpty
}

#endregion


public bool ForceUsingDmcVideoOrigin
{
get => Read(false);
set => Save(value);
}
}
62 changes: 62 additions & 0 deletions Hohoema.Core/Models/Player/Video/NicoVideoQualityEntity.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
#nullable enable
using Hohoema.Helpers;
using System;
using System.Collections.Generic;
using System.Linq;

namespace Hohoema.Models.Niconico.Video;

public sealed class NicoVideoQualityEntity
Expand All @@ -16,6 +21,7 @@ public NicoVideoQualityEntity(
Height = height;
}

public string Label { get; init; }
public bool IsAvailable { get; }

public NicoVideoQuality Quality { get; }
Expand All @@ -24,4 +30,60 @@ public NicoVideoQualityEntity(
public int? Bitrate { get; }
public int? Width { get; }
public int? Height { get; }

readonly static Dictionary<(string, int), string> _bitrateStringCached = new();

private readonly static string[] _videoQualityPostfixItems = new[]
{
"1080p",
"720p",
"480p",
"360p",
"low",
};

public override string ToString()
{
// キャッシュにヒットするかチェック
// stringを出来るだけ比較しない
foreach (var (key, value) in _bitrateStringCached)
{
if (Bitrate == key.Item2 && QualityId.EndsWith(key.Item1, StringComparison.Ordinal))
{
return value;
}
}

// キャッシュヒットしなかった場合
// 画質末尾に対する文字列分割を回避してキャッシュ生成を試行
foreach (var q in _videoQualityPostfixItems)
{
if (QualityId.EndsWith(q, System.StringComparison.Ordinal))
{
var key = (q, Bitrate ?? 0);
if (_bitrateStringCached.TryGetValue(key, out string cachedToString) is false)
{
cachedToString = Bitrate.HasValue
? $"{Label} ({NumberToKMGTPEZYStringHelper.ToKMGTPEZY(Bitrate.Value)}bps)"
: $"{Label}";
_bitrateStringCached.Add(key, cachedToString);
}

return cachedToString;
}
}

// 不明な画質末尾文字列に対しては分割してキャッシュ生成
var newKey = (Label, Bitrate ?? 0);
var outValue = Bitrate.HasValue
? $"{newKey.Item1} ({NumberToKMGTPEZYStringHelper.ToKMGTPEZY(newKey.Item2)}bps)"
: newKey.Item1;
_bitrateStringCached.Add(newKey, outValue);
return outValue;
}

public bool IsSameQuality(string qualityId)
{
return qualityId == QualityId;
}
}
Loading

0 comments on commit a0359d8

Please sign in to comment.