shitty bitfield packing code
This commit is contained in:
@@ -253,3 +253,31 @@ type PacketInfo struct {
|
||||
PacketId int32
|
||||
ProtocolVersion int32
|
||||
}
|
||||
|
||||
func Bool2uint8(b bool) uint8 {
|
||||
if b {
|
||||
return 1
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func Bool2uint16(b bool) uint16 {
|
||||
if b {
|
||||
return 1
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func Bool2uint32(b bool) uint32 {
|
||||
if b {
|
||||
return 1
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func Bool2uint64(b bool) uint64 {
|
||||
if b {
|
||||
return 1
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
File diff suppressed because one or more lines are too long
+9309
-9286
File diff suppressed because it is too large
Load Diff
+17010
-16986
File diff suppressed because it is too large
Load Diff
+15493
-15470
File diff suppressed because it is too large
Load Diff
@@ -270,7 +270,8 @@ func (g *Generator) GenerateProtocol(protocol Protocol, version string) (err err
|
||||
v := protocol.Types.Types[k]
|
||||
if strings.Contains(k, "packet") {
|
||||
delete(protocol.Types.Types, k)
|
||||
for prefix, types := range prefixTypeMap {
|
||||
for _, prefix := range OrderedKeys(prefixTypeMap) {
|
||||
types := prefixTypeMap[prefix]
|
||||
if prefix == "" {
|
||||
continue
|
||||
}
|
||||
|
||||
@@ -3,6 +3,8 @@ package main
|
||||
import (
|
||||
"go/ast"
|
||||
"go/token"
|
||||
"slices"
|
||||
"strconv"
|
||||
|
||||
"github.com/admin-else/strom/proto_generator/protodef"
|
||||
"github.com/go-viper/mapstructure/v2"
|
||||
@@ -156,11 +158,47 @@ func BitFieldEncoder(g *Generator, varToSet ast.Expr, dataRaw any, name string)
|
||||
for _, field := range data {
|
||||
totalSize += field.Size
|
||||
}
|
||||
p, err := BitSizeToUnsignedProtodefName(totalSize)
|
||||
packedVarName := name + "Packed"
|
||||
packedExpr := Ident(packedVarName)
|
||||
packedTypeStr := "uint" + strconv.Itoa(totalSize)
|
||||
|
||||
s = append(s, VarStmt(packedVarName, Ident(packedTypeStr)))
|
||||
|
||||
slices.Reverse(data)
|
||||
shiftBy := 0
|
||||
for _, field := range data {
|
||||
boolFix := packedTypeStr
|
||||
if field.Size == 1 {
|
||||
boolFix = "proto_base.Bool2" + boolFix
|
||||
}
|
||||
|
||||
s = append(s, OrAssign( // lisp ahh
|
||||
packedExpr,
|
||||
LeftShift(
|
||||
Grouping(
|
||||
BinAnd(
|
||||
Call(
|
||||
Ident(boolFix),
|
||||
SelectorExprAndStr(
|
||||
varToSet,
|
||||
CamelCase(field.Name),
|
||||
),
|
||||
),
|
||||
NumLitHex((1<<field.Size)-1),
|
||||
),
|
||||
),
|
||||
NumLitHex(shiftBy),
|
||||
),
|
||||
))
|
||||
shiftBy += field.Size
|
||||
}
|
||||
|
||||
encodePacked, err := g.VisitEncoder(packedExpr, "u"+strconv.Itoa(totalSize), name)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
return g.VisitEncoder(varToSet, p, name)
|
||||
s = append(s, encodePacked...)
|
||||
return
|
||||
}
|
||||
|
||||
func SwitchEncoder(g *Generator, varToSet ast.Expr, dataRaw any, name string) (s []ast.Stmt, err error) {
|
||||
|
||||
@@ -60,13 +60,16 @@ func VisitBufferType(_ *Generator, dataRaw any) (e ast.Expr, err error) {
|
||||
return
|
||||
}
|
||||
|
||||
func VisitArrayTypeVisitorType(g *Generator, dataRaw any) (ast.Expr, error) {
|
||||
func VisitArrayTypeVisitorType(g *Generator, dataRaw any) (e ast.Expr, err error) {
|
||||
var data struct {
|
||||
CountType any
|
||||
Type any
|
||||
}
|
||||
must(mapstructure.Decode(dataRaw, &data))
|
||||
e, err := g.VisitType(data.Type)
|
||||
err = mapstructure.Decode(dataRaw, &data)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
e, err = g.VisitType(data.Type)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -95,7 +95,7 @@ func NumLit(n int) *ast.BasicLit {
|
||||
}
|
||||
|
||||
func NumLitHex(n int) *ast.BasicLit {
|
||||
return NumLitStr(fmt.Sprintf("%X", n))
|
||||
return NumLitStr(fmt.Sprintf("0x%X", n))
|
||||
}
|
||||
|
||||
func NumLitStr(n string) *ast.BasicLit {
|
||||
@@ -194,6 +194,14 @@ func SubAssign(lhs, rhs ast.Expr) (s *ast.AssignStmt) {
|
||||
}
|
||||
}
|
||||
|
||||
func OrAssign(lhs, rhs ast.Expr) (s *ast.AssignStmt) {
|
||||
return &ast.AssignStmt{
|
||||
Lhs: []ast.Expr{lhs},
|
||||
Tok: token.OR_ASSIGN,
|
||||
Rhs: []ast.Expr{rhs},
|
||||
}
|
||||
}
|
||||
|
||||
func Exprs(exprs ...ast.Expr) []ast.Expr {
|
||||
return exprs
|
||||
}
|
||||
@@ -511,3 +519,9 @@ func LeftShift(lhs, rhs ast.Expr) *ast.BinaryExpr {
|
||||
Y: rhs,
|
||||
}
|
||||
}
|
||||
|
||||
func Grouping(e ast.Expr) *ast.ParenExpr {
|
||||
return &ast.ParenExpr{
|
||||
X: e,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -49,12 +49,6 @@ func ReverseMap[M map[K]V, K cmp.Ordered, V cmp.Ordered](m M) map[V]K {
|
||||
return ret
|
||||
}
|
||||
|
||||
func must(err error) {
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
func PopFront[T any](s []T) (T, []T, bool) {
|
||||
if len(s) == 0 {
|
||||
var zero T
|
||||
|
||||
Reference in New Issue
Block a user