137 va_end(va); |
137 va_end(va); |
138 |
138 |
139 DEBUG(grf, severity, "[%s:%d] %s", _cur_grfconfig->filename, _nfo_line, buf); |
139 DEBUG(grf, severity, "[%s:%d] %s", _cur_grfconfig->filename, _nfo_line, buf); |
140 } |
140 } |
141 |
141 |
142 static inline void check_length(int real, int wanted, const char *str) |
142 static inline bool check_length(int real, int wanted, const char *str) |
143 { |
143 { |
144 if (real >= wanted) return; |
144 if (real >= wanted) return true; |
145 grfmsg(0, "%s: Invalid pseudo sprite length %d (expected %d)!", str, real, wanted); |
145 grfmsg(0, "%s: Invalid pseudo sprite length %d (expected %d)!", str, real, wanted); |
|
146 return false; |
146 } |
147 } |
147 |
148 |
148 static inline byte grf_load_byte(byte **buf) |
149 static inline byte grf_load_byte(byte **buf) |
149 { |
150 { |
150 return *(*buf)++; |
151 return *(*buf)++; |
1332 if (len == 1) { |
1333 if (len == 1) { |
1333 grfmsg(8, "Silently ignoring one-byte special sprite 0x00"); |
1334 grfmsg(8, "Silently ignoring one-byte special sprite 0x00"); |
1334 return; |
1335 return; |
1335 } |
1336 } |
1336 |
1337 |
1337 check_length(len, 6, "FeatureChangeInfo"); |
1338 if (!check_length(len, 6, "FeatureChangeInfo")) return; |
1338 buf++; |
1339 buf++; |
1339 feature = grf_load_byte(&buf); |
1340 feature = grf_load_byte(&buf); |
1340 numprops = grf_load_byte(&buf); |
1341 numprops = grf_load_byte(&buf); |
1341 numinfo = grf_load_byte(&buf); |
1342 numinfo = grf_load_byte(&buf); |
1342 engine = grf_load_byte(&buf); |
1343 engine = grf_load_byte(&buf); |
1418 uint8 feature; |
1419 uint8 feature; |
1419 uint8 numprops; |
1420 uint8 numprops; |
1420 uint8 numinfo; |
1421 uint8 numinfo; |
1421 uint8 index; |
1422 uint8 index; |
1422 |
1423 |
1423 check_length(len, 6, "SafeChangeInfo"); |
1424 if (!check_length(len, 6, "SafeChangeInfo")) return; |
1424 buf++; |
1425 buf++; |
1425 feature = grf_load_byte(&buf); |
1426 feature = grf_load_byte(&buf); |
1426 numprops = grf_load_byte(&buf); |
1427 numprops = grf_load_byte(&buf); |
1427 numinfo = grf_load_byte(&buf); |
1428 numinfo = grf_load_byte(&buf); |
1428 index = grf_load_byte(&buf); |
1429 index = grf_load_byte(&buf); |
1584 uint8 setid; |
1585 uint8 setid; |
1585 uint8 type; |
1586 uint8 type; |
1586 SpriteGroup *group = NULL; |
1587 SpriteGroup *group = NULL; |
1587 byte *bufend = buf + len; |
1588 byte *bufend = buf + len; |
1588 |
1589 |
1589 check_length(len, 5, "NewSpriteGroup"); |
1590 if (!check_length(len, 5, "NewSpriteGroup")) return; |
1590 buf++; |
1591 buf++; |
1591 |
1592 |
1592 feature = grf_load_byte(&buf); |
1593 feature = grf_load_byte(&buf); |
1593 setid = grf_load_byte(&buf); |
1594 setid = grf_load_byte(&buf); |
1594 type = grf_load_byte(&buf); |
1595 type = grf_load_byte(&buf); |
1613 byte varadjust; |
1614 byte varadjust; |
1614 byte varsize; |
1615 byte varsize; |
1615 uint i; |
1616 uint i; |
1616 |
1617 |
1617 /* Check we can load the var size parameter */ |
1618 /* Check we can load the var size parameter */ |
1618 check_length(bufend - buf, 1, "NewSpriteGroup (Deterministic) (1)"); |
1619 if (!check_length(bufend - buf, 1, "NewSpriteGroup (Deterministic) (1)")) return; |
1619 |
1620 |
1620 group = AllocateSpriteGroup(); |
1621 group = AllocateSpriteGroup(); |
1621 group->type = SGT_DETERMINISTIC; |
1622 group->type = SGT_DETERMINISTIC; |
1622 group->g.determ.var_scope = HASBIT(type, 1) ? VSG_SCOPE_PARENT : VSG_SCOPE_SELF; |
1623 group->g.determ.var_scope = HASBIT(type, 1) ? VSG_SCOPE_PARENT : VSG_SCOPE_SELF; |
1623 |
1624 |
1626 case 0: group->g.determ.size = DSG_SIZE_BYTE; varsize = 1; break; |
1627 case 0: group->g.determ.size = DSG_SIZE_BYTE; varsize = 1; break; |
1627 case 1: group->g.determ.size = DSG_SIZE_WORD; varsize = 2; break; |
1628 case 1: group->g.determ.size = DSG_SIZE_WORD; varsize = 2; break; |
1628 case 2: group->g.determ.size = DSG_SIZE_DWORD; varsize = 4; break; |
1629 case 2: group->g.determ.size = DSG_SIZE_DWORD; varsize = 4; break; |
1629 } |
1630 } |
1630 |
1631 |
1631 check_length(bufend - buf, 5 + varsize, "NewSpriteGroup (Deterministic) (2)"); |
1632 if (!check_length(bufend - buf, 5 + varsize, "NewSpriteGroup (Deterministic) (2)")) return; |
1632 |
1633 |
1633 /* Loop through the var adjusts. Unfortunately we don't know how many we have |
1634 /* Loop through the var adjusts. Unfortunately we don't know how many we have |
1634 * from the outset, so we shall have to keep reallocing. */ |
1635 * from the outset, so we shall have to keep reallocing. */ |
1635 do { |
1636 do { |
1636 DeterministicSpriteGroupAdjust *adjust; |
1637 DeterministicSpriteGroupAdjust *adjust; |
1637 |
1638 |
1638 if (group->g.determ.num_adjusts > 0) { |
1639 if (group->g.determ.num_adjusts > 0) { |
1639 check_length(bufend - buf, 2 + varsize + 3, "NewSpriteGroup (Deterministic) (3)"); |
1640 if (!check_length(bufend - buf, 2 + varsize + 3, "NewSpriteGroup (Deterministic) (3)")) return; |
1640 } |
1641 } |
1641 |
1642 |
1642 group->g.determ.num_adjusts++; |
1643 group->g.determ.num_adjusts++; |
1643 ReallocT(&group->g.determ.adjusts, group->g.determ.num_adjusts); |
1644 ReallocT(&group->g.determ.adjusts, group->g.determ.num_adjusts); |
1644 |
1645 |
1666 } while (HASBIT(varadjust, 5)); |
1667 } while (HASBIT(varadjust, 5)); |
1667 |
1668 |
1668 group->g.determ.num_ranges = grf_load_byte(&buf); |
1669 group->g.determ.num_ranges = grf_load_byte(&buf); |
1669 CallocT(&group->g.determ.ranges, group->g.determ.num_ranges); |
1670 CallocT(&group->g.determ.ranges, group->g.determ.num_ranges); |
1670 |
1671 |
1671 check_length(bufend - buf, 2 + (2 + 2 * varsize) * group->g.determ.num_ranges, "NewSpriteGroup (Deterministic)"); |
1672 if (!check_length(bufend - buf, 2 + (2 + 2 * varsize) * group->g.determ.num_ranges, "NewSpriteGroup (Deterministic)")) return; |
1672 |
1673 |
1673 for (i = 0; i < group->g.determ.num_ranges; i++) { |
1674 for (i = 0; i < group->g.determ.num_ranges; i++) { |
1674 group->g.determ.ranges[i].group = GetGroupFromGroupID(setid, type, grf_load_word(&buf)); |
1675 group->g.determ.ranges[i].group = GetGroupFromGroupID(setid, type, grf_load_word(&buf)); |
1675 group->g.determ.ranges[i].low = grf_load_var(varsize, &buf); |
1676 group->g.determ.ranges[i].low = grf_load_var(varsize, &buf); |
1676 group->g.determ.ranges[i].high = grf_load_var(varsize, &buf); |
1677 group->g.determ.ranges[i].high = grf_load_var(varsize, &buf); |
1685 case 0x83: // Parent scope |
1686 case 0x83: // Parent scope |
1686 { |
1687 { |
1687 byte triggers; |
1688 byte triggers; |
1688 uint i; |
1689 uint i; |
1689 |
1690 |
1690 check_length(bufend - buf, 7, "NewSpriteGroup (Randomized) (1)"); |
1691 if (!check_length(bufend - buf, 7, "NewSpriteGroup (Randomized) (1)")) return; |
1691 |
1692 |
1692 group = AllocateSpriteGroup(); |
1693 group = AllocateSpriteGroup(); |
1693 group->type = SGT_RANDOMIZED; |
1694 group->type = SGT_RANDOMIZED; |
1694 group->g.random.var_scope = HASBIT(type, 1) ? VSG_SCOPE_PARENT : VSG_SCOPE_SELF; |
1695 group->g.random.var_scope = HASBIT(type, 1) ? VSG_SCOPE_PARENT : VSG_SCOPE_SELF; |
1695 |
1696 |
1698 group->g.random.cmp_mode = HASBIT(triggers, 7) ? RSG_CMP_ALL : RSG_CMP_ANY; |
1699 group->g.random.cmp_mode = HASBIT(triggers, 7) ? RSG_CMP_ALL : RSG_CMP_ANY; |
1699 group->g.random.lowest_randbit = grf_load_byte(&buf); |
1700 group->g.random.lowest_randbit = grf_load_byte(&buf); |
1700 group->g.random.num_groups = grf_load_byte(&buf); |
1701 group->g.random.num_groups = grf_load_byte(&buf); |
1701 CallocT(&group->g.random.groups, group->g.random.num_groups); |
1702 CallocT(&group->g.random.groups, group->g.random.num_groups); |
1702 |
1703 |
1703 check_length(bufend - buf, 2 * group->g.random.num_groups, "NewSpriteGroup (Randomized) (2)"); |
1704 if (!check_length(bufend - buf, 2 * group->g.random.num_groups, "NewSpriteGroup (Randomized) (2)")) return; |
1704 |
1705 |
1705 for (i = 0; i < group->g.random.num_groups; i++) { |
1706 for (i = 0; i < group->g.random.num_groups; i++) { |
1706 group->g.random.groups[i] = GetGroupFromGroupID(setid, type, grf_load_word(&buf)); |
1707 group->g.random.groups[i] = GetGroupFromGroupID(setid, type, grf_load_word(&buf)); |
1707 } |
1708 } |
1708 |
1709 |
1729 if (_cur_grffile->spriteset_start == 0) { |
1730 if (_cur_grffile->spriteset_start == 0) { |
1730 grfmsg(0, "NewSpriteGroup: No sprite set to work on! Skipping"); |
1731 grfmsg(0, "NewSpriteGroup: No sprite set to work on! Skipping"); |
1731 return; |
1732 return; |
1732 } |
1733 } |
1733 |
1734 |
1734 check_length(bufend - buf, 2 * num_loaded + 2 * num_loading, "NewSpriteGroup (Real) (1)"); |
1735 if (!check_length(bufend - buf, 2 * num_loaded + 2 * num_loading, "NewSpriteGroup (Real) (1)")) return; |
1735 |
1736 |
1736 group = AllocateSpriteGroup(); |
1737 group = AllocateSpriteGroup(); |
1737 group->type = SGT_REAL; |
1738 group->type = SGT_REAL; |
1738 |
1739 |
1739 group->g.real.num_loaded = num_loaded; |
1740 group->g.real.num_loaded = num_loaded; |
1794 uint8 idcount; |
1795 uint8 idcount; |
1795 bool wagover; |
1796 bool wagover; |
1796 uint8 cidcount; |
1797 uint8 cidcount; |
1797 int c, i; |
1798 int c, i; |
1798 |
1799 |
1799 check_length(len, 6, "FeatureMapSpriteGroup"); |
1800 if (!check_length(len, 6, "FeatureMapSpriteGroup")) return; |
1800 feature = buf[1]; |
1801 feature = buf[1]; |
1801 idcount = buf[2] & 0x7F; |
1802 idcount = buf[2] & 0x7F; |
1802 wagover = (buf[2] & 0x80) == 0x80; |
1803 wagover = (buf[2] & 0x80) == 0x80; |
1803 check_length(len, 3 + idcount, "FeatureMapSpriteGroup"); |
1804 if (!check_length(len, 3 + idcount, "FeatureMapSpriteGroup")) return; |
1804 |
1805 |
1805 /* If ``n-id'' (or ``idcount'') is zero, this is a ``feature |
1806 /* If ``n-id'' (or ``idcount'') is zero, this is a ``feature |
1806 * callback''. */ |
1807 * callback''. */ |
1807 if (idcount == 0) { |
1808 if (idcount == 0) { |
1808 grfmsg(2, "FeatureMapSpriteGroup: Feature callbacks not implemented yet"); |
1809 grfmsg(2, "FeatureMapSpriteGroup: Feature callbacks not implemented yet"); |
1809 return; |
1810 return; |
1810 } |
1811 } |
1811 |
1812 |
1812 cidcount = buf[3 + idcount]; |
1813 cidcount = buf[3 + idcount]; |
1813 check_length(len, 4 + idcount + cidcount * 3, "FeatureMapSpriteGroup"); |
1814 if (!check_length(len, 4 + idcount + cidcount * 3, "FeatureMapSpriteGroup")) return; |
1814 |
1815 |
1815 grfmsg(6, "FeatureMapSpriteGroup: Feature %d, %d ids, %d cids, wagon override %d", |
1816 grfmsg(6, "FeatureMapSpriteGroup: Feature %d, %d ids, %d cids, wagon override %d", |
1816 feature, idcount, cidcount, wagover); |
1817 feature, idcount, cidcount, wagover); |
1817 |
1818 |
1818 if (feature > GSF_STATION) { |
1819 if (feature > GSF_STATION) { |
2002 uint16 endid; |
2003 uint16 endid; |
2003 const char* name; |
2004 const char* name; |
2004 bool new_scheme = _cur_grffile->grf_version >= 7; |
2005 bool new_scheme = _cur_grffile->grf_version >= 7; |
2005 bool generic; |
2006 bool generic; |
2006 |
2007 |
2007 check_length(len, 6, "FeatureNewName"); |
2008 if (!check_length(len, 6, "FeatureNewName")) return; |
2008 buf++; |
2009 buf++; |
2009 feature = grf_load_byte(&buf); |
2010 feature = grf_load_byte(&buf); |
2010 lang = grf_load_byte(&buf); |
2011 lang = grf_load_byte(&buf); |
2011 num = grf_load_byte(&buf); |
2012 num = grf_load_byte(&buf); |
2012 generic = HASBIT(lang, 7); |
2013 generic = HASBIT(lang, 7); |
2354 uint32 mask = 0; |
2355 uint32 mask = 0; |
2355 bool result; |
2356 bool result; |
2356 GRFLabel *label; |
2357 GRFLabel *label; |
2357 GRFLabel *choice = NULL; |
2358 GRFLabel *choice = NULL; |
2358 |
2359 |
2359 check_length(len, 6, "SkipIf"); |
2360 if (!check_length(len, 6, "SkipIf")) return; |
2360 buf++; |
2361 buf++; |
2361 param = grf_load_byte(&buf); |
2362 param = grf_load_byte(&buf); |
2362 paramsize = grf_load_byte(&buf); |
2363 paramsize = grf_load_byte(&buf); |
2363 condtype = grf_load_byte(&buf); |
2364 condtype = grf_load_byte(&buf); |
2364 |
2365 |
2528 /* TODO: Check version. (We should have own versioning done somehow.) */ |
2529 /* TODO: Check version. (We should have own versioning done somehow.) */ |
2529 uint8 version; |
2530 uint8 version; |
2530 uint32 grfid; |
2531 uint32 grfid; |
2531 const char *name; |
2532 const char *name; |
2532 |
2533 |
2533 check_length(len, 8, "GRFInfo"); buf++; |
2534 if (!check_length(len, 8, "GRFInfo")) return; buf++; |
2534 version = grf_load_byte(&buf); |
2535 version = grf_load_byte(&buf); |
2535 grfid = grf_load_dword(&buf); |
2536 grfid = grf_load_dword(&buf); |
2536 name = (const char*)buf; |
2537 name = (const char*)buf; |
2537 |
2538 |
2538 _cur_grffile->grfid = grfid; |
2539 _cur_grffile->grfid = grfid; |
2644 /* Action 0x0D (GLS_SAFETYSCAN) */ |
2645 /* Action 0x0D (GLS_SAFETYSCAN) */ |
2645 static void SafeParamSet(byte *buf, int len) |
2646 static void SafeParamSet(byte *buf, int len) |
2646 { |
2647 { |
2647 uint8 target; |
2648 uint8 target; |
2648 |
2649 |
2649 check_length(len, 5, "SafeParamSet"); |
2650 if (!check_length(len, 5, "SafeParamSet")) return; |
2650 buf++; |
2651 buf++; |
2651 target = grf_load_byte(&buf); |
2652 target = grf_load_byte(&buf); |
2652 |
2653 |
2653 /* Only writing GRF parameters is considered safe */ |
2654 /* Only writing GRF parameters is considered safe */ |
2654 if (target < 0x80) return; |
2655 if (target < 0x80) return; |
2694 uint32 src1; |
2695 uint32 src1; |
2695 uint32 src2; |
2696 uint32 src2; |
2696 uint32 data = 0; |
2697 uint32 data = 0; |
2697 uint32 res; |
2698 uint32 res; |
2698 |
2699 |
2699 check_length(len, 5, "ParamSet"); |
2700 if (!check_length(len, 5, "ParamSet")) return; |
2700 buf++; |
2701 buf++; |
2701 target = grf_load_byte(&buf); |
2702 target = grf_load_byte(&buf); |
2702 oper = grf_load_byte(&buf); |
2703 oper = grf_load_byte(&buf); |
2703 src1 = grf_load_byte(&buf); |
2704 src1 = grf_load_byte(&buf); |
2704 src2 = grf_load_byte(&buf); |
2705 src2 = grf_load_byte(&buf); |
2962 * D grfids GRFIDs of the files to deactivate */ |
2963 * D grfids GRFIDs of the files to deactivate */ |
2963 |
2964 |
2964 byte num; |
2965 byte num; |
2965 int i; |
2966 int i; |
2966 |
2967 |
2967 check_length(len, 1, "GRFInhibit"); |
2968 if (!check_length(len, 1, "GRFInhibit")) return; |
2968 buf++, len--; |
2969 buf++, len--; |
2969 num = grf_load_byte(&buf); len--; |
2970 num = grf_load_byte(&buf); len--; |
2970 check_length(len, 4 * num, "GRFInhibit"); |
2971 if (!check_length(len, 4 * num, "GRFInhibit")) return; |
2971 |
2972 |
2972 for (i = 0; i < num; i++) { |
2973 for (i = 0; i < num; i++) { |
2973 uint32 grfid = grf_load_dword(&buf); |
2974 uint32 grfid = grf_load_dword(&buf); |
2974 |
2975 |
2975 /* GRF is unsafe it if tries to deactivate other GRFs */ |
2976 /* GRF is unsafe it if tries to deactivate other GRFs */ |
2993 * D grfids GRFIDs of the files to deactivate */ |
2994 * D grfids GRFIDs of the files to deactivate */ |
2994 |
2995 |
2995 byte num; |
2996 byte num; |
2996 int i; |
2997 int i; |
2997 |
2998 |
2998 check_length(len, 1, "GRFInhibit"); |
2999 if (!check_length(len, 1, "GRFInhibit")) return; |
2999 buf++, len--; |
3000 buf++, len--; |
3000 num = grf_load_byte(&buf); len--; |
3001 num = grf_load_byte(&buf); len--; |
3001 check_length(len, 4 * num, "GRFInhibit"); |
3002 if (!check_length(len, 4 * num, "GRFInhibit")) return; |
3002 |
3003 |
3003 for (i = 0; i < num; i++) { |
3004 for (i = 0; i < num; i++) { |
3004 uint32 grfid = grf_load_dword(&buf); |
3005 uint32 grfid = grf_load_dword(&buf); |
3005 GRFConfig *file = GetGRFConfig(grfid); |
3006 GRFConfig *file = GetGRFConfig(grfid); |
3006 |
3007 |
3021 * B label The label to define |
3022 * B label The label to define |
3022 * V comment Optional comment - ignored */ |
3023 * V comment Optional comment - ignored */ |
3023 |
3024 |
3024 GRFLabel *label; |
3025 GRFLabel *label; |
3025 |
3026 |
3026 check_length(len, 1, "DefineGotoLabel"); |
3027 if (!check_length(len, 1, "DefineGotoLabel")) return; |
3027 buf++; len--; |
3028 buf++; len--; |
3028 |
3029 |
3029 MallocT(&label, 1); |
3030 MallocT(&label, 1); |
3030 label->label = grf_load_byte(&buf); |
3031 label->label = grf_load_byte(&buf); |
3031 label->nfo_line = _nfo_line; |
3032 label->nfo_line = _nfo_line; |
3052 * |
3053 * |
3053 * W num Number of sound files that follow */ |
3054 * W num Number of sound files that follow */ |
3054 |
3055 |
3055 uint16 num; |
3056 uint16 num; |
3056 |
3057 |
3057 check_length(len, 1, "GRFSound"); |
3058 if (!check_length(len, 1, "GRFSound")) return; |
3058 buf++; |
3059 buf++; |
3059 num = grf_load_word(&buf); |
3060 num = grf_load_word(&buf); |
3060 |
3061 |
3061 _grf_data_blocks = num; |
3062 _grf_data_blocks = num; |
3062 _grf_data_type = GDT_SOUND; |
3063 _grf_data_type = GDT_SOUND; |
3189 |
3190 |
3190 uint8 num_def; |
3191 uint8 num_def; |
3191 uint i; |
3192 uint i; |
3192 |
3193 |
3193 buf++; len--; |
3194 buf++; len--; |
3194 check_length(len, 1, "LoadFontGlyph"); |
3195 if (!check_length(len, 1, "LoadFontGlyph")) return; |
3195 |
3196 |
3196 num_def = grf_load_byte(&buf); |
3197 num_def = grf_load_byte(&buf); |
3197 |
3198 |
3198 check_length(len, 1 + num_def * 4, "LoadFontGlyph"); |
3199 if (!check_length(len, 1 + num_def * 4, "LoadFontGlyph")) return; |
3199 |
3200 |
3200 for (i = 0; i < num_def; i++) { |
3201 for (i = 0; i < num_def; i++) { |
3201 FontSize size = (FontSize)grf_load_byte(&buf); |
3202 FontSize size = (FontSize)grf_load_byte(&buf); |
3202 uint8 num_char = grf_load_byte(&buf); |
3203 uint8 num_char = grf_load_byte(&buf); |
3203 uint16 base_char = grf_load_word(&buf); |
3204 uint16 base_char = grf_load_word(&buf); |