Initial project import
This commit is contained in:
commit
23f2d29e32
11957 files changed
+1754577
No files matched your search
@@ -0,0 +1,11 @@
|
||||
# Asset
|
||||
|
||||
The `asset` module provides a cross platform way to read assets, without cluttering
|
||||
the user code with comptime conditionals, like `$if ios {` or `$if android {` etc.
|
||||
|
||||
Currently it supports Android assets, and desktop applications, that do not use
|
||||
archived/zipped files, but could be extended to support archived files too in the future.
|
||||
|
||||
It relies on the assumption that each platform has a way to either read files, relative
|
||||
to the location of the executable, or a platform specific way, to package all asset files
|
||||
into the application archive/package/executable, that is then distributed to the end users.
|
||||
@@ -0,0 +1,44 @@
|
||||
module asset
|
||||
|
||||
import os
|
||||
|
||||
// get_path returns a platform specific path, based on the given asset base folder and relative path
|
||||
// of a resource in it. On desktop systems, it returns a path relative to the location of the executable.
|
||||
// On Android, it will return just the relative_path segment, allowing you to later use os.read_apk_asset
|
||||
// to read from it.
|
||||
@[manualfree]
|
||||
pub fn get_path(base_folder string, relative_path string) string {
|
||||
$if android {
|
||||
return relative_path.clone()
|
||||
} $else {
|
||||
fpath := os.join_path_single(base_folder, relative_path)
|
||||
defer { unsafe { fpath.free() } }
|
||||
return os.resource_abs_path(fpath)
|
||||
}
|
||||
}
|
||||
|
||||
// read_bytes will read all of the given asset, specified by its base_folder and relative path in it.
|
||||
// On Android, it will use os.read_apk_asset, relying that the asset base folder has been prepared, and
|
||||
// prepackaged inside your APK. On desktop systems, it will use the base_folder and relative_path, to
|
||||
// locate the file, in a way, that is relative to the executable.
|
||||
@[manualfree]
|
||||
pub fn read_bytes(base_folder string, relative_path string) ![]u8 {
|
||||
fpath := get_path(base_folder, relative_path)
|
||||
defer { unsafe { fpath.free() } }
|
||||
mut f_read := os.read_bytes
|
||||
$if android {
|
||||
f_read = os.read_apk_asset
|
||||
}
|
||||
res := f_read(fpath)!
|
||||
return res
|
||||
}
|
||||
|
||||
// read_text will return the full content of the given asset as a string.
|
||||
// See also read_bytes.
|
||||
@[manualfree]
|
||||
pub fn read_text(base_folder string, relative_path string) !string {
|
||||
bytes := read_bytes(base_folder, relative_path)!
|
||||
defer { unsafe { bytes.free() } }
|
||||
res := bytes.bytestr()
|
||||
return res
|
||||
}
|
||||
Reference in new issue
Block a user