annotate semiconginev2/contrib/steam.nim @ 1250:9ceb509af5ea

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