Skip to content

Instantly share code, notes, and snippets.

@headswe
Created October 13, 2015 19:13
Show Gist options
  • Save headswe/550e8baeff84fc24be31 to your computer and use it in GitHub Desktop.
Save headswe/550e8baeff84fc24be31 to your computer and use it in GitHub Desktop.
JSON exporter
#include "debug_console.hpp"
conClear();
_items = [];
#define TAB 9
#define BACKWARDSLASH 92
#define QUOTE 34
KK_fnc_insert = {
private ["_arr", "_i", "_res"];
_arr = _this select 0;
_i = _this select 2;
_res = [];
_res append (_arr select [0, _i]);
_res append (_this select 1);
_res append (_arr select [_i, count _arr - _i]);
_res
};
_escape = {
private ["_index","_foreachindex"];
_newarr = [];
_arr = toArray _this;
{
if(_x == BACKWARDSLASH || _x == QUOTE) then
{
_newarr pushBack 92;
};
if(_x != TAB) then {
_newarr pushBack _x;
};
} foreach _arr;
toString _newarr
};
_parse = {
params ["_class"];
private ["_attributes","_children","_attrname","_data","_out","_class","_name"];
_name = configName _x;
_attributes = [];
_children = [];
{
_attrname = configName _x;
if(!isClass _x) then
{
_data = switch ( true ) do
{
case (isNumber _x): {getNumber _x};
case (isText _x): {(getText _x) call _escape};
case (isArray _x): {getArray _x};
default { nil };
};
_out = format ["%1 = %2 (%3)",_attrname,_data,_x];
conWhite(_out);
_attributes pushBack [_attrname,_data];
}
else
{
_children pushBack ([_x] call _parse);
};
} foreach (configProperties [_class,"true",true]);
[_name,_attributes,_children]
};
{
_items pushBack ([_x] call _parse);
} foreach [(configfile >> "CfgWeapons" >> "arifle_MX_ACO_pointer_F")];//
//[(configFile >> "CfgWeapons" >> "MMG_02_camo_F")];
//("true" configClasses (configFile >> "CfgWeapons"));
_printNode = {
private ["_attributes","_children","_d","_count","_dot","_attrName","_data","_foreachindex","_attrname","_totalChildren"];
(_this select 0) params ["_name","_attributes","_children"];
_d = format ['{"Name":"%1",', _name];
conFile(_d);
_count = count _attributes;
for [{_i=0},{_i<_count},{_i=_i+1}] do {
_dot = "$"; //herpderp
if(_i != (_count -1)) then {_dot = "$"};
(_attributes select _i) params ["_attrName","_data"];
if(typeName _data == "STRING") then {_data = format ['"%1"', _data]};
if(typeName _data == "ARRAY") then {
{
if(typeName _x == "STRING") then
{
_data set [_foreachindex,(_x) call _escape];
};
} forEach _data;
_data = format ['%1', _data]
};
_d = format ['"%1" : %2 %3', _attrname,_data,_dot];
conFile(_d);
};
_totalChildren = count _children;
conFile('"children" : [');
{
[_x,(_totalChildren-1) == _foreachindex] call _printNode;
} foreach _children;
conFile(']');
if(_this select 1) then
{
conFile("}");
} else {
conFile("} $");
};
};
_totalC = count _items;
conFile('[');
{
[_x,_foreachindex == _totalC-1] call _printNode;
} foreach _items;
conFile(']');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment