Cambiado config.bin a config.txt
This commit is contained in:
@@ -177,6 +177,7 @@ bool Director::setFileList()
|
||||
asset->add(prefix + "/data/config/score.bin", t_data, false);
|
||||
asset->add(prefix + "/data/config/demo.bin", t_data);
|
||||
asset->add(prefix + "/data/config/config.bin", t_data, false);
|
||||
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);
|
||||
|
||||
@@ -328,7 +329,8 @@ void Director::initOptions()
|
||||
options->borderEnabled = false;
|
||||
|
||||
// Online
|
||||
options->online.enabled = true;
|
||||
options->online.enabled = false;
|
||||
options->online.server = "";
|
||||
options->online.gameID = "coffee_crisis";
|
||||
options->online.jailerID = "";
|
||||
options->online.score = 0;
|
||||
@@ -340,83 +342,60 @@ bool Director::loadConfigFile()
|
||||
// Indicador de éxito en la carga
|
||||
bool success = true;
|
||||
|
||||
const std::string p = asset->get("config.bin");
|
||||
std::string filename = p.substr(p.find_last_of("\\/") + 1);
|
||||
SDL_RWops *file = SDL_RWFromFile(p.c_str(), "r+b");
|
||||
// Variables para manejar el fichero
|
||||
std::string line;
|
||||
std::ifstream file(asset->get("config.txt"));
|
||||
|
||||
// El fichero no existe
|
||||
if (file == nullptr)
|
||||
// Si el fichero se puede abrir
|
||||
if (file.good())
|
||||
{
|
||||
printf("Warning: Unable to open %s file\n", filename.c_str());
|
||||
|
||||
// Crea el fichero para escritura
|
||||
file = SDL_RWFromFile(p.c_str(), "w+b");
|
||||
if (file != nullptr)
|
||||
{ // Ha podido crear el fichero
|
||||
printf("New file (%s) created!\n", filename.c_str());
|
||||
|
||||
// Escribe los datos
|
||||
SDL_RWwrite(file, &options->fullScreenMode, sizeof(options->fullScreenMode), 1);
|
||||
SDL_RWwrite(file, &options->windowSize, sizeof(options->windowSize), 1);
|
||||
SDL_RWwrite(file, &options->language, sizeof(options->language), 1);
|
||||
SDL_RWwrite(file, &options->difficulty, sizeof(options->difficulty), 1);
|
||||
SDL_RWwrite(file, &options->input.at(0).deviceType, sizeof(options->input.at(0).deviceType), 1);
|
||||
SDL_RWwrite(file, &options->input.at(1).deviceType, sizeof(options->input.at(1).deviceType), 1);
|
||||
SDL_RWwrite(file, &options->filter, sizeof(options->filter), 1);
|
||||
SDL_RWwrite(file, &options->vSync, sizeof(options->vSync), 1);
|
||||
SDL_RWwrite(file, &options->screenWidth, sizeof(options->screenWidth), 1);
|
||||
SDL_RWwrite(file, &options->screenHeight, sizeof(options->screenHeight), 1);
|
||||
SDL_RWwrite(file, &options->integerScale, sizeof(options->integerScale), 1);
|
||||
SDL_RWwrite(file, &options->keepAspect, sizeof(options->keepAspect), 1);
|
||||
|
||||
// Cierra el fichero
|
||||
SDL_RWclose(file);
|
||||
}
|
||||
else
|
||||
{ // No ha podido crear el fichero
|
||||
printf("Error: Unable to create file %s\n", filename.c_str());
|
||||
success = false;
|
||||
}
|
||||
}
|
||||
// El fichero existe
|
||||
else
|
||||
{
|
||||
// Carga los datos
|
||||
printf("Reading file %s\n", filename.c_str());
|
||||
SDL_RWread(file, &options->fullScreenMode, sizeof(options->fullScreenMode), 1);
|
||||
SDL_RWread(file, &options->windowSize, sizeof(options->windowSize), 1);
|
||||
SDL_RWread(file, &options->language, sizeof(options->language), 1);
|
||||
SDL_RWread(file, &options->difficulty, sizeof(options->difficulty), 1);
|
||||
SDL_RWread(file, &options->input.at(0).deviceType, sizeof(options->input.at(0).deviceType), 1);
|
||||
SDL_RWread(file, &options->input.at(1).deviceType, sizeof(options->input.at(1).deviceType), 1);
|
||||
SDL_RWread(file, &options->filter, sizeof(options->filter), 1);
|
||||
SDL_RWread(file, &options->vSync, sizeof(options->vSync), 1);
|
||||
SDL_RWread(file, &options->screenWidth, sizeof(options->screenWidth), 1);
|
||||
SDL_RWread(file, &options->screenHeight, sizeof(options->screenHeight), 1);
|
||||
SDL_RWread(file, &options->integerScale, sizeof(options->integerScale), 1);
|
||||
SDL_RWread(file, &options->keepAspect, sizeof(options->keepAspect), 1);
|
||||
|
||||
// Normaliza los valores
|
||||
const bool a = options->fullScreenMode == 0;
|
||||
const bool b = options->fullScreenMode == SDL_WINDOW_FULLSCREEN;
|
||||
const bool c = options->fullScreenMode == SDL_WINDOW_FULLSCREEN_DESKTOP;
|
||||
if (!(a || b || c))
|
||||
// Procesa el fichero linea a linea
|
||||
std::cout << "Reading file config.txt\n";
|
||||
while (std::getline(file, line))
|
||||
{
|
||||
options->fullScreenMode = 0;
|
||||
}
|
||||
|
||||
if (options->windowSize < 1 || options->windowSize > 4)
|
||||
{
|
||||
options->windowSize = 3;
|
||||
}
|
||||
|
||||
if (options->language < 0 || options->language > MAX_LANGUAGES)
|
||||
{
|
||||
options->language = en_UK;
|
||||
// Comprueba que la linea no sea un comentario
|
||||
if (line.substr(0, 1) != "#")
|
||||
{
|
||||
// Encuentra la posición del caracter '='
|
||||
int pos = line.find("=");
|
||||
// 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;
|
||||
success = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Cierra el fichero
|
||||
SDL_RWclose(file);
|
||||
std::cout << "Closing file config.txt\n\n";
|
||||
file.close();
|
||||
}
|
||||
|
||||
// El fichero no existe
|
||||
else
|
||||
{ // Crea el fichero con los valores por defecto
|
||||
saveConfigFile();
|
||||
}
|
||||
|
||||
// Normaliza los valores
|
||||
const bool a = options->fullScreenMode == 0;
|
||||
const bool b = options->fullScreenMode == SDL_WINDOW_FULLSCREEN;
|
||||
const bool c = options->fullScreenMode == SDL_WINDOW_FULLSCREEN_DESKTOP;
|
||||
if (!(a || b || c))
|
||||
{
|
||||
options->fullScreenMode = 0;
|
||||
}
|
||||
|
||||
if (options->windowSize < 1 || options->windowSize > 4)
|
||||
{
|
||||
options->windowSize = 3;
|
||||
}
|
||||
|
||||
if (options->language < 0 || options->language > MAX_LANGUAGES)
|
||||
{
|
||||
options->language = en_UK;
|
||||
}
|
||||
|
||||
return success;
|
||||
@@ -426,36 +405,60 @@ bool Director::loadConfigFile()
|
||||
bool Director::saveConfigFile()
|
||||
{
|
||||
bool success = true;
|
||||
const std::string p = asset->get("config.bin");
|
||||
std::string filename = p.substr(p.find_last_of("\\/") + 1);
|
||||
SDL_RWops *file = SDL_RWFromFile(p.c_str(), "w+b");
|
||||
if (file != nullptr)
|
||||
|
||||
// Crea y abre el fichero de texto
|
||||
std::ofstream file(asset->get("config.txt"));
|
||||
|
||||
// Escribe en el fichero
|
||||
file << "## VISUAL OPTIONS\n";
|
||||
if (options->fullScreenMode == 0)
|
||||
{
|
||||
// Guarda los datos
|
||||
SDL_RWwrite(file, &options->fullScreenMode, sizeof(options->fullScreenMode), 1);
|
||||
SDL_RWwrite(file, &options->windowSize, sizeof(options->windowSize), 1);
|
||||
SDL_RWwrite(file, &options->language, sizeof(options->language), 1);
|
||||
SDL_RWwrite(file, &options->difficulty, sizeof(options->difficulty), 1);
|
||||
SDL_RWwrite(file, &options->input.at(0).deviceType, sizeof(options->input.at(0).deviceType), 1);
|
||||
SDL_RWwrite(file, &options->input.at(1).deviceType, sizeof(options->input.at(1).deviceType), 1);
|
||||
SDL_RWwrite(file, &options->filter, sizeof(options->filter), 1);
|
||||
SDL_RWwrite(file, &options->vSync, sizeof(options->vSync), 1);
|
||||
SDL_RWwrite(file, &options->screenWidth, sizeof(options->screenWidth), 1);
|
||||
SDL_RWwrite(file, &options->screenHeight, sizeof(options->screenHeight), 1);
|
||||
SDL_RWwrite(file, &options->integerScale, sizeof(options->integerScale), 1);
|
||||
SDL_RWwrite(file, &options->keepAspect, sizeof(options->keepAspect), 1);
|
||||
file << "fullScreenMode=0\n";
|
||||
}
|
||||
|
||||
printf("Writing file %s\n", filename.c_str());
|
||||
else if (options->fullScreenMode == SDL_WINDOW_FULLSCREEN)
|
||||
{
|
||||
file << "fullScreenMode=SDL_WINDOW_FULLSCREEN\n";
|
||||
}
|
||||
|
||||
// Cierra el fichero
|
||||
SDL_RWclose(file);
|
||||
else if (options->fullScreenMode == SDL_WINDOW_FULLSCREEN_DESKTOP)
|
||||
{
|
||||
file << "fullScreenMode=SDL_WINDOW_FULLSCREEN_DESKTOP\n";
|
||||
}
|
||||
|
||||
file << "windowSize=" + std::to_string(options->windowSize) + "\n";
|
||||
|
||||
if (options->filter == FILTER_NEAREST)
|
||||
{
|
||||
file << "filter=FILTER_NEAREST\n";
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("Error: Unable to save %s file! %s\n", filename.c_str(), SDL_GetError());
|
||||
success = false;
|
||||
file << "filter=FILTER_LINEAL\n";
|
||||
}
|
||||
|
||||
file << "vSync=" + boolToString(options->vSync) + "\n";
|
||||
file << "integerScale=" + boolToString(options->integerScale) + "\n";
|
||||
file << "keepAspect=" + boolToString(options->keepAspect) + "\n";
|
||||
file << "borderEnabled=" + boolToString(options->borderEnabled) + "\n";
|
||||
file << "borderSize=" + std::to_string(options->borderSize) + "\n";
|
||||
file << "screenWidth=" + std::to_string(options->screenWidth) + "\n";
|
||||
file << "screenHeight=" + std::to_string(options->screenHeight) + "\n";
|
||||
|
||||
file << "\n## OTHER OPTIONS\n";
|
||||
file << "language=" + std::to_string(options->language) + "\n";
|
||||
file << "difficulty=" + std::to_string(options->difficulty) + "\n";
|
||||
file << "input0=" + std::to_string(options->input.at(0).deviceType) + "\n";
|
||||
file << "input1=" + std::to_string(options->input.at(1).deviceType) + "\n";
|
||||
|
||||
file << "\n## ONLINE OPTIONS\n";
|
||||
file << "enabled=" + boolToString(options->online.enabled) + "\n";
|
||||
file << "server=" + options->online.server + "\n";
|
||||
file << "jailerID=" + options->online.jailerID + "\n";
|
||||
|
||||
// Cierra el fichero
|
||||
file.close();
|
||||
|
||||
return success;
|
||||
}
|
||||
|
||||
@@ -543,22 +546,6 @@ void Director::initOnline()
|
||||
}
|
||||
|
||||
// Obten el Jailer ID
|
||||
std::fstream f;
|
||||
f.open(asset->get("jailer_id.txt"), std::ios::in);
|
||||
if (f.is_open())
|
||||
{
|
||||
std::string str;
|
||||
if (getline(f, options->online.jailerID))
|
||||
{
|
||||
f.close();
|
||||
}
|
||||
}
|
||||
else
|
||||
{ // Escribe en el fichero
|
||||
f << "";
|
||||
}
|
||||
f.close();
|
||||
|
||||
if (options->online.jailerID == "")
|
||||
{ // Jailer ID no definido
|
||||
screen->showText("No ha especificado ningun Jailer ID");
|
||||
@@ -581,4 +568,133 @@ void Director::initOnline()
|
||||
options->online.score = points;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Asigna variables a partir de dos cadenas
|
||||
bool Director::setOptions(options_t *options, std::string var, std::string value)
|
||||
{
|
||||
// Indicador de éxito en la asignación
|
||||
bool success = true;
|
||||
|
||||
if (var == "fullScreenMode")
|
||||
{
|
||||
if (value == "SDL_WINDOW_FULLSCREEN_DESKTOP")
|
||||
{
|
||||
options->fullScreenMode = SDL_WINDOW_FULLSCREEN_DESKTOP;
|
||||
}
|
||||
else if (value == "SDL_WINDOW_FULLSCREEN")
|
||||
{
|
||||
options->fullScreenMode = SDL_WINDOW_FULLSCREEN;
|
||||
}
|
||||
else
|
||||
{
|
||||
options->fullScreenMode = 0;
|
||||
}
|
||||
}
|
||||
|
||||
else if (var == "windowSize")
|
||||
{
|
||||
options->windowSize = std::stoi(value);
|
||||
if ((options->windowSize < 1) || (options->windowSize > 4))
|
||||
{
|
||||
options->windowSize = 3;
|
||||
}
|
||||
}
|
||||
|
||||
else if (var == "filter")
|
||||
{
|
||||
if (value == "FILTER_LINEAL")
|
||||
{
|
||||
options->filter = FILTER_LINEAL;
|
||||
}
|
||||
else
|
||||
{
|
||||
options->filter = FILTER_NEAREST;
|
||||
}
|
||||
}
|
||||
|
||||
else if (var == "vSync")
|
||||
{
|
||||
options->vSync = stringToBool(value);
|
||||
}
|
||||
|
||||
else if (var == "integerScale")
|
||||
{
|
||||
options->integerScale = stringToBool(value);
|
||||
}
|
||||
|
||||
else if (var == "keepAspect")
|
||||
{
|
||||
options->keepAspect = stringToBool(value);
|
||||
}
|
||||
|
||||
else if (var == "borderEnabled")
|
||||
{
|
||||
options->borderEnabled = stringToBool(value);
|
||||
}
|
||||
|
||||
else if (var == "borderSize")
|
||||
{
|
||||
options->borderSize = std::stof(value);
|
||||
if (options->borderSize < 0.0f || options->borderSize > 0.5f)
|
||||
{
|
||||
options->borderSize = 0.1f;
|
||||
}
|
||||
}
|
||||
|
||||
else if (var == "screenWidth")
|
||||
{
|
||||
options->screenWidth = std::stoi(value);
|
||||
}
|
||||
|
||||
else if (var == "screenHeight")
|
||||
{
|
||||
options->screenHeight = std::stoi(value);
|
||||
}
|
||||
|
||||
else if (var == "language")
|
||||
{
|
||||
options->language = std::stoi(value);
|
||||
}
|
||||
|
||||
else if (var == "difficulty")
|
||||
{
|
||||
options->difficulty = std::stoi(value);
|
||||
}
|
||||
|
||||
else if (var == "input0")
|
||||
{
|
||||
options->input.at(0).deviceType = std::stoi(value);
|
||||
}
|
||||
|
||||
else if (var == "input1")
|
||||
{
|
||||
options->input.at(1).deviceType = std::stoi(value);
|
||||
}
|
||||
|
||||
else if (var == "enabled")
|
||||
{
|
||||
options->online.enabled = stringToBool(value);
|
||||
}
|
||||
|
||||
else if (var == "server")
|
||||
{
|
||||
options->online.server = value;
|
||||
}
|
||||
|
||||
else if (var == "jailerID")
|
||||
{
|
||||
options->online.jailerID = value;
|
||||
}
|
||||
|
||||
else if (var == "" || var.substr(0, 1) == "#")
|
||||
{
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
success = false;
|
||||
}
|
||||
|
||||
return success;
|
||||
}
|
||||
Reference in New Issue
Block a user