types must have names

types
Jordan Orelli 8 years ago
parent 79200cae38
commit 4a0872f3ff

@ -1,6 +1,7 @@
package ent package ent
import ( import (
"fmt"
"strconv" "strconv"
"strings" "strings"
@ -48,6 +49,8 @@ type array_t struct {
count int count int
} }
func (t array_t) typeName() string { return fmt.Sprintf("array:%s", t.elem.typeName()) }
func (t array_t) read(r bit.Reader) (value, error) { func (t array_t) read(r bit.Reader) (value, error) {
var err error var err error
v := make(array, t.count) v := make(array, t.count)

@ -4,35 +4,61 @@ import (
"github.com/jordanorelli/hyperstone/bit" "github.com/jordanorelli/hyperstone/bit"
) )
var atom_types = map[string]typeFn{ var atom_types = []typeLiteral{
"bool": func(r bit.Reader) (value, error) { {
"bool",
func(r bit.Reader) (value, error) {
return bit.ReadBool(r), r.Err() return bit.ReadBool(r), r.Err()
}, },
"uint8": func(r bit.Reader) (value, error) { },
{
"uint8",
func(r bit.Reader) (value, error) {
// TODO: bounds check here // TODO: bounds check here
return uint8(bit.ReadVarInt(r)), r.Err() return uint8(bit.ReadVarInt(r)), r.Err()
}, },
"uint16": func(r bit.Reader) (value, error) { },
{
"uint16",
func(r bit.Reader) (value, error) {
// TODO: bounds check here // TODO: bounds check here
return uint16(bit.ReadVarInt(r)), r.Err() return uint16(bit.ReadVarInt(r)), r.Err()
}, },
"uint32": func(r bit.Reader) (value, error) { },
{
"uint32",
func(r bit.Reader) (value, error) {
return bit.ReadVarInt32(r), r.Err() return bit.ReadVarInt32(r), r.Err()
}, },
"uint64": func(r bit.Reader) (value, error) { },
{
"uint64",
func(r bit.Reader) (value, error) {
return bit.ReadVarInt(r), r.Err() return bit.ReadVarInt(r), r.Err()
}, },
"int8": func(r bit.Reader) (value, error) { },
{
"int8",
func(r bit.Reader) (value, error) {
// TODO: bounds check here // TODO: bounds check here
return int8(bit.ReadZigZag32(r)), r.Err() return int8(bit.ReadZigZag32(r)), r.Err()
}, },
"int32": func(r bit.Reader) (value, error) { },
{
"int32",
func(r bit.Reader) (value, error) {
return bit.ReadZigZag32(r), r.Err() return bit.ReadZigZag32(r), r.Err()
}, },
"CUtlStringToken": func(r bit.Reader) (value, error) { },
{
"CUtlStringToken",
func(r bit.Reader) (value, error) {
return bit.ReadVarInt(r), r.Err() return bit.ReadVarInt(r), r.Err()
}, },
"Color": func(r bit.Reader) (value, error) { },
{
"Color",
func(r bit.Reader) (value, error) {
u := bit.ReadVarInt(r) u := bit.ReadVarInt(r)
return color{ return color{
r: uint8(u >> 6 & 0xff), r: uint8(u >> 6 & 0xff),
@ -41,13 +67,16 @@ var atom_types = map[string]typeFn{
a: uint8(u >> 0 & 0xff), a: uint8(u >> 0 & 0xff),
}, r.Err() }, r.Err()
}, },
},
} }
func atomType(spec *typeSpec, env *Env) tÿpe { func atomType(spec *typeSpec, env *Env) tÿpe {
if t, ok := atom_types[spec.typeName]; ok { for _, t := range atom_types {
Debug.Printf(" atom type") if t.typeName() == spec.typeName {
Debug.Printf(" atom type: %s", t.typeName())
return t return t
} }
}
return nil return nil
} }

@ -15,6 +15,10 @@ func (c class) String() string {
return fmt.Sprintf("<%s.%d>", c.name, c.version) return fmt.Sprintf("<%s.%d>", c.name, c.version)
} }
func (c class) typeName() string {
return fmt.Sprintf("class:%s", c.name)
}
func (c *class) read(r bit.Reader) (value, error) { func (c *class) read(r bit.Reader) (value, error) {
bit.ReadBool(r) // ??? bit.ReadBool(r) // ???
return c.nü(), nil return c.nü(), nil

@ -23,9 +23,12 @@ func floatType(spec *typeSpec, env *Env) tÿpe {
} }
if spec.encoder == "coord" { if spec.encoder == "coord" {
Debug.Printf(" coord float type") Debug.Printf(" coord float type")
return typeFn(func(r bit.Reader) (value, error) { return typeLiteral{
"float:coord",
func(r bit.Reader) (value, error) {
return bit.ReadCoord(r), r.Err() return bit.ReadCoord(r), r.Err()
}) },
}
} }
if spec.serializer == "simulationtime" { if spec.serializer == "simulationtime" {
return nil return nil
@ -33,10 +36,13 @@ func floatType(spec *typeSpec, env *Env) tÿpe {
switch spec.bits { switch spec.bits {
case 0, 32: case 0, 32:
Debug.Printf(" std float type") Debug.Printf(" std float type")
return typeFn(func(r bit.Reader) (value, error) { return typeLiteral{
"float:std",
func(r bit.Reader) (value, error) {
// TODO: check uint32 overflow here? // TODO: check uint32 overflow here?
return math.Float32frombits(uint32(r.ReadBits(32))), r.Err() return math.Float32frombits(uint32(r.ReadBits(32))), r.Err()
}) },
}
default: default:
return qFloatType(spec, env) return qFloatType(spec, env)
} }
@ -81,6 +87,8 @@ type qfloat_t struct {
special *float32 special *float32
} }
func (t qfloat_t) typeName() string { return "qfloat" }
func (t qfloat_t) read(r bit.Reader) (value, error) { func (t qfloat_t) read(r bit.Reader) (value, error) {
if t.special != nil && bit.ReadBool(r) { if t.special != nil && bit.ReadBool(r) {
return *t.special, nil return *t.special, nil

@ -24,9 +24,12 @@ func genericType(spec *typeSpec, env *Env) tÿpe {
switch genericName { switch genericName {
case "CHandle", "CStrongHandle": case "CHandle", "CStrongHandle":
return typeFn(func(r bit.Reader) (value, error) { return typeLiteral{
fmt.Sprintf("handle:%s", genericName),
func(r bit.Reader) (value, error) {
return handle(bit.ReadVarInt(r)), r.Err() return handle(bit.ReadVarInt(r)), r.Err()
}) },
}
case "CUtlVector": case "CUtlVector":
return cutl_vector_t{elem} return cutl_vector_t{elem}
default: default:
@ -72,6 +75,10 @@ type cutl_vector_t struct {
elem tÿpe elem tÿpe
} }
func (t cutl_vector_t) typeName() string {
return fmt.Sprintf("vector:%s", t.elem.typeName())
}
func (t cutl_vector_t) read(r bit.Reader) (value, error) { func (t cutl_vector_t) read(r bit.Reader) (value, error) {
count := bit.ReadVarInt32(r) count := bit.ReadVarInt32(r)
return make(array, count), r.Err() return make(array, count), r.Err()

@ -14,7 +14,10 @@ func handleType(spec *typeSpec, env *Env) tÿpe {
} }
Debug.Printf(" handle type") Debug.Printf(" handle type")
return typeFn(func(r bit.Reader) (value, error) { return typeLiteral{
"handle:CGameSceneNodeHandle",
func(r bit.Reader) (value, error) {
return handle(bit.ReadVarInt(r)), r.Err() return handle(bit.ReadVarInt(r)), r.Err()
}) },
}
} }

@ -10,7 +10,10 @@ func hSeqType(spec *typeSpec, env *Env) tÿpe {
} }
Debug.Printf(" hsequence type") Debug.Printf(" hsequence type")
return typeFn(func(r bit.Reader) (value, error) { return typeLiteral{
"HSequence",
func(r bit.Reader) (value, error) {
return bit.ReadVarInt(r) - 1, r.Err() return bit.ReadVarInt(r) - 1, r.Err()
}) },
}
} }

@ -16,7 +16,7 @@ func qAngleType(spec *typeSpec, env *Env) tÿpe {
case spec.bits <= 0 || spec.bits > 32: case spec.bits <= 0 || spec.bits > 32:
return typeError("qangle pitch_yaw has invalid bit size: %d", spec.bits) return typeError("qangle pitch_yaw has invalid bit size: %d", spec.bits)
case spec.bits == 32: case spec.bits == 32:
return typeFn(pitchYaw_t) return typeLiteral{"qangle:pitchYaw", pitchYaw_t}
default: default:
return pitchYawAngles_t(spec.bits) return pitchYawAngles_t(spec.bits)
} }
@ -24,7 +24,9 @@ func qAngleType(spec *typeSpec, env *Env) tÿpe {
switch spec.bits { switch spec.bits {
case 0: case 0:
Debug.Printf(" qangle type") Debug.Printf(" qangle type")
return typeFn(func(r bit.Reader) (value, error) { return typeLiteral{
"qangle",
func(r bit.Reader) (value, error) {
var q qangle var q qangle
pitch, yaw, roll := bit.ReadBool(r), bit.ReadBool(r), bit.ReadBool(r) pitch, yaw, roll := bit.ReadBool(r), bit.ReadBool(r), bit.ReadBool(r)
if pitch { if pitch {
@ -37,7 +39,8 @@ func qAngleType(spec *typeSpec, env *Env) tÿpe {
q.roll = bit.ReadCoord(r) q.roll = bit.ReadCoord(r)
} }
return q, nil return q, nil
}) },
}
case 32: case 32:
return nil return nil
default: default:
@ -54,6 +57,8 @@ func pitchYaw_t(r bit.Reader) (value, error) {
type pitchYawAngles_t uint type pitchYawAngles_t uint
func (t pitchYawAngles_t) typeName() string { return "qangle:pitchYawAngles" }
func (t pitchYawAngles_t) read(r bit.Reader) (value, error) { func (t pitchYawAngles_t) read(r bit.Reader) (value, error) {
var q qangle var q qangle
q.pitch = bit.ReadAngle(r, uint(t)) q.pitch = bit.ReadAngle(r, uint(t))

@ -9,11 +9,16 @@ import (
type tÿpe interface { type tÿpe interface {
read(bit.Reader) (value, error) read(bit.Reader) (value, error)
typeName() string
} }
type typeFn func(bit.Reader) (value, error) type typeLiteral struct {
name string
readFn func(r bit.Reader) (value, error)
}
func (fn typeFn) read(r bit.Reader) (value, error) { return fn(r) } func (t typeLiteral) typeName() string { return t.name }
func (t typeLiteral) read(r bit.Reader) (value, error) { return t.readFn(r) }
type typeParseFn func(*typeSpec, *Env) tÿpe type typeParseFn func(*typeSpec, *Env) tÿpe
@ -39,9 +44,12 @@ func parseTypeSpec(spec *typeSpec, env *Env) tÿpe {
func unknownType(spec *typeSpec, env *Env) tÿpe { func unknownType(spec *typeSpec, env *Env) tÿpe {
Debug.Printf("Unknown Type: %v", spec) Debug.Printf("Unknown Type: %v", spec)
return typeFn(func(r bit.Reader) (value, error) { return typeLiteral{
name: fmt.Sprintf("unknown:%s", spec.typeName),
readFn: func(r bit.Reader) (value, error) {
return bit.ReadVarInt(r), r.Err() return bit.ReadVarInt(r), r.Err()
}) },
}
} }
// a type error is both an error and a type. It represents a type that we were // a type error is both an error and a type. It represents a type that we were
@ -54,6 +62,7 @@ func typeError(t string, args ...interface{}) tÿpe {
type error_t string type error_t string
func (e error_t) typeName() string { return "error" }
func (e error_t) Error() string { return string(e) } func (e error_t) Error() string { return string(e) }
func (e error_t) read(r bit.Reader) (value, error) { func (e error_t) read(r bit.Reader) (value, error) {
return nil, fmt.Errorf("type error: %s", string(e)) return nil, fmt.Errorf("type error: %s", string(e))

@ -1,6 +1,7 @@
package ent package ent
import ( import (
"fmt"
"github.com/jordanorelli/hyperstone/bit" "github.com/jordanorelli/hyperstone/bit"
) )
@ -24,6 +25,10 @@ type vector_t struct {
elem tÿpe elem tÿpe
} }
func (t vector_t) typeName() string {
return fmt.Sprintf("vector:%s", t.elem.typeName())
}
func (t vector_t) read(r bit.Reader) (value, error) { func (t vector_t) read(r bit.Reader) (value, error) {
var err error var err error
var v interface{} var v interface{}

Loading…
Cancel
Save