Skip to content

Commit

Permalink
U: 为energy兼容Go低版本增加emfs.IEmbedFS内置资源接口, 该接口适配于 embed.FS, 和 eneryge/go…
Browse files Browse the repository at this point in the history
…-bindata
  • Loading branch information
sxmxta committed Dec 31, 2023
1 parent e311459 commit 4114708
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 123 deletions.
37 changes: 11 additions & 26 deletions energy/emfs/emfs.go
Original file line number Diff line number Diff line change
@@ -1,49 +1,34 @@
package emfs

import (
"embed"
"io/ioutil"
"os"
)

var (
resourcesFS *embed.FS
libsFS *embed.FS
resourcesFS IEmbedFS
libsFS IEmbedFS
)

func SetLibsFS(lib *embed.FS) {
type IEmbedFS interface {
ReadFile(name string) ([]byte, error)
}

func SetLibsFS(lib IEmbedFS) {
libsFS = lib
}

func SetResourcesFS(resource *embed.FS) {
func SetResourcesFS(resource IEmbedFS) {
resourcesFS = resource
}

func GetLibsFS() *embed.FS {
func GetLibsFS() IEmbedFS {
return libsFS
}

func GetResourcesFS() *embed.FS {
func GetResourcesFS() IEmbedFS {
return resourcesFS
}

func IsExist(file string) bool {
if GetResourcesFS() == nil {
return false
}
if fs, err := GetResourcesFS().Open(file); err != nil {
return false
} else {
_, err := fs.Stat()
if os.IsExist(err) {
return true
} else if os.IsNotExist(err) {
return false
}
return true
}
}

func GetResources(file string) ([]byte, error) {
if GetResourcesFS() != nil {
return GetResourcesFS().ReadFile(file)
Expand All @@ -60,7 +45,7 @@ func GetLibs(fileName string) ([]byte, error) {
}
}

func SetEMFS(libs *embed.FS, resources *embed.FS) {
func SetEMFS(libs IEmbedFS, resources IEmbedFS) {
SetLibsFS(libs)
SetResourcesFS(resources)
}
18 changes: 3 additions & 15 deletions energy/inits/inits.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
package inits

import (
"embed"
"fmt"
"github.com/energye/golcl/energy/consts"
"github.com/energye/golcl/energy/emfs"
Expand All @@ -26,7 +25,6 @@ import (
"github.com/energye/golcl/lcl/rtl/version"
"github.com/energye/golcl/pkgs/libname"
"github.com/energye/golcl/pkgs/macapp"
"io"
"os"
"path"
"runtime"
Expand All @@ -37,7 +35,7 @@ var (
emfsLibsPath = "libs"
)

func Init(libs *embed.FS, resources *embed.FS) {
func Init(libs emfs.IEmbedFS, resources emfs.IEmbedFS) {
emfs.SetEMFS(libs, resources)
if libname.LibName == "" {
if runtime.GOOS == "darwin" {
Expand Down Expand Up @@ -74,25 +72,15 @@ func Init(libs *embed.FS, resources *embed.FS) {
// 如果liblcl动态库内置到EXE中, 在EXE中把liblcl释放到out目录
func releaseLib(fsPath, out string) {
if emfs.GetLibsFS() != nil {
var fsFile, err = emfs.GetLibsFS().Open(fsPath)
var liblcl, err = emfs.GetLibsFS().ReadFile(fsPath)
if err == nil {
var file *os.File
file, err = os.OpenFile(out, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0644)
if err != nil {
panic(err)
}
defer file.Close()
defer fsFile.Close()
var n int
//读取数据
buf := make([]byte, 4096)
for {
n, err = fsFile.Read(buf)
if err == io.EOF {
break
}
file.Write(buf[:n])
}
file.Write(liblcl)
fmt.Println("release success.")
}
}
Expand Down
21 changes: 1 addition & 20 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,22 +1,3 @@
module github.com/energye/golcl

go 1.18

require (
github.com/Knetic/govaluate v3.0.0+incompatible
github.com/disintegration/gift v1.2.1
github.com/energye/dylib v0.0.0-20230814115552-32dda0972e9e
github.com/fogleman/gg v1.3.0
github.com/go-ole/go-ole v1.2.6
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0
github.com/gomodule/redigo v2.0.0+incompatible
github.com/tryor/gdiplus v0.0.0-20200830101413-c570de9579b3
github.com/tryor/winapi v0.0.0-20200525040926-cd87d62e2f9b
github.com/vdobler/chart v1.0.0
golang.org/x/image v0.5.0
)

require (
github.com/llgcode/draw2d v0.0.0-20180825133448-f52c8a71aff0 // indirect
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f // indirect
)
go 1.10
58 changes: 0 additions & 58 deletions go.sum

This file was deleted.

4 changes: 0 additions & 4 deletions samples/action/main.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
package main

import (
"embed"
"github.com/energye/golcl/energy/inits"
"github.com/energye/golcl/lcl"
"github.com/energye/golcl/samples/action/src"
)
import _ "github.com/energye/golcl/pkgs/winappres"

//go:embed resources
var resources embed.FS

func main() {
inits.Init(nil, nil)
lcl.Application.Initialize()
Expand Down

0 comments on commit 4114708

Please sign in to comment.