comparison semiconginev2/contrib/steam.nim @ 1226:c8e3037aca66 compiletime-tests

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