Bug fixing

This commit is contained in:
Alexei Anoshenko 2024-05-20 16:51:30 +03:00
parent b4d1e34f21
commit 5edeca2286
4 changed files with 47 additions and 33 deletions

View File

@ -354,6 +354,8 @@ var apps = []*application{}
// StartApp - create the new application and start it // StartApp - create the new application and start it
func StartApp(addr string, createContentFunc func(Session) SessionContent, params AppParams) { func StartApp(addr string, createContentFunc func(Session) SessionContent, params AppParams) {
resources.scanDefaultResourcePath()
app := new(application) app := new(application)
app.params = params app.params = params
app.sessions = map[int]sessionInfo{} app.sessions = map[int]sessionInfo{}

View File

@ -46,17 +46,17 @@ var resources = resourceManager{
func AddEmbedResources(fs *embed.FS) { func AddEmbedResources(fs *embed.FS) {
resources.embedFS = append(resources.embedFS, fs) resources.embedFS = append(resources.embedFS, fs)
rootDirs := embedRootDirs(fs) rootDirs := resources.embedRootDirs(fs)
for _, dir := range rootDirs { for _, dir := range rootDirs {
switch dir { switch dir {
case imageDir: case imageDir:
scanEmbedImagesDir(fs, dir, "") resources.scanEmbedImagesDir(fs, dir, "")
case themeDir: case themeDir:
scanEmbedThemesDir(fs, dir) resources.scanEmbedThemesDir(fs, dir)
case stringsDir: case stringsDir:
scanEmbedStringsDir(fs, dir) resources.scanEmbedStringsDir(fs, dir)
case viewDir, rawDir: case viewDir, rawDir:
// do nothing // do nothing
@ -67,13 +67,13 @@ func AddEmbedResources(fs *embed.FS) {
if file.IsDir() { if file.IsDir() {
switch file.Name() { switch file.Name() {
case imageDir: case imageDir:
scanEmbedImagesDir(fs, dir+"/"+imageDir, "") resources.scanEmbedImagesDir(fs, dir+"/"+imageDir, "")
case themeDir: case themeDir:
scanEmbedThemesDir(fs, dir+"/"+themeDir) resources.scanEmbedThemesDir(fs, dir+"/"+themeDir)
case stringsDir: case stringsDir:
scanEmbedStringsDir(fs, dir+"/"+stringsDir) resources.scanEmbedStringsDir(fs, dir+"/"+stringsDir)
case viewDir, rawDir: case viewDir, rawDir:
// do nothing // do nothing
@ -85,7 +85,7 @@ func AddEmbedResources(fs *embed.FS) {
} }
} }
func embedRootDirs(fs *embed.FS) []string { func (resources *resourceManager) embedRootDirs(fs *embed.FS) []string {
result := []string{} result := []string{}
if files, err := fs.ReadDir("."); err == nil { if files, err := fs.ReadDir("."); err == nil {
for _, file := range files { for _, file := range files {
@ -97,34 +97,34 @@ func embedRootDirs(fs *embed.FS) []string {
return result return result
} }
func scanEmbedThemesDir(fs *embed.FS, dir string) { func (resources *resourceManager) scanEmbedThemesDir(fs *embed.FS, dir string) {
if files, err := fs.ReadDir(dir); err == nil { if files, err := fs.ReadDir(dir); err == nil {
for _, file := range files { for _, file := range files {
name := file.Name() name := file.Name()
path := dir + "/" + name path := dir + "/" + name
if file.IsDir() { if file.IsDir() {
scanEmbedThemesDir(fs, path) resources.scanEmbedThemesDir(fs, path)
} else if strings.ToLower(filepath.Ext(name)) == ".rui" { } else if strings.ToLower(filepath.Ext(name)) == ".rui" {
if data, err := fs.ReadFile(path); err == nil { if data, err := fs.ReadFile(path); err == nil {
registerThemeText(string(data)) resources.registerThemeText(string(data))
} }
} }
} }
} }
} }
func scanEmbedImagesDir(fs *embed.FS, dir, prefix string) { func (resources *resourceManager) scanEmbedImagesDir(fs *embed.FS, dir, prefix string) {
if files, err := fs.ReadDir(dir); err == nil { if files, err := fs.ReadDir(dir); err == nil {
for _, file := range files { for _, file := range files {
name := file.Name() name := file.Name()
path := dir + "/" + name path := dir + "/" + name
if file.IsDir() { if file.IsDir() {
scanEmbedImagesDir(fs, path, prefix+name+"/") resources.scanEmbedImagesDir(fs, path, prefix+name+"/")
} else { } else {
ext := strings.ToLower(filepath.Ext(name)) ext := strings.ToLower(filepath.Ext(name))
switch ext { switch ext {
case ".png", ".jpg", ".jpeg", ".svg", ".gif", ".bmp", ".webp": case ".png", ".jpg", ".jpeg", ".svg", ".gif", ".bmp", ".webp":
registerImage(fs, path, prefix+name) resources.registerImage(fs, path, prefix+name)
} }
} }
} }
@ -136,7 +136,7 @@ func invalidImageFileFormat(filename string) {
`". Image file name format: name[@x-param].ext (examples: icon.png, icon@1.5x.png)`) `". Image file name format: name[@x-param].ext (examples: icon.png, icon@1.5x.png)`)
} }
func registerImage(fs *embed.FS, path, filename string) { func (resources *resourceManager) registerImage(fs *embed.FS, path, filename string) {
resources.images[filename] = imagePath{fs: fs, path: path} resources.images[filename] = imagePath{fs: fs, path: path}
start := strings.LastIndex(filename, "@") start := strings.LastIndex(filename, "@")
@ -169,16 +169,16 @@ func registerImage(fs *embed.FS, path, filename string) {
} }
} }
func scanImagesDirectory(path, filePrefix string) { func (resources *resourceManager) scanImagesDirectory(path, filePrefix string) {
if files, err := os.ReadDir(path); err == nil { if files, err := os.ReadDir(path); err == nil {
for _, file := range files { for _, file := range files {
filename := file.Name() filename := file.Name()
if filename[0] != '.' { if filename[0] != '.' {
newPath := path + `/` + filename newPath := path + `/` + filename
if !file.IsDir() { if !file.IsDir() {
registerImage(nil, newPath, filePrefix+filename) resources.registerImage(nil, newPath, filePrefix+filename)
} else { } else {
scanImagesDirectory(newPath, filePrefix+filename+"/") resources.scanImagesDirectory(newPath, filePrefix+filename+"/")
} }
} }
} }
@ -187,17 +187,17 @@ func scanImagesDirectory(path, filePrefix string) {
} }
} }
func scanThemesDir(path string) { func (resources *resourceManager) scanThemesDir(path string) {
if files, err := os.ReadDir(path); err == nil { if files, err := os.ReadDir(path); err == nil {
for _, file := range files { for _, file := range files {
filename := file.Name() filename := file.Name()
if filename[0] != '.' { if filename[0] != '.' {
newPath := path + `/` + filename newPath := path + `/` + filename
if file.IsDir() { if file.IsDir() {
scanThemesDir(newPath) resources.scanThemesDir(newPath)
} else if strings.ToLower(filepath.Ext(newPath)) == ".rui" { } else if strings.ToLower(filepath.Ext(newPath)) == ".rui" {
if data, err := os.ReadFile(newPath); err == nil { if data, err := os.ReadFile(newPath); err == nil {
registerThemeText(string(data)) resources.registerThemeText(string(data))
} else { } else {
ErrorLog(err.Error()) ErrorLog(err.Error())
} }
@ -217,12 +217,21 @@ func SetResourcePath(path string) {
resources.path += "/" resources.path += "/"
} }
scanImagesDirectory(resources.path+imageDir, "") resources.scanImagesDirectory(resources.path+imageDir, "")
scanThemesDir(resources.path + themeDir) resources.scanThemesDir(resources.path + themeDir)
scanStringsDir(resources.path + stringsDir) resources.scanStringsDir(resources.path + stringsDir)
} }
func registerThemeText(text string) bool { func (resources *resourceManager) scanDefaultResourcePath() {
if exe, err := os.Executable(); err == nil {
path := filepath.Dir(exe) + "/resources/"
resources.scanImagesDirectory(path+imageDir, "")
resources.scanThemesDir(path + themeDir)
resources.scanStringsDir(path + stringsDir)
}
}
func (resources *resourceManager) registerThemeText(text string) bool {
theme, ok := CreateThemeFromText(text) theme, ok := CreateThemeFromText(text)
if !ok { if !ok {
return false return false
@ -268,7 +277,7 @@ func serveResourceFile(filename string, w http.ResponseWriter, r *http.Request)
if serveEmbed(fs, filename) { if serveEmbed(fs, filename) {
return true return true
} }
for _, dir := range embedRootDirs(fs) { for _, dir := range resources.embedRootDirs(fs) {
if serveEmbed(fs, dir+"/"+filename) { if serveEmbed(fs, dir+"/"+filename) {
return true return true
} }
@ -314,9 +323,10 @@ func serveResourceFile(filename string, w http.ResponseWriter, r *http.Request)
return false return false
} }
// ReadRawResource returns the contents of the raw resource with the specified name
func ReadRawResource(filename string) []byte { func ReadRawResource(filename string) []byte {
for _, fs := range resources.embedFS { for _, fs := range resources.embedFS {
rootDirs := embedRootDirs(fs) rootDirs := resources.embedRootDirs(fs)
for _, dir := range rootDirs { for _, dir := range rootDirs {
switch dir { switch dir {
case imageDir, themeDir, viewDir: case imageDir, themeDir, viewDir:
@ -351,11 +361,12 @@ func ReadRawResource(filename string) []byte {
return nil return nil
} }
// AllRawResources returns the list of all raw resouces
func AllRawResources() []string { func AllRawResources() []string {
result := []string{} result := []string{}
for _, fs := range resources.embedFS { for _, fs := range resources.embedFS {
rootDirs := embedRootDirs(fs) rootDirs := resources.embedRootDirs(fs)
for _, dir := range rootDirs { for _, dir := range rootDirs {
switch dir { switch dir {
case imageDir, themeDir, viewDir: case imageDir, themeDir, viewDir:
@ -397,6 +408,7 @@ func AllRawResources() []string {
return result return result
} }
// AllImageResources returns the list of all image resouces
func AllImageResources() []string { func AllImageResources() []string {
result := make([]string, 0, len(resources.images)) result := make([]string, 0, len(resources.images))
for image := range resources.images { for image := range resources.images {

View File

@ -9,13 +9,13 @@ import (
var stringResources = map[string]map[string]string{} var stringResources = map[string]map[string]string{}
func scanEmbedStringsDir(fs *embed.FS, dir string) { func (resources *resourceManager) scanEmbedStringsDir(fs *embed.FS, dir string) {
if files, err := fs.ReadDir(dir); err == nil { if files, err := fs.ReadDir(dir); err == nil {
for _, file := range files { for _, file := range files {
name := file.Name() name := file.Name()
path := dir + "/" + name path := dir + "/" + name
if file.IsDir() { if file.IsDir() {
scanEmbedStringsDir(fs, path) resources.scanEmbedStringsDir(fs, path)
} else if strings.ToLower(filepath.Ext(name)) == ".rui" { } else if strings.ToLower(filepath.Ext(name)) == ".rui" {
if data, err := fs.ReadFile(path); err == nil { if data, err := fs.ReadFile(path); err == nil {
loadStringResources(string(data)) loadStringResources(string(data))
@ -27,14 +27,14 @@ func scanEmbedStringsDir(fs *embed.FS, dir string) {
} }
} }
func scanStringsDir(path string) { func (resources *resourceManager) scanStringsDir(path string) {
if files, err := os.ReadDir(path); err == nil { if files, err := os.ReadDir(path); err == nil {
for _, file := range files { for _, file := range files {
filename := file.Name() filename := file.Name()
if filename[0] != '.' { if filename[0] != '.' {
newPath := path + `/` + filename newPath := path + `/` + filename
if file.IsDir() { if file.IsDir() {
scanStringsDir(newPath) resources.scanStringsDir(newPath)
} else if strings.ToLower(filepath.Ext(newPath)) == ".rui" { } else if strings.ToLower(filepath.Ext(newPath)) == ".rui" {
if data, err := os.ReadFile(newPath); err == nil { if data, err := os.ReadFile(newPath); err == nil {
loadStringResources(string(data)) loadStringResources(string(data))

View File

@ -114,7 +114,7 @@ func CreateViewFromResources(session Session, name string) View {
} }
for _, fs := range resources.embedFS { for _, fs := range resources.embedFS {
rootDirs := embedRootDirs(fs) rootDirs := resources.embedRootDirs(fs)
for _, dir := range rootDirs { for _, dir := range rootDirs {
switch dir { switch dir {
case imageDir, themeDir, rawDir: case imageDir, themeDir, rawDir: