mirror of
https://github.com/The-Powder-Toy/The-Powder-Toy.git
synced 2025-08-07 08:56:30 +02:00
Login working, segfaults sometimes
This commit is contained in:
19
src/Controller.h
Normal file
19
src/Controller.h
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
/*
|
||||||
|
* Controller.h
|
||||||
|
*
|
||||||
|
* Created on: Jan 25, 2012
|
||||||
|
* Author: Simon
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef CONTROLLER_H_
|
||||||
|
#define CONTROLLER_H_
|
||||||
|
|
||||||
|
class ControllerCallback
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
ControllerCallback() {}
|
||||||
|
virtual void ControllerExit() {}
|
||||||
|
virtual ~ControllerCallback() {}
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif /* CONTROLLER_H_ */
|
@@ -40,35 +40,31 @@ Client::~Client()
|
|||||||
http_done();
|
http_done();
|
||||||
}
|
}
|
||||||
|
|
||||||
LoginStatus Client::Login(string username, string password)
|
LoginStatus Client::Login(string username, string password, User & user)
|
||||||
{
|
{
|
||||||
lastError = "";
|
lastError = "";
|
||||||
std::stringstream urlStream;
|
std::stringstream urlStream;
|
||||||
std::stringstream hashStream;
|
std::stringstream hashStream;
|
||||||
unsigned char cHashData[16];
|
char passwordHash[33];
|
||||||
|
char totalHash[33];
|
||||||
|
|
||||||
//Build hash of username + password
|
user.ID = 0;
|
||||||
struct md5_context md5;
|
user.Username = "";
|
||||||
md5_init(&md5);
|
user.SessionID = "";
|
||||||
md5_update(&md5, (unsigned char *)username.c_str(), username.length());
|
user.SessionKey = "";
|
||||||
md5_update(&md5, (unsigned char *)"-", 1);
|
|
||||||
md5_update(&md5, (unsigned char *)password.c_str(), password.length());
|
//Doop
|
||||||
md5_final(cHashData, &md5);
|
md5_ascii(passwordHash, (const unsigned char *)password.c_str(), password.length());
|
||||||
//Make sure hash is Hex
|
passwordHash[32] = 0;
|
||||||
//char * cHashHex = (char *)malloc(33);
|
hashStream << username << "-" << passwordHash;
|
||||||
for(int i = 0; i < 16; i++)
|
md5_ascii(totalHash, (const unsigned char *)(hashStream.str().c_str()), hashStream.str().length());
|
||||||
{
|
totalHash[32] = 0;
|
||||||
hashStream << hexChars[cHashData[i]>>4] << hexChars[cHashData[i]&15];
|
|
||||||
//cHashHex[i*2] = hex[cHashData[i]>>4];
|
|
||||||
//cHashHex[i*2+1] = hex[cHashData[i]&15];
|
|
||||||
}
|
|
||||||
//cHashHex[32] = 0;
|
|
||||||
|
|
||||||
char * data;
|
char * data;
|
||||||
int dataStatus, dataLength;
|
int dataStatus, dataLength;
|
||||||
char * postNames[] = { "Username", "Hash", NULL };
|
char * postNames[] = { "Username", "Hash", NULL };
|
||||||
char * postDatas[] = { (char*)username.c_str(), (char*)hashStream.str().c_str() };
|
char * postDatas[] = { (char*)username.c_str(), totalHash };
|
||||||
int postLengths[] = { username.length(), hashStream.str().length() };
|
int postLengths[] = { username.length(), 32 };
|
||||||
data = http_multipart_post("http://" SERVER "/Login.json", postNames, postDatas, postLengths, NULL, NULL, NULL, &dataStatus, &dataLength);
|
data = http_multipart_post("http://" SERVER "/Login.json", postNames, postDatas, postLengths, NULL, NULL, NULL, &dataStatus, &dataLength);
|
||||||
//data = http_auth_get("http://" SERVER "/Login.json", (char*)username.c_str(), (char*)password.c_str(), NULL, &dataStatus, &dataLength);
|
//data = http_auth_get("http://" SERVER "/Login.json", (char*)username.c_str(), (char*)password.c_str(), NULL, &dataStatus, &dataLength);
|
||||||
std::cout << data << std::endl;
|
std::cout << data << std::endl;
|
||||||
@@ -84,6 +80,13 @@ LoginStatus Client::Login(string username, string password)
|
|||||||
free(data);
|
free(data);
|
||||||
if(tempStatus.Value() == 1)
|
if(tempStatus.Value() == 1)
|
||||||
{
|
{
|
||||||
|
json::Number userIDTemp = objDocument["UserID"];
|
||||||
|
json::String sessionIDTemp = objDocument["SessionID"];
|
||||||
|
json::String sessionKeyTemp = objDocument["SessionKey"];
|
||||||
|
user.Username = username;
|
||||||
|
user.ID = userIDTemp.Value();
|
||||||
|
user.SessionID = sessionIDTemp.Value();
|
||||||
|
user.SessionKey = sessionKeyTemp.Value();
|
||||||
return LoginOkay;
|
return LoginOkay;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
@@ -99,6 +102,10 @@ LoginStatus Client::Login(string username, string password)
|
|||||||
return LoginError;
|
return LoginError;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
lastError = http_ret_text(dataStatus);
|
||||||
|
}
|
||||||
if(data)
|
if(data)
|
||||||
{
|
{
|
||||||
free(data);
|
free(data);
|
||||||
|
@@ -9,6 +9,7 @@
|
|||||||
#include "search/Thumbnail.h"
|
#include "search/Thumbnail.h"
|
||||||
#include "search/Save.h"
|
#include "search/Save.h"
|
||||||
#include "Singleton.h"
|
#include "Singleton.h"
|
||||||
|
#include "User.h"
|
||||||
|
|
||||||
enum LoginStatus
|
enum LoginStatus
|
||||||
{
|
{
|
||||||
@@ -28,7 +29,7 @@ private:
|
|||||||
public:
|
public:
|
||||||
Client();
|
Client();
|
||||||
~Client();
|
~Client();
|
||||||
LoginStatus Login(string username, string password);
|
LoginStatus Login(string username, string password, User & user);
|
||||||
void ClearThumbnailRequests();
|
void ClearThumbnailRequests();
|
||||||
std::vector<Save*> * SearchSaves(int start, int count, string query, string sort, int & resultCount);
|
std::vector<Save*> * SearchSaves(int start, int count, string query, string sort, int & resultCount);
|
||||||
Thumbnail * GetPreview(int saveID, int saveDate);
|
Thumbnail * GetPreview(int saveID, int saveDate);
|
||||||
|
@@ -208,7 +208,7 @@ void md5_transform(unsigned buf[4], const unsigned char inraw[64])
|
|||||||
buf[3] += d;
|
buf[3] += d;
|
||||||
}
|
}
|
||||||
|
|
||||||
static char hex[] = "0123456789abcdef";
|
static char hexChars[] = "0123456789abcdef";
|
||||||
void md5_ascii(char *result, unsigned char const *buf, unsigned len)
|
void md5_ascii(char *result, unsigned char const *buf, unsigned len)
|
||||||
{
|
{
|
||||||
struct md5_context md5;
|
struct md5_context md5;
|
||||||
@@ -224,8 +224,8 @@ void md5_ascii(char *result, unsigned char const *buf, unsigned len)
|
|||||||
|
|
||||||
for (i=0; i<16; i++)
|
for (i=0; i<16; i++)
|
||||||
{
|
{
|
||||||
result[i*2] = hex[(hash[i]>>4)&0xF];
|
result[i*2] = hexChars[(hash[i]>>4)&0xF];
|
||||||
result[i*2+1] = hex[hash[i]&0x0F];
|
result[i*2+1] = hexChars[hash[i]&0x0F];
|
||||||
}
|
}
|
||||||
result[32] = 0;
|
result[32] = 0;
|
||||||
}
|
}
|
||||||
|
29
src/client/User.h
Normal file
29
src/client/User.h
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
/*
|
||||||
|
* User.h
|
||||||
|
*
|
||||||
|
* Created on: Jan 25, 2012
|
||||||
|
* Author: Simon
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef USER_H_
|
||||||
|
#define USER_H_
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
class User
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
int ID;
|
||||||
|
std::string Username;
|
||||||
|
std::string SessionID;
|
||||||
|
std::string SessionKey;
|
||||||
|
User(int id, std::string username):
|
||||||
|
ID(id),
|
||||||
|
Username(username)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
#endif /* USER_H_ */
|
@@ -11,9 +11,23 @@
|
|||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
|
class GameController::LoginCallback: public ControllerCallback
|
||||||
|
{
|
||||||
|
GameController * cc;
|
||||||
|
public:
|
||||||
|
LoginCallback(GameController * cc_) { cc = cc_; }
|
||||||
|
virtual void ControllerExit()
|
||||||
|
{
|
||||||
|
cc->gameModel->SetUser(cc->loginWindow->GetUser());
|
||||||
|
delete cc->loginWindow;
|
||||||
|
cc->loginWindow = NULL;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
GameController::GameController():
|
GameController::GameController():
|
||||||
search(NULL),
|
search(NULL),
|
||||||
renderOptions(NULL)
|
renderOptions(NULL),
|
||||||
|
loginWindow(NULL)
|
||||||
{
|
{
|
||||||
gameView = new GameView();
|
gameView = new GameView();
|
||||||
gameModel = new GameModel();
|
gameModel = new GameModel();
|
||||||
@@ -135,7 +149,7 @@ void GameController::OpenSearch()
|
|||||||
|
|
||||||
void GameController::OpenLogin()
|
void GameController::OpenLogin()
|
||||||
{
|
{
|
||||||
loginWindow = new LoginController();
|
loginWindow = new LoginController(new LoginCallback(this));
|
||||||
ui::Engine::Ref().ShowWindow(loginWindow->GetView());
|
ui::Engine::Ref().ShowWindow(loginWindow->GetView());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -25,6 +25,7 @@ private:
|
|||||||
RenderController * renderOptions;
|
RenderController * renderOptions;
|
||||||
LoginController * loginWindow;
|
LoginController * loginWindow;
|
||||||
public:
|
public:
|
||||||
|
class LoginCallback;
|
||||||
GameController();
|
GameController();
|
||||||
~GameController();
|
~GameController();
|
||||||
GameView * GetView();
|
GameView * GetView();
|
||||||
|
@@ -11,7 +11,8 @@ GameModel::GameModel():
|
|||||||
sim(NULL),
|
sim(NULL),
|
||||||
ren(NULL),
|
ren(NULL),
|
||||||
currentSave(NULL),
|
currentSave(NULL),
|
||||||
currentBrush(new Brush(ui::Point(4, 4)))
|
currentBrush(new Brush(ui::Point(4, 4))),
|
||||||
|
currentUser(0, "")
|
||||||
{
|
{
|
||||||
sim = new Simulation();
|
sim = new Simulation();
|
||||||
ren = new Renderer(ui::Engine::Ref().g, sim);
|
ren = new Renderer(ui::Engine::Ref().g, sim);
|
||||||
@@ -132,6 +133,17 @@ Renderer * GameModel::GetRenderer()
|
|||||||
return ren;
|
return ren;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
User GameModel::GetUser()
|
||||||
|
{
|
||||||
|
return currentUser;
|
||||||
|
}
|
||||||
|
|
||||||
|
void GameModel::SetUser(User user)
|
||||||
|
{
|
||||||
|
currentUser = user;
|
||||||
|
notifyUserChanged();
|
||||||
|
}
|
||||||
|
|
||||||
void GameModel::SetPaused(bool pauseState)
|
void GameModel::SetPaused(bool pauseState)
|
||||||
{
|
{
|
||||||
sim->sys_pause = pauseState?1:0;
|
sim->sys_pause = pauseState?1:0;
|
||||||
@@ -211,3 +223,11 @@ void GameModel::notifyActiveToolChanged()
|
|||||||
observers[i]->NotifyActiveToolChanged(this);
|
observers[i]->NotifyActiveToolChanged(this);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void GameModel::notifyUserChanged()
|
||||||
|
{
|
||||||
|
for(int i = 0; i < observers.size(); i++)
|
||||||
|
{
|
||||||
|
observers[i]->NotifyUserChanged(this);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@@ -7,6 +7,7 @@
|
|||||||
#include "Renderer.h"
|
#include "Renderer.h"
|
||||||
#include "GameView.h"
|
#include "GameView.h"
|
||||||
#include "Brush.h"
|
#include "Brush.h"
|
||||||
|
#include "client/User.h"
|
||||||
|
|
||||||
#include "Tool.h"
|
#include "Tool.h"
|
||||||
#include "Menu.h"
|
#include "Menu.h"
|
||||||
@@ -29,6 +30,7 @@ private:
|
|||||||
Simulation * sim;
|
Simulation * sim;
|
||||||
Renderer * ren;
|
Renderer * ren;
|
||||||
Tool * activeTool;
|
Tool * activeTool;
|
||||||
|
User currentUser;
|
||||||
void notifyRendererChanged();
|
void notifyRendererChanged();
|
||||||
void notifySimulationChanged();
|
void notifySimulationChanged();
|
||||||
void notifyPausedChanged();
|
void notifyPausedChanged();
|
||||||
@@ -37,6 +39,7 @@ private:
|
|||||||
void notifyMenuListChanged();
|
void notifyMenuListChanged();
|
||||||
void notifyToolListChanged();
|
void notifyToolListChanged();
|
||||||
void notifyActiveToolChanged();
|
void notifyActiveToolChanged();
|
||||||
|
void notifyUserChanged();
|
||||||
public:
|
public:
|
||||||
GameModel();
|
GameModel();
|
||||||
~GameModel();
|
~GameModel();
|
||||||
@@ -53,7 +56,8 @@ public:
|
|||||||
vector<Tool*> GetToolList();
|
vector<Tool*> GetToolList();
|
||||||
void SetActiveMenu(Menu * menu);
|
void SetActiveMenu(Menu * menu);
|
||||||
Menu * GetActiveMenu();
|
Menu * GetActiveMenu();
|
||||||
|
User GetUser();
|
||||||
|
void SetUser(User user);
|
||||||
Simulation * GetSimulation();
|
Simulation * GetSimulation();
|
||||||
Renderer * GetRenderer();
|
Renderer * GetRenderer();
|
||||||
};
|
};
|
||||||
|
@@ -305,6 +305,18 @@ void GameView::NotifySimulationChanged(GameModel * sender)
|
|||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
|
void GameView::NotifyUserChanged(GameModel * sender)
|
||||||
|
{
|
||||||
|
if(!sender->GetUser().ID)
|
||||||
|
{
|
||||||
|
loginButton->SetText("Login");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
loginButton->SetText(sender->GetUser().Username);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
void GameView::NotifyPausedChanged(GameModel * sender)
|
void GameView::NotifyPausedChanged(GameModel * sender)
|
||||||
{
|
{
|
||||||
|
@@ -48,6 +48,7 @@ public:
|
|||||||
void NotifyMenuListChanged(GameModel * sender);
|
void NotifyMenuListChanged(GameModel * sender);
|
||||||
void NotifyToolListChanged(GameModel * sender);
|
void NotifyToolListChanged(GameModel * sender);
|
||||||
void NotifyActiveToolChanged(GameModel * sender);
|
void NotifyActiveToolChanged(GameModel * sender);
|
||||||
|
void NotifyUserChanged(GameModel * sender);
|
||||||
virtual void OnMouseMove(int x, int y, int dx, int dy);
|
virtual void OnMouseMove(int x, int y, int dx, int dy);
|
||||||
virtual void OnMouseDown(int x, int y, unsigned button);
|
virtual void OnMouseDown(int x, int y, unsigned button);
|
||||||
virtual void OnMouseUp(int x, int y, unsigned button);
|
virtual void OnMouseUp(int x, int y, unsigned button);
|
||||||
|
@@ -18,7 +18,7 @@ Engine::Engine():
|
|||||||
mousey_(0),
|
mousey_(0),
|
||||||
mousexp_(0),
|
mousexp_(0),
|
||||||
mouseyp_(0),
|
mouseyp_(0),
|
||||||
FpsLimit(0.0f),
|
FpsLimit(60.0f),
|
||||||
windows(stack<Window*>()),
|
windows(stack<Window*>()),
|
||||||
lastBuffer(NULL),
|
lastBuffer(NULL),
|
||||||
prevBuffers(stack<pixel*>()),
|
prevBuffers(stack<pixel*>()),
|
||||||
|
@@ -6,8 +6,9 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include "LoginController.h"
|
#include "LoginController.h"
|
||||||
|
#include "client/User.h"
|
||||||
|
|
||||||
LoginController::LoginController() {
|
LoginController::LoginController(ControllerCallback * callback) {
|
||||||
// TODO Auto-generated constructor stub
|
// TODO Auto-generated constructor stub
|
||||||
loginView = new LoginView();
|
loginView = new LoginView();
|
||||||
loginModel = new LoginModel();
|
loginModel = new LoginModel();
|
||||||
@@ -15,6 +16,8 @@ LoginController::LoginController() {
|
|||||||
loginView->AttachController(this);
|
loginView->AttachController(this);
|
||||||
loginModel->AddObserver(loginView);
|
loginModel->AddObserver(loginView);
|
||||||
|
|
||||||
|
this->callback = callback;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void LoginController::Login(string username, string password)
|
void LoginController::Login(string username, string password)
|
||||||
@@ -22,6 +25,11 @@ void LoginController::Login(string username, string password)
|
|||||||
loginModel->Login(username, password);
|
loginModel->Login(username, password);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
User LoginController::GetUser()
|
||||||
|
{
|
||||||
|
return loginModel->GetUser();
|
||||||
|
}
|
||||||
|
|
||||||
void LoginController::Exit()
|
void LoginController::Exit()
|
||||||
{
|
{
|
||||||
if(ui::Engine::Ref().GetWindow() == loginView)
|
if(ui::Engine::Ref().GetWindow() == loginView)
|
||||||
@@ -29,6 +37,8 @@ void LoginController::Exit()
|
|||||||
ui::Engine::Ref().CloseWindow();
|
ui::Engine::Ref().CloseWindow();
|
||||||
loginView = NULL;
|
loginView = NULL;
|
||||||
}
|
}
|
||||||
|
if(callback)
|
||||||
|
callback->ControllerExit();
|
||||||
}
|
}
|
||||||
|
|
||||||
LoginController::~LoginController() {
|
LoginController::~LoginController() {
|
||||||
|
@@ -11,6 +11,8 @@
|
|||||||
#include <string>
|
#include <string>
|
||||||
#include "LoginView.h"
|
#include "LoginView.h"
|
||||||
#include "LoginModel.h"
|
#include "LoginModel.h"
|
||||||
|
#include "Controller.h"
|
||||||
|
#include "client/User.h"
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
@@ -19,12 +21,13 @@ class LoginModel;
|
|||||||
class LoginController {
|
class LoginController {
|
||||||
LoginView * loginView;
|
LoginView * loginView;
|
||||||
LoginModel * loginModel;
|
LoginModel * loginModel;
|
||||||
|
ControllerCallback * callback;
|
||||||
public:
|
public:
|
||||||
LoginController();
|
LoginController(ControllerCallback * callback = NULL);
|
||||||
void Login(string username, string password);
|
void Login(string username, string password);
|
||||||
void Exit();
|
void Exit();
|
||||||
LoginView * GetView() { return loginView; }
|
LoginView * GetView() { return loginView; }
|
||||||
|
User GetUser();
|
||||||
virtual ~LoginController();
|
virtual ~LoginController();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@@ -7,7 +7,9 @@
|
|||||||
|
|
||||||
#include "LoginModel.h"
|
#include "LoginModel.h"
|
||||||
|
|
||||||
LoginModel::LoginModel() {
|
LoginModel::LoginModel():
|
||||||
|
currentUser(0, "")
|
||||||
|
{
|
||||||
// TODO Auto-generated constructor stub
|
// TODO Auto-generated constructor stub
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -17,7 +19,7 @@ void LoginModel::Login(string username, string password)
|
|||||||
statusText = "Logging in...";
|
statusText = "Logging in...";
|
||||||
loginStatus = false;
|
loginStatus = false;
|
||||||
notifyStatusChanged();
|
notifyStatusChanged();
|
||||||
LoginStatus status = Client::Ref().Login(username, password);
|
LoginStatus status = Client::Ref().Login(username, password, currentUser);
|
||||||
switch(status)
|
switch(status)
|
||||||
{
|
{
|
||||||
case LoginOkay:
|
case LoginOkay:
|
||||||
@@ -41,6 +43,11 @@ string LoginModel::GetStatusText()
|
|||||||
return statusText;
|
return statusText;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
User LoginModel::GetUser()
|
||||||
|
{
|
||||||
|
return currentUser;
|
||||||
|
}
|
||||||
|
|
||||||
bool LoginModel::GetStatus()
|
bool LoginModel::GetStatus()
|
||||||
{
|
{
|
||||||
return loginStatus;
|
return loginStatus;
|
||||||
|
@@ -21,12 +21,14 @@ class LoginModel {
|
|||||||
string statusText;
|
string statusText;
|
||||||
bool loginStatus;
|
bool loginStatus;
|
||||||
void notifyStatusChanged();
|
void notifyStatusChanged();
|
||||||
|
User currentUser;
|
||||||
public:
|
public:
|
||||||
LoginModel();
|
LoginModel();
|
||||||
void Login(string username, string password);
|
void Login(string username, string password);
|
||||||
void AddObserver(LoginView * observer);
|
void AddObserver(LoginView * observer);
|
||||||
string GetStatusText();
|
string GetStatusText();
|
||||||
bool GetStatus();
|
bool GetStatus();
|
||||||
|
User GetUser();
|
||||||
virtual ~LoginModel();
|
virtual ~LoginModel();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user