1#include "AssetReader.h"
5#include "EntityReader.h"
6#include "ResourceReader.h"
7#include "SceneReader.h"
8#include "VariableReader.h"
10#include "ExtensionRegistry.h"
15#include "Resources/ResourceStore.h"
17#include "Foundation/Logging/Logger.h"
18#include "Foundation/Platform/FileContents.h"
24 bool loadSceneExtension(
const std::string& name)
26#if !defined( EMSCRIPTEN )
32 LOG_WARNING_ONCE(logger,
"Scene file in Cogs.js using extension Not Loaded: %s", name.c_str());
44 if (jsonTemplates.IsObject()) {
45 for (
const auto& t : jsonTemplates.GetObject()) {
46 readEntityDefinition(context->
store, toKey(t.name), t.value);
50 LOG_ERROR(logger,
"Expected templates section as JSON object");
57 if (jsonExtensions.IsString()) {
58 auto name = toString(jsonExtensions);
59 ok = loadSceneExtension(name) && ok;
60 }
else if (jsonExtensions.IsArray()) {
61 for (
const auto & element : jsonExtensions.GetArray()) {
62 if (element.IsString()) {
63 auto name = toString(element);
64 ok = loadSceneExtension(name) && ok;
66 LOG_ERROR(logger,
"Extension element must be string.");
71 LOG_ERROR(logger,
"Extensions may only be specified as a single string or array of strings.");
78 [[nodiscard]]
bool readImports(
Context * context,
const Value & jsonImports,
AssetLoadFlags flags, ComponentModel::Entity* root)
84 if (jsonImports.IsString()) {
85 ok = readAsset(context, toKey(jsonImports), flags, root);
86 }
else if (jsonImports.IsArray()) {
87 for (
const auto & element : jsonImports.GetArray()) {
88 if (element.IsString()) {
89 ok = readAsset(context, toKey(element), flags, root) && ok;
91 LOG_ERROR(logger,
"Import element must be string path.");
96 LOG_ERROR(logger,
"Imports may only be specified as a single string or array of strings.");
108 bool setup =
true && (flags & AssetLoadFlags::ClearScene) != 0;
110 if ((flags & AssetLoadFlags::NoDefault) != 0) {
114 for (
const auto& section : document.GetObject()) {
115 if (section.name ==
"meta") {
116 if (section.value.HasMember(
"flags")) {
124 context->
scene->setup(
true);
128 for (
const auto& section : document.GetObject()) {
130 switch (sectionName.
hash()) {
132 if (!readExtensions(context, section.value, flags)) {
133 LOG_ERROR(logger,
"Failed to load one or several 'extensions'. Scene loading aborted");
138 if (!readImports(context, section.value, flags, root)) {
139 LOG_ERROR(logger,
"Failed to read all 'imports'. Scene loading aborted");
144 readTemplates(context, section.value, flags);
147 readResources(context, section.value);
151 readScene(context, section.value, flags, root);
153 case Cogs::hash(
"variables"): [[fallthrough]];
155 readVariables(*context->variables, section.value, {});
160 LOG_WARNING(logger,
"Unknown scene section: %.*s", StringViewFormat(sectionName));
172 if ((flags & AssetLoadFlags::ClearScene) == AssetLoadFlags::ClearScene) {
173 context->
scene->clear();
176 rapidjson::Document document = parseJson(context, fileName, JsonParseFlags::NoCachedContent);
178 if (!document.IsObject()) {
179 LOG_ERROR(logger,
"readAsset: JSon Parse Error '%.*s'", StringViewFormat(fileName));
180 if ((flags & AssetLoadFlags::ClearScene) == AssetLoadFlags::ClearScene) {
181 context->
scene->setup(
true);
186 bool ok = loadAsset(context, document, flags, root);
187 if ((flags & AssetLoadFlags::ClearScene) == AssetLoadFlags::ClearScene) {
188 context->
scene->setup(
false);
197 if ((flags & AssetLoadFlags::ClearScene) == AssetLoadFlags::ClearScene) {
198 context->
scene->clear();
201 rapidjson::Document document = parseJson(contents, JsonParseFlags::NoCachedContent);
203 if (!document.IsObject()) {
204 LOG_ERROR(logger,
"readAssetFromString: JSon Parse Error: %.*s", StringViewFormat(contents.
substr(0,80)));
205 if ((flags & AssetLoadFlags::ClearScene) == AssetLoadFlags::ClearScene) {
206 context->
scene->setup(
true);
211 return loadAsset(context, document, flags, root);
220 if (!document.IsObject()) {
221 LOG_ERROR(logger,
"readAssetDefinitionFromDocument: JSon Parse Error '%.*s'", StringViewFormat(origin));
228 for (
const auto& section : document.GetObject()) {
230 switch (sectionName.
hash()) {
232 if (!readExtensions(context, section.value, flags)) {
233 LOG_ERROR(logger,
"Failed to load one or several 'extensions'. Scene loading aborted");
238 if (!readImports(context, section.value, flags, nullptr)) {
239 LOG_ERROR(logger,
"Failed to read all 'imports'. Scene loading aborted");
244 readTemplates(context, section.value, flags);
247 readResourceDefinitions(context, section.value, 0, assetDefinition);
253 case Cogs::hash(
"variables"): [[fallthrough]];
255 readVariables(*context->variables, section.value, {});
260 LOG_WARNING(logger,
"Unknown scene section: %.*s", StringViewFormat(sectionName));
267 JsonParseFlags createJsonParseFlags(
bool compressed) {
268 auto flags = JsonParseFlags::PreferUncachedContent;
270 flags |= JsonParseFlags::Compressed;
278 auto flags = createJsonParseFlags(compressed);
279 auto doc = parseJson(context, fileName, flags);
280 return readAssetDefinitionFromDocument(context, doc, fileName, assetDefinition);
283bool Cogs::Core::readAssetDefinitionFromFileContents(
class Context* context, std::unique_ptr<FileContents> contents,
AssetDefinition & assetDefinition,
bool compressed )
285 auto flags = createJsonParseFlags(compressed);
286 std::string origin = contents->origin().
to_string();
287 auto doc = parseJson(context, std::move(contents), flags);
288 return readAssetDefinitionFromDocument(context, doc, origin , assetDefinition);
Container for components, providing composition of dynamic entities.
A Context instance contains all the services, systems and runtime components needed to use Cogs.
class EntityStore * store
Entity store.
std::unique_ptr< class Scene > scene
Scene structure.
static bool hasExtension(const StringView &key, bool silent=false)
Check if an extension with the given key is present in this build.
static const void * loadExtensionModule(const std::string &path, void **modulehandle=nullptr, ExtensionModuleLoadResult *result=nullptr)
Load the extension module with the given name.
Log implementation class.
Provides a weakly referenced view over the contents of a string.
constexpr StringView substr(size_t offset, size_t count=NoPosition) const noexcept
Get the given sub string.
std::string to_string() const
String conversion method.
constexpr size_t hash() const noexcept
Get the hash code of the string.
Contains the Engine, Renderer, resource managers and other systems needed to run Cogs....
void COGSCORE_DLL_API readSceneDefinition(class Context *context, const Value &jsonScene, AssetLoadFlags flags, AssetDefinition &asset)
Parse JSON description of Entities in Cogs Scene. Store contents in asset param.
void COGSCORE_DLL_API readScene(class Context *context, const Value &jsonScene, AssetLoadFlags flags, ComponentModel::Entity *root)
Parse JSON description of Entities in Cogs Scene file and create Entities defined in scene.
AssetLoadFlags
Asset and Scene loading flags. May be combined with resource loading flags.
@ NoDefault
Don't load the default scene. Highly recommended as not setting this flag cause extra scene parse.
@ ClearScene
Clear the scene before loading the asset. NOT allowed for Bridge: loadAsset and loadAssetFromString.
constexpr Log getLogger(const char(&name)[LEN]) noexcept
Contains all Cogs related functionality.
constexpr size_t hash() noexcept
Simple getter function that returns the initial value for fnv1a hashing.