computerscare-vcv-modules

computerscare modules for VCV Rack
Log | Files | Refs

commit f8e7b10e8b37a7f329f355d454e7aebed4b5cb5b
parent 1d7bb07f23e7d1d8517da72ef3f86339734e86ea
Author: Adam M <[email protected]>
Date:   Mon, 22 Mar 2021 18:15:24 -0500

Dont crash if custom blank image directory doesnt exist

Diffstat:
Mplugin.json | 2+-
Msrc/ComputerscareBlank.cpp | 63++++++++++++++++++++++++++++++++++++---------------------------
Msrc/animatedGif.hpp | 9+++++----
3 files changed, 42 insertions(+), 32 deletions(-)

diff --git a/plugin.json b/plugin.json @@ -1,6 +1,6 @@ { "slug": "computerscare", - "version": "1.4.1", + "version": "1.4.2", "name": "computerscare", "brand": "computerscare", "author": "computerscare", diff --git a/src/ComputerscareBlank.cpp b/src/ComputerscareBlank.cpp @@ -346,40 +346,40 @@ struct ComputerscareBlank : ComputerscareMenuParamModule { jsonFlag = false; } void setContainingDirectory(int index = 0) { - std::string dir = rack::string::directory(paths[index]); std::string currentImageFullpath; parentDirectory = dir; int imageIndex = 0;; + struct dirent* dirp = NULL; DIR* rep = NULL; - rep = opendir(dir.c_str()); - catalog.clear(); //fichier.clear(); - while ((dirp = readdir(rep)) != NULL) { - std::string name = dirp->d_name; - - std::size_t found = name.find(".gif", name.length() - 5); - if (found == std::string::npos) found = name.find(".GIF", name.length() - 5); - if (found == std::string::npos) found = name.find(".png", name.length() - 5); - if (found == std::string::npos) found = name.find(".PNG", name.length() - 5); - if (found == std::string::npos) found = name.find(".jpg", name.length() - 5); - if (found == std::string::npos) found = name.find(".JPG", name.length() - 5); - if (found == std::string::npos) found = name.find(".jpeg", name.length() - 5); - if (found == std::string::npos) found = name.find(".JPEG", name.length() - 5); - if (found == std::string::npos) found = name.find(".bmp", name.length() - 5); - if (found == std::string::npos) found = name.find(".BMP", name.length() - 5); - if (found != std::string::npos) { - currentImageFullpath = parentDirectory + "/" + name; - catalog.push_back(currentImageFullpath); - if (currentImageFullpath == paths[index]) { - fileIndexInCatalog = imageIndex; + if (rep) { + while ((dirp = readdir(rep)) != NULL) { + std::string name = dirp->d_name; + + std::size_t found = name.find(".gif", name.length() - 5); + if (found == std::string::npos) found = name.find(".GIF", name.length() - 5); + if (found == std::string::npos) found = name.find(".png", name.length() - 5); + if (found == std::string::npos) found = name.find(".PNG", name.length() - 5); + if (found == std::string::npos) found = name.find(".jpg", name.length() - 5); + if (found == std::string::npos) found = name.find(".JPG", name.length() - 5); + if (found == std::string::npos) found = name.find(".jpeg", name.length() - 5); + if (found == std::string::npos) found = name.find(".JPEG", name.length() - 5); + if (found == std::string::npos) found = name.find(".bmp", name.length() - 5); + if (found == std::string::npos) found = name.find(".BMP", name.length() - 5); + if (found != std::string::npos) { + currentImageFullpath = parentDirectory + "/" + name; + catalog.push_back(currentImageFullpath); + if (currentImageFullpath == paths[index]) { + fileIndexInCatalog = imageIndex; + } + //DEBUG("we got gif:%s", name.c_str()); + imageIndex++; } - //DEBUG("we got gif:%s", name.c_str()); - imageIndex++; } } numFilesInCatalog = catalog.size(); @@ -856,6 +856,7 @@ struct tPNGDisplay : TBase { std::string path = "empty"; int img = 0; int currentFrame = -1; + bool missingOrBroken = false; AnimatedGifBuddy gifBuddy; tPNGDisplay() { @@ -895,9 +896,14 @@ struct tPNGDisplay : TBase { DEBUG("path not module path"); DEBUG("path: %s, modulePath:%s", path.c_str(), modulePath.c_str()); gifBuddy = AnimatedGifBuddy(args.vg, modulePath.c_str()); + if (gifBuddy.getImageStatus() == 3) { std::string badGifPath = asset::plugin(pluginInstance, "res/broken-file.gif"); gifBuddy = AnimatedGifBuddy(args.vg, badGifPath.c_str()); + missingOrBroken = true; + } + else { + missingOrBroken = false; } img = gifBuddy.getHandle(); int numImageFrames = gifBuddy.getFrameCount(); @@ -906,14 +912,18 @@ struct tPNGDisplay : TBase { //if this check isnt performed, windows crashes with non-gifs due to //the call to vector insert - if(numImageFrames > 1) { + if (numImageFrames > 1) { blankModule->setFrameCount(gifBuddy.getFrameCount()); blankModule->setFrameDelays(gifBuddy.getAllFrameDelaysSeconds()); blankModule->setTotalGifDuration(gifBuddy.getTotalGifDuration()); blankModule->setTotalGifDurationIfInPingPongMode(gifBuddy.getPingPongGifDuration()); blankModule->setFrameDelay(gifBuddy.getSecondsDelay(0)); } - blankModule->setImageStatus(gifBuddy.getImageStatus()); + + int imageStatus = gifBuddy.getImageStatus(); + blankModule->setImageStatus(imageStatus); + + //couldnt load file blankModule->setContainingDirectory(); blankModule->setReady(true); @@ -928,7 +938,7 @@ struct tPNGDisplay : TBase { 3) loaded from JSON dont reset zooms */ - if (blankModule->jsonFlag ) { + if (blankModule->jsonFlag && !missingOrBroken) { //dont want to reset zooms if loading from json //unsure of another way to distinguish (1) from (3) //other than this janky flag @@ -939,7 +949,6 @@ struct tPNGDisplay : TBase { path = modulePath; - } if (blankModule->imageFitEnum != lastEnum && lastEnum != -1) { diff --git a/src/animatedGif.hpp b/src/animatedGif.hpp @@ -186,10 +186,11 @@ struct AnimatedGifBuddy { printf("image status:%i\n", imageStatus); return 0; } - updateFrameDelaysSeconds(); - image = nvgCreateImageRGBA(ctx, w, h, imageFlags, img); - - initialized = true; + else { + updateFrameDelaysSeconds(); + image = nvgCreateImageRGBA(ctx, w, h, imageFlags, img); + initialized = true; + } return image; } void displayGifFrame(NVGcontext* ctx, int frameNumber) {