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

View file

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

View file

@ -1,4 +1,5 @@
import macros, strutils, math
import libnx/results
proc size*(enumTy: typedesc): int =
# Returns the number of items in a bit set enum
@ -88,8 +89,12 @@ type
len*: int
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 =
## Raise an exception
raise newException(ty, message)
proc `[]`[T](buff: Buffer[T], index: int): T =