Simplify AsepriteExternalFiles using one std::map for filenames/types

This commit is contained in:
David Capello 2023-01-09 12:52:18 -03:00
parent bb3ba19fc6
commit 4232410719
3 changed files with 17 additions and 19 deletions

View File

@ -1338,13 +1338,13 @@ static void ase_file_write_external_files_chunk(
return;
ChunkWriter chunk(f, frame_header, ASE_FILE_CHUNK_EXTERNAL_FILE);
fputl(ext_files.to_fn.size(), f); // Number of entries
fputl(ext_files.items.size(), f); // Number of entries
ase_file_write_padding(f, 8);
for (auto item : ext_files.to_fn) {
fputl(item.first, f); // ID
fputc(ext_files.types[item.first], f); // Type
for (const auto& it : ext_files.items) {
fputl(it.first, f); // ID
fputc(it.second.type, f); // Type
ase_file_write_padding(f, 7);
ase_file_write_string(f, item.second); // Filename
ase_file_write_string(f, it.second.fn); // Filename
}
}

View File

@ -106,25 +106,24 @@ struct AsepriteChunk {
};
struct AsepriteExternalFiles {
std::map<uint32_t, std::string> to_fn; // ID -> filename
struct Item {
std::string fn; // filename
uint8_t type; // type has one of the ASE_EXTERNAL_FILE_* values
};
std::map<uint32_t, Item> items;
std::map<std::string, uint32_t> to_id; // filename -> ID
std::map<uint32_t, uint8_t> types; // ID -> type (type has one of the ASE_EXTERNAL_FILE_* values)
uint32_t lastid = 0;
// Adds the external filename with the next autogenerated ID and specified type.
void put(const std::string& filename, uint8_t type) {
auto id = ++lastid;
to_fn[id] = filename;
to_id[filename] = id;
types[id] = type;
put(++lastid, filename, type);
}
// Adds the external filename using the specified ID and type.
void put(uint32_t id, const std::string& filename, uint8_t type) {
to_fn[id] = filename;
items[id] = Item{ filename, type };
to_id[filename] = id;
types[id] = type;
}
// Returns true if the given filename exists in the external files
@ -138,10 +137,10 @@ struct AsepriteExternalFiles {
}
bool getFilenameByID(uint32_t id, std::string& fn) const {
auto it = to_fn.find(id);
if (it == to_fn.end())
auto it = items.find(id);
if (it == items.end())
return false;
fn = it->second;
fn = it->second.fn;
return true;
}
};

View File

@ -1197,9 +1197,8 @@ doc::Tileset* AsepriteDecoder::readTilesetChunk(doc::Sprite* sprite,
const uint32_t extFileId = read32(); // filename ID in the external files chunk
const doc::tileset_index extTilesetId = read32(); // tileset ID in the external file
auto it = extFiles.to_fn.find(extFileId);
if (it != extFiles.to_fn.end()) {
auto fn = it->second;
std::string fn;
if (extFiles.getFilenameByID(extFileId, fn)) {
tileset->setExternal(fn, extTilesetId);
}
else {