Skip to content

Commit

Permalink
Implement conditional cfg adding to auxiliary LM files
Browse files Browse the repository at this point in the history
Missing testing.
  • Loading branch information
Atari2 committed Dec 7, 2024
1 parent 33baf1d commit 52d41bf
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 23 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,18 @@ The changelog is available [here](CHANGELOG.md).
Keep in mind that the areas for sprites (00-BF), shooters (C0-CF) and generators (D0-DF) are fixed.
If you want to insert a shooter, it has to be in the range of C0 to CF due to the sprite type's different coding.

### CFG Lunar magic in-editor displays

Sprites that use CFG files do not appear by default in Lunar Magic's custom sprite list, nor do they have a custom display.
To make your CFG sprite appear in the custom sprite list, you'll need to append "display" to the entry in the list.txt file.

For example:
```
00 Blue.cfg display ; this will make Blue.cfg appear in the custom sprite list
01 Red.cfg ; Red will not appear in the custom sprite list
```


### Per-Level Sprites

The slots B0 to BF are special, in that if you assign a level to them, they will become per-level sprites. The sprite will only use
Expand Down
22 changes: 12 additions & 10 deletions src/cfg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ bool read_cfg_file(sprite* spr) {

std::ifstream cfg_stream(spr->cfg_file);
if (!cfg_stream) {
io.error("Can't find CFG file %s, aborting insertion", spr->cfg_file.c_str());
io.error("Can't find CFG file %s, aborting insertion\n", spr->cfg_file.c_str());
return false;
}
std::string current_line;
Expand All @@ -58,17 +58,19 @@ bool read_cfg_file(sprite* spr) {
return false;
};

std::string sprite_name = fs::path{spr->cfg_file}.filename().replace_extension("").generic_string();
if (spr->displays_in_lm) {
std::string sprite_name = fs::path{spr->cfg_file}.filename().replace_extension("").generic_string();

spr->collections.push_back(collection{.name = sprite_name + " (extra bit clear)", .extra_bit = false, .prop = {}});
spr->collections.push_back(collection{.name = sprite_name + " (extra bit set)", .extra_bit = true, .prop = {}});
spr->displays.push_back(
display{.description = sprite_name + " (extra bit clear)", .tiles = {tile{}}, .extra_bit = false});
spr->displays.push_back(
display{.description = sprite_name + " (extra bit set)", .tiles = {tile{}}, .extra_bit = true});

io.debug("Parsed: %s, %zu lines\n", spr->cfg_file.c_str(), line - 1);
spr->collections.push_back(
collection{.name = sprite_name + " (extra bit clear)", .extra_bit = false, .prop = {}});
spr->collections.push_back(collection{.name = sprite_name + " (extra bit set)", .extra_bit = true, .prop = {}});
spr->displays.push_back(
display{.description = sprite_name + " (extra bit clear)", .tiles = {tile{}}, .extra_bit = false});
spr->displays.push_back(
display{.description = sprite_name + " (extra bit set)", .tiles = {tile{}}, .extra_bit = true});

io.debug("Parsed: %s, %zu lines\n", spr->cfg_file.c_str(), line - 1);
}
return true;
}

Expand Down
33 changes: 20 additions & 13 deletions src/sprite.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1194,7 +1194,7 @@ std::vector<std::string> listExtraAsm(const std::string& path, bool& has_error)
std::string line;
ListType type = ListType::Sprite;
sprite* spr = nullptr;
char cfgname[FILENAME_MAX] = {0};
std::string cfgname{};
const char* dir = nullptr;
while (std::getline(listStream, line)) {
int read_until = -1;
Expand All @@ -1215,7 +1215,7 @@ std::vector<std::string> listExtraAsm(const std::string& path, bool& has_error)
io.error("List line %d was malformed: \"%s\"\n", lineno, line.c_str());
return false;
}
strcpy(cfgname, line.c_str() + read_until);
cfgname = line.substr(read_until);
} else if (line.find(':') == line.length() - 1) { // if it's the last char in the string, it's a type change
using svt = std::pair<std::string_view, ListType>;
constexpr std::array typeArray{svt{"SPRITE:"sv, ListType::Sprite},
Expand Down Expand Up @@ -1243,15 +1243,16 @@ std::vector<std::string> listExtraAsm(const std::string& path, bool& has_error)
lineno, line.c_str());
return false;
}
strcpy(cfgname, line.c_str() + read_until);
cfgname = line.substr(read_until);
}

char* dot = strrchr(cfgname, '.');
if (dot == nullptr) {
io.error("Error on list line %d: missing extension on filename %s\n", lineno, cfgname);
size_t dot = cfgname.find_last_of('.');
if (dot == std::string::npos) {
io.error("Error on list line %d: missing extension on filename %s\n", lineno, cfgname.c_str());
return false;
}
dot++;
size_t space_after_ext = cfgname.find_first_of(' ', dot);
std::string_view ext = std::string_view{cfgname}.substr(dot + 1, space_after_ext == std::string::npos ? space_after_ext : space_after_ext - dot - 1);

if (rom != nullptr) {
if (sprite_id == GOAL_POST_SPRITE_ID && rom->is_exlevel()) {
Expand Down Expand Up @@ -1324,29 +1325,35 @@ std::vector<std::string> listExtraAsm(const std::string& path, bool& has_error)
dir = paths[PathType::Generators].c_str();
}
spr->directory = dir;
std::string fullFileName = std::string{dir} + std::string{cfgname};
std::string fullFileName = std::string{dir} + cfgname.substr(0, space_after_ext);

if (type != ListType::Sprite) {
if (strcmp(dot, "asm") && strcmp(dot, "ASM")) {
if (ext != "asm" && ext != "ASM") {
io.error("Error on list line %d: not an asm file\n", lineno, fullFileName.c_str());
return false;
}
spr->asm_file = std::move(fullFileName);
} else {
spr->cfg_file = std::move(fullFileName);
if (!strcmp(dot, "cfg") || !strcmp(dot, "CFG")) {
if (ext == "cfg" || ext == "CFG") {
spr->displays_in_lm = false;
if (space_after_ext != std::string::npos) {
// may be "display|nodisplay"
std::string_view display = std::string_view{cfgname}.substr(space_after_ext + 1);
spr->displays_in_lm = display.starts_with("display"sv);
}
if (!read_cfg_file(spr)) {
io.error("Error on list line %d: Cannot parse CFG file %s.\n", lineno, spr->cfg_file.c_str());
return false;
}

} else if (!strcmp(dot, "json") || !strcmp(dot, "JSON")) {
} else if (ext == "json" || ext == "JSON") {
if (!read_json_file(spr)) {
io.error("Error on list line %d: Cannot parse JSON file %s.\n", lineno, spr->cfg_file.c_str());
return false;
}
spr->displays_in_lm = true;
} else {
io.error("Error on list line %d: Unknown filetype %s\n", lineno, dot);
io.error("Error on list line %d: Unknown filetype %s\n", lineno, ext.data());
return false;
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/structs.h
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,8 @@ struct sprite {

std::vector<collection> collections{};

bool displays_in_lm;

ListType sprite_type = ListType::Sprite;
bool has_empty_table() const;
void clear();
Expand Down

0 comments on commit 52d41bf

Please sign in to comment.