diff options
author | Melanie Thielker | 2015-05-26 21:05:00 +0200 |
---|---|---|
committer | root | 2015-05-26 21:05:59 +0200 |
commit | 9766dc1f81521684a892fea60c87514183cc6d42 (patch) | |
tree | f9b0542b42e484c68d033cc43933a3a347133462 | |
parent | Comment spammy XMLRPC router message - we don't need to know every time a (diff) | |
download | opensim-SC-9766dc1f81521684a892fea60c87514183cc6d42.zip opensim-SC-9766dc1f81521684a892fea60c87514183cc6d42.tar.gz opensim-SC-9766dc1f81521684a892fea60c87514183cc6d42.tar.bz2 opensim-SC-9766dc1f81521684a892fea60c87514183cc6d42.tar.xz |
Add WhiteCore's LL Json functions
4 files changed, 279 insertions, 0 deletions
diff --git a/OpenSim/Region/ScriptEngine/Shared/Api/Implementation/LSL_Api.cs b/OpenSim/Region/ScriptEngine/Shared/Api/Implementation/LSL_Api.cs index a5d8292..3c8f54f 100644 --- a/OpenSim/Region/ScriptEngine/Shared/Api/Implementation/LSL_Api.cs +++ b/OpenSim/Region/ScriptEngine/Shared/Api/Implementation/LSL_Api.cs | |||
@@ -37,6 +37,7 @@ using System.Timers; | |||
37 | using Nini.Config; | 37 | using Nini.Config; |
38 | using log4net; | 38 | using log4net; |
39 | using OpenMetaverse; | 39 | using OpenMetaverse; |
40 | using OpenMetaverse.StructuredData; | ||
40 | using OpenMetaverse.Packets; | 41 | using OpenMetaverse.Packets; |
41 | using OpenSim; | 42 | using OpenSim; |
42 | using OpenSim.Framework; | 43 | using OpenSim.Framework; |
@@ -13661,6 +13662,244 @@ namespace OpenSim.Region.ScriptEngine.Shared.Api | |||
13661 | 13662 | ||
13662 | return String.Empty; | 13663 | return String.Empty; |
13663 | } | 13664 | } |
13665 | |||
13666 | public LSL_String llJsonGetValue(LSL_String json, LSL_List specifiers) | ||
13667 | { | ||
13668 | OSD o = OSDParser.DeserializeJson(json); | ||
13669 | OSD specVal = JsonGetSpecific(o, specifiers, 0); | ||
13670 | |||
13671 | return specVal.AsString(); | ||
13672 | } | ||
13673 | |||
13674 | public LSL_List llJson2List(LSL_String json) | ||
13675 | { | ||
13676 | try | ||
13677 | { | ||
13678 | OSD o = OSDParser.DeserializeJson(json); | ||
13679 | return (LSL_List)ParseJsonNode(o); | ||
13680 | } | ||
13681 | catch (Exception) | ||
13682 | { | ||
13683 | return new LSL_List(ScriptBaseClass.JSON_INVALID); | ||
13684 | } | ||
13685 | } | ||
13686 | |||
13687 | private object ParseJsonNode(OSD node) | ||
13688 | { | ||
13689 | if (node.Type == OSDType.Integer) | ||
13690 | return new LSL_Integer(node.AsInteger()); | ||
13691 | if (node.Type == OSDType.Boolean) | ||
13692 | return new LSL_Integer(node.AsBoolean() ? 1 : 0); | ||
13693 | if (node.Type == OSDType.Real) | ||
13694 | return new LSL_Float(node.AsReal()); | ||
13695 | if (node.Type == OSDType.UUID || node.Type == OSDType.String) | ||
13696 | return new LSL_String(node.AsString()); | ||
13697 | if (node.Type == OSDType.Array) | ||
13698 | { | ||
13699 | LSL_List resp = new LSL_List(); | ||
13700 | OSDArray ar = node as OSDArray; | ||
13701 | foreach (OSD o in ar) | ||
13702 | resp.Add(ParseJsonNode(o)); | ||
13703 | return resp; | ||
13704 | } | ||
13705 | if (node.Type == OSDType.Map) | ||
13706 | { | ||
13707 | LSL_List resp = new LSL_List(); | ||
13708 | OSDMap ar = node as OSDMap; | ||
13709 | foreach (KeyValuePair<string, OSD> o in ar) | ||
13710 | { | ||
13711 | resp.Add(new LSL_String(o.Key)); | ||
13712 | resp.Add(ParseJsonNode(o.Value)); | ||
13713 | } | ||
13714 | return resp; | ||
13715 | } | ||
13716 | throw new Exception(ScriptBaseClass.JSON_INVALID); | ||
13717 | } | ||
13718 | |||
13719 | public LSL_String llList2Json(LSL_String type, LSL_List values) | ||
13720 | { | ||
13721 | try | ||
13722 | { | ||
13723 | if (type == ScriptBaseClass.JSON_ARRAY) | ||
13724 | { | ||
13725 | OSDArray array = new OSDArray(); | ||
13726 | foreach (object o in values.Data) | ||
13727 | { | ||
13728 | array.Add(ListToJson(o)); | ||
13729 | } | ||
13730 | return OSDParser.SerializeJsonString(array); | ||
13731 | } | ||
13732 | else if (type == ScriptBaseClass.JSON_OBJECT) | ||
13733 | { | ||
13734 | OSDMap map = new OSDMap(); | ||
13735 | for (int i = 0; i < values.Data.Length; i += 2) | ||
13736 | { | ||
13737 | if (!(values.Data[i] is LSL_String)) | ||
13738 | return ScriptBaseClass.JSON_INVALID; | ||
13739 | map.Add(((LSL_String)values.Data[i]).m_string, ListToJson(values.Data[i + 1])); | ||
13740 | } | ||
13741 | return OSDParser.SerializeJsonString(map); | ||
13742 | } | ||
13743 | return ScriptBaseClass.JSON_INVALID; | ||
13744 | } | ||
13745 | catch (Exception ex) | ||
13746 | { | ||
13747 | return ex.Message; | ||
13748 | } | ||
13749 | } | ||
13750 | |||
13751 | private OSD ListToJson(object o) | ||
13752 | { | ||
13753 | if (o is LSL_Float) | ||
13754 | return OSD.FromReal(((LSL_Float)o).value); | ||
13755 | if (o is LSL_Integer) | ||
13756 | { | ||
13757 | int i = ((LSL_Integer)o).value; | ||
13758 | if (i == 0) | ||
13759 | return OSD.FromBoolean(false); | ||
13760 | else if (i == 1) | ||
13761 | return OSD.FromBoolean(true); | ||
13762 | return OSD.FromInteger(i); | ||
13763 | } | ||
13764 | if (o is LSL_Rotation) | ||
13765 | return OSD.FromString(((LSL_Rotation)o).ToString()); | ||
13766 | if (o is LSL_Vector) | ||
13767 | return OSD.FromString(((LSL_Vector)o).ToString()); | ||
13768 | if (o is LSL_String) | ||
13769 | { | ||
13770 | string str = ((LSL_String)o).m_string; | ||
13771 | if (str == ScriptBaseClass.JSON_NULL) | ||
13772 | return new OSD(); | ||
13773 | return OSD.FromString(str); | ||
13774 | } | ||
13775 | throw new Exception(ScriptBaseClass.JSON_INVALID); | ||
13776 | } | ||
13777 | |||
13778 | private OSD JsonGetSpecific(OSD o, LSL_List specifiers, int i) | ||
13779 | { | ||
13780 | object spec = specifiers.Data[i]; | ||
13781 | OSD nextVal = null; | ||
13782 | if (o is OSDArray) | ||
13783 | { | ||
13784 | if (spec is LSL_Integer) | ||
13785 | nextVal = ((OSDArray)o)[((LSL_Integer)spec).value]; | ||
13786 | } | ||
13787 | if (o is OSDMap) | ||
13788 | { | ||
13789 | if (spec is LSL_String) | ||
13790 | nextVal = ((OSDMap)o)[((LSL_String)spec).m_string]; | ||
13791 | } | ||
13792 | if (nextVal != null) | ||
13793 | { | ||
13794 | if (specifiers.Data.Length - 1 > i) | ||
13795 | return JsonGetSpecific(nextVal, specifiers, i + 1); | ||
13796 | } | ||
13797 | return nextVal; | ||
13798 | } | ||
13799 | |||
13800 | public LSL_String llJsonSetValue(LSL_String json, LSL_List specifiers, LSL_String value) | ||
13801 | { | ||
13802 | try | ||
13803 | { | ||
13804 | OSD o = OSDParser.DeserializeJson(json); | ||
13805 | JsonSetSpecific(o, specifiers, 0, value); | ||
13806 | return OSDParser.SerializeJsonString(o); | ||
13807 | } | ||
13808 | catch (Exception) | ||
13809 | { | ||
13810 | } | ||
13811 | return ScriptBaseClass.JSON_INVALID; | ||
13812 | } | ||
13813 | |||
13814 | private void JsonSetSpecific(OSD o, LSL_List specifiers, int i, LSL_String val) | ||
13815 | { | ||
13816 | object spec = specifiers.Data[i]; | ||
13817 | // 20131224 not used object specNext = i+1 == specifiers.Data.Length ? null : specifiers.Data[i+1]; | ||
13818 | OSD nextVal = null; | ||
13819 | if (o is OSDArray) | ||
13820 | { | ||
13821 | OSDArray array = ((OSDArray)o); | ||
13822 | if (spec is LSL_Integer) | ||
13823 | { | ||
13824 | int v = ((LSL_Integer)spec).value; | ||
13825 | if (v >= array.Count) | ||
13826 | array.Add(JsonBuildRestOfSpec(specifiers, i + 1, val)); | ||
13827 | else | ||
13828 | nextVal = ((OSDArray)o)[v]; | ||
13829 | } | ||
13830 | else if (spec is LSL_String && ((LSL_String)spec) == ScriptBaseClass.JSON_APPEND) | ||
13831 | array.Add(JsonBuildRestOfSpec(specifiers, i + 1, val)); | ||
13832 | } | ||
13833 | if (o is OSDMap) | ||
13834 | { | ||
13835 | if (spec is LSL_String) | ||
13836 | { | ||
13837 | OSDMap map = ((OSDMap)o); | ||
13838 | if (map.ContainsKey(((LSL_String)spec).m_string)) | ||
13839 | nextVal = map[((LSL_String)spec).m_string]; | ||
13840 | else | ||
13841 | map.Add(((LSL_String)spec).m_string, JsonBuildRestOfSpec(specifiers, i + 1, val)); | ||
13842 | } | ||
13843 | } | ||
13844 | if (nextVal != null) | ||
13845 | { | ||
13846 | if (specifiers.Data.Length - 1 > i) | ||
13847 | { | ||
13848 | JsonSetSpecific(nextVal, specifiers, i + 1, val); | ||
13849 | return; | ||
13850 | } | ||
13851 | } | ||
13852 | } | ||
13853 | |||
13854 | private OSD JsonBuildRestOfSpec(LSL_List specifiers, int i, LSL_String val) | ||
13855 | { | ||
13856 | object spec = i >= specifiers.Data.Length ? null : specifiers.Data[i]; | ||
13857 | // 20131224 not used object specNext = i+1 >= specifiers.Data.Length ? null : specifiers.Data[i+1]; | ||
13858 | |||
13859 | if (spec == null) | ||
13860 | return OSD.FromString(val); | ||
13861 | |||
13862 | if (spec is LSL_Integer || | ||
13863 | (spec is LSL_String && ((LSL_String)spec) == ScriptBaseClass.JSON_APPEND)) | ||
13864 | { | ||
13865 | OSDArray array = new OSDArray(); | ||
13866 | array.Add(JsonBuildRestOfSpec(specifiers, i + 1, val)); | ||
13867 | return array; | ||
13868 | } | ||
13869 | else if (spec is LSL_String) | ||
13870 | { | ||
13871 | OSDMap map = new OSDMap(); | ||
13872 | map.Add((LSL_String)spec, JsonBuildRestOfSpec(specifiers, i + 1, val)); | ||
13873 | return map; | ||
13874 | } | ||
13875 | return new OSD(); | ||
13876 | } | ||
13877 | |||
13878 | public LSL_String llJsonValueType(LSL_String json, LSL_List specifiers) | ||
13879 | { | ||
13880 | OSD o = OSDParser.DeserializeJson(json); | ||
13881 | OSD specVal = JsonGetSpecific(o, specifiers, 0); | ||
13882 | if (specVal == null) | ||
13883 | return ScriptBaseClass.JSON_INVALID; | ||
13884 | switch (specVal.Type) | ||
13885 | { | ||
13886 | case OSDType.Array: | ||
13887 | return ScriptBaseClass.JSON_ARRAY; | ||
13888 | case OSDType.Boolean: | ||
13889 | return specVal.AsBoolean() ? ScriptBaseClass.JSON_TRUE : ScriptBaseClass.JSON_FALSE; | ||
13890 | case OSDType.Integer: | ||
13891 | case OSDType.Real: | ||
13892 | return ScriptBaseClass.JSON_NUMBER; | ||
13893 | case OSDType.Map: | ||
13894 | return ScriptBaseClass.JSON_OBJECT; | ||
13895 | case OSDType.String: | ||
13896 | case OSDType.UUID: | ||
13897 | return ScriptBaseClass.JSON_STRING; | ||
13898 | case OSDType.Unknown: | ||
13899 | return ScriptBaseClass.JSON_NULL; | ||
13900 | } | ||
13901 | return ScriptBaseClass.JSON_INVALID; | ||
13902 | } | ||
13664 | } | 13903 | } |
13665 | 13904 | ||
13666 | public class NotecardCache | 13905 | public class NotecardCache |
diff --git a/OpenSim/Region/ScriptEngine/Shared/Api/Interface/ILSL_Api.cs b/OpenSim/Region/ScriptEngine/Shared/Api/Interface/ILSL_Api.cs index 8c51564..b642a9c 100644 --- a/OpenSim/Region/ScriptEngine/Shared/Api/Interface/ILSL_Api.cs +++ b/OpenSim/Region/ScriptEngine/Shared/Api/Interface/ILSL_Api.cs | |||
@@ -437,5 +437,10 @@ namespace OpenSim.Region.ScriptEngine.Shared.Api.Interfaces | |||
437 | void llSetAnimationOverride(LSL_String animState, LSL_String anim); | 437 | void llSetAnimationOverride(LSL_String animState, LSL_String anim); |
438 | void llResetAnimationOverride(LSL_String anim_state); | 438 | void llResetAnimationOverride(LSL_String anim_state); |
439 | LSL_String llGetAnimationOverride(LSL_String anim_state); | 439 | LSL_String llGetAnimationOverride(LSL_String anim_state); |
440 | LSL_String llJsonGetValue(LSL_String json, LSL_List specifiers); | ||
441 | LSL_List llJson2List(LSL_String json); | ||
442 | LSL_String llList2Json(LSL_String type, LSL_List values); | ||
443 | LSL_String llJsonSetValue(LSL_String json, LSL_List specifiers, LSL_String value); | ||
444 | LSL_String llJsonValueType(LSL_String json, LSL_List specifiers); | ||
440 | } | 445 | } |
441 | } | 446 | } |
diff --git a/OpenSim/Region/ScriptEngine/Shared/Api/Runtime/LSL_Constants.cs b/OpenSim/Region/ScriptEngine/Shared/Api/Runtime/LSL_Constants.cs index e6ab6ec..0e22ff3 100644 --- a/OpenSim/Region/ScriptEngine/Shared/Api/Runtime/LSL_Constants.cs +++ b/OpenSim/Region/ScriptEngine/Shared/Api/Runtime/LSL_Constants.cs | |||
@@ -788,6 +788,16 @@ namespace OpenSim.Region.ScriptEngine.Shared.ScriptBase | |||
788 | public const int KFM_CMD_STOP = 1; | 788 | public const int KFM_CMD_STOP = 1; |
789 | public const int KFM_CMD_PAUSE = 2; | 789 | public const int KFM_CMD_PAUSE = 2; |
790 | 790 | ||
791 | public const string JSON_ARRAY = "JSON_ARRAY"; | ||
792 | public const string JSON_OBJECT = "JSON_OBJECT"; | ||
793 | public const string JSON_INVALID = "JSON_INVALID"; | ||
794 | public const string JSON_NUMBER = "JSON_NUMBER"; | ||
795 | public const string JSON_STRING = "JSON_STRING"; | ||
796 | public const string JSON_TRUE = "JSON_TRUE"; | ||
797 | public const string JSON_FALSE = "JSON_FALSE"; | ||
798 | public const string JSON_NULL = "JSON_NULL"; | ||
799 | public const string JSON_APPEND = "JSON_APPEND"; | ||
800 | |||
791 | /// <summary> | 801 | /// <summary> |
792 | /// process name parameter as regex | 802 | /// process name parameter as regex |
793 | /// </summary> | 803 | /// </summary> |
diff --git a/OpenSim/Region/ScriptEngine/Shared/Api/Runtime/LSL_Stub.cs b/OpenSim/Region/ScriptEngine/Shared/Api/Runtime/LSL_Stub.cs index 78c41a7..5047162 100644 --- a/OpenSim/Region/ScriptEngine/Shared/Api/Runtime/LSL_Stub.cs +++ b/OpenSim/Region/ScriptEngine/Shared/Api/Runtime/LSL_Stub.cs | |||
@@ -2029,5 +2029,30 @@ namespace OpenSim.Region.ScriptEngine.Shared.ScriptBase | |||
2029 | { | 2029 | { |
2030 | return m_LSL_Functions.llGetAnimationOverride(anim_state); | 2030 | return m_LSL_Functions.llGetAnimationOverride(anim_state); |
2031 | } | 2031 | } |
2032 | |||
2033 | public LSL_String llJsonGetValue(LSL_String json, LSL_List specifiers) | ||
2034 | { | ||
2035 | return m_LSL_Functions.llJsonGetValue(json, specifiers); | ||
2036 | } | ||
2037 | |||
2038 | public LSL_List llJson2List(LSL_String json) | ||
2039 | { | ||
2040 | return m_LSL_Functions.llJson2List(json); | ||
2041 | } | ||
2042 | |||
2043 | public LSL_String llList2Json(LSL_String type, LSL_List values) | ||
2044 | { | ||
2045 | return m_LSL_Functions.llList2Json(type, values); | ||
2046 | } | ||
2047 | |||
2048 | public LSL_String llJsonSetValue(LSL_String json, LSL_List specifiers, LSL_String value) | ||
2049 | { | ||
2050 | return m_LSL_Functions.llJsonSetValue(json, specifiers, value); | ||
2051 | } | ||
2052 | |||
2053 | public LSL_String llJsonValueType(LSL_String json, LSL_List specifiers) | ||
2054 | { | ||
2055 | return m_LSL_Functions.llJsonValueType(json, specifiers); | ||
2056 | } | ||
2032 | } | 2057 | } |
2033 | } | 2058 | } |