Los datos se guardan en la carpeta de sistema

This commit is contained in:
2022-12-07 11:23:19 +01:00
parent c87e1e68a9
commit bbe82d329b
7 changed files with 262 additions and 64 deletions

View File

@@ -5,25 +5,38 @@
#include <iostream>
#include <fstream>
#include <string>
#include <sys/stat.h>
#include <unistd.h>
#ifndef _WIN32
#include <pwd.h>
#endif
// Constructor
Director::Director(std::string path)
Director::Director(int argc, char *argv[])
{
// Inicializa variables
section.name = PROG_SECTION_LOGO;
// Crea el objeto que controla los ficheros de recursos
asset = new Asset(path);
// Establece la lista de ficheros
if (!setFileList())
{ // Si falta algún fichero no inicia el programa
section.name = PROG_SECTION_QUIT;
}
// Inicializa las opciones del programa
initOptions();
// Comprueba los parametros del programa
checkProgramArguments(argc, argv);
// Crea la carpeta del sistema donde guardar datos
createSystemFolder();
// Crea el objeto que controla los ficheros de recursos
asset = new Asset(executablePath);
asset->setVerbose(options->console);
// Si falta algún fichero no inicia el programa
if (!setFileList())
{
exit(EXIT_FAILURE);
}
// Carga el fichero de configuración
loadConfigFile();
@@ -108,7 +121,10 @@ bool Director::initSDL()
if (SDL_Init(SDL_INIT_EVERYTHING) < 0)
// if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_GAMECONTROLLER | SDL_INIT_AUDIO) < 0)
{
std::cout << "SDL could not initialize!\nSDL Error: " << SDL_GetError() << std::endl;
if (options->console)
{
std::cout << "SDL could not initialize!\nSDL Error: " << SDL_GetError() << std::endl;
}
success = false;
}
else
@@ -119,7 +135,10 @@ bool Director::initSDL()
// Establece el filtro de la textura
if (!SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, std::to_string(options->filter).c_str()))
{
std::cout << "Warning: Nearest texture filtering not enabled!\n";
if (options->console)
{
std::cout << "Warning: Nearest texture filtering not enabled!\n";
}
}
// Crea la ventana
@@ -133,7 +152,10 @@ bool Director::initSDL()
window = SDL_CreateWindow(WINDOW_CAPTION, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, (options->gameWidth + incW) * options->windowSize, (options->gameHeight + incH) * options->windowSize, SDL_WINDOW_SHOWN | SDL_WINDOW_ALLOW_HIGHDPI);
if (window == nullptr)
{
std::cout << "Window could not be created!\nSDL Error: " << SDL_GetError() << std::endl;
if (options->console)
{
std::cout << "Window could not be created!\nSDL Error: " << SDL_GetError() << std::endl;
}
success = false;
}
else
@@ -150,7 +172,10 @@ bool Director::initSDL()
if (renderer == nullptr)
{
std::cout << "Renderer could not be created!\nSDL Error: " << SDL_GetError() << std::endl;
if (options->console)
{
std::cout << "Renderer could not be created!\nSDL Error: " << SDL_GetError() << std::endl;
}
success = false;
}
else
@@ -167,7 +192,10 @@ bool Director::initSDL()
}
}
std::cout << std::endl;
if (options->console)
{
std::cout << std::endl;
}
return success;
}
@@ -181,10 +209,9 @@ bool Director::setFileList()
#endif
// Ficheros de configuración
asset->add(prefix + "/data/config/score.bin", t_data, false);
asset->add(systemFolder + "/config.txt", t_data, false, true);
asset->add(systemFolder + "/score.bin", t_data, false, true);
asset->add(prefix + "/data/config/demo.bin", t_data);
asset->add(prefix + "/data/config/config.txt", t_data, false);
asset->add(prefix + "/data/config/jailer_id.txt", t_data, false);
asset->add(prefix + "/data/config/gamecontrollerdb.txt", t_data);
// Musicas
@@ -321,13 +348,11 @@ void Director::initOptions()
inp.deviceType = INPUT_USE_GAMECONTROLLER;
options->input.push_back(inp);
// Video
options->gameWidth = GAMECANVAS_WIDTH;
options->gameHeight = GAMECANVAS_HEIGHT;
options->videoMode = 0;
options->windowSize = 3;
options->language = ba_BA;
options->difficulty = DIFFICULTY_NORMAL;
options->playerSelected = 0;
options->filter = FILTER_NEAREST;
options->vSync = true;
options->integerScale = true;
@@ -336,6 +361,12 @@ void Director::initOptions()
options->borderHeight = 0;
options->borderEnabled = false;
// Varios
options->playerSelected = 0;
options->difficulty = DIFFICULTY_NORMAL;
options->language = ba_BA;
options->console = false;
// Online
options->online.enabled = false;
options->online.server = "";
@@ -345,6 +376,77 @@ void Director::initOptions()
options->online.score = 0;
}
// Comprueba los parametros del programa
void Director::checkProgramArguments(int argc, char *argv[])
{
// Establece la ruta del programa
executablePath = argv[0];
// Comprueba el resto de parametros
for (int i = 1; i < argc; ++i)
{
if (strcmp(argv[i], "--console") == 0)
{
options->console = true;
}
}
}
// Crea la carpeta del sistema donde guardar datos
void Director::createSystemFolder()
{
#ifdef DEBUG
const std::string folderName = "coffee_crisis_debug";
#else
const std::string folderName = "coffee_crisis";
#endif
#ifdef _WIN32
systemFolder = std::string(getenv("APPDATA")) + "/" + folderName;
#elif __APPLE__
struct passwd *pw = getpwuid(getuid());
const char *homedir = pw->pw_dir;
systemFolder = std::string(homedir) + "/Library/Application Support/" + folderName;
#elif __linux__
struct passwd *pw = getpwuid(getuid());
const char *homedir = pw->pw_dir;
systemFolder = std::string(homedir) + "/." + folderName;
#endif
struct stat st = {0};
if (stat(systemFolder.c_str(), &st) == -1)
{
errno = 0;
#ifdef _WIN32
int ret = mkdir(systemFolder.c_str());
#else
int ret = mkdir(systemFolder.c_str(), S_IRWXU);
#endif
if (ret == -1)
{
switch (errno)
{
case EACCES:
printf("the parent directory does not allow write");
exit(EXIT_FAILURE);
case EEXIST:
printf("pathname already exists");
exit(EXIT_FAILURE);
case ENAMETOOLONG:
printf("pathname is too long");
exit(EXIT_FAILURE);
default:
perror("mkdir");
exit(EXIT_FAILURE);
}
}
}
}
// Carga el fichero de configuración
bool Director::loadConfigFile()
{
@@ -352,14 +454,18 @@ bool Director::loadConfigFile()
bool success = true;
// Variables para manejar el fichero
const std::string filePath = "config.txt";
std::string line;
std::ifstream file(asset->get("config.txt"));
std::ifstream file(asset->get(filePath));
// Si el fichero se puede abrir
if (file.good())
{
// Procesa el fichero linea a linea
std::cout << "Reading file config.txt\n";
if (options->console)
{
std::cout << "Reading file " << filePath << std::endl;
}
while (std::getline(file, line))
{
// Comprueba que la linea no sea un comentario
@@ -370,15 +476,21 @@ bool Director::loadConfigFile()
// Procesa las dos subcadenas
if (!setOptions(options, line.substr(0, pos), line.substr(pos + 1, line.length())))
{
std::cout << "Warning: file config.txt\n";
std::cout << "unknown parameter " << line.substr(0, pos).c_str() << std::endl;
if (options->console)
{
std::cout << "Warning: file " << filePath << std::endl;
std::cout << "Unknown parameter " << line.substr(0, pos).c_str() << std::endl;
}
success = false;
}
}
}
// Cierra el fichero
std::cout << "Closing file config.txt\n\n";
if (options->console)
{
std::cout << "Closing file " << filePath << std::endl;
}
file.close();
}
@@ -544,7 +656,10 @@ void Director::initOnline()
if (options->online.jailerID == "")
{ // Jailer ID no definido
screen->showNotification("No ha especificado ningun Jailer ID");
std::cout << "No ha especificado ningun Jailer ID" << std::endl;
if (options->console)
{
std::cout << "No ha especificado ningun Jailer ID" << std::endl;
}
}
else
{ // Jailer ID iniciado
@@ -556,12 +671,18 @@ void Director::initOnline()
if (jscore::initOnlineScore(options->online.gameID))
{
screen->showNotification(options->online.jailerID + " ha iniciado sesion");
std::cout << options->online.jailerID << " ha iniciado sesion" << std::endl;
if (options->console)
{
std::cout << options->online.jailerID << " ha iniciado sesion" << std::endl;
}
}
else
{
screen->showNotification("Fallo al conectar a " + options->online.server);
std::cout << "Fallo al conectar a " << options->online.server << std::endl;
if (options->console)
{
std::cout << "Fallo al conectar a " << options->online.server << std::endl;
}
options->online.enabled = false;
@@ -573,7 +694,10 @@ void Director::initOnline()
if (points == 0)
{ // Fallo de conexión o no hay registros
screen->showNotification("No se ha podido obtener la puntuacion online");
std::cout << "No se ha podido obtener la puntuacion online" << std::endl;
if (options->console)
{
std::cout << "No se ha podido obtener la puntuacion online" << std::endl;
}
}
else
{