GithubHelp home page GithubHelp logo

hual / sampson Goto Github PK

View Code? Open in Web Editor NEW
24.0 6.0 7.0 3.2 MB

SAMPSON - A JSON plugin for SA-MP

License: MIT License

C++ 92.01% Makefile 2.91% C 3.99% SourcePawn 0.33% Roff 0.41% PHP 0.30% Pawn 0.06%
c-plus-plus json sa-mp

sampson's Introduction

SAMPSON

SAMPSON - A JSON plugin for SA-MP

Why?

I decided to build this plugin as a fellow member of the SA-MP community was having issues with DJSON. DJSON is also outdated and does not conform to various JSON standards. It caches to disk, has a large memory footprint, and uses SQL for... JSON storage. It seemed wrong to me, so I decided to build my own plugin.

Features:

The plugin currently features a JSON parser for files located in your 'scriptfiles' directory. It's compliant to the JSON standards and can parse strings, numbers, booleans and arrays.

Documentation:

I'll be doing tutorials on the usage of this plugin, and a Wiki for all the functions. But for now, I'll just dump the functions with basic information about them here:

You can define a node delimiter by defining JSON_DEFAULT_DELIMITER to a char; Default delimiter is

#define JSON_DEFAULT_DELIMITER '/'

json_parse_file
native JSONNode:json_parse_file(const file_path[])

Function: Reads a file, parses it, and stores it in memory for further parsing.
Returns: A handle to the root node.

json_parse_string
native JSONNode:json_parse_string(const string[])

Function: Parses a string and stores it in memory for further parsing.
Returns: A handle to the root node.

json_close
native json_close(JSONNode:node)

Function: Closes a JSON file with root node 'node' and frees all memory associated with it.
Returns: 1 on success, 0 on failure.

json_get_type
native json_get_type(JSONNode:node, const path[] = '\0', const path_delim = JSON_DEFAULT_DELIMITER)

Function: Gets the type of a node's value.
Returns: An integer representing the node's value type. See below for type definitions.

JSON_NULL
JSON_STRING
JSON_NUMBER
JSON_BOOL
JSON_ARRAY
JSON_NODE
json_get_node
native JSONNode:json_get_node(JSONNode:node, const path[], const path_delim = JSON_DEFAULT_DELIMITER)

Function: Gets the node at some path using 'node' as a root node.
Returns: The node on success, 0 if the node doesn't exist.

json_get_bool
native bool:json_get_bool(JSONNode:node, const path[] = '\0', const path_delim = JSON_DEFAULT_DELIMITER)

Function: Gets the value of some path using 'node' as a root node as a boolean.
Returns: The value of the specified path's key as a boolean.

json_get_int
native json_get_int(JSONNode:node, const path[] = '\0', const path_delim = JSON_DEFAULT_DELIMITER)

Function: Gets the value of some path using 'node' as a root node as an integer.
Returns: The value of the specified path's key as an integer.

json_get_float
native Float:json_get_float(JSONNode:node, const path[] = '\0', const path_delim = JSON_DEFAULT_DELIMITER)

Function: Gets the value of some path using 'node' as a root node as a float.
Returns: The value of the specified path's key as a float.

json_get_string
native json_get_string(JSONNode:node, dst[], const len = sizeof(dst), const path[] = '\0', const bool:packed = false, const path_delim = JSON_DEFAULT_DELIMITER)

Function: Gets the value of some path using 'node' as a root node as a string and writes it to 'dst'.
Returns: 1.

json_get_name
native json_get_name(JSONNode:node, dst[], const len = sizeof(dst), const bool:packed = false)

Function: Gets the name (key) of some path using 'node' as a root node and writes it to 'dst'.
Returns: 1 if the node has a name (key) associated with it, 0 if it doesn't.

json_get_array
native JSONArray:json_get_array(JSONNode:node, const path[] = '\0', const path_delim = JSON_DEFAULT_DELIMITER)

Function: Gets the value of some path using 'node' as a root node as an array.
Returns: A JSON array type which is used for array functions.

json_array_count
native json_array_count(JSONArray:array)

Function: Gets the amount of elements 'array' contains.
Returns: An integer representing the amount of elements 'array' contains.

json_array_at
native JSONNode:json_array_at(JSONArray:array, const index)

Function: Gets the element at 'index' in 'array' as a node.
Returns: The element as a JSON node.

Installation

Download the release archive and place its contents into your server folder. Open your configuration file and add 'SAMPSON' ('SAMPSON.so' on Linux) to your 'plugins' line.

Building from source

  1. Download the source code from the link provided above.
  2. On Windows, open the 'SAMPSON.sln' file in Visual Studio, select 'Release' build target and then build it.
  3. On Linux, run 'make' in the 'src' folder. The built plugin should be found in the 'build' folder.

Credits

  • The SA-MP team - SA-MP, SA-MP plugin SDK
  • Djole1337 - Testing support, Linux build support

sampson's People

Contributors

hual avatar jtylr avatar michaelbelgium avatar southclaws avatar thomas14 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

sampson's Issues

Node types are interpreted differently on each OS

I just upgrade to this since DJson (despite having write capability) was fucking SQLite up and decided to just wait for writing to be added to this (or add it myself). But it seems to be failing to read a lot of the settings on the linux server, I found this was because my type checks were returning odd values:

Both these tests loaded the same settings.json file, it has Unix line endings on both the linux and windows machine (exact same file).

Windows server output: everything loads fine

Loading Settings...
[07/04/2015 10:58:13] [JSON] type of 'server/motd' is 1
[07/04/2015 10:58:13] server/motd: Current bug with vehicles on restarts, always exit a vehicle before a restart!
[07/04/2015 10:58:13] [JSON] type of 'server/website' is 1
[07/04/2015 10:58:13] server/website: forum.scavengesurvive.com
[07/04/2015 10:58:13] [JSON] type of 'server/rules' is 4
[07/04/2015 10:58:13] server/rules[0]: 1. Only English in global chat. Please use the radio (/chatinfo) for other languages.
[07/04/2015 10:58:13] server/rules[1]: 2. No global chat use if local or radio is available. (/chatinfo) You will be muted for spam.
[07/04/2015 10:58:13] server/rules[2]: 3. No hacking, cheating or client modifications that give you advantages.
[07/04/2015 10:58:13] server/rules[3]: 4. No exploiting of map bugs such as hiding/building bases inside models.
[07/04/2015 10:58:13] server/rules[4]: 5. No exploiting server bugs, report them using the /bug command.
[07/04/2015 10:58:13] server/rules[5]: 6. No arguments, flaming or discrimination. You will be muted!
[07/04/2015 10:58:13] server/rules[6]: 7. You may insult a player's skills in the game, but don't make things personal.
[07/04/2015 10:58:13] server/rules[7]: 8. Report all hackers, playing with/alongside them will be punished.
[07/04/2015 10:58:13] server/rules[8]: 9. One account per person, break this rule and you and your base will get deleted.
[07/04/2015 10:58:13] [JSON] type of 'server/staff' is 4
[07/04/2015 10:58:13] server/staff[0]: Southclaw
[07/04/2015 10:58:13] server/staff[1]: Atomsk
[07/04/2015 10:58:13] server/staff[2]: VIRUXE
[07/04/2015 10:58:13] [JSON] type of 'server/max-uptime' is 2
[07/04/2015 10:58:13] server/max-uptime: 18000

And this is the linux log of the same loading sequence:

Loading Settings...
[07/04/2015 10:33:54] [JSON] type of 'server/motd' is 1
[07/04/2015 10:33:54] server/motd: Experimental testing build is currently in use. Last backup was 2015/04/04. Always exit a vehicle before a restart!
[07/04/2015 10:33:54] [JSON] type of 'server/website' is 1
[07/04/2015 10:33:54] server/website: forum.scavengesurvive.com
[07/04/2015 10:33:54] [JSON] type of 'server/rules' is 0
[07/04/2015 10:33:54] WARNING: JSON writing currently not supported by KingHual's plugin! Cannot auto-write default values.
[07/04/2015 10:33:54] [JSON] type of 'server/staff' is 4
[07/04/2015 10:33:54] server/staff[0]: Southclaw
[07/04/2015 10:33:54] server/staff[1]: Atomsk
[07/04/2015 10:33:54] server/staff[2]: VIRUXE
[07/04/2015 10:33:54] [JSON] type of 'server/max-uptime' is 2
[07/04/2015 10:33:54] server/max-uptime: 18000

It interpreted the node type of 'server/rules' as 0 which resulted in that message getting printed whereas I expected it to return 4 for an array type. This happens all over the load sequence with various settings and no visible pattern to it, the nodes it affects are ints, floats, strings, arrays, etc.

Here's the extract from that file:

{
  "server":{
    "file-check":0,
    "global-debug-level":0,
    "loot-spawn-multiplier":1.0,
    "max-uptime":18000,
    "motd":"Current bug with vehicles on restarts, always exit a vehicle before a restart!",
    "rules":[
      "1. Only English in global chat. Please use the radio (/chatinfo) for other languages.",
      "2. No global chat use if local or radio is available. (/chatinfo) You will be muted for spam.",
      "3. No hacking, cheating or client modifications that give you advantages.",
      "4. No exploiting of map bugs such as hiding/building bases inside models.",
      "5. No exploiting server bugs, report them using the /bug command.",
      "6. No arguments, flaming or discrimination. You will be muted!",
      "7. You may insult a player's skills in the game, but don't make things personal.",
      "8. Report all hackers, playing with/alongside them will be punished.",
      "9. One account per person, break this rule and you and your base will get deleted."
    ],
    "staff":[
      "Southclaw",
      "Atomsk",
      "VIRUXE"
    ],
    "website":"forum.scavengesurvive.com",
    "whitelist":0,
    "whitelist-auto-toggle":0
  },
...etc

And the loading functions are located here except the latest version uses if(json_get_type(json, path) != JSON_TYPE) instead of if(!json_get_node(json, path)) which also didn't work on these particular nodes.
(ignore the fact that I try to print a multidimensional on line 163, I thought that might have been the cause at first but then realised the error pops up just before any arrays are read anyway).

I would switch back to DJson but for some reason it just fucks SQLitei up completely so for now I just have to hardcode the settings until this is fixed (I'd try to help but am in no way a C++ expert!)

json_open issue.

json_open - crashes the server if the file doesn't exist rather than returning -1.

Memory issue.

for (;;) {
for (new i = 0; i != count; ++ i) {
json_get_string(json_array_at(array, i), tmp); // This caused memory leak at each iteration!
}
}

Add support for JSON writing too..

similar to @Southclaws 's requests plugin

native Node:JsonObject({_, Node}:...);
native Node:JsonInt(value);
native Node:JsonBool(bool:value);
native Node:JsonFloat(Float:value);
native Node:JsonString(const value[]);
//........

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.