WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

Commit 3b5df29

Browse files
committed
Don’t check file types that are not in the RomDB.
1 parent 0f32a5d commit 3b5df29

File tree

8 files changed

+91
-36
lines changed

8 files changed

+91
-36
lines changed

src/Fixdat.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ void Fixdat::write(const Game *game, const Result *result) {
6868

6969
auto empty = true;
7070

71-
for (size_t ft = 0; ft < TYPE_MAX; ft++) {
71+
for (auto ft: db->filetypes()) {
7272
for (size_t i = 0; i < game->files[ft].size(); i++) {
7373
auto &match = result->game_files[ft][i];
7474
auto &rom = game->files[ft][i];

src/RomDB.cc

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ std::unordered_map<int, std::string> RomDB::queries = {
133133
{ QUERY_FILE, "select name, merge, status, location, size, crc, md5, sha1, sha256, missing from file where game_id = :game_id and file_type = :file_type order by file_idx" },
134134
{ QUERY_GAME_ID, "select game_id from game where name = :name" },
135135
{ QUERY_GAME, "select game_id, description, dat_idx, parent from game where name = :name" },
136-
{ QUERY_HAS_DISKS, "select file_idx from file where file_type = 1 limit 1" },
136+
{ QUERY_HAS_FILE_TYPE, "select file_idx from file where file_type = :file_type limit 1" },
137137
{ QUERY_HASH_TYPE_CRC, "select name from file where file_type = :file_type and crc not null limit 1" },
138138
{ QUERY_HASH_TYPE_MD5, "select name from file where file_type = :file_type and md5 not null limit 1" },
139139
{ QUERY_HASH_TYPE_SHA1, "select name from file where file_type = :file_type and sha1 not null limit 1" },
@@ -216,9 +216,9 @@ Stats RomDB::get_stats() {
216216
}
217217

218218

219-
bool RomDB::has_disks() {
220-
auto stmt = get_statement(QUERY_HAS_DISKS);
221-
219+
bool RomDB::get_has_type(int type) {
220+
auto stmt = get_statement(QUERY_HAS_FILE_TYPE);
221+
stmt->set_int("file_type", type);
222222
return stmt->step();
223223
}
224224

@@ -227,9 +227,35 @@ int RomDB::hashtypes(filetype_t type) {
227227
if (hashtypes_[type] == -1) {
228228
read_hashtypes(type);
229229
}
230-
230+
231231
return hashtypes_[type];
232232
}
233+
RomDB::FileTypes RomDB::filetypes() {
234+
for (int i = 0; i < TYPE_MAX; i++) {
235+
(void)has_type(static_cast<filetype_t>(i));
236+
}
237+
return {*this};
238+
}
239+
240+
RomDB::FileTypeIterator::FileTypeIterator(const bool* types, int file_type) : types{types}, file_type{file_type} {
241+
skip_missing();
242+
}
243+
244+
245+
RomDB::FileTypeIterator& RomDB::FileTypeIterator::operator++() {
246+
if (file_type < TYPE_MAX) {
247+
file_type++;
248+
}
249+
skip_missing();
250+
return *this;
251+
}
252+
253+
void RomDB::FileTypeIterator::skip_missing() {
254+
while (file_type < TYPE_MAX && !types[file_type]) {
255+
file_type++;
256+
}
257+
}
258+
233259

234260

235261
RomDB::RomDB(const std::string &name, int mode) : DB(format, name, mode) {
@@ -243,6 +269,10 @@ RomDB::RomDB(const std::string &name, int mode) : DB(format, name, mode) {
243269
auto detector_id = Detector::get_id(DetectorDescriptor(stmt->get_string("name"), stmt->get_string("version")));
244270
detectors[detector_id] = read_detector();
245271
}
272+
273+
for (size_t i = 0; i < TYPE_MAX; i++) {
274+
has_types[i] = get_has_type(i);
275+
}
246276
}
247277

248278

src/RomDB.h

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ class RomDB : public DB {
5959
QUERY_FILE,
6060
QUERY_GAME_ID,
6161
QUERY_GAME,
62-
QUERY_HAS_DISKS,
62+
QUERY_HAS_FILE_TYPE,
6363
QUERY_HASH_TYPE_CRC,
6464
QUERY_HASH_TYPE_MD5,
6565
QUERY_HASH_TYPE_SHA1,
@@ -80,6 +80,36 @@ class RomDB : public DB {
8080
enum ParameterizedStatement {
8181
QUERY_FILE_FBH
8282
};
83+
84+
class FileTypeIterator {
85+
public:
86+
using iterator_category = std::forward_iterator_tag;
87+
using value_type = filetype_t;
88+
89+
FileTypeIterator(const bool* types, int file_type);
90+
91+
FileTypeIterator& operator++();
92+
FileTypeIterator operator++(int) {auto next=*this; ++*this; return next;}
93+
filetype_t operator*() const {return static_cast<filetype_t>(file_type);}
94+
bool operator==(const FileTypeIterator& other) const {return file_type == other.file_type;}
95+
bool operator!=(const FileTypeIterator& other) const {return file_type != other.file_type;}
96+
97+
private:
98+
void skip_missing();
99+
const bool* types;
100+
int file_type;
101+
};
102+
103+
class FileTypes {
104+
public:
105+
FileTypes(const RomDB& db): types{db.has_types} {}
106+
107+
FileTypeIterator begin() {return {types, 0};}
108+
FileTypeIterator end() {return {types, TYPE_MAX};}
109+
110+
private:
111+
const bool *types;
112+
};
83113

84114
RomDB(const std::string &name, int mode);
85115
~RomDB() override = default;
@@ -96,7 +126,8 @@ class RomDB : public DB {
96126
std::vector<std::string> get_clones(const std::string &game_name);
97127
void delete_game(const Game *game) { delete_game(game->name); }
98128
void delete_game(const std::string &name);
99-
bool has_disks();
129+
bool has_type(filetype_t type) const {return has_types[type];}
130+
bool has_disks() const {return has_type(TYPE_DISK);}
100131

101132
bool has_detector() const { return !detectors.empty(); }
102133
DetectorPtr get_detector(size_t id);
@@ -107,6 +138,7 @@ class RomDB : public DB {
107138
std::vector<RomLocation> read_file_by_hash(filetype_t ft, const Hashes &hashes);
108139
GamePtr read_game(const std::string &name);
109140
int hashtypes(filetype_t);
141+
FileTypes filetypes();
110142
std::vector<std::string> read_list(enum dbh_list type);
111143
void update_file_location(Game *game);
112144
void update_game_parent(const Game *game);
@@ -115,13 +147,15 @@ class RomDB : public DB {
115147
void write_game(Game *game);
116148
void write_hashtypes(int, int);
117149
int export_db(const std::unordered_set<std::string> &exclude, const DatEntry *dat, OutputContext *out);
118-
150+
151+
bool has_types[TYPE_MAX];
152+
119153
protected:
120154
std::string get_query(int name, bool parameterized) const override;
121155

122156
private:
123157
int hashtypes_[TYPE_MAX];
124-
158+
125159
static const std::string init2_sql;
126160
static const Statement query_hash_type[];
127161
static std::unordered_map<int, std::string> queries;
@@ -130,6 +164,7 @@ class RomDB : public DB {
130164
DBStatement *get_statement(Statement name) { return get_statement_internal(name); }
131165
DBStatement *get_statement(ParameterizedStatement name, const Hashes &hashes, bool have_size) { return get_statement_internal(name, hashes, have_size); }
132166

167+
bool get_has_type(int type);
133168
DetectorPtr read_detector();
134169
void read_files(Game *game, filetype_t ft);
135170
void read_hashtypes(filetype_t type);

src/Tree.cc

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,11 @@ Tree check_tree;
5050

5151
bool Tree::add(const std::string &game_name) {
5252
GamePtr game = db->read_game(game_name);
53-
53+
5454
if (!game) {
5555
return false;
5656
}
57-
57+
5858
auto tree = this;
5959

6060
if (!game->cloneof[1].empty()) {
@@ -117,21 +117,19 @@ void Tree::traverse() {
117117

118118
void Tree::traverse_internal(GameArchives *ancestor_archives) {
119119
GameArchives archives[] = { GameArchives(), ancestor_archives[0], ancestor_archives[1] };
120-
120+
121121
Progress::push_message("checking " + name);
122122

123123
auto flags = check ? ARCHIVE_FL_CREATE : 0;
124-
125-
for (size_t ft = 0; ft < TYPE_MAX; ft++) {
126-
auto filetype = static_cast<filetype_t>(ft);
127-
124+
125+
for (auto filetype: db->filetypes()) {
128126
auto full_name = findfile(filetype, name);
129127

130128
if (full_name.empty() && check) {
131129
full_name = make_file_name(filetype, name);
132130
}
133131
if (!full_name.empty()) {
134-
archives[0].archive[ft] = Archive::open(full_name, filetype, FILE_ROMSET, flags);
132+
archives[0].archive[filetype] = Archive::open(full_name, filetype, FILE_ROMSET, flags);
135133
}
136134
}
137135

@@ -149,7 +147,7 @@ void Tree::traverse_internal(GameArchives *ancestor_archives) {
149147

150148
Tree *Tree::add_node(const std::string &game_name, bool do_check) {
151149
auto it = children.find(game_name);
152-
150+
153151
if (it == children.end()) {
154152
auto child = std::make_shared<Tree>(game_name, do_check);
155153
children[game_name] = child;
@@ -166,7 +164,7 @@ Tree *Tree::add_node(const std::string &game_name, bool do_check) {
166164

167165
void Tree::process(GameArchives *archives) {
168166
auto game = db->read_game(name);
169-
167+
170168
if (!game) {
171169
output.error("db error: %s not found", name.c_str());
172170
return;
@@ -178,9 +176,9 @@ void Tree::process(GameArchives *archives) {
178176
Result res(game.get(), archives[0]);
179177

180178
check_old(game.get(), &res);
181-
for (size_t ft = 0; ft < TYPE_MAX; ft++) {
182-
check_game_files(game.get(), static_cast<filetype_t>(ft), archives, &res);
183-
check_archive_files(static_cast<filetype_t>(ft), archives[0], game->name, &res);
179+
for (auto ft: db->filetypes()) {
180+
check_game_files(game.get(), ft, archives, &res);
181+
check_archive_files(ft, archives[0], game->name, &res);
184182
}
185183
update_game_status(game.get(), &res);
186184

src/check_game_files.cc

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -256,9 +256,7 @@ void update_game_status(const Game* game, Result* result) {
256256
auto has_own = false;
257257
auto have_mia = false;
258258

259-
for (size_t ft = 0; ft < TYPE_MAX; ft++) {
260-
auto filetype = static_cast<filetype_t>(ft);
261-
259+
for (auto filetype: db->filetypes()) {
262260
for (size_t i = 0; i < game->files[filetype].size(); i++) {
263261
auto match = &result->game_files[filetype][i];
264262
auto& rom = game->files[filetype][i];

src/check_old.cc

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,7 @@ check_old(Game *game, Result *result) {
4444

4545
auto all_old = true;
4646

47-
for (size_t ft = 0; ft < TYPE_MAX; ft++) {
48-
auto filetype = static_cast<filetype_t>(ft);
49-
47+
for (auto filetype: db->filetypes()) {
5048
for (size_t i = 0; i < game->files[filetype].size(); i++) {
5149
if (find_in_old(filetype, &game->files[filetype][i], nullptr, &result->game_files[filetype][i]) != FIND_EXISTS) {
5250
all_old = false;

src/diagnostics.cc

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,7 @@ static void diagnostics_game(filetype_t ft, const Game *game, const Result &resu
5050
void diagnostics(const Game *game, const GameArchives &archives, const Result &result) {
5151
ckmame_cache->stats.add_game(result.game);
5252

53-
for (size_t ft = 0; ft < TYPE_MAX; ft++) {
54-
auto filetype = static_cast<filetype_t>(ft);
55-
53+
for (auto filetype: db->filetypes()) {
5654
for (size_t i = 0; i < game->files[filetype].size(); i++) {
5755
ckmame_cache->stats.add_rom(filetype, &game->files[filetype][i], result.game_files[filetype][i].quality);
5856
}

src/fix.cc

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,7 @@ static int clear_incomplete(Game *game, filetype_t filetype, Archive *archive, R
5454
int fix_game(Game *game, const GameArchives archives, Result *result) {
5555
int ret = 0;
5656

57-
for (size_t ft = 0; ft < TYPE_MAX; ft++) {
58-
auto filetype = static_cast<filetype_t>(ft);
57+
for (auto filetype: db->filetypes()) {
5958
Archive *archive = archives[filetype];
6059
GarbagePtr garbage;
6160
DeleteList::Mark extra_mark, needed_mark, superfluous_mark;
@@ -164,8 +163,7 @@ int fix_save_needed_from_unknown(Game *game, const GameArchives archives, Result
164163
auto needs_recheck = false;
165164

166165
if (configuration.fix_romset) {
167-
for (size_t ft = 0; ft < TYPE_MAX; ft++) {
168-
auto filetype = static_cast<filetype_t>(ft);
166+
for (auto filetype: db->filetypes()) {
169167
auto garbage_name = make_garbage_name(archives[filetype]->name, 0);
170168

171169
warn_set_info(WARN_TYPE_ARCHIVE, garbage_name);

0 commit comments

Comments
 (0)