From 19ee7d2516520db3a034d7b632fc36344264bddf Mon Sep 17 00:00:00 2001 From: lxgr-linux Date: Thu, 14 Nov 2024 00:43:32 +0100 Subject: [PATCH] feat(#267): Introduced embeded resource files --- pkg/server/config/config.go | 6 +++--- pkg/server/main.go | 9 ++++++++- pkg/server/resources/resources.go | 22 +++++++++++++++++++++- 3 files changed, 32 insertions(+), 5 deletions(-) diff --git a/pkg/server/config/config.go b/pkg/server/config/config.go index 30272f69..a9136cd5 100644 --- a/pkg/server/config/config.go +++ b/pkg/server/config/config.go @@ -1,10 +1,10 @@ package config import ( - "gopkg.in/yaml.v3" "os" "github.com/joho/godotenv" + "gopkg.in/yaml.v3" ) type Config struct { @@ -31,8 +31,8 @@ func FromEnv() Config { ServerPort: getEnvWithFallBack("POKETE_SERVER_PORT", "9988"), APIPort: getEnvWithFallBack("POKETE_API_PORT", "9989"), ServerType: getEnvWithFallBack("POKETE_SERVER_TYPE", "tcp"), - ClientVersion: getEnvWithFallBack("POKETE_SERVER_CLIENT_VERSION", "0.9.1"), - EntryMap: getEnvWithFallBack("POKETE_SERVER_CLIENT_ENTRYMAP", "playmap_1"), + ClientVersion: getEnvWithFallBack("POKETE_SERVER_CLIENT_VERSION", "0.9.2"), + EntryMap: getEnvWithFallBack("POKETE_SERVER_CLIENT_ENTRYMAP", "servermap_1"), } } diff --git a/pkg/server/main.go b/pkg/server/main.go index 08b5d76b..23df6b9d 100644 --- a/pkg/server/main.go +++ b/pkg/server/main.go @@ -1,6 +1,7 @@ package main import ( + _ "embed" "log" "github.com/lxgr-linux/pokete/server/config" @@ -9,6 +10,12 @@ import ( "github.com/lxgr-linux/pokete/server/resources" ) +//go:embed res/base_assets.json +var baseAssetBytes []byte + +//go:embed res/assets.json +var assetBytes []byte + func main() { p, err := buildPokete() if err != nil { @@ -23,7 +30,7 @@ func main() { func buildPokete() (*pokete.Pokete, error) { cfg := config.FromEnv() - r, err := resources.FromDir("./res") + r, err := resources.FromBytes(assetBytes, baseAssetBytes) if err != nil { return nil, err } diff --git a/pkg/server/resources/resources.go b/pkg/server/resources/resources.go index 17747c12..82654288 100644 --- a/pkg/server/resources/resources.go +++ b/pkg/server/resources/resources.go @@ -2,9 +2,10 @@ package resources import ( "encoding/json" - "github.com/lxgr-linux/pokete/resources" "os" "path" + + "github.com/lxgr-linux/pokete/resources" ) type Resources struct { @@ -26,6 +27,20 @@ func FromDir(baseDir string) (*Resources, error) { return &Resources{assets, baseAssets}, nil } +func FromBytes(assetBytes []byte, baseAssetBytes []byte) (*Resources, error) { + assets, err := readBytes[resources.Assets](assetBytes) + if err != nil { + return nil, err + } + + baseAssets, err := readBytes[resources.BaseAssets](baseAssetBytes) + if err != nil { + return nil, err + } + + return &Resources{assets, baseAssets}, nil +} + func readFile[T any](fileName string) (temp T, err error) { content, err := os.ReadFile(fileName) if err != nil { @@ -36,3 +51,8 @@ func readFile[T any](fileName string) (temp T, err error) { return } + +func readBytes[T any](b []byte) (temp T, err error) { + err = json.Unmarshal(b, &temp) + return +}