1 module unecht.steamaccess;
2 
3 version(EnableSteam):
4 
5 import derelict.steamworks.steamworks;
6 
7 import unecht.core.logger;
8 
9 import std.conv:to;
10 
11 ///
12 final class SteamAccess
13 {
14     this()
15     {
16         setup();
17     }
18 
19     void setup()
20     {
21         DerelictSteamworks.load();
22 
23         if(!SteamAPI_Init())
24             return log.warning("[Steam] client not running");
25 
26         client = SteamClient();
27             
28         if(!client)
29             return log.warning("[Steam] could not create client");
30             
31         clientPipe = SteamAPI_ISteamClient_CreateSteamPipe(client);
32             
33         if(!clientPipe)
34             return log.warning("[Steam] could not create clientPipe");
35             
36         userPipe = SteamAPI_ISteamClient_ConnectToGlobalUser(client, clientPipe);
37             
38         if(!userPipe)
39             return log.warning("[Steam] could not create userPipe");
40             
41         utils = SteamAPI_ISteamClient_GetISteamUtils(client, clientPipe, STEAMUTILS_INTERFACE_VERSION);
42             
43         if(!utils)
44             return log.warning("[Steam] could not create utils");
45 
46         SteamAPI_ISteamUtils_SetWarningMessageHook(utils, &warnCallback);
47             
48         screenshots = SteamAPI_ISteamClient_GetISteamScreenshots(client, userPipe, clientPipe, STEAMSCREENSHOTS_INTERFACE_VERSION);
49             
50         if(!screenshots)
51             return log.warning("[Steam] could not create screenshots");
52 
53         //TODO is that correct?
54         //SteamAPI_ISteamScreenshots_HookScreenshots(screenshots,true);
55             
56         friends = SteamAPI_ISteamClient_GetISteamFriends(client, userPipe, clientPipe, STEAMFRIENDS_INTERFACE_VERSION);
57             
58         if(!friends)
59             return log.warning("[Steam] could not create friends");
60 
61         userName = to!string(SteamAPI_ISteamFriends_GetPersonaName(friends));
62 
63         initialized = true;
64         log.infof("[Steam] up and running: '%s'",userName);
65     }
66 
67     void openOverlay(/+string overlay = "Friends"+/)
68     {
69         if(!initialized) return;
70 
71         SteamAPI_ISteamFriends_ActivateGameOverlay(friends, null);
72     }
73 
74     void triggerScreenshot()
75     {
76         if(!initialized) return;
77 
78         SteamAPI_ISteamScreenshots_TriggerScreenshot(screenshots);
79     }
80 
81     void update()
82     {
83         if(!initialized) return;
84 
85         SteamAPI_ISteamUtils_RunFrame(utils);
86     }
87 
88     private static nothrow extern(C) void warnCallback(int severity, const char * str)
89     {
90         // make nothrow
91         try log.warningf("[Steam] Warn: %s (%s)", str, severity);
92         catch(Throwable){}
93     }
94 
95 private:
96     ISteamFriends* friends;
97     ISteamUtils* utils;
98     ISteamScreenshots* screenshots;
99     ISteamClient* client;
100     HSteamPipe clientPipe;
101     HSteamPipe userPipe;
102     
103     bool initialized = false;
104 
105     string userName;
106 }