Fix userid issue and add user id to example code
This commit is contained in:
parent
83a79afb3d
commit
5c252fdd6e
5 changed files with 43 additions and 24 deletions
|
|
@ -3,6 +3,7 @@ import libnx/wrapper/hid
|
|||
import libnx/wrapper/console
|
||||
import libnx/account
|
||||
import libnx/utils
|
||||
import libnx/wrapper/types
|
||||
|
||||
proc main() =
|
||||
gfxInitDefault()
|
||||
|
|
@ -13,11 +14,16 @@ proc main() =
|
|||
withAccountService:
|
||||
try:
|
||||
let user = getActiveUser()
|
||||
echo "\x1b[6;2HUsername: " & user.username
|
||||
echo "\x1b[7;2HMiiID: " & $user.miiID
|
||||
echo "\x1b[8;2HIconID: " & $user.iconID
|
||||
except AccountError:
|
||||
let userID = user.id
|
||||
echo "\x1b[6;2HUserID: 0x" & userID.toHex()
|
||||
echo "\x1b[7;2HUsername: " & user.username
|
||||
echo "\x1b[8;2HMiiID: " & $user.miiID
|
||||
echo "\x1b[9;2HIconID: " & $user.iconID
|
||||
except AccountUserNotSelectedError:
|
||||
echo "\x1b[6;2HNo user currently selected!"
|
||||
except AccountError:
|
||||
let msg = getCurrentExceptionMsg()
|
||||
echo "\x1b[6;2H" & msg
|
||||
|
||||
mainLoop:
|
||||
let keysDown = hidKeysDown(CONTROLLER_P1_AUTO)
|
||||
|
|
|
|||
|
|
@ -16,6 +16,7 @@ task setup, "Download and generate bindings":
|
|||
|
||||
task buildExamples, "Build switch examples":
|
||||
exec "switch_build --author='jyapayne' --version='1.0.0' examples/helloworld/helloworld.nim"
|
||||
exec "switch_build --author='jyapayne' --version='1.0.0' examples/accounts/account_ex.nim"
|
||||
|
||||
#before install:
|
||||
# setupTask()
|
||||
|
|
|
|||
|
|
@ -6,6 +6,13 @@ import libnx/service
|
|||
|
||||
type
|
||||
AccountError* = object of Exception
|
||||
AccountInitError* = object of AccountError
|
||||
AccountImageSizeError* = object of AccountError
|
||||
AccountUserProfileError* = object of AccountError
|
||||
AccountUserNotSelectedError* = object of AccountError
|
||||
AccountUserDataError* = object of AccountError
|
||||
AccountActiveUserError* = object of AccountError
|
||||
AccountImageLoadError* = object of AccountError
|
||||
|
||||
User* = ref object
|
||||
id*: u128
|
||||
|
|
@ -25,41 +32,41 @@ type
|
|||
|
||||
var enabled = false
|
||||
|
||||
template raiseEx(message: string) =
|
||||
raise newException(AccountError, message)
|
||||
template raiseEx(ty: untyped, message: string) =
|
||||
raise newException(ty, message)
|
||||
|
||||
proc getService*(): Service =
|
||||
let serv = accountGetService()[]
|
||||
result = newService(serv)
|
||||
|
||||
proc init() =
|
||||
proc init*() =
|
||||
let service = getService()
|
||||
if service.isActive:
|
||||
return
|
||||
|
||||
let code = accountInitialize().newResult
|
||||
if code.failed:
|
||||
raiseEx("Error, account api could not be initialized: " & code.description)
|
||||
raiseEx(AccountInitError, "Error, account api could not be initialized: " & code.description)
|
||||
enabled = true
|
||||
|
||||
proc exit() =
|
||||
proc exit*() =
|
||||
let service = getService()
|
||||
if not service.isActive:
|
||||
return
|
||||
accountExit()
|
||||
enabled = false
|
||||
|
||||
proc close*(profile: AccountProfile) =
|
||||
accountProfileClose(profile.unsafeAddr)
|
||||
|
||||
template withAccountService*(code: untyped): typed =
|
||||
enabled = true
|
||||
template withAccountService*(code: typed): typed =
|
||||
init()
|
||||
code
|
||||
exit()
|
||||
enabled = false
|
||||
|
||||
proc ensureEnabled() =
|
||||
if not enabled:
|
||||
raiseEx("Use withAccountService to access account functions.")
|
||||
raiseEx(AccountError, "Use withAccountService to access account functions.")
|
||||
|
||||
proc getImageSize*(profile: AccountProfile): int =
|
||||
ensureEnabled()
|
||||
|
|
@ -67,7 +74,7 @@ proc getImageSize*(profile: AccountProfile): int =
|
|||
result = 0
|
||||
let code = accountProfileGetImageSize(profile.unsafeAddr, res.addr).newResult
|
||||
if code.failed:
|
||||
raiseEx("Error, image size could not be received: " & code.description)
|
||||
raiseEx(AccountImageSizeError, "Error, could not get image size: " & code.description)
|
||||
result = res.int
|
||||
|
||||
proc imageSize*(user: User): int =
|
||||
|
|
@ -76,7 +83,7 @@ proc imageSize*(user: User): int =
|
|||
let res = accountProfileGetImageSize(prof.addr, size.addr).newResult
|
||||
|
||||
if res.failed:
|
||||
raiseEx("Error, could not get image size: " & res.description)
|
||||
raiseEx(AccountImageSizeError, "Error, could not get image size: " & res.description)
|
||||
|
||||
result = size.int
|
||||
|
||||
|
|
@ -84,7 +91,7 @@ proc getProfileHelper(userID: u128): AccountProfile =
|
|||
let res = accountGetProfile(result.addr, userID).newResult
|
||||
|
||||
if res.failed:
|
||||
raiseEx("Error, could not get user profile: " & res.description)
|
||||
raiseEx(AccountUserProfileError, "Error, could not get user profile: " & res.description)
|
||||
|
||||
|
||||
proc loadImage*(user: User): AccountImage =
|
||||
|
|
@ -104,24 +111,27 @@ proc loadImage*(user: User): AccountImage =
|
|||
|
||||
if res.failed:
|
||||
prof.close()
|
||||
raiseEx("Error, could not load image: " & res.description)
|
||||
raiseEx(AccountImageLoadError, "Error, could not load image: " & res.description)
|
||||
|
||||
prof.close()
|
||||
|
||||
proc userID*(user: User): string =
|
||||
$user.id
|
||||
|
||||
proc getActiveUser*(): User =
|
||||
ensureEnabled()
|
||||
result = new(User)
|
||||
var
|
||||
userID: u128
|
||||
userID: u128 = newUInt128(0,0)
|
||||
selected: bool
|
||||
|
||||
var res = accountGetActiveUser(userID.addr, selected.addr).newResult
|
||||
|
||||
if res.failed:
|
||||
raiseEx("Error, could not get active user ID: " & res.description)
|
||||
raiseEx(AccountActiveUserError, "Error, could not get active user ID: " & res.description)
|
||||
|
||||
if not selected:
|
||||
raiseEx("No user currently selected!")
|
||||
raiseEx(AccountUserNotSelectedError, "No user currently selected!")
|
||||
|
||||
result.id = userID
|
||||
result.selected = selected
|
||||
|
|
@ -134,7 +144,7 @@ proc getActiveUser*(): User =
|
|||
res = accountProfileGet(prof.addr, userData.addr, profBase.addr).newResult
|
||||
|
||||
if res.failed:
|
||||
raiseEx("Error, could not get user data: " & res.description)
|
||||
raiseEx(AccountUserDataError, "Error, could not get user data: " & res.description)
|
||||
|
||||
result.username = profBase.username.join("")
|
||||
result.lastEdited = profBase.lastEditTimestamp
|
||||
|
|
@ -152,7 +162,10 @@ proc getProfile*(user: User): Profile =
|
|||
|
||||
let res = accountGetProfile(prof.addr, user.id).newResult
|
||||
if res.failed:
|
||||
raiseEx("Error, could not get account profile: " & res.description)
|
||||
raiseEx(
|
||||
AccountUserProfileError,
|
||||
"Error, could not get account profile: " & res.description
|
||||
)
|
||||
|
||||
result.service = newService(prof.s)
|
||||
prof.close()
|
||||
|
|
|
|||
|
|
@ -377,4 +377,3 @@ proc `$`*(val: u128): string =
|
|||
str.add(char(48 + digit))
|
||||
|
||||
result = str
|
||||
|
||||
|
|
|
|||
|
|
@ -203,4 +203,4 @@ proc smEncodeName*(name: cstring): uint64 {.cdecl, importc: "smEncodeName",
|
|||
##
|
||||
|
||||
proc smAddOverrideHandle*(name: uint64; handle: Handle) {.cdecl,
|
||||
importc: "smAddOverrideHandle", header: headersm.}
|
||||
importc: "smAddOverrideHandle", header: headersm.}
|
||||
Loading…
Add table
Add a link
Reference in a new issue