Move Read/WriteFile from Client to Platform

This commit is contained in:
Tamás Bálint Misius 2022-10-05 19:10:28 +02:00
parent 084f196248
commit 304cc3a47b
No known key found for this signature in database
GPG Key ID: 5B472A12F6ECA9F2
16 changed files with 46 additions and 51 deletions

View File

@ -34,7 +34,6 @@
#include "client/SaveInfo.h" #include "client/SaveInfo.h"
#include "client/GameSave.h" #include "client/GameSave.h"
#include "client/SaveFile.h" #include "client/SaveFile.h"
#include "client/Client.h"
#include "gui/game/GameController.h" #include "gui/game/GameController.h"
#include "gui/game/GameView.h" #include "gui/game/GameView.h"

View File

@ -940,7 +940,7 @@ int main(int argc, char * argv[])
try try
{ {
std::vector<char> gameSaveData; std::vector<char> gameSaveData;
if (!Client::Ref().ReadFile(gameSaveData, arguments["open"])) if (!Platform::ReadFile(gameSaveData, arguments["open"]))
{ {
new ErrorMessage("Error", "Could not read file"); new ErrorMessage("Error", "Could not read file");
} }

View File

@ -1015,7 +1015,7 @@ SaveFile * Client::LoadSaveFile(ByteString filename)
try try
{ {
std::vector<char> data; std::vector<char> data;
if (ReadFile(data, filename)) if (Platform::ReadFile(data, filename))
{ {
file->SetGameSave(new GameSave(std::move(data))); file->SetGameSave(new GameSave(std::move(data)));
} }
@ -1555,33 +1555,6 @@ void Client::SetPrefUnicode(ByteString prop, String value)
SetPref(prop, value.ToUtf8()); SetPref(prop, value.ToUtf8());
} }
bool Client::ReadFile(std::vector<char> &fileData, ByteString filename)
{
std::ifstream f(filename, std::ios::binary);
if (f) f.seekg(0, std::ios::end);
if (f) fileData.resize(f.tellg());
if (f) f.seekg(0);
if (f) f.read(&fileData[0], fileData.size());
if (!f)
{
std::cerr << "ReadFile: " << filename << ": " << strerror(errno) << std::endl;
return false;
}
return true;
}
bool Client::WriteFile(std::vector<char> fileData, ByteString filename)
{
std::ofstream f(filename, std::ios::binary);
if (f) f.write(&fileData[0], fileData.size());
if (!f)
{
std::cerr << "WriteFile: " << filename << ": " << strerror(errno) << std::endl;
return false;
}
return true;
}
bool Client::DoInstallation() bool Client::DoInstallation()
{ {
bool ok = true; bool ok = true;
@ -1683,7 +1656,7 @@ bool Client::DoInstallation()
desktopData = desktopData.Substitute("Exec=" APPEXE, "Exec=" + desktopEscapeString(desktopEscapeExec(exe))); desktopData = desktopData.Substitute("Exec=" APPEXE, "Exec=" + desktopEscapeString(desktopEscapeExec(exe)));
desktopData += ByteString::Build("Path=", desktopEscapeString(path), "\n"); desktopData += ByteString::Build("Path=", desktopEscapeString(path), "\n");
ByteString file = APPVENDOR "-" APPID ".desktop"; ByteString file = APPVENDOR "-" APPID ".desktop";
ok = ok && WriteFile(std::vector<char>(desktopData.begin(), desktopData.end()), file); ok = ok && Platform::WriteFile(std::vector<char>(desktopData.begin(), desktopData.end()), file);
ok = ok && !system(ByteString::Build("xdg-desktop-menu install ", file).c_str()); ok = ok && !system(ByteString::Build("xdg-desktop-menu install ", file).c_str());
ok = ok && !system(ByteString::Build("xdg-mime default ", file, " application/vnd.powdertoy.save").c_str()); ok = ok && !system(ByteString::Build("xdg-mime default ", file, " application/vnd.powdertoy.save").c_str());
ok = ok && !system(ByteString::Build("xdg-mime default ", file, " x-scheme-handler/ptsave").c_str()); ok = ok && !system(ByteString::Build("xdg-mime default ", file, " x-scheme-handler/ptsave").c_str());
@ -1692,28 +1665,28 @@ bool Client::DoInstallation()
if (ok) if (ok)
{ {
ByteString file = APPVENDOR "-save.xml"; ByteString file = APPVENDOR "-save.xml";
ok = ok && WriteFile(std::vector<char>(save_xml, save_xml + save_xml_size), file); ok = ok && Platform::WriteFile(std::vector<char>(save_xml, save_xml + save_xml_size), file);
ok = ok && !system(ByteString::Build("xdg-mime install ", file).c_str()); ok = ok && !system(ByteString::Build("xdg-mime install ", file).c_str());
Platform::RemoveFile(file); Platform::RemoveFile(file);
} }
if (ok) if (ok)
{ {
ByteString file = APPVENDOR "-cps32.png"; ByteString file = APPVENDOR "-cps32.png";
ok = ok && WriteFile(std::vector<char>(cps32_png, cps32_png + cps32_png_size), file); ok = ok && Platform::WriteFile(std::vector<char>(cps32_png, cps32_png + cps32_png_size), file);
ok = ok && !system(ByteString::Build("xdg-icon-resource install --noupdate --context mimetypes --size 32 ", file, " application-vnd.powdertoy.save").c_str()); ok = ok && !system(ByteString::Build("xdg-icon-resource install --noupdate --context mimetypes --size 32 ", file, " application-vnd.powdertoy.save").c_str());
Platform::RemoveFile(file); Platform::RemoveFile(file);
} }
if (ok) if (ok)
{ {
ByteString file = APPVENDOR "-cps16.png"; ByteString file = APPVENDOR "-cps16.png";
ok = ok && WriteFile(std::vector<char>(cps16_png, cps16_png + cps16_png_size), file); ok = ok && Platform::WriteFile(std::vector<char>(cps16_png, cps16_png + cps16_png_size), file);
ok = ok && !system(ByteString::Build("xdg-icon-resource install --noupdate --context mimetypes --size 16 ", file, " application-vnd.powdertoy.save").c_str()); ok = ok && !system(ByteString::Build("xdg-icon-resource install --noupdate --context mimetypes --size 16 ", file, " application-vnd.powdertoy.save").c_str());
Platform::RemoveFile(file); Platform::RemoveFile(file);
} }
if (ok) if (ok)
{ {
ByteString file = APPVENDOR "-exe48.png"; ByteString file = APPVENDOR "-exe48.png";
ok = ok && WriteFile(std::vector<char>(exe48_png, exe48_png + exe48_png_size), file); ok = ok && Platform::WriteFile(std::vector<char>(exe48_png, exe48_png + exe48_png_size), file);
ok = ok && !system(ByteString::Build("xdg-icon-resource install --noupdate --size 48 ", file, " " APPVENDOR "-" APPEXE).c_str()); ok = ok && !system(ByteString::Build("xdg-icon-resource install --noupdate --size 48 ", file, " " APPVENDOR "-" APPEXE).c_str());
Platform::RemoveFile(file); Platform::RemoveFile(file);
} }

View File

@ -113,9 +113,6 @@ public:
void Initialise(ByteString proxy, ByteString cafile, ByteString capath, bool disableNetwork); void Initialise(ByteString proxy, ByteString cafile, ByteString capath, bool disableNetwork);
bool IsFirstRun(); bool IsFirstRun();
bool ReadFile(std::vector<char> &fileData, ByteString filename);
bool WriteFile(std::vector<char> fileData, ByteString filename);
void AddListener(ClientListener * listener); void AddListener(ClientListener * listener);
void RemoveListener(ClientListener * listener); void RemoveListener(ClientListener * listener);

View File

@ -5,6 +5,7 @@
#include <cstdio> #include <cstdio>
#include <cassert> #include <cassert>
#include <fstream> #include <fstream>
#include <iostream>
#include <sys/stat.h> #include <sys/stat.h>
#ifdef WIN #ifdef WIN
@ -546,4 +547,31 @@ std::wstring WinWiden(const ByteString &source)
} }
#endif #endif
bool ReadFile(std::vector<char> &fileData, ByteString filename)
{
std::ifstream f(filename, std::ios::binary);
if (f) f.seekg(0, std::ios::end);
if (f) fileData.resize(f.tellg());
if (f) f.seekg(0);
if (f) f.read(&fileData[0], fileData.size());
if (!f)
{
std::cerr << "ReadFile: " << filename << ": " << strerror(errno) << std::endl;
return false;
}
return true;
}
bool WriteFile(std::vector<char> fileData, ByteString filename)
{
std::ofstream f(filename, std::ios::binary);
if (f) f.write(&fileData[0], fileData.size());
if (!f)
{
std::cerr << "WriteFile: " << filename << ": " << strerror(errno) << std::endl;
return false;
}
return true;
}
} }

View File

@ -42,6 +42,9 @@ namespace Platform
std::vector<ByteString> DirectorySearch(ByteString directory, ByteString search, std::vector<ByteString> extensions); std::vector<ByteString> DirectorySearch(ByteString directory, ByteString search, std::vector<ByteString> extensions);
String DoMigration(ByteString fromDir, ByteString toDir); String DoMigration(ByteString fromDir, ByteString toDir);
bool ReadFile(std::vector<char> &fileData, ByteString filename);
bool WriteFile(std::vector<char> fileData, ByteString filename);
#ifdef WIN #ifdef WIN
ByteString WinNarrow(const std::wstring &source); ByteString WinNarrow(const std::wstring &source);
std::wstring WinWiden(const ByteString &source); std::wstring WinWiden(const ByteString &source);

View File

@ -2,7 +2,6 @@
#include <algorithm> #include <algorithm>
#include "client/Client.h"
#include "client/GameSave.h" #include "client/GameSave.h"
#include "client/SaveFile.h" #include "client/SaveFile.h"
#include "common/Platform.h" #include "common/Platform.h"
@ -48,7 +47,7 @@ class LoadFilesTask: public Task
try try
{ {
std::vector<char> data; std::vector<char> data;
if (!Client::Ref().ReadFile(data, directory + *iter)) if (!Platform::ReadFile(data, directory + *iter))
continue; continue;
GameSave * tempSave = new GameSave(std::move(data)); GameSave * tempSave = new GameSave(std::move(data));
saveFile->SetGameSave(tempSave); saveFile->SetGameSave(tempSave);

View File

@ -1210,7 +1210,7 @@ void GameController::OpenLocalSaveWindow(bool asCurrent)
std::vector<char> saveData = gameSave->Serialise(); std::vector<char> saveData = gameSave->Serialise();
if (saveData.size() == 0) if (saveData.size() == 0)
new ErrorMessage("Error", "Unable to serialize game data."); new ErrorMessage("Error", "Unable to serialize game data.");
else if (!Client::Ref().WriteFile(saveData, gameModel->GetSaveFile()->GetName())) else if (!Platform::WriteFile(saveData, gameModel->GetSaveFile()->GetName()))
new ErrorMessage("Error", "Unable to write save file."); new ErrorMessage("Error", "Unable to write save file.");
else else
gameModel->SetInfoTip("Saved Successfully"); gameModel->SetInfoTip("Saved Successfully");

View File

@ -482,7 +482,7 @@ void GameModel::BuildBrushList()
for (size_t i = 0; i < brushFiles.size(); i++) for (size_t i = 0; i < brushFiles.size(); i++)
{ {
std::vector<char> brushData; std::vector<char> brushData;
if (!Client::Ref().ReadFile(brushData, BRUSH_DIR + ByteString(PATH_SEP) + brushFiles[i])) if (!Platform::ReadFile(brushData, BRUSH_DIR + ByteString(PATH_SEP) + brushFiles[i]))
{ {
std::cout << "Brushes: Skipping " << brushFiles[i] << ". Could not open" << std::endl; std::cout << "Brushes: Skipping " << brushFiles[i] << ". Could not open" << std::endl;
continue; continue;

View File

@ -933,7 +933,7 @@ ByteString GameView::TakeScreenshot(int captureUI, int fileType)
std::string date = format::UnixtimeToDate(screenshotTime, "%Y-%m-%d %H.%M.%S"); std::string date = format::UnixtimeToDate(screenshotTime, "%Y-%m-%d %H.%M.%S");
ByteString filename = ByteString::Build("screenshot ", date, suffix, extension); ByteString filename = ByteString::Build("screenshot ", date, suffix, extension);
Client::Ref().WriteFile(data, filename); Platform::WriteFile(data, filename);
doScreenshot = false; doScreenshot = false;
lastScreenshotTime = screenshotTime; lastScreenshotTime = screenshotTime;
@ -2147,7 +2147,7 @@ void GameView::OnDraw()
ByteString filename = ByteString::Build("recordings", PATH_SEP, recordingFolder, PATH_SEP, "frame_", Format::Width(recordingIndex++, 6), ".ppm"); ByteString filename = ByteString::Build("recordings", PATH_SEP, recordingFolder, PATH_SEP, "frame_", Format::Width(recordingIndex++, 6), ".ppm");
Client::Ref().WriteFile(data, filename); Platform::WriteFile(data, filename);
} }
if (logEntries.size()) if (logEntries.size())

View File

@ -4,7 +4,6 @@
#include "Button.h" #include "Button.h"
#include "AvatarButton.h" #include "AvatarButton.h"
#include "Format.h" #include "Format.h"
#include "client/Client.h"
#include "graphics/Graphics.h" #include "graphics/Graphics.h"
#include "ContextMenu.h" #include "ContextMenu.h"
#include "Keys.h" #include "Keys.h"

View File

@ -5,8 +5,6 @@
#include "common/tpt-minmax.h" #include "common/tpt-minmax.h"
#include "client/Client.h"
using namespace ui; using namespace ui;
ScrollPanel::ScrollPanel(Point position, Point size): ScrollPanel::ScrollPanel(Point position, Point size):

View File

@ -8,7 +8,6 @@
#include "OptionsController.h" #include "OptionsController.h"
#include "OptionsModel.h" #include "OptionsModel.h"
#include "client/Client.h"
#include "common/Platform.h" #include "common/Platform.h"
#include "graphics/Graphics.h" #include "graphics/Graphics.h"
#include "gui/Style.h" #include "gui/Style.h"

View File

@ -115,7 +115,7 @@ void LocalSaveActivity::saveWrite(ByteString finalFilename)
std::vector<char> saveData = gameSave->Serialise(); std::vector<char> saveData = gameSave->Serialise();
if (saveData.size() == 0) if (saveData.size() == 0)
new ErrorMessage("Error", "Unable to serialize game data."); new ErrorMessage("Error", "Unable to serialize game data.");
else if (!Client::Ref().WriteFile(saveData, finalFilename)) else if (!Platform::WriteFile(saveData, finalFilename))
new ErrorMessage("Error", "Unable to write save file."); new ErrorMessage("Error", "Unable to write save file.");
else else
{ {

View File

@ -1281,7 +1281,7 @@ int luatpt_getscript(lua_State* l)
{ {
return 0; return 0;
} }
if (!Client::Ref().WriteFile(std::vector<char>(scriptData.begin(), scriptData.end()), filename)) if (!Platform::WriteFile(std::vector<char>(scriptData.begin(), scriptData.end()), filename))
{ {
return luaL_error(l, "Unable to write to file"); return luaL_error(l, "Unable to write to file");
} }

View File

@ -3880,7 +3880,7 @@ int LuaScriptInterface::fileSystem_copy(lua_State * l)
auto filename = tpt_lua_checkByteString(l, 1); auto filename = tpt_lua_checkByteString(l, 1);
auto newFilename = tpt_lua_checkByteString(l, 2); auto newFilename = tpt_lua_checkByteString(l, 2);
std::vector<char> fileData; std::vector<char> fileData;
lua_pushboolean(l, Client::Ref().ReadFile(fileData, filename) && Client::Ref().WriteFile(fileData, newFilename)); lua_pushboolean(l, Platform::ReadFile(fileData, filename) && Platform::WriteFile(fileData, newFilename));
return 1; return 1;
} }