1
0

fix color and equipmentitem

This commit is contained in:
Sky Johnson 2025-09-05 13:50:10 -05:00
parent 678d653932
commit ca43e4637d
7 changed files with 1810 additions and 1066 deletions

View File

@ -0,0 +1,10 @@
{
"permissions": {
"allow": [
"Bash(go run:*)",
"Bash(grep:*)"
],
"deny": [],
"ask": []
}
}

View File

@ -443,8 +443,14 @@ func (p *{{.Name}}) Serialize(dest []byte) uint32 {
{{- else if eq (baseType .Type) "types.EquipmentItem"}}
binary.LittleEndian.PutUint16(dest[offset:], p.{{.GoName}}[i].Type)
offset += 2
binary.LittleEndian.PutUint32(dest[offset:], p.{{.GoName}}[i].Color.ToUint32())
offset += 4
dest[offset] = p.{{.GoName}}[i].Color.R
dest[offset+1] = p.{{.GoName}}[i].Color.G
dest[offset+2] = p.{{.GoName}}[i].Color.B
offset += 3
dest[offset] = p.{{.GoName}}[i].Highlight.R
dest[offset+1] = p.{{.GoName}}[i].Highlight.G
dest[offset+2] = p.{{.GoName}}[i].Highlight.B
offset += 3
{{- end}}
}
{{- else}}
@ -462,8 +468,10 @@ func (p *{{.Name}}) Serialize(dest []byte) uint32 {
binary.LittleEndian.PutUint16(dest[offset:], uint16(p.{{.GoName}}))
offset += 2
{{- else if eq .Type "types.Color"}}
binary.LittleEndian.PutUint32(dest[offset:], p.{{.GoName}}.ToUint32())
offset += 4
dest[offset] = p.{{.GoName}}.R
dest[offset+1] = p.{{.GoName}}.G
dest[offset+2] = p.{{.GoName}}.B
offset += 3
{{- else if or (eq .Type "int32") (eq .Type "uint32")}}
binary.LittleEndian.PutUint32(dest[offset:], uint32(p.{{.GoName}}))
offset += 4
@ -495,7 +503,7 @@ func (p *{{.Name}}) Size() uint32 {
{{- end}}
{{- else if .IsArray}}
{{- if eq (baseType .Type) "types.EquipmentItem"}}
size += {{.Size}} * 6
size += {{.Size}} * 8
{{- else if eq (sizeOf (baseType .Type)) 1}}
size += {{.Size}}
{{- else}}
@ -543,8 +551,10 @@ func (p *{{.Name}}) Size() uint32 {
binary.LittleEndian.PutUint32(dest[offset:], math.Float32bits(elem.{{.GoName}}))
offset += 4
{{- else if eq .Type "types.Color"}}
binary.LittleEndian.PutUint32(dest[offset:], elem.{{.GoName}}.ToUint32())
offset += 4
dest[offset] = elem.{{.GoName}}.R
dest[offset+1] = elem.{{.GoName}}.G
dest[offset+2] = elem.{{.GoName}}.B
offset += 3
{{- else if eq .Type "uint32"}}
binary.LittleEndian.PutUint32(dest[offset:], elem.{{.GoName}})
offset += 4
@ -627,8 +637,10 @@ func (p *{{.Name}}) Size() uint32 {
binary.LittleEndian.PutUint32(dest[offset:], math.Float32bits(nestedElem.{{.GoName}}))
offset += 4
{{- else if eq .Type "types.Color"}}
binary.LittleEndian.PutUint32(dest[offset:], nestedElem.{{.GoName}}.ToUint32())
offset += 4
dest[offset] = nestedElem.{{.GoName}}.R
dest[offset+1] = nestedElem.{{.GoName}}.G
dest[offset+2] = nestedElem.{{.GoName}}.B
offset += 3
{{- else if eq .Type "uint32"}}
binary.LittleEndian.PutUint32(dest[offset:], nestedElem.{{.GoName}})
offset += 4
@ -700,12 +712,14 @@ func sizeOf(typeName string) int {
return 1
case "int16", "uint16":
return 2
case "int32", "uint32", "float32", "types.Color":
case "types.Color":
return 3 // RGB: 3 bytes
case "int32", "uint32", "float32":
return 4
case "int64", "uint64", "float64":
return 8
case "types.EquipmentItem":
return 6
return 8 // 2 bytes type + 3 bytes color + 3 bytes highlight
default:
return 0
}

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

BIN
defs/generated/test.exe Normal file

Binary file not shown.

View File

@ -5,58 +5,65 @@ import (
"io"
)
// Color represents an RGBA color value
// Color represents an RGB color value (3 bytes) matching EQ2's format
type Color struct {
R uint8
G uint8
B uint8
A uint8
}
// NewColor creates a new Color from RGBA values
func NewColor(r, g, b, a uint8) Color {
return Color{R: r, G: g, B: b, A: a}
// NewColor creates a new Color from RGB values
func NewColor(r, g, b uint8) Color {
return Color{R: r, G: g, B: b}
}
// NewColorFromUint32 creates a Color from a packed uint32 (0xAARRGGBB)
// NewColorFromUint32 creates a Color from a packed uint32 (ignores alpha, uses 0x00RRGGBB)
func NewColorFromUint32(packed uint32) Color {
return Color{
R: uint8((packed >> 16) & 0xFF),
G: uint8((packed >> 8) & 0xFF),
B: uint8(packed & 0xFF),
A: uint8((packed >> 24) & 0xFF),
}
}
// ToUint32 converts the color to a packed uint32 (0xAARRGGBB)
// ToUint32 converts the color to a packed uint32 (0x00RRGGBB for compatibility)
func (c Color) ToUint32() uint32 {
return uint32(c.A)<<24 | uint32(c.R)<<16 | uint32(c.G)<<8 | uint32(c.B)
return uint32(c.R)<<16 | uint32(c.G)<<8 | uint32(c.B)
}
// Serialize writes the color as a uint32 to a writer
// Serialize writes the color as 3 bytes (RGB) to a writer
func (c Color) Serialize(w io.Writer) error {
return binary.Write(w, binary.LittleEndian, c.ToUint32())
}
// SerializeToBytes writes the color to a byte slice at the given offset
func (c Color) SerializeToBytes(dest []byte, offset *uint32) {
binary.LittleEndian.PutUint32(dest[*offset:], c.ToUint32())
*offset += 4
}
// Size returns the serialized size of the color (always 4 bytes)
func (c Color) Size() uint32 {
return 4
}
// Deserialize reads a color from a reader
func (c *Color) Deserialize(r io.Reader) error {
var packed uint32
if err := binary.Read(r, binary.LittleEndian, &packed); err != nil {
if err := binary.Write(w, binary.LittleEndian, c.R); err != nil {
return err
}
*c = NewColorFromUint32(packed)
return nil
if err := binary.Write(w, binary.LittleEndian, c.G); err != nil {
return err
}
return binary.Write(w, binary.LittleEndian, c.B)
}
// SerializeToBytes writes the color as 3 bytes to a byte slice at the given offset
func (c Color) SerializeToBytes(dest []byte, offset *uint32) {
dest[*offset] = c.R
dest[*offset+1] = c.G
dest[*offset+2] = c.B
*offset += 3
}
// Size returns the serialized size of the color (always 3 bytes)
func (c Color) Size() uint32 {
return 3
}
// Deserialize reads a color (3 bytes) from a reader
func (c *Color) Deserialize(r io.Reader) error {
if err := binary.Read(r, binary.LittleEndian, &c.R); err != nil {
return err
}
if err := binary.Read(r, binary.LittleEndian, &c.G); err != nil {
return err
}
return binary.Read(r, binary.LittleEndian, &c.B)
}
// EQ2Color is an alias for Color specifically for EQ2 packets

View File

@ -6,9 +6,11 @@ import (
)
// EquipmentItem represents an equipment item with model and color information
// Matches EQ2_EquipmentItem from C++ code
type EquipmentItem struct {
Type uint16 // Model/item type ID
Color Color // RGB color for the item
Type uint16 // Model/item type ID
Color Color // RGB color for the item (3 bytes)
Highlight Color // RGB highlight color for the item (3 bytes)
}
// Serialize writes the equipment item to a writer
@ -16,7 +18,10 @@ func (e *EquipmentItem) Serialize(w io.Writer) error {
if err := binary.Write(w, binary.LittleEndian, e.Type); err != nil {
return err
}
return e.Color.Serialize(w)
if err := e.Color.Serialize(w); err != nil {
return err
}
return e.Highlight.Serialize(w)
}
// SerializeToBytes writes the equipment item to a byte slice at the given offset
@ -24,11 +29,12 @@ func (e *EquipmentItem) SerializeToBytes(dest []byte, offset *uint32) {
binary.LittleEndian.PutUint16(dest[*offset:], e.Type)
*offset += 2
e.Color.SerializeToBytes(dest, offset)
e.Highlight.SerializeToBytes(dest, offset)
}
// Size returns the serialized size of the equipment item
func (e *EquipmentItem) Size() uint32 {
return 2 + e.Color.Size() // 2 bytes for type + color size
return 2 + e.Color.Size() + e.Highlight.Size() // 2 bytes for type + 3 bytes color + 3 bytes highlight = 8 bytes total
}
// Deserialize reads an equipment item from a reader
@ -36,5 +42,8 @@ func (e *EquipmentItem) Deserialize(r io.Reader) error {
if err := binary.Read(r, binary.LittleEndian, &e.Type); err != nil {
return err
}
return e.Color.Deserialize(r)
if err := e.Color.Deserialize(r); err != nil {
return err
}
return e.Highlight.Deserialize(r)
}