1405 def get_code_put_full(self, location: Location, packet: str) -> str:
1406 """Generate put code without array-diff."""
1407 size = self.size_at(location)
1408 inner_put = prefix(" ", self.inner_put(location, packet))
1410 send_size = bool(self.resize(location, packet, "s"))
1412 # Note: strictly speaking, we could allow size == MAX_UINT16,
1413 # but we might want to use that in the future to signal overlong
1414 # vectors (like with jumbo packets)
1415 # Though that would also mean packets larger than 64 KiB,
1416 # which we're a long way from
1418 fc_assert({size.actual @ packet} < MAX_UINT16);
1419 e |= DIO_PUT(arraylen, &dout, &field_addr, {size.actual @ packet});
1421#ifdef FREECIV_JSON_CONNECTION
1425#ifdef FREECIV_JSON_CONNECTION
1426 /* Create the array. */
1427 e |= DIO_PUT(farray, &dout, &field_addr, {size.actual @ packet});
1431 null_condition = self.null_condition(location)
1432 if null_condition is None:
1437if ({null_condition @ packet}) {{
1438 /* Transmit null as empty */
1439 e |= DIO_PUT(arraylen, &dout, &field_addr, 0);
1444if ({null_condition @ packet}) {{
1445 /* Transmit null as empty */
1446#ifdef FREECIV_JSON_CONNECTION
1447 /* Create the array. */
1448 e |= DIO_PUT(farray, &dout, &field_addr, 0);
1449#endif /* FREECIV_JSON_CONNECTION */
1454 int {location.index};
1458 {location.json_subloc} = plocation_elem_new(0);
1459#endif /* FREECIV_JSON_CONNECTION */
1461 for ({location.index} = 0; {location.index} < {size.actual @ packet}; {location.index}++) {{
1462#ifdef FREECIV_JSON_CONNECTION
1463 /* Next array element. */
1464 {location.json_subloc}->number = {location.index};
1465#endif /* FREECIV_JSON_CONNECTION */
1470#ifdef FREECIV_JSON_CONNECTION
1472 FC_FREE({location.json_subloc});
1473#endif /* FREECIV_JSON_CONNECTION */
1477 def get_code_put_diff(self, location: Location, packet: str, diff_packet: str) -> str:
1478 """Generate array-diff put code."""
1479 size = self.size_at(location)
1481 null_condition = self.null_condition(location)
1482 if null_condition is not None:
1483 safe_size = SizeInfo(size.declared @ None, size.actual.replace(f"({null_condition} ? 0 : {size.actual})"))
1487 if self.resize(location, packet, "s"):
1488 # starts and ends inside JSON ifdef
1491 /* Create the object to hold new size and delta. */
1492 e |= DIO_PUT(object, &dout, &field_addr);
1494 /* Enter object (start at size address). */
1495 {location.json_subloc} = plocation_field_new("size");
1496#endif /* FREECIV_JSON_CONNECTION */
1498 /* Write the new size */
1499 e |= DIO_PUT(uint16, &dout, &field_addr, {safe_size.actual @ packet});
1501#ifdef FREECIV_JSON_CONNECTION
1502 /* Delta address. */
1503 {location.json_subloc}->name = "delta";
1505 # starts and ends inside JSON ifdef
1508 FC_FREE({location.json_subloc});
1510 # everything else is nested one level deeper than usual
1511 location.json_depth += 1
1513 size_head = size_tail = ""
1515 # Note: At the moment, we're only deep-diffing our elements
1516 # if our array size is constant
1517 value_put = prefix(" ", self.inner_put(location, packet, diff_packet if size.constant else None, 2))
1518 inner_cmp = prefix(" ", self.inner_cmp(location, packet, diff_packet))
1519 index_put = prefix(" ", size.index_put(packet, location.index))
1520 index_put_sentinel = prefix(" ", safe_size.index_put(packet, safe_size.actual @ packet))
1521 size_check = prefix(" ", safe_size.size_check_index(location.name, packet))
1523 if not size.constant:
1525 if ({location.index} < {size.actual @ diff_packet}) {{
1526{prefix(" ", inner_cmp)}\
1528 /* Always transmit new elements */
1535 int {location.index};
1539#ifdef FREECIV_JSON_CONNECTION
1540 size_t count_{location.index} = 0;
1543 /* Create the array. */
1544 e |= DIO_PUT(farray, &dout, &field_addr, 0);
1547 {location.json_subloc} = plocation_elem_new(0);
1548#endif /* FREECIV_JSON_CONNECTION */
1550 for ({location.index} = 0; {location.index} < {safe_size.actual @ packet}; {location.index}++) {{
1557#ifdef FREECIV_JSON_CONNECTION
1558 /* Append next diff array element. */
1559 {location.json_subloc}->number = -1;
1561 /* Create the diff array element. */
1562 e |= DIO_PUT(object, &dout, &field_addr);
1564 /* Enter diff array element (start at the index address). */
1565 {location.json_subloc}->number = count_{location.index}++;
1566 {location.json_subloc}->sub_location = plocation_field_new("index");
1567#endif /* FREECIV_JSON_CONNECTION */
1569 /* Write the index */
1572#ifdef FREECIV_JSON_CONNECTION
1573 /* Content address. */
1574 {location.json_subloc}->sub_location->name = "data";
1575#endif /* FREECIV_JSON_CONNECTION */
1579#ifdef FREECIV_JSON_CONNECTION
1580 /* Exit diff array element. */
1581 FC_FREE({location.json_subloc}->sub_location);
1582#endif /* FREECIV_JSON_CONNECTION */
1585#ifdef FREECIV_JSON_CONNECTION
1586 /* Append diff array element. */
1587 {location.json_subloc}->number = -1;
1589 /* Create the terminating diff array element. */
1590 e |= DIO_PUT(object, &dout, &field_addr);
1592 /* Enter diff array element (start at the index address). */
1593 {location.json_subloc}->number = count_{location.index};
1594 {location.json_subloc}->sub_location = plocation_field_new("index");
1595#endif /* FREECIV_JSON_CONNECTION */
1597 /* Write the sentinel value */
1598{index_put_sentinel}\
1600#ifdef FREECIV_JSON_CONNECTION
1601 /* Exit diff array element. */
1602 FC_FREE({location.json_subloc}->sub_location);
1604 FC_FREE({location.json_subloc});
1606#endif /* FREECIV_JSON_CONNECTION */
1622 def get_code_get_full(self, location: Location, packet: str) -> str:
1623 """Generate get code without array-diff."""
1624 size = self.size_at(location)
1625 inner_get = prefix(" ", self.inner_get(location, packet))
1627 resize = self.resize(location, packet, location.index)
1630 if (!DIO_GET(arraylen, &din, &field_addr, &{location.index})) {{
1631 RECEIVE_PACKET_FIELD_ERROR({location.name});
1633{prefix(" ", resize)}\
1636 size_part = prefix(" ", size.size_check_get(location.name, packet))
1640 int {location.index};
1644#ifdef FREECIV_JSON_CONNECTION
1646 {location.json_subloc} = plocation_elem_new(0);
1647#endif /* FREECIV_JSON_CONNECTION */
1649 for ({location.index} = 0; {location.index} < {size.actual @ packet}; {location.index}++) {{
1650#ifdef FREECIV_JSON_CONNECTION
1651 /* Next array element */
1652 {location.json_subloc}->number = {location.index};
1653#endif /* FREECIV_JSON_CONNECTION */
1658#ifdef FREECIV_JSON_CONNECTION
1660 FC_FREE({location.json_subloc});
1661#endif /* FREECIV_JSON_CONNECTION */
1665 def get_code_get_diff(self, location: Location, packet: str) -> str:
1666 """Generate array-diff get code."""
1667 size = self.size_at(location)
1669 resize = self.resize(location, packet, location.index)
1671 # ends inside a JSON ifdef
1673#ifdef FREECIV_JSON_CONNECTION
1674/* Enter object (start at size address). */
1675{location.json_subloc} = plocation_field_new("size");
1676#endif /* FREECIV_JSON_CONNECTION */
1681 if (!DIO_GET(uint16, &din, &field_addr, &readin)) {{
1682 RECEIVE_PACKET_FIELD_ERROR({location.name});
1684{prefix(" ", resize)}\
1687#ifdef FREECIV_JSON_CONNECTION
1689{location.json_subloc}->name = "delta";
1692/* Exit diff object. */
1693FC_FREE({location.json_subloc});
1695 location.json_depth += 1
1698{prefix(" ", size.size_check_get(location.name, packet))}\
1699#ifdef FREECIV_JSON_CONNECTION
1703 # Note: At the moment, we're only deep-diffing our elements
1704 # if our array size is constant
1705 value_get = prefix(" ", self.inner_get(location, packet, size.constant, 2))
1706 index_get = prefix(" ", size.index_get(packet, location))
1709{size.size_check_index(location.name, packet)}\
1711/* Enter array (start at initial element). */
1712{location.json_subloc} = plocation_elem_new(0);
1713/* Enter diff array element (start at the index address). */
1714{location.json_subloc}->sub_location = plocation_field_new("index");
1715#endif /* FREECIV_JSON_CONNECTION */
1718 int {location.index};
1720 /* Read next index */
1723 if ({location.index} == {size.actual @ packet}) {{
1726 if ({location.index} > {size.actual @ packet}) {{
1727 RECEIVE_PACKET_FIELD_ERROR({location.name},
1728 ": unexpected index %d "
1729 "> length %d in array diff",
1731 {size.actual @ packet});
1734#ifdef FREECIV_JSON_CONNECTION
1735 /* Content address. */
1736 {location.json_subloc}->sub_location->name = "data";
1737#endif /* FREECIV_JSON_CONNECTION */
1741#ifdef FREECIV_JSON_CONNECTION
1742 /* Move to the next diff array element. */
1743 {location.json_subloc}->number++;
1744 /* Back to the index address. */
1745 {location.json_subloc}->sub_location->name = "index";
1746#endif /* FREECIV_JSON_CONNECTION */
1749#ifdef FREECIV_JSON_CONNECTION
1750/* Exit diff array element. */
1751FC_FREE({location.json_subloc}->sub_location);
1753FC_FREE({location.json_subloc});
1755#endif /* FREECIV_JSON_CONNECTION */