1 /**************************************************************************
3 * Copyright 2013-2014 RAD Game Tools and Valve Software
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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
24 **************************************************************************/
31 #include <sys/types.h>
34 #include <vogl_core.h>
35 #include <vogl_json.h>
41 #include "launchsteamgame.h"
44 std::string url_encode(const std::string &value);
47 LaunchSteamGame(unsigned int buffer_size, char *buffer)
49 vogl::json_document cur_doc;
51 const char *pvogl_cmd_params = NULL;
52 const char *gameport = NULL;
58 cur_doc.deserialize(buffer);
60 vogl::json_node *pjson_node = cur_doc.get_root()->find_child_object("parameters");
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);
67 std::string VOGL_CMD_LINE;
69 // Get the full path to our executable.
70 // set up LD_PRELOAD string
71 std::string LD_PRELOAD = "LD_PRELOAD=";
73 LD_PRELOAD += "libvogltrace32.so:$LD_PRELOAD";
75 LD_PRELOAD += "libvogltrace64.so:$LD_PRELOAD";
77 printf("\n%s\n", LD_PRELOAD.c_str());
79 // set up VOGL_CMD_LINE string
80 if ( (NULL != pvogl_cmd_params && pvogl_cmd_params[0] != '\0')
81 || (gameport != NULL && gameport[0] != '\0'))
83 VOGL_CMD_LINE += "VOGL_CMD_LINE=\"";
85 if (NULL != pvogl_cmd_params && pvogl_cmd_params[0] != '\0')
87 VOGL_CMD_LINE += pvogl_cmd_params;
90 if (NULL != gameport && gameport[0] != '\0')
92 VOGL_CMD_LINE += " --vogl_traceport ";
93 VOGL_CMD_LINE += gameport;
96 VOGL_CMD_LINE += "\"";
98 printf("\n%s\n", VOGL_CMD_LINE.c_str());
101 std::string steam_str = "steam steam://run/";
102 steam_str += pGameId;
104 std::string steam_args = LD_PRELOAD + " " + VOGL_CMD_LINE + " %command%";
105 std::string steam_fullcmd = steam_str + url_encode(steam_args);
107 printf("\nlaunch string:\n%s\n", steam_fullcmd.c_str());
109 system(steam_fullcmd.c_str());
111 return (NULL == gameport? 0: atoi(gameport));
117 url_encode(const std::string &value)
119 std::ostringstream escaped;
124 for (std::string::const_iterator i = value.begin(), n = value.end(); i != n; ++i)
126 std::string::value_type c = (*i);
128 if (isalnum(c) || c == '-' || c == '_' || c == '.' || c == '~')
133 escaped << '%' << std::setw(2) << ((int) c) << std::setw(0);
136 return escaped.str();
142 LaunchSteamGameReq(const char *gameid, const char *vogl_cmd_params, int bitness, const char *gameport, unsigned int *pbuffer_size, char **pbuffer)
144 vogl::json_document cur_doc;
145 vogl::dynamic_string dst;
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);
156 cur_doc.serialize(dst);
158 cbBuff = dst.get_len() + 1 + sizeof(int32_t);
159 pbBuff = (char *)malloc(cbBuff);
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));
171 *pbuffer_size = cbBuff;