Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for adding custom resources/parameters #25

Merged
merged 14 commits into from
May 16, 2024
Next Next commit
add custom parameters type
mlange-42 committed May 16, 2024
commit ee397837f9e2b9aed92093883f195cb18e6963a4
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -3,7 +3,7 @@ module github.com/mlange-42/beecs-cli
go 1.22.0

require (
github.com/mlange-42/arche v0.12.0
github.com/mlange-42/arche v0.12.1-0.20240516105122-6799138aa690
github.com/mlange-42/arche-model v0.8.1
github.com/mlange-42/arche-pixel v0.9.0
github.com/mlange-42/beecs v0.1.1-0.20240515233930-c565be9cfc5a
6 changes: 2 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
@@ -45,14 +45,12 @@ github.com/mazznoer/colorgrad v0.9.1 h1:MB80JYVndKWSMEM1beNqnuOowWGhoQc3DXWXkFp6
github.com/mazznoer/colorgrad v0.9.1/go.mod h1:WX2R9wt9B47+txJZVVpM9LY+LAGIdi4lTI5wIyreDH4=
github.com/mazznoer/csscolorparser v0.1.2 h1:/UBHuQg792ePmGFzTQAC9u+XbFr7/HzP/Gj70Phyz2A=
github.com/mazznoer/csscolorparser v0.1.2/go.mod h1:Aj22+L/rYN/Y6bj3bYqO3N6g1dtdHtGfQ32xZ5PJQic=
github.com/mlange-42/arche v0.12.0 h1:qGrTcUlmo8Ad95x3k058a65UKxN/+GDCQlD1UMU3Xrw=
github.com/mlange-42/arche v0.12.0/go.mod h1:gJ5J8vBreqrf4TcBomBFPGnWdE5P3qa4LtxYHn1gDcg=
github.com/mlange-42/arche v0.12.1-0.20240516105122-6799138aa690 h1:Bp491qQ601hjVGoDApCxCSp3Wt1GoKvIXf/Nphuj76k=
github.com/mlange-42/arche v0.12.1-0.20240516105122-6799138aa690/go.mod h1:gJ5J8vBreqrf4TcBomBFPGnWdE5P3qa4LtxYHn1gDcg=
github.com/mlange-42/arche-model v0.8.1 h1:PA6cq76wqz8IG02eoBzRh3tS+WYZ3aztZmMfwEjkr2M=
github.com/mlange-42/arche-model v0.8.1/go.mod h1:5Pzd3iAgdGNTiMgFJxHdc6a7qoaQPBK9xmyhrKTMmV8=
github.com/mlange-42/arche-pixel v0.9.0 h1:UhHXUjSpvbKeJgRiWQWGYPrNbnu+007U9HaBBgW7uOY=
github.com/mlange-42/arche-pixel v0.9.0/go.mod h1:UWr4ZfpvreTRWr76dqi30VCax19kEbiSDSNrbZTbMDQ=
github.com/mlange-42/beecs v0.1.1-0.20240515220635-14fb796648eb h1:s0BTpurjAcDKolESZz3lJslBdYsok/0X7DgCqtfedRA=
github.com/mlange-42/beecs v0.1.1-0.20240515220635-14fb796648eb/go.mod h1:4USO5e+eeIkKe6oVbhhBpbRIMYWsrgD0d+Fu/aEHBkY=
github.com/mlange-42/beecs v0.1.1-0.20240515233930-c565be9cfc5a h1:fEjnC7l7WnyCEZOvXfrkfHNeAE9eZu5QGXQIUMZG9Z0=
github.com/mlange-42/beecs v0.1.1-0.20240515233930-c565be9cfc5a/go.mod h1:4USO5e+eeIkKe6oVbhhBpbRIMYWsrgD0d+Fu/aEHBkY=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
77 changes: 77 additions & 0 deletions params/custom.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package params

import (
"bytes"
"encoding/json"
"fmt"
"os"
"reflect"

"github.com/mlange-42/arche/ecs"
"github.com/mlange-42/beecs-cli/util"
baseparams "github.com/mlange-42/beecs/params"
)

type entry struct {
Bytes []byte
}

func (e *entry) UnmarshalJSON(jsonData []byte) error {
e.Bytes = jsonData
return nil
}

type CustomParams struct {
Params baseparams.DefaultParams
Custom map[reflect.Type]any
}

type customParamsJs struct {
Params baseparams.DefaultParams
Custom map[string]entry
}

func (p *CustomParams) FromJSON(path string) error {
file, err := os.Open(path)
if err != nil {
return err
}
defer file.Close()

decoder := json.NewDecoder(file)
decoder.DisallowUnknownFields()

pars := customParamsJs{}
err = decoder.Decode(&pars)
if err != nil {
return err
}

p.Params = pars.Params

for tpName, entry := range pars.Custom {
tp, ok := util.GetResource(tpName)
if !ok {
return fmt.Errorf("resource type '%s' is not registered", tpName)
}
resourceVal := reflect.New(tp).Interface()

decoder := json.NewDecoder(bytes.NewReader(entry.Bytes))
decoder.DisallowUnknownFields()
if err := decoder.Decode(&resourceVal); err != nil {
return err
}

p.Custom[tp] = resourceVal
}
return nil
}

func (p *CustomParams) Apply(world *ecs.World) {
p.Params.Apply(world)

for tp, res := range p.Custom {
id := ecs.ResourceTypeID(world, tp)
world.Resources().Add(id, res)
}
}
15 changes: 14 additions & 1 deletion util/registry.go
Original file line number Diff line number Diff line change
@@ -7,23 +7,36 @@ import (
)

var observerRegistry map[string]reflect.Type
var resourcesRegistry map[string]reflect.Type

func init() {
observerRegistry = map[string]reflect.Type{}

registerObserver[obs.WorkerCohorts]()
registerObserver[obs.ForagingPeriod]()
registerObserver[obs.Stores]()
registerObserver[obs.PatchNectar]()
registerObserver[obs.PatchPollen]()

resourcesRegistry = map[string]reflect.Type{}
registerResource[obs.PatchPollen]()
}

func registerObserver[T any]() {
tp := reflect.TypeOf((*T)(nil)).Elem()
observerRegistry[tp.String()] = tp
}

func registerResource[T any]() {
tp := reflect.TypeOf((*T)(nil)).Elem()
resourcesRegistry[tp.String()] = tp
}

func GetObserver(name string) (reflect.Type, bool) {
t, ok := observerRegistry[name]
return t, ok
}

func GetResource(name string) (reflect.Type, bool) {
t, ok := resourcesRegistry[name]
return t, ok
}