Skip to content

Commit

Permalink
Fixed accessing a dictionary that might not be there
Browse files Browse the repository at this point in the history
  • Loading branch information
Leidtier committed Aug 6, 2024
1 parent 1dd169b commit 94ba964
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 54 deletions.
62 changes: 30 additions & 32 deletions plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,39 +14,37 @@ json getJsonFromHandle(int typedDictionaryHandle)
{
std::shared_ptr<TypedDictionary> dict = SKSE_HTTP_TypedDictionary::dicNestedDictionariesValues[typedDictionaryHandle];
json jsonToUse;
for (auto& [key, value]: dict->_dicElements)
{
std::string valueType = value->getTypeName();
if (valueType == "string")
jsonToUse[key] = dict->getString(key);
else if (valueType == "int")
jsonToUse[key] = dict->getInt(key);
else if (valueType == "float")
jsonToUse[key] = dict->getFloat(key);
else if (valueType == "bool")
jsonToUse[key] = dict->getBool(key);
else if (valueType == "stringArray")
jsonToUse[key] = dict->getStringArray(key);
else if (valueType == "intArray")
jsonToUse[key] = dict->getIntArray(key);
else if (valueType == "floatArray")
jsonToUse[key] = dict->getFloatArray(key);
else if (valueType == "boolArray")
jsonToUse[key] = dict->getBoolArray(key);
else if (valueType == "NestedDictionary")
{
int handle = dict->getNestedDictionary(key);
jsonToUse[key] = getJsonFromHandle(handle);
}
else if (valueType == "NestedDictionaryArray")
{
std::vector<int> handles = dict->getArrayOfNestedDictionaries(key);
auto jsonObjects = json::array();
size_t sizeOfHandles = handles.size();
for (auto i = 0; i < sizeOfHandles; ++i) {
jsonObjects.push_back(getJsonFromHandle(handles[i]));
if (dict) {
for (auto& [key, value] : dict->_dicElements) {
std::string valueType = value->getTypeName();
if (valueType == "string")
jsonToUse[key] = dict->getString(key);
else if (valueType == "int")
jsonToUse[key] = dict->getInt(key);
else if (valueType == "float")
jsonToUse[key] = dict->getFloat(key);
else if (valueType == "bool")
jsonToUse[key] = dict->getBool(key);
else if (valueType == "stringArray")
jsonToUse[key] = dict->getStringArray(key);
else if (valueType == "intArray")
jsonToUse[key] = dict->getIntArray(key);
else if (valueType == "floatArray")
jsonToUse[key] = dict->getFloatArray(key);
else if (valueType == "boolArray")
jsonToUse[key] = dict->getBoolArray(key);
else if (valueType == "NestedDictionary") {
int handle = dict->getNestedDictionary(key);
jsonToUse[key] = getJsonFromHandle(handle);
} else if (valueType == "NestedDictionaryArray") {
std::vector<int> handles = dict->getArrayOfNestedDictionaries(key);
auto jsonObjects = json::array();
size_t sizeOfHandles = handles.size();
for (auto i = 0; i < sizeOfHandles; ++i) {
jsonObjects.push_back(getJsonFromHandle(handles[i]));
}
jsonToUse[key] = jsonObjects;
}
jsonToUse[key] = jsonObjects;
}
}
return jsonToUse;
Expand Down
56 changes: 34 additions & 22 deletions src/SKSE_HTTP_TypedDictionary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,69 +14,80 @@ namespace SKSE_HTTP_TypedDictionary

void clearAll() {
for (auto& [key, value] : dicNestedDictionariesValues) {
value.reset();
if (value) {
value.reset();
}
}
dicNestedDictionariesValues.clear();
};

// Returns the value associated with the @key. If not, returns @default value
std::string getString(int object, std::string key, std::string defaultValue){
if(dicNestedDictionariesValues.contains(object) && dicNestedDictionariesValues[object]->hasElement(key)){
if (dicNestedDictionariesValues.contains(object) && dicNestedDictionariesValues[object] && dicNestedDictionariesValues[object]->hasElement(key)) {
return dicNestedDictionariesValues[object]->getString(key);
}
return defaultValue;
};
int getInt(int object, std::string key, int defaultValue){
if(dicNestedDictionariesValues.contains(object) && dicNestedDictionariesValues[object]->hasElement(key)){
if (dicNestedDictionariesValues.contains(object) && dicNestedDictionariesValues[object] &&
dicNestedDictionariesValues[object]->hasElement(key)) {
return dicNestedDictionariesValues[object]->getInt(key);
}
return defaultValue;
};
float getFloat(int object, std::string key, float defaultValue){
if(dicNestedDictionariesValues.contains(object) && dicNestedDictionariesValues[object]->hasElement(key)){
if (dicNestedDictionariesValues.contains(object) && dicNestedDictionariesValues[object] &&
dicNestedDictionariesValues[object]->hasElement(key)) {
return dicNestedDictionariesValues[object]->getFloat(key);
}
return defaultValue;
};
bool getBool(int object, std::string key, bool defaultValue){
if(dicNestedDictionariesValues.contains(object) && dicNestedDictionariesValues[object]->hasElement(key)){
if (dicNestedDictionariesValues.contains(object) && dicNestedDictionariesValues[object] &&
dicNestedDictionariesValues[object]->hasElement(key)) {
return dicNestedDictionariesValues[object]->getBool(key);
}
return defaultValue;
};
int getNestedDictionary(int object, std::string key, int defaultValue){
if(dicNestedDictionariesValues.contains(object) && dicNestedDictionariesValues[object]->hasElement(key)){
if (dicNestedDictionariesValues.contains(object) && dicNestedDictionariesValues[object] &&
dicNestedDictionariesValues[object]->hasElement(key)) {
return dicNestedDictionariesValues[object]->getNestedDictionary(key);
}
return defaultValue;
};

std::vector<std::string> getStringArray(int object, std::string key){
if(dicNestedDictionariesValues.contains(object) && dicNestedDictionariesValues[object]->hasElement(key)){
if (dicNestedDictionariesValues.contains(object) && dicNestedDictionariesValues[object] &&
dicNestedDictionariesValues[object]->hasElement(key)) {
return dicNestedDictionariesValues[object]->getStringArray(key);
}
return std::vector<std::string>();
};
std::vector<int> getIntArray(int object, std::string key){
if(dicNestedDictionariesValues.contains(object) && dicNestedDictionariesValues[object]->hasElement(key)){
if (dicNestedDictionariesValues.contains(object) && dicNestedDictionariesValues[object] &&
dicNestedDictionariesValues[object]->hasElement(key)) {
return dicNestedDictionariesValues[object]->getIntArray(key);
}
return std::vector<int>();
};
std::vector<float> getFloatArray(int object, std::string key){
if(dicNestedDictionariesValues.contains(object) && dicNestedDictionariesValues[object]->hasElement(key)){
if (dicNestedDictionariesValues.contains(object) && dicNestedDictionariesValues[object] &&
dicNestedDictionariesValues[object]->hasElement(key)) {
return dicNestedDictionariesValues[object]->getFloatArray(key);
}
return std::vector<float>();
};
std::vector<bool> getBoolArray(int object, std::string key){
if(dicNestedDictionariesValues.contains(object) && dicNestedDictionariesValues[object]->hasElement(key)){
if (dicNestedDictionariesValues.contains(object) && dicNestedDictionariesValues[object] &&
dicNestedDictionariesValues[object]->hasElement(key)) {
return dicNestedDictionariesValues[object]->getBoolArray(key);
}
return std::vector<bool>();
};
std::vector<int> getArrayOfNestedDictionaries(int object, std::string key){
if(dicNestedDictionariesValues.contains(object) && dicNestedDictionariesValues[object]->hasElement(key)){
if (dicNestedDictionariesValues.contains(object) && dicNestedDictionariesValues[object] &&
dicNestedDictionariesValues[object]->hasElement(key)) {
return dicNestedDictionariesValues[object]->getArrayOfNestedDictionaries(key);
}
return std::vector<int>();
Expand All @@ -85,49 +96,50 @@ namespace SKSE_HTTP_TypedDictionary
// Inserts @key: @value pair. Replaces existing pair with the same @key

void setString(int object, std::string key, std::string value){
if(dicNestedDictionariesValues.contains(object))
if (dicNestedDictionariesValues.contains(object) && dicNestedDictionariesValues[object])
dicNestedDictionariesValues[object]->setString(key, value);
}
void setInt(int object, std::string key, int value){
if(dicNestedDictionariesValues.contains(object))
if (dicNestedDictionariesValues.contains(object) && dicNestedDictionariesValues[object])
dicNestedDictionariesValues[object]->setInt(key, value);
}
void setFloat(int object, std::string key, float value){
if(dicNestedDictionariesValues.contains(object))
if (dicNestedDictionariesValues.contains(object) && dicNestedDictionariesValues[object])
dicNestedDictionariesValues[object]->setFloat(key, value);
}
void setBool(int object, std::string key, bool value){
if(dicNestedDictionariesValues.contains(object))
if (dicNestedDictionariesValues.contains(object) && dicNestedDictionariesValues[object])
dicNestedDictionariesValues[object]->setBool(key, value);
}
void setNestedDictionary(int object, std::string key, int value){
if(dicNestedDictionariesValues.contains(object))
if (dicNestedDictionariesValues.contains(object) && dicNestedDictionariesValues[object])
dicNestedDictionariesValues[object]->setNestedDictionary(key, value);
}

void setStringArray(int object, std::string key, std::vector<std::string> value){
if(dicNestedDictionariesValues.contains(object))
if (dicNestedDictionariesValues.contains(object) && dicNestedDictionariesValues[object])
dicNestedDictionariesValues[object]->setStringArray(key, value);
}
void setIntArray(int object, std::string key, std::vector<int> value){
if(dicNestedDictionariesValues.contains(object))
if (dicNestedDictionariesValues.contains(object) && dicNestedDictionariesValues[object])
dicNestedDictionariesValues[object]->setIntArray(key, value);
}
void setFloatArray(int object, std::string key, std::vector<float> value){
if(dicNestedDictionariesValues.contains(object))
if (dicNestedDictionariesValues.contains(object) && dicNestedDictionariesValues[object])
dicNestedDictionariesValues[object]->setFloatArray(key, value);
}
void setBoolArray(int object, std::string key, std::vector<bool> value){
if(dicNestedDictionariesValues.contains(object))
if (dicNestedDictionariesValues.contains(object) && dicNestedDictionariesValues[object])
dicNestedDictionariesValues[object]->setBoolArray(key, value);
}
void setArrayOfNestedDictionaries(int object, std::string key, std::vector<int> value){
if(dicNestedDictionariesValues.contains(object))
if (dicNestedDictionariesValues.contains(object) && dicNestedDictionariesValues[object])
dicNestedDictionariesValues[object]->setArrayOfNestedDictionaries(key, value);
}
// Returns true, if the container has @key: value pair
bool hasKey(int object, std::string key){
if(dicNestedDictionariesValues.contains(object) && dicNestedDictionariesValues[object]->hasElement(key))
if (dicNestedDictionariesValues.contains(object) && dicNestedDictionariesValues[object] &&
dicNestedDictionariesValues[object]->hasElement(key))
return true;
return false;
}
Expand Down

0 comments on commit 94ba964

Please sign in to comment.