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

Move CustomParams from beecs-cli to the core module #68

Merged
merged 1 commit into from
May 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
- Patches have coordinates for visualization; calculated if not provided (#65)
- Daily patch visits for pollen and nectar are counted, adds respective observers (#66)
- Stats for foraging rounds are recorded, adds respective table observer (#67)
- Move `CustomParams` from beecs-cli to the core module (#68)

### Bugfixes

Expand Down
107 changes: 107 additions & 0 deletions params/custom.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package params

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

"github.com/mlange-42/arche/ecs"
"github.com/mlange-42/beecs/registry"
"github.com/mlange-42/beecs/util"
)

type entry struct {
Bytes []byte
}

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

func (e entry) MarshalJSON() ([]byte, error) {
return e.Bytes, nil
}

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

type customParamsJs struct {
Parameters 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{
Parameters: p.Parameters,
}
err = decoder.Decode(&pars)
if err != nil {
return err
}

p.Parameters = pars.Parameters
if p.Custom == nil {
p.Custom = map[reflect.Type]any{}
}

for tpName, entry := range pars.Custom {
tp, ok := registry.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) ToJSON() ([]byte, error) {
par := customParamsJs{
Parameters: p.Parameters,
Custom: map[string]entry{},
}

for k, v := range p.Custom {
js, err := json.MarshalIndent(&v, "", " ")
if err != nil {
return []byte{}, err
}
par.Custom[k.String()] = entry{Bytes: js}
}

js, err := json.MarshalIndent(&par, "", " ")
if err != nil {
return []byte{}, err
}
return js, nil
}

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

for tp, res := range p.Custom {
id := ecs.ResourceTypeID(world, tp)
world.Resources().Add(id, util.CopyInterface[any](res))
}
}
49 changes: 49 additions & 0 deletions registry/registry.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package registry

import (
"fmt"
"reflect"
)

var observerRegistry = map[string]reflect.Type{}
var resourcesRegistry = map[string]reflect.Type{}
var systemsRegistry = map[string]reflect.Type{}

func RegisterObserver[T any]() {
tp := reflect.TypeOf((*T)(nil)).Elem()
if _, ok := observerRegistry[tp.String()]; ok {
panic(fmt.Sprintf("there is already an observer with type name '%s' registered", tp.String()))
}
observerRegistry[tp.String()] = tp
}

func RegisterResource[T any]() {
tp := reflect.TypeOf((*T)(nil)).Elem()
if _, ok := resourcesRegistry[tp.String()]; ok {
panic(fmt.Sprintf("there is already a resource with type name '%s' registered", tp.String()))
}
resourcesRegistry[tp.String()] = tp
}

func RegisterSystem[T any]() {
tp := reflect.TypeOf((*T)(nil)).Elem()
if _, ok := systemsRegistry[tp.String()]; ok {
panic(fmt.Sprintf("there is already a system with type name '%s' registered", tp.String()))
}
systemsRegistry[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
}

func GetSystem(name string) (reflect.Type, bool) {
t, ok := systemsRegistry[name]
return t, ok
}
14 changes: 14 additions & 0 deletions util/copy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package util

import "reflect"

// CopyInterface copies the underlying value of an interface pointer using reflection.
func CopyInterface[T any](value any) T {
v := reflect.Indirect(reflect.ValueOf(value))
b := v.Interface()

val := reflect.ValueOf(b)
ptr := reflect.New(val.Type()).Elem()
ptr.Set(val)
return ptr.Addr().Interface().(T)
}
Loading