Skip to content

Commit

Permalink
meow
Browse files Browse the repository at this point in the history
  • Loading branch information
SollyBunny committed Dec 7, 2024
1 parent f8b6d41 commit fa3bab0
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions src/game/client/components/chat.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 <vector>

#include <engine/console.h>
Expand Down Expand Up @@ -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); }
Expand Down
78 changes: 78 additions & 0 deletions src/game/client/components/tclient/translate.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#include <game/client/gameclient.h>
#include <game/client/lineinput.h>
#include <engine/shared/json.h>

#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<CTranslate *>(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<CHttpRequest> 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);
}
21 changes: 21 additions & 0 deletions src/game/client/components/tclient/translate.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#ifndef GAME_CLIENT_COMPONENTS_TRANSLATE_H
#define GAME_CLIENT_COMPONENTS_TRANSLATE_H
#include <game/client/component.h>

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
1 change: 1 addition & 0 deletions src/game/client/gameclient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ void CGameClient::OnConsoleInit()
&m_PlayerIndicator,
&m_Verify,
&m_Tater,
&m_Translate,
&m_Hud,
&m_Spectator,
&m_Emoticon,
Expand Down
2 changes: 2 additions & 0 deletions src/game/client/gameclient.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit fa3bab0

Please sign in to comment.