tests, load materials start
This commit is contained in:
@ -1,6 +1,17 @@
|
||||
package itemprops
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/rs/zerolog"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type Material struct {
|
||||
Id string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Flags MaterialFlags `json:"material_flags"`
|
||||
Density DimensionItemDensity `json:"density"`
|
||||
@ -9,6 +20,8 @@ type Material struct {
|
||||
BoilingPoint DimensionItemNullTemperature `json:"boiling_point,omitempty"`
|
||||
}
|
||||
|
||||
func (m Material) Unmarshal() {}
|
||||
|
||||
type MaterialFlags struct {
|
||||
ConductsElictricity bool `json:"conducts_elictricity"`
|
||||
BlocksLiquid bool `json:"blocks_liquid"`
|
||||
@ -18,3 +31,79 @@ type MaterialFlags struct {
|
||||
ConductsHeat bool `json:"conducts_heat"`
|
||||
Radiates bool `json:"radiates"`
|
||||
}
|
||||
|
||||
type MaterialMap map[string]Material
|
||||
|
||||
type tt map[string]MaterialMap
|
||||
|
||||
func NewMaterialMap(path string, logger zerolog.Logger) (*MaterialMap, error) {
|
||||
|
||||
mm := &MaterialMap{}
|
||||
tmp := make(map[string][]interface{})
|
||||
flags := make(map[string]*MaterialFlags)
|
||||
err := filepath.Walk(path+"/assets/materials",
|
||||
func(path string, info os.FileInfo, err error) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if strings.HasSuffix(path, ".json") {
|
||||
splt := strings.Split(path, "/")
|
||||
logger.Info().Msgf("loading %s %d", splt[len(splt)-1], info.Size())
|
||||
bytes, err := ioutil.ReadFile(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
ttmp := make(map[string][]interface{})
|
||||
err = json.Unmarshal(bytes, &ttmp)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for idx, _ := range ttmp {
|
||||
tmp[idx] = append(tmp[idx], ttmp[idx]...)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
})
|
||||
|
||||
_ = flags
|
||||
if lst, ok := tmp["material_flags"]; ok {
|
||||
for _, item := range lst {
|
||||
ttt := item.(map[string]interface{})
|
||||
_ = ttt
|
||||
for clause, item2 := range ttt {
|
||||
bts, err := json.Marshal(item2)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Could not marshal back:%w", err)
|
||||
}
|
||||
flags[clause] = &MaterialFlags{}
|
||||
err = json.Unmarshal(bts, flags[clause])
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Could not unmarshal to material_flags:%w", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
logger.Info().Msgf("loaded %d material flag sets", len(flags))
|
||||
|
||||
if lst, ok := tmp["materials"]; ok {
|
||||
for _, item := range lst {
|
||||
ttt := item.(map[string]interface{})
|
||||
_ = ttt
|
||||
for clause, item2 := range ttt {
|
||||
|
||||
bts, err := json.Marshal(item2)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Could not marshal back:%w", err)
|
||||
}
|
||||
flags[clause] = &MaterialFlags{}
|
||||
err = json.Unmarshal(bts, flags[clause])
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Could not unmarshal to material_flags:%w", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return mm, err
|
||||
}
|
||||
|
Reference in New Issue
Block a user