Skip to content

Commit fde4449

Browse files
committed
translate?
1 parent 48112dd commit fde4449

File tree

5 files changed

+24948
-13
lines changed

5 files changed

+24948
-13
lines changed

hooks/Chat.cpp

+62-13
Original file line numberDiff line numberDiff line change
@@ -34,23 +34,72 @@ void dChatController_AddChat(ChatController* __this, PlayerControl* sourcePlayer
3434
auto player = GetPlayerData(sourcePlayer);
3535
auto local = GetPlayerData(*Game::pLocalPlayer);
3636
std::string message = RemoveHtmlTags(convert_from_string(chatText));
37-
if (State.ReadGhostMessages) {
38-
bool wasDead = false;
39-
40-
if (player != NULL && player->fields.IsDead && local != NULL && !local->fields.IsDead) {
41-
local->fields.IsDead = true;
42-
wasDead = true;
43-
}
37+
if (State.ReadGhostMessages && (player != NULL && player->fields.IsDead && local != NULL && !local->fields.IsDead)) {
38+
local->fields.IsDead = true;
4439
ChatController_AddChat(__this, sourcePlayer, chatText, censor, method);
40+
local->fields.IsDead = false;
41+
}
42+
else ChatController_AddChat(__this, sourcePlayer, chatText, censor, method);
43+
44+
if (!State.translatePlayers.empty()) {
45+
if (IsInGame()) State.rpcQueue.push(new RpcTranslateMessage(RemoveHtmlTags(convert_from_string(NetworkedPlayerInfo_get_PlayerName(player, NULL))), message));
46+
if (IsInLobby()) State.lobbyRpcQueue.push(new RpcTranslateMessage(RemoveHtmlTags(convert_from_string(NetworkedPlayerInfo_get_PlayerName(player, NULL))), message));
47+
}
4548

46-
std::string playerName = convert_from_string(NetworkedPlayerInfo_get_PlayerName(player, nullptr));
47-
auto outfit = GetPlayerOutfit(player);
48-
uint32_t colorId = outfit->fields.ColorId;
49-
if (wasDead) {
50-
local->fields.IsDead = false;
49+
try {
50+
if (message.rfind("/reg ", 0) == 0) {
51+
if (message == "/reg trans clear") {
52+
State.translatePlayers.erase(convert_from_string(player->fields.FriendCode));
53+
} else if (message.rfind("/reg trans ", 0) == 0) {
54+
auto friendCode = convert_from_string(player->fields.FriendCode);
55+
std::stringstream ss;
56+
ss << message.substr(11);
57+
std::string t;
58+
ss >> t;
59+
std::string fromlang = t;
60+
ss >> t;
61+
std::string tolang = t;
62+
auto findt = State.translatePlayers.find(friendCode);
63+
if (findt != State.translatePlayers.end()) {
64+
findt->second.first = fromlang;
65+
findt->second.second = tolang;
66+
} else {
67+
std::pair<std::string, std::string> p = std::make_pair(fromlang, tolang);
68+
State.translatePlayers.emplace(friendCode, p);
69+
}
70+
}
71+
else if (sourcePlayer == (*Game::pLocalPlayer)) {
72+
if (message.rfind("/reg trans-for clear ", 0) == 0) {
73+
std::stringstream ss;
74+
ss << message.substr(21);
75+
std::string t;
76+
ss >> t;
77+
std::string friendCode = t;
78+
State.translatePlayers.erase(friendCode);
79+
} else if (message.rfind("/reg trans-for ", 0) == 0) {
80+
std::stringstream ss;
81+
ss << message.substr(15);
82+
std::string t;
83+
ss >> t;
84+
std::string friendCode = t;
85+
ss >> t;
86+
std::string fromlang = t;
87+
ss >> t;
88+
std::string tolang = t;
89+
auto findt = State.translatePlayers.find(friendCode);
90+
if (findt != State.translatePlayers.end()) {
91+
findt->second.first = fromlang;
92+
findt->second.second = tolang;
93+
} else {
94+
std::pair<std::string, std::string> p = std::make_pair(fromlang, tolang);
95+
State.translatePlayers.emplace(friendCode, p);
96+
}
97+
}
98+
}
5199
}
100+
} catch (...) {
52101
}
53-
else ChatController_AddChat(__this, sourcePlayer, chatText, censor, method);
102+
54103
if (State.Enable_SMAC) {
55104
if (State.SMAC_CheckChat && ((IsInGame() && !State.InMeeting && !player->fields.IsDead) || chatText->fields.m_stringLength > 120)) {
56105
SMAC_OnCheatDetected(sourcePlayer, "Abnormal Chat");

rpc/RpcSnapTo.cpp

+101
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
#include "pch-il2cpp.h"
22
#include "_rpc.h"
33
#include "game.h"
4+
#include "json.hpp"
5+
#include <stdio.h>
6+
#include <queue>
7+
#include <future>
48

59
RpcSnapTo::RpcSnapTo(Vector2 targetVector)
610
{
@@ -68,3 +72,100 @@ void RpcGoneForTarget::Process()
6872
}
6973
}
7074
}
75+
76+
RpcTranslateMessage::RpcTranslateMessage(std::string source, std::string message)
77+
{
78+
this->source = source;
79+
this->message = message;
80+
}
81+
82+
void translate_from_deeplx(PlayerSelection target, std::string source, std::string message, std::string sourcelang, std::string targetlang)
83+
{
84+
try {
85+
nlohmann::json j = {
86+
{"text", message},
87+
{"source_lang", sourcelang},
88+
{"target_lang", targetlang}
89+
};
90+
std::string prefix = "curl -X POST http://localhost:1188/translate -H \"Content-Type: application/json\" -d ";
91+
std::ostringstream o;
92+
std::string s = j.dump();
93+
for (auto c = s.cbegin(); c != s.cend(); c++) {
94+
switch (*c) {
95+
case '\\': o << "\\\\"; break;
96+
case '"': o << "\\\""; break;
97+
default: o << *c;
98+
}
99+
}
100+
prefix += o.str();
101+
FILE *fp = NULL;
102+
char buf[1024];
103+
char result[1000000] = {0};
104+
if ((fp = popen(prefix, "r")) != NULL) {
105+
while (fgets(buf, 1024, fp) != NULL) {
106+
strcat(result, buf);
107+
}
108+
pclose(fp);
109+
fp = NULL;
110+
}
111+
if (!target.validate().has_value()) return;
112+
auto j2 = nlohmann::json::parse(result);
113+
if (j2.find("code") == j2.end()) return;
114+
auto code = j2.at("code");
115+
if (code.is_number_integer() && 200 == code.get<int>()) {
116+
if (j2.find("data") == j2.end()) return;
117+
auto data = j2.at("data");
118+
if (data.is_string()) {
119+
std::string translated = j2.get<std::string>();
120+
std::queue<RPCInterface*>* queue = nullptr;
121+
if (IsInGame())
122+
queue = &State.rpcQueue;
123+
else if (IsInLobby())
124+
queue = &State.lobbyRpcQueue;
125+
else return;
126+
queue->push(new RpcTranslatedMessage(target, source, translated));
127+
}
128+
}
129+
} catch (...) {
130+
}
131+
}
132+
133+
void RpcTranslateMessage::Process()
134+
{
135+
for (auto p : GetAllPlayerControl()) {
136+
if (p == *Game::pLocalPlayer) continue;
137+
auto friendCode = convert_from_string(GetPlayerData(p)->fields.FriendCode);
138+
auto search = State.translatePlayers.find(friendCode);
139+
if (search == State.translatePlayers.end()) continue;
140+
std::string fromlang = search->second.first;
141+
std::string tolang = search->second.second;
142+
std::future<void> ignored = std::async(std::launch::async, translate_from_deeplx, PlayerSelection(p), source, message, fromlang, tolang);
143+
}
144+
}
145+
146+
RpcTranslatedMessage::RpcTranslatedMessage(PlayerSelection target, std::string source, std::string message)
147+
{
148+
this->target = target;
149+
this->source = source;
150+
this->message = message;
151+
}
152+
153+
void RpcTranslatedMessage::Process()
154+
{
155+
if (!target.validate().has_value()) return;
156+
std::string finalmessage = "Translated from [" + source + "]: \n" + message;
157+
if (target.validate().get_PlayerControl() != (*Game::pLocalPlayer)) {
158+
if (finalmessage.length() > 100) {
159+
finalmessage = "[" + source + "]: \n" + message;
160+
if (finalmessage.length() > 100) {
161+
finalmessage = message;
162+
}
163+
}
164+
auto writer = InnerNetClient_StartRpcImmediately((InnerNetClient*)(*Game::pAmongUsClient), (*Game::pLocalPlayer)->fields._.NetId,
165+
uint8_t(RpcCalls__Enum::SendChat), SendOption__Enum::None, target.validate().get_PlayerControl()->fields._.OwnerId, NULL);
166+
MessageWriter_WriteString(writer, convert_to_string(finalmessage), NULL);
167+
InnerNetClient_FinishRpcImmediately((InnerNetClient*)(*Game::pAmongUsClient), writer, NULL);
168+
} else {
169+
ChatController_AddChat(Game::HudManager.GetInstance()->fields.Chat, (*Game::pLocalPlayer), convert_to_string(finalmessage), false, NULL);
170+
}
171+
}

rpc/_rpc.h

+17
Original file line numberDiff line numberDiff line change
@@ -477,4 +477,21 @@ class RpcGoneForTarget : public RPCInterface {
477477
public:
478478
RpcGoneForTarget(PlayerSelection target, uint8_t back);
479479
virtual void Process() override;
480+
};
481+
482+
class RpcTranslateMessage : public RPCInterface {
483+
std::string source;
484+
std::string message;
485+
public:
486+
RpcTranslateMessage(std::string source, std::string message);
487+
virtual void Process() override;
488+
};
489+
490+
class RpcTranslatedMessage : public RPCInterface {
491+
PlayerSelection target;
492+
std::string source;
493+
std::string message;
494+
public:
495+
RpcTranslatedMessage(PlayerSelection target, std::string source, std::string message);
496+
virtual void Process() override;
480497
};

0 commit comments

Comments
 (0)