1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100
|
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "common/config-manager.h"
#include "common/random.h"
#include "common/memstream.h"
#include "mtropolis/coroutines.h"
#include "mtropolis/miniscript.h"
namespace MTropolis {
class MiniscriptInstructionParserFeedback : public IMiniscriptInstructionParserFeedback {
public:
explicit MiniscriptInstructionParserFeedback(Common::Array<MiniscriptReferences::GlobalRef> *globalRefs);
uint registerGlobalGUIDIndex(uint32 guid) override;
private:
Common::Array<MiniscriptReferences::GlobalRef> *_globalRefs;
};
MiniscriptInstructionParserFeedback::MiniscriptInstructionParserFeedback(Common::Array<MiniscriptReferences::GlobalRef> *globalRefs) : _globalRefs(globalRefs) {
}
uint MiniscriptInstructionParserFeedback::registerGlobalGUIDIndex(uint32 guid) {
for (uint i = 0; i < _globalRefs->size(); i++) {
if ((*_globalRefs)[i].guid == guid)
return i;
}
uint newIndex = _globalRefs->size();
MiniscriptReferences::GlobalRef globalRef;
globalRef.guid = guid;
_globalRefs->push_back(globalRef);
return newIndex;
}
bool miniscriptEvaluateTruth(const DynamicValue &value) {
// NOTE: Comparing equal to "true" only passes for 1 exactly, but for conditions,
// any non-zero value is true.
switch (value.getType()) {
case DynamicValueTypes::kBoolean:
return value.getBool();
case DynamicValueTypes::kInteger:
return (value.getInt() != 0);
case DynamicValueTypes::kFloat:
return !(value.getFloat() == 0.0);
case DynamicValueTypes::kObject:
return !value.getObject().object.expired();
default:
return false;
}
}
IMiniscriptInstructionParserFeedback::~IMiniscriptInstructionParserFeedback() {
}
MiniscriptInstruction::~MiniscriptInstruction() {
}
MiniscriptReferences::LocalRef::LocalRef() : guid(0) {
}
MiniscriptReferences::GlobalRef::GlobalRef() : guid(0) {
}
MiniscriptReferences::MiniscriptReferences(const Common::Array<LocalRef> &localRefs, const Common::Array<GlobalRef> &globalRefs) : _localRefs(localRefs), _globalRefs(globalRefs) {
}
void MiniscriptReferences::linkInternalReferences(ObjectLinkingScope *scope) {
// Resolve using name lookups since there are some known cases where the GUID is broken
// e.g. "bArriveFromCutScene" in "Set bArriveFromCutScene on PE" in Obsidian
for (Common::Array<LocalRef>::iterator it = _localRefs.begin(), itEnd = _localRefs.end(); it != itEnd; ++it)
it->resolution = scope->resolve(it->guid, it->name, false);
for (Common::Array<GlobalRef>::iterator it = _globalRefs.begin(), itEnd = _globalRefs.end(); it != itEnd; ++it)
it->resolution = scope->resolve(it->guid, "", true);
}
void MiniscriptReferences::visitInternalReferences(IStructuralReferenceVisitor *visitor) {
for (LocalRef &ref : _localRefs) {
Common::SharedPtr<RuntimeObject> obj = ref.resolution.lock();
if (obj) {
if (obj->isModifier()) {
Common::WeakPtr<Modifier> mod = obj.staticCast<Modifier>();
visitor->visitWeakModifierRef(mod);
ref.resolution = mod;
} else if (obj->isStructural()) {
Common::WeakPtr<Structural> struc = obj.staticCast<Structural>();
visitor->visitWeakStructuralRef(struc);
ref.resolution = struc;
}
}
}
for (GlobalRef &ref : _globalRefs) {
Common::SharedPtr<RuntimeObject> obj = ref.resolution.lock();
if (obj) {
if (obj->isModifier()) {
Common::WeakPtr<Modifier> mod = obj.staticCast<Modifier>();
visitor->visitWeakModifierRef(mod);
ref.resolution = mod;
} else if (obj->isStructural()) {
Common::WeakPtr<Structural> struc = obj.staticCast<Structural>();
visitor->visitWeakStructuralRef(struc);
ref.resolution = struc;
}
}
}
}
Common::WeakPtr<RuntimeObject> MiniscriptReferences::getRefByIndex(uint index) const {
if (index >= _localRefs.size())
return Common::WeakPtr<RuntimeObject>();
return _localRefs[index].resolution;
}
Common::WeakPtr<RuntimeObject> MiniscriptReferences::getGlobalRefByIndex(uint index) const {
if (index >= _globalRefs.size())
return Common::WeakPtr<RuntimeObject>();
return _globalRefs[index].resolution;
}
MiniscriptProgram::MiniscriptProgram(const Common::SharedPtr<Common::Array<uint8> > &programData, const Common::Array<MiniscriptInstruction *> &instructions, const Common::Array<Attribute> &attributes)
: _programData(programData), _instructions(instructions), _attributes(attributes) {
}
MiniscriptProgram::~MiniscriptProgram() {
// Destruct all instructions
for (Common::Array<MiniscriptInstruction *>::const_iterator it = _instructions.begin(), itEnd = _instructions.end(); it != itEnd; ++it)
(*it)->~MiniscriptInstruction();
}
const Common::Array<MiniscriptInstruction *> &MiniscriptProgram::getInstructions() const {
return _instructions;
}
const Common::Array<MiniscriptProgram::Attribute> &MiniscriptProgram::getAttributes() const {
return _attributes;
}
template<class T>
struct MiniscriptInstructionLoader {
static bool loadInstruction(void *dest, uint32 instrFlags, Data::DataReader &instrDataReader, IMiniscriptInstructionParserFeedback &feedback);
};
template<class T>
bool MiniscriptInstructionLoader<T>::loadInstruction(void *dest, uint32 instrFlags, Data::DataReader &instrDataReader, IMiniscriptInstructionParserFeedback &feedback) {
// Default loader for simple instructions with no private data
new (dest) T();
return true;
}
template<>
bool MiniscriptInstructionLoader<MiniscriptInstructions::Send>::loadInstruction(void *dest, uint32 instrFlags, Data::DataReader &instrDataReader, IMiniscriptInstructionParserFeedback &feedback) {
Data::Event dataEvent;
if (!dataEvent.load(instrDataReader))
return false;
Event evt;
if (!evt.load(dataEvent))
return false;
MessageFlags msgFlags;
msgFlags.immediate = ((instrFlags & 0x04) == 0);
msgFlags.cascade = ((instrFlags & 0x08) == 0);
msgFlags.relay = ((instrFlags & 0x10) == 0);
new (dest) MiniscriptInstructions::Send(evt, msgFlags);
return true;
}
template<>
bool MiniscriptInstructionLoader<MiniscriptInstructions::BuiltinFunc>::loadInstruction(void *dest, uint32 instrFlags, Data::DataReader &instrDataReader, IMiniscriptInstructionParserFeedback &feedback) {
uint32 functionID;
if (!instrDataReader.readU32(functionID))
return false;
if (functionID < 1 || functionID > 20)
return false; // Unknown function
new (dest) MiniscriptInstructions::BuiltinFunc(static_cast<MiniscriptInstructions::BuiltinFunc::BuiltinFunctionID>(functionID));
return true;
}
template<>
bool MiniscriptInstructionLoader<MiniscriptInstructions::GetChild>::loadInstruction(void *dest, uint32 instrFlags, Data::DataReader &instrDataReader, IMiniscriptInstructionParserFeedback &feedback) {
uint32 childAttribute;
if (!instrDataReader.readU32(childAttribute))
return false;
new (dest) MiniscriptInstructions::GetChild(childAttribute, (instrFlags & 1) != 0, (instrFlags & 32) != 0);
return true;
}
template<>
bool MiniscriptInstructionLoader<MiniscriptInstructions::PushGlobal>::loadInstruction(void *dest, uint32 instrFlags, Data::DataReader &instrDataReader, IMiniscriptInstructionParserFeedback &feedback) {
uint32 globalID;
if (!instrDataReader.readU32(globalID))
return false;
new (dest) MiniscriptInstructions::PushGlobal(globalID, (instrFlags & 1) != 0);
return true;
}
template<>
bool MiniscriptInstructionLoader<MiniscriptInstructions::Jump>::loadInstruction(void *dest, uint32 instrFlags, Data::DataReader &instrDataReader, IMiniscriptInstructionParserFeedback &feedback) {
uint32 jumpFlags, unknown, instrOffset;
if (!instrDataReader.readU32(jumpFlags) || !instrDataReader.readU32(unknown) || !instrDataReader.readU32(instrOffset))
return false;
bool isConditional = (jumpFlags == 2);
if (jumpFlags != 1 && jumpFlags != 2)
return false; // Don't recognize this flag combination
if (instrOffset == 0)
return false; // Not valid
new (dest) MiniscriptInstructions::Jump(instrOffset, isConditional);
return true;
}
template<>
bool MiniscriptInstructionLoader<MiniscriptInstructions::PushValue>::loadInstruction(void *dest, uint32 instrFlags, Data::DataReader &instrDataReader, IMiniscriptInstructionParserFeedback &feedback) {
uint16 dataType;
if (!instrDataReader.readU16(dataType))
return false;
if (dataType == 0)
new (dest) MiniscriptInstructions::PushValue(MiniscriptInstructions::PushValue::kDataTypeNull, nullptr, false);
else if (dataType == 0x15) {
Common::XPFloat f;
if (!instrDataReader.readPlatformFloat(f))
return false;
double d = f.toDouble();
new (dest) MiniscriptInstructions::PushValue(MiniscriptInstructions::PushValue::kDataTypeDouble, &d, false);
} else if (dataType == 0x1a) {
uint8 boolValue;
if (!instrDataReader.readU8(boolValue))
return false;
bool b = (boolValue != 0);
new (dest) MiniscriptInstructions::PushValue(MiniscriptInstructions::PushValue::kDataTypeBool, &b, false);
} else if (dataType == 0x1f9) {
uint32 refValue;
if (!instrDataReader.readU32(refValue))
return false;
new (dest) MiniscriptInstructions::PushValue(MiniscriptInstructions::PushValue::kDataTypeLocalRef, &refValue, (instrFlags & 1) != 0);
} else if (dataType == 0x1fa) {
uint32 refValue;
if (!instrDataReader.readU32(refValue))
return false;
uint32 indexedRef = feedback.registerGlobalGUIDIndex(refValue);
new (dest) MiniscriptInstructions::PushValue(MiniscriptInstructions::PushValue::kDataTypeGlobalRef, &indexedRef, (instrFlags & 1) != 0);
} else if (dataType == 0x1d) {
MiniscriptInstructions::PushValue::Label label;
if (!instrDataReader.readU32(label.superGroup) || !instrDataReader.readU32(label.id))
return false;
new (dest) MiniscriptInstructions::PushValue(MiniscriptInstructions::PushValue::kDataTypeLabel, &label, false);
} else
return false;
return true;
}
template<>
bool MiniscriptInstructionLoader<MiniscriptInstructions::PushString>::loadInstruction(void *dest, uint32 instrFlags, Data::DataReader &instrDataReader, IMiniscriptInstructionParserFeedback &feedback) {
uint16 strLength;
if (!instrDataReader.readU16(strLength))
return false;
// Unlike most cases, in this case the string is null-terminated but the str length doesn't include the terminator
Common::String str;
if (!instrDataReader.readTerminatedStr(str, strLength + 1))
return false;
new (dest) MiniscriptInstructions::PushString(str);
return true;
}
struct SIMiniscriptInstructionFactory {
bool (*create)(void *dest, uint32 instrFlags, Data::DataReader &instrDataReader, MiniscriptInstruction *&outMiniscriptInstructionPtr, IMiniscriptInstructionParserFeedback &feedback);
void (*getSizeAndAlignment)(size_t &outSize, size_t &outAlignment);
};
template<class T>
class MiniscriptInstructionFactory {
public:
static bool create(void *dest, uint32 instrFlags, Data::DataReader &instrDataReader, MiniscriptInstruction *&outMiniscriptInstructionPtr, IMiniscriptInstructionParserFeedback &feedback);
static void getSizeAndAlignment(size_t &outSize, size_t &outAlignment);
static SIMiniscriptInstructionFactory *getInstance();
private:
static SIMiniscriptInstructionFactory _instance;
};
template<class T>
bool MiniscriptInstructionFactory<T>::create(void *dest, uint32 instrFlags, Data::DataReader &instrDataReader, MiniscriptInstruction *&outMiniscriptInstructionPtr, IMiniscriptInstructionParserFeedback &feedback) {
if (!MiniscriptInstructionLoader<T>::loadInstruction(dest, instrFlags, instrDataReader, feedback))
return false;
outMiniscriptInstructionPtr = static_cast<MiniscriptInstruction *>(static_cast<T *>(dest));
return true;
}
template<class T>
void MiniscriptInstructionFactory<T>::getSizeAndAlignment(size_t &outSize, size_t &outAlignment) {
outSize = sizeof(T);
outAlignment = alignof(T);
}
template<class T>
inline SIMiniscriptInstructionFactory *MiniscriptInstructionFactory<T>::getInstance() {
return &_instance;
}
template<class T>
SIMiniscriptInstructionFactory MiniscriptInstructionFactory<T>::_instance = {
MiniscriptInstructionFactory<T>::create,
MiniscriptInstructionFactory<T>::getSizeAndAlignment
};
MiniscriptParser::InstructionData::InstructionData()
: opcode(0), flags(0), pdPosition(0), instrFactory(nullptr) {
}
bool MiniscriptParser::parse(const Data::MiniscriptProgram &program, Common::SharedPtr<MiniscriptProgram> &outProgram, Common::SharedPtr<MiniscriptReferences> &outReferences) {
Common::Array<MiniscriptReferences::LocalRef> localRefs;
Common::Array<MiniscriptReferences::GlobalRef> globalRefs;
Common::Array<MiniscriptProgram::Attribute> attributes;
Common::SharedPtr<Common::Array<uint8> > programDataPtr;
Common::Array<MiniscriptInstruction *> miniscriptInstructions;
// If the program is empty then just return an empty program
if (program.bytecode.size() == 0 || program.numOfInstructions == 0) {
outProgram = Common::SharedPtr<MiniscriptProgram>(new MiniscriptProgram(programDataPtr, miniscriptInstructions, attributes));
outReferences = Common::SharedPtr<MiniscriptReferences>(new MiniscriptReferences(localRefs, globalRefs));
return true;
}
localRefs.resize(program.localRefs.size());
for (size_t i = 0; i < program.localRefs.size(); i++) {
localRefs[i].guid = program.localRefs[i].guid;
localRefs[i].name = program.localRefs[i].name;
}
attributes.resize(program.attributes.size());
for (size_t i = 0; i < program.attributes.size(); i++) {
attributes[i].name = program.attributes[i].name;
}
Common::MemoryReadStream stream(&program.bytecode[0], program.bytecode.size());
Data::DataReader reader(0, stream, program.dataFormat, kRuntimeVersion100, false);
Common::Array<InstructionData> rawInstructions;
rawInstructions.resize(program.numOfInstructions);
for (size_t i = 0; i < program.numOfInstructions; i++) {
InstructionData &rawInstruction = rawInstructions[i];
uint16 instrSize;
if (!reader.readU16(rawInstruction.opcode) || !reader.readU16(rawInstruction.flags) || !reader.readU16(instrSize))
return false;
if (instrSize < 6)
return false;
if (instrSize > 6) {
rawInstruction.contents.resize(instrSize - 6);
if (!reader.read(&rawInstruction.contents[0], instrSize - 6))
return false;
}
}
programDataPtr.reset(new Common::Array<uint8>());
Common::Array<uint8> &programData = *programDataPtr.get();
// Find out how much space we need and place instructions
size_t maxAlignment = 1;
size_t programDataSize = 0;
for (size_t i = 0; i < program.numOfInstructions; i++) {
InstructionData &rawInstruction = rawInstructions[i];
SIMiniscriptInstructionFactory *factory = resolveOpcode(rawInstruction.opcode);
rawInstruction.instrFactory = factory;
if (!factory)
return false;
size_t compiledSize = 0;
size_t compiledAlignment = 0;
factory->getSizeAndAlignment(compiledSize, compiledAlignment);
if (programDataSize % compiledAlignment != 0)
programDataSize += (compiledAlignment - (programDataSize % compiledAlignment));
rawInstruction.pdPosition = programDataSize;
programDataSize += compiledSize;
if (maxAlignment < compiledAlignment)
maxAlignment = compiledAlignment;
}
programData.resize(programDataSize + maxAlignment - 1);
uintptr programDataAddress = reinterpret_cast<uintptr>(&programData[0]);
size_t baseOffset = 0;
if (programDataAddress % maxAlignment != 0)
baseOffset = (maxAlignment - (programDataSize % maxAlignment));
miniscriptInstructions.resize(program.numOfInstructions);
MiniscriptInstructionParserFeedback parserFeedback(&globalRefs);
// Create instructions
for (size_t i = 0; i < program.numOfInstructions; i++) {
const InstructionData &rawInstruction = rawInstructions[i];
const void *dataLoc = nullptr;
if (rawInstruction.contents.size() != 0)
dataLoc = &rawInstruction.contents[0];
Common::MemoryReadStream instrContentsStream(static_cast<const byte *>(dataLoc), rawInstruction.contents.size());
Data::DataReader instrContentsReader(0, instrContentsStream, reader.getDataFormat(), reader.getRuntimeVersion(), reader.isVersionAutoDetect());
if (!rawInstruction.instrFactory->create(&programData[baseOffset + rawInstruction.pdPosition], rawInstruction.flags, instrContentsReader, miniscriptInstructions[i], parserFeedback)) {
// Destroy any already-created instructions
for (size_t di = 0; di < i; di++) {
miniscriptInstructions[i - 1 - di]->~MiniscriptInstruction();
}
return false;
}
}
// Done
outProgram = Common::SharedPtr<MiniscriptProgram>(new MiniscriptProgram(programDataPtr, miniscriptInstructions, attributes));
outReferences = Common::SharedPtr<MiniscriptReferences>(new MiniscriptReferences(localRefs, globalRefs));
return true;
}
SIMiniscriptInstructionFactory *MiniscriptParser::resolveOpcode(uint16 opcode) {
switch (opcode) {
case 0x834:
return MiniscriptInstructionFactory<MiniscriptInstructions::Set>::getInstance();
case 0x898:
return MiniscriptInstructionFactory<MiniscriptInstructions::Send>::getInstance();
case 0xc9:
return MiniscriptInstructionFactory<MiniscriptInstructions::Add>::getInstance();
case 0xca:
return MiniscriptInstructionFactory<MiniscriptInstructions::Sub>::getInstance();
case 0xcb:
return MiniscriptInstructionFactory<MiniscriptInstructions::Mul>::getInstance();
case 0xcc:
return MiniscriptInstructionFactory<MiniscriptInstructions::Div>::getInstance();
case 0xcd:
return MiniscriptInstructionFactory<MiniscriptInstructions::Pow>::getInstance();
case 0xce:
return MiniscriptInstructionFactory<MiniscriptInstructions::And>::getInstance();
case 0xcf:
return MiniscriptInstructionFactory<MiniscriptInstructions::Or>::getInstance();
case 0xd0:
return MiniscriptInstructionFactory<MiniscriptInstructions::Neg>::getInstance();
case 0xd1:
return MiniscriptInstructionFactory<MiniscriptInstructions::Not>::getInstance();
case 0xd2:
return MiniscriptInstructionFactory<MiniscriptInstructions::CmpEqual>::getInstance();
case 0xd3:
return MiniscriptInstructionFactory<MiniscriptInstructions::CmpNotEqual>::getInstance();
case 0xd4:
return MiniscriptInstructionFactory<MiniscriptInstructions::CmpLessOrEqual>::getInstance();
case 0xd5:
return MiniscriptInstructionFactory<MiniscriptInstructions::CmpLess>::getInstance();
case 0xd6:
return MiniscriptInstructionFactory<MiniscriptInstructions::CmpGreaterOrEqual>::getInstance();
case 0xd7:
return MiniscriptInstructionFactory<MiniscriptInstructions::CmpGreater>::getInstance();
case 0xd8:
return MiniscriptInstructionFactory<MiniscriptInstructions::BuiltinFunc>::getInstance();
case 0xd9:
return MiniscriptInstructionFactory<MiniscriptInstructions::DivInt>::getInstance();
case 0xda:
return MiniscriptInstructionFactory<MiniscriptInstructions::Modulo>::getInstance();
case 0xdb:
return MiniscriptInstructionFactory<MiniscriptInstructions::StrConcat>::getInstance();
case 0x12f:
return MiniscriptInstructionFactory<MiniscriptInstructions::PointCreate>::getInstance();
case 0x130:
return MiniscriptInstructionFactory<MiniscriptInstructions::RangeCreate>::getInstance();
case 0x131:
return MiniscriptInstructionFactory<MiniscriptInstructions::VectorCreate>::getInstance();
case 0x135:
return MiniscriptInstructionFactory<MiniscriptInstructions::GetChild>::getInstance();
case 0x136:
return MiniscriptInstructionFactory<MiniscriptInstructions::ListAppend>::getInstance();
case 0x137:
return MiniscriptInstructionFactory<MiniscriptInstructions::ListCreate>::getInstance();
case 0x191:
return MiniscriptInstructionFactory<MiniscriptInstructions::PushValue>::getInstance();
case 0x192:
return MiniscriptInstructionFactory<MiniscriptInstructions::PushGlobal>::getInstance();
case 0x193:
return MiniscriptInstructionFactory<MiniscriptInstructions::PushString>::getInstance();
case 0x7d3:
return MiniscriptInstructionFactory<MiniscriptInstructions::Jump>::getInstance();
default:
return nullptr;
}
}
namespace MiniscriptInstructions {
MiniscriptInstructionOutcome UnimplementedInstruction::execute(MiniscriptThread *thread) const {
thread->error("Unimplemented instruction");
return kMiniscriptInstructionOutcomeFailed;
}
MiniscriptInstructionOutcome Set::execute(MiniscriptThread *thread) const {
if (thread->getStackSize() != 2) {
// Sets are only allowed when they would empty the stack
thread->error("Invalid stack state for set instruction");
return kMiniscriptInstructionOutcomeFailed;
}
// Convert value
MiniscriptInstructionOutcome outcome = kMiniscriptInstructionOutcomeContinue;
const MiniscriptStackValue &srcValue = thread->getStackValueFromTop(0);
MiniscriptStackValue &target = thread->getStackValueFromTop(1);
if (target.value.getType() == DynamicValueTypes::kWriteProxy) {
const DynamicValueWriteProxy &proxy = target.value.getWriteProxy();
outcome = proxy.pod.ifc->write(thread, srcValue.value, proxy.pod.objectRef, proxy.pod.ptrOrOffset);
if (outcome == kMiniscriptInstructionOutcomeFailed) {
thread->error("Failed to assign value to proxy");
return outcome;
}
} else {
VariableModifier *var = nullptr;
if (target.value.getType() == DynamicValueTypes::kObject) {
Common::SharedPtr<RuntimeObject> obj = target.value.getObject().object.lock();
if (obj && obj->isModifier() && static_cast<const Modifier *>(obj.get())->isVariable())
var = static_cast<VariableModifier *>(obj.get());
}
if (var != nullptr) {
(void)var->varSetValue(thread, srcValue.value);
} else {
thread->error("Can't assign to rvalue");
return kMiniscriptInstructionOutcomeFailed;
}
}
thread->popValues(2);
return outcome;
}
Send::Send(const Event &evt, const MessageFlags &messageFlags) : _evt(evt), _messageFlags(messageFlags) {
}
MiniscriptInstructionOutcome Send::execute(MiniscriptThread *thread) const {
if (thread->getStackSize() != 2) {
// Send instructions are only allowed if they empty the stack
thread->error("Invalid stack state for send instruction");
return kMiniscriptInstructionOutcomeFailed;
}
DynamicValue &targetValue = thread->getStackValueFromTop(0).value;
DynamicValue &payloadValue = thread->getStackValueFromTop(1).value;
if (targetValue.getType() != DynamicValueTypes::kObject) {
// Failed sends are non-fatal (Obsidian requires this in the aircraft propulsion room to enter the propulsion puzzle)
warning("Invalid message destination (target isn't an object reference)");
thread->popValues(2);
return kMiniscriptInstructionOutcomeContinue;
}
Common::SharedPtr<RuntimeObject> obj = targetValue.getObject().object.lock();
if (!obj) {
// Obsidian also triggers NAV_Restart on Project Started, which triggers "<init globals> on NAV_Restart"
// which sends PRG_Toggle_Status_Display to sharedScene, even though at that point there is no shared scene.
warning("Invalid message destination (target object is invalid)");
thread->popValues(2);
return kMiniscriptInstructionOutcomeContinue;
}
Common::SharedPtr<MessageProperties> msgProps(new MessageProperties(_evt, payloadValue, thread->getModifier()->getSelfReference()));
Common::SharedPtr<MessageDispatch> dispatch;
if (obj->isModifier())
dispatch.reset(new MessageDispatch(msgProps, static_cast<Modifier *>(obj.get()), _messageFlags.cascade, _messageFlags.relay, true));
else if (obj->isStructural())
dispatch.reset(new MessageDispatch(msgProps, static_cast<Structural *>(obj.get()), _messageFlags.cascade, _messageFlags.relay, true));
else {
warning("Invalid message destination (target object is not a modifier or structural object)");
return kMiniscriptInstructionOutcomeContinue;
}
thread->popValues(2);
if (_messageFlags.immediate) {
thread->getRuntime()->sendMessageOnVThread(dispatch);
return kMiniscriptInstructionOutcomeYieldToVThread;
} else {
thread->getRuntime()->queueMessage(dispatch);
return kMiniscriptInstructionOutcomeContinue;
}
}
MiniscriptInstructionOutcome BinaryArithInstruction::execute(MiniscriptThread *thread) const {
if (thread->getStackSize() < 2) {
thread->error("Stack underflow");
return kMiniscriptInstructionOutcomeFailed;
}
MiniscriptInstructionOutcome outcome = thread->dereferenceRValue(0);
if (outcome != kMiniscriptInstructionOutcomeContinue)
return outcome;
outcome = thread->dereferenceRValue(1);
if (outcome != kMiniscriptInstructionOutcomeContinue)
return outcome;
DynamicValue &rs = thread->getStackValueFromTop(0).value;
DynamicValue &lsDest = thread->getStackValueFromTop(1).value;
if (lsDest.getType() == DynamicValueTypes::kPoint && rs.getType() == DynamicValueTypes::kPoint) {
Common::Point lsPoint = lsDest.getPoint();
Common::Point rsPoint = rs.getPoint();
double resultX = 0.0;
double resultY = 0.0;
outcome = arithExecute(thread, resultX, lsPoint.x, rsPoint.x);
if (outcome != kMiniscriptInstructionOutcomeContinue)
return outcome;
outcome = arithExecute(thread, resultY, lsPoint.y, rsPoint.y);
if (outcome != kMiniscriptInstructionOutcomeContinue)
return outcome;
lsDest.setPoint(Common::Point(static_cast<int16>(round(resultX)), static_cast<int16>(round(resultY))));
} else {
double leftVal = 0.0;
switch (lsDest.getType()) {
case DynamicValueTypes::kInteger:
leftVal = lsDest.getInt();
break;
case DynamicValueTypes::kFloat:
leftVal = lsDest.getFloat();
break;
case DynamicValueTypes::kBoolean:
leftVal = lsDest.getBool() ? 1.0 : 0.0;
break;
default:
thread->error("Invalid left-side type for binary arithmetic operator");
return kMiniscriptInstructionOutcomeFailed;
}
double rightVal = 0.0;
switch (rs.getType()) {
case DynamicValueTypes::kInteger:
rightVal = rs.getInt();
break;
case DynamicValueTypes::kFloat:
rightVal = rs.getFloat();
break;
case DynamicValueTypes::kBoolean:
rightVal = rs.getBool() ? 1.0 : 0.0;
break;
default:
thread->error("Invalid right-side type for binary arithmetic operator");
return kMiniscriptInstructionOutcomeFailed;
}
double result = 0.0;
outcome = arithExecute(thread, result, leftVal, rightVal);
if (outcome != kMiniscriptInstructionOutcomeContinue)
return outcome;
lsDest.setFloat(result);
}
thread->popValues(1);
return kMiniscriptInstructionOutcomeContinue;
}
MiniscriptInstructionOutcome Add::arithExecute(MiniscriptThread *thread, double &result, double left, double right) const {
result = left + right;
return kMiniscriptInstructionOutcomeContinue;
}
MiniscriptInstructionOutcome Sub::arithExecute(MiniscriptThread *thread, double &result, double left, double right) const {
result = left - right;
return kMiniscriptInstructionOutcomeContinue;
}
MiniscriptInstructionOutcome Mul::arithExecute(MiniscriptThread *thread, double &result, double left, double right) const {
result = left * right;
return kMiniscriptInstructionOutcomeContinue;
}
MiniscriptInstructionOutcome Div::arithExecute(MiniscriptThread *thread, double &result, double left, double right) const {
if (right == 0.0) {
thread->error("Arithmetic error: Division by zero");
return kMiniscriptInstructionOutcomeFailed;
}
result = left / right;
return kMiniscriptInstructionOutcomeContinue;
}
MiniscriptInstructionOutcome Pow::arithExecute(MiniscriptThread *thread, double &result, double left, double right) const {
if (left < 0.0 && right != floor(right)) {
thread->error("Arithmetic error: Left side is negative but right side is not an integer");
return kMiniscriptInstructionOutcomeFailed;
}
result = pow(left, right);
return kMiniscriptInstructionOutcomeContinue;
}
MiniscriptInstructionOutcome DivInt::arithExecute(MiniscriptThread *thread, double &result, double left, double right) const {
if (right == 0.0) {
thread->error("Arithmetic error: Integer division by zero");
return kMiniscriptInstructionOutcomeFailed;
}
result = floor(left / right);
return kMiniscriptInstructionOutcomeContinue;
}
MiniscriptInstructionOutcome Modulo::arithExecute(MiniscriptThread *thread, double &result, double left, double right) const {
if (right == 0.0) {
thread->error("Arithmetic error: Modulo division by zero");
return kMiniscriptInstructionOutcomeFailed;
}
// fmod keeps the sign from the left operand, but mTropolis modulo keeps the
// sign of the right operand.
double r = fmod(left, right);
if (signbit(left) != signbit(right)) {
if (r == 0.0)
r = copysign(0.0, right);
else
r += right;
}
result = r;
return kMiniscriptInstructionOutcomeContinue;
}
MiniscriptInstructionOutcome UnorderedCompareInstruction::execute(MiniscriptThread *thread) const {
if (thread->getStackSize() < 2) {
thread->error("Stack underflow");
return kMiniscriptInstructionOutcomeFailed;
}
MiniscriptInstructionOutcome outcome = thread->dereferenceRValue(0);
if (outcome != kMiniscriptInstructionOutcomeContinue)
return outcome;
outcome = thread->dereferenceRValue(1);
if (outcome != kMiniscriptInstructionOutcomeContinue)
return outcome;
DynamicValue &rs = thread->getStackValueFromTop(0).value;
DynamicValue &lsDest = thread->getStackValueFromTop(1).value;
bool isEqual = false;
bool isUndefined = false;
switch (lsDest.getType()) {
case DynamicValueTypes::kString:
if (rs.getType() == DynamicValueTypes::kString)
isEqual = caseInsensitiveEqual(lsDest.getString(), rs.getString());
break;
case DynamicValueTypes::kBoolean: {
switch (rs.getType()) {
case DynamicValueTypes::kInteger:
isEqual = (rs.getInt() == (lsDest.getBool() ? 1 : 0));
break;
case DynamicValueTypes::kFloat:
isEqual = (rs.getFloat() == (lsDest.getBool() ? 1.0 : 0.0));
break;
case DynamicValueTypes::kBoolean:
isEqual = (rs.getBool() == lsDest.getBool());
break;
default:
isEqual = (lsDest.getBool() == false);
break;
}
} break;
case DynamicValueTypes::kFloat: {
if (isnan(lsDest.getFloat()))
isUndefined = true;
else {
switch (rs.getType()) {
case DynamicValueTypes::kInteger:
isEqual = (rs.getInt() == lsDest.getFloat());
break;
case DynamicValueTypes::kFloat:
if (isnan(rs.getFloat()))
isUndefined = true;
else
isEqual = (rs.getFloat() == lsDest.getFloat());
break;
case DynamicValueTypes::kBoolean:
isEqual = ((rs.getBool() ? 1.0 : 0.0) == lsDest.getFloat());
break;
default:
isEqual = false;
break;
}
}
} break;
case DynamicValueTypes::kInteger: {
switch (rs.getType()) {
case DynamicValueTypes::kInteger:
isEqual = (rs.getInt() == lsDest.getInt());
break;
case DynamicValueTypes::kFloat:
if (isnan(rs.getFloat()))
isUndefined = true;
else
isEqual = (rs.getFloat() == lsDest.getInt());
break;
case DynamicValueTypes::kBoolean:
isEqual = ((rs.getBool() ? 1 : 0) == lsDest.getInt());
break;
default:
isEqual = false;
break;
}
} break;
case DynamicValueTypes::kLabel: {
// Really not sure how this works but there are buggy scripts in Obsidian which
// were probably written as "if loop = false then ..." except Miniscript resolved
// "loop" as a sound marker (!) because a sound marker with that name exists instead
// of resolving it as equivalent to element.loop as it usually would.
// Strict equality checks prevent the "GEN_Streaming_Update on ALC" script from
// working, which prevents the bqtstreaming and bstreaming flags from being cleared,
// causing, among other things, the player to get stuck after the Forest->Bureau
// transition because the stuck streaming flags are blocking the VO.
if (rs.getType() == DynamicValueTypes::kBoolean)
isEqual = !rs.getBool();
else
isEqual = (lsDest == rs);
} break;
default:
isEqual = (lsDest == rs);
break;
}
lsDest.setBool(isUndefined == false && this->resolve(isEqual));
thread->popValues(1);
return kMiniscriptInstructionOutcomeContinue;
}
MiniscriptInstructionOutcome And::execute(MiniscriptThread *thread) const {
if (thread->getStackSize() < 2) {
thread->error("Stack underflow");
return kMiniscriptInstructionOutcomeFailed;
}
MiniscriptInstructionOutcome outcome = thread->dereferenceRValue(0);
if (outcome != kMiniscriptInstructionOutcomeContinue)
return outcome;
outcome = thread->dereferenceRValue(1);
if (outcome != kMiniscriptInstructionOutcomeContinue)
return outcome;
DynamicValue &rs = thread->getStackValueFromTop(0).value;
DynamicValue &lsDest = thread->getStackValueFromTop(1).value;
lsDest.setBool(miniscriptEvaluateTruth(lsDest) && miniscriptEvaluateTruth(rs));
thread->popValues(1);
return kMiniscriptInstructionOutcomeContinue;
}
MiniscriptInstructionOutcome Or::execute(MiniscriptThread *thread) const {
if (thread->getStackSize() < 2) {
thread->error("Stack underflow");
return kMiniscriptInstructionOutcomeFailed;
}
MiniscriptInstructionOutcome outcome = thread->dereferenceRValue(0);
if (outcome != kMiniscriptInstructionOutcomeContinue)
return outcome;
outcome = thread->dereferenceRValue(1);
if (outcome != kMiniscriptInstructionOutcomeContinue)
return outcome;
DynamicValue &rs = thread->getStackValueFromTop(0).value;
DynamicValue &lsDest = thread->getStackValueFromTop(1).value;
lsDest.setBool(miniscriptEvaluateTruth(lsDest) || miniscriptEvaluateTruth(rs));
thread->popValues(1);
return kMiniscriptInstructionOutcomeContinue;
}
MiniscriptInstructionOutcome Neg::execute(MiniscriptThread *thread) const {
if (thread->getStackSize() < 1) {
thread->error("Stack underflow");
return kMiniscriptInstructionOutcomeFailed;
}
MiniscriptInstructionOutcome outcome = thread->dereferenceRValue(0);
if (outcome != kMiniscriptInstructionOutcomeContinue)
return outcome;
DynamicValue &value = thread->getStackValueFromTop(0).value;
switch (value.getType()) {
case DynamicValueTypes::kFloat:
value.setFloat(-value.getFloat());
break;
case DynamicValueTypes::kInteger: {
int32 i = value.getInt();
if (i == (0 - 1 -0x7fffffff))
value.setFloat(-static_cast<double>(i));
else
value.setInt(-i);
} break;
default:
thread->error("Couldn't negate a value of a non-numeric type");
return kMiniscriptInstructionOutcomeFailed;
}
return kMiniscriptInstructionOutcomeContinue;
}
MiniscriptInstructionOutcome Not::execute(MiniscriptThread *thread) const {
if (thread->getStackSize() < 1) {
thread->error("Stack underflow");
return kMiniscriptInstructionOutcomeFailed;
}
MiniscriptInstructionOutcome outcome = thread->dereferenceRValue(0);
if (outcome != kMiniscriptInstructionOutcomeContinue)
return outcome;
DynamicValue &value = thread->getStackValueFromTop(0).value;
value.setBool(!miniscriptEvaluateTruth(value));
return kMiniscriptInstructionOutcomeContinue;
}
MiniscriptInstructionOutcome OrderedCompareInstruction::execute(MiniscriptThread *thread) const {
if (thread->getStackSize() < 2) {
thread->error("Stack underflow");
return kMiniscriptInstructionOutcomeFailed;
}
MiniscriptInstructionOutcome outcome = thread->dereferenceRValue(0);
if (outcome != kMiniscriptInstructionOutcomeContinue)
return outcome;
outcome = thread->dereferenceRValue(1);
if (outcome != kMiniscriptInstructionOutcomeContinue)
return outcome;
DynamicValue &rs = thread->getStackValueFromTop(0).value;
DynamicValue &lsDest = thread->getStackValueFromTop(1).value;
double leftValue = 0.0;
double rightValue = 0.0;
if (lsDest.getType() == DynamicValueTypes::kFloat)
leftValue = lsDest.getFloat();
else if (lsDest.getType() == DynamicValueTypes::kInteger)
leftValue = lsDest.getInt();
else {
thread->error("Left-side value is invalid for comparison");
return kMiniscriptInstructionOutcomeFailed;
}
if (rs.getType() == DynamicValueTypes::kFloat)
rightValue = rs.getFloat();
else if (rs.getType() == DynamicValueTypes::kInteger)
rightValue = rs.getInt();
else {
thread->error("Right-side value is invalid for comparison");
return kMiniscriptInstructionOutcomeFailed;
}
lsDest.setBool(this->compareFloat(leftValue, rightValue));
thread->popValues(1);
return kMiniscriptInstructionOutcomeContinue;
}
BuiltinFunc::BuiltinFunc(BuiltinFunctionID bfid) : _funcID(bfid) {
}
MiniscriptInstructionOutcome BuiltinFunc::execute(MiniscriptThread *thread) const {
size_t stackArgsNeeded = 1;
bool returnsValue = true;
if (thread->getStackSize() < stackArgsNeeded) {
thread->error("Stack underflow");
return kMiniscriptInstructionOutcomeFailed;
}
for (size_t i = 0; i < stackArgsNeeded; i++) {
MiniscriptInstructionOutcome outcome = thread->dereferenceRValue(i);
if (outcome != kMiniscriptInstructionOutcomeContinue)
return outcome;
}
DynamicValue staticDest;
DynamicValue *dest = nullptr;
if (returnsValue) {
if (stackArgsNeeded > 0)
dest = &thread->getStackValueFromTop(stackArgsNeeded - 1).value;
else
dest = &staticDest;
}
MiniscriptInstructionOutcome outcome = executeFunction(thread, dest);
if (outcome != kMiniscriptInstructionOutcomeContinue)
return outcome;
if (stackArgsNeeded > 0) {
size_t valuesToPop = stackArgsNeeded;
if (returnsValue)
valuesToPop--;
if (valuesToPop > 0) {
// coverity[dead_error_line]
thread->popValues(valuesToPop);
}
} else {
// coverity[dead_error_line]
if (returnsValue)
thread->pushValue(staticDest);
}
return kMiniscriptInstructionOutcomeContinue;
}
MiniscriptInstructionOutcome BuiltinFunc::executeFunction(MiniscriptThread *thread, DynamicValue *returnValue) const {
switch (_funcID) {
case kSin:
case kCos:
case kRandom:
case kSqrt:
case kTan:
case kAbs:
case kSign:
case kArctangent:
case kExp:
case kLn:
case kLog:
case kCosH:
case kSinH:
case kTanH:
case kTrunc:
case kRound:
return executeSimpleNumericInstruction(thread, returnValue);
case kRect2Polar:
return executeRectToPolar(thread, returnValue);
case kPolar2Rect:
return executePolarToRect(thread, returnValue);
case kNum2Str:
return executeNum2Str(thread, returnValue);
case kStr2Num:
return executeStr2Num(thread, returnValue);
default:
thread->error("Unimplemented built-in function");
return kMiniscriptInstructionOutcomeFailed;
}
}
MiniscriptInstructionOutcome BuiltinFunc::executeSimpleNumericInstruction(MiniscriptThread *thread, DynamicValue *returnValue) const {
double result = 0.0;
double input = 0.0;
const DynamicValue &inputDynamicValue = thread->getStackValueFromTop(0).value;
switch (inputDynamicValue.getType()) {
case DynamicValueTypes::kInteger:
input = inputDynamicValue.getInt();
break;
case DynamicValueTypes::kFloat:
input = inputDynamicValue.getFloat();
break;
default:
thread->error("Invalid numeric function input type");
return kMiniscriptInstructionOutcomeFailed;
}
switch (_funcID) {
case kSin:
result = sin(input * (M_PI / 180.0));
break;
case kCos:
result = cos(input * (M_PI / 180.0));
break;
case kRandom:
if (input < 1.5)
result = 0.0;
else {
uint rngMax = static_cast<uint>(floor(input + 0.5)) - 1;
result = thread->getRuntime()->getRandom()->getRandomNumber(rngMax);
}
break;
case kSqrt:
result = sqrt(input);
break;
case kTan:
result = tan(input * (M_PI / 180.0));
break;
case kAbs:
result = fabs(input);
break;
case kSign:
if (input < 0.0)
result = -1;
else if (input > 0.0)
result = 1;
else
result = 0;
break;
case kArctangent:
result = atan(input) * (180.0 / M_PI);
break;
case kExp:
result = exp(input);
break;
case kLn:
result = log(input);
break;
case kLog:
result = log10(input);
break;
case kCosH:
result = cosh(input * (M_PI / 180.0));
break;
case kSinH:
result = sinh(input * (M_PI / 180.0));
break;
case kTanH:
result = tanh(input * (M_PI / 180.0));
break;
case kTrunc:
result = trunc(input);
break;
case kRound:
result = round(input);
break;
default:
thread->error("Unimplemented numeric function");
return kMiniscriptInstructionOutcomeFailed;
}
returnValue->setFloat(result);
return kMiniscriptInstructionOutcomeContinue;
}
MiniscriptInstructionOutcome BuiltinFunc::executeRectToPolar(MiniscriptThread *thread, DynamicValue *returnValue) const {
const DynamicValue &inputDynamicValue = thread->getStackValueFromTop(0).value;
if (inputDynamicValue.getType() != DynamicValueTypes::kPoint) {
thread->error("Polar to rect input must be a vector");
return kMiniscriptInstructionOutcomeFailed;
}
Common::Point pt = inputDynamicValue.getPoint();
double angle = atan2(pt.y, pt.x);
double magnitude = sqrt(pt.x * pt.x + pt.y * pt.y);
returnValue->setVector(AngleMagVector::createRadians(angle, magnitude));
return kMiniscriptInstructionOutcomeContinue;
}
MiniscriptInstructionOutcome BuiltinFunc::executePolarToRect(MiniscriptThread *thread, DynamicValue *returnValue) const {
const DynamicValue &inputDynamicValue = thread->getStackValueFromTop(0).value;
if (inputDynamicValue.getType() != DynamicValueTypes::kVector) {
thread->error("Polar to rect input must be a vector");
return kMiniscriptInstructionOutcomeFailed;
}
const AngleMagVector &vec = inputDynamicValue.getVector();
double x = cos(vec.angleDegrees * (M_PI / 180.0)) * vec.magnitude;
double y = sin(vec.angleDegrees * (M_PI / 180.0)) * vec.magnitude;
returnValue->setPoint(Common::Point(static_cast<int16>(round(x)), static_cast<int16>(round(y))));
return kMiniscriptInstructionOutcomeContinue;
}
MiniscriptInstructionOutcome BuiltinFunc::executeNum2Str(MiniscriptThread *thread, DynamicValue *returnValue) const {
Common::String result;
const DynamicValue &inputDynamicValue = thread->getStackValueFromTop(0).value;
switch (inputDynamicValue.getType()) {
case DynamicValueTypes::kInteger:
result = Common::String::format("%i", static_cast<int>(inputDynamicValue.getInt()));
break;
case DynamicValueTypes::kFloat:
result = Common::String::format("%g", static_cast<double>(inputDynamicValue.getFloat()));
break;
default:
thread->error("Invalid input value to num2str");
return kMiniscriptInstructionOutcomeFailed;
}
returnValue->setString(result);
return kMiniscriptInstructionOutcomeContinue;
}
MiniscriptInstructionOutcome BuiltinFunc::executeStr2Num(MiniscriptThread *thread, DynamicValue *returnValue) const {
double result = 0.0;
const DynamicValue &inputDynamicValue = thread->getStackValueFromTop(0).value;
if (inputDynamicValue.getType() != DynamicValueTypes::kString) {
thread->error("Invalid input value to str2num");
return kMiniscriptInstructionOutcomeFailed;
}
const Common::String &str = inputDynamicValue.getString();
if (str.empty())
result = 0.0;
else if (!sscanf(str.c_str(), "%lf", &result)) {
// NOTE: sscanf will properly handle cases where a number is followed by a non-numeric value,
// which is consistent with mTropolis' behavior.
//
// If it fails, the result is 0.0 and no script error.
//
// This includes a case in Obsidian where it tries to parse in invalid room number "L100"
// upon entering the sky face for the first time in Bureau.
#ifdef MTROPOLIS_DEBUG_ENABLE
if (Debugger *debugger = thread->getRuntime()->debugGetDebugger())
debugger->notify(kDebugSeverityWarning, Common::String::format("Failed to parse '%s' as a number", str.c_str()));
#endif
result = 0.0;
}
returnValue->setFloat(result);
return kMiniscriptInstructionOutcomeContinue;
}
MiniscriptInstructionOutcome StrConcat::execute(MiniscriptThread *thread) const {
if (thread->getStackSize() < 2) {
thread->error("Stack underflow");
return kMiniscriptInstructionOutcomeFailed;
}
MiniscriptInstructionOutcome outcome = thread->dereferenceRValue(0);
if (outcome != kMiniscriptInstructionOutcomeContinue)
return outcome;
outcome = thread->dereferenceRValue(1);
if (outcome != kMiniscriptInstructionOutcomeContinue)
return outcome;
DynamicValue &rVal = thread->getStackValueFromTop(0).value;
DynamicValue &lValDest = thread->getStackValueFromTop(1).value;
if (rVal.getType() != DynamicValueTypes::kString) {
thread->error("String concat right side was not a string");
return kMiniscriptInstructionOutcomeFailed;
}
if (lValDest.getType() != DynamicValueTypes::kString) {
thread->error("String concat left side was not a string");
return kMiniscriptInstructionOutcomeFailed;
}
lValDest.setString(lValDest.getString() + rVal.getString());
thread->popValues(1);
return kMiniscriptInstructionOutcomeContinue;
}
MiniscriptInstructionOutcome PointCreate::execute(MiniscriptThread *thread) const {
if (thread->getStackSize() < 2) {
thread->error("Stack underflow");
return kMiniscriptInstructionOutcomeFailed;
}
MiniscriptInstructionOutcome outcome = thread->dereferenceRValue(0);
if (outcome != kMiniscriptInstructionOutcomeContinue)
return outcome;
outcome = thread->dereferenceRValue(1);
if (outcome != kMiniscriptInstructionOutcomeContinue)
return outcome;
DynamicValue &yVal = thread->getStackValueFromTop(0).value;
DynamicValue &xValDest = thread->getStackValueFromTop(1).value;
int16 coords[2];
DynamicValue *coordInputs[2] = {&xValDest, &yVal};
for (int i = 0; i < 2; i++) {
DynamicValue *v = coordInputs[i];
DynamicValue listContents;
if (v->getType() == DynamicValueTypes::kList) {
// Yes this is actually allowed
const Common::SharedPtr<DynamicList> &list = v->getList();
if (list->getSize() != 1 || !list->getAtIndex(0, listContents)) {
thread->error("Can't convert list to integer");
return kMiniscriptInstructionOutcomeFailed;
}
v = &listContents;
}
switch (v->getType()) {
case DynamicValueTypes::kFloat:
coords[i] = static_cast<int16>(floor(v->getFloat() + 0.5)) & 0xffff;
break;
case DynamicValueTypes::kInteger:
coords[i] = static_cast<int16>(v->getInt());
break;
case DynamicValueTypes::kBoolean:
coords[i] = (v->getBool()) ? 1 : 0;
break;
default:
thread->error("Invalid input for point creation");
return kMiniscriptInstructionOutcomeFailed;
}
}
xValDest.setPoint(Common::Point(coords[0], coords[1]));
thread->popValues(1);
return kMiniscriptInstructionOutcomeContinue;
}
MiniscriptInstructionOutcome RangeCreate::execute(MiniscriptThread *thread) const {
if (thread->getStackSize() < 2) {
thread->error("Stack underflow");
return kMiniscriptInstructionOutcomeFailed;
}
MiniscriptInstructionOutcome outcome = thread->dereferenceRValue(0);
if (outcome != kMiniscriptInstructionOutcomeContinue)
return outcome;
outcome = thread->dereferenceRValue(1);
if (outcome != kMiniscriptInstructionOutcomeContinue)
return outcome;
DynamicValue &yVal = thread->getStackValueFromTop(0).value;
DynamicValue &xValDest = thread->getStackValueFromTop(1).value;
int32 coords[2];
DynamicValue *coordInputs[2] = {&xValDest, &yVal};
for (int i = 0; i < 2; i++) {
DynamicValue *v = coordInputs[i];
DynamicValue listContents;
if (v->getType() == DynamicValueTypes::kList) {
// Yes this is actually allowed
const Common::SharedPtr<DynamicList> &list = v->getList();
if (list->getSize() != 1 || !list->getAtIndex(0, listContents)) {
thread->error("Can't convert list to integer");
return kMiniscriptInstructionOutcomeFailed;
}
v = &listContents;
}
switch (v->getType()) {
case DynamicValueTypes::kFloat:
coords[i] = static_cast<int32>(floor(v->getFloat() + 0.5));
break;
case DynamicValueTypes::kInteger:
coords[i] = v->getInt();
break;
case DynamicValueTypes::kBoolean:
coords[i] = (v->getBool()) ? 1 : 0;
break;
default:
thread->error("Invalid input for point creation");
return kMiniscriptInstructionOutcomeFailed;
}
}
xValDest.setIntRange(IntRange(coords[0], coords[1]));
thread->popValues(1);
return kMiniscriptInstructionOutcomeContinue;
}
GetChild::GetChild(uint32 attribute, bool isLValue, bool isIndexed)
: _attribute(attribute), _isLValue(isLValue), _isIndexed(isIndexed) {
}
MiniscriptInstructionOutcome GetChild::execute(MiniscriptThread *thread) const {
const Common::Array<MiniscriptProgram::Attribute> &attribs = thread->getProgram()->getAttributes();
if (_attribute >= attribs.size()) {
thread->error("Invalid attribute index");
return kMiniscriptInstructionOutcomeFailed;
}
const Common::String &attrib = attribs[_attribute].name;
MiniscriptInstructionOutcome outcome = kMiniscriptInstructionOutcomeFailed;
if (_isIndexed) {
if (thread->getStackSize() < 2) {
thread->error("Stack underflow");
return kMiniscriptInstructionOutcomeFailed;
}
// Convert index
outcome = thread->dereferenceRValue(0);
if (outcome != kMiniscriptInstructionOutcomeContinue)
return outcome;
const MiniscriptStackValue &indexSlot = thread->getStackValueFromTop(0);
MiniscriptStackValue &indexableValueSlot = thread->getStackValueFromTop(1);
if (_isLValue) {
if (indexableValueSlot.value.getType() == DynamicValueTypes::kObject) {
Common::SharedPtr<RuntimeObject> obj = indexableValueSlot.value.getObject().object.lock();
if (!obj) {
thread->error("Tried to write '" + attrib + "' to an invalid object reference");
return kMiniscriptInstructionOutcomeFailed;
}
DynamicValueWriteProxy proxy;
outcome = obj->writeRefAttributeIndexed(thread, proxy, attrib, indexSlot.value);
if (outcome == kMiniscriptInstructionOutcomeFailed) {
thread->error("Failed to get a writeable reference to indexed attribute '" + attrib + "'");
return outcome;
}
indexableValueSlot.value.setWriteProxy(proxy);
} else if (indexableValueSlot.value.getType() == DynamicValueTypes::kWriteProxy) {
DynamicValueWriteProxy proxy = indexableValueSlot.value.getWriteProxy();
outcome = proxy.pod.ifc->refAttribIndexed(thread, proxy, proxy.pod.objectRef, proxy.pod.ptrOrOffset, attrib, indexSlot.value);
if (outcome == kMiniscriptInstructionOutcomeFailed) {
thread->error("Can't write to indexed attribute '" + attrib + "'");
return outcome;
}
indexableValueSlot.value.setWriteProxy(proxy);
} else {
thread->error("Tried to l-value index something that was not writeable");
return kMiniscriptInstructionOutcomeFailed;
}
} else {
outcome = readRValueAttribIndexed(thread, indexableValueSlot.value, attrib, indexSlot.value);
if (outcome != kMiniscriptInstructionOutcomeContinue)
return outcome;
}
thread->popValues(1);
} else {
if (thread->getStackSize() < 1) {
thread->error("Stack underflow");
return kMiniscriptInstructionOutcomeFailed;
}
MiniscriptStackValue &indexableValueSlot = thread->getStackValueFromTop(0);
if (_isLValue) {
if (indexableValueSlot.value.getType() == DynamicValueTypes::kObject) {
Common::SharedPtr<RuntimeObject> obj = indexableValueSlot.value.getObject().object.lock();
if (!obj) {
thread->error("Tried to indirect '" + attrib + "' using an invalid object reference");
return kMiniscriptInstructionOutcomeFailed;
}
DynamicValueWriteProxy writeProxy;
outcome = obj->writeRefAttribute(thread, writeProxy, attrib);
if (outcome == kMiniscriptInstructionOutcomeFailed) {
thread->error("Failed to get a writeable reference to attribute '" + attrib + "'");
return outcome;
}
indexableValueSlot.value.setWriteProxy(writeProxy);
} else if (indexableValueSlot.value.getType() == DynamicValueTypes::kWriteProxy) {
DynamicValueWriteProxy proxy = indexableValueSlot.value.getWriteProxy();
outcome = proxy.pod.ifc->refAttrib(thread, proxy, proxy.pod.objectRef, proxy.pod.ptrOrOffset, attrib);
if (outcome == kMiniscriptInstructionOutcomeFailed) {
thread->error("Can't write to attribute '" + attrib + "'");
return outcome;
}
indexableValueSlot.value.setWriteProxy(proxy);
} else {
thread->error("Tried to l-value index something that was not writeable");
return kMiniscriptInstructionOutcomeFailed;
}
} else {
outcome = readRValueAttrib(thread, indexableValueSlot.value, attrib);
}
}
return outcome;
}
MiniscriptInstructionOutcome GetChild::readRValueAttrib(MiniscriptThread *thread, DynamicValue &valueSrcDest, const Common::String &attrib) const {
switch (valueSrcDest.getType()) {
case DynamicValueTypes::kPoint:
if (attrib == "x")
valueSrcDest.setInt(valueSrcDest.getPoint().x);
else if (attrib == "y")
valueSrcDest.setInt(valueSrcDest.getPoint().y);
else {
thread->error("Point has no attribute '" + attrib + "'");
return kMiniscriptInstructionOutcomeFailed;
}
break;
case DynamicValueTypes::kIntegerRange:
if (attrib == "start")
valueSrcDest.setInt(valueSrcDest.getIntRange().min);
else if (attrib == "end")
valueSrcDest.setInt(valueSrcDest.getIntRange().max);
else {
thread->error("Integer range has no attribute '" + attrib + "'");
return kMiniscriptInstructionOutcomeFailed;
}
break;
case DynamicValueTypes::kVector:
if (attrib == "angle")
valueSrcDest.setFloat(valueSrcDest.getVector().angleDegrees);
else if (attrib == "magnitude")
valueSrcDest.setFloat(valueSrcDest.getVector().magnitude);
else {
thread->error("Vector has no attribute '" + attrib + "'");
return kMiniscriptInstructionOutcomeFailed;
}
break;
case DynamicValueTypes::kObject: {
Common::SharedPtr<RuntimeObject> obj = valueSrcDest.getObject().object.lock();
if (!obj) {
thread->error("Unable to read object attribute '" + attrib + "' from invalid object");
return kMiniscriptInstructionOutcomeFailed;
} else if (!obj->readAttribute(thread, valueSrcDest, attrib)) {
thread->error("Unable to read object attribute '" + attrib + "'");
return kMiniscriptInstructionOutcomeFailed;
}
} break;
case DynamicValueTypes::kList: {
Common::SharedPtr<DynamicList> list = valueSrcDest.getList();
if (attrib == "count") {
valueSrcDest.setInt(list->getSize());
} else {
thread->error("Unable to read list attribute '" + attrib + "'");
return kMiniscriptInstructionOutcomeFailed;
}
} break;
default:
thread->error("Unable to read attribute '" + attrib + "' from rvalue");
return kMiniscriptInstructionOutcomeFailed;
}
return kMiniscriptInstructionOutcomeContinue;
}
MiniscriptInstructionOutcome GetChild::readRValueAttribIndexed(MiniscriptThread *thread, DynamicValue &valueSrcDest, const Common::String &attrib, const DynamicValue &index) const {
switch (valueSrcDest.getType()) {
case DynamicValueTypes::kList:
if (attrib == "value") {
// Hold list ref since it may get released by the read operation
Common::SharedPtr<DynamicList> list = valueSrcDest.getList();
size_t realIndex = 0;
if (!DynamicList::dynamicValueToIndex(realIndex, index)) {
thread->error("Unable to list value at specified index");
return kMiniscriptInstructionOutcomeFailed;
}
if (!list->getAtIndex(realIndex, valueSrcDest)) {
thread->error("List read index out of bounds");
return kMiniscriptInstructionOutcomeFailed;
}
} else {
thread->error("Unable to read list attribute '" + attrib + "'");
return kMiniscriptInstructionOutcomeFailed;
}
break;
case DynamicValueTypes::kObject: {
Common::SharedPtr<RuntimeObject> obj = valueSrcDest.getObject().object.lock();
if (!obj) {
thread->error("Unable to read object indexed attribute '" + attrib + "' from invalid object");
return kMiniscriptInstructionOutcomeFailed;
} else if (!obj->readAttributeIndexed(thread, valueSrcDest, attrib, index)) {
thread->error("Unable to read object indexed attribute '" + attrib + "'");
return kMiniscriptInstructionOutcomeFailed;
}
} break;
default:
thread->error("Unable to read indexed rvalue attribute '" + attrib + "'");
return kMiniscriptInstructionOutcomeFailed;
}
return kMiniscriptInstructionOutcomeContinue;
}
PushValue::PushValue(DataType dataType, const void *value, bool isLValue)
: _dataType(dataType)/*, _isLValue(isLValue) */ {
switch (dataType) {
case DataType::kDataTypeBool:
new (static_cast<bool *>(&_value.b)) bool(*static_cast<const bool *>(value));
break;
case DataType::kDataTypeDouble:
new (static_cast<double *>(&_value.f)) double(*static_cast<const double *>(value));
break;
case DataType::kDataTypeLocalRef:
case DataType::kDataTypeGlobalRef:
new (static_cast<uint32 *>(&_value.ref)) uint32(*static_cast<const uint32 *>(value));
break;
case DataType::kDataTypeLabel:
new (static_cast<Label *>(&_value.lbl)) Label(*static_cast<const Label *>(value));
break;
case DataType::kDataTypeNull:
break;
default:
warning("PushValue instruction has an unknown type of value, this will probably malfunction!");
break;
}
}
PushValue::ValueUnion::ValueUnion() {
}
PushValue::~PushValue() {
switch (_dataType) {
case DataType::kDataTypeLabel:
_value.lbl.~Label();
break;
default:
break;
}
}
MiniscriptInstructionOutcome ListCreate::execute(MiniscriptThread *thread) const {
if (thread->getStackSize() < 2) {
thread->error("Stack underflow");
return kMiniscriptInstructionOutcomeFailed;
}
MiniscriptStackValue &rs = thread->getStackValueFromTop(0);
MiniscriptStackValue &lsDest = thread->getStackValueFromTop(1);
Common::SharedPtr<DynamicList> list(new DynamicList());
if (!list->setAtIndex(0, lsDest.value)) {
thread->error("Failed to set value 1 of list");
return kMiniscriptInstructionOutcomeFailed;
}
if (!list->setAtIndex(1, rs.value)) {
thread->error("Failed to set value 2 of list");
return kMiniscriptInstructionOutcomeFailed;
}
lsDest.value.setList(list);
thread->popValues(1);
return kMiniscriptInstructionOutcomeContinue;
}
MiniscriptInstructionOutcome ListAppend::execute(MiniscriptThread *thread) const {
if (thread->getStackSize() < 2) {
thread->error("Stack underflow");
return kMiniscriptInstructionOutcomeFailed;
}
MiniscriptStackValue &rs = thread->getStackValueFromTop(0);
MiniscriptStackValue &lsDest = thread->getStackValueFromTop(1);
if (lsDest.value.getType() != DynamicValueTypes::kList) {
thread->error("Expected list on left side of list_append");
return kMiniscriptInstructionOutcomeFailed;
}
Common::SharedPtr<DynamicList> listRef = lsDest.value.getList();
if (listRef.refCount() != 2) {
listRef = listRef->clone();
lsDest.value.setList(listRef);
}
if (!listRef->setAtIndex(listRef->getSize(), rs.value)) {
thread->error("Failed to expand list");
return kMiniscriptInstructionOutcomeFailed;
}
thread->popValues(1);
return kMiniscriptInstructionOutcomeContinue;
}
MiniscriptInstructionOutcome PushValue::execute(MiniscriptThread *thread) const {
DynamicValue value;
switch (_dataType) {
case DataType::kDataTypeNull:
value.clear();
break;
case DataType::kDataTypeDouble:
value.setFloat(_value.f);
break;
case DataType::kDataTypeBool:
value.setBool(_value.b);
break;
case DataType::kDataTypeLocalRef:
value.setObject(ObjectReference(thread->getRefs()->getRefByIndex(_value.ref)));
break;
case DataType::kDataTypeGlobalRef:
value.setObject(ObjectReference(thread->getRefs()->getGlobalRefByIndex(_value.ref)));
break;
case DataType::kDataTypeLabel: {
MTropolis::Label label;
label.id = _value.lbl.id;
label.superGroupID = _value.lbl.superGroup;
value.setLabel(label);
} break;
default:
assert(false);
break;
}
thread->pushValue(value);
return kMiniscriptInstructionOutcomeContinue;
}
PushGlobal::PushGlobal(uint32 globalID, bool isLValue) : _globalID(globalID), _isLValue(isLValue) {
}
MiniscriptInstructionOutcome PushGlobal::execute(MiniscriptThread *thread) const {
thread->pushValue(DynamicValue());
DynamicValue &value = thread->getStackValueFromTop(0).value;
switch (_globalID) {
case kGlobalRefElement:
case kGlobalRefSection:
case kGlobalRefScene:
case kGlobalRefProject:
return executeFindFilteredParent(thread, value);
case kGlobalRefModifier:
value.setObject(thread->getModifier()->getSelfReference());
break;
case kGlobalRefIncomingData:
if (_isLValue) {
DynamicValueWriteProxy proxy;
thread->createWriteIncomingDataProxy(proxy);
value.setWriteProxy(proxy);
} else
value = thread->getMessageProperties()->getValue();
break;
case kGlobalRefSource:
value.setObject(ObjectReference(thread->getMessageProperties()->getSource()));
break;
case kGlobalRefMouse:
value.setPoint(thread->getRuntime()->getCachedMousePosition());
break;
case kGlobalRefTicks:
value.setInt(thread->getRuntime()->getPlayTime() * 60 / 1000);
break;
case kGlobalRefSharedScene:
value.setObject(ObjectReference(thread->getRuntime()->getActiveSharedScene()));
break;
case kGlobalRefActiveScene:
value.setObject(ObjectReference(thread->getRuntime()->getActiveMainScene()));
break;
default:
assert(false);
thread->error("Unknown global ref type");
return kMiniscriptInstructionOutcomeFailed;
}
return kMiniscriptInstructionOutcomeContinue;
}
MiniscriptInstructionOutcome PushGlobal::executeFindFilteredParent(MiniscriptThread *thread, DynamicValue &result) const {
Common::WeakPtr<RuntimeObject> ref = thread->getModifier()->getSelfReference();
for (;;) {
Common::SharedPtr<RuntimeObject> obj = ref.lock();
if (!obj)
break;
bool isMatch = false;
switch (_globalID) {
case kGlobalRefElement:
isMatch = obj->isStructural(); // We don't classify the project, sections, and subsections as elements, but mTropolis does
break;
case kGlobalRefSection:
isMatch = obj->isSection();
break;
case kGlobalRefScene:
// FIXME: Need better detection of scenes
isMatch = obj->isElement() && static_cast<Element *>(obj.get())->getParent()->isSubsection();
break;
case kGlobalRefProject:
isMatch = obj->isProject();
break;
default:
break;
};
if (isMatch)
break;
else if (obj->isStructural()) {
Structural *parent = static_cast<Structural *>(obj.get())->getParent();
if (parent)
ref = parent->getSelfReference();
else {
ref.reset();
break;
}
} else if (obj->isModifier()) {
ref = static_cast<Modifier *>(obj.get())->getParent();
} else {
ref.reset();
break;
}
}
result.setObject(ref);
return kMiniscriptInstructionOutcomeContinue;
}
PushString::PushString(const Common::String &str) : _str(str) {
}
MiniscriptInstructionOutcome PushString::execute(MiniscriptThread *thread) const {
DynamicValue str;
str.setString(_str);
thread->pushValue(str);
return kMiniscriptInstructionOutcomeContinue;
}
Jump::Jump(uint32 instrOffset, bool isConditional) : _instrOffset(instrOffset), _isConditional(isConditional) {
}
MiniscriptInstructionOutcome Jump::execute(MiniscriptThread *thread) const {
if (_isConditional) {
if (thread->getStackSize() < 1) {
thread->error("Stack underflow");
return kMiniscriptInstructionOutcomeFailed;
}
MiniscriptInstructionOutcome outcome = thread->dereferenceRValue(0);
if (outcome != kMiniscriptInstructionOutcomeContinue)
return outcome;
bool isTrue = miniscriptEvaluateTruth(thread->getStackValueFromTop(0).value);
thread->popValues(1);
if (!isTrue)
thread->jumpOffset(this->_instrOffset);
} else {
thread->jumpOffset(this->_instrOffset);
}
return kMiniscriptInstructionOutcomeContinue;
}
} // End of namespace MiniscriptInstructions
MiniscriptThread::MiniscriptThread(Runtime *runtime, const Common::SharedPtr<MessageProperties> &msgProps, const Common::SharedPtr<MiniscriptProgram> &program, const Common::SharedPtr<MiniscriptReferences> &refs, Modifier *modifier)
: _runtime(runtime), _msgProps(msgProps), _program(program), _refs(refs), _modifier(modifier), _currentInstruction(0), _failed(false) {
}
void MiniscriptThread::error(const Common::String &message) {
#ifdef MTROPOLIS_DEBUG_ENABLE
if (_runtime->debugGetDebugger())
_runtime->debugGetDebugger()->notify(kDebugSeverityError, Common::String("Miniscript error: " + message));
#endif
warning("Miniscript error in (%x '%s'): %s", _modifier->getStaticGUID(), _modifier->getName().c_str(), message.c_str());
// This should be redundant
_failed = true;
}
const Common::SharedPtr<MiniscriptProgram> &MiniscriptThread::getProgram() const {
return _program;
}
const Common::SharedPtr<MiniscriptReferences> &MiniscriptThread::getRefs() const {
return _refs;
}
Modifier *MiniscriptThread::getModifier() const {
return _modifier;
}
const Common::SharedPtr<MessageProperties> &MiniscriptThread::getMessageProperties() const {
return _msgProps;
}
Runtime *MiniscriptThread::getRuntime() const {
return _runtime;
}
void MiniscriptThread::pushValue(const DynamicValue &value) {
_stack.push_back(MiniscriptStackValue());
MiniscriptStackValue &stackValue = _stack.back();
stackValue.value = value;
}
void MiniscriptThread::popValues(size_t count) {
while (count--)
_stack.pop_back();
}
size_t MiniscriptThread::getStackSize() const {
return _stack.size();
}
MiniscriptStackValue &MiniscriptThread::getStackValueFromTop(size_t offset) {
assert(offset < _stack.size());
return _stack[_stack.size() - 1 - offset];
}
MiniscriptInstructionOutcome MiniscriptThread::dereferenceRValue(size_t offset) {
assert(offset < _stack.size());
MiniscriptStackValue &stackValue = _stack[_stack.size() - 1 - offset];
switch (stackValue.value.getType()) {
case DynamicValueTypes::kObject: {
Common::SharedPtr<RuntimeObject> obj = stackValue.value.getObject().object.lock();
if (obj && obj->isModifier()) {
const Modifier *modifier = static_cast<const Modifier *>(obj.get());
if (modifier->isVariable()) {
static_cast<const VariableModifier *>(modifier)->varGetValue(stackValue.value);
}
}
} break;
case DynamicValueTypes::kWriteProxy:
this->error("Attempted to dereference an lvalue proxy");
return kMiniscriptInstructionOutcomeFailed;
case DynamicValueTypes::kList:
stackValue.value.setList(stackValue.value.getList()->clone());
break;
default:
break;
}
return kMiniscriptInstructionOutcomeContinue;
}
void MiniscriptThread::jumpOffset(size_t offset) {
if (offset == 0) {
this->error("Invalid jump offset");
_failed = true;
return;
}
_currentInstruction += offset - 1;
}
bool MiniscriptThread::evaluateTruthOfResult(bool &isTrue) {
if (_stack.size() != 1) {
this->error("Miniscript program didn't return a result");
return false;
}
MiniscriptInstructionOutcome outcome = dereferenceRValue(0);
if (outcome != kMiniscriptInstructionOutcomeContinue) {
this->error("Miniscript program result couldn't be dereferenced");
return false;
}
isTrue = miniscriptEvaluateTruth(_stack[0].value);
return true;
}
void MiniscriptThread::createWriteIncomingDataProxy(DynamicValueWriteProxy &proxy) {
proxy.pod.ifc = DynamicValueWriteInterfaceGlue<IncomingDataWriteInterface>::getInstance();
proxy.pod.objectRef = this;
proxy.pod.ptrOrOffset = 0;
}
void MiniscriptThread::retryInstruction() {
_currentInstruction--;
}
MiniscriptInstructionOutcome MiniscriptThread::IncomingDataWriteInterface::write(MiniscriptThread *thread, const DynamicValue &value, void *objectRef, uintptr ptrOrOffset) {
thread->_msgProps->setValue(value);
return kMiniscriptInstructionOutcomeContinue;
}
MiniscriptInstructionOutcome MiniscriptThread::IncomingDataWriteInterface::refAttrib(MiniscriptThread *thread, DynamicValueWriteProxy &proxy, void *objectRef, uintptr ptrOrOffset, const Common::String &attrib) {
// TODO: Generic refAttrib for dynamic values
return kMiniscriptInstructionOutcomeFailed;
}
MiniscriptInstructionOutcome MiniscriptThread::IncomingDataWriteInterface::refAttribIndexed(MiniscriptThread *thread, DynamicValueWriteProxy &proxy, void *objectRef, uintptr ptrOrOffset, const Common::String &attrib, const DynamicValue &index) {
// TODO: Generic refAttribIndexed for dynamic values
return kMiniscriptInstructionOutcomeFailed;
}
CORO_BEGIN_DEFINITION(MiniscriptThread::ResumeThreadCoroutine)
struct Locals {
MiniscriptThread *self = nullptr;
size_t numInstrs = 0;
size_t instrNum = 0;
};
CORO_BEGIN_FUNCTION
locals->self = params->thread.get();
locals->numInstrs = locals->self->_program->getInstructions().size();
CORO_IF (locals->numInstrs == 0)
CORO_RETURN;
CORO_END_IF
CORO_WHILE (locals->self->_currentInstruction < locals->numInstrs && !locals->self->_failed)
CORO_AWAIT_MINISCRIPT(locals->self->runNextInstruction());
CORO_END_WHILE
CORO_END_FUNCTION
CORO_END_DEFINITION
MiniscriptInstructionOutcome MiniscriptThread::runNextInstruction() {
const MiniscriptInstruction *instr = _program->getInstructions()[_currentInstruction++];
MiniscriptInstructionOutcome outcome = instr->execute(this);
if (outcome == kMiniscriptInstructionOutcomeFailed) {
// Treat this as non-fatal but bail out of the execution loop
_failed = true;
return kMiniscriptInstructionOutcomeContinue;
}
// Otherwise continue
return outcome;
}
MiniscriptInstructionOutcome MiniscriptThread::tryLoadVariable(MiniscriptStackValue &stackValue) {
if (stackValue.value.getType() == DynamicValueTypes::kObject) {
Common::SharedPtr<RuntimeObject> obj = stackValue.value.getObject().object.lock();
if (obj && obj->isModifier() && static_cast<Modifier *>(obj.get())->isVariable()) {
VariableModifier *varMod = static_cast<VariableModifier *>(obj.get());
varMod->varGetValue(stackValue.value);
}
}
return kMiniscriptInstructionOutcomeContinue;
}
MiniscriptInstructionOutcome miniscriptIgnoreFailure(MiniscriptInstructionOutcome outcome) {
if (outcome == kMiniscriptInstructionOutcomeFailed)
return kMiniscriptInstructionOutcomeContinue;
return outcome;
}
} // End of namespace MTropolis
|