fix flags loading

This commit is contained in:
thefish 2022-10-18 06:37:11 +03:00
parent def43265de
commit f52e235799
2 changed files with 251 additions and 243 deletions

View File

@ -195,7 +195,7 @@ func (suite *IpTestSuite) TestMaterialDeserialization() {
} }
teststeel := Material{ teststeel := Material{
Name: "teststeel", Name: "steel",
Flags: metalMaterialFlags, Flags: metalMaterialFlags,
Density: DimensionItemDensity{decimal.NewFromInt(7800)}, Density: DimensionItemDensity{decimal.NewFromInt(7800)},
FractureToughness: DimensionFractureToughness{decimal.NewFromInt(30)}, FractureToughness: DimensionFractureToughness{decimal.NewFromInt(30)},
@ -203,6 +203,7 @@ func (suite *IpTestSuite) TestMaterialDeserialization() {
BoilingPoint: DimensionItemNullTemperature{decimal.NullDecimal{decimal.NewFromInt(3200), true}}, BoilingPoint: DimensionItemNullTemperature{decimal.NullDecimal{decimal.NewFromInt(3200), true}},
} }
_ = teststeel loadedsteel := mm["steel"]
suite.Equalf(teststeel, *loadedsteel, "error: %s")
_ = mm _ = mm
} }

View File

@ -32,13 +32,13 @@ type MaterialFlags struct {
Radiates bool `json:"radiates"` Radiates bool `json:"radiates"`
} }
type MaterialMap map[string]Material type MaterialMap map[string]*Material
type tt map[string]MaterialMap 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{} mm := make(MaterialMap)
tmp := make(map[string][]interface{}) tmp := make(map[string][]interface{})
flags := make(map[string]*MaterialFlags) flags := make(map[string]*MaterialFlags)
err := filepath.Walk(path+"/assets/materials", err := filepath.Walk(path+"/assets/materials",
@ -93,12 +93,19 @@ func NewMaterialMap(path string, logger zerolog.Logger) (*MaterialMap, error) {
_ = ttt _ = ttt
for clause, item2 := range ttt { for clause, item2 := range ttt {
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) bts, err := json.Marshal(item2)
if err != nil { if err != nil {
return nil, fmt.Errorf("Could not marshal back:%w", err) return nil, fmt.Errorf("Could not marshal back:%w", err)
} }
flags[clause] = &MaterialFlags{} mm[clause] = &Material{}
err = json.Unmarshal(bts, flags[clause]) err = json.Unmarshal(bts, mm[clause])
if err != nil { if err != nil {
return nil, fmt.Errorf("Could not unmarshal to material_flags:%w", err) return nil, fmt.Errorf("Could not unmarshal to material_flags:%w", err)
} }