Browse Source

Use base16 for VariantMap hash key conversion in both directions in JSONValue. Expose JSONValue Get/Set Variant & VariantMap to script.

Lasse Öörni 8 years ago
parent
commit
cc26cac198

+ 14 - 0
Source/Urho3D/AngelScript/ResourceAPI.cpp

@@ -334,6 +334,16 @@ static CScriptArray* JSONValueGetValues(const JSONValue& jsonValue)
     }
     }
 }
 }
 
 
+static void JSONValueSetVariant(const Variant& variant, JSONValue& jsonValue)
+{
+    jsonValue.SetVariant(variant, GetScriptContext());
+}
+
+static void JSONValueSetVariantMap(const VariantMap& variantMap, JSONValue& jsonValue)
+{
+    jsonValue.SetVariantMap(variantMap, GetScriptContext());
+}
+
 static void RegisterJSONValue(asIScriptEngine* engine)
 static void RegisterJSONValue(asIScriptEngine* engine)
 {
 {
     engine->RegisterEnum("JSONValueType");
     engine->RegisterEnum("JSONValueType");
@@ -368,6 +378,8 @@ static void RegisterJSONValue(asIScriptEngine* engine)
     engine->RegisterObjectMethod("JSONValue", "JSONValue& opAssign(double)", asMETHODPR(JSONValue, operator =, (double), JSONValue&), asCALL_THISCALL);
     engine->RegisterObjectMethod("JSONValue", "JSONValue& opAssign(double)", asMETHODPR(JSONValue, operator =, (double), JSONValue&), asCALL_THISCALL);
     engine->RegisterObjectMethod("JSONValue", "JSONValue& opAssign(const String&in)", asMETHODPR(JSONValue, operator =, (const String&), JSONValue&), asCALL_THISCALL);
     engine->RegisterObjectMethod("JSONValue", "JSONValue& opAssign(const String&in)", asMETHODPR(JSONValue, operator =, (const String&), JSONValue&), asCALL_THISCALL);
     engine->RegisterObjectMethod("JSONValue", "JSONValue& opAssign(const JSONValue&in)", asMETHODPR(JSONValue, operator =, (const JSONValue&), JSONValue&), asCALL_THISCALL);
     engine->RegisterObjectMethod("JSONValue", "JSONValue& opAssign(const JSONValue&in)", asMETHODPR(JSONValue, operator =, (const JSONValue&), JSONValue&), asCALL_THISCALL);
+    engine->RegisterObjectMethod("JSONValue", "void SetVariant(const Variant&in)", asFUNCTION(JSONValueSetVariant), asCALL_CDECL_OBJLAST);
+    engine->RegisterObjectMethod("JSONValue", "void SetVariantMap(const VariantMap&in)", asFUNCTION(JSONValueSetVariantMap), asCALL_CDECL_OBJLAST);
 
 
     engine->RegisterObjectMethod("JSONValue", "JSONValueType get_valueType() const", asMETHOD(JSONValue, GetValueType), asCALL_THISCALL);
     engine->RegisterObjectMethod("JSONValue", "JSONValueType get_valueType() const", asMETHOD(JSONValue, GetValueType), asCALL_THISCALL);
     engine->RegisterObjectMethod("JSONValue", "JSONNumberType get_numberType() const", asMETHOD(JSONValue, GetNumberType), asCALL_THISCALL);
     engine->RegisterObjectMethod("JSONValue", "JSONNumberType get_numberType() const", asMETHOD(JSONValue, GetNumberType), asCALL_THISCALL);
@@ -386,6 +398,8 @@ static void RegisterJSONValue(asIScriptEngine* engine)
     engine->RegisterObjectMethod("JSONValue", "float GetFloat() const", asMETHOD(JSONValue, GetFloat), asCALL_THISCALL);
     engine->RegisterObjectMethod("JSONValue", "float GetFloat() const", asMETHOD(JSONValue, GetFloat), asCALL_THISCALL);
     engine->RegisterObjectMethod("JSONValue", "double GetDouble() const", asMETHOD(JSONValue, GetDouble), asCALL_THISCALL);
     engine->RegisterObjectMethod("JSONValue", "double GetDouble() const", asMETHOD(JSONValue, GetDouble), asCALL_THISCALL);
     engine->RegisterObjectMethod("JSONValue", "const String& GetString() const", asMETHOD(JSONValue, GetString), asCALL_THISCALL);
     engine->RegisterObjectMethod("JSONValue", "const String& GetString() const", asMETHOD(JSONValue, GetString), asCALL_THISCALL);
+    engine->RegisterObjectMethod("JSONValue", "Variant GetVariant() const", asMETHOD(JSONValue, GetVariant), asCALL_THISCALL);
+    engine->RegisterObjectMethod("JSONValue", "VariantMap GetVariantMap() const", asMETHOD(JSONValue, GetVariantMap), asCALL_THISCALL);
 
 
     engine->RegisterObjectMethod("JSONValue", "JSONValue& opIndex(uint)", asFUNCTION(JSONValueAtPosition), asCALL_CDECL_OBJLAST);
     engine->RegisterObjectMethod("JSONValue", "JSONValue& opIndex(uint)", asFUNCTION(JSONValueAtPosition), asCALL_CDECL_OBJLAST);
     engine->RegisterObjectMethod("JSONValue", "const JSONValue& opIndex(uint) const", asFUNCTION(JSONValueAtPosition), asCALL_CDECL_OBJLAST);
     engine->RegisterObjectMethod("JSONValue", "const JSONValue& opIndex(uint) const", asFUNCTION(JSONValueAtPosition), asCALL_CDECL_OBJLAST);

+ 66 - 0
Source/Urho3D/LuaScript/pkgs/Resource/JSONValue.pkg

@@ -37,6 +37,8 @@ class JSONValue
     tolua_outside void JSONValueSetString @ SetString(const String value);
     tolua_outside void JSONValueSetString @ SetString(const String value);
     tolua_outside void JSONValueSetArray @ SetArray(const JSONArray& value);
     tolua_outside void JSONValueSetArray @ SetArray(const JSONArray& value);
     tolua_outside void JSONValueSetObject @ SetObject(const JSONObject& value);
     tolua_outside void JSONValueSetObject @ SetObject(const JSONObject& value);
+    void SetVariant(const Variant& value);
+    void SetVariantMap(const VariantMap& value);
 
 
     JSONValueType GetValueType() const;
     JSONValueType GetValueType() const;
     JSONNumberType GetNumberType() const;
     JSONNumberType GetNumberType() const;
@@ -57,6 +59,8 @@ class JSONValue
     const String GetString() const;
     const String GetString() const;
     const JSONArray& GetArray() const;
     const JSONArray& GetArray() const;
     const JSONObject& GetObject() const;
     const JSONObject& GetObject() const;
+    Variant GetVariant() const;
+    VariantMap GetVariantMap() const;
 
 
     JSONValue& operator [](unsigned index);
     JSONValue& operator [](unsigned index);
     const JSONValue& operator [](unsigned index) const;
     const JSONValue& operator [](unsigned index) const;
@@ -128,4 +132,66 @@ static void JSONValueSetObject(JSONValue* jsonValue, const JSONObject& value)
     (*jsonValue) = value;
     (*jsonValue) = value;
 }
 }
 
 
+#define TOLUA_DISABLE_tolua_ResourceLuaAPI_JSONValue_SetVariant00
+static int tolua_ResourceLuaAPI_JSONValue_SetVariant00(lua_State* tolua_S)
+{
+#ifndef TOLUA_RELEASE
+ tolua_Error tolua_err;
+ if (
+ !tolua_isusertype(tolua_S,1,"JSONValue",0,&tolua_err) ||
+ (tolua_isvaluenil(tolua_S,2,&tolua_err) || !tolua_isusertype(tolua_S,2,"const Variant",0,&tolua_err)) ||
+ !tolua_isnoobj(tolua_S,3,&tolua_err)
+ )
+ goto tolua_lerror;
+ else
+#endif
+ {
+  JSONValue* self = (JSONValue*)  tolua_tousertype(tolua_S,1,0);
+  const Variant* value = ((const Variant*)  tolua_tousertype(tolua_S,2,0));
+#ifndef TOLUA_RELEASE
+ if (!self) tolua_error(tolua_S,"invalid 'self' in function 'SetVariant'", NULL);
+#endif
+ {
+  self->SetVariant(*value, GetContext(tolua_S));
+ }
+ }
+ return 0;
+#ifndef TOLUA_RELEASE
+ tolua_lerror:
+ tolua_error(tolua_S,"#ferror in function 'SetVariant'.",&tolua_err);
+ return 0;
+#endif
+}
+
+#define TOLUA_DISABLE_tolua_ResourceLuaAPI_JSONValue_SetVariantMap00
+static int tolua_ResourceLuaAPI_JSONValue_SetVariantMap00(lua_State* tolua_S)
+{
+#ifndef TOLUA_RELEASE
+ tolua_Error tolua_err;
+ if (
+ !tolua_isusertype(tolua_S,1,"JSONValue",0,&tolua_err) ||
+ (tolua_isvaluenil(tolua_S,2,&tolua_err) || !tolua_isusertype(tolua_S,2,"const VariantMap",0,&tolua_err)) ||
+ !tolua_isnoobj(tolua_S,3,&tolua_err)
+ )
+ goto tolua_lerror;
+ else
+#endif
+ {
+  JSONValue* self = (JSONValue*)  tolua_tousertype(tolua_S,1,0);
+  const VariantMap* value = ((const VariantMap*)  tolua_tousertype(tolua_S,2,0));
+#ifndef TOLUA_RELEASE
+ if (!self) tolua_error(tolua_S,"invalid 'self' in function 'SetVariantMap'", NULL);
+#endif
+ {
+  self->SetVariantMap(*value, GetContext(tolua_S));
+ }
+ }
+ return 0;
+#ifndef TOLUA_RELEASE
+ tolua_lerror:
+ tolua_error(tolua_S,"#ferror in function 'SetVariantMap'.",&tolua_err);
+ return 0;
+#endif
+}
+
 $}
 $}

+ 2 - 1
Source/Urho3D/Resource/JSONValue.cpp

@@ -581,7 +581,8 @@ VariantMap JSONValue::GetVariantMap() const
 
 
     for (ConstJSONObjectIterator i = Begin(); i != End(); ++i)
     for (ConstJSONObjectIterator i = Begin(); i != End(); ++i)
     {
     {
-        StringHash key(ToUInt(i->first_));
+        /// \todo Ideally this should allow any strings, but for now the convention is that the keys need to be hexadecimal StringHashes
+        StringHash key(ToUInt(i->first_, 16));
         Variant variant = i->second_.GetVariant();
         Variant variant = i->second_.GetVariant();
         variantMap[key] = variant;
         variantMap[key] = variant;
     }
     }