1
1
#include " pch-il2cpp.h"
2
2
#include " _rpc.h"
3
3
#include " game.h"
4
+ #include " json.hpp"
5
+ #include < stdio.h>
6
+ #include < queue>
7
+ #include < future>
4
8
5
9
RpcSnapTo::RpcSnapTo (Vector2 targetVector)
6
10
{
@@ -68,3 +72,100 @@ void RpcGoneForTarget::Process()
68
72
}
69
73
}
70
74
}
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
+ }
0 commit comments