5 namespace GameHub
.Utils
9 private static string load_file(string path
, string file
="")
11 var f
= FSUtils
.file(path
, file
);
12 if(!f
.query_exists()) return "";
16 FileUtils
.get_contents(f
.get_path(), out data
);
25 private static Message
prepare_message(string url
, string method
="GET", string? auth
= null, HashMap
<string, string>? headers
= null)
27 var message
= new
Message(method
, url
);
31 message
.request_headers
.append("Authorization", "Bearer " + auth
);
36 foreach(var header
in headers
.entries
)
38 message
.request_headers
.append(header
.key
, header
.value
);
45 private static string load_remote_file(string url
, string method
="GET", string? auth
= null, HashMap
<string, string>? headers
= null)
47 var session
= new
Session();
48 var message
= prepare_message(url
, method
, auth
, headers
);
50 var status
= session
.send_message(message
);
51 if (status
== 200) return (string) message
.response_body
.data
;
55 private static async
string load_remote_file_async(string url
, string method
="GET", string? auth
= null, HashMap
<string, string>? headers
= null)
58 var session
= new
Session();
59 var message
= prepare_message(url
, method
, auth
, headers
);
61 session
.queue_message(message
, (s
, m
) => {
62 if(m
.status_code
== 200) result
= (string) m
.response_body
.data
;
63 Idle
.add(load_remote_file_async
.callback);
69 public static Json
.Node
parse_json(string json
)
73 var parser
= new Json
.Parser();
74 parser
.load_from_data(json
);
75 return parser
.get_root();
81 return new Json
.Node(Json
.NodeType
.NULL
);
84 public static Json
.Node
parse_vdf(string vdf
)
86 return parse_json(vdf_to_json(vdf
));
89 public static Json
.Node
parse_json_file(string path
, string file
="")
91 return parse_json(load_file(path
, file
));
94 public static Json
.Node
parse_vdf_file(string path
, string file
="")
96 return parse_vdf(load_file(path
, file
));
99 public static Json
.Node
parse_remote_json_file(string url
, string method
="GET", string? auth
= null, HashMap
<string, string>? headers
= null)
101 return parse_json(load_remote_file(url
, method
, auth
, headers
));
104 public static Json
.Node
parse_remote_vdf_file(string url
, string method
="GET", string? auth
= null, HashMap
<string, string>? headers
= null)
106 return parse_vdf(load_remote_file(url
, method
, auth
, headers
));
109 public static async Json
.Node
parse_remote_json_file_async(string url
, string method
="GET", string? auth
= null, HashMap
<string, string>? headers
= null)
111 return parse_json(yield
load_remote_file_async(url
, method
, auth
, headers
));
114 public static async Json
.Node
parse_remote_vdf_file_async(string url
, string method
="GET", string? auth
= null, HashMap
<string, string>? headers
= null)
116 return parse_vdf(yield
load_remote_file_async(url
, method
, auth
, headers
));
119 public static Json
.Object?
json_object(Json
.Node root
, string[] keys
)
121 Json
.Object? obj
= root
.get_object();
123 foreach(var key
in keys
)
125 if(obj
!= null && obj
.has_member(key
)) obj
= obj
.get_object_member(key
);
128 if(obj
== null) break;
134 private static string vdf_to_json(string vdf_data
)
140 var nl_commas
= new
Regex("(\"|\\})(\\s*?\\r?\\n\\s*?\")");
141 var semicolons
= new
Regex("\"(\\s*?\\r?\\n?\\s*?(?:\"|\\{))");
143 json
= nl_commas
.replace(json
, json
.length
, 0, "\\g<1>,\\g<2>");
144 json
= semicolons
.replace(json
, json
.length
, 0, "\":\\g<1>");
151 return "{" + json
+ "}";