export TMap<FString, FString> to ZScript

This commit is contained in:
Alexander Kromm 2019-12-29 17:35:06 +07:00 committed by Christoph Oelckers
parent 77469e0512
commit 703686beee
8 changed files with 178 additions and 0 deletions

View file

@ -1171,6 +1171,7 @@ add_executable( zdoom WIN32 MACOSX_BUNDLE
utility/x86.cpp utility/x86.cpp
utility/strnatcmp.c utility/strnatcmp.c
utility/zstring.cpp utility/zstring.cpp
utility/dictionary.cpp
utility/math/asin.c utility/math/asin.c
utility/math/atan.c utility/math/atan.c
utility/math/const.c utility/math/const.c

View file

@ -55,6 +55,7 @@
#include "wi_stuff.h" #include "wi_stuff.h"
#include "a_dynlight.h" #include "a_dynlight.h"
#include "types.h" #include "types.h"
#include "utility/dictionary.h"
static TArray<FPropertyInfo*> properties; static TArray<FPropertyInfo*> properties;
static TArray<AFuncDesc> AFTable; static TArray<AFuncDesc> AFTable;
@ -841,6 +842,21 @@ void InitThingdef()
wbplayerstruct->Size = sizeof(wbplayerstruct_t); wbplayerstruct->Size = sizeof(wbplayerstruct_t);
wbplayerstruct->Align = alignof(wbplayerstruct_t); wbplayerstruct->Align = alignof(wbplayerstruct_t);
auto dictionarystruct = NewStruct("Dictionary", nullptr, true);
dictionarystruct->Size = sizeof(Dictionary);
dictionarystruct->Align = alignof(Dictionary);
NewPointer(dictionarystruct, false)->InstallHandlers(
[](FSerializer &ar, const char *key, const void *addr)
{
ar(key, *(Dictionary **)addr);
},
[](FSerializer &ar, const char *key, void *addr)
{
Serialize<Dictionary>(ar, key, *(Dictionary **)addr, nullptr);
return true;
}
);
FAutoSegIterator probe(CRegHead, CRegTail); FAutoSegIterator probe(CRegHead, CRegTail);
while (*++probe != NULL) while (*++probe != NULL)

View file

@ -2156,6 +2156,76 @@ template<> FSerializer &Serialize(FSerializer &arc, const char *key, FFont *&fon
} }
//==========================================================================
//
// Dictionary
//
//==========================================================================
FString DictionaryToString(const Dictionary &dict)
{
Dictionary::ConstPair *pair;
Dictionary::ConstIterator i { dict };
rapidjson::StringBuffer buffer;
rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
writer.StartObject();
while (i.NextPair(pair))
{
writer.Key(pair->Key);
writer.String(pair->Value);
}
writer.EndObject();
FString contents { buffer.GetString(), buffer.GetSize() };
return contents;
}
Dictionary *DictionaryFromString(const FString &string)
{
Dictionary *const dict = new Dictionary;
rapidjson::Document doc;
doc.Parse(string.GetChars(), string.Len());
if (doc.GetType() != rapidjson::Type::kObjectType)
{
I_Error("Dictionary is expected to be an object.");
return dict;
}
for (auto i = doc.MemberBegin(); i != doc.MemberEnd(); ++i)
{
if (i->value.GetType() != rapidjson::Type::kStringType)
{
I_Error("Dictionary value is expected to be a string.");
return dict;
}
dict->Insert(i->name.GetString(), i->value.GetString());
}
return dict;
}
template<> FSerializer &Serialize(FSerializer &arc, const char *key, Dictionary *&dict, Dictionary **)
{
if (arc.isWriting())
{
FString contents { DictionaryToString(*dict) };
return arc(key, contents);
}
else
{
FString contents;
arc(key, contents);
dict = DictionaryFromString(contents);
return arc;
}
}
//========================================================================== //==========================================================================
// //
// Handler to retrieve a numeric value of any kind. // Handler to retrieve a numeric value of any kind.

View file

@ -7,6 +7,7 @@
#include "r_defs.h" #include "r_defs.h"
#include "resourcefiles/file_zip.h" #include "resourcefiles/file_zip.h"
#include "tflags.h" #include "tflags.h"
#include "utility/dictionary.h"
extern bool save_full; extern bool save_full;
@ -270,6 +271,7 @@ template<> FSerializer &Serialize(FSerializer &arc, const char *key, FDoorAnimat
template<> FSerializer &Serialize(FSerializer &arc, const char *key, char *&pstr, char **def); template<> FSerializer &Serialize(FSerializer &arc, const char *key, char *&pstr, char **def);
template<> FSerializer &Serialize(FSerializer &arc, const char *key, FFont *&font, FFont **def); template<> FSerializer &Serialize(FSerializer &arc, const char *key, FFont *&font, FFont **def);
template<> FSerializer &Serialize(FSerializer &arc, const char *key, FLevelLocals *&font, FLevelLocals **def); template<> FSerializer &Serialize(FSerializer &arc, const char *key, FLevelLocals *&font, FLevelLocals **def);
template<> FSerializer &Serialize(FSerializer &arc, const char *key, Dictionary *&dict, Dictionary **def);
FSerializer &Serialize(FSerializer &arc, const char *key, FState *&state, FState **def, bool *retcode); FSerializer &Serialize(FSerializer &arc, const char *key, FState *&state, FState **def, bool *retcode);
template<> inline FSerializer &Serialize(FSerializer &arc, const char *key, FState *&state, FState **def) template<> inline FSerializer &Serialize(FSerializer &arc, const char *key, FState *&state, FState **def)
@ -311,5 +313,7 @@ FSerializer &Serialize(FSerializer &arc, const char *key, TFlags<T, TT> &flags,
return Serialize(arc, key, flags.Value, def? &def->Value : nullptr); return Serialize(arc, key, flags.Value, def? &def->Value : nullptr);
} }
FString DictionaryToString(const Dictionary &dict);
Dictionary *DictionaryFromString(const FString &string);
#endif #endif

View file

@ -0,0 +1,70 @@
#include "utility/dictionary.h"
#include "scripting/vm/vm.h"
#include "serializer.h"
//=====================================================================================
//
// Dictionary exports
//
//=====================================================================================
DEFINE_ACTION_FUNCTION(_Dictionary, Create)
{
PARAM_PROLOGUE;
ACTION_RETURN_POINTER(new Dictionary);
}
static void DictInsert(Dictionary *dict, const FString &key, const FString &value)
{
dict->Insert(key, value);
}
DEFINE_ACTION_FUNCTION_NATIVE(_Dictionary, Insert, DictInsert)
{
PARAM_SELF_STRUCT_PROLOGUE(Dictionary);
PARAM_STRING(key);
PARAM_STRING(value);
DictInsert(self, key, value);
return 0;
}
static void DictAt(const Dictionary *dict, const FString &key, FString *result)
{
const FString *value = dict->CheckKey(key);
*result = value ? *value : "";
}
DEFINE_ACTION_FUNCTION_NATIVE(_Dictionary, At, DictAt)
{
PARAM_SELF_STRUCT_PROLOGUE(Dictionary);
PARAM_STRING(key);
FString result;
DictAt(self, key, &result);
ACTION_RETURN_STRING(result);
}
static void DictToString(const Dictionary *dict, FString *result)
{
*result = DictionaryToString(*dict);
}
DEFINE_ACTION_FUNCTION_NATIVE(_Dictionary, ToString, DictToString)
{
PARAM_SELF_STRUCT_PROLOGUE(Dictionary);
FString result;
DictToString(self, &result);
ACTION_RETURN_STRING(result);
}
static Dictionary *DictFromString(const FString& string)
{
return DictionaryFromString(string);
}
DEFINE_ACTION_FUNCTION_NATIVE(_Dictionary, FromString, DictFromString)
{
PARAM_PROLOGUE;
PARAM_STRING(string);
ACTION_RETURN_POINTER(DictFromString(string));
}

6
src/utility/dictionary.h Normal file
View file

@ -0,0 +1,6 @@
#pragma once
#include "tarray.h"
#include "zstring.h"
using Dictionary = TMap<FString, FString>;

View file

@ -8,6 +8,7 @@ version "4.2"
#include "zscript/destructible.zs" #include "zscript/destructible.zs"
#include "zscript/level_postprocessor.zs" #include "zscript/level_postprocessor.zs"
#include "zscript/level_compatibility.zs" #include "zscript/level_compatibility.zs"
#include "zscript/dictionary.zs"
#include "zscript/actors/actor.zs" #include "zscript/actors/actor.zs"
#include "zscript/actors/checks.zs" #include "zscript/actors/checks.zs"

View file

@ -0,0 +1,10 @@
struct Dictionary native
{
native static Dictionary Create();
native static Dictionary FromString(String s);
native void Insert(String key, String value);
native String At(String key) const;
native String ToString() const;
}