1
1
#include " pch-il2cpp.h"
2
2
#include " _rpc.h"
3
3
#include " game.h"
4
+ #include " state.hpp"
5
+ #include " json.hpp"
6
+ #include < stdio.h>
7
+ #include < queue>
8
+ #include < future>
9
+ #include < codecvt>
10
+ #include < windows.h>
4
11
5
12
RpcSnapTo::RpcSnapTo (Vector2 targetVector)
6
13
{
@@ -68,3 +75,148 @@ void RpcGoneForTarget::Process()
68
75
}
69
76
}
70
77
}
78
+
79
+ RpcTranslateMessage::RpcTranslateMessage (PlayerSelection sourcePlayer, std::string source, std::string message)
80
+ {
81
+ this ->sourcePlayer = sourcePlayer;
82
+ this ->source = source;
83
+ this ->message = message;
84
+ }
85
+
86
+ std::string UTF8ToGBK (const std::string& utf8Data)
87
+ {
88
+ std::wstring_convert<std::codecvt_utf8<wchar_t >> conv;
89
+ std::wstring wString = conv.from_bytes (utf8Data); // utf-8 => wstring
90
+
91
+ std::wstring_convert<std::codecvt< wchar_t , char , std::mbstate_t >>
92
+ convert (new std::codecvt< wchar_t , char , std::mbstate_t >(" CHS" ));
93
+ std::string str = convert.to_bytes (wString); // wstring => string
94
+
95
+ return str;
96
+ }
97
+
98
+ int translate_from_deeplx (PlayerSelection target, std::string source, std::string message, std::string sourcelang, std::string targetlang)
99
+ {
100
+ try {
101
+ nlohmann::json j = {
102
+ {" text" , message},
103
+ {" source_lang" , sourcelang},
104
+ {" target_lang" , targetlang}
105
+ };
106
+ std::string prefix = " curl -X POST http://localhost:1188/translate -H \" Content-Type: application/json\" -d " ;
107
+ std::ostringstream o;
108
+ o << " \" " ;
109
+ std::string s = j.dump ();
110
+ for (auto c = s.cbegin (); c != s.cend (); c++) {
111
+ switch (*c) {
112
+ case ' \\ ' : o << " \\\\ " ; break ;
113
+ case ' "' : o << " \\\" " ; break ;
114
+ default : o << *c;
115
+ }
116
+ }
117
+ o << " \" " ;
118
+ prefix += o.str ();
119
+ FILE *fp = NULL ;
120
+ char buf[1024 ];
121
+ char result[33000 ] = {0 };
122
+ auto console = GetConsoleWindow ();
123
+ if (console == NULL ) {
124
+ AllocConsole ();
125
+ console = GetConsoleWindow ();
126
+ }
127
+ ShowWindow (console, SW_HIDE);
128
+ if ((fp = _popen (UTF8ToGBK (prefix).c_str (), " r" )) != NULL ) {
129
+ while (fgets (buf, 1024 , fp) != NULL ) {
130
+ strcat_s (result, 33000 , buf);
131
+ }
132
+ _pclose (fp);
133
+ fp = NULL ;
134
+ }
135
+ LOG_ERROR (" Translate 0" );
136
+ if (!target.validate ().has_value ()) return 0 ;
137
+ auto j2 = nlohmann::json::parse (result);
138
+ if (j2.find (" code" ) == j2.end ()) return 0 ;
139
+ auto code = j2.at (" code" );
140
+ LOG_ERROR (" Translate 1" );
141
+ if (code.is_number_integer () && 200 == code.get <int >()) {
142
+ if (j2.find (" data" ) == j2.end ()) return 0 ;
143
+ auto data = j2.at (" data" );
144
+ if (data.is_string ()) {
145
+ LOG_ERROR (" Translate 2" );
146
+ std::string translated = data.get <std::string>();
147
+ if (translated == message) {
148
+ LOG_ERROR (" Translate duplicated" );
149
+ return 0 ;
150
+ }
151
+ std::queue<RPCInterface*>* queue = nullptr ;
152
+ if (IsInGame ())
153
+ queue = &State.rpcQueue ;
154
+ else if (IsInLobby ())
155
+ queue = &State.lobbyRpcQueue ;
156
+ else return 0 ;
157
+ queue->push (new RpcTranslatedMessage (target, source, translated));
158
+ LOG_ERROR (" Translated ok" );
159
+ }
160
+ }
161
+ } catch (...) {
162
+ LOG_ERROR (" Translate error" );
163
+ }
164
+ return 0 ;
165
+ }
166
+
167
+ static std::queue<std::shared_ptr<std::future<int >>> asyncQueue;
168
+
169
+ void RpcTranslateMessage::Process ()
170
+ {
171
+ while (!asyncQueue.empty ()) {
172
+ std::shared_ptr<std::future<int >> task = asyncQueue.front ();
173
+ if (!task->valid ()) {
174
+ asyncQueue.pop ();
175
+ task.reset ();
176
+ } else {
177
+ break ;
178
+ }
179
+ }
180
+ for (auto p : GetAllPlayerControl ()) {
181
+ if (sourcePlayer.validate ().has_value () && p == sourcePlayer.validate ().get_PlayerControl ()) continue ;
182
+ auto friendCode = convert_from_string (GetPlayerData (p)->fields .FriendCode );
183
+ auto search = State.translatePlayers .find (friendCode);
184
+ if (search == State.translatePlayers .end ()) continue ;
185
+ std::string fromlang = search->second .first ;
186
+ std::string tolang = search->second .second ;
187
+ asyncQueue.push (std::make_shared<std::future<int >>(std::async (std::launch::async, translate_from_deeplx, PlayerSelection (p), source, message, fromlang, tolang)));
188
+ }
189
+ }
190
+
191
+ RpcTranslatedMessage::RpcTranslatedMessage (PlayerSelection target, std::string source, std::string message)
192
+ {
193
+ this ->target = target;
194
+ this ->source = source;
195
+ this ->message = message;
196
+ }
197
+
198
+ void RpcTranslatedMessage::Process ()
199
+ {
200
+ if (!target.validate ().has_value ()) return ;
201
+ std::string finalmessage = " Translated from =" + source + " =: \n " + message;
202
+ if (target.validate ().get_PlayerControl () != (*Game::pLocalPlayer)) {
203
+ if (State.ChatCooldown < 3 .f ) return ;
204
+ State.ChatCooldown = 0 .f ;
205
+ ChatController_AddChat (Game::HudManager.GetInstance ()->fields .Chat , (*Game::pLocalPlayer), convert_to_string (finalmessage), false , NULL );
206
+ if (finalmessage.length () > 100 ) {
207
+ finalmessage = " =" + source + " =: \n " + message;
208
+ if (finalmessage.length () > 100 ) {
209
+ finalmessage = message;
210
+ }
211
+ }
212
+ auto writer = InnerNetClient_StartRpcImmediately ((InnerNetClient*)(*Game::pAmongUsClient), (*Game::pLocalPlayer)->fields ._ .NetId ,
213
+ uint8_t (RpcCalls__Enum::SendChat), SendOption__Enum::None, target.validate ().get_PlayerControl ()->fields ._ .OwnerId , NULL );
214
+ MessageWriter_WriteString (writer, convert_to_string (finalmessage), NULL );
215
+ InnerNetClient_FinishRpcImmediately ((InnerNetClient*)(*Game::pAmongUsClient), writer, NULL );
216
+ LOG_ERROR (" Other" );
217
+ } else {
218
+ ChatController_AddChat (Game::HudManager.GetInstance ()->fields .Chat , (*Game::pLocalPlayer), convert_to_string (finalmessage), false , NULL );
219
+ LOG_ERROR (" Self" );
220
+ }
221
+ LOG_ERROR (" Message sent" );
222
+ }
0 commit comments