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,49 +4,78 @@ 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) { {
return bit.ReadBool(r), r.Err() "bool",
func(r bit.Reader) (value, error) {
return bit.ReadBool(r), r.Err()
},
}, },
"uint8": func(r bit.Reader) (value, error) { {
// TODO: bounds check here "uint8",
return uint8(bit.ReadVarInt(r)), r.Err() func(r bit.Reader) (value, error) {
// TODO: bounds check here
return uint8(bit.ReadVarInt(r)), r.Err()
},
}, },
"uint16": func(r bit.Reader) (value, error) { {
// TODO: bounds check here "uint16",
return uint16(bit.ReadVarInt(r)), r.Err() func(r bit.Reader) (value, error) {
// TODO: bounds check here
return uint16(bit.ReadVarInt(r)), r.Err()
},
}, },
"uint32": func(r bit.Reader) (value, error) { {
return bit.ReadVarInt32(r), r.Err() "uint32",
func(r bit.Reader) (value, error) {
return bit.ReadVarInt32(r), r.Err()
},
}, },
"uint64": func(r bit.Reader) (value, error) { {
return bit.ReadVarInt(r), r.Err() "uint64",
func(r bit.Reader) (value, error) {
return bit.ReadVarInt(r), r.Err()
},
}, },
"int8": func(r bit.Reader) (value, error) { {
// TODO: bounds check here "int8",
return int8(bit.ReadZigZag32(r)), r.Err() func(r bit.Reader) (value, error) {
// TODO: bounds check here
return int8(bit.ReadZigZag32(r)), r.Err()
},
}, },
"int32": func(r bit.Reader) (value, error) { {
return bit.ReadZigZag32(r), r.Err() "int32",
func(r bit.Reader) (value, error) {
return bit.ReadZigZag32(r), r.Err()
},
}, },
"CUtlStringToken": func(r bit.Reader) (value, error) { {
return bit.ReadVarInt(r), r.Err() "CUtlStringToken",
func(r bit.Reader) (value, error) {
return bit.ReadVarInt(r), r.Err()
},
}, },
"Color": func(r bit.Reader) (value, error) { {
u := bit.ReadVarInt(r) "Color",
return color{ func(r bit.Reader) (value, error) {
r: uint8(u >> 6 & 0xff), u := bit.ReadVarInt(r)
g: uint8(u >> 4 & 0xff), return color{
b: uint8(u >> 2 & 0xff), r: uint8(u >> 6 & 0xff),
a: uint8(u >> 0 & 0xff), g: uint8(u >> 4 & 0xff),
}, r.Err() b: uint8(u >> 2 & 0xff),
a: uint8(u >> 0 & 0xff),
}, 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 {
return t Debug.Printf(" atom type: %s", t.typeName())
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{
return bit.ReadCoord(r), r.Err() "float:coord",
}) func(r bit.Reader) (value, error) {
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{
// TODO: check uint32 overflow here? "float:std",
return math.Float32frombits(uint32(r.ReadBits(32))), r.Err() func(r bit.Reader) (value, error) {
}) // TODO: check uint32 overflow here?
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{
return handle(bit.ReadVarInt(r)), r.Err() fmt.Sprintf("handle:%s", genericName),
}) func(r bit.Reader) (value, error) {
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{
return handle(bit.ReadVarInt(r)), r.Err() "handle:CGameSceneNodeHandle",
}) func(r bit.Reader) (value, error) {
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{
return bit.ReadVarInt(r) - 1, r.Err() "HSequence",
}) func(r bit.Reader) (value, error) {
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,20 +24,23 @@ 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{
var q qangle "qangle",
pitch, yaw, roll := bit.ReadBool(r), bit.ReadBool(r), bit.ReadBool(r) func(r bit.Reader) (value, error) {
if pitch { var q qangle
q.pitch = bit.ReadCoord(r) pitch, yaw, roll := bit.ReadBool(r), bit.ReadBool(r), bit.ReadBool(r)
} if pitch {
if yaw { q.pitch = bit.ReadCoord(r)
q.yaw = bit.ReadCoord(r) }
} if yaw {
if roll { q.yaw = bit.ReadCoord(r)
q.roll = bit.ReadCoord(r) }
} if roll {
return q, nil q.roll = bit.ReadCoord(r)
}) }
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{
return bit.ReadVarInt(r), r.Err() name: fmt.Sprintf("unknown:%s", spec.typeName),
}) readFn: func(r bit.Reader) (value, error) {
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,7 +62,8 @@ func typeError(t string, args ...interface{}) tÿpe {
type error_t string type error_t string
func (e error_t) Error() string { return string(e) } func (e error_t) typeName() string { return "error" }
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