]> git.cworth.org Git - vogl/blob - src/common/launchsteamgame.cpp
Initial vogl checkin
[vogl] / src / common / launchsteamgame.cpp
1 /**************************************************************************
2  *
3  * Copyright 2013-2014 RAD Game Tools and Valve Software
4  * All Rights Reserved.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  * THE SOFTWARE.
23  *
24  **************************************************************************/
25  
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <unistd.h>
29 #include <errno.h>
30 #include <string.h>
31 #include <sys/types.h>
32 #include <time.h>
33
34 #include <vogl_core.h>
35 #include <vogl_json.h>
36
37 #include <iomanip>
38 #include <sstream>
39
40 #include "commands.h"
41 #include "launchsteamgame.h"
42
43
44 std::string url_encode(const std::string &value);
45
46 int 
47 LaunchSteamGame(unsigned int buffer_size, char *buffer)
48 {
49     vogl::json_document cur_doc;
50     const char *pGameId;
51     const char *pvogl_cmd_params = NULL;
52     const char *gameport = NULL;
53     int bitness = 0;
54
55     if (0 >= buffer_size)
56         return -1;
57
58     cur_doc.deserialize(buffer);
59
60     vogl::json_node *pjson_node = cur_doc.get_root()->find_child_object("parameters");
61
62     pGameId = pjson_node->value_as_string_ptr("gameid", "");
63     pvogl_cmd_params = pjson_node->value_as_string_ptr("vogl_cmd_params", NULL);
64     bitness = pjson_node->value_as_int("bitness", 0);
65     gameport = pjson_node->value_as_string_ptr("gameport", NULL);
66
67     std::string VOGL_CMD_LINE;
68
69     // Get the full path to our executable.
70     // set up LD_PRELOAD string
71     std::string LD_PRELOAD = "LD_PRELOAD=";
72     if (32 == bitness)
73         LD_PRELOAD += "libvogltrace32.so:$LD_PRELOAD";
74     else
75         LD_PRELOAD += "libvogltrace64.so:$LD_PRELOAD";
76
77     printf("\n%s\n", LD_PRELOAD.c_str());
78
79     // set up VOGL_CMD_LINE string
80     if ( (NULL != pvogl_cmd_params && pvogl_cmd_params[0] != '\0') 
81       || (gameport != NULL && gameport[0] != '\0'))
82     {
83         VOGL_CMD_LINE += "VOGL_CMD_LINE=\"";
84
85         if (NULL != pvogl_cmd_params && pvogl_cmd_params[0] != '\0')
86         {
87             VOGL_CMD_LINE += pvogl_cmd_params;
88         }
89
90         if (NULL != gameport && gameport[0] != '\0')
91         {
92             VOGL_CMD_LINE += " --vogl_traceport ";
93             VOGL_CMD_LINE += gameport;
94         }
95
96         VOGL_CMD_LINE += "\"";
97
98         printf("\n%s\n", VOGL_CMD_LINE.c_str());
99     }
100
101     std::string steam_str = "steam steam://run/";
102     steam_str += pGameId;
103     steam_str += "//";
104     std::string steam_args = LD_PRELOAD + " " + VOGL_CMD_LINE + " %command%";
105     std::string steam_fullcmd = steam_str + url_encode(steam_args);
106
107     printf("\nlaunch string:\n%s\n", steam_fullcmd.c_str());
108
109     system(steam_fullcmd.c_str());
110
111     return (NULL == gameport? 0: atoi(gameport));
112 }
113
114
115
116 std::string
117 url_encode(const std::string &value)
118 {
119     std::ostringstream escaped;
120
121     escaped.fill('0');
122     escaped << std::hex;
123
124     for (std::string::const_iterator i = value.begin(), n = value.end(); i != n; ++i)
125     {
126         std::string::value_type c = (*i);
127
128         if (isalnum(c) || c == '-' || c == '_' || c == '.' || c == '~')
129             escaped << c;
130         else if (c == ' ')
131             escaped << "%20";
132         else
133             escaped << '%' << std::setw(2) << ((int) c) << std::setw(0);
134     }
135
136     return escaped.str();
137 }
138
139
140
141 int
142 LaunchSteamGameReq(const char *gameid, const char *vogl_cmd_params, int bitness, const char *gameport, unsigned int *pbuffer_size, char **pbuffer)
143 {
144     vogl::json_document cur_doc;
145     vogl::dynamic_string dst;
146
147     char *pbBuff;
148     unsigned int cbBuff;
149
150     vogl::json_node &meta_node = cur_doc.get_root()->add_object("parameters");
151     meta_node.add_key_value("gameid", gameid);
152     meta_node.add_key_value("vogl_cmd_params", vogl_cmd_params);
153     meta_node.add_key_value("bitness", bitness);
154     meta_node.add_key_value("gameport", gameport);
155
156     cur_doc.serialize(dst);
157
158     cbBuff = dst.get_len() + 1 + sizeof(int32_t);
159     pbBuff = (char *)malloc(cbBuff);
160     if (NULL == pbBuff)
161     {
162         printf("OOM\n");
163         return -1;
164     }
165
166     //  First part of buffer is the command id
167     *((int32_t *)pbBuff) = LAUNCHSTEAMGAME;
168     strncpy((char *)(pbBuff+sizeof(int32_t)), dst.get_ptr(), cbBuff - sizeof(int32_t));
169
170     *pbuffer = pbBuff;
171     *pbuffer_size = cbBuff;
172
173     return 0;
174 }
175