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)
## *
## @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 = 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) 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 = 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) 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 = 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) 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 = 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) 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 = 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) 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 = 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 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 = 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 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 = 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) result = new(Service)
serviceCreate(result.serv.addr, handle) 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 = 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) result = new(Service)
serviceCreateDomainSubservice(result.serv.addr, parent.serv.addr, objectId) 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 = 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 serviceConvertToDomain(service.serv.addr).newResult
## *
## @brief Closes a service.
## @param[in] s Service object.
##
proc close*(service: Service) = proc close*(service: Service) =
## *
## @brief Closes a service.
## @param[in] s Service object.
##
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 =