/* Copyright (c) 2022 Alex Diener This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages arising from the use of this software. Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions: 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software. 3. This notice may not be removed or altered from any source distribution. Alex Diener alex@ludobloom.com */ #include "3dmodelio/MeshConfigurationRegistry.h" #include #define stemobject_implementation MeshConfigurationRegistry stemobject_vtable_begin(); stemobject_vtable_entry(dispose); stemobject_vtable_end(); #define MESH_VERSION_0_SUPPORT 1 MeshConfigurationRegistry * MeshConfigurationRegistry_create(void) { stemobject_create_implementation(init) } bool MeshConfigurationRegistry_init(MeshConfigurationRegistry * self) { call_super(init, self); self->entryCount = 0; self->entries = NULL; return true; } void MeshConfigurationRegistry_dispose(MeshConfigurationRegistry * self) { free(self->entries); call_super_virtual(dispose, self); } void MeshConfigurationRegistry_registerConfiguration(MeshConfigurationRegistry * self, struct MeshConfigurationRegistry_entry entry) { for (unsigned int entryIndex = 0; entryIndex < self->entryCount; entryIndex++) { if (self->entries[entryIndex].name == entry.name) { #ifdef DEBUG fprintf(stderr, "Warning: Attempt to register mesh configuration \"%s\", which is already registered; doing nothing\n", entry.name); #endif return; } } self->entries = realloc(self->entries, (self->entryCount + 1) * sizeof(*self->entries)); self->entries[self->entryCount] = entry; self->entryCount++; } struct MeshConfigurationRegistry_entry * MeshConfigurationRegistry_lookup(MeshConfigurationRegistry * self, Atom name) { for (unsigned int entryIndex = 0; entryIndex < self->entryCount; entryIndex++) { if (self->entries[entryIndex].name == name) { return &self->entries[entryIndex]; } } return NULL; } MeshConfigurationData * MeshConfigurationRegistry_deserializeMeshConfigurationData(MeshConfigurationRegistry * self, compat_type(DeserializationContext *) deserializationContext) { DeserializationContext * context = deserializationContext; call_virtual(beginStructure, context, MESH_CONFIGURATION_DATA_FORMAT_TYPE); const char * formatType = call_virtual(readString, context, "format_type"); unsigned int formatVersion = call_virtual(readUInt16, context, "format_version"); const char * configurationTypeString = NULL; if (!strcmp(formatType, MESH_CONFIGURATION_DATA_FORMAT_TYPE)) { if (formatVersion > MESH_CONFIGURATION_DATA_FORMAT_VERSION) { return NULL; } configurationTypeString = call_virtual(readStringNullable, context, "type"); #if MESH_VERSION_0_SUPPORT } else if (!strcmp(formatType, "material")) { if (formatVersion > 0) { return NULL; } call_virtual(readString, context, "name"); configurationTypeString = "material_v0"; #endif } MeshConfigurationData * configurationData = NULL; if (configurationTypeString != NULL) { struct MeshConfigurationRegistry_entry * configurationEntry = MeshConfigurationRegistry_lookup(self, Atom_getExisting(configurationTypeString)); if (configurationEntry != NULL && configurationEntry->deserializeImplementation != NULL) { configurationData = configurationEntry->deserializeImplementation(context); } } call_virtual(endStructure, context); return configurationData; }