annotate 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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
973
7fb8f62a9ea5 add: api to interact with steam
sam <sam@basx.dev>
parents:
diff changeset
1 import std/dynlib
7fb8f62a9ea5 add: api to interact with steam
sam <sam@basx.dev>
parents:
diff changeset
2 import std/strutils
7fb8f62a9ea5 add: api to interact with steam
sam <sam@basx.dev>
parents:
diff changeset
3 import std/logging
7fb8f62a9ea5 add: api to interact with steam
sam <sam@basx.dev>
parents:
diff changeset
4
7fb8f62a9ea5 add: api to interact with steam
sam <sam@basx.dev>
parents:
diff changeset
5 var
7fb8f62a9ea5 add: api to interact with steam
sam <sam@basx.dev>
parents:
diff changeset
6 steam_api: LibHandle
7fb8f62a9ea5 add: api to interact with steam
sam <sam@basx.dev>
parents:
diff changeset
7 steam_is_loaded = false
7fb8f62a9ea5 add: api to interact with steam
sam <sam@basx.dev>
parents:
diff changeset
8
7fb8f62a9ea5 add: api to interact with steam
sam <sam@basx.dev>
parents:
diff changeset
9 when defined(linux):
7fb8f62a9ea5 add: api to interact with steam
sam <sam@basx.dev>
parents:
diff changeset
10 proc dlerror(): cstring {.stdcall, importc.}
7fb8f62a9ea5 add: api to interact with steam
sam <sam@basx.dev>
parents:
diff changeset
11 steam_api = "libsteam_api.so".loadLib()
7fb8f62a9ea5 add: api to interact with steam
sam <sam@basx.dev>
parents:
diff changeset
12 if steam_api == nil:
7fb8f62a9ea5 add: api to interact with steam
sam <sam@basx.dev>
parents:
diff changeset
13 echo dlerror()
7fb8f62a9ea5 add: api to interact with steam
sam <sam@basx.dev>
parents:
diff changeset
14 elif defined(windows):
7fb8f62a9ea5 add: api to interact with steam
sam <sam@basx.dev>
parents:
diff changeset
15 steam_api = "steam_api".loadLib()
974
2a5e7b54736e did: fix spelling
sam <sam@basx.dev>
parents: 973
diff changeset
16 # TODO: maybe should get some error reporting on windows too?
973
7fb8f62a9ea5 add: api to interact with steam
sam <sam@basx.dev>
parents:
diff changeset
17
7fb8f62a9ea5 add: api to interact with steam
sam <sam@basx.dev>
parents:
diff changeset
18
7fb8f62a9ea5 add: api to interact with steam
sam <sam@basx.dev>
parents:
diff changeset
19 # required to store reference, when calling certain APIs
7fb8f62a9ea5 add: api to interact with steam
sam <sam@basx.dev>
parents:
diff changeset
20 type
7fb8f62a9ea5 add: api to interact with steam
sam <sam@basx.dev>
parents:
diff changeset
21 SteamUserStatsRef = ptr object
7fb8f62a9ea5 add: api to interact with steam
sam <sam@basx.dev>
parents:
diff changeset
22 var userStats: SteamUserStatsRef
7fb8f62a9ea5 add: api to interact with steam
sam <sam@basx.dev>
parents:
diff changeset
23
974
2a5e7b54736e did: fix spelling
sam <sam@basx.dev>
parents: 973
diff changeset
24 # load function pointers for steam API
973
7fb8f62a9ea5 add: api to interact with steam
sam <sam@basx.dev>
parents:
diff changeset
25 var
7fb8f62a9ea5 add: api to interact with steam
sam <sam@basx.dev>
parents:
diff changeset
26 Shutdown*: proc() {.stdcall.}
7fb8f62a9ea5 add: api to interact with steam
sam <sam@basx.dev>
parents:
diff changeset
27 Init: proc(msg: ptr array[1024, char]): cint {.stdcall.}
7fb8f62a9ea5 add: api to interact with steam
sam <sam@basx.dev>
parents:
diff changeset
28 SteamUserStats: proc(): SteamUserStatsRef {.stdcall.}
7fb8f62a9ea5 add: api to interact with steam
sam <sam@basx.dev>
parents:
diff changeset
29 RequestCurrentStats: proc(self: SteamUserStatsRef): bool {.stdcall.} # needs to be called before the achievment-stuff
7fb8f62a9ea5 add: api to interact with steam
sam <sam@basx.dev>
parents:
diff changeset
30 ClearAchievement: proc(self: SteamUserStatsRef, pchName: cstring): bool {.stdcall.}
7fb8f62a9ea5 add: api to interact with steam
sam <sam@basx.dev>
parents:
diff changeset
31 SetAchievement: proc(self: SteamUserStatsRef, pchName: cstring): bool {.stdcall.}
7fb8f62a9ea5 add: api to interact with steam
sam <sam@basx.dev>
parents:
diff changeset
32 StoreStats: proc(self: SteamUserStatsRef): bool {.stdcall.} # needs to be called in order for achievments to be saved
7fb8f62a9ea5 add: api to interact with steam
sam <sam@basx.dev>
parents:
diff changeset
33 # dynlib-helper function
7fb8f62a9ea5 add: api to interact with steam
sam <sam@basx.dev>
parents:
diff changeset
34 proc loadFunc[T](nimFunc: var T, dllFuncName: string) =
7fb8f62a9ea5 add: api to interact with steam
sam <sam@basx.dev>
parents:
diff changeset
35 nimFunc = cast[T](steam_api.checkedSymAddr(dllFuncName))
7fb8f62a9ea5 add: api to interact with steam
sam <sam@basx.dev>
parents:
diff changeset
36 if steam_api != nil:
7fb8f62a9ea5 add: api to interact with steam
sam <sam@basx.dev>
parents:
diff changeset
37 loadFunc(Init, "SteamAPI_InitFlat")
7fb8f62a9ea5 add: api to interact with steam
sam <sam@basx.dev>
parents:
diff changeset
38 loadFunc(Shutdown, "SteamAPI_Shutdown")
7fb8f62a9ea5 add: api to interact with steam
sam <sam@basx.dev>
parents:
diff changeset
39 loadFunc(SteamUserStats, "SteamAPI_SteamUserStats_v012")
7fb8f62a9ea5 add: api to interact with steam
sam <sam@basx.dev>
parents:
diff changeset
40 loadFunc(RequestCurrentStats, "SteamAPI_ISteamUserStats_RequestCurrentStats")
7fb8f62a9ea5 add: api to interact with steam
sam <sam@basx.dev>
parents:
diff changeset
41 loadFunc(ClearAchievement, "SteamAPI_ISteamUserStats_ClearAchievement")
7fb8f62a9ea5 add: api to interact with steam
sam <sam@basx.dev>
parents:
diff changeset
42 loadFunc(SetAchievement, "SteamAPI_ISteamUserStats_SetAchievement")
7fb8f62a9ea5 add: api to interact with steam
sam <sam@basx.dev>
parents:
diff changeset
43 loadFunc(StoreStats, "SteamAPI_ISteamUserStats_StoreStats")
7fb8f62a9ea5 add: api to interact with steam
sam <sam@basx.dev>
parents:
diff changeset
44
7fb8f62a9ea5 add: api to interact with steam
sam <sam@basx.dev>
parents:
diff changeset
45
7fb8f62a9ea5 add: api to interact with steam
sam <sam@basx.dev>
parents:
diff changeset
46 # nice wrappers for steam API
7fb8f62a9ea5 add: api to interact with steam
sam <sam@basx.dev>
parents:
diff changeset
47
7fb8f62a9ea5 add: api to interact with steam
sam <sam@basx.dev>
parents:
diff changeset
48 proc SteamRequestCurrentStats*(): bool =
7fb8f62a9ea5 add: api to interact with steam
sam <sam@basx.dev>
parents:
diff changeset
49 RequestCurrentStats(userStats)
7fb8f62a9ea5 add: api to interact with steam
sam <sam@basx.dev>
parents:
diff changeset
50
7fb8f62a9ea5 add: api to interact with steam
sam <sam@basx.dev>
parents:
diff changeset
51 proc SteamClearAchievement*(name: string): bool =
7fb8f62a9ea5 add: api to interact with steam
sam <sam@basx.dev>
parents:
diff changeset
52 userStats.ClearAchievement(name.cstring)
7fb8f62a9ea5 add: api to interact with steam
sam <sam@basx.dev>
parents:
diff changeset
53
7fb8f62a9ea5 add: api to interact with steam
sam <sam@basx.dev>
parents:
diff changeset
54 proc SteamSetAchievement*(name: string): bool =
7fb8f62a9ea5 add: api to interact with steam
sam <sam@basx.dev>
parents:
diff changeset
55 userStats.SetAchievement(name.cstring)
7fb8f62a9ea5 add: api to interact with steam
sam <sam@basx.dev>
parents:
diff changeset
56
991
b260ebc9d638 del: unused parameter
sam <sam@basx.dev>
parents: 974
diff changeset
57 proc SteamStoreStats*(): bool =
973
7fb8f62a9ea5 add: api to interact with steam
sam <sam@basx.dev>
parents:
diff changeset
58 userStats.StoreStats()
7fb8f62a9ea5 add: api to interact with steam
sam <sam@basx.dev>
parents:
diff changeset
59
7fb8f62a9ea5 add: api to interact with steam
sam <sam@basx.dev>
parents:
diff changeset
60 proc SteamShutdown*() =
7fb8f62a9ea5 add: api to interact with steam
sam <sam@basx.dev>
parents:
diff changeset
61 Shutdown()
7fb8f62a9ea5 add: api to interact with steam
sam <sam@basx.dev>
parents:
diff changeset
62
7fb8f62a9ea5 add: api to interact with steam
sam <sam@basx.dev>
parents:
diff changeset
63
7fb8f62a9ea5 add: api to interact with steam
sam <sam@basx.dev>
parents:
diff changeset
64 # helper funcs
7fb8f62a9ea5 add: api to interact with steam
sam <sam@basx.dev>
parents:
diff changeset
65 proc SteamAvailable*(): bool =
7fb8f62a9ea5 add: api to interact with steam
sam <sam@basx.dev>
parents:
diff changeset
66 steam_api != nil and steam_is_loaded
7fb8f62a9ea5 add: api to interact with steam
sam <sam@basx.dev>
parents:
diff changeset
67
7fb8f62a9ea5 add: api to interact with steam
sam <sam@basx.dev>
parents:
diff changeset
68 # first function that should be called
7fb8f62a9ea5 add: api to interact with steam
sam <sam@basx.dev>
parents:
diff changeset
69 proc TrySteamInit*() =
7fb8f62a9ea5 add: api to interact with steam
sam <sam@basx.dev>
parents:
diff changeset
70 if steam_api != nil and not steam_is_loaded:
7fb8f62a9ea5 add: api to interact with steam
sam <sam@basx.dev>
parents:
diff changeset
71 var msg: array[1024, char]
7fb8f62a9ea5 add: api to interact with steam
sam <sam@basx.dev>
parents:
diff changeset
72 let success = Init(addr msg) == 0
7fb8f62a9ea5 add: api to interact with steam
sam <sam@basx.dev>
parents:
diff changeset
73 warn join(@msg, "")
7fb8f62a9ea5 add: api to interact with steam
sam <sam@basx.dev>
parents:
diff changeset
74 if success:
7fb8f62a9ea5 add: api to interact with steam
sam <sam@basx.dev>
parents:
diff changeset
75 userStats = SteamUserStats()
7fb8f62a9ea5 add: api to interact with steam
sam <sam@basx.dev>
parents:
diff changeset
76 steam_is_loaded = SteamRequestCurrentStats()