Skip to content

Commit

Permalink
Fix mia-games when updating ROM databases.
Browse files Browse the repository at this point in the history
  • Loading branch information
dillof committed Jun 26, 2024
1 parent de9bb6b commit 31eabb5
Show file tree
Hide file tree
Showing 10 changed files with 81 additions and 64 deletions.
6 changes: 4 additions & 2 deletions src/DatRepository.cc
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,8 @@ void DatRepository::update_directory(const std::string &directory, const DatDBPt
auto output = OutputContextHeader();

auto source = std::make_shared<ParserSourceZip>(filepath, zip_archive, entry_name);
auto parser = Parser::create(source, {}, nullptr, &output, {});
auto parser_options = Parser::Options{{}, false};
auto parser = Parser::create(source, {}, nullptr, &output, parser_options);
if (parser) {
if (parser->parse_header()) {
auto header = output.get_header();
Expand All @@ -176,7 +177,8 @@ void DatRepository::update_directory(const std::string &directory, const DatDBPt
auto output = OutputContextHeader();

auto source = std::make_shared<ParserSourceFile>(filepath);
auto parser = Parser::create(source, {}, nullptr, &output, {});
auto parser_options = Parser::Options{{}, false};
auto parser = Parser::create(source, {}, nullptr, &output, parser_options);

if (parser) {
if (parser->parse_header() && output.close()) {
Expand Down
17 changes: 11 additions & 6 deletions src/MkMameDB.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@

#include "Command.h"
#include "OutputContext.h"
#include "Parser.h"

class MkMameDB : public Command {
public:
Expand All @@ -46,19 +47,23 @@ class MkMameDB : public Command {
bool global_cleanup() override;

private:
bool process_file(const std::string &fname, OutputContext *out);
bool process_stdin(OutputContext *out);

Parser::Options parser_options{{}};
std::string dbname, dbname_real;
std::unordered_set<std::string> exclude;
std::vector<std::string> file_patterns;
std::unordered_set<std::string> skip_files;
DatEntry dat;
int flags;
OutputContext::Format fmt;
int flags{0};
OutputContext::Format fmt{OutputContext::FORMAT_DB};
std::string detector_name;
bool force;
bool runtest;
bool force{false};
bool runtest{false};

bool list_available_dats;
bool list_dats;
bool list_available_dats{false};
bool list_dats{false};
};

#endif // MKMAMEDB_H
41 changes: 30 additions & 11 deletions src/Parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ std::string Parser::state_name(parser_state_t state) {
}

ParserPtr Parser::create(const ParserSourcePtr& source, const std::unordered_set<std::string>& exclude,
const DatEntry* dat, OutputContext* output_context, Options options) {
const DatEntry* dat, OutputContext* output_context, const Options& options) {
size_t length = 0;
for (const auto& pair : format_start) {
length = std::max(pair.first.length(), length);
Expand All @@ -112,19 +112,19 @@ ParserPtr Parser::create(const ParserSourcePtr& source, const std::unordered_set
return {};

case CLRMAMEPRO:
return std::shared_ptr<Parser>(new ParserCm(source, exclude, dat, output_context, std::move(options)));
return std::shared_ptr<Parser>(new ParserCm(source, exclude, dat, output_context, options));
case ROMCENTER:
return std::shared_ptr<Parser>(new ParserRc(source, exclude, dat, output_context, std::move(options)));
return std::shared_ptr<Parser>(new ParserRc(source, exclude, dat, output_context, options));
case XML:
return std::shared_ptr<Parser>(new ParserXml(source, exclude, dat, output_context, std::move(options)));
return std::shared_ptr<Parser>(new ParserXml(source, exclude, dat, output_context, options));
}

return {};
}

bool Parser::parse(const ParserSourcePtr& source, const std::unordered_set<std::string>& exclude, const DatEntry* dat,
OutputContext* out, Options options) {
auto parser = create(source, exclude, dat, out, std::move(options));
OutputContext* out, const Options& options) {
auto parser = create(source, exclude, dat, out, options);

if (!parser) {
output.file_error("unknown dat format");
Expand Down Expand Up @@ -516,15 +516,16 @@ bool Parser::prog_version(const std::string& attr) {


Parser::Parser(ParserSourcePtr source, std::unordered_set<std::string> exclude, const DatEntry* dat,
OutputContext* output_context_, Options options)
: options(std::move(options)),
OutputContext* output_context_, const Options& options)
: options(options),
lineno(0),
header_only(false),
ignore(std::move(exclude)),
output_context(output_context_),
ps(std::move(source)),
flags(0),
header_set(false),
error(false),
state(PARSE_IN_HEADER) {
dat_default.merge(dat, nullptr);
for (auto& i : r) {
Expand Down Expand Up @@ -610,15 +611,15 @@ void Parser::rom_end(filetype_t ft) {
}
else {
std::string name;
size_t n = 1;
size_t i = 1;
while (true) {
auto path = std::filesystem::path(r[ft]->name);
name = path.stem().string() + " (" + std::to_string(n) + ")" + path.extension().string();
name = path.stem().string() + " (" + std::to_string(i) + ")" + path.extension().string();
if (std::none_of(g->files[ft].begin(), g->files[ft].end(),
[&name](const Rom& rom) { return name == rom.name; })) {
break;
}
n += 1;
i += 1;
}
output.line_error(lineno, "warning: two different ROMs with same name '%s', renamed to '%s'",
r[ft]->name.c_str(), name.c_str());
Expand All @@ -642,3 +643,21 @@ void Parser::rom_end(filetype_t ft) {
}
}
}

Parser::Options::Options(std::optional<std::string> dat_name, bool use_configuration) {
if (!use_configuration) {
return;
}

if (dat_name) {
game_name_suffix = configuration.dat_game_name_suffix(*dat_name);
use_description_as_name = configuration.dat_use_description_as_name(*dat_name);
}
else {
use_description_as_name = configuration.use_description_as_name;
}
if (!configuration.mia_games.empty()) {
auto list = slurp_lines(configuration.mia_games);
mia_games.insert(list.begin(), list.end());
}
}
14 changes: 7 additions & 7 deletions src/Parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,21 +60,21 @@ class Parser {
public:
class Options {
public:
Options() : full_archive_names(false), use_description_as_name(false) {}
Options(std::optional<std::string> dat_name, bool use_configuration = true);

bool full_archive_names;
bool full_archive_names = false;
std::string game_name_suffix;
bool use_description_as_name;
bool use_description_as_name = false;
std::unordered_set<std::string> mia_games;
};

static ParserPtr create(const ParserSourcePtr& source, const std::unordered_set<std::string>& exclude,
const DatEntry* dat, OutputContext* output, Options options);
const DatEntry* dat, OutputContext* output, const Options& options);

static bool parse(const ParserSourcePtr& source, const std::unordered_set<std::string>& exclude,
const DatEntry* dat, OutputContext* output, Options options);
const DatEntry* dat, OutputContext* output, const Options& options);

Options options;
const Options& options;

/* TODO: move out of context */
size_t lineno; /* current line number in input file */
Expand Down Expand Up @@ -112,7 +112,7 @@ class Parser {
bool prog_version(const std::string& attr);

Parser(ParserSourcePtr source, std::unordered_set<std::string> exclude, const DatEntry* dat, OutputContext* output_,
Options options);
const Options& options);
virtual ~Parser() = default;

protected:
Expand Down
7 changes: 4 additions & 3 deletions src/ParserCm.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,22 +37,23 @@
#include "Parser.h"

#include <unordered_set>
#include <utility>

class ParserCm : public Parser {
public:
bool parse() override;

ParserCm(ParserSourcePtr source, const std::unordered_set<std::string>& exclude, const DatEntry* dat,
OutputContext* output, Options options)
: Parser(source, exclude, dat, output, std::move(options)), ignoring_line(false) {}
OutputContext* output, const Options& options)
: Parser(std::move(source), exclude, dat, output, options), ignoring_line(false) {}
~ParserCm() override = default;

private:
enum State { TOP, GAME, PROG };

class Tokenizer {
public:
explicit Tokenizer(const std::string& s) : string(s), position(0) {}
explicit Tokenizer(std::string s) : string(std::move(s)), position(0) {}

std::string get();

Expand Down
4 changes: 3 additions & 1 deletion src/ParserDir.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,14 @@
IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include <utility>

#include "Archive.h"
#include "Parser.h"

class ParserDir : public Parser {
public:
ParserDir(ParserSourcePtr source, const std::unordered_set<std::string> &exclude, const DatEntry *dat, OutputContext *output, Options options, const std::string &dname, int hashtypes_, bool runtest_ = false) : Parser(source, exclude, dat, output, std::move(options)), directory_name(dname), hashtypes(hashtypes_), runtest(runtest_) { }
ParserDir(ParserSourcePtr source, const std::unordered_set<std::string> &exclude, const DatEntry *dat, OutputContext *output, const Options& options, std::string dname, int hashtypes_, bool runtest_ = false) : Parser(std::move(source), exclude, dat, output, options), directory_name(std::move(dname)), hashtypes(hashtypes_), runtest(runtest_) { }
~ParserDir() override = default;

bool parse() override;
Expand Down
2 changes: 1 addition & 1 deletion src/ParserRc.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@

class ParserRc : public Parser {
public:
ParserRc(ParserSourcePtr source, const std::unordered_set<std::string> &exclude, const DatEntry *dat, OutputContext *output, Options options) : Parser(std::move(source), exclude, dat, output, std::move(options)) {}
ParserRc(ParserSourcePtr source, const std::unordered_set<std::string> &exclude, const DatEntry *dat, OutputContext *output, const Options& options) : Parser(std::move(source), exclude, dat, output, options) {}
~ParserRc() override = default;

bool parse() override;
Expand Down
2 changes: 1 addition & 1 deletion src/ParserXml.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@

class ParserXml : public Parser {
public:
ParserXml(ParserSourcePtr source, const std::unordered_set<std::string> &exclude, const DatEntry *dat, OutputContext *output, Options options) : Parser(std::move(source), exclude, dat, output, std::move(options)) { }
ParserXml(ParserSourcePtr source, const std::unordered_set<std::string> &exclude, const DatEntry *dat, OutputContext *output, const Options& options) : Parser(std::move(source), exclude, dat, output, options) { }
~ParserXml() override = default;

bool parse() override;
Expand Down
Loading

0 comments on commit 31eabb5

Please sign in to comment.