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