aorecomp

Anarchy Online classic in the browser

git clone https://aorecomp.ubeagle.com/code/aorecomp.git


server/target_protocol.go (8540B)


      1 package main
      2 
      3 import (
      4 	"bytes"
      5 	"encoding/binary"
      6 	"time"
      7 )
      8 
      9 func targetPacket(packet []byte) []byte {
     10 	// Convert the shared 11.0 messages to the recovered 14.9.1 wire layout.
     11 	packet = packet[:binary.BigEndian.Uint16(packet[6:8])]
     12 	switch binary.BigEndian.Uint32(packet[20:24]) {
     13 	case 0x36284f6e:
     14 		if binary.BigEndian.Uint32(packet[52:56]) == 104 {
     15 			binary.BigEndian.PutUint32(packet[56:60], binary.BigEndian.Uint32(packet[56:60])-16)
     16 		}
     17 	case 0x5f4b1a39:
     18 		// Preserve the variable generator object, then strip only the legacy name.
     19 		end := 93
     20 		switch binary.BigEndian.Uint32(packet[77:81]) {
     21 		case 0:
     22 		case 51103:
     23 			if binary.BigEndian.Uint16(packet[89:91]) != 3 {
     24 				panic("unsupported mission generator version")
     25 			}
     26 			end = 116 + 6*int(binary.BigEndian.Uint32(packet[104:108]))
     27 		default:
     28 			panic("unsupported playfield generator")
     29 		}
     30 		packet = packet[:end]
     31 	case 0x271b3a6b:
     32 		packet[36] = '0'
     33 		flags := binary.BigEndian.Uint32(packet[37:41])
     34 		// 14.9.1 packs Side in bits 0-2 and skin Race in bits 10-11.
     35 		appearance := uint32(binary.BigEndian.Uint16(packet[67:69]))
     36 		packed := appearance>>9&3 | (appearance>>2&3)<<3 | (appearance>>4&7)<<5
     37 		packed |= (appearance>>7&3)<<8 | (appearance&3)<<10 | (appearance>>11&31)<<12
     38 		if flags&1 != 0 && appearance>>4&7 == 6 {
     39 			// Local PvE creatures use the monster side for enemy targeting.
     40 			packed = packed&^7 | 3
     41 		}
     42 		tail := append([]byte(nil), packet[69:]...)
     43 		packet = binary.BigEndian.AppendUint32(packet[:67], packed)
     44 		packet = append(packet, tail...)
     45 		nameEnd := 72 + int(packet[71])
     46 		if flags&1 != 0 {
     47 			// kkr.dll SimpleChar 0x10082da0: DWORD and four shorts after NPC names.
     48 			tail = append([]byte(nil), packet[nameEnd:]...)
     49 			packet = append(packet[:nameEnd], make([]byte, 12)...)
     50 			packet = append(packet, tail...)
     51 			nameEnd += 12
     52 		} else {
     53 			nameEnd += 20
     54 		}
     55 		if flags&0x90 != 0 {
     56 			// kkr.dll 0x1015b580: encoded count, then fixed 32-byte material names.
     57 			vehicle := nameEnd + 1 + 14
     58 			if flags&0x1000 != 0 {
     59 				vehicle++
     60 			}
     61 			cursor := vehicle + 4 + int(binary.BigEndian.Uint32(packet[vehicle:])) + 9
     62 			if flags&0x2000 != 0 {
     63 				cursor++
     64 			}
     65 			count := int(binary.BigEndian.Uint32(packet[cursor-4:])/1009 - 1)
     66 			converted := append([]byte(nil), packet[:cursor]...)
     67 			for range count {
     68 				end := cursor + bytes.IndexByte(packet[cursor:], 0) + 1
     69 				material := make([]byte, 32)
     70 				copy(material, packet[cursor:end-1])
     71 				converted = append(converted, material...)
     72 				converted = append(converted, packet[end:end+12]...)
     73 				cursor = end + 12
     74 			}
     75 			packet = append(converted, packet[cursor:]...)
     76 		}
     77 		if flags&1 != 0 {
     78 			// Tab targeting requires NPC stat 194 (InPlay), supplied by flag bit 1.
     79 			flags |= 2
     80 		}
     81 		binary.BigEndian.PutUint32(packet[37:41], flags|0x100)
     82 	case 0x29304349:
     83 		// FullCharacter has seven encoded lists before its stats.
     84 		inventoryCount := int(binary.BigEndian.Uint32(packet[40:44])/1009 - 1)
     85 		inventoryEnd := 44 + inventoryCount*32
     86 		for index := 0; index < inventoryCount; index++ {
     87 			cursor := 44 + index*32
     88 			slot := binary.BigEndian.Uint32(packet[cursor:])
     89 			if slot >= 64 && slot < 94 {
     90 				binary.BigEndian.PutUint32(packet[cursor:], slot-16)
     91 			}
     92 		}
     93 		tail := append([]byte(nil), packet[inventoryEnd:]...)
     94 		packet = binary.BigEndian.AppendUint32(packet[:inventoryEnd], 1009)
     95 		packet = append(packet, tail...)
     96 		// Preserve the plain-counted saved-stat map before the remaining groups.
     97 		// kkr.dll sub_3607F5B0: presence DWORD, encoded list and encoded effect list.
     98 		packet = packet[:len(packet)-16]
     99 		for _, value := range []uint32{0, 1009, 1009} {
    100 			packet = binary.BigEndian.AppendUint32(packet, value)
    101 		}
    102 	case 0x47537a24:
    103 		// Ordinary inventory uses shared slots 64..93 and native slots 48..77.
    104 		if binary.BigEndian.Uint32(packet[36:40]) == 104 {
    105 			binary.BigEndian.PutUint32(packet[40:44], binary.BigEndian.Uint32(packet[40:44])-16)
    106 		}
    107 		destination := binary.BigEndian.Uint32(packet[52:56])
    108 		if binary.BigEndian.Uint32(packet[44:48]) == 50000 && destination >= 64 && destination < 94 {
    109 			binary.BigEndian.PutUint32(packet[52:56], destination-16)
    110 		}
    111 	case 0x3b11256f:
    112 		// Parentless SimpleItems have a transform before their placement bytes.
    113 		if binary.BigEndian.Uint32(packet[40:44]) == 50000 && packet[60] == 104 && packet[61] >= 64 && packet[61] < 94 {
    114 			packet[61] -= 16
    115 		}
    116 	case 0x465a5d73, 0x3b1d2268:
    117 		// Preserve first-free sentinel 111; translate only ordinary shared slots.
    118 		if packet[60] == 104 && packet[61] >= 64 && packet[61] < 94 {
    119 			packet[61] -= 16
    120 		}
    121 	case 0x35505644:
    122 		if binary.BigEndian.Uint32(packet[56:60]) == 104 {
    123 			binary.BigEndian.PutUint32(packet[60:64], binary.BigEndian.Uint32(packet[60:64])-16)
    124 		}
    125 	case 0x52526858:
    126 		if (len(packet) == 68 || len(packet) == 76) && binary.BigEndian.Uint32(packet[60:64]) == 104 {
    127 			binary.BigEndian.PutUint32(packet[64:68], binary.BigEndian.Uint32(packet[64:68])-16)
    128 		}
    129 	case 0x26515e61:
    130 		if binary.BigEndian.Uint32(packet[36:40]) == 104 {
    131 			binary.BigEndian.PutUint32(packet[40:44], binary.BigEndian.Uint32(packet[40:44])-16)
    132 		}
    133 	case 0x5e477770:
    134 		action := binary.BigEndian.Uint32(packet[36:40])
    135 		if (action == 52 || action == 53 || action == 81 || action == 112 || action == 146) && binary.BigEndian.Uint32(packet[40:44]) == 104 {
    136 			binary.BigEndian.PutUint32(packet[44:48], binary.BigEndian.Uint32(packet[44:48])-16)
    137 		}
    138 		if (action == 53 || action == 81) && binary.BigEndian.Uint32(packet[48:52]) == 104 {
    139 			binary.BigEndian.PutUint32(packet[52:56], binary.BigEndian.Uint32(packet[52:56])-16)
    140 		}
    141 	case 0x54111123:
    142 		packet = append(packet[:36:36], encodeMovement1491(packet[36:])...)
    143 	case 0x25327751:
    144 		body := packet[36:]
    145 		packet = append(packet[:36:36], encodeMovementBase1491(body)...)
    146 		packet = append(packet, body[32:]...)
    147 	case 0x2d2b2169:
    148 		packet = append(packet[:36:36], encodeMovementBase1491(packet[36:])...)
    149 	case 0x5f52412e:
    150 		packet = binary.BigEndian.AppendUint32(packet, uint32(time.Now().Unix()))
    151 	case 0x43197d22:
    152 		// Teleport carries the full playfield proxy after XYZ.
    153 		zone := binary.BigEndian.Uint32(packet[64:68])
    154 		tail := append([]byte(nil), packet[52:]...)
    155 		packet = append(packet[:48], 'a')
    156 		for _, value := range []uint32{51100, zone, zone, 0, 40016, zone} {
    157 			packet = binary.BigEndian.AppendUint32(packet, value)
    158 		}
    159 		packet = append(packet, tail...)
    160 	}
    161 
    162 	// llr.dll Construct 0x100240b0: byte pass-on flag and no compression DWORD.
    163 	packet = append(packet[:32], packet[35:]...)
    164 	packet = append(packet[:16], packet[20:]...)
    165 	binary.BigEndian.PutUint16(packet[6:8], uint16(len(packet)))
    166 	return append(packet, make([]byte, (-len(packet))&3)...)
    167 }
    168 
    169 func decodeInventory1491(body []byte) {
    170 	if len(body) == 40 && binary.BigEndian.Uint32(body) == 0x36284f6e && binary.BigEndian.Uint32(body[32:36]) == 104 {
    171 		binary.BigEndian.PutUint32(body[36:40], decodeInventorySlot1491(binary.BigEndian.Uint32(body[36:40])))
    172 		return
    173 	}
    174 	if (len(body) == 48 || len(body) == 56) && binary.BigEndian.Uint32(body) == 0x52526858 && binary.BigEndian.Uint32(body[40:44]) == 104 {
    175 		binary.BigEndian.PutUint32(body[44:48], decodeInventorySlot1491(binary.BigEndian.Uint32(body[44:48])))
    176 		return
    177 	}
    178 	if len(body) != 36 {
    179 		return
    180 	}
    181 	switch binary.BigEndian.Uint32(body) {
    182 	case 0x3a243f41:
    183 		if binary.BigEndian.Uint32(body[16:20]) == 104 {
    184 			binary.BigEndian.PutUint32(body[20:24], decodeInventorySlot1491(binary.BigEndian.Uint32(body[20:24])))
    185 		}
    186 	case 0x47537a24:
    187 		if binary.BigEndian.Uint32(body[16:20]) == 104 {
    188 			binary.BigEndian.PutUint32(body[20:24], decodeInventorySlot1491(binary.BigEndian.Uint32(body[20:24])))
    189 		}
    190 		destination := binary.BigEndian.Uint32(body[32:36])
    191 		if binary.BigEndian.Uint32(body[24:28]) == 50000 && destination >= 48 && destination != 111 {
    192 			binary.BigEndian.PutUint32(body[32:36], decodeInventorySlot1491(destination))
    193 		}
    194 	case 0x5e477770:
    195 		action := binary.BigEndian.Uint32(body[16:20])
    196 		if (action == 52 || action == 53 || action == 81 || action == 112) && binary.BigEndian.Uint32(body[20:24]) == 104 {
    197 			binary.BigEndian.PutUint32(body[24:28], decodeInventorySlot1491(binary.BigEndian.Uint32(body[24:28])))
    198 		}
    199 		if (action == 53 || action == 81) && binary.BigEndian.Uint32(body[28:32]) == 104 {
    200 			binary.BigEndian.PutUint32(body[32:36], decodeInventorySlot1491(binary.BigEndian.Uint32(body[32:36])))
    201 		}
    202 	}
    203 }
    204 
    205 func decodeInventorySlot1491(slot uint32) uint32 {
    206 	if slot < 48 || slot >= 78 {
    207 		// Keep malformed inventory slots outside both inventory and equipment.
    208 		return 94
    209 	}
    210 	return slot + 16
    211 }