Mercurial > games > semicongine
comparison semiconginev2/old/steam.nim @ 1218:56781cc0fc7c compiletime-tests
did: renamge main package
author | sam <sam@basx.dev> |
---|---|
date | Wed, 17 Jul 2024 21:01:37 +0700 |
parents | semicongine/old/steam.nim@a3eb305bcac2 |
children |
comparison
equal
deleted
inserted
replaced
1217:f819a874058f | 1218:56781cc0fc7c |
---|---|
1 import std/dynlib | |
2 import std/strutils | |
3 import std/logging | |
4 | |
5 var | |
6 steam_api: LibHandle | |
7 steam_is_loaded = false | |
8 | |
9 when defined(linux): | |
10 proc dlerror(): cstring {.stdcall, importc.} | |
11 steam_api = "libsteam_api.so".loadLib() | |
12 if steam_api == nil: | |
13 echo dlerror() | |
14 elif defined(windows): | |
15 steam_api = "steam_api".loadLib() | |
16 # TODO: maybe should get some error reporting on windows too? | |
17 | |
18 | |
19 # required to store reference, when calling certain APIs | |
20 type | |
21 SteamUserStatsRef = ptr object | |
22 var userStats: SteamUserStatsRef | |
23 | |
24 # load function pointers for steam API | |
25 var | |
26 Shutdown*: proc() {.stdcall.} | |
27 Init: proc(msg: ptr array[1024, char]): cint {.stdcall.} | |
28 SteamUserStats: proc(): SteamUserStatsRef {.stdcall.} | |
29 RequestCurrentStats: proc(self: SteamUserStatsRef): bool {.stdcall.} # needs to be called before the achievment-stuff | |
30 ClearAchievement: proc(self: SteamUserStatsRef, pchName: cstring): bool {.stdcall.} | |
31 SetAchievement: proc(self: SteamUserStatsRef, pchName: cstring): bool {.stdcall.} | |
32 StoreStats: proc(self: SteamUserStatsRef): bool {.stdcall.} # needs to be called in order for achievments to be saved | |
33 # dynlib-helper function | |
34 proc loadFunc[T](nimFunc: var T, dllFuncName: string) = | |
35 nimFunc = cast[T](steam_api.checkedSymAddr(dllFuncName)) | |
36 if steam_api != nil: | |
37 loadFunc(Init, "SteamAPI_InitFlat") | |
38 loadFunc(Shutdown, "SteamAPI_Shutdown") | |
39 loadFunc(SteamUserStats, "SteamAPI_SteamUserStats_v012") | |
40 loadFunc(RequestCurrentStats, "SteamAPI_ISteamUserStats_RequestCurrentStats") | |
41 loadFunc(ClearAchievement, "SteamAPI_ISteamUserStats_ClearAchievement") | |
42 loadFunc(SetAchievement, "SteamAPI_ISteamUserStats_SetAchievement") | |
43 loadFunc(StoreStats, "SteamAPI_ISteamUserStats_StoreStats") | |
44 | |
45 | |
46 # nice wrappers for steam API | |
47 | |
48 proc SteamRequestCurrentStats*(): bool = | |
49 RequestCurrentStats(userStats) | |
50 | |
51 proc SteamClearAchievement*(name: string): bool = | |
52 userStats.ClearAchievement(name.cstring) | |
53 | |
54 proc SteamSetAchievement*(name: string): bool = | |
55 userStats.SetAchievement(name.cstring) | |
56 | |
57 proc SteamStoreStats*(): bool = | |
58 userStats.StoreStats() | |
59 | |
60 proc SteamShutdown*() = | |
61 Shutdown() | |
62 | |
63 | |
64 # helper funcs | |
65 proc SteamAvailable*(): bool = | |
66 steam_api != nil and steam_is_loaded | |
67 | |
68 # first function that should be called | |
69 proc TrySteamInit*() = | |
70 if steam_api != nil and not steam_is_loaded: | |
71 var msg: array[1024, char] | |
72 let success = Init(addr msg) == 0 | |
73 warn join(@msg, "") | |
74 if success: | |
75 userStats = SteamUserStats() | |
76 steam_is_loaded = SteamRequestCurrentStats() |