Fix userid issue and add user id to example code

This commit is contained in:
Joey Yakimowich-Payne 2018-06-24 20:14:54 +09:00
commit 5c252fdd6e
5 changed files with 43 additions and 24 deletions

View file

@ -3,6 +3,7 @@ import libnx/wrapper/hid
import libnx/wrapper/console import libnx/wrapper/console
import libnx/account import libnx/account
import libnx/utils import libnx/utils
import libnx/wrapper/types
proc main() = proc main() =
gfxInitDefault() gfxInitDefault()
@ -13,11 +14,16 @@ proc main() =
withAccountService: withAccountService:
try: try:
let user = getActiveUser() let user = getActiveUser()
echo "\x1b[6;2HUsername: " & user.username let userID = user.id
echo "\x1b[7;2HMiiID: " & $user.miiID echo "\x1b[6;2HUserID: 0x" & userID.toHex()
echo "\x1b[8;2HIconID: " & $user.iconID echo "\x1b[7;2HUsername: " & user.username
except AccountError: echo "\x1b[8;2HMiiID: " & $user.miiID
echo "\x1b[9;2HIconID: " & $user.iconID
except AccountUserNotSelectedError:
echo "\x1b[6;2HNo user currently selected!" echo "\x1b[6;2HNo user currently selected!"
except AccountError:
let msg = getCurrentExceptionMsg()
echo "\x1b[6;2H" & msg
mainLoop: mainLoop:
let keysDown = hidKeysDown(CONTROLLER_P1_AUTO) let keysDown = hidKeysDown(CONTROLLER_P1_AUTO)

View file

@ -16,6 +16,7 @@ task setup, "Download and generate bindings":
task buildExamples, "Build switch examples": 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/helloworld/helloworld.nim"
exec "switch_build --author='jyapayne' --version='1.0.0' examples/accounts/account_ex.nim"
#before install: #before install:
# setupTask() # setupTask()

View file

@ -6,6 +6,13 @@ import libnx/service
type type
AccountError* = object of Exception 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 User* = ref object
id*: u128 id*: u128
@ -25,41 +32,41 @@ type
var enabled = false var enabled = false
template raiseEx(message: string) = template raiseEx(ty: untyped, message: string) =
raise newException(AccountError, message) raise newException(ty, message)
proc getService*(): Service = proc getService*(): Service =
let serv = accountGetService()[] let serv = accountGetService()[]
result = newService(serv) result = newService(serv)
proc init() = proc init*() =
let service = getService() let service = getService()
if service.isActive: if service.isActive:
return return
let code = accountInitialize().newResult let code = accountInitialize().newResult
if code.failed: 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() let service = getService()
if not service.isActive: if not service.isActive:
return return
accountExit() accountExit()
enabled = false
proc close*(profile: AccountProfile) = proc close*(profile: AccountProfile) =
accountProfileClose(profile.unsafeAddr) accountProfileClose(profile.unsafeAddr)
template withAccountService*(code: untyped): typed = template withAccountService*(code: typed): typed =
enabled = true
init() init()
code code
exit() exit()
enabled = false
proc ensureEnabled() = proc ensureEnabled() =
if not enabled: if not enabled:
raiseEx("Use withAccountService to access account functions.") raiseEx(AccountError, "Use withAccountService to access account functions.")
proc getImageSize*(profile: AccountProfile): int = proc getImageSize*(profile: AccountProfile): int =
ensureEnabled() ensureEnabled()
@ -67,7 +74,7 @@ proc getImageSize*(profile: AccountProfile): int =
result = 0 result = 0
let code = accountProfileGetImageSize(profile.unsafeAddr, res.addr).newResult let code = accountProfileGetImageSize(profile.unsafeAddr, res.addr).newResult
if code.failed: 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 result = res.int
proc imageSize*(user: User): int = proc imageSize*(user: User): int =
@ -76,7 +83,7 @@ proc imageSize*(user: User): int =
let res = accountProfileGetImageSize(prof.addr, size.addr).newResult let res = accountProfileGetImageSize(prof.addr, size.addr).newResult
if res.failed: 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 result = size.int
@ -84,7 +91,7 @@ proc getProfileHelper(userID: u128): AccountProfile =
let res = accountGetProfile(result.addr, userID).newResult let res = accountGetProfile(result.addr, userID).newResult
if res.failed: 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 = proc loadImage*(user: User): AccountImage =
@ -104,24 +111,27 @@ proc loadImage*(user: User): AccountImage =
if res.failed: if res.failed:
prof.close() prof.close()
raiseEx("Error, could not load image: " & res.description) raiseEx(AccountImageLoadError, "Error, could not load image: " & res.description)
prof.close() prof.close()
proc userID*(user: User): string =
$user.id
proc getActiveUser*(): User = proc getActiveUser*(): User =
ensureEnabled() ensureEnabled()
result = new(User) result = new(User)
var var
userID: u128 userID: u128 = newUInt128(0,0)
selected: bool selected: bool
var res = accountGetActiveUser(userID.addr, selected.addr).newResult var res = accountGetActiveUser(userID.addr, selected.addr).newResult
if res.failed: 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: if not selected:
raiseEx("No user currently selected!") raiseEx(AccountUserNotSelectedError, "No user currently selected!")
result.id = userID result.id = userID
result.selected = selected result.selected = selected
@ -134,7 +144,7 @@ proc getActiveUser*(): User =
res = accountProfileGet(prof.addr, userData.addr, profBase.addr).newResult res = accountProfileGet(prof.addr, userData.addr, profBase.addr).newResult
if res.failed: 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.username = profBase.username.join("")
result.lastEdited = profBase.lastEditTimestamp result.lastEdited = profBase.lastEditTimestamp
@ -152,7 +162,10 @@ proc getProfile*(user: User): Profile =
let res = accountGetProfile(prof.addr, user.id).newResult let res = accountGetProfile(prof.addr, user.id).newResult
if res.failed: 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) result.service = newService(prof.s)
prof.close() prof.close()

View file

@ -377,4 +377,3 @@ proc `$`*(val: u128): string =
str.add(char(48 + digit)) str.add(char(48 + digit))
result = str result = str