examples,os: add an os.asset module, use it to simplify code in examples/, by removing $if android { checks (#22281)

This commit is contained in:
Delyan Angelov 2024-09-22 16:04:05 +03:00 committed by GitHub
parent 64d1770cf1
commit cfa91d81d7
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
12 changed files with 69 additions and 117 deletions

View file

@ -1,52 +1,24 @@
module obj
import os
import os.asset
// read a file as single lines
pub fn read_lines_from_file(file_path string) []string {
mut path := ''
mut rows := []string{}
$if android {
path = 'models/' + file_path
bts := os.read_apk_asset(path) or {
eprintln('File [${path}] NOT FOUND!')
return rows
}
rows = bts.bytestr().split_into_lines()
} $else {
path = if os.exists(file_path) {
file_path
} else {
os.resource_abs_path('assets/models/${file_path}')
}
rows = os.read_lines(path) or {
eprintln('File [${path}] NOT FOUND! file_path: ${file_path}')
return rows
}
if os.exists(file_path) {
return os.read_lines(file_path) or { [] }
}
return rows
return read_bytes_from_file(file_path).bytestr().split_into_lines()
}
// read a file as []u8
pub fn read_bytes_from_file(file_path string) []u8 {
mut path := ''
mut buffer := []u8{}
$if android {
path = 'models/' + file_path
buffer = os.read_apk_asset(path) or {
eprintln('Texture file: [${path}] NOT FOUND!')
exit(0)
}
} $else {
path = if os.exists(file_path) {
file_path
} else {
os.resource_abs_path('assets/models/${file_path}')
}
buffer = os.read_bytes(path) or {
eprintln('Texture file: [${path}] NOT FOUND!')
exit(0)
}
if os.exists(file_path) {
return os.read_bytes(file_path) or { [] }
}
mpath := 'models/${file_path}'
return asset.read_bytes('assets', mpath) or {
eprintln('Model file NOT FOUND: `${mpath}`')
exit(0)
}
return buffer
}