From 5c252fdd6e4ccfa27b900c061ae387f671c7e70e Mon Sep 17 00:00:00 2001 From: Joey Yakimowich-Payne Date: Sun, 24 Jun 2018 20:14:54 +0900 Subject: [PATCH] Fix userid issue and add user id to example code --- examples/accounts/account_ex.nim | 14 ++++++--- libnx.nimble | 1 + src/libnx/account.nim | 49 ++++++++++++++++++++------------ src/libnx/ext/integer128.nim | 1 - src/libnx/wrapper/sm.nim | 2 +- 5 files changed, 43 insertions(+), 24 deletions(-) diff --git a/examples/accounts/account_ex.nim b/examples/accounts/account_ex.nim index cb1f105..96d057e 100644 --- a/examples/accounts/account_ex.nim +++ b/examples/accounts/account_ex.nim @@ -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) diff --git a/libnx.nimble b/libnx.nimble index 1b41cf9..6e16a60 100644 --- a/libnx.nimble +++ b/libnx.nimble @@ -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() diff --git a/src/libnx/account.nim b/src/libnx/account.nim index 282483d..2cc8883 100644 --- a/src/libnx/account.nim +++ b/src/libnx/account.nim @@ -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() diff --git a/src/libnx/ext/integer128.nim b/src/libnx/ext/integer128.nim index 676e653..03ee6a1 100644 --- a/src/libnx/ext/integer128.nim +++ b/src/libnx/ext/integer128.nim @@ -377,4 +377,3 @@ proc `$`*(val: u128): string = str.add(char(48 + digit)) result = str - diff --git a/src/libnx/wrapper/sm.nim b/src/libnx/wrapper/sm.nim index 39936b3..8b8d2d1 100644 --- a/src/libnx/wrapper/sm.nim +++ b/src/libnx/wrapper/sm.nim @@ -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.} \ No newline at end of file