Skip to content

Commit

Permalink
feat(#267): Introduced embeded resource files
Browse files Browse the repository at this point in the history
  • Loading branch information
lxgr-linux committed Nov 13, 2024
1 parent 241914e commit 19ee7d2
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 5 deletions.
6 changes: 3 additions & 3 deletions pkg/server/config/config.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package config

import (
"gopkg.in/yaml.v3"
"os"

"github.com/joho/godotenv"
"gopkg.in/yaml.v3"
)

type Config struct {
Expand All @@ -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"),
}
}

Expand Down
9 changes: 8 additions & 1 deletion pkg/server/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
_ "embed"
"log"

"github.com/lxgr-linux/pokete/server/config"
Expand All @@ -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 {
Expand All @@ -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
}
Expand Down
22 changes: 21 additions & 1 deletion pkg/server/resources/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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
}

0 comments on commit 19ee7d2

Please sign in to comment.