Skip to content
This repository has been archived by the owner on Feb 15, 2024. It is now read-only.

Commit

Permalink
fix path characters for fsb
Browse files Browse the repository at this point in the history
  • Loading branch information
supsm committed Feb 26, 2023
1 parent d958d96 commit b781bed
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 36 deletions.
33 changes: 8 additions & 25 deletions src/cim.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,6 @@ using mcpppp::checkpoint;

namespace cim
{
const static std::regex bad_regex("[^a-z0-9/._-]", std::regex_constants::optimize | std::regex_constants::ECMAScript);

static void fixpathchars(std::string& s)
{
mcpppp::findreplace(s, " ", "_");
s = mcpppp::lowercase(s);
s = std::regex_replace(s, bad_regex, "-");
checkpoint();
}

static void fixpathchars(std::u8string& s)
{
mcpppp::findreplace(s, u8" ", u8"_");
s = mcpppp::lowercase(s);
s = mbtoc8(std::regex_replace(reinterpret_cast<const char*>(s.c_str()), bad_regex, "-"));
checkpoint();
}

// change model paths to work with cim
// @param path path of resourcepack
Expand Down Expand Up @@ -100,7 +83,7 @@ namespace cim
{
// optifine allows spaces somehow
origtemp = temp;
fixpathchars(temp);
mcpppp::conv::fixpathchars(temp);
// has no namespace, use default minecraft namespace
if (!mcpppp::copy(path / u8"assets/minecraft/textures" / (mbtoc8(origtemp) + u8".png"),
path / u8"assets" / mbtoc8(mcnamespace) / "textures/extra/minecraft" / (mbtoc8(temp) + u8".png")))
Expand All @@ -124,7 +107,7 @@ namespace cim
ns.push_back(temp.at(i));
}
origtemp = temp;
fixpathchars(temp);
mcpppp::conv::fixpathchars(temp);
if (!mcpppp::copy(path / u8"assets" / mbtoc8(ns) / u8"textures" / (mbtoc8(origtemp) + u8".png"),
path / u8"assets" / mbtoc8(mcnamespace) / "textures/extra" / mbtoc8(ns) / (mbtoc8(temp) + u8".png")))
{
Expand All @@ -148,7 +131,7 @@ namespace cim

std::filesystem::create_directories(path / u8"assets" / mbtoc8(mcnamespace) / "models" / outputpath);
std::u8string filename = entry.path().filename().u8string();
fixpathchars(filename);
mcpppp::conv::fixpathchars(filename);
std::ofstream fout(path / u8"assets" / mbtoc8(mcnamespace) / "models" / outputpath / filename);
fout << j.dump(1, '\t') << std::endl;
fout.close();
Expand All @@ -170,12 +153,12 @@ namespace cim
std::u8string folderpath = entry.path().generic_u8string();
folderpath.erase(folderpath.begin(), folderpath.begin() + static_cast<std::string::difference_type>(folderpath.rfind(u8"/cit/") + 5));
folderpath.erase(folderpath.end() - static_cast<std::string::difference_type>(entry.path().filename().u8string().size()), folderpath.end());
fixpathchars(folderpath);
mcpppp::conv::fixpathchars(folderpath);
if (entry.path().extension() == ".png")
{
std::filesystem::create_directories(path / u8"assets" / mbtoc8(mcnamespace) / "textures/item" / folderpath);
std::u8string filename = entry.path().filename().u8string();
fixpathchars(filename);
mcpppp::conv::fixpathchars(filename);
mcpppp::copy(entry.path(), path / u8"assets" / mbtoc8(mcnamespace) / "textures/item" / folderpath / filename);
}
else
Expand Down Expand Up @@ -221,7 +204,7 @@ namespace cim
std::u8string folderpath = entry.path().generic_u8string();
folderpath.erase(folderpath.begin(), folderpath.begin() + static_cast<std::string::difference_type>(folderpath.rfind(u8"/cit/") + 5));
folderpath.erase(folderpath.end() - static_cast<std::string::difference_type>(entry.path().filename().u8string().size()), folderpath.end());
fixpathchars(folderpath);
mcpppp::conv::fixpathchars(folderpath);

// convert range stuff from optifine format (with -) to chime format (with [,] >= <= etc)
const auto handlerange = [](const std::string& optifine_range) -> std::string
Expand Down Expand Up @@ -293,7 +276,7 @@ namespace cim
else if (option == "texture")
{
texture = value;
fixpathchars(texture);
mcpppp::conv::fixpathchars(texture);
if (texture.ends_with(".png"))
{
texture.erase(texture.end() - 4, texture.end());
Expand Down Expand Up @@ -328,7 +311,7 @@ namespace cim
else if (option == "model")
{
model = value;
fixpathchars(model);
mcpppp::conv::fixpathchars(model);
// std::string::contains in C++23
if (model.find(".json") != std::string::npos)
{
Expand Down
16 changes: 13 additions & 3 deletions src/fsb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,16 @@ namespace fsb
// convert optifine image format (1 image for all 6 sides) into fsb image format (1 image per side)
// @param path path of resourcepack
// @param overworldsky whether image is of overworld sky (and not end)
// @param output location to output to (relative to `path`)
// @param output_path location to output to (relative to `path`)
// @param entry directory entry of image file to convert
// @param filename name of image file (no extension)
static void png(const std::filesystem::path& path, const bool overworldsky, const std::u8string& output_path, const std::filesystem::directory_entry& entry, const std::u8string& filename)
static void png(const std::filesystem::path& path, const bool overworldsky, std::u8string output_path, const std::filesystem::directory_entry& entry, std::u8string filename)
{
output<level_t::detail>("FSB: Converting {}", c8tomb(entry.path().generic_u8string()));

mcpppp::conv::fixpathchars(output_path);
mcpppp::conv::fixpathchars(filename);

// skip if already converted
// TODO: might cause some issues with reconverting
if (std::filesystem::exists(path / output_path / (filename + u8"_top" + (overworldsky ? u8"" : u8"_end") + u8".png")) &&
Expand Down Expand Up @@ -92,6 +96,8 @@ namespace fsb
state.info_png.color.bitdepth = 8;
state.encoder.auto_convert = false;

// TODO: use mcpppp::conv::fixpathchars

// make a copy of state because lodepng::encode modifies state and it breaks things
state_copy = state;
checkError(lodepng::encode(buffer, image1, outw / 4, outh, state_copy));
Expand Down Expand Up @@ -141,8 +147,8 @@ namespace fsb

// convert optifine properties files into fsb properties json
// @param path path of resourcepack
// @param optifine true if in optifine directory, false if mcpatcher
// @param overworldsky whether properties file is overworld sky (and not end)
// @param optifine true if in optifine directory, false if mcpatcher
// @param entry directory entry of properties file
static void prop(const std::filesystem::path& path, const bool overworldsky, const bool optifine, const std::filesystem::directory_entry& entry)
{
Expand Down Expand Up @@ -421,6 +427,8 @@ namespace fsb
}
else
{
// image doesn't exist, replace with 1 pixel transparent texture
// to at least get rid of purple checkerboard
std::filesystem::path fsb_save_dir = path / "assets/fabricskyboxes/sky";
if (!relative)
{
Expand All @@ -439,6 +447,8 @@ namespace fsb
buffer.shrink_to_fit();
checkpoint();
}
mcpppp::conv::fixpathchars(sourcefolder);
mcpppp::conv::fixpathchars(sourcefile);
source = u8"fabricskyboxes:sky/" + sourcefolder + sourcefile;

j["textures"]["top"] = c8tomb(source) + "_top" + (overworldsky ? "" : "_end") + ".png";
Expand Down
7 changes: 0 additions & 7 deletions src/utility.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,6 @@ namespace mcpppp

namespace conv
{
std::string ununderscore(std::string str)
{
str.erase(std::remove(str.begin(), str.end(), '_'), str.end());
checkpoint();
return str;
}

std::string oftoregex(std::string of)
{
findreplace(of, ".", "\\.");
Expand Down
29 changes: 28 additions & 1 deletion src/utility.h
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,11 @@ namespace mcpppp
// remove underscore '_' character from string
// @param str string to remove underscores from (passed by value)
// @return `str` with underscores removed
std::string ununderscore(std::string str);
inline std::string ununderscore(std::string str)
{
str.erase(std::remove(str.begin(), str.end(), '_'), str.end());
return str;
}

// convert optifine regex-like format to java regex
// @param of string of optifine format
Expand All @@ -393,6 +397,29 @@ namespace mcpppp
// @param s string to parse
// @return pair of height values, or { INT_MIN, INT_MIN } on failure
std::pair<int, int> parse_range(const std::string_view& s);

// matches characters that are invalid location paths
const inline std::regex bad_regex("[^a-z0-9/._-]", std::regex_constants::optimize | std::regex_constants::ECMAScript);

// fixes character paths so they are valid minecraft resource locations
// @param s string to fix (passed by reference)
// @return none, output is stored in `s`
inline void fixpathchars(std::string& s)
{
mcpppp::findreplace(s, " ", "_");
s = mcpppp::lowercase(s);
s = std::regex_replace(s, bad_regex, "-");
}

// fixes character paths so they are valid minecraft resource locations
// @param s string to fix (passed by reference)
// @return none, output is stored in `s`
inline void fixpathchars(std::u8string& s)
{
mcpppp::findreplace(s, u8" ", u8"_");
s = mcpppp::lowercase(s);
s = mbtoc8(std::regex_replace(reinterpret_cast<const char*>(s.c_str()), bad_regex, "-"));
}
}

// lol
Expand Down

0 comments on commit b781bed

Please sign in to comment.