Skip to content

Commit

Permalink
Merge pull request #449 from Wargus/filesystem
Browse files Browse the repository at this point in the history
Use more `fs::path`
  • Loading branch information
Jarod42 authored Aug 4, 2023
2 parents f6be62c + aac4741 commit 31da98a
Show file tree
Hide file tree
Showing 25 changed files with 230 additions and 410 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -551,14 +551,14 @@ set(stratagus_generic_HDRS
src/include/depend.h
src/include/editor.h
src/include/online_service.h
src/include/filesystem.h
src/include/font.h
src/include/fov.h
src/include/fow.h
src/include/fow_utils.h
src/include/game.h
src/include/icons.h
src/include/interface.h
src/include/iocompat.h
src/include/iolib.h
src/include/luacallback.h
src/include/map.h
Expand Down
24 changes: 8 additions & 16 deletions src/action/actions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@
#include "commands.h"
#include "game.h"
#include "interface.h"
#include "iocompat.h"
#include "luacallback.h"
#include "map.h"
#include "missile.h"
Expand Down Expand Up @@ -486,26 +485,19 @@ static void DumpUnitInfo(CUnit &unit)
time_t now;
time(&now);

std::string path(Parameters::Instance.GetUserDirectory());
fs::path path(Parameters::Instance.GetUserDirectory());
if (!GameName.empty()) {
path += "/";
path += GameName;
}
path += "/logs";
struct stat tmp;
if (stat(path.c_str(), &tmp) < 0) {
makedir(path.c_str(), 0777);
path /= GameName;
}
path /= "logs";
fs::create_directories(path);

path += "/unit_log_of_stratagus_";
path += std::to_string(ThisPlayer->Index);
path += "_";
path += std::to_string((intmax_t)now);
path += ".log";
path /= "unit_log_of_stratagus_" + std::to_string(ThisPlayer->Index) + "_"
+ std::to_string((intmax_t) now) + ".log";

logf = fopen(path.c_str(), "wb");
logf = fopen(path.string().c_str(), "wb");
if (!logf) {
DebugPrint("Could not open %s for writing\n" _C_ path.c_str());
DebugPrint("Could not open %s for writing\n" _C_ path.string().c_str());
return ;
}
fprintf(logf, "; Log file generated by Stratagus Version " VERSION "\n");
Expand Down
7 changes: 3 additions & 4 deletions src/editor/editloop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@
#include "game.h"
#include "guichan.h"
#include "interface.h"
#include "iocompat.h"
#include "iolib.h"
#include "map.h"
#include "menus.h"
Expand Down Expand Up @@ -1882,15 +1881,15 @@ static void EditorCallbackExit()
void CEditor::Init()
{
// Load and evaluate the editor configuration file
const std::string filename = LibraryFileName(Parameters::Instance.luaEditorStartFilename.c_str());
if (access(filename.c_str(), R_OK)) {
const fs::path filename = LibraryFileName(Parameters::Instance.luaEditorStartFilename.c_str());
if (!fs::exists(filename)) {
fprintf(stderr, "Editor configuration file '%s' was not found\n"
"Specify another with '-E file.lua'\n",
Parameters::Instance.luaEditorStartFilename.c_str());
ExitFatal(-1);
}

ShowLoadProgress(_("Script %s"), filename.c_str());
ShowLoadProgress(_("Script %s"), filename.string().c_str());
LuaLoadFile(filename);
LuaGarbageCollect();

Expand Down
89 changes: 30 additions & 59 deletions src/game/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@
#include "editor.h"
#include "font.h"
#include "interface.h"
#include "iocompat.h"
#include "iolib.h"
#include "map.h"
#include "minimap.h"
Expand Down Expand Up @@ -182,47 +181,19 @@ void FreeAllContainers()
*/
static void LoadStratagusMap(const std::string &smpname, const std::string &mapname)
{
char mapfull[PATH_MAX];
CFile file;

fs::path mapfull = mapname;
// Try the same directory as the smp file first
strcpy_s(mapfull, sizeof(mapfull), smpname.c_str());
char *p = strrchr(mapfull, '/');
if (!p) {
p = mapfull;
} else {
++p;
}
strcpy_s(p, sizeof(mapfull) - (p - mapfull), mapname.c_str());

if (file.open(mapfull, CL_OPEN_READ) == -1) {
// Not found, try StratagusLibPath and the smp's dir
strcpy_s(mapfull, sizeof(mapfull), StratagusLibPath.c_str());
strcat_s(mapfull, sizeof(mapfull), "/");
strcat_s(mapfull, sizeof(mapfull), smpname.c_str());
char *p = strrchr(mapfull, '/');
if (!p) {
p = mapfull;
} else {
++p;
}
strcpy_s(p, sizeof(mapfull) - (p - mapfull), mapname.c_str());
if (file.open(mapfull, CL_OPEN_READ) == -1) {
// Not found again, try StratagusLibPath
strcpy_s(mapfull, sizeof(mapfull), StratagusLibPath.c_str());
strcat_s(mapfull, sizeof(mapfull), "/");
strcat_s(mapfull, sizeof(mapfull), mapname.c_str());
if (file.open(mapfull, CL_OPEN_READ) == -1) {
// Not found, try mapname by itself as a last resort
strcpy_s(mapfull, sizeof(mapfull), mapname.c_str());
} else {
file.close();
}
} else {
file.close();
// Not found, try StratagusLibPath and the smp's dir
// Not found again, try StratagusLibPath
// Not found, try mapname by itself as a last resort
for (auto candidate : {fs::path(smpname).parent_path() / mapname,
fs::path(StratagusLibPath) / fs::path(smpname).parent_path() / mapname,
fs::path(StratagusLibPath) / mapname,
fs::path(mapname)}) {
if (fs::exists(candidate)) {
mapfull = candidate;
break;
}
} else {
file.close();
}

if (LcmPreventRecurse) {
Expand All @@ -232,7 +203,7 @@ static void LoadStratagusMap(const std::string &smpname, const std::string &mapn
InitPlayers();
LcmPreventRecurse = 1;
if (LuaLoadFile(mapfull) == -1) {
fprintf(stderr, "Can't load lua file: %s\n", mapfull);
fprintf(stderr, "Can't load lua file: %s\n", mapfull.string().c_str());
ExitFatal(-1);
}
LcmPreventRecurse = 0;
Expand All @@ -252,7 +223,7 @@ static void LoadStratagusMap(const std::string &smpname, const std::string &mapn
}

// Write a small image of map preview
static void WriteMapPreview(const char *mapname, CMap &map)
static void WriteMapPreview(const fs::path &mapname, CMap &map)
{
const int rectSize = 5; // size of rectange used for player start spots
const SDL_PixelFormat *fmt = MinimapSurface->format;
Expand All @@ -273,7 +244,7 @@ static void WriteMapPreview(const char *mapname, CMap &map)
}

SDL_UnlockSurface(preview);
IMG_SavePNG(preview, mapname);
IMG_SavePNG(preview, mapname.string().c_str());
SDL_FreeSurface(preview);
}

Expand Down Expand Up @@ -345,12 +316,12 @@ static int WriteMapPresentation(const std::string &mapname, CMap &map, Vec2i new
** @param map map to save
** @param writeTerrain write the tiles map in the .sms
*/
int WriteMapSetup(const char *mapSetup, CMap &map, int writeTerrain, Vec2i newSize, Vec2i offset)
static int WriteMapSetup(const fs::path &mapSetup, CMap &map, int writeTerrain, Vec2i newSize, Vec2i offset)
{
FileWriter *f = nullptr;

try {
f = CreateFileWriter(mapSetup);
f = CreateFileWriter(mapSetup.string());

f->printf("-- Stratagus Map Setup\n");
f->printf("-- File generated by the Stratagus V" VERSION " builtin map editor.\n");
Expand All @@ -360,7 +331,7 @@ int WriteMapSetup(const char *mapSetup, CMap &map, int writeTerrain, Vec2i newSi
f->printf("-- preamble\n");
f->printf("if CanAccessFile(__file__ .. \".preamble\") then Load(__file__ .. \".preamble\", Editor.Running == 0) end\n\n");
if (!Map.Info.Preamble.empty()) {
FileWriter *preamble = CreateFileWriter(std::string(mapSetup) + ".preamble");
FileWriter *preamble = CreateFileWriter(mapSetup.string() + ".preamble");
preamble->write(Map.Info.Preamble.c_str(), Map.Info.Preamble.size());
delete preamble;
}
Expand Down Expand Up @@ -516,13 +487,13 @@ int WriteMapSetup(const char *mapSetup, CMap &map, int writeTerrain, Vec2i newSi
f->printf("-- postamble\n");
f->printf("if CanAccessFile(__file__ .. \".postamble\") then Load(__file__ .. \".postamble\", Editor.Running == 0) end\n\n");
if (!Map.Info.Postamble.empty()) {
FileWriter *postamble = CreateFileWriter(std::string(mapSetup) + ".postamble");
FileWriter *postamble = CreateFileWriter(mapSetup.string() + ".postamble");
postamble->write(Map.Info.Postamble.c_str(), Map.Info.Postamble.size());
delete postamble;
}

} catch (const FileException &) {
fprintf(stderr, "Can't save map setup : '%s' \n", mapSetup);
fprintf(stderr, "Can't save map setup : '%s' \n", mapSetup.string().c_str());
delete f;
return -1;
}
Expand All @@ -547,25 +518,26 @@ int SaveStratagusMap(const std::string &mapName, CMap &map, int writeTerrain, Ve
ExitFatal(-1);
}

char mapSetup[PATH_MAX];
strcpy_s(mapSetup, sizeof(mapSetup), mapName.c_str());
char *setupExtension = strstr(mapSetup, ".smp");
if (!setupExtension) {
std::string extraExtension; // typically compression extension
fs::path mapSetup = mapName;
while (mapSetup.has_extension() && mapSetup.extension() != ".smp") {
extraExtension = mapSetup.extension().string() + extraExtension;
mapSetup.replace_extension();
}
if (mapSetup.extension() != ".smp") {
fprintf(stderr, "%s: invalid Stratagus map filename\n", mapName.c_str());
return -1;
}

char previewName[PATH_MAX];
strcpy_s(previewName, sizeof(previewName), mapName.c_str());
char *previewExtension = strstr(previewName, ".smp");
memcpy(previewExtension, ".png\0", 5 * sizeof(char));
fs::path previewName = mapSetup;
previewName.replace_extension(".png");
WriteMapPreview(previewName, map);

memcpy(setupExtension, ".sms", 4 * sizeof(char));
if (WriteMapPresentation(mapName, map, newSize) == -1) {
return -1;
}

mapSetup.replace_extension(".sms" + extraExtension);
return WriteMapSetup(mapSetup, map, writeTerrain, newSize, offset);
}

Expand Down Expand Up @@ -1166,8 +1138,7 @@ static int CclSetGameName(lua_State *l)
}

if (!GameName.empty()) {
std::string path = Parameters::Instance.GetUserDirectory() + "/" + GameName;
makedir(path.c_str(), 0777);
fs::create_directories(Parameters::Instance.GetUserDirectory() / GameName);
}
return 0;
}
Expand Down
32 changes: 12 additions & 20 deletions src/game/replay.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
#include "commands.h"
#include "game.h"
#include "interface.h"
#include "iocompat.h"
#include "iolib.h"
#include "map.h"
#include "netconnect.h"
Expand All @@ -57,6 +56,7 @@
#include "version.h"

#include <sstream>
#include <sys/stat.h>
#include <time.h>

extern void ExpandPath(std::string &newpath, const std::string &path);
Expand Down Expand Up @@ -371,34 +371,27 @@ void CommandLog(const char *action, const CUnit *unit, int flush,
if (!LogFile) {
time_t now;
time(&now);
struct stat tmp;

std::string path(Parameters::Instance.GetUserDirectory());
fs::path path(Parameters::Instance.GetUserDirectory());
if (!GameName.empty()) {
path += "/";
path += GameName;
path /= GameName;
}
path += "/logs";
path /= "logs";

if (stat(path.c_str(), &tmp) < 0) {
makedir(path.c_str(), 0777);
}
fs::create_directories(path);

path += "/log_of_stratagus_";
path += std::to_string(ThisPlayer->Index);
path += "_";
path += std::to_string((intmax_t)now);
path += ".log";
path /= "/log_of_stratagus_" + std::to_string(ThisPlayer->Index) + "_"
+ std::to_string((intmax_t) now) + ".log";

LogFile = new CFile;
if (LogFile->open(path.c_str(), CL_OPEN_WRITE) == -1) {
if (LogFile->open(path.string().c_str(), CL_OPEN_WRITE) == -1) {
// don't retry for each command
CommandLogDisabled = false;
delete LogFile;
LogFile = nullptr;
return;
}
LastLogFileName = path;
LastLogFileName = path.string();
if (CurrentReplay) {
SaveFullLog(*LogFile);
}
Expand Down Expand Up @@ -909,7 +902,6 @@ int SaveReplay(const std::string &filename)
{
FILE *fd;
char *buf;
std::string destination;
struct stat sb;
size_t size;

Expand All @@ -918,7 +910,7 @@ int SaveReplay(const std::string &filename)
return -1;
}

destination = Parameters::Instance.GetUserDirectory() + "/" + GameName + "/logs/" + filename;
auto destination = Parameters::Instance.GetUserDirectory() / GameName / "logs" / filename;

if (!LastLogFileName.empty() && stat(LastLogFileName.c_str(), &sb)) {
fprintf(stderr, "stat failed\n");
Expand All @@ -938,9 +930,9 @@ int SaveReplay(const std::string &filename)
size = fread(buf, sb.st_size, 1, fd);
fclose(fd);

fd = fopen(destination.c_str(), "wb");
fd = fopen(destination.string().c_str(), "wb");
if (!fd) {
fprintf(stderr, "Can't save to '%s'\n", destination.c_str());
fprintf(stderr, "Can't save to '%s'\n", destination.string().c_str());
delete[] buf;
return -1;
}
Expand Down
Loading

0 comments on commit 31da98a

Please sign in to comment.