Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: orderlist Importing / Exporting. #679

Open
wants to merge 15 commits into
base: jgrpp
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/fileio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ static const char * const _subdirs[] = {
"save" PATHSEP "autosave" PATHSEP,
"scenario" PATHSEP,
"scenario" PATHSEP "heightmap" PATHSEP,
"orderlist" PATHSEP,
"gm" PATHSEP,
"data" PATHSEP,
"baseset" PATHSEP,
Expand Down Expand Up @@ -1094,7 +1095,7 @@ void DeterminePaths(const char *exe, bool only_local_path)
DEBUG(misc, 1, "%s found as personal directory", _personal_dir.c_str());

static const Subdirectory default_subdirs[] = {
SAVE_DIR, AUTOSAVE_DIR, SCENARIO_DIR, HEIGHTMAP_DIR, BASESET_DIR, NEWGRF_DIR, AI_DIR, AI_LIBRARY_DIR, GAME_DIR, GAME_LIBRARY_DIR, SCREENSHOT_DIR, SOCIAL_INTEGRATION_DIR
SAVE_DIR, AUTOSAVE_DIR, SCENARIO_DIR, HEIGHTMAP_DIR, ORDERLIST_DIR, BASESET_DIR, NEWGRF_DIR, AI_DIR, AI_LIBRARY_DIR, GAME_DIR, GAME_LIBRARY_DIR, SCREENSHOT_DIR, SOCIAL_INTEGRATION_DIR
};

for (uint i = 0; i < lengthof(default_subdirs); i++) {
Expand Down
6 changes: 6 additions & 0 deletions src/fileio_type.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ enum AbstractFileType {
FT_SAVEGAME, ///< old or new savegame
FT_SCENARIO, ///< old or new scenario
FT_HEIGHTMAP, ///< heightmap file
FT_ORDERLIST, ///< orderlist file

FT_INVALID = 7, ///< Invalid or unknown file type.
FT_NUMBITS = 3, ///< Number of bits required for storing a #AbstractFileType value.
Expand All @@ -34,6 +35,9 @@ enum DetailedFileType {
DFT_HEIGHTMAP_BMP, ///< BMP file.
DFT_HEIGHTMAP_PNG, ///< PNG file.

/* Orderlist files */
DFT_ORDERLIST, ///< JSON file.

/* fios 'files' */
DFT_FIOS_DRIVE, ///< A drive (letter) entry.
DFT_FIOS_PARENT, ///< A parent directory entry.
Expand Down Expand Up @@ -76,6 +80,7 @@ enum FiosType {
FIOS_TYPE_OLD_SCENARIO = MAKE_FIOS_TYPE(FT_SCENARIO, DFT_OLD_GAME_FILE),
FIOS_TYPE_PNG = MAKE_FIOS_TYPE(FT_HEIGHTMAP, DFT_HEIGHTMAP_PNG),
FIOS_TYPE_BMP = MAKE_FIOS_TYPE(FT_HEIGHTMAP, DFT_HEIGHTMAP_BMP),
FIOS_TYPE_ORDERLIST = MAKE_FIOS_TYPE(FT_ORDERLIST, DFT_ORDERLIST),

FIOS_TYPE_INVALID = MAKE_FIOS_TYPE(FT_INVALID, DFT_INVALID),
};
Expand Down Expand Up @@ -111,6 +116,7 @@ enum Subdirectory {
AUTOSAVE_DIR, ///< Subdirectory of save for autosaves
SCENARIO_DIR, ///< Base directory for all scenarios
HEIGHTMAP_DIR, ///< Subdirectory of scenario for heightmaps
ORDERLIST_DIR, ///< Subdirectort for all orderlists
OLD_GM_DIR, ///< Old subdirectory for the music
OLD_DATA_DIR, ///< Old subdirectory for the data.
BASESET_DIR, ///< Subdirectory for all base data (base sets, intro game)
Expand Down
74 changes: 72 additions & 2 deletions src/fios.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ void FileList::BuildFileList(AbstractFileType abstract_filetype, SaveLoadOperati
FiosGetHeightmapList(fop, show_dirs, *this);
break;

case FT_ORDERLIST:
FiosGetOrderlistList(fop, show_dirs, *this);
break;

default:
NOT_REACHED();
}
Expand Down Expand Up @@ -185,6 +189,7 @@ bool FiosBrowseTo(const FiosItem *item)
case FIOS_TYPE_OLDFILE:
case FIOS_TYPE_SCENARIO:
case FIOS_TYPE_OLD_SCENARIO:
case FIOS_TYPE_ORDERLIST:
case FIOS_TYPE_PNG:
case FIOS_TYPE_BMP:
return false;
Expand Down Expand Up @@ -224,6 +229,11 @@ static std::string FiosMakeFilename(const std::string *path, const char *name, c
* @param last Last element of buffer \a buf.
* @return The completed filename.
*/

std::string FiosMakeOrderListName(const char *name)
{
return FiosMakeFilename(_fios_path, name, ".json");
}
std::string FiosMakeSavegameName(const char *name)
{
const char *extension = (_game_mode == GM_EDITOR) ? ".scn" : ".sav";
Expand All @@ -249,9 +259,23 @@ std::string FiosMakeHeightmapName(const char *name)
* @param name Filename to delete.
* @return Whether the file deletion was successful.
*/
bool FiosDelete(const char *name)
bool FiosDelete(const char *name,AbstractFileType ft)
{
std::string filename = FiosMakeSavegameName(name);
std::string filename;

switch (ft) {
case FT_SAVEGAME:
case FT_SCENARIO:
filename = FiosMakeSavegameName(name);
break;
case FT_ORDERLIST:
filename = FiosMakeOrderListName(name);
break;
default:
NOT_REACHED();
break;
}

return unlink(filename.c_str()) == 0;
}

Expand Down Expand Up @@ -499,6 +523,52 @@ void FiosGetSavegameList(SaveLoadOperation fop, bool show_dirs, FileList &file_l
FiosGetFileList(fop, show_dirs, &FiosGetSavegameListCallback, NO_DIRECTORY, file_list);
}

/**
* Callback for FiosGetOrderlistList. It tells if a file is a orederlist or not.
* @param fop Purpose of collecting the list.
* @param file Name of the file to check.
* @param ext A pointer to the extension identifier inside file
* @param title Buffer if a callback wants to lookup the title of the file; nullptr to skip the lookup
* @param last Last available byte in buffer (to prevent buffer overflows); not used when title == nullptr
* @return a FIOS_TYPE_* type of the found file, FIOS_TYPE_INVALID if not a savegame
* @see FiosGetFileList
* @see FiosGetOrderlistList
*/
FiosType FiosGetOrderlistListCallback(SaveLoadOperation fop, const std::string &file, const char *ext, char *title, const char *last)
{
/* Show orderlist files
* .json orderlist files
*/

/* Don't crash if we supply no extension */
if (ext == nullptr) ext = "";

if (StrEqualsIgnoreCase(ext, ".json")) {
GetFileTitle(file, title, last, ORDERLIST_DIR);
return FIOS_TYPE_ORDERLIST;
}

return FIOS_TYPE_INVALID;
}

/**
* Get a list of orderlists.
* @param fop Purpose of collecting the list.
* @param show_dirs Whether to show directories.
* @param file_list Destination of the found files.
* @see FiosGetFileList
*/
void FiosGetOrderlistList(SaveLoadOperation fop, bool show_dirs, FileList &file_list)
{
static std::optional<std::string> fios_save_path;

if (!fios_save_path) fios_save_path = FioFindDirectory(ORDERLIST_DIR);

_fios_path = &(*fios_save_path);

FiosGetFileList(fop, show_dirs, &FiosGetOrderlistListCallback, NO_DIRECTORY, file_list);
}

/**
* Callback for FiosGetFileList. It tells if a file is a scenario or not.
* @param fop Purpose of collecting the list.
Expand Down
9 changes: 7 additions & 2 deletions src/fios.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "newgrf_config.h"
#include "network/core/tcp_content_type.h"
#include <vector>
#include <order_type.h>


/** Special values for save-load window for the data parameter of #InvalidateWindowData. */
Expand Down Expand Up @@ -51,23 +52,27 @@ DECLARE_ENUM_AS_BIT_SET(SortingBits)
/* Variables to display file lists */
extern SortingBits _savegame_sort_order;

void ShowSaveLoadDialog(AbstractFileType abstract_filetype, SaveLoadOperation fop);
void ShowSaveLoadDialog(AbstractFileType abstract_filetype, SaveLoadOperation fop,const Vehicle * veh = nullptr);

void FiosGetSavegameList(SaveLoadOperation fop, bool show_dirs, FileList &file_list);
void FiosGetScenarioList(SaveLoadOperation fop, bool show_dirs, FileList &file_list);
void FiosGetHeightmapList(SaveLoadOperation fop, bool show_dirs, FileList &file_list);
void FiosGetOrderlistList(SaveLoadOperation fop, bool show_dirs, FileList &file_list);

bool FiosBrowseTo(const FiosItem *item);

std::string FiosGetCurrentPath();
std::optional<uint64_t> FiosGetDiskFreeSpace(const std::string &path);
bool FiosDelete(const char *name);
bool FiosDelete(const char *name, AbstractFileType file_type);
std::string FiosMakeHeightmapName(const char *name);
std::string FiosMakeSavegameName(const char *name);
std::string FiosMakeOrderListName(const char *name);


FiosType FiosGetSavegameListCallback(SaveLoadOperation fop, const std::string &file, const char *ext, char *title, const char *last);
FiosType FiosGetScenarioListCallback(SaveLoadOperation fop, const std::string &file, const char *ext, char *title, const char *last);
FiosType FiosGetHeightmapListCallback(SaveLoadOperation fop, const std::string &file, const char *ext, char *title, const char *last);
FiosType FiosGetOrderlistListCallback(SaveLoadOperation fop, const std::string &file, const char *ext, char *title, const char *last);

void ScanScenarios();
const char *FindScenario(const ContentInfo *ci, bool md5sum);
Expand Down
Loading
Loading