Skip to content

Commit dddf67d

Browse files
committed
Merge Add client asynchronous kvp natives (pr-3316)
69796ab - feat(kvp-client): add client asynchronous kvp natives
2 parents ddfe3d5 + 69796ab commit dddf67d

File tree

5 files changed

+21
-10
lines changed

5 files changed

+21
-10
lines changed

code/components/citizen-resources-client/src/KVScriptFunctions.cpp

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -100,30 +100,32 @@ static std::string FormatKey(const char* key, const std::string& resource = {})
100100
return "res:" + resName + ":" + key;
101101
}
102102

103+
template<bool sync = true>
103104
static void PutResourceKvp(fx::ScriptContext& context, const char* data, size_t size)
104105
{
105106
auto db = EnsureDatabase();
106107
auto key = FormatKey(context.CheckArgument<const char*>(0));
107108

108109
leveldb::WriteOptions options;
109-
options.sync = true;
110+
options.sync = sync;
110111

111112
db->Put(options, key, leveldb::Slice{ data, size });
112113
}
113114

114-
template<typename T>
115+
template<typename T, bool sync = true>
115116
static void SetResourceKvp(fx::ScriptContext& context)
116117
{
117118
msgpack::sbuffer buffer;
118119
msgpack::packer<msgpack::sbuffer> packer(buffer);
119120
packer.pack(std::is_pointer_v<T> ? context.CheckArgument<T>(1) : context.GetArgument<T>(1));
120121

121-
PutResourceKvp(context, buffer.data(), buffer.size());
122+
PutResourceKvp<sync>(context, buffer.data(), buffer.size());
122123
}
123124

125+
template<bool sync = true>
124126
static void SetResourceKvpRaw(fx::ScriptContext& context)
125127
{
126-
PutResourceKvp(context, context.CheckArgument<const char*>(1), context.GetArgument<size_t>(2));
128+
PutResourceKvp<sync>(context, context.CheckArgument<const char*>(1), context.GetArgument<size_t>(2));
127129
}
128130

129131
struct AnyType {};
@@ -319,12 +321,15 @@ static void EndFindKvp(fx::ScriptContext& context)
319321
handle->dbIter.reset();
320322
}
321323

324+
template<bool sync = true>
322325
static void DeleteResourceKvp(fx::ScriptContext& context)
323326
{
324327
auto db = EnsureDatabase();
325328
auto key = FormatKey(context.CheckArgument<const char*>(0));
329+
leveldb::WriteOptions options;
330+
options.sync = sync;
326331

327-
db->Delete(leveldb::WriteOptions{}, key);
332+
db->Delete(options, key);
328333
}
329334

330335
#include <VFSStreamDevice.h>
@@ -580,10 +585,16 @@ static InitFunction initFunction([]()
580585
fx::ScriptEngine::RegisterNativeHandler("GET_EXTERNAL_KVP_STRING", GetExternalKvp<const char*>);
581586
fx::ScriptEngine::RegisterNativeHandler("GET_EXTERNAL_KVP_FLOAT", GetExternalKvp<float>);
582587

583-
fx::ScriptEngine::RegisterNativeHandler("DELETE_RESOURCE_KVP", DeleteResourceKvp);
588+
fx::ScriptEngine::RegisterNativeHandler("DELETE_RESOURCE_KVP", DeleteResourceKvp<true>);
584589

585590
fx::ScriptEngine::RegisterNativeHandler("START_FIND_KVP", StartFindKvp);
586591
fx::ScriptEngine::RegisterNativeHandler("START_FIND_EXTERNAL_KVP", StartFindExternalKvp);
587592
fx::ScriptEngine::RegisterNativeHandler("FIND_KVP", FindKvp);
588593
fx::ScriptEngine::RegisterNativeHandler("END_FIND_KVP", EndFindKvp);
594+
595+
// asynchronous
596+
fx::ScriptEngine::RegisterNativeHandler("SET_RESOURCE_KVP_NO_SYNC", SetResourceKvp<const char*, false>);
597+
fx::ScriptEngine::RegisterNativeHandler("SET_RESOURCE_KVP_INT_NO_SYNC", SetResourceKvp<int, false>);
598+
fx::ScriptEngine::RegisterNativeHandler("SET_RESOURCE_KVP_FLOAT_NO_SYNC", SetResourceKvp<float, false>);
599+
fx::ScriptEngine::RegisterNativeHandler("DELETE_RESOURCE_KVP_NO_SYNC", DeleteResourceKvp<false>);
589600
});

ext/native-decls/kvp/DeleteResourceKvpNoSync.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
ns: CFX
3-
apiset: server
3+
apiset: shared
44
---
55
## DELETE_RESOURCE_KVP_NO_SYNC
66

ext/native-decls/kvp/SetResourceKvpFloatNoSync.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
ns: CFX
3-
apiset: server
3+
apiset: shared
44
---
55
## SET_RESOURCE_KVP_FLOAT_NO_SYNC
66

ext/native-decls/kvp/SetResourceKvpIntNoSync.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
ns: CFX
3-
apiset: server
3+
apiset: shared
44
---
55
## SET_RESOURCE_KVP_INT_NO_SYNC
66

ext/native-decls/kvp/SetResourceKvpNoSync.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
ns: CFX
3-
apiset: server
3+
apiset: shared
44
---
55
## SET_RESOURCE_KVP_NO_SYNC
66

0 commit comments

Comments
 (0)