Loadable: Add registerCustomProtocol() for e.g. reading assets in mobile.
This commit is contained in:
parent
6794b35978
commit
6212b816f0
1 changed files with 20 additions and 0 deletions
|
@ -44,6 +44,7 @@
|
||||||
|
|
||||||
{.hint[ConvFromXtoItselfNotNeeded]:off.}
|
{.hint[ConvFromXtoItselfNotNeeded]:off.}
|
||||||
|
|
||||||
|
import std/tables
|
||||||
import arr_ref # for SliceMem
|
import arr_ref # for SliceMem
|
||||||
|
|
||||||
type LoadableResourceStatus* = enum
|
type LoadableResourceStatus* = enum
|
||||||
|
@ -236,6 +237,15 @@ func escapeUTF8*(s: string): string =
|
||||||
|
|
||||||
# TODO: automatically disable threads when not in main thread
|
# TODO: automatically disable threads when not in main thread
|
||||||
|
|
||||||
|
type ProtocolHandler* = proc(uri: string): proc(self: LoadableResource)
|
||||||
|
var custom_protocol_handlers: Table[string, ProtocolHandler]
|
||||||
|
|
||||||
|
proc registerCustomProtocol*(prefix: string, handler: ProtocolHandler) =
|
||||||
|
## Registers a handler for a custom protocol. The function will be run for
|
||||||
|
## each uri that is requested, and will return a start_func, which in turn
|
||||||
|
## will call self.onload()
|
||||||
|
custom_protocol_handlers[prefix] = handler
|
||||||
|
|
||||||
proc loadUri*(
|
proc loadUri*(
|
||||||
uri: string,
|
uri: string,
|
||||||
onload_func: proc(ok: bool, err: string, data: SliceMem[byte]) = nil,
|
onload_func: proc(ok: bool, err: string, data: SliceMem[byte]) = nil,
|
||||||
|
@ -246,6 +256,15 @@ proc loadUri*(
|
||||||
|
|
||||||
echo "fetching ", uri
|
echo "fetching ", uri
|
||||||
|
|
||||||
|
for k,v in custom_protocol_handlers:
|
||||||
|
if uri.startswith k:
|
||||||
|
proc str(): string = uri
|
||||||
|
var self = newLoadableResource[Fetch](v(uri), str, false)
|
||||||
|
self.onload_func = onload_func
|
||||||
|
if auto_start:
|
||||||
|
start(self)
|
||||||
|
return self
|
||||||
|
|
||||||
var start_func: proc(self: LoadableResource)
|
var start_func: proc(self: LoadableResource)
|
||||||
var self: Fetch
|
var self: Fetch
|
||||||
var uri = uri
|
var uri = uri
|
||||||
|
@ -318,3 +337,4 @@ proc loadUri*(
|
||||||
if auto_start:
|
if auto_start:
|
||||||
start(self)
|
start(self)
|
||||||
return self
|
return self
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue