From fa3bab09b31ee43c2891dab8a598e225e1ec5479 Mon Sep 17 00:00:00 2001 From: SollyBunny Date: Sat, 7 Dec 2024 13:57:39 +0000 Subject: [PATCH] meow --- CMakeLists.txt | 2 + src/game/client/components/chat.h | 3 + .../client/components/tclient/translate.cpp | 78 +++++++++++++++++++ .../client/components/tclient/translate.h | 21 +++++ src/game/client/gameclient.cpp | 1 + src/game/client/gameclient.h | 2 + 6 files changed, 107 insertions(+) create mode 100644 src/game/client/components/tclient/translate.cpp create mode 100644 src/game/client/components/tclient/translate.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 21f37220720..28ac8126876 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2419,6 +2419,8 @@ if(CLIENT) components/tclient/skinprofiles.h components/tclient/tater.cpp components/tclient/tater.h + components/tclient/translate.cpp + components/tclient/translate.h components/tclient/verify.cpp components/tclient/verify.h components/tclient/warlist.cpp diff --git a/src/game/client/components/chat.h b/src/game/client/components/chat.h index abf80809e8a..884deea49cb 100644 --- a/src/game/client/components/chat.h +++ b/src/game/client/components/chat.h @@ -2,6 +2,7 @@ /* If you are missing that file, acquire a complete release at teeworlds.com. */ #ifndef GAME_CLIENT_COMPONENTS_CHAT_H #define GAME_CLIENT_COMPONENTS_CHAT_H +#include "base/system.h" #include #include @@ -145,6 +146,8 @@ class CChat : public CComponent bool LineShouldHighlight(const char *pLine, const char *pName); void StoreSave(const char *pText); + friend class CTranslate; + public: CChat(); int Sizeof() const override { return sizeof(*this); } diff --git a/src/game/client/components/tclient/translate.cpp b/src/game/client/components/tclient/translate.cpp new file mode 100644 index 00000000000..3fc7ad899ed --- /dev/null +++ b/src/game/client/components/tclient/translate.cpp @@ -0,0 +1,78 @@ +#include +#include +#include + +#include "translate.h" + +CTranslate::CTranslate() +{ + OnReset(); +} + +void CTranslate::ConTranslate(IConsole::IResult *pResult, void *pUserData) +{ + const char *pName; + if (pResult->NumArguments() == 0) + pName = nullptr; + else + pName = pResult->GetString(0); + + CTranslate *pThis = static_cast(pUserData); + pThis->Translate(pName); + +} + +void CTranslate::OnConsoleInit() +{ + Console()->Register("translate", "?r[name]", CFGFLAG_CLIENT, ConTranslate, this, "Translate last message (of a given name)"); +} + +void CTranslate::TranslateLibretranslate(const CChat::CLine *pLine) +{ + static const char FORMAT[] = "{\"q\":\"%s\",\"source\":\"auto\",\"target\":\"%s\",\"format\":\"text\",\"alternatives\":0,\"api-key\":\"%s\"}"; + char aBuf[sizeof(FORMAT) + sizeof(CChat::CLine::m_aText) + 256]; + str_format(aBuf, sizeof(aBuf), FORMAT, pLine->m_aText, "en", "30fc3646-c79e-485f-8db3-74a7c7a52729"); + + std::shared_ptr pGet = HttpGet("https://icosrv.sollybunny.xyz:1623"); + pGet->Timeout(CTimeout{10000, 0, 500, 10}); + + Http()->Run(pGet); + + unsigned char *outBuf; + size_t outBufSize; + pGet->Result(&outBuf, &outBufSize); + printf("%s\n", outBuf); +} + +const CChat::CLine *CTranslate::FindMessage(const char *pName) +{ + // No messages at all + if(GameClient()->m_Chat.m_CurrentLine < 0) + return nullptr; + if(!pName) + return &GameClient()->m_Chat.m_aLines[GameClient()->m_Chat.m_CurrentLine]; + for(int i = GameClient()->m_Chat.m_CurrentLine; i >= 0; --i) + { + if(str_comp(GameClient()->m_Chat.m_aLines[i].m_aName, pName) == 0) + return &GameClient()->m_Chat.m_aLines[i]; + } + for(int i = GameClient()->m_Chat.m_CurrentLine; i >= 0; --i) + { + if(str_comp_nocase(GameClient()->m_Chat.m_aLines[i].m_aName, pName) == 0) + return &GameClient()->m_Chat.m_aLines[i]; + } + return nullptr; + +} + +void CTranslate::Translate(const char *pName) +{ + const CChat::CLine *pLine = FindMessage(pName); + if(!pLine) + { + GameClient()->m_Chat.Echo("No message to translate"); + return; + } + + TranslateLibretranslate(pLine); +} diff --git a/src/game/client/components/tclient/translate.h b/src/game/client/components/tclient/translate.h new file mode 100644 index 00000000000..5872e0a1f2e --- /dev/null +++ b/src/game/client/components/tclient/translate.h @@ -0,0 +1,21 @@ +#ifndef GAME_CLIENT_COMPONENTS_TRANSLATE_H +#define GAME_CLIENT_COMPONENTS_TRANSLATE_H +#include + +class CTranslate : public CComponent +{ + static void ConTranslate(IConsole::IResult *pResult, void *pUserData); + + void TranslateLibretranslate(const CChat::CLine *pLine); + + const CChat::CLine *FindMessage(const char *pName); + void Translate(const char *pName); + +public: + CTranslate(); + virtual int Sizeof() const override { return sizeof(*this); } + + virtual void OnConsoleInit() override; +}; + +#endif diff --git a/src/game/client/gameclient.cpp b/src/game/client/gameclient.cpp index ce9dacccbfe..c4e92b5b4fc 100644 --- a/src/game/client/gameclient.cpp +++ b/src/game/client/gameclient.cpp @@ -145,6 +145,7 @@ void CGameClient::OnConsoleInit() &m_PlayerIndicator, &m_Verify, &m_Tater, + &m_Translate, &m_Hud, &m_Spectator, &m_Emoticon, diff --git a/src/game/client/gameclient.h b/src/game/client/gameclient.h index 0634f310201..711a32f5de2 100644 --- a/src/game/client/gameclient.h +++ b/src/game/client/gameclient.h @@ -64,6 +64,7 @@ #include "components/tclient/rainbow.h" #include "components/tclient/skinprofiles.h" #include "components/tclient/tater.h" +#include "components/tclient/translate.h" #include "components/tclient/verify.h" #include "components/tclient/warlist.h" #include "components/voting.h" @@ -180,6 +181,7 @@ class CGameClient : public IGameClient CSkinProfiles m_SkinProfiles; CBindWheel m_Bindwheel; CTater m_Tater; + CTranslate m_Translate; CPlayerIndicator m_PlayerIndicator; COutlines m_Outlines; CRainbow m_Rainbow;