Skip to content

Commit

Permalink
better import from prusa/orca
Browse files Browse the repository at this point in the history
  • Loading branch information
supermerill committed Jan 30, 2024
2 parents d24b494 + fecf990 commit 79eb38e
Show file tree
Hide file tree
Showing 16 changed files with 706 additions and 127 deletions.
10 changes: 8 additions & 2 deletions src/PrusaSlicer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,10 @@ int CLI::run(int argc, char **argv)
if (! config_substitutions.empty()) {
boost::nowide::cout << "The following configuration values were substituted when loading \" << file << \":\n";
for (const ConfigSubstitution &subst : config_substitutions)
boost::nowide::cout << "\tkey = \"" << subst.opt_def->opt_key << "\"\t loaded = \"" << subst.old_value << "\tsubstituted = \"" << subst.new_value->serialize() << "\"\n";
if(subst.opt_def)
boost::nowide::cout << "\tkey = \"" << subst.opt_def->opt_key << "\"\t loaded = \"" << subst.old_value << "\tsubstituted = \"" << subst.new_value->serialize() << "\"\n";
else
boost::nowide::cout << "\tkey = \"" << subst.old_name << "\"\t can't be loaded (value = \"" << subst.old_value <<"\")\n";
}
config.normalize_fdm();
PrinterTechnology other_printer_technology = get_printer_technology(config);
Expand Down Expand Up @@ -195,7 +198,10 @@ int CLI::run(int argc, char **argv)
if (! config_substitutions.substitutions.empty()) {
boost::nowide::cout << "The following configuration values were substituted when loading \" << file << \":\n";
for (const ConfigSubstitution& subst : config_substitutions.substitutions)
boost::nowide::cout << "\tkey = \"" << subst.opt_def->opt_key << "\"\t loaded = \"" << subst.old_value << "\tsubstituted = \"" << subst.new_value->serialize() << "\"\n";
if(subst.opt_def)
boost::nowide::cout << "\tkey = \"" << subst.opt_def->opt_key << "\"\t loaded = \"" << subst.old_value << "\tsubstituted = \"" << subst.new_value->serialize() << "\"\n";
else
boost::nowide::cout << "\tkey = \"" << subst.old_name << "\"\t can't be loaded (value = \"" << subst.old_value <<"\")\n";
}
// config is applied to m_print_config before the current m_config values.
config += std::move(m_print_config);
Expand Down
15 changes: 9 additions & 6 deletions src/libslic3r/AppConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,9 @@ void AppConfig::set_defaults()
if (get("check_material_export").empty())
set("check_material_export", "0");

if (get("show_unknown_setting").empty())
set("show_unknown_setting", "1");

if (get("use_custom_toolbar_size").empty())
set("use_custom_toolbar_size", "0");

Expand Down Expand Up @@ -504,9 +507,6 @@ void AppConfig::set_defaults()
if (get("show_splash_screen").empty())
set("show_splash_screen", "1");

if (get("show_splash_screen").empty())
set("show_splash_screen", "1");

if (get("restore_win_position").empty())
set("restore_win_position", "1"); // allowed values - "1", "0", "crashed_at_..."

Expand All @@ -519,7 +519,7 @@ void AppConfig::set_defaults()
{

//try to load splashscreen from ui file
std::map<std::string, std::string> key2splashscreen = {{"splash_screen_editor", "benchy-splashscreen.jpg"}, {"splash_screen_gcodeviewer", "prusa-gcodepreview.jpg"} };
std::map<std::string, std::string> key2splashscreen = {{"splash_screen_editor", ""}, {"splash_screen_gcodeviewer", ""} };
boost::property_tree::ptree tree_splashscreen;
boost::filesystem::path path_colors = boost::filesystem::path(layout_config_path()) / "colors.ini";
try {
Expand Down Expand Up @@ -547,10 +547,13 @@ void AppConfig::set_defaults()
if (get("splash_screen_gcodeviewer").empty())
set("splash_screen_gcodeviewer", "default");

if (!get("show_splash_screen_random").empty() && get("show_splash_screen_random") == "1") {
bool switch_to_random = get("show_splash_screen_random") == "1";
if (switch_to_random || key2splashscreen["splash_screen_editor"].empty())
set("splash_screen_editor", "random");
if (switch_to_random || key2splashscreen["splash_screen_gcodeviewer"].empty())
set("splash_screen_gcodeviewer", "random");
if (switch_to_random)
set("show_splash_screen_random", "0");
}
}

#ifdef _WIN32
Expand Down
107 changes: 83 additions & 24 deletions src/libslic3r/Config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -565,12 +565,17 @@ bool ConfigBase::set_deserialize_nothrow(const t_config_option_key &opt_key_src,
{
t_config_option_key opt_key = opt_key_src;
std::string value = value_src;
//note: should be done BEFORE calling set_deserialize
// Both opt_key and value may be modified by handle_legacy().
// If the opt_key is no more valid in this version of Slic3r, opt_key is cleared by handle_legacy().
this->handle_legacy(opt_key, value);
if (opt_key.empty())
if (opt_key.empty()) {
assert(false);
// Ignore the option.
return true;
}
assert(opt_key == opt_key_src);
assert(value == value_src);
return this->set_deserialize_raw(opt_key, value, substitutions_ctxt, append);
}

Expand Down Expand Up @@ -937,9 +942,18 @@ ConfigSubstitutions ConfigBase::load(const boost::property_tree::ptree &tree, Fo
for (const boost::property_tree::ptree::value_type &v : tree) {
t_config_option_key opt_key = v.first;
try {
this->set_deserialize(opt_key, v.second.get_value<std::string>(), substitutions_ctxt);
std::string value = v.second.get_value<std::string>();
PrintConfigDef::handle_legacy(opt_key, value, true);
if (opt_key.empty()) {
if (substitutions_ctxt.rule != ForwardCompatibilitySubstitutionRule::Disable) {
substitutions_ctxt.substitutions.emplace_back(v.first, value);
}
} else {
this->set_deserialize(opt_key, value, substitutions_ctxt);
}
} catch (UnknownOptionException & /* e */) {
// ignore
assert(false);
} catch (BadOptionValueException & e) {
if (compatibility_rule == ForwardCompatibilitySubstitutionRule::Disable)
throw e;
Expand All @@ -954,34 +968,37 @@ ConfigSubstitutions ConfigBase::load(const boost::property_tree::ptree &tree, Fo
}

// Load the config keys from the given string.
size_t ConfigBase::load_from_gcode_string_legacy(ConfigBase& config, const char* str, ConfigSubstitutionContext& substitutions)
std::map<t_config_option_key, std::string> ConfigBase::load_gcode_string_legacy(const char* str)
{
std::map<t_config_option_key, std::string> opt_key_values;
if (str == nullptr)
return 0;
return opt_key_values;

// Walk line by line in reverse until a non-configuration key appears.
const char *data_start = str;
// boost::nowide::ifstream seems to cook the text data somehow, so less then the 64k of characters may be retrieved.
const char *end = data_start + strlen(str);
size_t num_key_value_pairs = 0;
const char *end = data_start + strlen(str);
for (;;) {
// Extract next line.
for (--end; end > data_start && (*end == '\r' || *end == '\n'); --end);
for (--end; end > data_start && (*end == '\r' || *end == '\n'); --end)
;
if (end == data_start)
break;
const char *start = end ++;
for (; start > data_start && *start != '\r' && *start != '\n'; --start);
const char *start = end++;
for (; start > data_start && *start != '\r' && *start != '\n'; --start)
;
if (start == data_start)
break;
// Extracted a line from start to end. Extract the key = value pair.
if (end - (++ start) < 10 || start[0] != ';' || start[1] != ' ')
if (end - (++start) < 10 || start[0] != ';' || start[1] != ' ')
break;
const char *key = start + 2;
if (!((*key >= 'a' && *key <= 'z') || (*key >= 'A' && *key <= 'Z')))
// A key must start with a letter.
break;
const char *sep = key;
for (; sep != end && *sep != '='; ++ sep) ;
for (; sep != end && *sep != '='; ++sep)
;
if (sep == end || sep[-1] != ' ' || sep[1] != ' ')
break;
const char *value = sep + 2;
Expand All @@ -991,29 +1008,56 @@ size_t ConfigBase::load_from_gcode_string_legacy(ConfigBase& config, const char*
if (key_end - key < 3)
break;
// The key may contain letters, digits and underscores.
for (const char *c = key; c != key_end; ++ c)
for (const char *c = key; c != key_end; ++c)
if (!((*c >= 'a' && *c <= 'z') || (*c >= 'A' && *c <= 'Z') || (*c >= '0' && *c <= '9') || *c == '_')) {
key = nullptr;
break;
}
if (key == nullptr)
break;
opt_key_values.emplace(std::string(key, key_end), std::string(value, end));
end = start;
}
return opt_key_values;
}


size_t ConfigBase::load_from_gcode_string_legacy(ConfigBase& config, const char* str, ConfigSubstitutionContext& substitutions)
{
if (str == nullptr)
return 0;

// Walk line by line in reverse until a non-configuration key appears.
const char *data_start = str;
// boost::nowide::ifstream seems to cook the text data somehow, so less then the 64k of characters may be retrieved.
const char *end = data_start + strlen(str);
size_t num_key_value_pairs = 0;
for (auto [key, value] : load_gcode_string_legacy(str)) {
try {
config.set_deserialize(std::string(key, key_end), std::string(value, end), substitutions);
++num_key_value_pairs;
std::string opt_key = key;
PrintConfigDef::handle_legacy(opt_key, value, true);
if (opt_key.empty()) {
if (substitutions.rule != ForwardCompatibilitySubstitutionRule::Disable) {
substitutions.substitutions.emplace_back(key, value);
}
} else {
config.set_deserialize(opt_key, value, substitutions);
++num_key_value_pairs;
}
}
catch (UnknownOptionException & /* e */) {
// ignore
// log & ignore
if (substitutions.rule != ForwardCompatibilitySubstitutionRule::Disable)
substitutions.substitutions.emplace_back(key, value);
} catch (BadOptionValueException & e) {
if (substitutions.rule == ForwardCompatibilitySubstitutionRule::Disable)
throw e;
// log the error
const ConfigDef* def = config.def();
if (def == nullptr) throw e;
const ConfigOptionDef* optdef = def->get(std::string(key, key_end));
substitutions.substitutions.emplace_back(optdef, std::string(value, end), ConfigOptionUniquePtr(optdef->default_value->clone()));
const ConfigOptionDef* optdef = def->get(key);
substitutions.substitutions.emplace_back(optdef, value, ConfigOptionUniquePtr(optdef->default_value->clone()));
}
end = start;
}

return num_key_value_pairs;
Expand Down Expand Up @@ -1166,10 +1210,19 @@ ConfigSubstitutions ConfigBase::load_from_gcode_file(const std::string &file, Fo
boost::trim(key);
boost::trim(value);
try {
this->set_deserialize(key, value, substitutions_ctxt);
++ key_value_pairs;
std::string opt_key = key;
PrintConfigDef::handle_legacy(opt_key, value, true);
if (opt_key.empty()) {
if (substitutions_ctxt.rule != ForwardCompatibilitySubstitutionRule::Disable) {
substitutions_ctxt.substitutions.emplace_back(key, value);
}
} else {
this->set_deserialize(opt_key, value, substitutions_ctxt);
++key_value_pairs;
}
} catch (UnknownOptionException & /* e */) {
// ignore
assert(false);
}
}
}
Expand Down Expand Up @@ -1404,10 +1457,16 @@ bool DynamicConfig::read_cli(int argc, const char* const argv[], t_config_option
// Just bail out if the configuration value is not understood.
ConfigSubstitutionContext context(ForwardCompatibilitySubstitutionRule::Disable);
// Any scalar value of a type different from Bool and String.
if (! this->set_deserialize_nothrow(opt_key, value, context, false)) {
boost::nowide::cerr << "Invalid value supplied for --" << token.c_str() << std::endl;
return false;
}
t_config_option_key opt_key_new = opt_key;
PrintConfigDef::handle_legacy(opt_key_new, value, true);
if (opt_key_new.empty()) {
boost::nowide::cerr << "Invalid key supplied: --" << opt_key << std::endl;
} else {
if (!this->set_deserialize_nothrow(opt_key_new, value, context, false)) {
boost::nowide::cerr << "Invalid value supplied for --" << token.c_str() << std::endl;
return false;
}
}
}
}
return true;
Expand Down
5 changes: 4 additions & 1 deletion src/libslic3r/Config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -354,10 +354,12 @@ using ConfigOptionUniquePtr = std::unique_ptr<ConfigOption, ConfigOptionDeleter
// This structure serves to inform the user about the substitutions having been done during file import.
struct ConfigSubstitution {
const ConfigOptionDef *opt_def { nullptr };
std::string old_name; // for when opt_def is nullptr (option not defined in this version)
std::string old_value;
ConfigOptionUniquePtr new_value;
ConfigSubstitution() = default;
ConfigSubstitution(const ConfigOptionDef* def, std::string old, ConfigOptionUniquePtr&& new_v) : opt_def(def), old_value(old), new_value(std::move(new_v)) {}
ConfigSubstitution(const ConfigOptionDef* def, std::string old, ConfigOptionUniquePtr&& new_v) : opt_def(def), old_name(), old_value(old), new_value(std::move(new_v)) {}
ConfigSubstitution(std::string bad_key, std::string value) : opt_def(nullptr), old_name(bad_key), old_value(value), new_value() {}
};

using ConfigSubstitutions = std::vector<ConfigSubstitution>;
Expand Down Expand Up @@ -2364,6 +2366,7 @@ class ConfigBase : public ConfigOptionResolver
// Set all the nullable values to nils.
void null_nullables();

static std::map<t_config_option_key, std::string> load_gcode_string_legacy(const char* str);
static size_t load_from_gcode_string_legacy(ConfigBase& config, const char* str, ConfigSubstitutionContext& substitutions);

private:
Expand Down
Loading

0 comments on commit 79eb38e

Please sign in to comment.