/[svn]/libgig/trunk/src/gig.cpp
ViewVC logotype

Diff of /libgig/trunk/src/gig.cpp

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 3722 by schoenebeck, Wed Jan 22 15:37:09 2020 UTC revision 3903 by schoenebeck, Wed May 12 18:31:25 2021 UTC
# Line 3807  namespace { Line 3807  namespace {
3807       * @throws gig::Exception if requested zone could not be deleted       * @throws gig::Exception if requested zone could not be deleted
3808       */       */
3809      void Region::DeleteDimensionZone(dimension_t type, int zone) {      void Region::DeleteDimensionZone(dimension_t type, int zone) {
3810            if (!Dimensions)
3811                throw gig::Exception("Could not delete dimension zone, because there is no dimension at all.");
3812          dimension_def_t* oldDef = GetDimensionDefinition(type);          dimension_def_t* oldDef = GetDimensionDefinition(type);
3813          if (!oldDef)          if (!oldDef)
3814              throw gig::Exception("Could not delete dimension zone, no such dimension of given type");              throw gig::Exception("Could not delete dimension zone, no such dimension of given type");
# Line 3835  namespace { Line 3837  namespace {
3837          // requested by the arguments of this method call) to the temporary          // requested by the arguments of this method call) to the temporary
3838          // region, and don't use Region::CopyAssign() here for this task, since          // region, and don't use Region::CopyAssign() here for this task, since
3839          // it would also alter fast lookup helper variables here and there          // it would also alter fast lookup helper variables here and there
3840          dimension_def_t newDef;          dimension_def_t newDef = {};
3841          for (int i = 0; i < Dimensions; ++i) {          for (int i = 0; i < Dimensions; ++i) {
3842              dimension_def_t def = pDimensionDefinitions[i]; // copy, don't reference              dimension_def_t def = pDimensionDefinitions[i]; // copy, don't reference
3843              // is this the dimension requested by the method arguments? ...              // is this the dimension requested by the method arguments? ...
# Line 3846  namespace { Line 3848  namespace {
3848              }              }
3849              tempRgn->AddDimension(&def);              tempRgn->AddDimension(&def);
3850          }          }
3851            // silence clang sanitizer warning
3852            if (newDef.dimension == dimension_none)
3853                throw gig::Exception("Unexpected internal failure resolving dimension in DeleteDimensionZone() [this is a bug].");
3854    
3855          // find the dimension index in the tempRegion which is the dimension          // find the dimension index in the tempRegion which is the dimension
3856          // type passed to this method (paranoidly expecting different order)          // type passed to this method (paranoidly expecting different order)
# Line 4594  namespace { Line 4599  namespace {
4599              Name.resize(nameSize, ' ');              Name.resize(nameSize, ' ');
4600              for (int i = 0; i < nameSize; ++i)              for (int i = 0; i < nameSize; ++i)
4601                  Name[i] = ckScri->ReadUint8();                  Name[i] = ckScri->ReadUint8();
4602                // check if an uuid was already stored along with this script
4603                if (headerSize >= 6*sizeof(int32_t) + nameSize + 16) { // yes ...
4604                    for (uint i = 0; i < 16; ++i) {
4605                        Uuid[i] = ckScri->ReadUint8();
4606                    }
4607                } else { // no uuid yet, generate one now ...
4608                    GenerateUuid();
4609                }
4610              // to handle potential future extensions of the header              // to handle potential future extensions of the header
4611              ckScri->SetPos(sizeof(int32_t) + headerSize);              ckScri->SetPos(sizeof(int32_t) + headerSize);
4612              // read actual script data              // read actual script data
# Line 4608  namespace { Line 4621  namespace {
4621              Bypass   = false;              Bypass   = false;
4622              crc      = 0;              crc      = 0;
4623              Name     = "Unnamed Script";              Name     = "Unnamed Script";
4624                GenerateUuid();
4625          }          }
4626      }      }
4627    
# Line 4662  namespace { Line 4676  namespace {
4676          __calculateCRC(&data[0], data.size(), crc);          __calculateCRC(&data[0], data.size(), crc);
4677          __finalizeCRC(crc);          __finalizeCRC(crc);
4678          // make sure chunk exists and has the required size          // make sure chunk exists and has the required size
4679          const file_offset_t chunkSize = (file_offset_t) 7*sizeof(int32_t) + Name.size() + data.size();          const file_offset_t chunkSize =
4680                (file_offset_t) 7*sizeof(int32_t) + Name.size() + 16 + data.size();
4681          if (!pChunk) pChunk = pGroup->pList->AddSubChunk(CHUNK_ID_SCRI, chunkSize);          if (!pChunk) pChunk = pGroup->pList->AddSubChunk(CHUNK_ID_SCRI, chunkSize);
4682          else pChunk->Resize(chunkSize);          else pChunk->Resize(chunkSize);
4683          // fill the chunk data to be written to disk          // fill the chunk data to be written to disk
4684          uint8_t* pData = (uint8_t*) pChunk->LoadChunkData();          uint8_t* pData = (uint8_t*) pChunk->LoadChunkData();
4685          int pos = 0;          int pos = 0;
4686          store32(&pData[pos], uint32_t(6*sizeof(int32_t) + Name.size())); // total header size          store32(&pData[pos], uint32_t(6*sizeof(int32_t) + Name.size() + 16)); // total header size
4687          pos += sizeof(int32_t);          pos += sizeof(int32_t);
4688          store32(&pData[pos], Compression);          store32(&pData[pos], Compression);
4689          pos += sizeof(int32_t);          pos += sizeof(int32_t);
# Line 4684  namespace { Line 4699  namespace {
4699          pos += sizeof(int32_t);          pos += sizeof(int32_t);
4700          for (int i = 0; i < Name.size(); ++i, ++pos)          for (int i = 0; i < Name.size(); ++i, ++pos)
4701              pData[pos] = Name[i];              pData[pos] = Name[i];
4702            for (int i = 0; i < 16; ++i, ++pos)
4703                pData[pos] = Uuid[i];
4704          for (int i = 0; i < data.size(); ++i, ++pos)          for (int i = 0; i < data.size(); ++i, ++pos)
4705              pData[pos] = data[i];              pData[pos] = data[i];
4706      }      }
4707    
4708      /**      /**
4709         * Generate a new Universally Unique Identifier (UUID) for this script.
4710         */
4711        void Script::GenerateUuid() {
4712            DLS::dlsid_t dlsid;
4713            DLS::Resource::GenerateDLSID(&dlsid);
4714            Uuid[0]  = dlsid.ulData1       & 0xff;
4715            Uuid[1]  = dlsid.ulData1 >>  8 & 0xff;
4716            Uuid[2]  = dlsid.ulData1 >> 16 & 0xff;
4717            Uuid[3]  = dlsid.ulData1 >> 24 & 0xff;
4718            Uuid[4]  = dlsid.usData2       & 0xff;
4719            Uuid[5]  = dlsid.usData2 >>  8 & 0xff;
4720            Uuid[6]  = dlsid.usData3       & 0xff;
4721            Uuid[7]  = dlsid.usData3 >>  8 & 0xff;
4722            Uuid[8]  = dlsid.abData[0];
4723            Uuid[9]  = dlsid.abData[1];
4724            Uuid[10] = dlsid.abData[2];
4725            Uuid[11] = dlsid.abData[3];
4726            Uuid[12] = dlsid.abData[4];
4727            Uuid[13] = dlsid.abData[5];
4728            Uuid[14] = dlsid.abData[6];
4729            Uuid[15] = dlsid.abData[7];
4730        }
4731    
4732        /**
4733       * Move this script from its current ScriptGroup to another ScriptGroup       * Move this script from its current ScriptGroup to another ScriptGroup
4734       * given by @a pGroup.       * given by @a pGroup.
4735       *       *
# Line 4958  namespace { Line 4999  namespace {
4999          // own gig format extensions          // own gig format extensions
5000          RIFF::List* lst3LS = insList->GetSubList(LIST_TYPE_3LS);          RIFF::List* lst3LS = insList->GetSubList(LIST_TYPE_3LS);
5001          if (lst3LS) {          if (lst3LS) {
5002                // script slots (that is references to instrument scripts)
5003              RIFF::Chunk* ckSCSL = lst3LS->GetSubChunk(CHUNK_ID_SCSL);              RIFF::Chunk* ckSCSL = lst3LS->GetSubChunk(CHUNK_ID_SCSL);
5004              if (ckSCSL) {              if (ckSCSL) {
5005                  ckSCSL->SetPos(0);                  ckSCSL->SetPos(0);
# Line 4977  namespace { Line 5019  namespace {
5019                      }                      }
5020                  }                  }
5021              }              }
5022    
5023                // overridden script 'patch' variables
5024                RIFF::Chunk* ckSCPV = lst3LS->GetSubChunk(CHUNK_ID_SCPV);
5025                if (ckSCPV) {
5026                    ckSCPV->SetPos(0);
5027    
5028                    int nScripts = ckSCPV->ReadUint32();
5029                    for (int iScript = 0; iScript < nScripts; ++iScript) {
5030                        _UUID uuid;
5031                        for (int i = 0; i < 16; ++i)
5032                            uuid[i] = ckSCPV->ReadUint8();
5033                        uint slot = ckSCPV->ReadUint32();
5034                        ckSCPV->ReadUint32(); // unused, reserved 32 bit
5035                        int nVars = ckSCPV->ReadUint32();
5036                        for (int iVar = 0; iVar < nVars; ++iVar) {
5037                            uint8_t type = ckSCPV->ReadUint8();
5038                            ckSCPV->ReadUint8();  // unused, reserved byte
5039                            int blobSize = ckSCPV->ReadUint16();
5040                            RIFF::file_offset_t pos = ckSCPV->GetPos();
5041                            // assuming 1st bit is set in 'type', otherwise blob not
5042                            // supported for decoding
5043                            if (type & 1) {
5044                                String name, value;
5045                                int len = ckSCPV->ReadUint16();
5046                                for (int i = 0; i < len; ++i)
5047                                    name += (char) ckSCPV->ReadUint8();
5048                                len = ckSCPV->ReadUint16();
5049                                for (int i = 0; i < len; ++i)
5050                                    value += (char) ckSCPV->ReadUint8();
5051                                if (!name.empty()) // 'name' should never be empty, but just to be sure
5052                                    scriptVars[uuid][slot][name] = value;
5053                            }
5054                            // also for potential future extensions: seek forward
5055                            // according to blob size
5056                            ckSCPV->SetPos(pos + blobSize);
5057                        }
5058                    }
5059                }
5060          }          }
5061    
5062          if (pProgress)          if (pProgress)
# Line 5068  namespace { Line 5148  namespace {
5148    
5149             RIFF::List* lst3LS = pCkInstrument->GetSubList(LIST_TYPE_3LS);             RIFF::List* lst3LS = pCkInstrument->GetSubList(LIST_TYPE_3LS);
5150             if (!lst3LS) lst3LS = pCkInstrument->AddSubList(LIST_TYPE_3LS);             if (!lst3LS) lst3LS = pCkInstrument->AddSubList(LIST_TYPE_3LS);
5151    
5152               // save script slots (that is references to instrument scripts)
5153             const int slotCount = (int) pScriptRefs->size();             const int slotCount = (int) pScriptRefs->size();
5154             const int headerSize = 3 * sizeof(uint32_t);             const int headerSize = 3 * sizeof(uint32_t);
5155             const int slotSize  = 2 * sizeof(uint32_t);             const int slotSize  = 2 * sizeof(uint32_t);
# Line 5092  namespace { Line 5174  namespace {
5174                 store32(&pData[pos], (*pScriptRefs)[i].bypass ? 1 : 0);                 store32(&pData[pos], (*pScriptRefs)[i].bypass ? 1 : 0);
5175                 pos += sizeof(uint32_t);                 pos += sizeof(uint32_t);
5176             }             }
5177    
5178               // save overridden script 'patch' variables ...
5179    
5180               // the actual 'scriptVars' member variable might contain variables of
5181               // scripts which are currently no longer assigned to any script slot
5182               // of this instrument, we need to get rid of these variables here to
5183               // prevent saving those persistently, however instead of touching the
5184               // member variable 'scriptVars' directly, rather strip a separate
5185               // copy such that the overridden values are not lost during an
5186               // instrument editor session (i.e. if script might be re-assigned)
5187               _VarsByScript vars = stripScriptVars();
5188               if (!vars.empty()) {
5189                   // determine total size required for 'SCPV' RIFF chunk, and the
5190                   // total amount of scripts being overridden (the latter is
5191                   // required because a script might be used on several script
5192                   // slots, hence vars.size() could then not be used here instead)
5193                   size_t totalChunkSize = 4;
5194                   size_t totalScriptsOverridden = 0;
5195                   for (const auto& script : vars) {
5196                       for (const auto& slot : script.second) {
5197                           totalScriptsOverridden++;
5198                           totalChunkSize += 16 + 4 + 4 + 4;
5199                           for (const auto& var : slot.second) {
5200                               totalChunkSize += 4 + 2 + var.first.length() +
5201                                                     2 + var.second.length();
5202                           }
5203                       }
5204                   }
5205    
5206                   // ensure 'SCPV' RIFF chunk exists (with required size)
5207                   RIFF::Chunk* ckSCPV = lst3LS->GetSubChunk(CHUNK_ID_SCPV);
5208                   if (!ckSCPV) ckSCPV = lst3LS->AddSubChunk(CHUNK_ID_SCPV, totalChunkSize);
5209                   else ckSCPV->Resize(totalChunkSize);
5210    
5211                   // store the actual data to 'SCPV' RIFF chunk
5212                   uint8_t* pData = (uint8_t*) ckSCPV->LoadChunkData();
5213                   int pos = 0;
5214                   store32(&pData[pos], (uint32_t) totalScriptsOverridden); // scripts count
5215                   pos += 4;
5216                   for (const auto& script : vars) {
5217                       for (const auto& slot : script.second) {
5218                           for (int i = 0; i < 16; ++i)
5219                               pData[pos+i] = script.first[i]; // uuid
5220                           pos += 16;
5221                           store32(&pData[pos], (uint32_t) slot.first); // slot index
5222                           pos += 4;
5223                           store32(&pData[pos], (uint32_t) 0); // unused, reserved 32 bit
5224                           pos += 4;
5225                           store32(&pData[pos], (uint32_t) slot.second.size()); // variables count
5226                           pos += 4;
5227                           for (const auto& var : slot.second) {
5228                               pData[pos++] = 1; // type
5229                               pData[pos++] = 0; // reserved byte
5230                               store16(&pData[pos], 2 + var.first.size() + 2 + var.second.size()); // blob size
5231                               pos += 2;
5232                               store16(&pData[pos], var.first.size()); // variable name length
5233                               pos += 2;
5234                               for (int i = 0; i < var.first.size(); ++i)
5235                                   pData[pos++] = var.first[i];
5236                               store16(&pData[pos], var.second.size()); // variable value length
5237                               pos += 2;
5238                               for (int i = 0; i < var.second.size(); ++i)
5239                                   pData[pos++] = var.second[i];
5240                           }
5241                       }
5242                   }
5243               } else {
5244                   // no script variable overridden by this instrument, so get rid
5245                   // of 'SCPV' RIFF chunk (if any)
5246                   RIFF::Chunk* ckSCPV = lst3LS->GetSubChunk(CHUNK_ID_SCPV);
5247                   if (ckSCPV) lst3LS->DeleteSubChunk(ckSCPV);
5248               }
5249         } else {         } else {
5250             // no script slots, so get rid of any LS custom RIFF chunks (if any)             // no script slots, so get rid of any LS custom RIFF chunks (if any)
5251             RIFF::List* lst3LS = pCkInstrument->GetSubList(LIST_TYPE_3LS);             RIFF::List* lst3LS = pCkInstrument->GetSubList(LIST_TYPE_3LS);
# Line 5516  namespace { Line 5670  namespace {
5670              scriptPoolFileOffsets.at(index).bypass = bBypass;              scriptPoolFileOffsets.at(index).bypass = bBypass;
5671      }      }
5672    
5673        /// type cast (by copy) uint8_t[16] -> std::array<uint8_t,16>
5674        inline std::array<uint8_t,16> _UUIDFromCArray(const uint8_t* pData) {
5675            std::array<uint8_t,16> uuid;
5676            memcpy(&uuid[0], pData, 16);
5677            return uuid;
5678        }
5679    
5680        /**
5681         * Returns true if this @c Instrument has any script slot which references
5682         * the @c Script identified by passed @p uuid.
5683         */
5684        bool Instrument::ReferencesScriptWithUuid(const _UUID& uuid) {
5685            const uint nSlots = ScriptSlotCount();
5686            for (uint iSlot = 0; iSlot < nSlots; ++iSlot)
5687                if (_UUIDFromCArray(&GetScriptOfSlot(iSlot)->Uuid[0]) == uuid)
5688                    return true;
5689            return false;
5690        }
5691    
5692        /** @brief Checks whether a certain script 'patch' variable value is set.
5693         *
5694         * Returns @c true if the initial value for the requested script variable is
5695         * currently overridden by this instrument.
5696         *
5697         * @remarks Real-time instrument scripts allow to declare special 'patch'
5698         * variables, which essentially behave like regular variables of their data
5699         * type, however their initial value may optionally be overridden on a per
5700         * instrument basis. That allows to share scripts between instruments while
5701         * still being able to fine tune certain aspects of the script for each
5702         * instrument individually.
5703         *
5704         * @note This is an own format extension which did not exist i.e. in the
5705         * GigaStudio 4 software. It will currently only work with LinuxSampler and
5706         * Gigedit.
5707         *
5708         * @param slot - script slot index of the variable to be retrieved
5709         * @param variable - name of the 'patch' variable in that script
5710         */
5711        bool Instrument::IsScriptPatchVariableSet(int slot, String variable) {
5712            if (variable.empty()) return false;
5713            Script* script = GetScriptOfSlot(slot);
5714            if (!script) return false;
5715            const _UUID uuid = _UUIDFromCArray(&script->Uuid[0]);
5716            if (!scriptVars.count(uuid)) return false;
5717            const _VarsBySlot& slots = scriptVars.find(uuid)->second;
5718            if (slots.empty()) return false;
5719            if (slots.count(slot))
5720                return slots.find(slot)->second.count(variable);
5721            else
5722                return slots.begin()->second.count(variable);
5723        }
5724    
5725        /** @brief Get all overridden script 'patch' variables.
5726         *
5727         * Returns map of key-value pairs reflecting all patch variables currently
5728         * being overridden by this instrument for the given script @p slot, where
5729         * key is the variable name and value is the hereby currently overridden
5730         * value for that variable.
5731         *
5732         * @remarks Real-time instrument scripts allow to declare special 'patch'
5733         * variables, which essentially behave like regular variables of their data
5734         * type, however their initial value may optionally be overridden on a per
5735         * instrument basis. That allows to share scripts between instruments while
5736         * still being able to fine tune certain aspects of the script for each
5737         * instrument individually.
5738         *
5739         * @note This is an own format extension which did not exist i.e. in the
5740         * GigaStudio 4 software. It will currently only work with LinuxSampler and
5741         * Gigedit.
5742         *
5743         * @param slot - script slot index of the variable to be retrieved
5744         */
5745        std::map<String,String> Instrument::GetScriptPatchVariables(int slot) {
5746            Script* script = GetScriptOfSlot(slot);
5747            if (!script) return std::map<String,String>();
5748            const _UUID uuid = _UUIDFromCArray(&script->Uuid[0]);
5749            if (!scriptVars.count(uuid)) return std::map<String,String>();
5750            const _VarsBySlot& slots = scriptVars.find(uuid)->second;
5751            if (slots.empty()) return std::map<String,String>();
5752            const _PatchVars& vars =
5753                (slots.count(slot)) ?
5754                    slots.find(slot)->second : slots.begin()->second;
5755            return vars;
5756        }
5757    
5758        /** @brief Get overridden initial value for 'patch' variable.
5759         *
5760         * Returns current initial value for the requested script variable being
5761         * overridden by this instrument.
5762         *
5763         * @remarks Real-time instrument scripts allow to declare special 'patch'
5764         * variables, which essentially behave like regular variables of their data
5765         * type, however their initial value may optionally be overridden on a per
5766         * instrument basis. That allows to share scripts between instruments while
5767         * still being able to fine tune certain aspects of the script for each
5768         * instrument individually.
5769         *
5770         * @note This is an own format extension which did not exist i.e. in the
5771         * GigaStudio 4 software. It will currently only work with LinuxSampler and
5772         * Gigedit.
5773         *
5774         * @param slot - script slot index of the variable to be retrieved
5775         * @param variable - name of the 'patch' variable in that script
5776         */
5777        String Instrument::GetScriptPatchVariable(int slot, String variable) {
5778            std::map<String,String> vars = GetScriptPatchVariables(slot);
5779            return (vars.count(variable)) ? vars.find(variable)->second : "";
5780        }
5781    
5782        /** @brief Override initial value for 'patch' variable.
5783         *
5784         * Overrides initial value for the requested script variable for this
5785         * instrument with the passed value.
5786         *
5787         * @remarks Real-time instrument scripts allow to declare special 'patch'
5788         * variables, which essentially behave like regular variables of their data
5789         * type, however their initial value may optionally be overridden on a per
5790         * instrument basis. That allows to share scripts between instruments while
5791         * still being able to fine tune certain aspects of the script for each
5792         * instrument individually.
5793         *
5794         * @note This is an own format extension which did not exist i.e. in the
5795         * GigaStudio 4 software. It will currently only work with LinuxSampler and
5796         * Gigedit.
5797         *
5798         * @param slot - script slot index of the variable to be set
5799         * @param variable - name of the 'patch' variable in that script
5800         * @param value - overridden initial value for that script variable
5801         * @throws gig::Exception if given script @p slot index is invalid or given
5802         *         @p variable name is empty
5803         */
5804        void Instrument::SetScriptPatchVariable(int slot, String variable, String value) {
5805            if (variable.empty())
5806                throw Exception("Variable name must not be empty");
5807            Script* script = GetScriptOfSlot(slot);
5808            if (!script)
5809                throw Exception("No script slot with index " + ToString(slot));
5810            const _UUID uuid = _UUIDFromCArray(&script->Uuid[0]);
5811            scriptVars[uuid][slot][variable] = value;
5812        }
5813    
5814        /** @brief Drop overridden initial value(s) for 'patch' variable(s).
5815         *
5816         * Reverts initial value(s) for requested script variable(s) back to their
5817         * default initial value(s) defined in the script itself.
5818         *
5819         * Both arguments of this method are optional. The most obvious use case of
5820         * this method would be passing a valid script @p slot index and a
5821         * (non-emtpy string as) @p variable name to this method, which would cause
5822         * that single variable to be unset for that specific script slot (on this
5823         * @c Instrument level).
5824         *
5825         * Not passing a value (or @c -1 for @p slot and/or empty string for
5826         * @p variable) means 'wildcard'. So accordingly absence of argument(s) will
5827         * cause all variables and/or for all script slots being unset. Hence this
5828         * method serves 2^2 = 4 possible use cases in total and accordingly covers
5829         * 4 different behaviours in one method.
5830         *
5831         * @remarks Real-time instrument scripts allow to declare special 'patch'
5832         * variables, which essentially behave like regular variables of their data
5833         * type, however their initial value may optionally be overridden on a per
5834         * instrument basis. That allows to share scripts between instruments while
5835         * still being able to fine tune certain aspects of the script for each
5836         * instrument individually.
5837         *
5838         * @note This is an own format extension which did not exist i.e. in the
5839         * GigaStudio 4 software. It will currently only work with LinuxSampler and
5840         * Gigedit.
5841         *
5842         * @param slot - script slot index of the variable to be unset
5843         * @param variable - name of the 'patch' variable in that script
5844         */
5845        void Instrument::UnsetScriptPatchVariable(int slot, String variable) {
5846            Script* script = GetScriptOfSlot(slot);
5847    
5848            // option 1: unset a particular variable of one particular script slot
5849            if (slot != -1 && !variable.empty()) {
5850                if (!script) return;
5851                const _UUID uuid = _UUIDFromCArray(&script->Uuid[0]);
5852                if (!scriptVars.count(uuid)) return;
5853                if (!scriptVars[uuid].count(slot)) return;
5854                if (scriptVars[uuid][slot].count(variable))
5855                    scriptVars[uuid][slot].erase(
5856                        scriptVars[uuid][slot].find(variable)
5857                    );
5858                if (scriptVars[uuid][slot].empty())
5859                    scriptVars[uuid].erase( scriptVars[uuid].find(slot) );
5860                if (scriptVars[uuid].empty())
5861                    scriptVars.erase( scriptVars.find(uuid) );
5862                return;
5863            }
5864    
5865            // option 2: unset all variables of all script slots
5866            if (slot == -1 && variable.empty()) {
5867                scriptVars.clear();
5868                return;
5869            }
5870    
5871            // option 3: unset all variables of one particular script slot only
5872            if (slot != -1) {
5873                if (!script) return;
5874                const _UUID uuid = _UUIDFromCArray(&script->Uuid[0]);
5875                if (scriptVars.count(uuid))
5876                    scriptVars.erase( scriptVars.find(uuid) );
5877                return;
5878            }
5879    
5880            // option 4: unset a particular variable of all script slots
5881            _VarsByScript::iterator itScript = scriptVars.begin();
5882            _VarsByScript::iterator endScript = scriptVars.end();
5883            while (itScript != endScript) {
5884                _VarsBySlot& slots = itScript->second;
5885                _VarsBySlot::iterator itSlot = slots.begin();
5886                _VarsBySlot::iterator endSlot = slots.end();
5887                while (itSlot != endSlot) {
5888                    _PatchVars& vars = itSlot->second;
5889                    if (vars.count(variable))
5890                        vars.erase( vars.find(variable) );
5891                    if (vars.empty())
5892                        slots.erase(itSlot++); // postfix increment to avoid iterator invalidation
5893                    else
5894                        ++itSlot;
5895                }
5896                if (slots.empty())
5897                    scriptVars.erase(itScript++); // postfix increment to avoid iterator invalidation
5898                else
5899                    ++itScript;
5900            }
5901        }
5902    
5903        /**
5904         * Returns stripped version of member variable @c scriptVars, where scripts
5905         * no longer referenced by this @c Instrument are filtered out, and so are
5906         * variables of meanwhile obsolete slots (i.e. a script still being
5907         * referenced, but previously overridden on a script slot which either no
5908         * longer exists or is hosting another script now).
5909         */
5910        Instrument::_VarsByScript Instrument::stripScriptVars() {
5911            _VarsByScript vars;
5912            _VarsByScript::const_iterator itScript = scriptVars.begin();
5913            _VarsByScript::const_iterator endScript = scriptVars.end();
5914            for (; itScript != endScript; ++itScript) {
5915                const _UUID& uuid = itScript->first;
5916                if (!ReferencesScriptWithUuid(uuid))
5917                    continue;
5918                const _VarsBySlot& slots = itScript->second;
5919                _VarsBySlot::const_iterator itSlot = slots.begin();
5920                _VarsBySlot::const_iterator endSlot = slots.end();
5921                for (; itSlot != endSlot; ++itSlot) {
5922                    Script* script = GetScriptOfSlot(itSlot->first);
5923                    if (!script) continue;
5924                    if (_UUIDFromCArray(&script->Uuid[0]) != uuid) continue;
5925                    if (itSlot->second.empty()) continue;
5926                    vars[uuid][itSlot->first] = itSlot->second;
5927                }
5928            }
5929            return vars;
5930        }
5931    
5932      /**      /**
5933       * Make a (semi) deep copy of the Instrument object given by @a orig       * Make a (semi) deep copy of the Instrument object given by @a orig
5934       * and assign it to this object.       * and assign it to this object.
# Line 5550  namespace { Line 5963  namespace {
5963          PianoReleaseMode = orig->PianoReleaseMode;          PianoReleaseMode = orig->PianoReleaseMode;
5964          DimensionKeyRange = orig->DimensionKeyRange;          DimensionKeyRange = orig->DimensionKeyRange;
5965          scriptPoolFileOffsets = orig->scriptPoolFileOffsets;          scriptPoolFileOffsets = orig->scriptPoolFileOffsets;
5966          pScriptRefs = orig->pScriptRefs;          // deep copy of pScriptRefs required (to avoid undefined behaviour)
5967            if (pScriptRefs) delete pScriptRefs;
5968            pScriptRefs = new std::vector<_ScriptPooolRef>;
5969            if (orig->pScriptRefs)
5970                *pScriptRefs = *orig->pScriptRefs;
5971            scriptVars = orig->scriptVars;
5972                    
5973          // free old midi rules          // free old midi rules
5974          for (int i = 0 ; pMidiRules[i] ; i++) {          for (int i = 0 ; pMidiRules[i] ; i++) {
# Line 5592  namespace { Line 6010  namespace {
6010       */       */
6011      bool Instrument::UsesAnyGigFormatExtension() const {      bool Instrument::UsesAnyGigFormatExtension() const {
6012          if (!pRegions) return false;          if (!pRegions) return false;
6013            if (!scriptVars.empty()) return true;
6014          RegionList::const_iterator iter = pRegions->begin();          RegionList::const_iterator iter = pRegions->begin();
6015          RegionList::const_iterator end  = pRegions->end();          RegionList::const_iterator end  = pRegions->end();
6016          for (; iter != end; ++iter) {          for (; iter != end; ++iter) {

Legend:
Removed from v.3722  
changed lines
  Added in v.3903

  ViewVC Help
Powered by ViewVC