[Bf-blender-cvs] [80a4c388f94] blender-projects-basics: Cleanup: Move project settings code to own file

Julian Eisel noreply at git.blender.org
Fri Oct 14 19:15:05 CEST 2022


Commit: 80a4c388f94ae124389ec0fb68f97dfda3f80cb4
Author: Julian Eisel
Date:   Fri Oct 14 19:13:06 2022 +0200
Branches: blender-projects-basics
https://developer.blender.org/rB80a4c388f94ae124389ec0fb68f97dfda3f80cb4

Cleanup: Move project settings code to own file

Will probably reorganize things even further, but step by step.

===================================================================

M	source/blender/blenkernel/BKE_blender_project.hh
M	source/blender/blenkernel/CMakeLists.txt
M	source/blender/blenkernel/intern/blender_project.cc
A	source/blender/blenkernel/intern/blender_project_settings.cc

===================================================================

diff --git a/source/blender/blenkernel/BKE_blender_project.hh b/source/blender/blenkernel/BKE_blender_project.hh
index 99d76e3a597..41531ee382c 100644
--- a/source/blender/blenkernel/BKE_blender_project.hh
+++ b/source/blender/blenkernel/BKE_blender_project.hh
@@ -23,7 +23,13 @@ namespace blender::bke {
 class ProjectSettings;
 struct CustomAssetLibraries;
 
+/**
+ * The active project (if there is one) is stored in static memory here too, and can be queried
+ * using #BlenderProject::get_active().
+ */
 class BlenderProject {
+  friend class ProjectSettings;
+
   std::unique_ptr<ProjectSettings> settings_;
 
  public:
@@ -31,6 +37,12 @@ class BlenderProject {
   static auto set_active_from_settings(std::unique_ptr<ProjectSettings> settings)
       -> BlenderProject *;
 
+  /**
+   * Check if the directory given by \a path contains a .blender_project directory and should thus
+   * be considered a project root directory.
+   */
+  static bool path_is_project_root(StringRef path);
+
   /**
    * Check if \a path points into a project and return the root directory path of that project (the
    * one containing the .blender_project directory). Walks "upwards" through the path and returns
diff --git a/source/blender/blenkernel/CMakeLists.txt b/source/blender/blenkernel/CMakeLists.txt
index 25dd74ed40d..f20666074a0 100644
--- a/source/blender/blenkernel/CMakeLists.txt
+++ b/source/blender/blenkernel/CMakeLists.txt
@@ -83,6 +83,7 @@ set(SRC
   intern/blender.c
   intern/blender_copybuffer.c
   intern/blender_project.cc
+  intern/blender_project_settings.cc
   intern/blender_undo.c
   intern/blender_user_menu.c
   intern/blendfile.c
diff --git a/source/blender/blenkernel/intern/blender_project.cc b/source/blender/blenkernel/intern/blender_project.cc
index 32631623a09..780ef412141 100644
--- a/source/blender/blenkernel/intern/blender_project.cc
+++ b/source/blender/blenkernel/intern/blender_project.cc
@@ -4,14 +4,11 @@
  * \ingroup bke
  */
 
-#include <fstream>
-
 #include "BKE_asset_library_custom.h"
 #include "BKE_blender_project.h"
 #include "BKE_blender_project.hh"
 
 #include "BLI_path_util.h"
-#include "BLI_serialize.hh"
 #include "BLI_string.h"
 
 #include "DNA_space_types.h"
@@ -19,13 +16,8 @@
 #include "BLI_fileops.h"
 #include "BLI_path_util.h"
 
-namespace serialize = blender::io::serialize;
-
 namespace blender::bke {
 
-static StringRef path_strip_trailing_native_slash(StringRef path);
-static bool path_contains_project_settings(StringRef path);
-
 BlenderProject::BlenderProject(std::unique_ptr<ProjectSettings> settings)
     : settings_(std::move(settings))
 {
@@ -66,7 +58,7 @@ StringRef BlenderProject::project_root_path_find_from_path(StringRef path)
 
   while (cur_path.size()) {
     std::string pa = StringRef(path_native.c_str(), cur_path.size());
-    if (bke::path_contains_project_settings(pa)) {
+    if (path_is_project_root(pa)) {
       return path.substr(0, cur_path.size());
     }
 
@@ -89,20 +81,6 @@ ProjectSettings &BlenderProject::get_settings() const
 
 /* ---------------------------------------------------------------------- */
 
-ProjectSettings::ProjectSettings(StringRef project_root_path)
-    : project_root_path_(project_root_path)
-{
-}
-
-bool ProjectSettings::create_settings_directory(StringRef project_root_path)
-{
-  std::string project_root_path_native = project_root_path;
-  BLI_path_slash_native(project_root_path_native.data());
-
-  return BLI_dir_create_recursive(
-      std::string(project_root_path_native + SEP + SETTINGS_DIRNAME).c_str());
-}
-
 static StringRef path_strip_trailing_native_slash(StringRef path)
 {
   const int64_t pos_before_trailing_slash = path.find_last_not_of(SEP);
@@ -111,315 +89,12 @@ static StringRef path_strip_trailing_native_slash(StringRef path)
              path.substr(0, pos_before_trailing_slash + 1);
 }
 
-static bool path_contains_project_settings(StringRef path)
+bool BlenderProject::path_is_project_root(StringRef path)
 {
   path = path_strip_trailing_native_slash(path);
   return BLI_exists(std::string(path + SEP_STR + ProjectSettings::SETTINGS_DIRNAME).c_str());
 }
 
-struct ExtractedSettings {
-  std::string project_name;
-  ListBase asset_libraries = {nullptr, nullptr}; /* CustomAssetLibraryDefinition */
-};
-
-static std::unique_ptr<serialize::Value> read_settings_file(StringRef settings_filepath)
-{
-  std::ifstream is;
-  is.open(settings_filepath);
-  if (is.fail()) {
-    return nullptr;
-  }
-
-  serialize::JsonFormatter formatter;
-  /* Will not be a dictionary in case of error (corrupted file). */
-  std::unique_ptr<serialize::Value> deserialized_values = formatter.deserialize(is);
-  is.close();
-
-  if (deserialized_values->type() != serialize::eValueType::Dictionary) {
-    return nullptr;
-  }
-
-  return deserialized_values;
-}
-
-static std::unique_ptr<ExtractedSettings> extract_settings(
-    const serialize::DictionaryValue &dictionary)
-{
-  using namespace serialize;
-
-  std::unique_ptr extracted_settings = std::make_unique<ExtractedSettings>();
-
-  const DictionaryValue::Lookup attributes = dictionary.create_lookup();
-
-  /* "project": */ {
-    const DictionaryValue::LookupValue *project_value = attributes.lookup_ptr("project");
-    BLI_assert(project_value != nullptr);
-
-    const DictionaryValue *project_dict = (*project_value)->as_dictionary_value();
-    const StringValue *project_name_value =
-        project_dict->create_lookup().lookup("name")->as_string_value();
-    if (project_name_value) {
-      extracted_settings->project_name = project_name_value->value();
-    }
-  }
-  /* "asset_libraries": */ {
-    const DictionaryValue::LookupValue *asset_libraries_value = attributes.lookup_ptr(
-        "asset_libraries");
-    if (asset_libraries_value) {
-      const ArrayValue *asset_libraries_array = (*asset_libraries_value)->as_array_value();
-      if (!asset_libraries_array) {
-        throw std::runtime_error(
-            "Unexpected asset_library format in settings.json, expected array");
-      }
-
-      for (const ArrayValue::Item &element : asset_libraries_array->elements()) {
-        const DictionaryValue *object_value = element->as_dictionary_value();
-        if (!object_value) {
-          throw std::runtime_error(
-              "Unexpected asset_library entry in settings.json, expected dictionary entries only");
-        }
-        const DictionaryValue::Lookup element_lookup = object_value->create_lookup();
-        const DictionaryValue::LookupValue *name_value = element_lookup.lookup_ptr("name");
-        if (name_value && (*name_value)->type() != eValueType::String) {
-          throw std::runtime_error(
-              "Unexpected asset_library entry in settings.json, expected name to be string");
-        }
-        const DictionaryValue::LookupValue *path_value = element_lookup.lookup_ptr("path");
-        if (path_value && (*path_value)->type() != eValueType::String) {
-          throw std::runtime_error(
-              "Unexpected asset_library entry in settings.json, expected path to be string");
-        }
-
-        /* TODO this isn't really extracting, should be creating data from the settings be a
-         * separate step? */
-        CustomAssetLibraryDefinition *library = BKE_asset_library_custom_add(
-            &extracted_settings->asset_libraries);
-        /* Name or path may not be set, this is fine. */
-        if (name_value) {
-          std::string name = (*name_value)->as_string_value()->value();
-          BKE_asset_library_custom_name_set(
-              &extracted_settings->asset_libraries, library, name.c_str());
-        }
-        if (path_value) {
-          std::string path = (*path_value)->as_string_value()->value();
-          BKE_asset_library_custom_path_set(library, path.c_str());
-        }
-      }
-    }
-  }
-
-  return extracted_settings;
-}
-
-struct ResolvedPaths {
-  std::string settings_filepath;
-  std::string project_root_path;
-};
-
-/**
- * Returned paths can be assumed to use native slashes.
- */
-static ResolvedPaths resolve_paths_from_project_path(StringRef project_path)
-{
-  std::string project_path_native = project_path;
-  BLI_path_slash_native(project_path_native.data());
-
-  ResolvedPaths resolved_paths{};
-
-  const StringRef path_no_trailing_slashes = path_strip_trailing_native_slash(project_path_native);
-  if (path_no_trailing_slashes.endswith(ProjectSettings::SETTINGS_DIRNAME)) {
-    resolved_paths.project_root_path =
-        StringRef(path_no_trailing_slashes).drop_suffix(ProjectSettings::SETTINGS_DIRNAME.size());
-  }
-  else {
-    resolved_paths.project_root_path = std::string(path_no_trailing_slashes) + SEP;
-  }
-  resolved_paths.settings_filepath = resolved_paths.project_root_path +
-                                     ProjectSettings::SETTINGS_DIRNAME + SEP +
-                                     ProjectSettings::SETTINGS_FILENAME;
-
-  return resolved_paths;
-}
-
-std::unique_ptr<ProjectSettings> ProjectSettings::load_from_disk(StringRef project_path)
-{
-  ResolvedPaths paths = resolve_paths_from_project_path(project_path);
-
-  if (!BLI_exists(paths.project_root_path.c_str())) {
-    return nullptr;
-  }
-  if (!path_contains_project_settings(paths.project_root_path.c_str())) {
-    return nullptr;
-  }
-
-  std::unique_ptr<serialize::Value> values = read_settings_file(paths.settings_filepath);
-  std::unique_ptr<ExtractedSettings> extracted_settings = nullptr;
-  if (values) {
-    BLI_assert(values->as_dictionary_value() != nullptr);
-    extracted_settings = extract_settings(*values->as_dictionary_value());
-  }
-
-  std::unique_ptr loaded_settings = std::make_unique<ProjectSettings>(paths.project_root_path);
-  if (extracted_settings) {
-    loaded_settings->project_name_ = extracted_settings->project_name;
-    /* Moves ownership. */
-    loaded_settings->asset_libraries_ = CustomAssetLibraries(extracted_settings->asset_libraries);
-  }
-
-  return loaded_settings;
-}
-
-std::unique_ptr<ProjectSettings> ProjectSettings::load_from_path(StringRef path)
-{
-  StringRef project_root = bke::BlenderProject::project_root_path_find_from_path(path);
-  if (project_root.is_empty()) {
-    return nullptr;
-  }
-
-  return bke::ProjectSettings::l

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list