fix flags loading
This commit is contained in:
@ -1,109 +1,116 @@
|
||||
package itemprops
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/rs/zerolog"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"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"`
|
||||
FractureToughness DimensionFractureToughness `json:"fracture_toughness"`
|
||||
MeltingPoint DimensionItemNullTemperature `json:"melting_point,omitempty"`
|
||||
BoilingPoint DimensionItemNullTemperature `json:"boiling_point,omitempty"`
|
||||
Id string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Flags MaterialFlags `json:"material_flags"`
|
||||
Density DimensionItemDensity `json:"density"`
|
||||
FractureToughness DimensionFractureToughness `json:"fracture_toughness"`
|
||||
MeltingPoint DimensionItemNullTemperature `json:"melting_point,omitempty"`
|
||||
BoilingPoint DimensionItemNullTemperature `json:"boiling_point,omitempty"`
|
||||
}
|
||||
|
||||
func (m Material) Unmarshal() {}
|
||||
|
||||
type MaterialFlags struct {
|
||||
ConductsElictricity bool `json:"conducts_elictricity"`
|
||||
BlocksLiquid bool `json:"blocks_liquid"`
|
||||
AcidResistant bool `json:"acid_resistant"`
|
||||
BlocksGas bool `json:"blocks_gas"`
|
||||
Flammable bool `json:"flammable"`
|
||||
ConductsHeat bool `json:"conducts_heat"`
|
||||
Radiates bool `json:"radiates"`
|
||||
ConductsElictricity bool `json:"conducts_elictricity"`
|
||||
BlocksLiquid bool `json:"blocks_liquid"`
|
||||
AcidResistant bool `json:"acid_resistant"`
|
||||
BlocksGas bool `json:"blocks_gas"`
|
||||
Flammable bool `json:"flammable"`
|
||||
ConductsHeat bool `json:"conducts_heat"`
|
||||
Radiates bool `json:"radiates"`
|
||||
}
|
||||
|
||||
type MaterialMap map[string]Material
|
||||
type MaterialMap map[string]*Material
|
||||
|
||||
type tt map[string]MaterialMap
|
||||
|
||||
func NewMaterialMap(path string, logger zerolog.Logger) (*MaterialMap, error) {
|
||||
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
|
||||
}
|
||||
mm := make(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 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
|
||||
})
|
||||
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))
|
||||
_ = 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 {
|
||||
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
|
||||
toReplace := item2.(map[string]interface{})["material_flags"]
|
||||
|
||||
if ref, ok := toReplace.(map[string]interface{})["$ref"]; ok {
|
||||
rfs := strings.Split(ref.(string), "/")
|
||||
referredFlag := rfs[len(rfs)-1]
|
||||
item2.(map[string]interface{})["material_flags"] = flags[referredFlag]
|
||||
}
|
||||
bts, err := json.Marshal(item2)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Could not marshal back:%w", err)
|
||||
}
|
||||
mm[clause] = &Material{}
|
||||
err = json.Unmarshal(bts, mm[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