Skip to content

Commit

Permalink
remove regex lrc lib, use my own, regex sucks
Browse files Browse the repository at this point in the history
  • Loading branch information
Dr.Abc committed Sep 1, 2024
1 parent 1cd1748 commit f596136
Show file tree
Hide file tree
Showing 11 changed files with 186 additions and 405 deletions.
9 changes: 2 additions & 7 deletions ABCEnchance.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,7 @@ $(FreeImageCheckRequirements)</Command>
<ClCompile Include="src\Source\Utility\config.cpp" />
<ClCompile Include="src\Source\Utility\curl.cpp" />
<ClCompile Include="src\Source\Utility\httpclient.cpp" />
<ClCompile Include="src\Source\Utility\liblrc.cpp" />
<ClCompile Include="src\Source\Utility\mymathlib.cpp" />
<ClCompile Include="src\Source\Utility\NeteaseApi.cpp" />
<ClCompile Include="src\Source\Utility\opengl\gl_def.cpp" />
Expand All @@ -267,8 +268,6 @@ $(FreeImageCheckRequirements)</Command>
<ClCompile Include="src\Source\Var\voice_banmgr.cpp" />
<ClCompile Include="src\Source\Var\voice_status.cpp" />
<ClCompile Include="src\Source\viewmodellag.cpp" />
<ClCompile Include="src\thirdparty\liblrc\lrc_parser.cpp" />
<ClCompile Include="src\thirdparty\liblrc\lyrics.cpp" />
<ClCompile Include="src\thirdparty\wadlib\texture.cpp" />
<ClCompile Include="src\thirdparty\wadlib\wadfile.cpp" />
<ClCompile Include="vgui2\BaseUI.cpp" />
Expand Down Expand Up @@ -495,6 +494,7 @@ $(FreeImageCheckRequirements)</Command>
<ClInclude Include="src\Header\Utility\crypto.h" />
<ClInclude Include="src\Header\Utility\curl.h" />
<ClInclude Include="src\Header\Utility\httpclient.h" />
<ClInclude Include="src\Header\Utility\liblrc.h" />
<ClInclude Include="src\Header\Utility\mymathlib.h" />
<ClInclude Include="src\Header\Utility\NeteaseApi.h" />
<ClInclude Include="src\Header\Utility\opengl\gl_def.h" />
Expand Down Expand Up @@ -536,8 +536,6 @@ $(FreeImageCheckRequirements)</Command>
<ClInclude Include="src\thirdparty\Fmod\fmod_memoryinfo.h" />
<ClInclude Include="src\thirdparty\Fmod\fmod_output.h" />
<ClInclude Include="src\thirdparty\formatter.h" />
<ClInclude Include="src\thirdparty\liblrc\lrc_parser.h" />
<ClInclude Include="src\thirdparty\liblrc\lyrics.h" />
<ClInclude Include="src\thirdparty\qrcodegen.h" />
<ClInclude Include="src\thirdparty\wadlib\texture.h" />
<ClInclude Include="src\thirdparty\wadlib\wadfile.h" />
Expand Down Expand Up @@ -644,9 +642,6 @@ $(FreeImageCheckRequirements)</Command>
<ClInclude Include="vgui_controls\WizardPanel.h" />
<ClInclude Include="vgui_controls\WizardSubPanel.h" />
</ItemGroup>
<ItemGroup>
<Text Include="src\thirdparty\liblrc\readme.txt" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
Expand Down
26 changes: 6 additions & 20 deletions ABCEnchance.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,6 @@
<Filter Include="Public\Thirdparty\FMod">
<UniqueIdentifier>{e2d1923a-70a8-4c9d-b847-a7deee85c14b}</UniqueIdentifier>
</Filter>
<Filter Include="Public\Thirdparty\liblrc">
<UniqueIdentifier>{04d26f33-c396-4278-b88d-9b827331b2e7}</UniqueIdentifier>
</Filter>
<Filter Include="Public\Thirdparty\wadlib">
<UniqueIdentifier>{2ed9b323-3371-4758-883e-76bc89d24f96}</UniqueIdentifier>
</Filter>
Expand Down Expand Up @@ -650,12 +647,6 @@
<ClCompile Include="src\Source\Utility\config.cpp">
<Filter>Public\Source\Utility</Filter>
</ClCompile>
<ClCompile Include="src\thirdparty\liblrc\lrc_parser.cpp">
<Filter>Public\Thirdparty\liblrc</Filter>
</ClCompile>
<ClCompile Include="src\thirdparty\liblrc\lyrics.cpp">
<Filter>Public\Thirdparty\liblrc</Filter>
</ClCompile>
<ClCompile Include="src\thirdparty\wadlib\texture.cpp">
<Filter>Public\Thirdparty\wadlib</Filter>
</ClCompile>
Expand Down Expand Up @@ -707,6 +698,9 @@
<ClCompile Include="src\Source\steamrichpresence.cpp">
<Filter>Public\Source</Filter>
</ClCompile>
<ClCompile Include="src\Source\Utility\liblrc.cpp">
<Filter>Public\Source\LIb</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="SourceSDK\Color.h">
Expand Down Expand Up @@ -1468,12 +1462,6 @@
<ClInclude Include="src\thirdparty\Fmod\fmodwindowsstoreapp.h">
<Filter>Public\Thirdparty\FMod</Filter>
</ClInclude>
<ClInclude Include="src\thirdparty\liblrc\lrc_parser.h">
<Filter>Public\Thirdparty\liblrc</Filter>
</ClInclude>
<ClInclude Include="src\thirdparty\liblrc\lyrics.h">
<Filter>Public\Thirdparty\liblrc</Filter>
</ClInclude>
<ClInclude Include="src\thirdparty\wadlib\texture.h">
<Filter>Public\Thirdparty\wadlib</Filter>
</ClInclude>
Expand Down Expand Up @@ -1522,10 +1510,8 @@
<ClInclude Include="src\Header\steamrichpresence.h">
<Filter>Public\Header</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<Text Include="src\thirdparty\liblrc\readme.txt">
<Filter>Public\Thirdparty\liblrc</Filter>
</Text>
<ClInclude Include="src\Header\Utility\liblrc.h">
<Filter>Public\Header\LIb</Filter>
</ClInclude>
</ItemGroup>
</Project>
6 changes: 3 additions & 3 deletions src/Header/HUD/vgui/neteasemusic.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

#include "NeteaseApi.h"
#include "soundengine.h"
#include "liblrc/lrc_parser.h"
#include "liblrc.h"

#include <vgui_controls/Frame.h>
#include "IViewportPanel.h"
Expand Down Expand Up @@ -138,8 +138,8 @@ class CNeteasePanel : public vgui::EditablePanel, public IViewportPanel{
unsigned long m_uiStartPlayOffset = 0;

std::shared_ptr<netease::CMusic> m_pPlaying = nullptr;
std::unique_ptr<lrc::Lyrics> m_pLyric = nullptr;
std::unique_ptr<lrc::Lyrics> m_pTransLyric = nullptr;
std::unique_ptr<lrc::CLrcCollection> m_pLyric = nullptr;
std::unique_ptr<lrc::CLrcCollection> m_pTransLyric = nullptr;
std::shared_ptr<netease::CMy> m_pLogined = nullptr;

PLAYSTATE m_pNowState;
Expand Down
28 changes: 28 additions & 0 deletions src/Header/Utility/liblrc.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#pragma once
#include <string>
#include <vector>

namespace lrc {
using lrcattribute_t = struct lrcattribute_s
{
std::u8string Key = u8"";
std::u8string Value = u8"";
};
class CLrcLine
{
public:
std::u8string CurrentLyric = u8"";
time_t StartTime = 0;
time_t EndTime = 0;
const char* GetLyric();
};
class CLrcCollection {
public:
std::vector<CLrcLine*> Lines = {};
std::vector<lrcattribute_t*> Attributes = {};
size_t Size();
CLrcLine* LyricAt(time_t time);
~CLrcCollection();
};
extern CLrcCollection* LrcParser(const char8_t* lrc);
}
43 changes: 28 additions & 15 deletions src/Source/HUD/vgui/neteasemusic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -475,25 +475,39 @@ void CNeteasePanel::Think() {
//lyric
if (m_pLyric != nullptr && m_pLyric->Size() > 0) {
auto lrc = m_pLyric->LyricAt(pos);
std::wstring szLrc = lrc.lyric;
m_pLyricLable->SetText(szLrc.c_str());
szLrc.resize(szLrc.size() * std::clamp(1.0f - (static_cast<float>(lrc.end_time - pos) / (lrc.end_time - lrc.start_time)), 0.0f, 1.0f));
m_pLyricLableHighlight->SetText(szLrc.c_str());
if (lrc) {
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter;
std::wstring szLrc = converter.from_bytes(reinterpret_cast<const char*>(lrc->CurrentLyric.data()));
m_pLyricLable->SetText(szLrc.c_str());
szLrc.resize(szLrc.size() * std::clamp(1.0f - (static_cast<float>(lrc->EndTime - pos) / (lrc->EndTime - lrc->StartTime)), 0.0f, 1.0f));
m_pLyricLableHighlight->SetText(szLrc.c_str());
}
else {
m_pLyricLable->SetText(L"");
m_pLyricLableHighlight->SetText(L"");
}
}
else {
m_pLyricLable->SetText("");
m_pLyricLableHighlight->SetText("");
m_pLyricLable->SetText(L"");
m_pLyricLableHighlight->SetText(L"");
}
if (m_pTransLyric != nullptr && m_pTransLyric->Size() > 0) {
auto lrc = m_pTransLyric->LyricAt(pos);
std::wstring szLrc = lrc.lyric;
m_pTranslatedLyricLable->SetText(szLrc.c_str());
szLrc.resize(szLrc.size() * std::clamp(1.0f - (static_cast<float>(lrc.end_time - pos) / (lrc.end_time - lrc.start_time)), 0.0f, 1.0f));
m_pTranslatedLyricLableHighlight->SetText(szLrc.c_str());
if (lrc) {
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter;
std::wstring szLrc = converter.from_bytes(reinterpret_cast<const char*>(lrc->CurrentLyric.data()));
m_pTranslatedLyricLable->SetText(szLrc.c_str());
szLrc.resize(szLrc.size() * std::clamp(1.0f - (static_cast<float>(lrc->EndTime - pos) / (lrc->EndTime - lrc->StartTime)), 0.0f, 1.0f));
m_pTranslatedLyricLableHighlight->SetText(szLrc.c_str());
}
else {
m_pTranslatedLyricLable->SetText(L"");
m_pTranslatedLyricLableHighlight->SetText(L"");
}
}
else {
m_pTranslatedLyricLable->SetText("");
m_pTranslatedLyricLableHighlight->SetText("");
m_pTranslatedLyricLable->SetText(L"");
m_pTranslatedLyricLableHighlight->SetText(L"");
}
}
else
Expand Down Expand Up @@ -822,9 +836,8 @@ void CNeteasePanel::PlayListMusic(){
panel->m_pAlbumPanel->SetImage(s_pAlbumImage);
//Set Lyric
if (obj->lyric != nullptr) {
lrc::LrcParser parser;
panel->m_pLyric = parser.ParseString(std::wstring_convert<std::codecvt_utf8<wchar_t>>().from_bytes(obj->lyric->lyric));
panel->m_pTransLyric = parser.ParseString(std::wstring_convert<std::codecvt_utf8<wchar_t>>().from_bytes(obj->lyric->tlyric));
panel->m_pLyric = std::unique_ptr<lrc::CLrcCollection>(lrc::LrcParser(reinterpret_cast<const char8_t*>(obj->lyric->lyric.c_str())));
panel->m_pTransLyric = std::unique_ptr<lrc::CLrcCollection>(lrc::LrcParser(reinterpret_cast<const char8_t*>(obj->lyric->tlyric.c_str())));
}
//Text
panel->m_pMusicNameLable->SetText(obj->music->name.c_str());
Expand Down
119 changes: 119 additions & 0 deletions src/Source/Utility/liblrc.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
#include "liblrc.h"

#include <algorithm>
#include <iomanip>
#include <sstream>

namespace lrc{
CLrcCollection* LrcParser(const char8_t* lrc) {
std::vector<std::u8string> splited;
std::u8string s = lrc;
std::u8string token;
std::basic_istringstream<char8_t> tokenStream(s);
while (std::getline(tokenStream, token, u8'\n')) {
splited.push_back(token);
}

CLrcCollection* collection = new CLrcCollection();
for (auto iter = splited.begin(); iter != splited.end(); iter++) {
std::u8string line = *iter;
if (*line.begin() != u8'[')
continue;
size_t tagend = line.rfind(u8']');
//wtf???
if (tagend == std::u8string::npos)
continue;
//Is Attribute
if (tagend == line.size() - 1) {
bool invalue = false;
lrcattribute_t* attribute = new lrcattribute_t();
for (size_t i = 1; i < line.size() - 1; i++) {
char8_t c = line[i];
if (c == u8':') {
invalue = true;
continue;
}
if (invalue)
attribute->Value += c;
else
attribute->Key += c;
}
collection->Attributes.push_back(attribute);
}
//Is Lyric
else {
std::u8string header = line.substr(0, tagend + 1);
std::u8string lrc = line.substr(tagend + 1);

std::u8string timestamp = u8"";
CLrcLine* lineitem = nullptr;
for (size_t i = 0; i < header.size(); i++) {
char8_t c = header[i];
switch (c)
{
case u8'[': {
lineitem = new CLrcLine();
break;
}
case u8']': {
if (lineitem) {
time_t m, s, ms;
char dem;
std::istringstream ss(reinterpret_cast<const char*>(timestamp.c_str()));
ss >> m >> dem >> s >> dem >> ms;
lineitem->StartTime = m * 60000 + s * 1000 + ms;
lineitem->CurrentLyric = lrc;
collection->Lines.push_back(lineitem);
lineitem = nullptr;
}
break;
}
default:
timestamp += c;
break;
}
}
//wtf line
if (lineitem)
delete lineitem;
}
}
std::sort(collection->Lines.begin(), collection->Lines.end(), [](const CLrcLine* a, const CLrcLine* b) {
return a->StartTime < b->StartTime;
});
if (collection->Lines.size() > 1) {
for (size_t i = 0; i < collection->Lines.size() - 1; ++i) {
collection->Lines[i]->EndTime = collection->Lines[i + 1]->StartTime;
}
}
return collection;
}

const char* CLrcLine::GetLyric(){
return reinterpret_cast<const char*>(CurrentLyric.c_str());
}

size_t CLrcCollection::Size(){
return Lines.size();
}

CLrcLine* CLrcCollection::LyricAt(time_t time){
for (auto iter = Lines.rbegin(); iter != Lines.rend(); iter++) {
auto line = (*iter);
if (line->StartTime <= time)
return line;
}
return nullptr;
}

CLrcCollection::~CLrcCollection() {
for (auto iter = Lines.begin(); iter != Lines.end(); iter++) {
delete (*iter);
}
for (auto iter = Attributes.begin(); iter != Attributes.end(); iter++) {
delete (*iter);
}
Lines.clear();
Attributes.clear();
}
}
Loading

0 comments on commit f596136

Please sign in to comment.