Skip to content

Commit

Permalink
Merge pull request etternagame#457 from etternagame/multipackutf8
Browse files Browse the repository at this point in the history
Multipackutf8
  • Loading branch information
nico-abram authored Mar 8, 2019
2 parents 945acc0 + 8101004 commit 1442fdf
Showing 1 changed file with 94 additions and 8 deletions.
102 changes: 94 additions & 8 deletions src/NetworkSyncManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,92 @@ static LocalizedString CONNECTION_SUCCESSFUL("NetworkSyncManager",
"Connection to '%s' successful.");
static LocalizedString CONNECTION_FAILED("NetworkSyncManager",
"Connection failed.");

// Utility function (Since json needs to be valid utf8)
string correct_non_utf_8(string *str)
{
int i,f_size=str->size();
unsigned char c,c2,c3,c4;
string to;
to.reserve(f_size);

for(i=0 ; i<f_size ; i++){
c=(unsigned char)(*str)[i];
if(c<32){//control char
if(c==9 || c==10 || c==13){//allow only \t \n \r
to.append(1,c);
}
continue;
}else if(c<127){//normal ASCII
to.append(1,c);
continue;
}else if(c<160){//control char (nothing should be defined here either ASCI, ISO_8859-1 or UTF8, so skipping)
if(c2==128){//fix microsoft mess, add euro
to.append(1,226);
to.append(1,130);
to.append(1,172);
}
if(c2==133){//fix IBM mess, add NEL = \n\r
to.append(1,10);
to.append(1,13);
}
continue;
}else if(c<192){//invalid for UTF8, converting ASCII
to.append(1,(unsigned char)194);
to.append(1,c);
continue;
}else if(c<194){//invalid for UTF8, converting ASCII
to.append(1,(unsigned char)195);
to.append(1,c-64);
continue;
}else if(c<224 && i+1<f_size){//possibly 2byte UTF8
c2=(unsigned char)(*str)[i+1];
if(c2>127 && c2<192){//valid 2byte UTF8
if(c==194 && c2<160){//control char, skipping
;
}else{
to.append(1,c);
to.append(1,c2);
}
i++;
continue;
}
}else if(c<240 && i+2<f_size){//possibly 3byte UTF8
c2=(unsigned char)(*str)[i+1];
c3=(unsigned char)(*str)[i+2];
if(c2>127 && c2<192 && c3>127 && c3<192){//valid 3byte UTF8
to.append(1,c);
to.append(1,c2);
to.append(1,c3);
i+=2;
continue;
}
}else if(c<245 && i+3<f_size){//possibly 4byte UTF8
c2=(unsigned char)(*str)[i+1];
c3=(unsigned char)(*str)[i+2];
c4=(unsigned char)(*str)[i+3];
if(c2>127 && c2<192 && c3>127 && c3<192 && c4>127 && c4<192){//valid 4byte UTF8
to.append(1,c);
to.append(1,c2);
to.append(1,c3);
to.append(1,c4);
i+=3;
continue;
}
}
//invalid UTF8, converting ASCII (c>245 || string too short for multi-byte))
to.append(1,(unsigned char)195);
to.append(1,c-64);
}
return to;
}

string correct_non_utf_8(const RString &str)
{
string stdStr = str.c_str();
auto utf8ValidStr = correct_non_utf_8(&stdStr);
return utf8ValidStr;
}

static LocalizedString INITIALIZING_CLIENT_NETWORK(
"NetworkSyncManager",
"Initializing Client Network...");
Expand Down Expand Up @@ -719,8 +804,9 @@ ETTProtocol::Update(NetworkSyncManager* n, float fDeltaTime)
payload["client"] = GAMESTATE->GetEtternaVersion();
payload["packs"] = json::array();
auto& packs = SONGMAN->GetSongGroupNames();
for(auto& pack : packs)
payload["packs"].push_back(pack.c_str());
for(auto& pack : packs) {
payload["packs"].push_back(correct_non_utf_8(pack).c_str());
}
Send(hello);
}
break;
Expand Down Expand Up @@ -1419,11 +1505,11 @@ ETTProtocol::SelectUserSong(NetworkSyncManager* n, Song* song)
j["type"] = ettClientMessageMap[ettpc_selectchart];
}
auto& payload = j["payload"];
payload["title"] = song->m_sMainTitle;
payload["subtitle"] = song->m_sSubTitle;
payload["artist"] = song->m_sArtist;
payload["title"] = correct_non_utf_8(song->m_sMainTitle).c_str();
payload["subtitle"] = correct_non_utf_8(song->m_sSubTitle).c_str();
payload["artist"] = correct_non_utf_8(song->m_sArtist).c_str();
payload["filehash"] = song->GetFileHash().c_str();
payload["pack"] = song->m_sGroupName.c_str();
payload["pack"] = correct_non_utf_8(song->m_sGroupName).c_str();
payload["chartkey"] = curSteps->GetChartKey().c_str();
payload["difficulty"] = DifficultyToString(curSteps->GetDifficulty());
payload["meter"] = curSteps->GetMeter();
Expand Down Expand Up @@ -1966,4 +2052,4 @@ LUA_REGISTER_CLASS(ChartRequest)
* OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
* OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*/
*/

0 comments on commit 1442fdf

Please sign in to comment.