Cleanup code and comments

This commit is contained in:
Joey Yakimowich-Payne 2018-07-06 21:47:19 +09:00
commit e4e0dcd1b0
4 changed files with 80 additions and 58 deletions

View file

@ -36,11 +36,15 @@ type
var enabled = false var enabled = false
proc getService*(): Service = proc getService*(): Service =
let serv = accountGetService()[] let serv = accountGetService()[]
result = newService(serv) result = newService(serv)
proc init*() =
proc init() =
## Initializes the account service. Should not
## be used in client code
let service = getService() let service = getService()
if service.isActive: if service.isActive:
return return
@ -53,25 +57,31 @@ proc init*() =
) )
enabled = true enabled = true
proc exit*() =
proc exit() =
## Exits and closes the Account service
let service = getService() let service = getService()
if not service.isActive: if not service.isActive:
return return
accountExit() accountExit()
enabled = false enabled = false
proc close*(profile: AccountProfile) = proc close*(profile: AccountProfile) =
accountProfileClose(profile.unsafeAddr) accountProfileClose(profile.unsafeAddr)
template withAccountService*(code: untyped): typed = template withAccountService*(code: untyped): typed =
init() init()
code code
exit() exit()
proc ensureEnabled() = proc ensureEnabled() =
if not enabled: if not enabled:
raiseEx(AccountError, "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()
var res = 0.csize var res = 0.csize
@ -81,6 +91,7 @@ proc getImageSize*(profile: AccountProfile): int =
raiseEx(AccountImageSizeError, "Error, could not get image size: " & 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 =
ensureEnabled() ensureEnabled()
var prof: AccountProfile var prof: AccountProfile
@ -92,6 +103,7 @@ proc imageSize*(user: User): int =
result = size.int result = size.int
proc getProfileHelper(userID: u128): AccountProfile = proc getProfileHelper(userID: u128): AccountProfile =
let res = accountGetProfile(result.addr, userID).newResult let res = accountGetProfile(result.addr, userID).newResult
@ -120,9 +132,11 @@ proc loadImage*(user: User): AccountImage =
prof.close() prof.close()
proc userID*(user: User): string = proc userID*(user: User): string =
$user.id $user.id
proc getUser*(userID: u128): User = proc getUser*(userID: u128): User =
ensureEnabled() ensureEnabled()
result = new(User) result = new(User)
@ -203,6 +217,7 @@ proc getProfile*(user: User): Profile =
proc getUserCount*(): int32 = proc getUserCount*(): int32 =
## Gets the number of users on the switch
ensureEnabled() ensureEnabled()
var count: int32 var count: int32
let res = accountGetUserCount(count.addr).newResult let res = accountGetUserCount(count.addr).newResult
@ -215,6 +230,7 @@ proc getUserCount*(): int32 =
proc listAllUsers*(): seq[User] = proc listAllUsers*(): seq[User] =
## Gets a list of all users currently on the switch
ensureEnabled() ensureEnabled()
result = @[] result = @[]

View file

@ -94,6 +94,7 @@ proc succeeded*(res: Result): bool = res.code.R_SUCCEEDED
proc failed*(res: Result): bool = res.code.R_FAILED proc failed*(res: Result): bool = res.code.R_FAILED
proc newResult*(code: uint32): Result = proc newResult*(code: uint32): Result =
## Create a result from a libnx error code for friendlier syntax.
result = new(Result) result = new(Result)
result.code = code result.code = code

View file

@ -14,94 +14,94 @@ proc getSmService*(serv: Service): sm.Service =
proc newService*(serv: sm.Service): Service = proc newService*(serv: sm.Service): Service =
result = Service(serv: serv) result = Service(serv: serv)
proc isOverride*(service: Service): bool =
## * ## *
## @brief Returns whether a service is overriden in the homebrew environment. ## @brief Returns whether a service is overriden in the homebrew environment.
## @param[in] s Service object. ## @param[in] s Service object.
## @return true if overriden. ## @return true if overriden.
## ##
proc isOverride*(service: Service): bool =
serviceIsOverride(service.serv.addr) serviceIsOverride(service.serv.addr)
proc isActive*(service: Service): bool =
## * ## *
## @brief Returns whether a service has been initialized. ## @brief Returns whether a service has been initialized.
## @param[in] s Service object. ## @param[in] s Service object.
## @return true if initialized. ## @return true if initialized.
## ##
proc isActive*(service: Service): bool =
serviceIsActive(service.serv.addr) serviceIsActive(service.serv.addr)
proc isDomain*(service: Service): bool =
## * ## *
## @brief Returns whether a service is a domain. ## @brief Returns whether a service is a domain.
## @param[in] s Service object. ## @param[in] s Service object.
## @return true if a domain. ## @return true if a domain.
## ##
proc isDomain*(service: Service): bool =
serviceIsDomain(service.serv.addr) serviceIsDomain(service.serv.addr)
proc isDomainSubservice*(service: Service): bool =
## * ## *
## @brief Returns whether a service is a domain subservice. ## @brief Returns whether a service is a domain subservice.
## @param[in] s Service object. ## @param[in] s Service object.
## @return true if a domain subservice. ## @return true if a domain subservice.
## ##
proc isDomainSubservice*(service: Service): bool =
serviceIsDomainSubservice(service.serv.addr) serviceIsDomainSubservice(service.serv.addr)
proc objectId*(service: Service): uint32 =
## * ## *
## @brief For a domain/domain subservice, return the associated object ID. ## @brief For a domain/domain subservice, return the associated object ID.
## @param[in] s Service object, necessarily a domain or domain subservice. ## @param[in] s Service object, necessarily a domain or domain subservice.
## @return The object ID. ## @return The object ID.
## ##
proc objectId*(service: Service): uint32 =
serviceGetObjectId(service.serv.addr) serviceGetObjectId(service.serv.addr)
proc close*(service: Service; objectId: uint32): Result =
## * ## *
## @brief Closes a domain object by ID. ## @brief Closes a domain object by ID.
## @param[in] s Service object, necessarily a domain or domain subservice. ## @param[in] s Service object, necessarily a domain or domain subservice.
## @param object_id ID of the object to close. ## @param object_id ID of the object to close.
## @return Result code. ## @return Result code.
## ##
proc close*(service: Service; objectId: uint32): Result =
serviceCloseObjectById(service.serv.addr, objectId).newResult serviceCloseObjectById(service.serv.addr, objectId).newResult
proc ipcDispatch*(service: Service): Result =
## * ## *
## @brief Dispatches an IPC request to a service. ## @brief Dispatches an IPC request to a service.
## @param[in] s Service object. ## @param[in] s Service object.
## @return Result code. ## @return Result code.
## ##
proc ipcDispatch*(service: Service): Result =
serviceIpcDispatch(service.serv.addr).newResult serviceIpcDispatch(service.serv.addr).newResult
proc createService*(handle: Handle): Service =
## * ## *
## @brief Creates a service object from an IPC session handle. ## @brief Creates a service object from an IPC session handle.
## @param[out] s Service object. ## @param[out] s Service object.
## @param[in] h IPC session handle. ## @param[in] h IPC session handle.
## ##
proc createService*(handle: Handle): Service =
result = new(Service) result = new(Service)
serviceCreate(result.serv.addr, handle) serviceCreate(result.serv.addr, handle)
proc createDomainSubservice*(parent: Service; objectId: uint32): Service =
## * ## *
## @brief Creates a domain subservice object from a parent service. ## @brief Creates a domain subservice object from a parent service.
## @param[out] s Service object. ## @param[out] s Service object.
## @param[in] parent Parent service, necessarily a domain or domain subservice. ## @param[in] parent Parent service, necessarily a domain or domain subservice.
## @param[in] object_id Object ID for this subservice. ## @param[in] object_id Object ID for this subservice.
## ##
proc createDomainSubservice*(parent: Service; objectId: uint32): Service =
result = new(Service) result = new(Service)
serviceCreateDomainSubservice(result.serv.addr, parent.serv.addr, objectId) serviceCreateDomainSubservice(result.serv.addr, parent.serv.addr, objectId)
proc convertToDomain*(service: Service): Result =
## * ## *
## @brief Converts a regular service to a domain. ## @brief Converts a regular service to a domain.
## @param[in] s Service object. ## @param[in] s Service object.
## @return Result code. ## @return Result code.
## ##
proc convertToDomain*(service: Service): Result =
serviceConvertToDomain(service.serv.addr).newResult serviceConvertToDomain(service.serv.addr).newResult
proc close*(service: Service) =
## * ## *
## @brief Closes a service. ## @brief Closes a service.
## @param[in] s Service object. ## @param[in] s Service object.
## ##
proc close*(service: Service) =
serviceClose(service.serv.addr) serviceClose(service.serv.addr)

View file

@ -1,4 +1,5 @@
import macros, strutils, math import macros, strutils, math
import libnx/results
proc size*(enumTy: typedesc): int = proc size*(enumTy: typedesc): int =
# Returns the number of items in a bit set enum # Returns the number of items in a bit set enum
@ -88,8 +89,12 @@ type
len*: int len*: int
data*: ptr UncheckedArray[T] data*: ptr UncheckedArray[T]
template raiseEx*(ty: untyped, message: string, rc: Result): untyped =
## Raise an exception with a result description
raise newException(ty, message & ": " & rc.description)
template raiseEx*(ty: untyped, message: string): untyped = template raiseEx*(ty: untyped, message: string): untyped =
## Raise an exception
raise newException(ty, message) raise newException(ty, message)
proc `[]`[T](buff: Buffer[T], index: int): T = proc `[]`[T](buff: Buffer[T], index: int): T =