Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

存在本地歌词跳过搜索 #11

Merged
merged 7 commits into from
Sep 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions M.Resources/Localisation/LLin/Plugins/CloudMusicStrings.zh.resx
Original file line number Diff line number Diff line change
Expand Up @@ -108,4 +108,19 @@
<data name="offset_adjust_to_lyric" xml:space="preserve">
<value>对其偏移至该歌词</value>
</data>
<data name="search_state_fail" xml:space="preserve">
<value>未找到歌曲或信息不匹配</value>
</data>
<data name="search_state_searching" xml:space="preserve">
<value>搜索中</value>
</data>
<data name="search_state_fuzzy_searching" xml:space="preserve">
<value>模糊搜索中</value>
</data>
<data name="search_state_success" xml:space="preserve">
<value>已就绪</value>
</data>
<data name="search_by_id" xml:space="preserve">
<value>按网易云ID搜索歌词</value>
</data>
</root>
9 changes: 9 additions & 0 deletions M.Resources/Localisation/LLin/Plugins/StpStrings.zh.resx
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,13 @@
<data name="bars_per_visual" xml:space="preserve">
<value>频谱密度</value>
</data>
<data name="type_a_settings" xml:space="preserve">
<value>Type A 设置</value>
</data>
<data name="type_b_settings" xml:space="preserve">
<value>Type B 设置</value>
</data>
<data name="spinning_cover_and_visualizer" xml:space="preserve">
<value>旋转唱片</value>
</data>
</root>
3 changes: 3 additions & 0 deletions M.Resources/Localisation/LLin/Plugins/YaspStrings.zh.resx
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,7 @@
<data name="use_avatar_for_coverii" xml:space="preserve">
<value>(封面II) 使用头像作为封面</value>
</data>
<data name="panel_type" xml:space="preserve">
<value>面板样式</value>
</data>
</root>
2 changes: 1 addition & 1 deletion M.Resources/M.Resources.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@
<EmbeddedResource Include="Documents\**\*" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="ppy.osu.Framework" Version="2024.205.0" />
<PackageReference Include="ppy.osu.Framework" Version="2024.912.0" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Net.Http;
using System.Text.Encodings.Web;
using System.Threading;
using Newtonsoft.Json;
using osu.Framework.Allocation;
using osu.Framework.Bindables;
using osu.Framework.Localisation;
using osu.Framework.Platform;
using osu.Game.Beatmaps;
using osu.Game.Rulesets.IGPlayer.Feature.Player.Misc;
using osu.Game.Rulesets.IGPlayer.Feature.Player.Plugins.Bundle.CloudMusic.Misc;
using osu.Game.Rulesets.IGPlayer.Localisation.LLin.Plugins;
using Component = osu.Framework.Graphics.Component;

namespace osu.Game.Rulesets.IGPlayer.Feature.Player.Plugins.Bundle.CloudMusic.Helper
Expand All @@ -22,16 +23,16 @@ public partial class LyricProcessor : Component

public enum SearchState
{
[Description("未找到歌曲或信息不匹配")]
[LocalisableDescription(typeof(CloudMusicStrings), nameof(CloudMusicStrings.SearchStateFail))]
Fail,

[Description("搜索中")]
[LocalisableDescription(typeof(CloudMusicStrings), nameof(CloudMusicStrings.SearchStateSearching))]
Searching,

[Description("模糊搜索中")]
[LocalisableDescription(typeof(CloudMusicStrings), nameof(CloudMusicStrings.SearchStateFuzzySearching))]
FuzzySearching,

[Description("已就绪")]
[LocalisableDescription(typeof(CloudMusicStrings), nameof(CloudMusicStrings.SearchStateSuccess))]
Success
}

Expand Down Expand Up @@ -73,26 +74,15 @@ public void Search(SearchOption searchOption)
var onFinish = searchOption.OnFinish;
var onFail = searchOption.OnFail;

if (!searchOption.NoLocalFile)
if (!searchOption.NoLocalFile && searchOption.NoRetry)
{
try
{
string filePath = $"custom/lyrics/beatmap-{beatmap.BeatmapSetInfo.ID}.json";

string content = File.ReadAllText(storage.GetFullPath(filePath, true));

var deserializeObject = JsonConvert.DeserializeObject<APILyricResponseRoot>(content);
var localLyrics = GetLocalLyrics(beatmap);

if (deserializeObject != null)
{
onFinish?.Invoke(deserializeObject);
setState(SearchState.Success);
return;
}
}
catch
if (localLyrics != null)
{
//忽略异常
setState(SearchState.Success);
onFinish?.Invoke(localLyrics);
return;
}
}

Expand All @@ -116,7 +106,7 @@ public void Search(SearchOption searchOption)
//处理要搜索的歌名: "标题 艺术家"
string title = beatmap.Metadata.GetTitle();
string artist = searchOption.NoArtist ? string.Empty : $" {beatmap.Metadata.GetArtist()}";
string target = encoder.Encode($"{title}{artist}");
string target = encoder.Encode($"{title} {artist}");

var req = new APISearchRequest(target);

Expand Down Expand Up @@ -147,6 +137,24 @@ public void Search(SearchOption searchOption)
currentSearchRequest = req;
}

public APILyricResponseRoot? GetLocalLyrics(WorkingBeatmap beatmap)
{
APILyricResponseRoot? deserializedObject = null;

try
{
string path = storage.GetFullPath(lyricFilePath(beatmap), true);
string content = File.ReadAllText(path);
deserializedObject = JsonConvert.DeserializeObject<APILyricResponseRoot>(content);
}
catch
{
//忽略异常
}

return deserializedObject;
}

/// <summary>
/// 通过给定的网易云音乐ID搜索歌曲
/// </summary>
Expand Down Expand Up @@ -181,6 +189,8 @@ public void SearchByNeteaseID(long id, WorkingBeatmap beatmap, Action<APILyricRe
onSongSearchRequestFinish(meta, null);
}

private static string lyricFilePath(WorkingBeatmap beatmap) => $"custom/lyrics/beatmap-{beatmap.BeatmapSetInfo.ID}.json";

/// <summary>
/// 当歌曲搜索请求完成后...
/// </summary>
Expand Down Expand Up @@ -260,15 +270,12 @@ private void onSongSearchRequestFinish(RequestFinishMeta meta, APISearchRequest?
[Resolved]
private Storage storage { get; set; } = null!;

public void WriteLrcToFile(APILyricResponseRoot? responseRoot, WorkingBeatmap working)
public void WriteLrcToFile(APILyricResponseRoot? responseRoot, WorkingBeatmap beatmap)
{
try
{
string target = $"custom/lyrics/beatmap-{working.BeatmapSetInfo.ID}.json";

string serializeObject = JsonConvert.SerializeObject(responseRoot);

File.WriteAllText(storage.GetFullPath(target, true), serializeObject);
string serializedObject = JsonConvert.SerializeObject(responseRoot);
File.WriteAllText(storage.GetFullPath(lyricFilePath(beatmap), true), serializedObject);
}
catch (Exception e)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ public struct RequestFinishMeta
/// <returns>相似度百分比</returns>
public float GetSimiliarPrecentage()
{
string neteaseTitle = GetNeteaseTitle();
string ourTitle = SourceBeatmap?.Metadata.GetTitle() ?? string.Empty;
string neteaseTitle = GetNeteaseTitle().ToLowerInvariant();
string ourTitle = SourceBeatmap?.Metadata.GetTitle().ToLowerInvariant() ?? string.Empty;

string source = neteaseTitle.Length > ourTitle.Length ? neteaseTitle : ourTitle;
string target = neteaseTitle.Length > ourTitle.Length ? ourTitle : neteaseTitle;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -284,15 +284,24 @@ public void RefreshLyric(bool noLocalFile = false)
Lyrics.Clear();
currentResponseRoot = null;
CurrentLine = null;

Offset.Value = 0d;

if (UserDefinitionHelper.BeatmapMetaHaveDefinition(CurrentWorkingBeatmap.BeatmapInfo, out long neid))
GetLyricFor(neid);
else if (UserDefinitionHelper.OnlineIDHaveDefinition(CurrentWorkingBeatmap.BeatmapSetInfo.OnlineID, out neid))
GetLyricFor(neid);
var localLyrics = LyricProcessor.GetLocalLyrics(CurrentWorkingBeatmap);

if (noLocalFile || localLyrics == null)
{
if (UserDefinitionHelper.BeatmapMetaHaveDefinition(CurrentWorkingBeatmap.BeatmapInfo, out long neid))
GetLyricFor(neid);
else if (UserDefinitionHelper.OnlineIDHaveDefinition(CurrentWorkingBeatmap.BeatmapSetInfo.OnlineID, out neid))
GetLyricFor(neid);
else
LyricProcessor.Search(SearchOption.From(CurrentWorkingBeatmap, noLocalFile, onLyricRequestFinished, onLyricRequestFail, TitleSimilarThreshold.Value));
}
else
LyricProcessor.Search(SearchOption.From(CurrentWorkingBeatmap, noLocalFile, onLyricRequestFinished, onLyricRequestFail, TitleSimilarThreshold.Value));
{
LyricProcessor.State.Value = LyricProcessor.SearchState.Success;
onLyricRequestFinished(localLyrics);
}
}

private double targetTime => track.CurrentTime + Offset.Value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public List<Lyric> ToLyricList()
//可能存在一行歌词多个时间,所以先创建列表
List<Lyric> lyrics = new List<Lyric>();

//Logging.Log($"处理歌词: {lyricString}");
//Logger.Log($"处理歌词: {lyricString}");

bool propertyDetected = false;
string propertyName = string.Empty;
Expand Down Expand Up @@ -107,7 +107,7 @@ public List<Lyric> ToLyricList()
string propertyName = string.Empty;
string lyricContent = string.Empty;

IList<int> times = new List<int>();
List<int> times = [];

//Logging.Log($"处理翻译歌词: {tlyricString}");

Expand Down Expand Up @@ -155,7 +155,7 @@ public List<Lyric> ToLyricList()
foreach (var lrc in result.FindAll(l => l.Time == time))
{
lrc.TranslatedString = lyricContent;
//Logging.Log($"设置歌词歌词: {lrc}");
//Logger.Log($"设置歌词: {lrc}");
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

如果没有什么特殊原因的话Logger调用最好替换为Logging来避免和osu的日志混在一起

}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ public static class StringExtensions
public static int ToMilliseconds(this string src)
{
string[] spilt = src.Contains(':')
? src.Split(":")
: src.Split(".", 2);
? src.Split(':')
: src.Split('.', 2);

if (spilt.Length < 2)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using osu.Framework.Allocation;
using osu.Framework.Bindables;
using osu.Framework.Extensions.Color4Extensions;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
Expand Down Expand Up @@ -36,13 +37,14 @@ public partial class LyricPiece : DrawableLyric, IHasTooltip, IHasContextMenu
new OsuMenuItem(
CloudMusicStrings.AdjustOffsetToLyric.ToString(),
MenuItemType.Standard,
() => plugin.Offset.Value = Value.Time - mvisScreen.CurrentTrack.CurrentTime)
() => plugin.Offset.Value = Value.Time - llinScreen.CurrentTrack.CurrentTime)
};

private Box hoverBox = null!;
private OsuSpriteText contentText = null!;
private OsuSpriteText translateText = null!;
private OsuSpriteText timeText = null!;
private readonly BindableDouble offset = new BindableDouble();

public LyricPiece(Lyric lrc)
{
Expand All @@ -58,7 +60,7 @@ public LyricPiece()
}

[Resolved]
private IImplementLLin mvisScreen { get; set; } = null!;
private IImplementLLin llinScreen { get; set; } = null!;

[Resolved]
private CustomColourProvider colourProvider { get; set; } = null!;
Expand Down Expand Up @@ -164,23 +166,30 @@ private void load()
{
bgBox.Colour = colourProvider.Highlight1.Opacity(isCurrent ? 1 : 0);
}, true);
offset.BindValueChanged(_ => Schedule(() => UpdateValue(Value)), true);
}

private bool isCurrent_real;
private bool isCurrentReal;

private bool isCurrent
{
get => isCurrent_real;
get => isCurrentReal;
set
{
bgBox.FadeColour(colourProvider.Highlight1.Opacity(value ? 1 : 0), 300, Easing.OutQuint);
textFillFlow.FadeColour(value ? Color4.Black : Color4.White, 300, Easing.OutQuint);
timeText.FadeColour(value ? Color4.Black : Color4.White, 300, Easing.OutQuint);

isCurrent_real = value;
isCurrentReal = value;
}
}

protected override void LoadComplete()
{
base.LoadComplete();
offset.BindTo(plugin.Offset);
}

protected override void Update()
{
isCurrent = plugin.CurrentLine != null && plugin.CurrentLine.Equals(Value);
Expand All @@ -196,26 +205,26 @@ protected override void UpdateValue(Lyric lyric)
contentText.Text = lyric.Content;
translateText.Text = lyric.TranslatedString;

var timeSpan = TimeSpan.FromMilliseconds(lyric.Time);
var timeSpan = TimeSpan.FromMilliseconds(Math.Max(lyric.Time - offset.Value, 0));
timeText.Text = $"{timeSpan:mm\\:ss\\.fff}";
TooltipText = $"{timeText.Text}"
+ (string.IsNullOrEmpty(lyric.Content)
? ""
? string.Empty
: $"- {lyric.Content}")
+ (string.IsNullOrEmpty(lyric.TranslatedString)
? ""
? string.Empty
: $"- {lyric.TranslatedString}");

haveLyric = string.IsNullOrEmpty(lyric.Content);
haveLyric = !string.IsNullOrWhiteSpace(lyric.Content);

Colour = haveLyric
? Color4Extensions.FromHex(@"555")
: Color4.White;
? Color4.White
: Color4Extensions.FromHex(@"555");
}

protected override bool OnClick(ClickEvent e)
{
mvisScreen.SeekTo(Value.Time + 1);
llinScreen.SeekTo(Value.Time + 1 - offset.Value);
return base.OnClick(e);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ private void load(LyricPlugin plugin)
{
this.Schedule(() =>
{
statusText.Text = $"{v.NewValue.GetDescription()}";
statusText.Text = v.NewValue.GetLocalisableDescription();

var color = Color4.White;

Expand Down Expand Up @@ -191,7 +191,7 @@ private void initToolbox()
Anchor = Anchor.TopRight,
Origin = Anchor.TopRight,
RelativeSizeAxes = Axes.X,
PlaceholderText = "按网易云ID搜索歌词"
PlaceholderText = CloudMusicStrings.SearchById
},
new FillFlowContainer
{
Expand Down Expand Up @@ -243,7 +243,7 @@ private void initToolbox()
plugin.GetLyricFor(id);
else
{
textBox.Text = "";
textBox.Text = string.Empty;
}
};
}
Expand Down
Loading
Loading