summaryrefslogtreecommitdiff
path: root/src/client
diff options
context:
space:
mode:
authorSimon Robertshaw <simon@hardwired.org.uk>2012-08-01 21:29:22 (GMT)
committer Simon Robertshaw <simon@hardwired.org.uk>2012-08-01 21:29:22 (GMT)
commitcbefea85d717bd599fa0559f091b051e904d9e2f (patch)
treee11849927aa88b270470c55d8b0fc052d34851e4 /src/client
parent29ac6380ba649e30dc29771b2833a86f20c9dbfe (diff)
downloadpowder-cbefea85d717bd599fa0559f091b051e904d9e2f.zip
powder-cbefea85d717bd599fa0559f091b051e904d9e2f.tar.gz
Local Saving, Server Saving rewrite
Diffstat (limited to 'src/client')
-rw-r--r--src/client/Client.cpp71
-rw-r--r--src/client/Client.h6
-rw-r--r--src/client/GameSave.cpp9
-rw-r--r--src/client/GameSave.h1
-rw-r--r--src/client/SaveInfo.cpp15
-rw-r--r--src/client/SaveInfo.h6
6 files changed, 101 insertions, 7 deletions
diff --git a/src/client/Client.cpp b/src/client/Client.cpp
index 7f26a4c..29f6a94 100644
--- a/src/client/Client.cpp
+++ b/src/client/Client.cpp
@@ -198,6 +198,64 @@ std::vector<std::string> Client::DirectorySearch(std::string directory, std::str
return searchResults;
}
+void Client::WriteFile(std::vector<unsigned char> fileData, std::string filename)
+{
+ try
+ {
+ std::ofstream fileStream;
+ fileStream.open(string(filename).c_str(), ios::binary);
+ if(fileStream.is_open())
+ {
+ fileStream.write((char*)&fileData[0], fileData.size());
+ fileStream.close();
+ }
+ }
+ catch (std::exception & e)
+ {
+ std::cerr << "WriteFile:" << e.what() << std::endl;
+ throw;
+ }
+}
+
+bool Client::FileExists(std::string filename)
+{
+ bool exists = false;
+ try
+ {
+ std::ofstream fileStream;
+ fileStream.open(string(filename).c_str(), ios::binary);
+ if(fileStream.is_open())
+ {
+ exists = true;
+ fileStream.close();
+ }
+ }
+ catch (std::exception & e)
+ {
+ exists = false;
+ }
+ return exists;
+}
+
+void Client::WriteFile(std::vector<char> fileData, std::string filename)
+{
+ try
+ {
+ std::ofstream fileStream;
+ fileStream.open(string(filename).c_str(), ios::binary);
+ if(fileStream.is_open())
+ {
+ fileStream.write(&fileData[0], fileData.size());
+ fileStream.close();
+ }
+ }
+ catch (std::exception & e)
+ {
+ std::cerr << "WriteFile:" << e.what() << std::endl;
+ throw;
+ }
+}
+
std::vector<unsigned char> Client::ReadFile(std::string filename)
{
try
@@ -400,7 +458,7 @@ User Client::GetAuthUser()
return authUser;
}
-RequestStatus Client::UploadSave(SaveInfo * save)
+RequestStatus Client::UploadSave(SaveInfo & save)
{
lastError = "";
int gameDataLength;
@@ -412,13 +470,14 @@ RequestStatus Client::UploadSave(SaveInfo * save)
userIDStream << authUser.ID;
if(authUser.ID)
{
- if(!save->GetGameSave())
+ if(!save.GetGameSave())
{
lastError = "Empty game save";
return RequestFailure;
}
+ save.SetID(0);
- gameData = save->GetGameSave()->Serialise(gameDataLength);
+ gameData = save.GetGameSave()->Serialise(gameDataLength);
if(!gameData)
{
@@ -427,8 +486,8 @@ RequestStatus Client::UploadSave(SaveInfo * save)
}
char * postNames[] = { "Name", "Description", "Data:save.bin", "Publish", NULL };
- char * postDatas[] = { (char *)(save->name.c_str()), (char *)(save->Description.c_str()), gameData, (char *)(save->Published?"Public":"Private") };
- int postLengths[] = { save->name.length(), save->Description.length(), gameDataLength, save->Published?6:7 };
+ char * postDatas[] = { (char *)(save.GetName().c_str()), (char *)(save.GetDescription().c_str()), gameData, (char *)(save.GetPublished()?"Public":"Private") };
+ int postLengths[] = { save.GetName().length(), save.GetDescription().length(), gameDataLength, save.GetPublished()?6:7 };
//std::cout << postNames[0] << " " << postDatas[0] << " " << postLengths[0] << std::endl;
data = http_multipart_post("http://" SERVER "/Save.api", postNames, postDatas, postLengths, (char *)(userIDStream.str().c_str()), NULL, (char *)(authUser.SessionID.c_str()), &dataStatus, &dataLength);
}
@@ -458,7 +517,7 @@ RequestStatus Client::UploadSave(SaveInfo * save)
}
else
{
- save->id = tempID;
+ save.SetID(tempID);
}
}
free(data);
diff --git a/src/client/Client.h b/src/client/Client.h
index ccf62f3..453342a 100644
--- a/src/client/Client.h
+++ b/src/client/Client.h
@@ -88,11 +88,15 @@ public:
std::vector<unsigned char> ReadFile(std::string filename);
+ void WriteFile(std::vector<unsigned char> fileData, std::string filename);
+ void WriteFile(std::vector<char> fileData, std::string filename);
+ bool FileExists(std::string filename);
+
void AddListener(ClientListener * listener);
void RemoveListener(ClientListener * listener);
RequestStatus ExecVote(int saveID, int direction);
- RequestStatus UploadSave(SaveInfo * save);
+ RequestStatus UploadSave(SaveInfo & save);
SaveFile * GetStamp(string stampID);
void DeleteStamp(string stampID);
diff --git a/src/client/GameSave.cpp b/src/client/GameSave.cpp
index 96306ac..f615c6f 100644
--- a/src/client/GameSave.cpp
+++ b/src/client/GameSave.cpp
@@ -278,6 +278,15 @@ void GameSave::setSize(int newWidth, int newHeight)
fanVelY[y] = &fanVelYPtr[y*blockWidth];
}
+std::vector<char> GameSave::Serialise()
+{
+ int dataSize;
+ char * data = Serialise(dataSize);
+ std::vector<char> dataVect(data, data+dataSize);
+ delete data;
+ return dataVect;
+}
+
char * GameSave::Serialise(int & dataSize)
{
return serialiseOPS(dataSize);
diff --git a/src/client/GameSave.h b/src/client/GameSave.h
index 5bd9e7a..2c86ea1 100644
--- a/src/client/GameSave.h
+++ b/src/client/GameSave.h
@@ -64,6 +64,7 @@ public:
~GameSave();
void setSize(int width, int height);
char * Serialise(int & dataSize);
+ std::vector<char> Serialise();
void Transform(matrix2d transform, vector2d translate);
void Expand();
diff --git a/src/client/SaveInfo.cpp b/src/client/SaveInfo.cpp
index da1033f..1df74c7 100644
--- a/src/client/SaveInfo.cpp
+++ b/src/client/SaveInfo.cpp
@@ -45,6 +45,21 @@ string SaveInfo::GetName() {
return name;
}
+void SaveInfo::SetDescription(string description) {
+ Description = description;
+}
+string SaveInfo::GetDescription() {
+ return Description;
+}
+
+void SaveInfo::SetPublished(bool published) {
+ Published = published;
+}
+
+bool SaveInfo::GetPublished() {
+ return Published;
+}
+
void SaveInfo::SetVote(int vote)
{
this->vote = vote;
diff --git a/src/client/SaveInfo.h b/src/client/SaveInfo.h
index e1b013e..a54741a 100644
--- a/src/client/SaveInfo.h
+++ b/src/client/SaveInfo.h
@@ -44,6 +44,12 @@ public:
void SetName(string name);
string GetName();
+ void SetDescription(string description);
+ string GetDescription();
+
+ void SetPublished(bool published);
+ bool GetPublished();
+
void SetUserName(string userName);
string GetUserName();