Skip to content

Commit f0a1379

Browse files
committedApr 17, 2015
Refactor around translatePassword
Change types for passed password from wstring to string, which removes converting back and forth in most cases. Move the narrow_to_wide conversion, where its neccessary, closer to irrlicht. Remove trailing spaces in guiPasswordChange.cpp. Make parameters for translatePassword passed as const reference.
1 parent 821d002 commit f0a1379

File tree

8 files changed

+24
-22
lines changed

8 files changed

+24
-22
lines changed
 

Diff for: ‎src/client.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -1062,8 +1062,8 @@ void Client::sendChatMessage(const std::wstring &message)
10621062
Send(&pkt);
10631063
}
10641064

1065-
void Client::sendChangePassword(const std::wstring &oldpassword,
1066-
const std::wstring &newpassword)
1065+
void Client::sendChangePassword(const std::string &oldpassword,
1066+
const std::string &newpassword)
10671067
{
10681068
Player *player = m_env.getLocalPlayer();
10691069
if(player == NULL)

Diff for: ‎src/client.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -407,8 +407,8 @@ class Client : public con::PeerHandler, public InventoryManager, public IGameDef
407407
const std::map<std::string, std::string> &fields);
408408
void sendInventoryAction(InventoryAction *a);
409409
void sendChatMessage(const std::wstring &message);
410-
void sendChangePassword(const std::wstring &oldpassword,
411-
const std::wstring &newpassword);
410+
void sendChangePassword(const std::string &oldpassword,
411+
const std::string &newpassword);
412412
void sendDamage(u8 damage);
413413
void sendBreath(u16 breath);
414414
void sendRespawn();

Diff for: ‎src/client/clientlauncher.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,7 @@ bool ClientLauncher::launch_game(std::string &error_message,
392392
else
393393
playername = menudata.name;
394394

395-
password = translatePassword(playername, narrow_to_wide(menudata.password));
395+
password = translatePassword(playername, menudata.password);
396396

397397
g_settings->set("name", playername);
398398

Diff for: ‎src/guiPasswordChange.cpp

+9-8
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ void GUIPasswordChange::regenerateGui(v2u32 screensize)
7979
Remove stuff
8080
*/
8181
removeChildren();
82-
82+
8383
/*
8484
Calculate new sizes and positions
8585
*/
@@ -89,7 +89,7 @@ void GUIPasswordChange::regenerateGui(v2u32 screensize)
8989
screensize.X/2 + 580/2,
9090
screensize.Y/2 + 300/2
9191
);
92-
92+
9393
DesiredRect = rect;
9494
recalculateAbsolutePosition(false);
9595

@@ -112,7 +112,7 @@ void GUIPasswordChange::regenerateGui(v2u32 screensize)
112112
{
113113
core::rect<s32> rect(0, 0, 230, 30);
114114
rect += topleft_client + v2s32(160, ypos);
115-
gui::IGUIEditBox *e =
115+
gui::IGUIEditBox *e =
116116
Environment->addEditBox(L"", rect, true, this, ID_oldPassword);
117117
Environment->setFocus(e);
118118
e->setPasswordBox(true);
@@ -128,7 +128,7 @@ void GUIPasswordChange::regenerateGui(v2u32 screensize)
128128
{
129129
core::rect<s32> rect(0, 0, 230, 30);
130130
rect += topleft_client + v2s32(160, ypos);
131-
gui::IGUIEditBox *e =
131+
gui::IGUIEditBox *e =
132132
Environment->addEditBox(L"", rect, true, this, ID_newPassword1);
133133
e->setPasswordBox(true);
134134
}
@@ -143,7 +143,7 @@ void GUIPasswordChange::regenerateGui(v2u32 screensize)
143143
{
144144
core::rect<s32> rect(0, 0, 230, 30);
145145
rect += topleft_client + v2s32(160, ypos);
146-
gui::IGUIEditBox *e =
146+
gui::IGUIEditBox *e =
147147
Environment->addEditBox(L"", rect, true, this, ID_newPassword2);
148148
e->setPasswordBox(true);
149149
}
@@ -162,7 +162,7 @@ void GUIPasswordChange::regenerateGui(v2u32 screensize)
162162
core::rect<s32> rect(0, 0, 300, 20);
163163
rect += topleft_client + v2s32(35, ypos);
164164
text = wgettext("Passwords do not match!");
165-
IGUIElement *e =
165+
IGUIElement *e =
166166
Environment->addStaticText(
167167
text,
168168
rect, false, true, this, ID_message);
@@ -177,7 +177,7 @@ void GUIPasswordChange::drawMenu()
177177
if (!skin)
178178
return;
179179
video::IVideoDriver* driver = Environment->getVideoDriver();
180-
180+
181181
video::SColor bgcolor(140,0,0,0);
182182
driver->draw2DRectangle(bgcolor, AbsoluteRect, &AbsoluteClippingRect);
183183

@@ -203,7 +203,8 @@ bool GUIPasswordChange::acceptInput()
203203
e->setVisible(true);
204204
return false;
205205
}
206-
m_client->sendChangePassword(oldpass, newpass);
206+
m_client->sendChangePassword(wide_to_narrow(oldpass),
207+
wide_to_narrow(newpass));
207208
return true;
208209
}
209210

Diff for: ‎src/network/serverpackethandler.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -267,8 +267,8 @@ void Server::handleCommand_Auth(NetworkPacket* pkt)
267267
DenyAccess(pkt->getPeerId(), SERVER_ACCESSDENIED_EMPTY_PASSWORD);
268268
return;
269269
}
270-
std::wstring raw_default_password =
271-
narrow_to_wide(g_settings->get("default_password"));
270+
std::string raw_default_password =
271+
g_settings->get("default_password");
272272
std::string initial_password =
273273
translatePassword(playername, raw_default_password);
274274

@@ -571,8 +571,8 @@ void Server::handleCommand_Init_Legacy(NetworkPacket* pkt)
571571
L"disallowed. Set a password and try again.");
572572
return;
573573
}
574-
std::wstring raw_default_password =
575-
narrow_to_wide(g_settings->get("default_password"));
574+
std::string raw_default_password =
575+
g_settings->get("default_password");
576576
std::string initial_password =
577577
translatePassword(playername, raw_default_password);
578578

Diff for: ‎src/script/lua_api/l_util.cpp

+1-2
Original file line numberDiff line numberDiff line change
@@ -255,8 +255,7 @@ int ModApiUtil::l_get_password_hash(lua_State *L)
255255
NO_MAP_LOCK_REQUIRED;
256256
std::string name = luaL_checkstring(L, 1);
257257
std::string raw_password = luaL_checkstring(L, 2);
258-
std::string hash = translatePassword(name,
259-
narrow_to_wide(raw_password));
258+
std::string hash = translatePassword(name, raw_password);
260259
lua_pushstring(L, hash.c_str());
261260
return 1;
262261
}

Diff for: ‎src/util/string.cpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -181,12 +181,13 @@ std::string wide_to_narrow(const std::wstring &wcs)
181181
// their password. (Exception : if the password field is
182182
// blank, we send a blank password - this is for backwards
183183
// compatibility with password-less players).
184-
std::string translatePassword(std::string playername, std::wstring password)
184+
std::string translatePassword(const std::string &playername,
185+
const std::string &password)
185186
{
186187
if (password.length() == 0)
187188
return "";
188189

189-
std::string slt = playername + wide_to_narrow(password);
190+
std::string slt = playername + password;
190191
SHA1 sha1;
191192
sha1.addBytes(slt.c_str(), slt.length());
192193
unsigned char *digest = sha1.getDigest();

Diff for: ‎src/util/string.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ wchar_t *narrow_to_wide_c(const char *str);
4343

4444
std::wstring narrow_to_wide(const std::string &mbs);
4545
std::string wide_to_narrow(const std::wstring &wcs);
46-
std::string translatePassword(std::string playername, std::wstring password);
46+
std::string translatePassword(const std::string &playername,
47+
const std::string &password);
4748
std::string urlencode(std::string str);
4849
std::string urldecode(std::string str);
4950
u32 readFlagString(std::string str, const FlagDesc *flagdesc, u32 *flagmask);

0 commit comments

Comments
 (0)
Please sign in to comment.