Last active
September 13, 2019 23:50
-
-
Save katsaii/fc3f55075fff4a8213097cf462345efd to your computer and use it in GitHub Desktop.
Finds the nested data structure type from a Game Maker list.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// @desc Returns the type of data structure nested at an index of a ds_list. | |
/// @param id {Integer} The id of the ds_list to consider. | |
/// @param pos {Integer} The index of the ds_list to inspect. | |
/// @author Kat @katsaii | |
if (is_real(argument0[| argument1])) { | |
// copy the ds_list into a duplicate list that can be manipulated freely | |
var clone = ds_list_create(); | |
ds_list_copy(json_list, argument0); | |
// delete all records other than what's required | |
repeat (argument1) { | |
// trim the left hand side of the list | |
json_list[| 0] = noone; | |
/* This prevents nested structures from being deleted when we remove | |
* the record from the cloned map. | |
*/ | |
ds_list_delete(json_list, 0); | |
} | |
repeat (ds_list_size(json_list) - 1) { | |
// trim the right hand side of the list | |
json_list[| 1] = noone; // see above | |
ds_list_delete(json_list,1); | |
} | |
// encode the final json and free dynamic structures | |
var obj = ds_map_create(); | |
ds_map_add_list(obj, "list", clone); | |
var context = json_encode(obj); | |
clone[| 0] = noone; // see above | |
ds_map_destroy(obj); | |
// use the json string to determine the type of the value contained | |
var trim_start = string_pos("[", context) + 2; | |
var trim_end = string_length(context) - 3; | |
var trimmed = string_copy(context, | |
trim_start, | |
trim_end - trim_start); | |
switch (string_char_at(trimmed, 1)) { | |
// the first character is enough information to infer the type of structure | |
case "[": | |
return ds_type_list; | |
break; | |
case "{": | |
return ds_type_map; | |
break; | |
} | |
} | |
// if the key is not real, there is no chance of it being a data structure | |
return -1; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment