2013-09-01 14:27:21 +00:00
|
|
|
/*
|
|
|
|
** config.cpp
|
|
|
|
**
|
|
|
|
** This file is part of mkxp.
|
|
|
|
**
|
|
|
|
** Copyright (C) 2013 Jonas Kulla <Nyocurio@gmail.com>
|
|
|
|
**
|
|
|
|
** mkxp is free software: you can redistribute it and/or modify
|
|
|
|
** it under the terms of the GNU General Public License as published by
|
|
|
|
** the Free Software Foundation, either version 2 of the License, or
|
|
|
|
** (at your option) any later version.
|
|
|
|
**
|
|
|
|
** mkxp is distributed in the hope that it will be useful,
|
|
|
|
** but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
** GNU General Public License for more details.
|
|
|
|
**
|
|
|
|
** You should have received a copy of the GNU General Public License
|
|
|
|
** along with mkxp. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
2013-12-11 19:46:54 +00:00
|
|
|
#include <boost/program_options/options_description.hpp>
|
|
|
|
#include <boost/program_options/parsers.hpp>
|
|
|
|
#include <boost/program_options/variables_map.hpp>
|
2014-01-01 02:03:53 +00:00
|
|
|
|
2013-12-11 19:46:54 +00:00
|
|
|
#include <fstream>
|
2014-08-20 03:20:07 +00:00
|
|
|
#include <stdint.h>
|
2013-12-11 19:46:54 +00:00
|
|
|
|
|
|
|
#include "debugwriter.h"
|
|
|
|
#include "util.h"
|
|
|
|
|
2014-08-20 03:20:07 +00:00
|
|
|
#ifdef INI_ENCODING
|
|
|
|
extern "C" {
|
|
|
|
#include <libguess.h>
|
|
|
|
}
|
|
|
|
#include <iconv.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* http://stackoverflow.com/a/1031773 */
|
|
|
|
static bool validUtf8(const char *string)
|
|
|
|
{
|
|
|
|
const uint8_t *bytes = (uint8_t*) string;
|
|
|
|
|
|
|
|
while(*bytes)
|
|
|
|
{
|
|
|
|
if( (/* ASCII
|
|
|
|
* use bytes[0] <= 0x7F to allow ASCII control characters */
|
|
|
|
bytes[0] == 0x09 ||
|
|
|
|
bytes[0] == 0x0A ||
|
|
|
|
bytes[0] == 0x0D ||
|
|
|
|
(0x20 <= bytes[0] && bytes[0] <= 0x7E)
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
bytes += 1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( (/* non-overlong 2-byte */
|
|
|
|
(0xC2 <= bytes[0] && bytes[0] <= 0xDF) &&
|
|
|
|
(0x80 <= bytes[1] && bytes[1] <= 0xBF)
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
bytes += 2;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( (/* excluding overlongs */
|
|
|
|
bytes[0] == 0xE0 &&
|
|
|
|
(0xA0 <= bytes[1] && bytes[1] <= 0xBF) &&
|
|
|
|
(0x80 <= bytes[2] && bytes[2] <= 0xBF)
|
|
|
|
) ||
|
|
|
|
(/* straight 3-byte */
|
|
|
|
((0xE1 <= bytes[0] && bytes[0] <= 0xEC) ||
|
|
|
|
bytes[0] == 0xEE ||
|
|
|
|
bytes[0] == 0xEF) &&
|
|
|
|
(0x80 <= bytes[1] && bytes[1] <= 0xBF) &&
|
|
|
|
(0x80 <= bytes[2] && bytes[2] <= 0xBF)
|
|
|
|
) ||
|
|
|
|
(/* excluding surrogates */
|
|
|
|
bytes[0] == 0xED &&
|
|
|
|
(0x80 <= bytes[1] && bytes[1] <= 0x9F) &&
|
|
|
|
(0x80 <= bytes[2] && bytes[2] <= 0xBF)
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
bytes += 3;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( (/* planes 1-3 */
|
|
|
|
bytes[0] == 0xF0 &&
|
|
|
|
(0x90 <= bytes[1] && bytes[1] <= 0xBF) &&
|
|
|
|
(0x80 <= bytes[2] && bytes[2] <= 0xBF) &&
|
|
|
|
(0x80 <= bytes[3] && bytes[3] <= 0xBF)
|
|
|
|
) ||
|
|
|
|
(/* planes 4-15 */
|
|
|
|
(0xF1 <= bytes[0] && bytes[0] <= 0xF3) &&
|
|
|
|
(0x80 <= bytes[1] && bytes[1] <= 0xBF) &&
|
|
|
|
(0x80 <= bytes[2] && bytes[2] <= 0xBF) &&
|
|
|
|
(0x80 <= bytes[3] && bytes[3] <= 0xBF)
|
|
|
|
) ||
|
|
|
|
(/* plane 16 */
|
|
|
|
bytes[0] == 0xF4 &&
|
|
|
|
(0x80 <= bytes[1] && bytes[1] <= 0x8F) &&
|
|
|
|
(0x80 <= bytes[2] && bytes[2] <= 0xBF) &&
|
|
|
|
(0x80 <= bytes[3] && bytes[3] <= 0xBF)
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
bytes += 4;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-12-11 19:46:54 +00:00
|
|
|
typedef std::vector<std::string> StringVec;
|
|
|
|
namespace po = boost::program_options;
|
2013-09-01 14:27:21 +00:00
|
|
|
|
|
|
|
Config::Config()
|
2014-08-28 21:11:10 +00:00
|
|
|
: rgssVersion(0),
|
|
|
|
debugMode(false),
|
2013-09-01 14:27:21 +00:00
|
|
|
winResizable(false),
|
|
|
|
fullscreen(false),
|
2013-10-09 12:17:21 +00:00
|
|
|
fixedAspectRatio(true),
|
2013-09-23 09:00:50 +00:00
|
|
|
smoothScaling(false),
|
2013-09-01 14:27:21 +00:00
|
|
|
vsync(false),
|
2014-08-28 21:11:10 +00:00
|
|
|
defScreenW(0),
|
|
|
|
defScreenH(0),
|
2013-10-17 00:18:16 +00:00
|
|
|
fixedFramerate(0),
|
2013-12-21 20:18:20 +00:00
|
|
|
frameSkip(true),
|
2013-09-01 14:27:21 +00:00
|
|
|
solidFonts(false),
|
2013-10-20 20:38:46 +00:00
|
|
|
gameFolder("."),
|
2014-01-01 20:56:38 +00:00
|
|
|
anyAltToggleFS(false),
|
2014-01-01 06:07:18 +00:00
|
|
|
allowSymlinks(false),
|
2014-04-11 16:11:32 +00:00
|
|
|
pathCache(true),
|
|
|
|
useScriptNames(false)
|
2014-07-31 01:32:07 +00:00
|
|
|
{
|
|
|
|
midi.chorus = false;
|
|
|
|
midi.reverb = false;
|
2014-08-25 03:28:46 +00:00
|
|
|
SE.sourceCount = 6;
|
2014-07-31 01:32:07 +00:00
|
|
|
}
|
2013-09-01 14:27:21 +00:00
|
|
|
|
2014-01-10 19:16:34 +00:00
|
|
|
void Config::read(int argc, char *argv[])
|
2013-09-01 14:27:21 +00:00
|
|
|
{
|
2013-12-11 19:46:54 +00:00
|
|
|
#define PO_DESC_ALL \
|
2014-08-28 21:11:10 +00:00
|
|
|
PO_DESC(rgssVersion, int) \
|
2013-12-11 19:46:54 +00:00
|
|
|
PO_DESC(debugMode, bool) \
|
2014-01-01 06:07:18 +00:00
|
|
|
PO_DESC(winResizable, bool) \
|
|
|
|
PO_DESC(fullscreen, bool) \
|
|
|
|
PO_DESC(fixedAspectRatio, bool) \
|
|
|
|
PO_DESC(smoothScaling, bool) \
|
|
|
|
PO_DESC(vsync, bool) \
|
|
|
|
PO_DESC(defScreenW, int) \
|
|
|
|
PO_DESC(defScreenH, int) \
|
|
|
|
PO_DESC(fixedFramerate, int) \
|
|
|
|
PO_DESC(frameSkip, bool) \
|
|
|
|
PO_DESC(solidFonts, bool) \
|
|
|
|
PO_DESC(gameFolder, std::string) \
|
2014-01-01 23:11:57 +00:00
|
|
|
PO_DESC(anyAltToggleFS, bool) \
|
2014-01-01 06:07:18 +00:00
|
|
|
PO_DESC(allowSymlinks, bool) \
|
2014-01-01 23:11:57 +00:00
|
|
|
PO_DESC(iconPath, std::string) \
|
2014-08-20 03:20:07 +00:00
|
|
|
PO_DESC(titleLanguage, std::string) \
|
2014-07-31 01:32:07 +00:00
|
|
|
PO_DESC(midi.soundFont, std::string) \
|
|
|
|
PO_DESC(midi.chorus, bool) \
|
|
|
|
PO_DESC(midi.reverb, bool) \
|
2014-08-25 03:28:46 +00:00
|
|
|
PO_DESC(SE.sourceCount, int) \
|
2014-01-01 06:07:18 +00:00
|
|
|
PO_DESC(customScript, std::string) \
|
2014-04-11 16:11:32 +00:00
|
|
|
PO_DESC(pathCache, bool) \
|
|
|
|
PO_DESC(useScriptNames, bool)
|
2013-09-03 09:07:56 +00:00
|
|
|
|
2014-01-01 02:03:53 +00:00
|
|
|
// Not gonna take your shit boost
|
|
|
|
#define GUARD_ALL( exp ) try { exp } catch(...) {}
|
|
|
|
|
2013-12-11 19:46:54 +00:00
|
|
|
#define PO_DESC(key, type) (#key, po::value< type >()->default_value(key))
|
2013-09-03 09:07:56 +00:00
|
|
|
|
2013-12-11 19:46:54 +00:00
|
|
|
po::options_description podesc;
|
|
|
|
podesc.add_options()
|
|
|
|
PO_DESC_ALL
|
2014-08-24 05:27:36 +00:00
|
|
|
("preloadScript", po::value<StringVec>()->composing())
|
2014-01-10 19:16:34 +00:00
|
|
|
("RTP", po::value<StringVec>()->composing())
|
Font: Overhaul font asset discovery
Previously, any font names requested by RGSS would be translated
directly to filenames by lowercasing and replacing spaces with
underscores (and finally doing some extension substitution).
To make this whole thing work smoother as well as get closer to
how font discovery is done in VX, we now scan the "Fonts/" folder
at startup and index all present font assets by their family name;
now, if an "Open Sans" font is present in "Fonts/", it will be
used regardless of filename.
Font assets with "Regular" style are preferred, but in their
absence, mkxp will make use of any other style it can find for
the respective family. This is not the exact same behavior as
VX, but it should cover 95% of use cases.
Previously, one could substitute fonts via filenames, ie. to
substitute "Arial" with "Open Sans", one would just rename
"OpenSans.ttf" to "arial.ttf" and put it in "Fonts/". With the
above change, this is no longer possible. As an alternative, one
can now explicitly specify font family substitutions via mkxp.conf;
eg. for the above case, one would add
fontSub=Arial>Open Sans
to the configuration file. Multiple such rules can be specified.
In the process, I also added the ability to provide
'Font.(default_)name' with an array of font families to search
for the first existing one instead of a plain string.
This makes the behavior closer to RMXP; however, it doesn't
work 100% the same: when a reference to the 'Font.name' array is
held and additional strings are added to it without re-assignig
the array to 'Font.name', those will be ignored.
2014-04-11 11:37:14 +00:00
|
|
|
("fontSub", po::value<StringVec>()->composing())
|
2014-07-24 21:11:12 +00:00
|
|
|
("rubyLoadpath", po::value<StringVec>()->composing())
|
2013-12-11 19:46:54 +00:00
|
|
|
;
|
|
|
|
|
2014-01-10 19:16:34 +00:00
|
|
|
po::variables_map vm;
|
|
|
|
|
|
|
|
/* Parse command line options */
|
|
|
|
po::parsed_options cmdPo =
|
|
|
|
po::command_line_parser(argc, argv).options(podesc)
|
|
|
|
.allow_unregistered()
|
|
|
|
.run();
|
|
|
|
|
|
|
|
GUARD_ALL( po::store(cmdPo, vm); )
|
|
|
|
|
|
|
|
/* Parse configuration file (mkxp.conf) */
|
2013-12-11 19:46:54 +00:00
|
|
|
std::ifstream confFile;
|
|
|
|
confFile.open("mkxp.conf");
|
|
|
|
|
2014-01-01 16:01:05 +00:00
|
|
|
if (confFile)
|
|
|
|
{
|
2014-01-01 02:03:53 +00:00
|
|
|
GUARD_ALL( po::store(po::parse_config_file(confFile, podesc, true), vm); )
|
|
|
|
}
|
2013-12-11 19:46:54 +00:00
|
|
|
|
|
|
|
confFile.close();
|
|
|
|
|
2014-01-10 19:16:34 +00:00
|
|
|
po::notify(vm);
|
2013-12-11 19:46:54 +00:00
|
|
|
|
|
|
|
#undef PO_DESC
|
|
|
|
#define PO_DESC(key, type) GUARD_ALL( key = vm[#key].as< type >(); )
|
|
|
|
|
|
|
|
PO_DESC_ALL;
|
2013-09-03 09:07:56 +00:00
|
|
|
|
2014-08-24 05:27:36 +00:00
|
|
|
GUARD_ALL( preloadScripts = vm["preloadScript"].as<StringVec>(); );
|
|
|
|
|
2013-12-11 19:46:54 +00:00
|
|
|
GUARD_ALL( rtps = vm["RTP"].as<StringVec>(); );
|
|
|
|
|
Font: Overhaul font asset discovery
Previously, any font names requested by RGSS would be translated
directly to filenames by lowercasing and replacing spaces with
underscores (and finally doing some extension substitution).
To make this whole thing work smoother as well as get closer to
how font discovery is done in VX, we now scan the "Fonts/" folder
at startup and index all present font assets by their family name;
now, if an "Open Sans" font is present in "Fonts/", it will be
used regardless of filename.
Font assets with "Regular" style are preferred, but in their
absence, mkxp will make use of any other style it can find for
the respective family. This is not the exact same behavior as
VX, but it should cover 95% of use cases.
Previously, one could substitute fonts via filenames, ie. to
substitute "Arial" with "Open Sans", one would just rename
"OpenSans.ttf" to "arial.ttf" and put it in "Fonts/". With the
above change, this is no longer possible. As an alternative, one
can now explicitly specify font family substitutions via mkxp.conf;
eg. for the above case, one would add
fontSub=Arial>Open Sans
to the configuration file. Multiple such rules can be specified.
In the process, I also added the ability to provide
'Font.(default_)name' with an array of font families to search
for the first existing one instead of a plain string.
This makes the behavior closer to RMXP; however, it doesn't
work 100% the same: when a reference to the 'Font.name' array is
held and additional strings are added to it without re-assignig
the array to 'Font.name', those will be ignored.
2014-04-11 11:37:14 +00:00
|
|
|
GUARD_ALL( fontSubs = vm["fontSub"].as<StringVec>(); );
|
|
|
|
|
2014-08-28 21:11:10 +00:00
|
|
|
GUARD_ALL( rubyLoadpaths = vm["rubyLoadpath"].as<StringVec>(); );
|
2014-07-24 21:11:12 +00:00
|
|
|
|
2013-12-11 19:46:54 +00:00
|
|
|
#undef PO_DESC
|
|
|
|
#undef PO_DESC_ALL
|
2014-08-25 03:28:46 +00:00
|
|
|
|
2014-08-29 07:42:13 +00:00
|
|
|
rgssVersion = clamp(rgssVersion, 0, 3);
|
|
|
|
|
2014-08-25 03:28:46 +00:00
|
|
|
SE.sourceCount = clamp(SE.sourceCount, 1, 64);
|
2013-09-01 14:27:21 +00:00
|
|
|
}
|
|
|
|
|
2014-01-05 09:31:15 +00:00
|
|
|
static std::string baseName(const std::string &path)
|
|
|
|
{
|
2014-01-06 17:45:01 +00:00
|
|
|
size_t pos = path.find_last_of("/\\");
|
2014-01-05 09:31:15 +00:00
|
|
|
|
|
|
|
if (pos == path.npos)
|
2014-01-06 17:45:01 +00:00
|
|
|
return path;
|
2014-01-05 09:31:15 +00:00
|
|
|
|
2014-01-06 17:45:01 +00:00
|
|
|
return path.substr(pos + 1);
|
2014-01-05 09:31:15 +00:00
|
|
|
}
|
|
|
|
|
2013-09-01 14:27:21 +00:00
|
|
|
void Config::readGameINI()
|
|
|
|
{
|
2013-12-11 19:46:54 +00:00
|
|
|
if (!customScript.empty())
|
2013-09-01 14:27:21 +00:00
|
|
|
{
|
2014-01-05 09:31:15 +00:00
|
|
|
game.title = baseName(customScript);
|
2013-09-01 14:27:21 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-12-11 19:46:54 +00:00
|
|
|
po::options_description podesc;
|
|
|
|
podesc.add_options()
|
|
|
|
("Game.Title", po::value<std::string>())
|
|
|
|
("Game.Scripts", po::value<std::string>())
|
|
|
|
;
|
2013-09-01 14:27:21 +00:00
|
|
|
|
2013-12-11 19:46:54 +00:00
|
|
|
std::string iniPath = gameFolder + "/Game.ini";
|
|
|
|
|
|
|
|
std::ifstream iniFile;
|
|
|
|
iniFile.open((iniPath).c_str());
|
|
|
|
|
|
|
|
po::variables_map vm;
|
2014-01-01 02:03:53 +00:00
|
|
|
GUARD_ALL( po::store(po::parse_config_file(iniFile, podesc, true), vm); )
|
2013-12-11 19:46:54 +00:00
|
|
|
po::notify(vm);
|
|
|
|
|
|
|
|
iniFile.close();
|
|
|
|
|
|
|
|
GUARD_ALL( game.title = vm["Game.Title"].as<std::string>(); );
|
|
|
|
GUARD_ALL( game.scripts = vm["Game.Scripts"].as<std::string>(); );
|
|
|
|
|
|
|
|
strReplace(game.scripts, '\\', '/');
|
|
|
|
|
2014-08-20 03:20:07 +00:00
|
|
|
#ifdef INI_ENCODING
|
|
|
|
/* Can add more later */
|
|
|
|
const char *languages[] =
|
|
|
|
{
|
|
|
|
titleLanguage.c_str(),
|
|
|
|
GUESS_REGION_JP, /* Japanese */
|
|
|
|
GUESS_REGION_KR, /* Korean */
|
|
|
|
GUESS_REGION_CN, /* Chinese */
|
|
|
|
0
|
|
|
|
};
|
|
|
|
|
|
|
|
bool convSuccess = true;
|
|
|
|
|
|
|
|
/* Verify that the game title is UTF-8, and if not,
|
|
|
|
* try to determine the encoding and convert to UTF-8 */
|
|
|
|
if (!validUtf8(game.title.c_str()))
|
|
|
|
{
|
|
|
|
const char *encoding = 0;
|
|
|
|
convSuccess = false;
|
|
|
|
|
|
|
|
for (size_t i = 0; languages[i]; ++i)
|
|
|
|
{
|
|
|
|
encoding = libguess_determine_encoding(game.title.c_str(),
|
|
|
|
game.title.size(),
|
|
|
|
languages[i]);
|
|
|
|
if (encoding)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (encoding)
|
|
|
|
{
|
|
|
|
iconv_t cd = iconv_open("UTF-8", encoding);
|
|
|
|
|
|
|
|
size_t inLen = game.title.size();
|
|
|
|
size_t outLen = inLen * 4;
|
|
|
|
std::string buf(outLen, '\0');
|
|
|
|
char *inPtr = const_cast<char*>(game.title.c_str());
|
|
|
|
char *outPtr = const_cast<char*>(buf.c_str());
|
|
|
|
|
|
|
|
errno = 0;
|
|
|
|
size_t result = iconv(cd, &inPtr, &inLen, &outPtr, &outLen);
|
|
|
|
|
|
|
|
iconv_close(cd);
|
|
|
|
|
|
|
|
if (result != (size_t) -1 && errno == 0)
|
|
|
|
{
|
|
|
|
buf.resize(buf.size()-outLen);
|
|
|
|
game.title = buf;
|
|
|
|
convSuccess = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!convSuccess)
|
|
|
|
game.title.clear();
|
|
|
|
#else
|
|
|
|
if (!validUtf8(game.title.c_str()))
|
|
|
|
game.title.clear();
|
|
|
|
#endif
|
|
|
|
|
2013-12-11 19:46:54 +00:00
|
|
|
if (game.title.empty())
|
2014-01-05 09:31:15 +00:00
|
|
|
game.title = baseName(gameFolder);
|
2014-08-28 21:11:10 +00:00
|
|
|
|
|
|
|
if (rgssVersion == 0)
|
|
|
|
{
|
|
|
|
/* Try to guess RGSS version based on Data/Scripts extension */
|
|
|
|
rgssVersion = 1;
|
|
|
|
|
|
|
|
if (!game.scripts.empty())
|
|
|
|
{
|
|
|
|
const char *p = &game.scripts[game.scripts.size()];
|
|
|
|
const char *head = &game.scripts[0];
|
|
|
|
|
|
|
|
while (--p != head)
|
|
|
|
if (*p == '.')
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (!strcmp(p, ".rvdata"))
|
|
|
|
rgssVersion = 2;
|
|
|
|
else if (!strcmp(p, ".rvdata2"))
|
|
|
|
rgssVersion = 3;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (defScreenW <= 0)
|
|
|
|
defScreenW = (rgssVersion == 1 ? 640 : 544);
|
|
|
|
|
|
|
|
if (defScreenH <= 0)
|
|
|
|
defScreenH = (rgssVersion == 1 ? 480 : 416);
|
2013-09-01 14:27:21 +00:00
|
|
|
}
|