From c9954afa7ff1fa614487830c9f40ed9d60a658d6 Mon Sep 17 00:00:00 2001 From: Alexei Anoshenko <2277098+anoshenko@users.noreply.github.com> Date: Wed, 17 Jul 2024 16:32:05 +0300 Subject: [PATCH] Added OpenRawResource function --- CHANGELOG.md | 1 + resources.go | 41 ++++++++++++++++++++++++++++++++++++++++- 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d69d149..9e18103 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ * Added "mod", "rem", "round", "round-up", "round-down", and "round-to-zero" SizeFunc functions * Added ModSize, RemSize, RoundSize, RoundUpSize, RoundDownSize, and RoundToZeroSize functions * Added Start, Stop, Pause, and Resume methods to Animation interface +* Added OpenRawResource function # v0.16.0 * Can use ListAdapter as "content" property value of ListLayout diff --git a/resources.go b/resources.go index 91a6ffd..a0ae401 100644 --- a/resources.go +++ b/resources.go @@ -3,6 +3,7 @@ package rui import ( "embed" "io" + "io/fs" "net/http" "os" "path/filepath" @@ -329,7 +330,7 @@ func ReadRawResource(filename string) []byte { rootDirs := resources.embedRootDirs(fs) for _, dir := range rootDirs { switch dir { - case imageDir, themeDir, viewDir: + case imageDir, themeDir, viewDir, stringsDir: // do nothing case rawDir: @@ -361,6 +362,44 @@ func ReadRawResource(filename string) []byte { return nil } +// OpenRawResource returns the contents of the raw resource with the specified name +func OpenRawResource(filename string) fs.File { + for _, fs := range resources.embedFS { + rootDirs := resources.embedRootDirs(fs) + for _, dir := range rootDirs { + switch dir { + case imageDir, themeDir, viewDir, stringsDir: + // do nothing + + case rawDir: + if file, err := fs.Open(dir + "/" + filename); err == nil { + return file + } + + default: + if file, err := fs.Open(dir + "/" + rawDir + "/" + filename); err == nil { + return file + } + } + } + } + + if resources.path != "" { + if file, err := os.Open(resources.path + rawDir + "/" + filename); err == nil { + return file + } + } + + if exe, err := os.Executable(); err == nil { + if file, err := os.Open(filepath.Dir(exe) + "/resources/" + rawDir + "/" + filename); err == nil { + return file + } + } + + ErrorLogF(`The "%s" raw file don't found`, filename) + return nil +} + // AllRawResources returns the list of all raw resouces func AllRawResources() []string { result := []string{}