30#include "RNA_prototypes.hh"
65 switch (value.type()) {
66 case toml::value_t::empty:
68 case toml::value_t::boolean:
70 case toml::value_t::integer:
72 case toml::value_t::floating:
74 case toml::value_t::string:
76 case toml::value_t::offset_datetime:
77 return "offset datetime";
78 case toml::value_t::local_datetime:
79 return "local datetime";
80 case toml::value_t::local_date:
82 case toml::value_t::local_time:
84 case toml::value_t::array:
86 case toml::value_t::table:
102 this->name = toml::find<std::string>(
v,
"name");
104 toml::value var =
v.at(
"name");
105 std::string err = toml::format_error(
106 "Invalid project variable name",
108 "Invalid variable name: \"" + this->name +
109 "\". Variable names can not be empty, must not start with a digit, and "
110 "must contain only alphanumeric characters and underscores.");
112 throw toml::type_error(err, var.location());
115 auto type = toml::find<std::string>(
v,
"type");
116 if (
type ==
"INTEGER") {
119 else if (
type ==
"FLOAT") {
122 else if (
type ==
"STRING") {
126 toml::value var =
v.at(
"type");
127 std::string err = toml::format_error(
128 "Invalid Project variable type",
130 "invalid value for \'type' expected \"INTEGER\", \"FLOAT\", or \"STRING\", but got \"" +
133 throw toml::type_error(err, var.location());
136 this->value =
v.at(
"value");
137 toml::value
value = this->value;
140 std::string err = toml::format_error(
141 "Project Variable type and value mismatch",
143 "Actual and declared type of project variable \"value\" do not match. Got \"" +
146 throw toml::type_error(err,
value.location());
149 std::string err = toml::format_error(
150 "Project Variable type and value mismatch",
152 "Actual and declared type of project variable \"value\" do not match. Got \"" +
155 throw toml::type_error(err,
value.location());
158 if (!
value.is_string()) {
159 std::string err = toml::format_error(
160 "Project Variable type and value mismatch",
162 "Actual and declared type of project variable \"value\" do not match. Got \"" +
165 throw toml::type_error(err,
value.location());
168 auto subtype = toml::find<std::string>(
v,
"subtype");
173 else if (
subtype ==
"FILEPATH") {
177 toml::value var =
v.at(
"subtype");
178 std::string err = toml::format_error(
"Invalid Project variable subtype",
180 "invalid value for \'subtype' expected \"NONE\" or "
181 "\"FILEPATH\", but got \"" +
184 throw toml::type_error(err, var.location());
187 this->description = toml::find_or(
v,
"description",
"");
194 toml::value value = project_variable.
value;
196 switch (project_variable.
type) {
198 new_var->
value_set(toml::get<int>(value));
201 new_var->
value_set(toml::get<float>(value));
204 new_var->
value_set(toml::get<std::string>(value));
233 this->name = toml::find<std::string>(
v,
"name");
235 std::string import_method_str = toml::find<std::string>(
v,
"import_method");
239 toml::value var =
v.at(
"import_method");
240 std::string err = toml::format_error(
"Invalid Project asset library import method",
242 "invalid value for \'import_method. "
245 "\" is not a valid import method");
247 throw toml::type_error(err, var.location());
250 if (
v.contains(
"url")) {
251 this->is_remote_asset_lib =
true;
252 this->url = toml::find<std::string>(
v,
"url");
254 this->access_token = toml::find_or(
v,
"access_token",
"");
258 this->path = toml::find<std::string>(
v,
"path");
260 this->use_relative_path = toml::find<bool>(
v,
"use_relative_path");
262 this->uuid = toml::find_or(
v,
"uuid",
"");
271 project_asset_library.
name.c_str(),
272 project_asset_library.
url.c_str(),
279 std::optional<UUID> uuid = {};
280 bool invalid_uuid =
false;
281 if (project_asset_library.
uuid !=
"") {
283 uuid =
UUID(project_asset_library.
uuid);
285 catch (
const std::runtime_error &
e) {
291 &
U, project_asset_library.
name.c_str(), project_asset_library.
path.c_str(), uuid);
308 std::string toml_path,
309 std::string project_root_path)
313 if (!config_file.is_open()) {
314 throw std::system_error(errno, std::generic_category(),
"Failed to open file: " + toml_path);
317 toml::value config_values = toml::parse(config_file, toml_path,
toml_version);
318 toml::value schema_var = config_values.at(
"schema_version");
319 int version = schema_var.as_integer();
322 std::string err = toml::format_error(
323 "Too new project schema version!",
326 throw toml::type_error(err, schema_var.location());
333 auto project_name = toml::find<std::string>(config_values,
"name");
335 throw std::invalid_argument(
336 "Couldn't initialize the project because the project name was empty");
340 if (config_values.contains(
"ocio_config")) {
341 auto ocio_config_path = config_values.at(
"ocio_config").as_string();
342 project->set_ocio_config_path(ocio_config_path);
345 if (config_values.contains(
"variables")) {
346 for (
const auto &variable : toml::find<toml::array>(config_values,
"variables")) {
347 auto project_variable = toml::get<TomlProjectVariable>(variable);
352 if (config_values.contains(
"asset_libraries")) {
353 for (
const auto &
library : toml::find<toml::array>(config_values,
"asset_libraries")) {
354 auto project_asset_library = toml::get<TomlAssetLibrary>(
library);
359 if (config_values.contains(
"splash_image")) {
360 auto splash_image_path = config_values.at(
"splash_image").as_string();
361 project->set_splash_image_path(splash_image_path);
373 toml::ordered_array toml_array;
374 for (
auto &project_variable :
project->variables) {
375 toml::ordered_table variable_data = {{
"name", std::string(project_variable->name_get())}};
385 variable_data[
"type"] = std::string(idname);
387 switch (project_variable->type_get()) {
389 variable_data[
"value"] = project_variable->value_int_get();
392 variable_data[
"value"] = project_variable->value_float_get();
395 variable_data[
"value"] = std::string(project_variable->value_string_get());
396 string_subtype = project_variable->string_subtype_get();
403 variable_data[
"subtype"] = std::string(idname);
410 StringRefNull description = project_variable->description_get();
413 variable_data[
"description"] = std::string(description);
416 toml_array.push_back(variable_data);
423 toml::ordered_array toml_array;
424 for (
auto &user_library :
U.asset_libraries.items_mutable()) {
426 const char *import_method;
428 int(user_library.import_method),
429 &import_method) == 0)
434 toml::ordered_table asset_library_data;
437 asset_library_data = {{
"name", user_library.name},
438 {
"import_method", std::string(import_method)},
439 {
"url", user_library.remote_url}};
441 asset_library_data[
"access_token"] = std::string(user_library.auth_token);
446 UUID uuid =
UUID(user_library.uuid);
447 asset_library_data = {
448 {
"name", user_library.name},
449 {
"import_method", std::string(import_method)},
450 {
"path", user_library.dirpath},
452 {
"uuid", uuid.
str()}};
454 toml_array.push_back(asset_library_data);
463 {
"name", std::string(
project->get_name())}};
467 toml_table[
"ocio_config"] = std::string(ocio_config_path);
471 if (!variables.empty()) {
472 toml_table[
"variables"] = variables;
476 if (!asset_libraries.empty()) {
477 toml_table[
"asset_libraries"] = asset_libraries;
481 if (!splash_image_path.
is_empty()) {
482 toml_table[
"splash_image"] = std::string(splash_image_path);
506 CLOG_ERROR(&
LOG,
"Couldn't save project config, no active project.");
512 CLOG_ERROR(&
LOG,
"Failed to create project config directory '%s'", config_dir.c_str());
522 if (!config_file.is_open()) {
524 "Failed to open project config file '%s' for writing: %s",
526 std::strerror(errno));
529 config_file << toml::format(toml::ordered_value(toml_config),
bke::toml_version);
531 if (config_file.fail()) {
549 CLOG_ERROR(&
LOG,
"Project root path '%s', is not a directory", project_root_path.c_str());
562 catch (
const std::exception &
e) {
563 CLOG_ERROR(&
LOG,
"Failed loading project from toml: \n%s",
e.what());
#define BLENDER_PROJECT_CONFIG_DIR
#define BLENDER_PROJECT_CONFIG_FILE
File and directory operations.
#define CLOG_ERROR(clg_ref,...)
#define CLOG_INFO_NOCHECK(clg_ref, format,...)
#define BLI_assert_unreachable()
void string_subtype_set(ProjectVariableStringSubtype type)
void description_set(StringRef description)
void value_set(StringRefNull value)
constexpr bool is_empty() const
constexpr toml::spec toml_version
static toml::ordered_table create_toml_from_blender_project(BlenderProject *project)
static toml::ordered_array project_asset_libraries_to_toml()
static void project_variable_add(BlenderProject *project, TomlProjectVariable project_variable)
static void create_blender_project_from_toml(Main *bmain, std::string toml_path, std::string project_root_path)
static toml::ordered_array project_variables_to_toml(BlenderProject *project)
static void project_asset_library_add(TomlAssetLibrary project_asset_library)
const int project_schema_version
static std::string toml_type_name(const toml::value &value)
bool is_valid_project_variable_name(StringRef name)
ProjectVariableStringSubtype
const EnumPropertyItem rna_enum_asset_import_method_items[]
void BKE_blender_project_config_autosave(Main *bmain)
@ ASSET_LIBRARY_USE_AUTH_TOKEN
@ ASSET_LIBRARY_PROJECT_DEFINED
@ ASSET_LIBRARY_USE_REMOTE_URL
@ ASSET_LIBRARY_RELATIVE_PATH
bool BLI_dir_create_recursive(const char *dirname) ATTR_NONNULL()
bke::BlenderProject * BKE_blender_project_get(const Main *bmain)
const EnumPropertyItem rna_enum_project_variable_type_items[]
ATTR_WARN_UNUSED_RESULT const BMVert * v
auto BKE_blender_project_read_callback(const Main *bmain, Fn lambda)
void BKE_blender_project_clear()
struct bUserAssetLibrary * BKE_preferences_project_asset_library_add(struct UserDef *userdef, const char *name, const char *dirpath, std::optional< UUID > uuid=std::nullopt) ATTR_NONNULL(1)
@ USER_PREF_FLAG_PROJECT_SAVE
void BKE_blender_project_recent_update(const Main *bmain)
struct bUserAssetLibrary bUserAssetLibrary * BKE_preferences_project_remote_asset_library_add(UserDef *userdef, const char *name, const char *remote_url, const char *auth_token) ATTR_NONNULL(1
bool BKE_blender_project_config_file_write(Main *bmain)
bool RNA_enum_id_from_value(const EnumPropertyItem *item, int value, const char **r_identifier)
bool BLI_is_dir(const char *path) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
bool BKE_blender_project_init(blender::StringRef name, blender::StringRef root_path)
bool RNA_enum_value_from_identifier(const EnumPropertyItem *item, const char *identifier, int *r_value)
const EnumPropertyItem rna_enum_project_variable_string_subtype_items[]
char * BLI_strdup(const char *str) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1) ATTR_MALLOC
bool BLI_is_file(const char *path) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
bool BKE_blender_project_init_from_root_path(Main *bmain, std::string project_root_path)
ATTR_WARN_UNUSED_RESULT const BMVert const BMEdge * e
void from_toml(const toml::value &v)
ProjectVariableStringSubtype subtype
void from_toml(const toml::value &v)