File: _renpysteam.pyx

package info (click to toggle)
renpy 7.1.3%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 107,352 kB
  • sloc: python: 42,124; ansic: 4,781; makefile: 43; sh: 14
file content (561 lines) | stat: -rw-r--r-- 13,674 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
# Copyright 2004-2018 Tom Rothamel <pytom@bishoujo.us>
#
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation files
# (the "Software"), to deal in the Software without restriction,
# including without limitation the rights to use, copy, modify, merge,
# publish, distribute, sublicense, and/or sell copies of the Software,
# and to permit persons to whom the Software is furnished to do so,
# subject to the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

from __future__ import print_function

version = 2

cdef extern from "steam/steam_api.h":
    ctypedef bint bool
    ctypedef int int32
    ctypedef unsigned int uint32
    ctypedef unsigned long long uint64

    # Init.

    bool SteamAPI_Init()
    void SteamAPI_RunCallbacks()
    bool SteamAPI_RestartAppIfNecessary(unsigned int)

    # Stats & Achievements.

    cdef cppclass ISteamUserStats:
        bool RequestCurrentStats()
        bool StoreStats()

        bool GetAchievement(const char *pchName, bool *pbAchieved);
        bool SetAchievement(const char *pchName);
        bool ClearAchievement(const char *pchName);
        bool IndicateAchievementProgress(const char *pchName, uint32 nCurProgress, uint32 nMaxProgress)
        int GetNumAchievements()
        char *GetAchievementName(int)

        bool GetStat(const char *pchName, int32 *pData)
        bool GetStat(const char *pchName, float *pData)
        bool SetStat(const char *pchName, int32 iData)
        bool SetStat(const char *pchName, float fData)

    ctypedef struct UserStatsReceived_t
    ISteamUserStats *SteamUserStats()

    # Utils.

    cdef enum ENotificationPosition:
        k_EPositionTopLeft
        k_EPositionTopRight
        k_EPositionBottomLeft
        k_EPositionBottomRight

    cdef cppclass ISteamUtils:
        bint IsOverlayEnabled()
        void SetOverlayNotificationPosition(ENotificationPosition eNotificationPosition)
        bool BOverlayNeedsPresent()
        char *GetSteamUILanguage()

    ISteamUtils *SteamUtils()

    # Apps.

    ctypedef int AppId_t

    cdef cppclass ISteamApps:
        bool BIsSubscribedApp(AppId_t appID)
        const char *GetCurrentGameLanguage()
        bool GetCurrentBetaName( char *pchName, int cchNameBufferSize );

        bool BIsDlcInstalled(AppId_t nAppID)
        void InstallDLC(AppId_t nAppID)
        void UninstallDLC(AppId_t nAppID)

        bool GetDlcDownloadProgress(AppId_t nAppID, uint64 *punBytesDownloaded, uint64 *punBytesTotal)

    ctypedef struct DlcInstalled_t:
        AppId_t m_nAppID

    ISteamApps *SteamApps()

    # Friends.

    cdef enum EOverlayToStoreFlag:
        k_EOverlayToStoreFlag_None
        k_EOverlayToStoreFlag_AddToCart
        k_EOverlayToStoreFlag_AddToCartAndShow

    cdef cppclass ISteamFriends:
        const char *GetPersonaName()
        void ActivateGameOverlay(const char *pchDialog)
        void ActivateGameOverlayToWebPage(const char *pchURL)
        void ActivateGameOverlayToStore(AppId_t nAppID, EOverlayToStoreFlag eFlag)

    ISteamFriends *SteamFriends()

    # User.

    ctypedef uint32 AccountID_t
    ctypedef uint32 HAuthTicket

    cdef cppclass CSteamID:
        AccountID_t GetAccountID()

    cdef cppclass ISteamUser:
        CSteamID GetSteamID()
        HAuthTicket GetAuthSessionTicket(void *pTicket, int cbMaxTicket, uint32 *pcbTicket)
        void CancelAuthTicket(HAuthTicket hAuthTicket)

    ISteamUser *SteamUser()


cdef extern from "steamcallbacks.h":
    cdef cppclass SteamCallback[T]:
        SteamCallback(void (*)(T *))

import renpy

######################################################### Stats and Achievements

# # A callable that is called when the stats are available.
# got_stats = None
#
# cdef void call_got_stats(UserStatsReceived_t *s):
#     if got_stats is not None:
#         got_stats()
#
# cdef SteamCallback[UserStatsReceived_t] *stats_received_callback = \
#     new SteamCallback[UserStatsReceived_t](call_got_stats)

def retrieve_stats():
    """
    :doc: steam_stats

    Retrieves achievements and statistics from Steam.
    """
    """
     `callback` will be
    called with no parameters if and when the statistics become available.
    """

#     global got_stats
#     got_stats = callback

    SteamUserStats().RequestCurrentStats()

def store_stats():
    """
    :doc: steam_stats

    Stores statistics and achievements on the Steam server.
    """

    SteamUserStats().StoreStats()

def list_achievements():
    """
    :doc: steam_stats

    Returns a list of achievement names.
    """

    rv = [ ]

    cdef int na = SteamUserStats().GetNumAchievements()
    cdef char *s
    cdef int i

    for 0 <= i < na:
        s = <char *> SteamUserStats().GetAchievementName(i)
        rv.append(s)

    return rv

def get_achievement(name):
    """
    :doc: steam_stats

    Gets the state of the achievements with `name`. This returns True if the
    achievement has been granted, False if it hasn't, and None if the achievement
    is unknown or an error occurs.
    """

    cdef bool rv

    if not SteamUserStats().GetAchievement(name, &rv):
        return None

    return rv

def grant_achievement(name):
    """
    :doc: steam_stats

    Grants the achievement with `name`. Call :func:`_renpysteam.store_stats` to
    push this change to the server.
    """

    return SteamUserStats().SetAchievement(name)

def clear_achievement(name):
    """
    :doc: steam_stats

    Clears the achievement with `name`. Call :func:`_renpysteam.store_stats` to
    push this change to the server.
    """

    return SteamUserStats().ClearAchievement(name)

def indicate_achievement_progress(name, cur_progress, max_progress):
    """
    :doc: steam_stats

    Indicates achievement progress to the user. This does *not* unlock the
    achievement.
    """

    return SteamUserStats().IndicateAchievementProgress(name, cur_progress, max_progress)

def get_float_stat(name):
    """
    :doc: steam_stats

    Returns the value of the stat with `name`, or None if no such stat
    exits.
    """

    cdef float rv

    if not SteamUserStats().GetStat(<char *> name, &rv):
        return None

    return rv

def set_float_stat(name, value):
    """
    :doc: steam_stats

    Sets the value of the stat with `name`, which must have the type of
    FLOAT. Call :func:`_renpysteam.store_stats` to push this change to the
    server.
    """

    cdef float v = value

    return SteamUserStats().SetStat(<char *> name, v)

def get_int_stat(name):
    """
    :doc: steam_stats

    Returns the value of the stat with `name`, or None if no such stat
    exits.
    """

    cdef int32 rv

    if not SteamUserStats().GetStat(<char *> name, &rv):
        return None

    return rv

def set_int_stat(name, value):
    """
    :doc: steam_stats

    Sets the value of the stat with `name`, which must have the type of
    INT. Call :func:`_renpysteam.store_stats` to push this change to the
    server.
    """

    cdef int32 v = value

    return SteamUserStats().SetStat(<char *> name, v)


########################################################################### Apps

def is_subscribed_app(appid):
    """
    :doc: steam_apps

    Returns true if the user owns the app with `appid`, and false otherwise.
    """

    return SteamApps().BIsSubscribedApp(appid)

def get_current_game_language():
    """
    :doc: steam_apps

    Return the name of the language the user has selected.
    """

    cdef const char *s = SteamApps().GetCurrentGameLanguage()
    return str(s)

def get_steam_ui_language():
    """
    :doc: steam_apps

    Return the name of the language the steam UI is using.
    """

    cdef const char *s = SteamUtils().GetSteamUILanguage()
    return str(s)

def get_current_beta_name():
    """
    :doc: steam_apps

    Returns the name of the current beta, or None if it can't.
    """

    cdef char rv[256]

    if not SteamApps().GetCurrentBetaName(rv, 256):
        return None

    return str(rv)

def dlc_installed(appid):
    """
    :doc: steam_apps

    Returns True if `dlc` is installed, or False otherwise.
    """

    return SteamApps().BIsDlcInstalled(appid)

def install_dlc(appid): # , callback):
    """
    :doc: steam_apps

    Requests the DLC with `appid` be installed.
    """

    """
    If not None, `callback`
    will be called with `appid` when the install finishes. Only one
    callback can be registered at a time.
    """

    SteamApps().InstallDLC(appid)

def uninstall_dlc(appid):
    """
    :doc: steam_apps

    Requests that the DLC with `appid` be uninstalled.
    """

    SteamApps().UninstallDLC(appid)

def dlc_progress(appid):
    """
    :doc: steam_apps

    Reports the progress towards DLC download completion.

    """

    cdef uint64 done = 0
    cdef uint64 total = 0

    if SteamApps().GetDlcDownloadProgress(appid, &done, &total):
        return done, total
    else:
        return None


######################################################################## Overlay

def is_overlay_enabled():
    """
    :doc: steam_overlay

    Returns true if the steam overlay is enabled. (This might take a while to
    return true once the game starts.)
    """

    return SteamUtils().IsOverlayEnabled()

def overlay_needs_present():
    """
    :doc: steam_overlay

    Returns true if the steam overlay is enabled. (This might take a while to
    return true once the game starts.)
    """

    return SteamUtils().BOverlayNeedsPresent()


POSITION_TOP_LEFT = k_EPositionTopLeft
POSITION_TOP_RIGHT = k_EPositionTopRight
POSITION_BOTTOM_LEFT = k_EPositionBottomLeft
POSITION_BOTTOM_RIGHT = k_EPositionBottomRight

def set_overlay_notification_position(position):
    """
    :doc: steam_overlay

    Sets the position of the steam overlay. `Position` should be one of
    _renpysteam.POSTION_TOP_LEFT, .POSITION_TOP_RIGHT, .POSITION_BOTTOM_LEFT,
    or .POSITION_BOTTOM_RIGHT.
    """

    SteamUtils().SetOverlayNotificationPosition(position)

def activate_overlay(dialog):
    """
    :doc: steam_overlay

    Activates the Steam overlay.

    `dialog`
        The dialog to open the overlay to. One of "Friends", "Community",
        "Players", "Settings", "OfficialGameGroup", "Stats", "Achievements"
    """

    SteamFriends().ActivateGameOverlay(dialog)

def activate_overlay_to_web_page(url):
    """
    :doc: steam_overlay

    Activates the Steam overlay, and opens the web page at `url`.
    """

    SteamFriends().ActivateGameOverlayToWebPage(url)

STORE_NONE = k_EOverlayToStoreFlag_None
STORE_ADD_TO_CART = k_EOverlayToStoreFlag_AddToCart
STORE_ADD_TO_CART_AND_SHOW = k_EOverlayToStoreFlag_AddToCartAndShow

def activate_overlay_to_store(appid, flag=STORE_NONE):
    """
    :doc: steam_overlay

    Opens the steam overlay to the store.

    `appid`
        The appid to open.

    `flag`
        One of _renpysteam.STORE_NONE, .STORE_ADD_TO_CART, or .STORE_ADD_TO_CART_AND_SHOW.
    """

    SteamFriends().ActivateGameOverlayToStore(appid, flag)

########################################################################### User

def get_persona_name():
    """
    :doc: steam_user

    Returns the user's publicly-visible name.
    """

    return SteamFriends().GetPersonaName().decode("utf-8")


def get_account_id():
    """
    :doc: steam_user

    Returns the user's account ID.
    """

    return SteamUser().GetSteamID().GetAccountID()

cdef HAuthTicket h_ticket = 0
ticket = None

def get_session_ticket():
    """
    :doc: steam_user

    Gets a ticket that can be sent to the server to authenticate this user.
    """

    global ticket
    global h_ticket

    cdef char ticket_buf[2048]
    cdef uint32 ticket_len

    if ticket is not None:
        return ticket

    h_ticket = SteamUser().GetAuthSessionTicket(ticket_buf, 2048, &ticket_len)

    if h_ticket:
        ticket = ticket_buf[0:ticket_len]

    return ticket

def cancel_ticket():
    """
    :doc: steam_user

    Cancels the ticket returned by :func:`_renpysteam.get_session_ticket`.
    """


    global h_ticket
    global tocket

    SteamUser().CancelAuthTicket(h_ticket)

    h_ticket = 0
    ticket = None


################################################################# Initialization

# Have we been initialized?
initialized = None

# Called periodically to run callbacks.
def periodic():
    SteamAPI_RunCallbacks()

# Initialize the Steam API.
def init():
    """
    :doc: steam

    Initializes the Steam API. Returns true for success, false for failure.
    If a failure has occurred, no other steam functions should be called.

    This may be called multiple times, but only attempts initialization the
    first time it's been called.
    """

    global initialized

    if initialized is None:
        initialized = SteamAPI_Init()

        if initialized:
            renpy.config.periodic_callbacks.append(periodic)
            renpy.config.needs_redraw_callbacks.append(overlay_needs_present)
            set_overlay_notification_position(POSITION_TOP_RIGHT)

    return initialized