]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/menu/command/menu_cmd.qc
Merge remote branch 'origin/master' into samual/updatecommands
[xonotic/xonotic-data.pk3dir.git] / qcsrc / menu / command / menu_cmd.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                 if(do_cvar)
49                         strunzone(do_cvar);
50                 return;
51         }
52         if(do_exec)
53                 localcmd(data);
54         if(do_cvar)
55         {
56                 cvar_set(do_cvar, data);
57                 strunzone(do_cvar);
58         }
59         if(!do_exec && !do_cvar)
60                 print(data);
61 }
62
63 void GameCommand(string theCommand)
64 {
65         float argc;
66         argc = tokenize_console(theCommand);
67
68         if(argv(0) == "help" || argc == 0)
69         {
70                 print(_("Usage: menu_cmd command..., where possible commands are:\n"));
71                 print(_("  sync - reloads all cvars on the current menu page\n"));
72                 print(_("  directmenu ITEM - select a menu item as main item\n"));
73                 GameCommand_Generic("help");
74                 return;
75         }
76
77         if(GameCommand_Generic(theCommand))
78                 return;
79
80         if(argv(0) == "sync")
81         {
82                 m_sync();
83                 return;
84         }
85
86         if(argv(0) == "directmenu") if(argc == 2)
87         {
88                 // switch to a menu item
89                 if(!isdemo()) // don't allow this command in demos
90                         m_goto(argv(1));
91                 return;
92         }
93
94         if(argv(0) == "directpanelhudmenu")
95         {
96                 // switch to a menu item
97                 m_goto(strcat("HUD", argv(1)));
98                 return;
99         }
100
101         if(argv(0) == "skinselect")
102         {
103                 m_goto_skin_selector();
104                 return;
105         }
106
107         if(argv(0) == "languageselect")
108         {
109                 m_goto_language_selector();
110                 return;
111         }
112
113         if(argv(0) == "videosettings")
114         {
115                 m_goto_video_settings();
116                 return;
117         }
118
119         if(argv(0) == "dumptree")
120         {
121                 _dumptree_space = "";
122                 depthfirst(main, parent, firstChild, nextSibling, _dumptree_open, _dumptree_close, NULL);
123                 return;
124         }
125
126         if(argv(0) == "curl")
127         {
128                 float do_exec;
129                 string do_cvar;
130                 float key;
131                 float i, j;
132                 string url;
133                 float buf;
134                 float r;
135
136                 do_exec = FALSE;
137                 do_cvar = string_null;
138                 key = -1;
139
140                 for(i = 1; i+1 < argc; ++i)
141                 {
142                         if(argv(i) == "--cvar" && i+2 < argc)
143                         {
144                                 ++i;
145                                 do_cvar = strzone(argv(i));
146                                 continue;
147                         }
148                         if(argv(i) == "--exec")
149                         {
150                                 do_exec = TRUE;
151                                 continue;
152                         }
153                         if(argv(i) == "--key" && i+2 < argc)
154                         {
155                                 ++i;
156                                 key = stof(argv(i));
157                                 continue;
158                         }
159                         break;
160                 }
161
162                 // now, argv(i) is the URL
163                 // following args may be POST parameters
164                 url = argv(i);
165                 ++i;
166                 buf = buf_create();
167                 j = 0;
168                 for(; i+1 < argc; i += 2)
169                         bufstr_set(buf, ++j, sprintf("%s=%s", uri_escape(argv(i)), uri_escape(argv(i+1))));
170                 if(i < argc)
171                         bufstr_set(buf, ++j, sprintf("submit=%s", uri_escape(argv(i))));
172
173                 if(j == 0) // no args: GET
174                         r = crypto_uri_postbuf(url, URI_GET_CURL + curl_uri_get_pos, string_null, string_null, -1, key);
175                 else // with args: POST
176                         r = crypto_uri_postbuf(url, URI_GET_CURL + curl_uri_get_pos, "application/x-www-form-urlencoded", "&", buf, key);
177
178                 if(r)
179                 {
180                         curl_uri_get_exec[curl_uri_get_pos] = do_exec;
181                         curl_uri_get_cvar[curl_uri_get_pos] = do_cvar;
182                         curl_uri_get_pos = mod(curl_uri_get_pos + 1, URI_GET_CURL_END - URI_GET_CURL + 1);
183                 }
184                 else
185                         print(_("error creating curl handle\n"));
186
187                 buf_del(buf);
188
189                 return;
190         }
191
192         print(_("Invalid command. For a list of supported commands, try menu_cmd help.\n"));
193 }