]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/menu/gamecommand.qc
e637bf5aa87a3f71bd43bf99c94f55f1fc5960a7
[xonotic/xonotic-data.pk3dir.git] / qcsrc / menu / gamecommand.qc
1 void GameCommand_Init()
2 {
3         // make gg call menu QC theCommands
4         localcmd("alias qc_cmd \"menu_cmd $*\"\n");
5 }
6
7 string _dumptree_space;
8 void _dumptree_open(entity pass, entity me)
9 {
10         string s;
11         s = me.toString(me);
12         if(s == "")
13                 s = me.classname;
14         else
15                 s = strcat(me.classname, ": ", s);
16         print(_dumptree_space, etos(me), " (", s, ")");
17         if(me.firstChild)
18         {
19                 print(" {\n");
20                 _dumptree_space = strcat(_dumptree_space, "  ");
21         }
22         else
23                 print("\n");
24 }
25 void _dumptree_close(entity pass, entity me)
26 {
27         if(me.firstChild)
28         {
29                 _dumptree_space = substring(_dumptree_space, 0, strlen(_dumptree_space) - 2);
30                 print(_dumptree_space, "}\n");
31         }
32 }
33
34 float curl_uri_get_pos;
35 float curl_uri_get_exec[URI_GET_CURL_END - URI_GET_CURL + 1];
36 string curl_uri_get_cvar[URI_GET_CURL_END - URI_GET_CURL + 1];
37 void Curl_URI_Get_Callback(float id, float status, string data)
38 {
39         float i;
40         float do_exec;
41         string do_cvar;
42         i = id - URI_GET_CURL;
43         do_exec = curl_uri_get_exec[i];
44         do_cvar = curl_uri_get_cvar[i];
45         if(status != 0)
46         {
47                 print(sprintf(_("error: status is %d\n"), status));
48                 return;
49         }
50         if(do_exec)
51                 localcmd(data);
52         if(do_cvar)
53                 cvar_set(do_cvar, data);
54         if(!do_exec && !do_cvar)
55                 print(data);
56 }
57
58 void GameCommand(string theCommand)
59 {
60         float argc;
61         argc = tokenize_console(theCommand);
62
63         if(argv(0) == "help" || argc == 0)
64         {
65                 print(_("Usage: menu_cmd command..., where possible commands are:\n"));
66                 print(_("  sync - reloads all cvars on the current menu page\n"));
67                 print(_("  directmenu ITEM - select a menu item as main item\n"));
68                 GameCommand_Generic("help");
69                 return;
70         }
71
72         if(GameCommand_Generic(theCommand))
73                 return;
74
75         if(argv(0) == "sync")
76         {
77                 m_sync();
78                 return;
79         }
80
81         if(argv(0) == "directmenu") if(argc == 2)
82         {
83                 // switch to a menu item
84                 if(!isdemo()) // don't allow this command in demos
85                         m_goto(argv(1));
86                 return;
87         }
88
89         if(argv(0) == "directpanelhudmenu")
90         {
91                 // switch to a menu item
92                 m_goto(strcat("HUD", argv(1)));
93                 return;
94         }
95
96         if(argv(0) == "skinselect")
97         {
98                 m_goto_skin_selector();
99                 return;
100         }
101
102         if(argv(0) == "languageselect")
103         {
104                 m_goto_language_selector();
105                 return;
106         }
107
108         if(argv(0) == "videosettings")
109         {
110                 m_goto_video_settings();
111                 return;
112         }
113
114         if(argv(0) == "dumptree")
115         {
116                 _dumptree_space = "";
117                 depthfirst(main, parent, firstChild, nextSibling, _dumptree_open, _dumptree_close, NULL);
118                 return;
119         }
120
121         if(argv(0) == "curl")
122         {
123                 float do_exec;
124                 string do_cvar;
125                 float key;
126                 float i, j;
127                 string url;
128                 float buf;
129                 float r;
130
131                 do_exec = FALSE;
132                 do_cvar = string_null;
133                 key = -1;
134
135                 for(i = 1; i+1 < argc; ++i)
136                 {
137                         if(argv(i) == "--cvar" && i+2 < argc)
138                         {
139                                 ++i;
140                                 do_cvar = argv(i);
141                                 continue;
142                         }
143                         if(argv(i) == "--exec")
144                         {
145                                 do_exec = TRUE;
146                                 continue;
147                         }
148                         if(argv(i) == "--key" && i+2 < argc)
149                         {
150                                 ++i;
151                                 key = stof(argv(i));
152                                 continue;
153                         }
154                         break;
155                 }
156
157                 // now, argv(i) is the URL
158                 // following args may be POST parameters
159                 url = argv(i);
160                 ++i;
161                 buf = buf_create();
162                 j = 0;
163                 for(; i+1 < argc; i += 2)
164                         bufstr_set(buf, ++j, sprintf("%s=%s", uri_escape(argv(i)), uri_escape(argv(i+1))));
165                 if(i < argc)
166                         bufstr_set(buf, ++j, sprintf("submit=%s", uri_escape(argv(i))));
167
168                 if(j == 0) // no args: GET
169                         r = crypto_uri_postbuf(url, URI_GET_CURL + curl_uri_get_pos, string_null, string_null, -1, key);
170                 else // with args: POST
171                         r = crypto_uri_postbuf(url, URI_GET_CURL + curl_uri_get_pos, "application/x-www-form-urlencoded", "&", buf, key);
172
173                 if(r)
174                 {
175                         curl_uri_get_exec[curl_uri_get_pos] = do_exec;
176                         curl_uri_get_cvar[curl_uri_get_pos] = do_cvar;
177                         curl_uri_get_pos = mod(curl_uri_get_pos + 1, URI_GET_CURL_END - URI_GET_CURL + 1);
178                 }
179                 else
180                         print(_("error creating curl handle\n"));
181
182                 buf_del(buf);
183
184                 return;
185         }
186
187         print(_("Invalid command. For a list of supported commands, try menu_cmd help.\n"));
188 }