refactor: rename and update JSON extension components

- Renamed YYJSON to JSON throughout the codebase for consistency
- Updated file names and references, including YYJSONManager to JsonManager
- Added some new native functions
This commit is contained in:
ProjectSky 2025-11-08 20:16:30 +08:00
parent 2212b5f900
commit 5af62b03b8
19 changed files with 8081 additions and 4219 deletions

View File

@ -678,7 +678,7 @@ else:
'extensions/tf2/AMBuilder',
'extensions/topmenus/AMBuilder',
'extensions/updater/AMBuilder',
'extensions/yyjson/AMBuilder',
'extensions/json/AMBuilder',
]
if builder.backend == 'amb2':

View File

@ -2,7 +2,7 @@
import os
for cxx in builder.targets:
binary = SM.ExtLibrary(builder, cxx, 'yyjson.ext')
binary = SM.ExtLibrary(builder, cxx, 'json.ext')
if binary.compiler.family == 'gcc' or binary.compiler.family == 'clang':
binary.compiler.cxxflags += ['-fno-rtti']
binary.compiler.cflags += ['-fPIC']
@ -14,13 +14,13 @@ for cxx in builder.targets:
]
binary.compiler.cxxincludes += [
os.path.join(builder.sourcePath, 'extensions', 'yyjson', 'yyjson'),
os.path.join(builder.sourcePath, 'extensions', 'json', 'yyjson'),
]
binary.sources += [
'extension.cpp',
'YYJSONManager.cpp',
'json_natives.cpp',
'JsonManager.cpp',
'JsonNatives.cpp',
'yyjson/yyjson.c',
'../../public/smsdk_ext.cpp',
]

402
extensions/json/JsonManager.h Executable file
View File

@ -0,0 +1,402 @@
#ifndef _INCLUDE_SM_JSON_JSONMANAGER_H_
#define _INCLUDE_SM_JSON_JSONMANAGER_H_
#include <IJsonManager.h>
#include <yyjson.h>
#include <random>
#include <memory>
#include <charconv>
/**
* @brief JSON value wrapper
*
* Wraps json mutable/immutable documents and values.
* Used as the primary data type for JSON operations.
*/
class JsonValue {
public:
JsonValue() = default;
~JsonValue() {
if (m_pDocument_mut.unique()) {
yyjson_mut_doc_free(m_pDocument_mut.get());
}
if (m_pDocument.unique()) {
yyjson_doc_free(m_pDocument.get());
}
}
JsonValue(const JsonValue&) = delete;
JsonValue& operator=(const JsonValue&) = delete;
void ResetObjectIterator() {
m_iterInitialized = false;
}
void ResetArrayIterator() {
m_iterInitialized = false;
m_arrayIndex = 0;
}
bool IsMutable() const {
return m_pDocument_mut != nullptr;
}
bool IsImmutable() const {
return m_pDocument != nullptr;
}
// Mutable document
std::shared_ptr<yyjson_mut_doc> m_pDocument_mut;
yyjson_mut_val* m_pVal_mut{ nullptr };
// Immutable document
std::shared_ptr<yyjson_doc> m_pDocument;
yyjson_val* m_pVal{ nullptr };
// Mutable document iterators
yyjson_mut_obj_iter m_iterObj;
yyjson_mut_arr_iter m_iterArr;
// Immutable document iterators
yyjson_obj_iter m_iterObjImm;
yyjson_arr_iter m_iterArrImm;
Handle_t m_handle{ BAD_HANDLE };
size_t m_arrayIndex{ 0 };
size_t m_readSize{ 0 };
bool m_iterInitialized{ false };
};
/**
* @brief Array iterator wrapper
*
* Wraps yyjson_arr_iter and yyjson_mut_arr_iter for array iteration.
*/
class JsonArrIter {
public:
JsonArrIter() = default;
~JsonArrIter() = default;
JsonArrIter(const JsonArrIter&) = delete;
JsonArrIter& operator=(const JsonArrIter&) = delete;
bool IsMutable() const {
return m_isMutable;
}
std::shared_ptr<yyjson_mut_doc> m_pDocument_mut;
std::shared_ptr<yyjson_doc> m_pDocument;
yyjson_mut_arr_iter m_iterMut;
yyjson_arr_iter m_iterImm;
Handle_t m_handle{ BAD_HANDLE };
bool m_isMutable{ false };
bool m_initialized{ false };
};
/**
* @brief Object iterator wrapper
*
* Wraps yyjson_obj_iter and yyjson_mut_obj_iter for object iteration.
*/
class JsonObjIter {
public:
JsonObjIter() = default;
~JsonObjIter() = default;
JsonObjIter(const JsonObjIter&) = delete;
JsonObjIter& operator=(const JsonObjIter&) = delete;
bool IsMutable() const {
return m_isMutable;
}
std::shared_ptr<yyjson_mut_doc> m_pDocument_mut;
std::shared_ptr<yyjson_doc> m_pDocument;
yyjson_mut_obj_iter m_iterMut;
yyjson_obj_iter m_iterImm;
void* m_currentKey{ nullptr };
Handle_t m_handle{ BAD_HANDLE };
bool m_isMutable{ false };
bool m_initialized{ false };
};
class JsonManager : public IJsonManager
{
public:
JsonManager();
~JsonManager();
public:
// ========== Document Operations ==========
virtual JsonValue* ParseJSON(const char* json_str, bool is_file, bool is_mutable,
yyjson_read_flag read_flg, char* error, size_t error_size) override;
virtual bool WriteToString(JsonValue* handle, char* buffer, size_t buffer_size,
yyjson_write_flag write_flg, size_t* out_size) override;
virtual bool WriteToFile(JsonValue* handle, const char* path, yyjson_write_flag write_flg,
char* error, size_t error_size) override;
virtual bool Equals(JsonValue* handle1, JsonValue* handle2) override;
virtual bool EqualsStr(JsonValue* handle, const char* str) override;
virtual JsonValue* DeepCopy(JsonValue* targetDoc, JsonValue* sourceValue) override;
virtual const char* GetTypeDesc(JsonValue* handle) override;
virtual size_t GetSerializedSize(JsonValue* handle, yyjson_write_flag write_flg) override;
virtual JsonValue* ToMutable(JsonValue* handle) override;
virtual JsonValue* ToImmutable(JsonValue* handle) override;
virtual yyjson_type GetType(JsonValue* handle) override;
virtual yyjson_subtype GetSubtype(JsonValue* handle) override;
virtual bool IsArray(JsonValue* handle) override;
virtual bool IsObject(JsonValue* handle) override;
virtual bool IsInt(JsonValue* handle) override;
virtual bool IsUint(JsonValue* handle) override;
virtual bool IsSint(JsonValue* handle) override;
virtual bool IsNum(JsonValue* handle) override;
virtual bool IsBool(JsonValue* handle) override;
virtual bool IsTrue(JsonValue* handle) override;
virtual bool IsFalse(JsonValue* handle) override;
virtual bool IsFloat(JsonValue* handle) override;
virtual bool IsStr(JsonValue* handle) override;
virtual bool IsNull(JsonValue* handle) override;
virtual bool IsCtn(JsonValue* handle) override;
virtual bool IsMutable(JsonValue* handle) override;
virtual bool IsImmutable(JsonValue* handle) override;
virtual size_t GetReadSize(JsonValue* handle) override;
// ========== Object Operations ==========
virtual JsonValue* ObjectInit() override;
virtual JsonValue* ObjectInitWithStrings(const char** pairs, size_t count) override;
virtual JsonValue* ObjectParseString(const char* str, yyjson_read_flag read_flg,
char* error, size_t error_size) override;
virtual JsonValue* ObjectParseFile(const char* path, yyjson_read_flag read_flg,
char* error, size_t error_size) override;
virtual size_t ObjectGetSize(JsonValue* handle) override;
virtual bool ObjectGetKey(JsonValue* handle, size_t index, const char** out_key) override;
virtual JsonValue* ObjectGetValueAt(JsonValue* handle, size_t index) override;
virtual JsonValue* ObjectGet(JsonValue* handle, const char* key) override;
virtual bool ObjectGetBool(JsonValue* handle, const char* key, bool* out_value) override;
virtual bool ObjectGetFloat(JsonValue* handle, const char* key, double* out_value) override;
virtual bool ObjectGetInt(JsonValue* handle, const char* key, int* out_value) override;
virtual bool ObjectGetInt64(JsonValue* handle, const char* key, std::variant<int64_t, uint64_t>* out_value) override;
virtual bool ObjectGetString(JsonValue* handle, const char* key, const char** out_str, size_t* out_len) override;
virtual bool ObjectIsNull(JsonValue* handle, const char* key, bool* out_is_null) override;
virtual bool ObjectHasKey(JsonValue* handle, const char* key, bool use_pointer) override;
virtual bool ObjectRenameKey(JsonValue* handle, const char* old_key, const char* new_key, bool allow_duplicate) override;
virtual bool ObjectSet(JsonValue* handle, const char* key, JsonValue* value) override;
virtual bool ObjectSetBool(JsonValue* handle, const char* key, bool value) override;
virtual bool ObjectSetFloat(JsonValue* handle, const char* key, double value) override;
virtual bool ObjectSetInt(JsonValue* handle, const char* key, int value) override;
virtual bool ObjectSetInt64(JsonValue* handle, const char* key, std::variant<int64_t, uint64_t> value) override;
virtual bool ObjectSetNull(JsonValue* handle, const char* key) override;
virtual bool ObjectSetString(JsonValue* handle, const char* key, const char* value) override;
virtual bool ObjectRemove(JsonValue* handle, const char* key) override;
virtual bool ObjectClear(JsonValue* handle) override;
virtual bool ObjectSort(JsonValue* handle, JSON_SORT_ORDER sort_mode) override;
// ========== Array Operations ==========
virtual JsonValue* ArrayInit() override;
virtual JsonValue* ArrayInitWithStrings(const char** strings, size_t count) override;
virtual JsonValue* ArrayInitWithInt32(const int32_t* values, size_t count) override;
virtual JsonValue* ArrayInitWithInt64(const char** values, size_t count,
char* error, size_t error_size) override;
virtual JsonValue* ArrayInitWithBool(const bool* values, size_t count) override;
virtual JsonValue* ArrayInitWithFloat(const double* values, size_t count) override;
virtual JsonValue* ArrayParseString(const char* str, yyjson_read_flag read_flg,
char* error, size_t error_size) override;
virtual JsonValue* ArrayParseFile(const char* path, yyjson_read_flag read_flg,
char* error, size_t error_size) override;
virtual size_t ArrayGetSize(JsonValue* handle) override;
virtual JsonValue* ArrayGet(JsonValue* handle, size_t index) override;
virtual JsonValue* ArrayGetFirst(JsonValue* handle) override;
virtual JsonValue* ArrayGetLast(JsonValue* handle) override;
virtual bool ArrayGetBool(JsonValue* handle, size_t index, bool* out_value) override;
virtual bool ArrayGetFloat(JsonValue* handle, size_t index, double* out_value) override;
virtual bool ArrayGetInt(JsonValue* handle, size_t index, int* out_value) override;
virtual bool ArrayGetInt64(JsonValue* handle, size_t index, std::variant<int64_t, uint64_t>* out_value) override;
virtual bool ArrayGetString(JsonValue* handle, size_t index, const char** out_str, size_t* out_len) override;
virtual bool ArrayIsNull(JsonValue* handle, size_t index) override;
virtual bool ArrayReplace(JsonValue* handle, size_t index, JsonValue* value) override;
virtual bool ArrayReplaceBool(JsonValue* handle, size_t index, bool value) override;
virtual bool ArrayReplaceFloat(JsonValue* handle, size_t index, double value) override;
virtual bool ArrayReplaceInt(JsonValue* handle, size_t index, int value) override;
virtual bool ArrayReplaceInt64(JsonValue* handle, size_t index, std::variant<int64_t, uint64_t> value) override;
virtual bool ArrayReplaceNull(JsonValue* handle, size_t index) override;
virtual bool ArrayReplaceString(JsonValue* handle, size_t index, const char* value) override;
virtual bool ArrayAppend(JsonValue* handle, JsonValue* value) override;
virtual bool ArrayAppendBool(JsonValue* handle, bool value) override;
virtual bool ArrayAppendFloat(JsonValue* handle, double value) override;
virtual bool ArrayAppendInt(JsonValue* handle, int value) override;
virtual bool ArrayAppendInt64(JsonValue* handle, std::variant<int64_t, uint64_t> value) override;
virtual bool ArrayAppendNull(JsonValue* handle) override;
virtual bool ArrayAppendString(JsonValue* handle, const char* value) override;
virtual bool ArrayInsert(JsonValue* handle, size_t index, JsonValue* value) override;
virtual bool ArrayInsertBool(JsonValue* handle, size_t index, bool value) override;
virtual bool ArrayInsertInt(JsonValue* handle, size_t index, int value) override;
virtual bool ArrayInsertInt64(JsonValue* handle, size_t index, std::variant<int64_t, uint64_t> value) override;
virtual bool ArrayInsertFloat(JsonValue* handle, size_t index, double value) override;
virtual bool ArrayInsertString(JsonValue* handle, size_t index, const char* value) override;
virtual bool ArrayInsertNull(JsonValue* handle, size_t index) override;
virtual bool ArrayPrepend(JsonValue* handle, JsonValue* value) override;
virtual bool ArrayPrependBool(JsonValue* handle, bool value) override;
virtual bool ArrayPrependInt(JsonValue* handle, int value) override;
virtual bool ArrayPrependInt64(JsonValue* handle, std::variant<int64_t, uint64_t> value) override;
virtual bool ArrayPrependFloat(JsonValue* handle, double value) override;
virtual bool ArrayPrependString(JsonValue* handle, const char* value) override;
virtual bool ArrayPrependNull(JsonValue* handle) override;
virtual bool ArrayRemove(JsonValue* handle, size_t index) override;
virtual bool ArrayRemoveFirst(JsonValue* handle) override;
virtual bool ArrayRemoveLast(JsonValue* handle) override;
virtual bool ArrayRemoveRange(JsonValue* handle, size_t start_index, size_t end_index) override;
virtual bool ArrayClear(JsonValue* handle) override;
virtual int ArrayIndexOfBool(JsonValue* handle, bool search_value) override;
virtual int ArrayIndexOfString(JsonValue* handle, const char* search_value) override;
virtual int ArrayIndexOfInt(JsonValue* handle, int search_value) override;
virtual int ArrayIndexOfInt64(JsonValue* handle, int64_t search_value) override;
virtual int ArrayIndexOfUint64(JsonValue* handle, uint64_t search_value) override;
virtual int ArrayIndexOfFloat(JsonValue* handle, double search_value) override;
virtual bool ArraySort(JsonValue* handle, JSON_SORT_ORDER sort_mode) override;
// ========== Value Operations ==========
virtual JsonValue* Pack(const char* format, IPackParamProvider* param_provider, char* error, size_t error_size) override;
virtual JsonValue* CreateBool(bool value) override;
virtual JsonValue* CreateFloat(double value) override;
virtual JsonValue* CreateInt(int value) override;
virtual JsonValue* CreateInt64(std::variant<int64_t, uint64_t> value) override;
virtual JsonValue* CreateNull() override;
virtual JsonValue* CreateString(const char* value) override;
virtual bool GetBool(JsonValue* handle, bool* out_value) override;
virtual bool GetFloat(JsonValue* handle, double* out_value) override;
virtual bool GetInt(JsonValue* handle, int* out_value) override;
virtual bool GetInt64(JsonValue* handle, std::variant<int64_t, uint64_t>* out_value) override;
virtual bool GetString(JsonValue* handle, const char** out_str, size_t* out_len) override;
// ========== Pointer Operations ==========
virtual JsonValue* PtrGet(JsonValue* handle, const char* path, char* error, size_t error_size) override;
virtual bool PtrGetBool(JsonValue* handle, const char* path, bool* out_value, char* error, size_t error_size) override;
virtual bool PtrGetFloat(JsonValue* handle, const char* path, double* out_value, char* error, size_t error_size) override;
virtual bool PtrGetInt(JsonValue* handle, const char* path, int* out_value, char* error, size_t error_size) override;
virtual bool PtrGetInt64(JsonValue* handle, const char* path, std::variant<int64_t, uint64_t>* out_value, char* error, size_t error_size) override;
virtual bool PtrGetString(JsonValue* handle, const char* path, const char** out_str, size_t* out_len, char* error, size_t error_size) override;
virtual bool PtrGetIsNull(JsonValue* handle, const char* path, bool* out_is_null, char* error, size_t error_size) override;
virtual bool PtrGetLength(JsonValue* handle, const char* path, size_t* out_len, char* error, size_t error_size) override;
virtual bool PtrSet(JsonValue* handle, const char* path, JsonValue* value, char* error, size_t error_size) override;
virtual bool PtrSetBool(JsonValue* handle, const char* path, bool value, char* error, size_t error_size) override;
virtual bool PtrSetFloat(JsonValue* handle, const char* path, double value, char* error, size_t error_size) override;
virtual bool PtrSetInt(JsonValue* handle, const char* path, int value, char* error, size_t error_size) override;
virtual bool PtrSetInt64(JsonValue* handle, const char* path, std::variant<int64_t, uint64_t> value, char* error, size_t error_size) override;
virtual bool PtrSetString(JsonValue* handle, const char* path, const char* value, char* error, size_t error_size) override;
virtual bool PtrSetNull(JsonValue* handle, const char* path, char* error, size_t error_size) override;
virtual bool PtrAdd(JsonValue* handle, const char* path, JsonValue* value, char* error, size_t error_size) override;
virtual bool PtrAddBool(JsonValue* handle, const char* path, bool value, char* error, size_t error_size) override;
virtual bool PtrAddFloat(JsonValue* handle, const char* path, double value, char* error, size_t error_size) override;
virtual bool PtrAddInt(JsonValue* handle, const char* path, int value, char* error, size_t error_size) override;
virtual bool PtrAddInt64(JsonValue* handle, const char* path, std::variant<int64_t, uint64_t> value, char* error, size_t error_size) override;
virtual bool PtrAddString(JsonValue* handle, const char* path, const char* value, char* error, size_t error_size) override;
virtual bool PtrAddNull(JsonValue* handle, const char* path, char* error, size_t error_size) override;
virtual bool PtrRemove(JsonValue* handle, const char* path, char* error, size_t error_size) override;
virtual JsonValue* PtrTryGet(JsonValue* handle, const char* path) override;
virtual bool PtrTryGetBool(JsonValue* handle, const char* path, bool* out_value) override;
virtual bool PtrTryGetFloat(JsonValue* handle, const char* path, double* out_value) override;
virtual bool PtrTryGetInt(JsonValue* handle, const char* path, int* out_value) override;
virtual bool PtrTryGetInt64(JsonValue* handle, const char* path, std::variant<int64_t, uint64_t>* out_value) override;
virtual bool PtrTryGetString(JsonValue* handle, const char* path, const char** out_str, size_t* out_len) override;
// ========== Iterator Operations ==========
virtual bool ObjectForeachNext(JsonValue* handle, const char** out_key,
size_t* out_key_len, JsonValue** out_value) override;
virtual bool ArrayForeachNext(JsonValue* handle, size_t* out_index,
JsonValue** out_value) override;
virtual bool ObjectForeachKeyNext(JsonValue* handle, const char** out_key,
size_t* out_key_len) override;
virtual bool ArrayForeachIndexNext(JsonValue* handle, size_t* out_index) override;
// ========== Array Iterator Operations ==========
virtual JsonArrIter* ArrIterInit(JsonValue* handle) override;
virtual JsonArrIter* ArrIterWith(JsonValue* handle) override;
virtual JsonValue* ArrIterNext(JsonArrIter* iter) override;
virtual bool ArrIterHasNext(JsonArrIter* iter) override;
virtual size_t ArrIterGetIndex(JsonArrIter* iter) override;
virtual void* ArrIterRemove(JsonArrIter* iter) override;
// ========== Object Iterator Operations ==========
virtual JsonObjIter* ObjIterInit(JsonValue* handle) override;
virtual JsonObjIter* ObjIterWith(JsonValue* handle) override;
virtual void* ObjIterNext(JsonObjIter* iter) override;
virtual bool ObjIterHasNext(JsonObjIter* iter) override;
virtual JsonValue* ObjIterGetVal(JsonObjIter* iter, void* key) override;
virtual JsonValue* ObjIterGet(JsonObjIter* iter, const char* key) override;
virtual size_t ObjIterGetIndex(JsonObjIter* iter) override;
virtual void* ObjIterRemove(JsonObjIter* iter) override;
// ========== Iterator Release Operations ==========
virtual void ReleaseArrIter(JsonArrIter* iter) override;
virtual void ReleaseObjIter(JsonObjIter* iter) override;
// ========== Iterator Handle Type Operations ==========
virtual HandleType_t GetArrIterHandleType() override;
virtual HandleType_t GetObjIterHandleType() override;
virtual JsonArrIter* GetArrIterFromHandle(IPluginContext* pContext, Handle_t handle) override;
virtual JsonObjIter* GetObjIterFromHandle(IPluginContext* pContext, Handle_t handle) override;
// ========== Release Operations ==========
virtual void Release(JsonValue* value) override;
// ========== Handle Type Operations ==========
virtual HandleType_t GetHandleType() override;
// ========== Handle Operations ==========
virtual JsonValue* GetFromHandle(IPluginContext* pContext, Handle_t handle) override;
// ========== Number Read/Write Operations ==========
virtual JsonValue* ReadNumber(const char* dat, uint32_t read_flg = 0,
char* error = nullptr, size_t error_size = 0, size_t* out_consumed = nullptr) override;
virtual bool WriteNumber(JsonValue* handle, char* buffer, size_t buffer_size,
size_t* out_written = nullptr) override;
// ========== Floating-Point Format Operations ==========
virtual bool SetFpToFloat(JsonValue* handle, bool flt) override;
virtual bool SetFpToFixed(JsonValue* handle, int prec) override;
// ========== Direct Value Modification Operations ==========
virtual bool SetBool(JsonValue* handle, bool value) override;
virtual bool SetInt(JsonValue* handle, int value) override;
virtual bool SetInt64(JsonValue* handle, std::variant<int64_t, uint64_t> value) override;
virtual bool SetFloat(JsonValue* handle, double value) override;
virtual bool SetString(JsonValue* handle, const char* value) override;
virtual bool SetNull(JsonValue* handle) override;
virtual bool ParseInt64Variant(const char* value, std::variant<int64_t, uint64_t>* out_value,
char* error = nullptr, size_t error_size = 0) override;
private:
std::random_device m_randomDevice;
std::mt19937 m_randomGenerator;
// Helper methods
static std::unique_ptr<JsonValue> CreateWrapper();
static std::shared_ptr<yyjson_mut_doc> WrapDocument(yyjson_mut_doc* doc);
static std::shared_ptr<yyjson_mut_doc> CopyDocument(yyjson_doc* doc);
static std::shared_ptr<yyjson_mut_doc> CreateDocument();
static std::shared_ptr<yyjson_doc> WrapImmutableDocument(yyjson_doc* doc);
// Pack helper methods
static const char* SkipSeparators(const char* ptr);
static void SetPackError(char* error, size_t error_size, const char* fmt, ...);
static yyjson_mut_val* PackImpl(yyjson_mut_doc* doc, const char* format,
IPackParamProvider* provider, char* error,
size_t error_size, const char** out_end_ptr);
// PtrTryGet helper methods
struct PtrGetValueResult {
yyjson_mut_val* mut_val{ nullptr };
yyjson_val* imm_val{ nullptr };
bool success{ false };
};
static PtrGetValueResult PtrGetValueInternal(JsonValue* handle, const char* path);
};
#endif // _INCLUDE_SM_JSON_JSONMANAGER_H_

3244
extensions/json/JsonNatives.cpp Executable file

File diff suppressed because it is too large Load Diff

85
extensions/json/extension.cpp Executable file
View File

@ -0,0 +1,85 @@
#include "extension.h"
#include "JsonManager.h"
JsonExtension g_JsonExt;
SMEXT_LINK(&g_JsonExt);
HandleType_t g_JsonType;
HandleType_t g_ArrIterType;
HandleType_t g_ObjIterType;
JsonHandler g_JsonHandler;
ArrIterHandler g_ArrIterHandler;
ObjIterHandler g_ObjIterHandler;
IJsonManager* g_pJsonManager;
bool JsonExtension::SDK_OnLoad(char* error, size_t maxlen, bool late)
{
sharesys->AddNatives(myself, g_JsonNatives);
sharesys->RegisterLibrary(myself, "json");
HandleAccess haJSON;
handlesys->InitAccessDefaults(nullptr, &haJSON);
haJSON.access[HandleAccess_Read] = 0;
haJSON.access[HandleAccess_Delete] = 0;
HandleError err;
g_JsonType = handlesys->CreateType("JSON", &g_JsonHandler, 0, nullptr, &haJSON, myself->GetIdentity(), &err);
if (!g_JsonType) {
snprintf(error, maxlen, "Failed to create JSON handle type (err: %d)", err);
return false;
}
g_ArrIterType = handlesys->CreateType("JSONArrIter", &g_ArrIterHandler, 0, nullptr, &haJSON, myself->GetIdentity(), &err);
if (!g_ArrIterType) {
snprintf(error, maxlen, "Failed to create JSONArrIter handle type (err: %d)", err);
return false;
}
g_ObjIterType = handlesys->CreateType("JSONObjIter", &g_ObjIterHandler, 0, nullptr, &haJSON, myself->GetIdentity(), &err);
if (!g_ObjIterType) {
snprintf(error, maxlen, "Failed to create JSONObjIter handle type (err: %d)", err);
return false;
}
// Delete the existing instance if it exists
if (g_pJsonManager) {
delete g_pJsonManager;
g_pJsonManager = nullptr;
}
g_pJsonManager = new JsonManager();
if (!g_pJsonManager) {
snprintf(error, maxlen, "Failed to create JSON manager instance");
return false;
}
return sharesys->AddInterface(myself, g_pJsonManager);
}
void JsonExtension::SDK_OnUnload()
{
handlesys->RemoveType(g_JsonType, myself->GetIdentity());
handlesys->RemoveType(g_ArrIterType, myself->GetIdentity());
handlesys->RemoveType(g_ObjIterType, myself->GetIdentity());
if (g_pJsonManager) {
delete g_pJsonManager;
g_pJsonManager = nullptr;
}
}
void JsonHandler::OnHandleDestroy(HandleType_t type, void* object)
{
delete (JsonValue*)object;
}
void ArrIterHandler::OnHandleDestroy(HandleType_t type, void* object)
{
delete (JsonArrIter*)object;
}
void ObjIterHandler::OnHandleDestroy(HandleType_t type, void* object)
{
delete (JsonObjIter*)object;
}

42
extensions/json/extension.h Executable file
View File

@ -0,0 +1,42 @@
#ifndef _INCLUDE_SOURCEMOD_EXTENSION_PROPER_H_
#define _INCLUDE_SOURCEMOD_EXTENSION_PROPER_H_
#include "smsdk_ext.h"
#include "IJsonManager.h"
class JsonExtension : public SDKExtension
{
public:
virtual bool SDK_OnLoad(char *error, size_t maxlength, bool late);
virtual void SDK_OnUnload();
};
class JsonHandler : public IHandleTypeDispatch
{
public:
void OnHandleDestroy(HandleType_t type, void *object);
};
class ArrIterHandler : public IHandleTypeDispatch
{
public:
void OnHandleDestroy(HandleType_t type, void *object);
};
class ObjIterHandler : public IHandleTypeDispatch
{
public:
void OnHandleDestroy(HandleType_t type, void *object);
};
extern JsonExtension g_JsonExt;
extern HandleType_t g_JsonType;
extern HandleType_t g_ArrIterType;
extern HandleType_t g_ObjIterType;
extern JsonHandler g_JsonHandler;
extern ArrIterHandler g_ArrIterHandler;
extern ObjIterHandler g_ObjIterHandler;
extern const sp_nativeinfo_t g_JsonNatives[];
extern IJsonManager* g_pJsonManager;
#endif // _INCLUDE_SOURCEMOD_EXTENSION_PROPER_H_

View File

@ -1,12 +1,12 @@
#ifndef _INCLUDE_SOURCEMOD_EXTENSION_CONFIG_H_
#define _INCLUDE_SOURCEMOD_EXTENSION_CONFIG_H_
#define SMEXT_CONF_NAME "SourceMod YYJSON Extension"
#define SMEXT_CONF_NAME "SourceMod JSON Extension"
#define SMEXT_CONF_DESCRIPTION "Provide JSON Native"
#define SMEXT_CONF_VERSION "1.1.4a"
#define SMEXT_CONF_VERSION "1.1.5"
#define SMEXT_CONF_AUTHOR "ProjectSky"
#define SMEXT_CONF_URL "https://github.com/ProjectSky/sm-ext-yyjson"
#define SMEXT_CONF_LOGTAG "yyjson"
#define SMEXT_CONF_LOGTAG "json"
#define SMEXT_CONF_LICENSE "GPL"
#define SMEXT_CONF_DATESTRING __DATE__

View File

@ -45,13 +45,13 @@ BEGIN
BEGIN
BLOCK "000004b0"
BEGIN
VALUE "Comments", "YYJSON Extension"
VALUE "FileDescription", "SourceMod YYJSON Extension"
VALUE "Comments", "JSON Extension"
VALUE "FileDescription", "SourceMod JSON Extension"
VALUE "FileVersion", SM_VERSION_FILE
VALUE "InternalName", "SourceMod YYJSON Extension"
VALUE "InternalName", "SourceMod JSON Extension"
VALUE "LegalCopyright", "Copyright (c) 2004-2009, AlliedModders LLC"
VALUE "OriginalFilename", BINARY_NAME
VALUE "ProductName", "SourceMod YYJSON Extension"
VALUE "ProductName", "SourceMod JSON Extension"
VALUE "ProductVersion", SM_VERSION_STRING
END
END

View File

@ -1,264 +0,0 @@
#ifndef _INCLUDE_SM_YYJSON_YYJSONMANAGER_H_
#define _INCLUDE_SM_YYJSON_YYJSONMANAGER_H_
#include <IYYJSONManager.h>
#include <yyjson.h>
#include <IHandleSys.h>
#include <random>
#include <memory>
/**
* @brief JSON value wrapper
*
* Wraps yyjson mutable/immutable documents and values.
* Used as the primary data type for JSON operations.
*/
class YYJSONValue {
public:
YYJSONValue() = default;
~YYJSONValue() {
if (m_pDocument_mut.unique()) {
yyjson_mut_doc_free(m_pDocument_mut.get());
}
if (m_pDocument.unique()) {
yyjson_doc_free(m_pDocument.get());
}
}
YYJSONValue(const YYJSONValue&) = delete;
YYJSONValue& operator=(const YYJSONValue&) = delete;
void ResetObjectIterator() {
m_iterInitialized = false;
}
void ResetArrayIterator() {
m_iterInitialized = false;
m_arrayIndex = 0;
}
bool IsMutable() const {
return m_pDocument_mut != nullptr;
}
bool IsImmutable() const {
return m_pDocument != nullptr;
}
// Mutable document
std::shared_ptr<yyjson_mut_doc> m_pDocument_mut;
yyjson_mut_val* m_pVal_mut{ nullptr };
// Immutable document
std::shared_ptr<yyjson_doc> m_pDocument;
yyjson_val* m_pVal{ nullptr };
// Mutable document iterators
yyjson_mut_obj_iter m_iterObj;
yyjson_mut_arr_iter m_iterArr;
// Immutable document iterators
yyjson_obj_iter m_iterObjImm;
yyjson_arr_iter m_iterArrImm;
Handle_t m_handle{ BAD_HANDLE };
size_t m_arrayIndex{ 0 };
size_t m_readSize{ 0 };
bool m_iterInitialized{ false };
};
class YYJSONManager : public IYYJSONManager
{
public:
YYJSONManager();
~YYJSONManager();
public:
// ========== Document Operations ==========
virtual YYJSONValue* ParseJSON(const char* json_str, bool is_file, bool is_mutable,
yyjson_read_flag read_flg, char* error, size_t error_size) override;
virtual bool WriteToString(YYJSONValue* handle, char* buffer, size_t buffer_size,
yyjson_write_flag write_flg, size_t* out_size) override;
virtual bool WriteToFile(YYJSONValue* handle, const char* path, yyjson_write_flag write_flg,
char* error, size_t error_size) override;
virtual bool Equals(YYJSONValue* handle1, YYJSONValue* handle2) override;
virtual YYJSONValue* DeepCopy(YYJSONValue* targetDoc, YYJSONValue* sourceValue) override;
virtual const char* GetTypeDesc(YYJSONValue* handle) override;
virtual size_t GetSerializedSize(YYJSONValue* handle, yyjson_write_flag write_flg) override;
virtual YYJSONValue* ToMutable(YYJSONValue* handle) override;
virtual YYJSONValue* ToImmutable(YYJSONValue* handle) override;
virtual yyjson_type GetType(YYJSONValue* handle) override;
virtual yyjson_subtype GetSubtype(YYJSONValue* handle) override;
virtual bool IsArray(YYJSONValue* handle) override;
virtual bool IsObject(YYJSONValue* handle) override;
virtual bool IsInt(YYJSONValue* handle) override;
virtual bool IsUint(YYJSONValue* handle) override;
virtual bool IsSint(YYJSONValue* handle) override;
virtual bool IsNum(YYJSONValue* handle) override;
virtual bool IsBool(YYJSONValue* handle) override;
virtual bool IsTrue(YYJSONValue* handle) override;
virtual bool IsFalse(YYJSONValue* handle) override;
virtual bool IsFloat(YYJSONValue* handle) override;
virtual bool IsStr(YYJSONValue* handle) override;
virtual bool IsNull(YYJSONValue* handle) override;
virtual bool IsCtn(YYJSONValue* handle) override;
virtual bool IsMutable(YYJSONValue* handle) override;
virtual bool IsImmutable(YYJSONValue* handle) override;
virtual size_t GetReadSize(YYJSONValue* handle) override;
// ========== Object Operations ==========
virtual YYJSONValue* ObjectInit() override;
virtual YYJSONValue* ObjectInitWithStrings(const char** pairs, size_t count) override;
virtual YYJSONValue* ObjectParseString(const char* str, yyjson_read_flag read_flg,
char* error, size_t error_size) override;
virtual YYJSONValue* ObjectParseFile(const char* path, yyjson_read_flag read_flg,
char* error, size_t error_size) override;
virtual size_t ObjectGetSize(YYJSONValue* handle) override;
virtual bool ObjectGetKey(YYJSONValue* handle, size_t index, const char** out_key) override;
virtual YYJSONValue* ObjectGetValueAt(YYJSONValue* handle, size_t index) override;
virtual YYJSONValue* ObjectGet(YYJSONValue* handle, const char* key) override;
virtual bool ObjectGetBool(YYJSONValue* handle, const char* key, bool* out_value) override;
virtual bool ObjectGetFloat(YYJSONValue* handle, const char* key, double* out_value) override;
virtual bool ObjectGetInt(YYJSONValue* handle, const char* key, int* out_value) override;
virtual bool ObjectGetInt64(YYJSONValue* handle, const char* key, int64_t* out_value) override;
virtual bool ObjectGetString(YYJSONValue* handle, const char* key, const char** out_str, size_t* out_len) override;
virtual bool ObjectIsNull(YYJSONValue* handle, const char* key, bool* out_is_null) override;
virtual bool ObjectHasKey(YYJSONValue* handle, const char* key, bool use_pointer) override;
virtual bool ObjectRenameKey(YYJSONValue* handle, const char* old_key, const char* new_key, bool allow_duplicate) override;
virtual bool ObjectSet(YYJSONValue* handle, const char* key, YYJSONValue* value) override;
virtual bool ObjectSetBool(YYJSONValue* handle, const char* key, bool value) override;
virtual bool ObjectSetFloat(YYJSONValue* handle, const char* key, double value) override;
virtual bool ObjectSetInt(YYJSONValue* handle, const char* key, int value) override;
virtual bool ObjectSetInt64(YYJSONValue* handle, const char* key, int64_t value) override;
virtual bool ObjectSetNull(YYJSONValue* handle, const char* key) override;
virtual bool ObjectSetString(YYJSONValue* handle, const char* key, const char* value) override;
virtual bool ObjectRemove(YYJSONValue* handle, const char* key) override;
virtual bool ObjectClear(YYJSONValue* handle) override;
virtual bool ObjectSort(YYJSONValue* handle, YYJSON_SORT_ORDER sort_mode) override;
// ========== Array Operations ==========
virtual YYJSONValue* ArrayInit() override;
virtual YYJSONValue* ArrayInitWithStrings(const char** strings, size_t count) override;
virtual YYJSONValue* ArrayParseString(const char* str, yyjson_read_flag read_flg,
char* error, size_t error_size) override;
virtual YYJSONValue* ArrayParseFile(const char* path, yyjson_read_flag read_flg,
char* error, size_t error_size) override;
virtual size_t ArrayGetSize(YYJSONValue* handle) override;
virtual YYJSONValue* ArrayGet(YYJSONValue* handle, size_t index) override;
virtual YYJSONValue* ArrayGetFirst(YYJSONValue* handle) override;
virtual YYJSONValue* ArrayGetLast(YYJSONValue* handle) override;
virtual bool ArrayGetBool(YYJSONValue* handle, size_t index, bool* out_value) override;
virtual bool ArrayGetFloat(YYJSONValue* handle, size_t index, double* out_value) override;
virtual bool ArrayGetInt(YYJSONValue* handle, size_t index, int* out_value) override;
virtual bool ArrayGetInt64(YYJSONValue* handle, size_t index, int64_t* out_value) override;
virtual bool ArrayGetString(YYJSONValue* handle, size_t index, const char** out_str, size_t* out_len) override;
virtual bool ArrayIsNull(YYJSONValue* handle, size_t index) override;
virtual bool ArrayReplace(YYJSONValue* handle, size_t index, YYJSONValue* value) override;
virtual bool ArrayReplaceBool(YYJSONValue* handle, size_t index, bool value) override;
virtual bool ArrayReplaceFloat(YYJSONValue* handle, size_t index, double value) override;
virtual bool ArrayReplaceInt(YYJSONValue* handle, size_t index, int value) override;
virtual bool ArrayReplaceInt64(YYJSONValue* handle, size_t index, int64_t value) override;
virtual bool ArrayReplaceNull(YYJSONValue* handle, size_t index) override;
virtual bool ArrayReplaceString(YYJSONValue* handle, size_t index, const char* value) override;
virtual bool ArrayAppend(YYJSONValue* handle, YYJSONValue* value) override;
virtual bool ArrayAppendBool(YYJSONValue* handle, bool value) override;
virtual bool ArrayAppendFloat(YYJSONValue* handle, double value) override;
virtual bool ArrayAppendInt(YYJSONValue* handle, int value) override;
virtual bool ArrayAppendInt64(YYJSONValue* handle, int64_t value) override;
virtual bool ArrayAppendNull(YYJSONValue* handle) override;
virtual bool ArrayAppendString(YYJSONValue* handle, const char* value) override;
virtual bool ArrayRemove(YYJSONValue* handle, size_t index) override;
virtual bool ArrayRemoveFirst(YYJSONValue* handle) override;
virtual bool ArrayRemoveLast(YYJSONValue* handle) override;
virtual bool ArrayRemoveRange(YYJSONValue* handle, size_t start_index, size_t end_index) override;
virtual bool ArrayClear(YYJSONValue* handle) override;
virtual int ArrayIndexOfBool(YYJSONValue* handle, bool search_value) override;
virtual int ArrayIndexOfString(YYJSONValue* handle, const char* search_value) override;
virtual int ArrayIndexOfInt(YYJSONValue* handle, int search_value) override;
virtual int ArrayIndexOfInt64(YYJSONValue* handle, int64_t search_value) override;
virtual int ArrayIndexOfFloat(YYJSONValue* handle, double search_value) override;
virtual bool ArraySort(YYJSONValue* handle, YYJSON_SORT_ORDER sort_mode) override;
// ========== Value Operations ==========
virtual YYJSONValue* Pack(const char* format, IPackParamProvider* param_provider, char* error, size_t error_size) override;
virtual YYJSONValue* CreateBool(bool value) override;
virtual YYJSONValue* CreateFloat(double value) override;
virtual YYJSONValue* CreateInt(int value) override;
virtual YYJSONValue* CreateInt64(int64_t value) override;
virtual YYJSONValue* CreateNull() override;
virtual YYJSONValue* CreateString(const char* value) override;
virtual bool GetBool(YYJSONValue* handle, bool* out_value) override;
virtual bool GetFloat(YYJSONValue* handle, double* out_value) override;
virtual bool GetInt(YYJSONValue* handle, int* out_value) override;
virtual bool GetInt64(YYJSONValue* handle, int64_t* out_value) override;
virtual bool GetString(YYJSONValue* handle, const char** out_str, size_t* out_len) override;
// ========== Pointer Operations ==========
virtual YYJSONValue* PtrGet(YYJSONValue* handle, const char* path, char* error, size_t error_size) override;
virtual bool PtrGetBool(YYJSONValue* handle, const char* path, bool* out_value, char* error, size_t error_size) override;
virtual bool PtrGetFloat(YYJSONValue* handle, const char* path, double* out_value, char* error, size_t error_size) override;
virtual bool PtrGetInt(YYJSONValue* handle, const char* path, int* out_value, char* error, size_t error_size) override;
virtual bool PtrGetInt64(YYJSONValue* handle, const char* path, int64_t* out_value, char* error, size_t error_size) override;
virtual bool PtrGetString(YYJSONValue* handle, const char* path, const char** out_str, size_t* out_len, char* error, size_t error_size) override;
virtual bool PtrGetIsNull(YYJSONValue* handle, const char* path, bool* out_is_null, char* error, size_t error_size) override;
virtual bool PtrGetLength(YYJSONValue* handle, const char* path, size_t* out_len, char* error, size_t error_size) override;
virtual bool PtrSet(YYJSONValue* handle, const char* path, YYJSONValue* value, char* error, size_t error_size) override;
virtual bool PtrSetBool(YYJSONValue* handle, const char* path, bool value, char* error, size_t error_size) override;
virtual bool PtrSetFloat(YYJSONValue* handle, const char* path, double value, char* error, size_t error_size) override;
virtual bool PtrSetInt(YYJSONValue* handle, const char* path, int value, char* error, size_t error_size) override;
virtual bool PtrSetInt64(YYJSONValue* handle, const char* path, int64_t value, char* error, size_t error_size) override;
virtual bool PtrSetString(YYJSONValue* handle, const char* path, const char* value, char* error, size_t error_size) override;
virtual bool PtrSetNull(YYJSONValue* handle, const char* path, char* error, size_t error_size) override;
virtual bool PtrAdd(YYJSONValue* handle, const char* path, YYJSONValue* value, char* error, size_t error_size) override;
virtual bool PtrAddBool(YYJSONValue* handle, const char* path, bool value, char* error, size_t error_size) override;
virtual bool PtrAddFloat(YYJSONValue* handle, const char* path, double value, char* error, size_t error_size) override;
virtual bool PtrAddInt(YYJSONValue* handle, const char* path, int value, char* error, size_t error_size) override;
virtual bool PtrAddInt64(YYJSONValue* handle, const char* path, int64_t value, char* error, size_t error_size) override;
virtual bool PtrAddString(YYJSONValue* handle, const char* path, const char* value, char* error, size_t error_size) override;
virtual bool PtrAddNull(YYJSONValue* handle, const char* path, char* error, size_t error_size) override;
virtual bool PtrRemove(YYJSONValue* handle, const char* path, char* error, size_t error_size) override;
virtual YYJSONValue* PtrTryGet(YYJSONValue* handle, const char* path) override;
virtual bool PtrTryGetBool(YYJSONValue* handle, const char* path, bool* out_value) override;
virtual bool PtrTryGetFloat(YYJSONValue* handle, const char* path, double* out_value) override;
virtual bool PtrTryGetInt(YYJSONValue* handle, const char* path, int* out_value) override;
virtual bool PtrTryGetInt64(YYJSONValue* handle, const char* path, int64_t* out_value) override;
virtual bool PtrTryGetString(YYJSONValue* handle, const char* path, const char** out_str, size_t* out_len) override;
// ========== Iterator Operations ==========
virtual bool ObjectForeachNext(YYJSONValue* handle, const char** out_key,
size_t* out_key_len, YYJSONValue** out_value) override;
virtual bool ArrayForeachNext(YYJSONValue* handle, size_t* out_index,
YYJSONValue** out_value) override;
virtual bool ObjectForeachKeyNext(YYJSONValue* handle, const char** out_key,
size_t* out_key_len) override;
virtual bool ArrayForeachIndexNext(YYJSONValue* handle, size_t* out_index) override;
// ========== Release Operations ==========
virtual void Release(YYJSONValue* value) override;
// ========== Handle Type Operations ==========
virtual HandleType_t GetHandleType() override;
// ========== Handle Operations ==========
virtual YYJSONValue* GetFromHandle(IPluginContext* pContext, Handle_t handle) override;
private:
std::random_device m_randomDevice;
std::mt19937 m_randomGenerator;
// Helper methods
static std::unique_ptr<YYJSONValue> CreateWrapper();
static std::shared_ptr<yyjson_mut_doc> WrapDocument(yyjson_mut_doc* doc);
static std::shared_ptr<yyjson_mut_doc> CopyDocument(yyjson_doc* doc);
static std::shared_ptr<yyjson_mut_doc> CreateDocument();
static std::shared_ptr<yyjson_doc> WrapImmutableDocument(yyjson_doc* doc);
// Pack helper methods
static const char* SkipSeparators(const char* ptr);
static void SetPackError(char* error, size_t error_size, const char* fmt, ...);
static yyjson_mut_val* PackImpl(yyjson_mut_doc* doc, const char* format,
IPackParamProvider* provider, char* error,
size_t error_size, const char** out_end_ptr);
};
#endif // _INCLUDE_SM_YYJSON_YYJSONMANAGER_H_

View File

@ -1,57 +0,0 @@
#include "extension.h"
#include "YYJSONManager.h"
JsonExtension g_JsonExtension;
SMEXT_LINK(&g_JsonExtension);
HandleType_t g_htJSON;
JSONHandler g_JSONHandler;
IYYJSONManager* g_pYYJSONManager;
bool JsonExtension::SDK_OnLoad(char* error, size_t maxlen, bool late)
{
sharesys->AddNatives(myself, json_natives);
sharesys->RegisterLibrary(myself, "yyjson");
HandleAccess haJSON;
handlesys->InitAccessDefaults(nullptr, &haJSON);
haJSON.access[HandleAccess_Read] = 0;
haJSON.access[HandleAccess_Delete] = 0;
HandleError err;
g_htJSON = handlesys->CreateType("YYJSON", &g_JSONHandler, 0, nullptr, &haJSON, myself->GetIdentity(), &err);
if (!g_htJSON) {
snprintf(error, maxlen, "Failed to create YYJSON handle type (err: %d)", err);
return false;
}
// Delete the existing instance if it exists
if (g_pYYJSONManager) {
delete g_pYYJSONManager;
g_pYYJSONManager = nullptr;
}
g_pYYJSONManager = new YYJSONManager();
if (!g_pYYJSONManager) {
snprintf(error, maxlen, "Failed to create YYJSONManager instance");
return false;
}
return sharesys->AddInterface(myself, g_pYYJSONManager);
}
void JsonExtension::SDK_OnUnload()
{
handlesys->RemoveType(g_htJSON, myself->GetIdentity());
if (g_pYYJSONManager) {
delete g_pYYJSONManager;
g_pYYJSONManager = nullptr;
}
}
void JSONHandler::OnHandleDestroy(HandleType_t type, void* object)
{
delete (YYJSONValue*)object;
}

View File

@ -1,28 +0,0 @@
#ifndef _INCLUDE_SOURCEMOD_EXTENSION_PROPER_H_
#define _INCLUDE_SOURCEMOD_EXTENSION_PROPER_H_
#include "smsdk_ext.h"
#include <yyjson.h>
#include <random>
#include "IYYJSONManager.h"
class JsonExtension : public SDKExtension
{
public:
virtual bool SDK_OnLoad(char *error, size_t maxlength, bool late);
virtual void SDK_OnUnload();
};
class JSONHandler : public IHandleTypeDispatch
{
public:
void OnHandleDestroy(HandleType_t type, void *object);
};
extern JsonExtension g_JsonExtension;
extern HandleType_t g_htJSON;
extern JSONHandler g_JSONHandler;
extern const sp_nativeinfo_t json_natives[];
extern IYYJSONManager* g_pYYJSONManager;
#endif // _INCLUDE_SOURCEMOD_EXTENSION_PROPER_H_

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff