Implemented helper functions in KRContext to simplify API endpoint input validation.
This commit is contained in:
@@ -406,13 +406,12 @@ KrResult KRContext::loadResource(const KrLoadResourceInfo* loadResourceInfo)
|
|||||||
|
|
||||||
KrResult KRContext::unloadResource(const KrUnloadResourceInfo* unloadResourceInfo)
|
KrResult KRContext::unloadResource(const KrUnloadResourceInfo* unloadResourceInfo)
|
||||||
{
|
{
|
||||||
if (unloadResourceInfo->resourceHandle < 0 || unloadResourceInfo->resourceHandle >= m_resourceMapSize) {
|
KRResource* resource = nullptr;
|
||||||
return KR_ERROR_OUT_OF_BOUNDS;
|
KrResult res = getMappedResource(unloadResourceInfo->resourceHandle, &resource);
|
||||||
}
|
if (res != KR_SUCCESS) {
|
||||||
KRResource* resource = m_resourceMap[unloadResourceInfo->resourceHandle];
|
return res;
|
||||||
if (resource == nullptr) {
|
|
||||||
return KR_ERROR_NOT_MAPPED;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO - Need to implement unloading logic
|
// TODO - Need to implement unloading logic
|
||||||
return KR_ERROR_NOT_IMPLEMENTED;
|
return KR_ERROR_NOT_IMPLEMENTED;
|
||||||
}
|
}
|
||||||
@@ -455,26 +454,30 @@ KrResult KRContext::unmapResource(const KrUnmapResourceInfo* unmapResourceInfo)
|
|||||||
|
|
||||||
KrResult KRContext::getResourceData(const KrGetResourceDataInfo* getResourceDataInfo, KrGetResourceDataCallback callback)
|
KrResult KRContext::getResourceData(const KrGetResourceDataInfo* getResourceDataInfo, KrGetResourceDataCallback callback)
|
||||||
{
|
{
|
||||||
if (getResourceDataInfo->resourceHandle < 0 || getResourceDataInfo->resourceHandle >= m_resourceMapSize) {
|
// TODO - This will be asynchronous... The function will always succeed, but the asynchronous result could report a failure
|
||||||
return KR_ERROR_OUT_OF_BOUNDS;
|
|
||||||
}
|
|
||||||
// TODO - This will be asynchronous...
|
|
||||||
KRDataBlock data;
|
KRDataBlock data;
|
||||||
KrGetResourceDataResult result = {};
|
KrGetResourceDataResult result = {};
|
||||||
if (m_resourceMap[getResourceDataInfo->resourceHandle] == nullptr) {
|
|
||||||
result.result = KR_ERROR_NOT_MAPPED;
|
KRResource* resource = nullptr;
|
||||||
|
KrResult res = getMappedResource(getResourceDataInfo->resourceHandle, &resource);
|
||||||
|
if (res != KR_SUCCESS) {
|
||||||
|
result.result = res;
|
||||||
callback(result);
|
callback(result);
|
||||||
} else if (m_resourceMap[getResourceDataInfo->resourceHandle]->save(data)) {
|
return KR_SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (resource->save(data)) {
|
||||||
data.lock();
|
data.lock();
|
||||||
result.data = data.getStart();
|
result.data = data.getStart();
|
||||||
result.length = static_cast<size_t>(data.getSize());
|
result.length = static_cast<size_t>(data.getSize());
|
||||||
result.result = KR_SUCCESS;
|
result.result = KR_SUCCESS;
|
||||||
callback(result);
|
callback(result);
|
||||||
data.unlock();
|
data.unlock();
|
||||||
} else {
|
return KR_SUCCESS;
|
||||||
result.result = KR_ERROR_UNEXPECTED;
|
|
||||||
callback(result);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
result.result = KR_ERROR_UNEXPECTED;
|
||||||
|
callback(result);
|
||||||
|
|
||||||
return KR_SUCCESS;
|
return KR_SUCCESS;
|
||||||
}
|
}
|
||||||
@@ -502,40 +505,28 @@ KrResult KRContext::createBundle(const KrCreateBundleInfo* createBundleInfo)
|
|||||||
|
|
||||||
KrResult KRContext::moveToBundle(const KrMoveToBundleInfo* moveToBundleInfo)
|
KrResult KRContext::moveToBundle(const KrMoveToBundleInfo* moveToBundleInfo)
|
||||||
{
|
{
|
||||||
if (moveToBundleInfo->bundleHandle < 0 || moveToBundleInfo->bundleHandle >= m_resourceMapSize) {
|
KRResource* resource = nullptr;
|
||||||
return KR_ERROR_OUT_OF_BOUNDS;
|
KrResult res = getMappedResource(moveToBundleInfo->resourceHandle, &resource);
|
||||||
|
if (res != KR_SUCCESS) {
|
||||||
|
return res;
|
||||||
}
|
}
|
||||||
if (moveToBundleInfo->resourceHandle < 0 || moveToBundleInfo->resourceHandle >= m_resourceMapSize) {
|
KRBundle* bundle = nullptr;
|
||||||
return KR_ERROR_OUT_OF_BOUNDS;
|
res = getMappedResource<KRBundle>(moveToBundleInfo->bundleHandle, &bundle);
|
||||||
}
|
if (res != KR_SUCCESS) {
|
||||||
KRResource* resource = m_resourceMap[moveToBundleInfo->resourceHandle];
|
return res;
|
||||||
if (resource == nullptr) {
|
|
||||||
return KR_ERROR_NOT_MAPPED;
|
|
||||||
}
|
|
||||||
KRResource* bundleResource = m_resourceMap[moveToBundleInfo->bundleHandle];
|
|
||||||
if (bundleResource == nullptr) {
|
|
||||||
return KR_ERROR_NOT_MAPPED;
|
|
||||||
}
|
|
||||||
KRBundle* bundle = dynamic_cast<KRBundle*>(bundleResource);
|
|
||||||
if (bundle == nullptr) {
|
|
||||||
return KR_ERROR_INCORRECT_TYPE;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return resource->moveToBundle(bundle);
|
return resource->moveToBundle(bundle);
|
||||||
}
|
}
|
||||||
|
|
||||||
KrResult KRContext::compileAllShaders(const KrCompileAllShadersInfo* pCompileAllShadersInfo)
|
KrResult KRContext::compileAllShaders(const KrCompileAllShadersInfo* pCompileAllShadersInfo)
|
||||||
{
|
{
|
||||||
if (pCompileAllShadersInfo->bundleHandle < 0 || pCompileAllShadersInfo->bundleHandle >= m_resourceMapSize) {
|
KRBundle* bundle = nullptr;
|
||||||
return KR_ERROR_OUT_OF_BOUNDS;
|
KrResult res = getMappedResource<KRBundle>(pCompileAllShadersInfo->bundleHandle, &bundle);
|
||||||
}
|
if (res != KR_SUCCESS) {
|
||||||
KRResource* bundleResource = m_resourceMap[pCompileAllShadersInfo->bundleHandle];
|
return res;
|
||||||
if (bundleResource == nullptr) {
|
|
||||||
return KR_ERROR_NOT_MAPPED;
|
|
||||||
}
|
|
||||||
KRBundle* bundle = dynamic_cast<KRBundle*>(bundleResource);
|
|
||||||
if (bundle == nullptr) {
|
|
||||||
return KR_ERROR_INCORRECT_TYPE;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (pCompileAllShadersInfo->logHandle < -1 || pCompileAllShadersInfo->logHandle >= m_resourceMapSize) {
|
if (pCompileAllShadersInfo->logHandle < -1 || pCompileAllShadersInfo->logHandle >= m_resourceMapSize) {
|
||||||
return KR_ERROR_OUT_OF_BOUNDS;
|
return KR_ERROR_OUT_OF_BOUNDS;
|
||||||
}
|
}
|
||||||
@@ -562,12 +553,10 @@ KrResult KRContext::compileAllShaders(const KrCompileAllShadersInfo* pCompileAll
|
|||||||
|
|
||||||
KrResult KRContext::saveResource(const KrSaveResourceInfo* saveResourceInfo)
|
KrResult KRContext::saveResource(const KrSaveResourceInfo* saveResourceInfo)
|
||||||
{
|
{
|
||||||
if (saveResourceInfo->resourceHandle < 0 || saveResourceInfo->resourceHandle >= m_resourceMapSize) {
|
KRResource* resource = nullptr;
|
||||||
return KR_ERROR_OUT_OF_BOUNDS;
|
KrResult res = getMappedResource(saveResourceInfo->resourceHandle, &resource);
|
||||||
}
|
if (res != KR_SUCCESS) {
|
||||||
KRResource* resource = m_resourceMap[saveResourceInfo->resourceHandle];
|
return res;
|
||||||
if (resource == nullptr) {
|
|
||||||
return KR_ERROR_NOT_MAPPED;
|
|
||||||
}
|
}
|
||||||
if (resource->save(saveResourceInfo->pResourcePath)) {
|
if (resource->save(saveResourceInfo->pResourcePath)) {
|
||||||
return KR_SUCCESS;
|
return KR_SUCCESS;
|
||||||
@@ -751,37 +740,38 @@ KrResult KRContext::deleteNodeChildren(const KrDeleteNodeChildrenInfo* pDeleteNo
|
|||||||
return KR_ERROR_NOT_IMPLEMENTED;
|
return KR_ERROR_NOT_IMPLEMENTED;
|
||||||
}
|
}
|
||||||
|
|
||||||
KrResult KRContext::createNode(const KrCreateNodeInfo* pCreateNodeInfo)
|
KrResult KRContext::getMappedNode(KrSceneNodeMapIndex sceneNodeHandle, KRScene* scene, KRNode** node)
|
||||||
{
|
{
|
||||||
if (pCreateNodeInfo->sceneHandle < 0 || pCreateNodeInfo->sceneHandle >= m_resourceMapSize) {
|
*node = nullptr;
|
||||||
|
if (sceneNodeHandle < 0 || sceneNodeHandle >= m_nodeMapSize) {
|
||||||
return KR_ERROR_OUT_OF_BOUNDS;
|
return KR_ERROR_OUT_OF_BOUNDS;
|
||||||
}
|
}
|
||||||
KRResource* sceneResource = m_resourceMap[pCreateNodeInfo->sceneHandle];
|
if (sceneNodeHandle == KR_NULL_HANDLE) {
|
||||||
if (sceneResource == nullptr) {
|
*node = scene->getRootNode();
|
||||||
return KR_ERROR_NOT_MAPPED;
|
} else {
|
||||||
|
// TODO - Handle node deletions by deleting nodes from m_nodeMap
|
||||||
|
*node = m_nodeMap[sceneNodeHandle];
|
||||||
|
if (*node == nullptr) {
|
||||||
|
return KR_ERROR_NOT_FOUND;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
KRScene* scene = dynamic_cast<KRScene*>(sceneResource);
|
return KR_SUCCESS;
|
||||||
if (scene == nullptr) {
|
}
|
||||||
return KR_ERROR_INCORRECT_TYPE;
|
|
||||||
|
KrResult KRContext::createNode(const KrCreateNodeInfo* pCreateNodeInfo)
|
||||||
|
{
|
||||||
|
KRScene* scene = nullptr;
|
||||||
|
KrResult res = getMappedResource<KRScene>(pCreateNodeInfo->sceneHandle, &scene);
|
||||||
|
if (res != KR_SUCCESS) {
|
||||||
|
return res;
|
||||||
}
|
}
|
||||||
if (pCreateNodeInfo->newNodeHandle < 0 || pCreateNodeInfo->newNodeHandle >= m_nodeMapSize) {
|
if (pCreateNodeInfo->newNodeHandle < 0 || pCreateNodeInfo->newNodeHandle >= m_nodeMapSize) {
|
||||||
return KR_ERROR_OUT_OF_BOUNDS;
|
return KR_ERROR_OUT_OF_BOUNDS;
|
||||||
}
|
}
|
||||||
if (pCreateNodeInfo->relativeNodeHandle < 0 || pCreateNodeInfo->relativeNodeHandle >= m_nodeMapSize) {
|
|
||||||
return KR_ERROR_OUT_OF_BOUNDS;
|
|
||||||
}
|
|
||||||
if (pCreateNodeInfo->location < 0 || pCreateNodeInfo->location >= KR_SCENE_NODE_INSERT_MAX_ENUM) {
|
|
||||||
return KR_ERROR_OUT_OF_BOUNDS;
|
|
||||||
}
|
|
||||||
KRNode* relativeNode = nullptr;
|
KRNode* relativeNode = nullptr;
|
||||||
if (pCreateNodeInfo->relativeNodeHandle == KR_NULL_HANDLE) {
|
res = getMappedNode(pCreateNodeInfo->relativeNodeHandle, scene, &relativeNode);
|
||||||
relativeNode = scene->getRootNode();
|
if (res != KR_SUCCESS) {
|
||||||
} else {
|
return res;
|
||||||
// TODO - Handle node deletions by deleting nodes from m_nodeMap
|
|
||||||
relativeNode = m_nodeMap[pCreateNodeInfo->relativeNodeHandle];
|
|
||||||
if (relativeNode == nullptr) {
|
|
||||||
return KR_ERROR_NOT_FOUND;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (pCreateNodeInfo->location == KR_SCENE_NODE_INSERT_BEFORE ||
|
if (pCreateNodeInfo->location == KR_SCENE_NODE_INSERT_BEFORE ||
|
||||||
@@ -793,7 +783,7 @@ KrResult KRContext::createNode(const KrCreateNodeInfo* pCreateNodeInfo)
|
|||||||
}
|
}
|
||||||
|
|
||||||
KRNode* newNode = nullptr;
|
KRNode* newNode = nullptr;
|
||||||
KrResult res = KRNode::createNode(pCreateNodeInfo, scene, &newNode);
|
res = KRNode::createNode(pCreateNodeInfo, scene, &newNode);
|
||||||
if (res != KR_SUCCESS) {
|
if (res != KR_SUCCESS) {
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
@@ -903,3 +893,17 @@ KrResult KRContext::deleteWindowSurface(const KrDeleteWindowSurfaceInfo* deleteW
|
|||||||
|
|
||||||
return m_surfaceManager->destroy(surfaceHandle);
|
return m_surfaceManager->destroy(surfaceHandle);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
KrResult KRContext::getMappedResource(KrResourceMapIndex resourceHandle, KRResource** resource)
|
||||||
|
{
|
||||||
|
*resource = nullptr;
|
||||||
|
if (resourceHandle < 0 || resourceHandle >= m_resourceMapSize) {
|
||||||
|
return KR_ERROR_OUT_OF_BOUNDS;
|
||||||
|
}
|
||||||
|
*resource = m_resourceMap[resourceHandle];
|
||||||
|
if (*resource == nullptr) {
|
||||||
|
return KR_ERROR_NOT_MAPPED;
|
||||||
|
}
|
||||||
|
return KR_SUCCESS;
|
||||||
|
}
|
||||||
@@ -74,6 +74,7 @@ public:
|
|||||||
KRContext(const KrInitializeInfo* initializeInfo);
|
KRContext(const KrInitializeInfo* initializeInfo);
|
||||||
~KRContext();
|
~KRContext();
|
||||||
|
|
||||||
|
// -=-=-=- Begin: Public API Entry Points -=-=-=-
|
||||||
KrResult createWindowSurface(const KrCreateWindowSurfaceInfo* createWindowSurfaceInfo);
|
KrResult createWindowSurface(const KrCreateWindowSurfaceInfo* createWindowSurfaceInfo);
|
||||||
KrResult deleteWindowSurface(const KrDeleteWindowSurfaceInfo* deleteWindowSurfaceInfo);
|
KrResult deleteWindowSurface(const KrDeleteWindowSurfaceInfo* deleteWindowSurfaceInfo);
|
||||||
|
|
||||||
@@ -97,7 +98,29 @@ public:
|
|||||||
KrResult deleteNodeChildren(const KrDeleteNodeChildrenInfo* pDeleteNodeChildrenInfo);
|
KrResult deleteNodeChildren(const KrDeleteNodeChildrenInfo* pDeleteNodeChildrenInfo);
|
||||||
KrResult createNode(const KrCreateNodeInfo* pCreateNodeInfo);
|
KrResult createNode(const KrCreateNodeInfo* pCreateNodeInfo);
|
||||||
KrResult updateNode(const KrUpdateNodeInfo* pUpdateNodeInfo);
|
KrResult updateNode(const KrUpdateNodeInfo* pUpdateNodeInfo);
|
||||||
|
// -=-=-=- End: Public API Entry Points -=-=-=-
|
||||||
|
|
||||||
|
// -=-=-=- Start: Helper functions for Public API Entry Points
|
||||||
|
KrResult getMappedNode(KrSceneNodeMapIndex sceneNodeHandle, KRScene* scene, KRNode** node);
|
||||||
|
KrResult getMappedResource(KrResourceMapIndex resourceHandle, KRResource** resource);
|
||||||
|
|
||||||
|
template<class T> KrResult getMappedResource(KrResourceMapIndex resourceHandle, T** resource)
|
||||||
|
{
|
||||||
|
static_assert(std::is_base_of<KRResource, T>::value, "KRContext::getMappedResource called for class that is not a KRResource subclass");
|
||||||
|
*resource = nullptr;
|
||||||
|
|
||||||
|
KRResource* uncastResource = nullptr;
|
||||||
|
KrResult res = getMappedResource(resourceHandle, &uncastResource);
|
||||||
|
if (res != KR_SUCCESS) {
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
*resource = dynamic_cast<T*>(uncastResource);
|
||||||
|
if (*resource == nullptr) {
|
||||||
|
return KR_ERROR_INCORRECT_TYPE;
|
||||||
|
}
|
||||||
|
return KR_SUCCESS;
|
||||||
|
}
|
||||||
|
// -=-=-=- End: Helper functions for Public API Entry Points
|
||||||
|
|
||||||
KRResource* loadResource(const std::string& file_name, KRDataBlock* data);
|
KRResource* loadResource(const std::string& file_name, KRDataBlock* data);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user