mirror of
https://github.com/vlang/v.git
synced 2025-09-13 14:32:26 +03:00
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:
parent
64d1770cf1
commit
cfa91d81d7
12 changed files with 69 additions and 117 deletions
|
@ -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
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue