This commit is contained in:
NatureFreshMilk 2019-02-22 13:57:33 +01:00
parent 534b09649d
commit dc7c1e58d1
3 changed files with 50 additions and 51 deletions

View File

@ -1,8 +1,8 @@
package areasparser
import (
"io/ioutil"
"mapserver/luaparser"
"io/ioutil"
"mapserver/luaparser"
)
type GenericPos struct {
@ -12,52 +12,52 @@ type GenericPos struct {
}
type Area struct {
Owner string `json:"owner"`
Name string `json:"name"`
Pos1 *GenericPos `json:"pos1"`
Pos2 *GenericPos `json:"pos2"`
Owner string `json:"owner"`
Name string `json:"name"`
Pos1 *GenericPos `json:"pos1"`
Pos2 *GenericPos `json:"pos2"`
}
func ParseFile(filename string) ([]*Area, error) {
content, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}
content, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}
return Parse(content)
return Parse(content)
}
func Parse(data []byte) ([]*Area, error) {
p := luaparser.New()
areas := make([]*Area, 0)
p := luaparser.New()
areas := make([]*Area, 0)
list, err := p.ParseList(string(data[:]))
list, err := p.ParseList(string(data[:]))
if err != nil {
return nil, err
}
if err != nil {
return nil, err
}
for _, entry := range list {
a := Area{}
a.Name = entry["name"].(string)
a.Owner = entry["owner"].(string)
for _, entry := range list {
a := Area{}
a.Name = entry["name"].(string)
a.Owner = entry["owner"].(string)
p1 := GenericPos{}
pos1 := entry["pos1"].(map[string]interface{})
p1.X = pos1["x"].(int)
p1.Y = pos1["y"].(int)
p1.Z = pos1["z"].(int)
a.Pos1 = &p1
p1 := GenericPos{}
pos1 := entry["pos1"].(map[string]interface{})
p1.X = pos1["x"].(int)
p1.Y = pos1["y"].(int)
p1.Z = pos1["z"].(int)
a.Pos1 = &p1
p2 := GenericPos{}
pos2 := entry["pos2"].(map[string]interface{})
p2.X = pos2["x"].(int)
p2.Y = pos2["y"].(int)
p2.Z = pos2["z"].(int)
a.Pos2 = &p2
p2 := GenericPos{}
pos2 := entry["pos2"].(map[string]interface{})
p2.X = pos2["x"].(int)
p2.Y = pos2["y"].(int)
p2.Z = pos2["z"].(int)
a.Pos2 = &p2
areas = append(areas, &a)
}
areas = append(areas, &a)
}
return areas, nil
return areas, nil
}

View File

@ -1,26 +1,25 @@
package areasparser
import (
"testing"
"fmt"
"encoding/json"
"encoding/json"
"fmt"
"testing"
)
func TestParse(t *testing.T){
func TestParse(t *testing.T) {
a, err := ParseFile("testdata/areas.dat")
a, err := ParseFile("testdata/areas.dat")
if err != nil {
t.Fatal(err)
}
if err != nil {
t.Fatal(err)
}
j, err := json.MarshalIndent(a, "", " ")
j, err := json.MarshalIndent(a, "", " ")
if err != nil {
t.Fatal(err)
}
if err != nil {
t.Fatal(err)
}
fmt.Println(string(j[:]))
fmt.Println(string(j[:]))
}

View File

@ -47,7 +47,7 @@ func parseMap(t *lua.LTable) map[string]interface{} {
return result
}
func (this *LuaParser) ParseList(expr string) ([]map[string]interface{}, error){
func (this *LuaParser) ParseList(expr string) ([]map[string]interface{}, error) {
result := make([]map[string]interface{}, 0)
err := this.state.DoString(expr)