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
|
/* SCCS Id: @(#)mrecover.c 3.4 1996/07/24 */
/* Copyright (c) David Hairston, 1993. */
/* NetHack may be freely redistributed. See license for details. */
/* Macintosh Recovery Application */
/* based on code in util/recover.c. the significant differences are:
* - GUI vs. CLI. the vast majority of code here supports the GUI.
* - Mac toolbox equivalents are used in place of ANSI functions.
* - void restore_savefile(void) is event driven.
* - integral type substitutions here and there.
*/
/*
* Think C 5.0.4 project specs:
* signature: 'nhRc'
* SIZE (-1) info: flags: 0x5880, size: 65536L/65536L (64k/64k)
* libraries: MacTraps [yes], MacTraps2 (HFileStuff) [yes], ANSI [no]
* compatibility: system 6 and system 7
* misc: sizeof(int): 2, "\p": unsigned char, enum size varies,
* prototypes required, type checking enforced, no optimizers,
* FAR CODE [no], FAR DATA [no], SEPARATE STRS [no], single segment,
* short macsbug symbols
*/
/*
* To do (maybe, just maybe):
* - Merge with the code in util/recover.c.
* - Document launch (e.g. GUI equivalent of 'recover basename').
* - Drag and drop.
* - Internal memory tweaks (stack and heap usage).
* - Use status file to allow resuming aborted recoveries.
* - Bundle 'LEVL' files with recover (easier document launch).
* - Prohibit recovering games "in progress".
* - Share AppleEvents with NetHack to auto-recover crashed games.
*/
#include "config.h"
/**** Toolbox defines ****/
/* MPW C headers (99.44% pure) */
#include <Errors.h>
#include <OSUtils.h>
#include <Resources.h>
#include <Files.h>
#ifdef applec
#include <SysEqu.h>
#endif
#include <Menus.h>
#include <Devices.h>
#include <Events.h>
#include <DiskInit.h>
#include <Notification.h>
#include <Packages.h>
#include <Script.h>
#include <StandardFile.h>
#include <ToolUtils.h>
#include <Processes.h>
#include <Fonts.h>
#include <TextUtils.h>
#ifdef THINK /* glue for System 7 Icon Family call (needed by Think C 5.0.4) */
pascal OSErr GetIconSuite(Handle *theIconSuite, short theResID, long selector)
= {0x303C, 0x0501, 0xABC9};
#endif
/**** Application defines ****/
/* Memory */
typedef struct memBytes /* format of 'memB' resource, preloaded/locked */
{
short memReserved;
short memCleanup; /* 4 - memory monitor activity limit */
long memPreempt; /* 32k - start iff FreeMem() > */
long memWarning; /* 12k - warn if MaxMem() < */
long memAbort; /* 4k - abort if MaxMem() < */
long memIOBuf; /* 16k - read/write buffer size */
} memBytes, *memBytesPtr, **memBytesHandle;
#define membID 128 /* 'memB' resource ID */
/* Cursor */
#define CURS_FRAME 4L /* 1/15 second - spin cursor */
#define CURS_LATENT 60L /* pause before spin cursor */
#define curs_Init (-1) /* token for set arrow */
#define curs_Total 8 /* maybe 'acur' would be better */
#define cursorOffset 128 /* GetCursor(cursorOffset + i) */
/* Menu */
enum
{
mbar_Init = -1,
mbarAppl, /* normal mode */
mbarRecover, /* in recovery mode */
mbarDA /* DA in front mode */
};
enum
{
menuApple,
menuFile,
menuEdit,
menu_Total,
muidApple = 128,
muidFile,
muidEdit
};
enum
{
/* Apple menu */
mitmAbout = 1,
mitmHelp,
____128_1,
/* File menu */
mitmOpen = 1,
____129_1,
mitmClose_DA,
____129_2,
mitmQuit
/* standard minimum required Edit menu */
};
/* Alerts */
enum
{
alrtNote, /* general messages */
alrtHelp, /* help message */
alrt_Total,
alertAppleMenu = 127, /* menuItem to alert ID offset */
alidNote,
alidHelp
};
#define aboutBufSize 80 /* i.e. 2 lines of 320 pixels */
/* Notification */
#define nmBufSize (32 + aboutBufSize + 32)
typedef struct notifRec
{
NMRec nmr;
struct notifRec * nmNext;
short nmDispose;
unsigned char nmBuf[nmBufSize];
} notifRec, *notifPtr;
#define nmPending nmRefCon /* &in.Notify */
#define iconNotifyID 128
#define ics_1_and_4 0x00000300
/* Dialogs */
enum
{
dlogProgress = 256
};
enum
{
uitmThermo = 1
};
enum
{
initItem,
invalItem,
drawItem
};
/* Miscellaneous */
typedef struct modeFlags
{
short Front; /* fg/bg event handling */
short Notify; /* level of pending NM notifications */
short Dialog; /* a modeless dialog is open */
short Recover; /* restoration progress index */
} modeFlags;
/* convenient define to allow easier (for me) parsing of 'vers' resource */
typedef struct versXRec
{
NumVersion numVers;
short placeCode;
unsigned char versStr[]; /* (small string)(large string) */
} versXRec, *versXPtr, **versXHandle;
/**** Global variables ****/
modeFlags in = {1}; /* in Front */
EventRecord wnEvt;
SysEnvRec sysEnv;
unsigned char aboutBuf[aboutBufSize]; /* vers 1 "Get Info" string */
memBytesPtr pBytes; /* memory management */
unsigned short memActivity; /* more memory management */
MenuHandle mHnd[menu_Total];
CursPtr cPtr[curs_Total]; /* busy cursors */
unsigned long timeCursor; /* next cursor frame time */
short oldCursor = curs_Init; /* see adjustGUI() below */
notifPtr pNMQ; /* notification queue pointer */
notifRec nmt; /* notification template */
DialogTHndl thermoTHnd;
DialogRecord dlgThermo; /* progress thermometer */
#define DLGTHM ((DialogPtr) &dlgThermo)
#define WNDTHM ((WindowPtr) &dlgThermo)
#define GRFTHM ((GrafPtr) &dlgThermo)
Point sfGetWhere; /* top left corner of get file dialog */
Ptr pIOBuf; /* read/write buffer pointer */
short vRefNum; /* SFGetFile working directory/volume refnum */
long dirID; /* directory i.d. */
NMUPP nmCompletionUPP; /* UPP for nmCompletion */
FileFilterUPP basenameFileFilterUPP; /* UPP for basenameFileFilter */
UserItemUPP drawThermoUPP; /* UPP for progress callback */
#define MAX_RECOVER_COUNT 256
#define APP_NAME_RES_ID (-16396) /* macfile.h */
#define PLAYER_NAME_RES_ID 1001 /* macfile.h */
/* variables from util/recover.c */
#define SAVESIZE FILENAME
unsigned char savename[SAVESIZE]; /* originally a C string */
unsigned char lock[256]; /* pascal string */
int hpid; /* NetHack (unix-style) process i.d. */
short saveRefNum; /* save file descriptor */
short gameRefNum; /* level 0 file descriptor */
short levRefNum; /* level n file descriptor */
/**** Prototypes ****/
static void warmup(void);
static Handle alignTemplate(ResType, short, short, short, Point *);
pascal void nmCompletion(NMRec *);
static void noteErrorMessage(unsigned char *, unsigned char *);
static void note(short, short, unsigned char *);
static void adjustGUI(void);
static void adjustMemory(void);
static void optionMemStats(void);
static void RecoverMenuEvent(long);
static void eventLoop(void);
static void cooldown(void);
pascal void drawThermo(WindowPtr, short);
static void itemizeThermo(short);
pascal Boolean basenameFileFilter(ParmBlkPtr);
static void beginRecover(void);
static void continueRecover(void);
static void endRecover(void);
static short saveRezStrings(void);
/* analogous prototypes from util/recover.c */
static void set_levelfile_name(long);
static short open_levelfile(long);
static short create_savefile(unsigned char *);
static void copy_bytes(short, short);
static void restore_savefile(void);
/* auxiliary prototypes */
static long read_levelfile(short, Ptr, long);
static long write_savefile(short, Ptr, long);
static void close_file(short *);
static void unlink_file(unsigned char *);
/**** Routines ****/
main()
{
/* heap adjust */
MaxApplZone();
MoreMasters();
MoreMasters();
/* manager initialization */
InitGraf(&qd.thePort);
InitFonts();
InitWindows();
InitMenus();
TEInit();
InitDialogs(0L);
InitCursor();
nmCompletionUPP = NewNMProc(nmCompletion);
basenameFileFilterUPP = NewFileFilterProc(basenameFileFilter);
drawThermoUPP = NewUserItemProc(drawThermo);
/* get system environment, notification requires 6.0 or better */
(void) SysEnvirons(curSysEnvVers, &sysEnv);
if (sysEnv.systemVersion < 0x0600)
{
ParamText("\pAbort: System 6.0 is required", "\p", "\p", "\p");
(void) Alert(alidNote, (ModalFilterUPP) 0L);
ExitToShell();
}
warmup();
eventLoop();
/* normally these routines are never reached from here */
cooldown();
ExitToShell();
return 0;
}
static void
warmup()
{
short i;
/* pre-System 7 MultiFinder hack for smooth launch */
for (i = 0; i < 10; i++)
{
if (WaitNextEvent(osMask, &wnEvt, 2L, (RgnHandle) 0L))
if (((wnEvt.message & osEvtMessageMask) >> 24) == suspendResumeMessage)
in.Front = (wnEvt.message & resumeFlag);
}
#if 0 // ???
/* clear out the Finder info */
{
short message, count;
CountAppFiles(&message, &count);
while(count)
ClrAppFiles(count--);
}
#endif
/* fill out the notification template */
nmt.nmr.qType = nmType;
nmt.nmr.nmMark = 1;
nmt.nmr.nmSound = (Handle) -1L; /* system beep */
nmt.nmr.nmStr = nmt.nmBuf;
nmt.nmr.nmResp = nmCompletionUPP;
nmt.nmr.nmPending = (long) &in.Notify;
#if 1
{
/* get the app name */
ProcessInfoRec info;
ProcessSerialNumber psn;
info.processInfoLength = sizeof(info);
info.processName = nmt.nmBuf;
info.processAppSpec = NULL;
GetCurrentProcess(&psn);
GetProcessInformation(&psn, &info);
}
#else
/* prepend app name (31 chars or less) to notification buffer */
{
short apRefNum;
Handle apParams;
GetAppParms(* (Str255 *) &nmt.nmBuf, &apRefNum, &apParams);
}
#endif
/* add formatting (two line returns) */
nmt.nmBuf[++(nmt.nmBuf[0])] = '\r';
nmt.nmBuf[++(nmt.nmBuf[0])] = '\r';
/**** note() is usable now but not aesthetically complete ****/
/* get notification icon */
if (sysEnv.systemVersion < 0x0700)
{
if (! (nmt.nmr.nmIcon = GetResource('SICN', iconNotifyID)))
note(nilHandleErr, 0, "\pNil SICN Handle");
}
else
{
if (GetIconSuite(&nmt.nmr.nmIcon, iconNotifyID, ics_1_and_4))
note(nilHandleErr, 0, "\pBad Icon Family");
}
/* load and align various dialog/alert templates */
(void) alignTemplate('ALRT', alidNote, 0, 4, (Point *) 0L);
(void) alignTemplate('ALRT', alidHelp, 0, 4, (Point *) 0L);
thermoTHnd = (DialogTHndl) alignTemplate('DLOG', dlogProgress, 20, 8, (Point *) 0L);
(void) alignTemplate('DLOG', getDlgID, 0, 6, (Point *) &sfGetWhere);
/* get the "busy cursors" (preloaded/locked) */
for (i = 0; i < curs_Total; i++)
{
CursHandle cHnd;
if (! (cHnd = GetCursor(i + cursorOffset)))
note(nilHandleErr, 0, "\pNil CURS Handle");
cPtr[i] = *cHnd;
}
/* get the 'vers' 1 long (Get Info) string - About Recover... */
{
versXHandle vHnd;
if (! (vHnd = (versXHandle) GetResource('vers', 1)))
note(nilHandleErr, 0, "\pNil vers Handle");
i = (**vHnd).versStr[0] + 1; /* offset to Get Info pascal string */
if ((aboutBuf[0] = (**vHnd).versStr[i]) > (aboutBufSize - 1))
aboutBuf[0] = aboutBufSize - 1;
i++;
MoveHHi((Handle) vHnd); /* DEE - Fense ... */
HLock((Handle) vHnd);
BlockMove(&((**vHnd).versStr[i]), &(aboutBuf[1]), aboutBuf[0]);
ReleaseResource((Handle) vHnd);
}
/* form the menubar */
for (i = 0; i < menu_Total; i++)
{
if (! (mHnd[i] = GetMenu(i + muidApple)))
note(nilHandleErr, 0, "\pNil MENU Handle");
/* expand the apple menu */
if (i == menuApple)
AddResMenu(mHnd[menuApple], 'DRVR');
InsertMenu(mHnd[i], 0);
}
/* pre-emptive memory check */
{
memBytesHandle hBytes;
Size grow;
if (! (hBytes = (memBytesHandle) GetResource('memB', membID)))
note(nilHandleErr, 0, "\pNil Memory Handle");
pBytes = *hBytes;
if (MaxMem(&grow) < pBytes->memPreempt)
note(memFullErr, 0, "\pMore Memory Required\rTry adding 16k");
memActivity = pBytes->memCleanup; /* force initial cleanup */
}
/* get the I/O buffer */
if (! (pIOBuf = NewPtr(pBytes->memIOBuf)))
note(memFullErr, 0, "\pNil I/O Pointer");
}
/* align a window-related template to the main screen */
static Handle
alignTemplate(ResType rezType, short rezID, short vOff, short vDenom, Point *pPt)
{
Handle rtnHnd;
Rect *pRct;
vOff += GetMBarHeight();
if (! (rtnHnd = GetResource(rezType, rezID)))
note(nilHandleErr, 0, "\pNil Template Handle");
pRct = (Rect *) *rtnHnd;
/* don't move memory while aligning rect */
pRct->right -= pRct->left; /* width */
pRct->bottom -= pRct->top; /* height */
pRct->left = (qd.screenBits.bounds.right - pRct->right) / 2;
pRct->top = (qd.screenBits.bounds.bottom - pRct->bottom - vOff) / vDenom;
pRct->top += vOff;
pRct->right += pRct->left;
pRct->bottom += pRct->top;
if (pPt)
*pPt = * (Point *) pRct; /* top left corner */
return rtnHnd;
}
/* notification completion routine */
pascal void
nmCompletion(NMRec * pNMR)
{
(void) NMRemove(pNMR);
(* (short *) (pNMR->nmPending))--; /* decrement pending note level */
((notifPtr) pNMR)->nmDispose = 1; /* allow DisposPtr() */
}
/*
* handle errors inside of note(). the error message is appended to the
* given message but on a separate line and must fit within nmBufSize.
*/
static void
noteErrorMessage(unsigned char *msg, unsigned char *errMsg)
{
short i = nmt.nmBuf[0] + 1; /* insertion point */
BlockMove(&msg[1], &nmt.nmBuf[i], msg[0]);
nmt.nmBuf[i + msg[0]] = '\r';
nmt.nmBuf[0] += (msg[0] + 1);
note(memFullErr, 0, errMsg);
}
/*
* display messages using Notification Manager or an alert.
* no run-length checking is done. the messages are created to fit
* in the allocated space (nmBufSize and aboutBufSize).
*/
static void
note(short errorSignal, short alertID, unsigned char *msg)
{
if (! errorSignal)
{
Size grow;
if (MaxMem(&grow) < pBytes->memAbort)
noteErrorMessage(msg, "\pOut of Memory");
}
if (errorSignal || !in.Front)
{
notifPtr pNMR;
short i = nmt.nmBuf[0] + 1; /* insertion point */
if (errorSignal) /* use notification template */
{
pNMR = &nmt;
/* we're going to abort so add in this prefix */
BlockMove("Abort: ", &nmt.nmBuf[i], 7);
i += 7;
nmt.nmBuf[0] += 7;
}
else /* allocate a notification record */
{
if (! (pNMR = (notifPtr) NewPtr(sizeof(notifRec))))
noteErrorMessage(msg, "\pNil New Pointer");
/* initialize it */
*pNMR = nmt;
pNMR->nmr.nmStr = (StringPtr) &(pNMR->nmBuf);
/* update the notification queue */
if (!pNMQ)
pNMQ = pNMR;
else
{
notifPtr pNMX;
/* find the end of the queue */
for (pNMX = pNMQ; pNMX->nmNext; pNMX = pNMX->nmNext)
;
pNMX->nmNext = pNMR;
}
}
/* concatenate the message */
BlockMove(&msg[1], &((pNMR->nmBuf)[i]), msg[0]);
(pNMR->nmBuf)[0] += msg[0];
in.Notify++; /* increase note pending level */
NMInstall((NMRec *) pNMR);
if (errorSignal)
cooldown();
return;
}
/* in front and no error so use an alert */
ParamText(msg, "\p", "\p", "\p");
(void) Alert(alertID, (ModalFilterUPP) 0L);
ResetAlrtStage();
memActivity++;
}
static void
adjustGUI()
{
static short oldMenubar = mbar_Init; /* force initial update */
short newMenubar;
WindowPeek frontWindow;
/* oldCursor is external so it can be reset in endRecover() */
static short newCursor = curs_Init;
unsigned long timeNow;
short useArrow;
/* adjust menubar 1st */
newMenubar = in.Recover ? mbarRecover : mbarAppl;
/* desk accessories take precedence */
if (frontWindow = (WindowPeek) FrontWindow())
if (frontWindow->windowKind < 0)
newMenubar = mbarDA;
if (newMenubar != oldMenubar)
{
/* adjust menus */
switch (oldMenubar = newMenubar)
{
case mbarAppl:
EnableItem(mHnd[menuFile], mitmOpen);
SetItemMark(mHnd[menuFile], mitmOpen, noMark);
DisableItem(mHnd[menuFile], mitmClose_DA);
DisableItem(mHnd[menuEdit], 0);
break;
case mbarRecover:
DisableItem(mHnd[menuFile], mitmOpen);
SetItemMark(mHnd[menuFile], mitmOpen, checkMark);
DisableItem(mHnd[menuFile], mitmClose_DA);
DisableItem(mHnd[menuEdit], 0);
break;
case mbarDA:
DisableItem(mHnd[menuFile], mitmOpen);
EnableItem(mHnd[menuFile], mitmClose_DA);
EnableItem(mHnd[menuEdit], 0);
break;
}
DrawMenuBar();
}
/* now adjust the cursor */
if (useArrow = (!in.Recover || (newMenubar == mbarDA)))
newCursor = curs_Init;
else if ((timeNow = TickCount()) >= timeCursor) /* spin cursor */
{
timeCursor = timeNow + CURS_FRAME;
if (++newCursor >= curs_Total)
newCursor = 0;
}
if (newCursor != oldCursor)
{
oldCursor = newCursor;
SetCursor(useArrow ? &qd.arrow : cPtr[newCursor]);
}
}
static void
adjustMemory()
{
Size grow;
memActivity = 0;
if (MaxMem(&grow) < pBytes->memWarning)
note(noErr, alidNote, "\pWarning: Memory is running low");
(void) ResrvMem((Size) FreeMem()); /* move all handles high */
}
/* show memory stats: FreeMem, MaxBlock, PurgeSpace, and StackSpace */
static void
optionMemStats()
{
unsigned char *pFormat = "\pFree:#k Max:#k Purge:#k Stack:#k";
char *pSub = "#"; /* not a pascal string */
unsigned char nBuf[16];
long nStat, contig;
Handle strHnd;
long nOffset;
short i;
if (wnEvt.modifiers & shiftKey)
adjustMemory();
if (! (strHnd = NewHandle((Size) 128)))
{
note(noErr, alidNote, "\pOops: Memory stats unavailable!");
return;
}
SetString((StringHandle) strHnd, pFormat);
nOffset = 1L;
for (i = 1; i <= 4; i++)
{
/* get the replacement number stat */
switch (i)
{
case 1: nStat = FreeMem(); break;
case 2: nStat = MaxBlock(); break;
case 3: PurgeSpace(&nStat, &contig); break;
case 4: nStat = StackSpace(); break;
}
NumToString((nStat >> 10), * (Str255 *) &nBuf);
**strHnd += nBuf[0] - 1;
nOffset = Munger(strHnd, nOffset, (Ptr) pSub, 1L, (Ptr) &nBuf[1], nBuf[0]);
}
MoveHHi(strHnd);
HLock(strHnd);
note(noErr, alidNote, (unsigned char *) *strHnd);
DisposHandle(strHnd);
}
static void
RecoverMenuEvent(long menuEntry)
{
short menuID = HiWord(menuEntry);
short menuItem = LoWord(menuEntry);
switch (menuID)
{
case muidApple:
switch (menuItem)
{
case mitmAbout:
if (wnEvt.modifiers & optionKey)
optionMemStats();
/* fall thru */
case mitmHelp:
note(noErr, (alertAppleMenu + menuItem), aboutBuf);
break;
default: /* DA's or apple menu items */
{
unsigned char daName[32];
GetItem(mHnd[menuApple], menuItem, * (Str255 *) &daName);
(void) OpenDeskAcc(daName);
memActivity++;
}
break;
}
break;
case muidFile:
switch (menuItem)
{
case mitmOpen:
beginRecover();
break;
case mitmClose_DA:
{
WindowPeek frontWindow;
short refNum;
if (frontWindow = (WindowPeek) FrontWindow())
if ((refNum = frontWindow->windowKind) < 0)
CloseDeskAcc(refNum);
memActivity++;
}
break;
case mitmQuit:
cooldown();
break;
}
break;
case muidEdit:
(void) SystemEdit(menuItem - 1);
break;
}
HiliteMenu(0);
}
static void
eventLoop()
{
short wneMask = (in.Front ? everyEvent : (osMask + updateMask));
long wneSleep = (in.Front ? 0L : 3L);
while (1)
{
if (in.Front)
adjustGUI();
if (memActivity >= pBytes->memCleanup)
adjustMemory();
(void) WaitNextEvent(wneMask, &wnEvt, wneSleep, (RgnHandle) 0L);
if (in.Dialog)
(void) IsDialogEvent(&wnEvt);
switch (wnEvt.what)
{
case osEvt:
if (((wnEvt.message & osEvtMessageMask) >> 24) == suspendResumeMessage)
{
in.Front = (wnEvt.message & resumeFlag);
wneMask = (in.Front ? everyEvent : (osMask + updateMask));
wneSleep = (in.Front ? 0L : 3L);
}
break;
case nullEvent:
/* adjust the FIFO notification queue */
if (pNMQ && pNMQ->nmDispose)
{
notifPtr pNMX = pNMQ->nmNext;
DisposPtr((Ptr) pNMQ);
pNMQ = pNMX;
memActivity++;
}
if (in.Recover)
continueRecover();
break;
case mouseDown:
{
WindowPtr whichWindow;
switch(FindWindow( wnEvt . where , &whichWindow))
{
case inMenuBar:
RecoverMenuEvent(MenuSelect( wnEvt . where ));
break;
case inSysWindow:
SystemClick(&wnEvt, whichWindow);
break;
case inDrag:
{
Rect boundsRect = qd.screenBits.bounds;
Point offsetPt;
InsetRect(&boundsRect, 4, 4);
boundsRect.top += GetMBarHeight();
DragWindow(whichWindow, * ((Point *) &wnEvt.where), &boundsRect);
boundsRect = whichWindow->portRect;
offsetPt = * (Point *) &(whichWindow->portBits.bounds);
OffsetRect(&boundsRect, -offsetPt.h, -offsetPt.v);
* (Rect *) *thermoTHnd = boundsRect;
}
break;
}
}
break;
case keyDown:
{
char key = (wnEvt.message & charCodeMask);
if (wnEvt.modifiers & cmdKey)
{
if (key == '.')
{
if (in.Recover)
{
endRecover();
note(noErr, alidNote, "\pSorry: Recovery aborted");
}
}
else
RecoverMenuEvent(MenuKey(key));
}
}
break;
/* without windows these events belong to our thermometer */
case updateEvt:
case activateEvt:
{
DialogPtr dPtr;
short itemHit;
(void) DialogSelect(&wnEvt, &dPtr, &itemHit);
}
case diskEvt:
if (HiWord(wnEvt.message))
{
Point pt = {60, 60};
(void) DIBadMount(pt, wnEvt.message);
DIUnload();
memActivity++;
}
break;
} /* switch (wnEvt.what) */
} /* while (1) */
}
static void
cooldown()
{
if (in.Recover)
endRecover();
/* wait for pending notifications to complete */
while (in.Notify)
(void) WaitNextEvent(0, &wnEvt, 3L, (RgnHandle) 0L);
ExitToShell();
}
/* draw the progress thermometer and frame. 1 level <=> 1 horiz. pixel */
pascal void
drawThermo(WindowPtr wPtr, short inum)
{
itemizeThermo(drawItem);
}
/* manage progress thermometer dialog */
static void
itemizeThermo(short itemMode)
{
short iTyp, iTmp;
Handle iHnd;
Rect iRct;
GetDItem(DLGTHM, uitmThermo, &iTyp, &iHnd, &iRct);
switch(itemMode)
{
case initItem:
SetDItem(DLGTHM, uitmThermo, iTyp, (Handle) drawThermoUPP, &iRct);
break;
case invalItem:
{
GrafPtr oldPort;
GetPort(&oldPort);
SetPort(GRFTHM);
InsetRect(&iRct, 1, 1);
InvalRect(&iRct);
SetPort(oldPort);
}
break;
case drawItem:
FrameRect(&iRct);
InsetRect(&iRct, 1, 1);
iTmp = iRct.right;
iRct.right = iRct.left + in.Recover;
PaintRect(&iRct);
iRct.left = iRct.right;
iRct.right = iTmp;
EraseRect(&iRct);
break;
}
}
/* show only <pid-plname>.0 files in get file dialog */
pascal Boolean
basenameFileFilter(ParmBlkPtr pPB)
{
unsigned char *pC;
if (! (pC = (unsigned char *) pPB->fileParam.ioNamePtr))
return true;
if ((*pC < 4) || (*pC > 28)) /* save/ 1name .0 */
return true;
if ((pC[*pC - 1] == '.') && (pC[*pC] == '0')) /* bingo! */
return false;
return true;
}
static void
beginRecover()
{
SFTypeList levlType = {'LEVL'};
SFReply sfGetReply;
SFGetFile(sfGetWhere, "\p", basenameFileFilterUPP, 1, levlType,
(DlgHookUPP) 0L, &sfGetReply);
memActivity++;
if (! sfGetReply.good)
return;
/* get volume (working directory) refnum, basename, and directory i.d. */
vRefNum = sfGetReply.vRefNum;
BlockMove(sfGetReply.fName, lock, sfGetReply.fName[0] + 1);
{
static CInfoPBRec catInfo;
catInfo.hFileInfo.ioNamePtr = (StringPtr) sfGetReply.fName;
catInfo.hFileInfo.ioVRefNum = sfGetReply.vRefNum;
catInfo.hFileInfo.ioDirID = 0L;
if (PBGetCatInfoSync(&catInfo))
{
note(noErr, alidNote, "\pSorry: Bad File Info");
return;
}
dirID = catInfo.hFileInfo.ioFlParID;
}
/* open the progress thermometer dialog */
(void) GetNewDialog(dlogProgress, (Ptr) &dlgThermo, (WindowPtr) -1L);
if (ResError() || MemError())
note(noErr, alidNote, "\pOops: Progress thermometer unavailable");
else
{
in.Dialog = 1;
memActivity++;
itemizeThermo(initItem);
ShowWindow(WNDTHM);
}
timeCursor = TickCount() + CURS_LATENT;
saveRefNum = gameRefNum = levRefNum = -1;
in.Recover = 1;
}
static void
continueRecover()
{
restore_savefile();
/* update the thermometer */
if (in.Dialog && ! (in.Recover % 4))
itemizeThermo(invalItem);
if (in.Recover <= MAX_RECOVER_COUNT)
return;
endRecover();
if (saveRezStrings())
return;
note(noErr, alidNote, "\pOK: Recovery succeeded");
}
/* no messages from here (since we might be quitting) */
static void
endRecover()
{
in.Recover = 0;
oldCursor = curs_Init;
SetCursor(&qd.arrow);
/* clean up abandoned files */
if (gameRefNum >= 0)
(void) FSClose(gameRefNum);
if (levRefNum >= 0)
(void) FSClose(levRefNum);
if (saveRefNum >= 0)
{
(void) FSClose(saveRefNum);
(void) FlushVol((StringPtr) 0L, vRefNum);
/* its corrupted so trash it ... */
(void) HDelete(vRefNum, dirID, savename);
}
saveRefNum = gameRefNum = levRefNum = -1;
/* close the progress thermometer dialog */
in.Dialog = 0;
CloseDialog(DLGTHM);
DisposHandle(dlgThermo.items);
memActivity++;
}
/* add friendly, non-essential resource strings to save file */
static short
saveRezStrings()
{
short sRefNum;
StringHandle strHnd;
short i, rezID;
unsigned char *plName;
HCreateResFile(vRefNum, dirID, savename);
sRefNum = HOpenResFile(vRefNum, dirID, savename, fsRdWrPerm);
if (sRefNum <= 0)
{
note(noErr, alidNote, "\pOK: Minor resource map error");
return 1;
}
/* savename and hpid get mutilated here... */
plName = savename + 5; /* save/ */
*savename -= 5;
do
{
plName++;
(*savename)--;
hpid /= 10;
}
while (hpid);
*plName = *savename;
for (i = 1; i <= 2; i++)
{
switch (i)
{
case 1:
rezID = PLAYER_NAME_RES_ID;
strHnd = NewString(* (Str255 *) plName);
break;
case 2:
rezID = APP_NAME_RES_ID;
strHnd = NewString(* (Str255 *) "\pNetHack");
break;
}
if (! strHnd)
{
note(noErr, alidNote, "\pOK: Minor \'STR \' resource error");
CloseResFile(sRefNum);
return 1;
}
/* should check for errors... */
AddResource((Handle) strHnd, 'STR ', rezID, * (Str255 *) "\p");
}
memActivity++;
/* should check for errors... */
CloseResFile(sRefNum);
return 0;
}
static void
set_levelfile_name(long lev)
{
unsigned char *tf;
/* find the dot. this is guaranteed to happen. */
for (tf = (lock + *lock); *tf != '.'; tf--, lock[0]--)
;
/* append the level number string (pascal) */
if (tf > lock)
{
NumToString(lev, * (Str255 *) tf);
lock[0] += *tf;
*tf = '.';
}
else /* huh??? */
{
endRecover();
note(noErr, alidNote, "\pSorry: File Name Error");
}
}
static short
open_levelfile(long lev)
{
OSErr openErr;
short fRefNum;
set_levelfile_name(lev);
if (! in.Recover)
return (-1);
if ((openErr = HOpen(vRefNum, dirID, lock, fsRdWrPerm, &fRefNum))
&& (openErr != fnfErr))
{
endRecover();
note(noErr, alidNote, "\pSorry: File Open Error");
return (-1);
}
return (openErr ? -1 : fRefNum);
}
static short
create_savefile(unsigned char *savename)
{
short fRefNum;
/* translate savename to a pascal string (in place) */
{
unsigned char *pC;
short nameLen;
for (pC = savename; *pC; pC++);
nameLen = pC - savename;
for ( ; pC > savename; pC--)
*pC = *(pC - 1);
*savename = nameLen;
}
if (HCreate(vRefNum, dirID, savename, MAC_CREATOR, SAVE_TYPE)
|| HOpen(vRefNum, dirID, savename, fsRdWrPerm, &fRefNum))
{
endRecover();
note(noErr, alidNote, "\pSorry: File Create Error");
return (-1);
}
return fRefNum;
}
static void
copy_bytes(short inRefNum, short outRefNum)
{
char *buf = (char *) pIOBuf;
long bufSiz = pBytes->memIOBuf;
long nfrom, nto;
do
{
nfrom = read_levelfile(inRefNum, buf, bufSiz);
if (! in.Recover)
return;
nto = write_savefile(outRefNum, buf, nfrom);
if (! in.Recover)
return;
if (nto != nfrom)
{
endRecover();
note(noErr, alidNote, "\pSorry: File Copy Error");
return;
}
}
while (nfrom == bufSiz);
}
static void
restore_savefile()
{
static int savelev;
long saveTemp, lev;
xchar levc;
struct version_info version_data;
/* level 0 file contains:
* pid of creating process (ignored here)
* level number for current level of save file
* name of save file nethack would have created
* and game state
*/
lev = in.Recover - 1;
if (lev == 0L)
{
gameRefNum = open_levelfile(0L);
if (in.Recover)
(void) read_levelfile(gameRefNum, (Ptr) &hpid, sizeof(hpid));
if (in.Recover)
saveTemp = read_levelfile(gameRefNum, (Ptr) &savelev, sizeof(savelev));
if (in.Recover && (saveTemp != sizeof(savelev)))
{
endRecover();
note(noErr, alidNote, "\pSorry: \"checkpoint\" was not enabled");
return;
}
if (in.Recover)
(void) read_levelfile(gameRefNum, (Ptr) savename, sizeof(savename));
if (in.Recover)
(void) read_levelfile(gameRefNum,
(Ptr) &version_data, sizeof version_data);
/* save file should contain:
* current level (including pets)
* (non-level-based) game state
* other levels
*/
if (in.Recover)
saveRefNum = create_savefile(savename);
if (in.Recover)
levRefNum = open_levelfile(savelev);
if (in.Recover)
(void) write_savefile(saveRefNum,
(Ptr) &version_data, sizeof version_data);
if (in.Recover)
copy_bytes(levRefNum, saveRefNum);
if (in.Recover)
close_file(&levRefNum);
if (in.Recover)
unlink_file(lock);
if (in.Recover)
copy_bytes(gameRefNum, saveRefNum);
if (in.Recover)
close_file(&gameRefNum);
if (in.Recover)
set_levelfile_name(0L);
if (in.Recover)
unlink_file(lock);
}
else if (lev != savelev)
{
levRefNum = open_levelfile(lev);
if (levRefNum >= 0)
{
/* any or all of these may not exist */
levc = (xchar) lev;
(void) write_savefile(saveRefNum, (Ptr) &levc, sizeof(levc));
if (in.Recover)
copy_bytes(levRefNum, saveRefNum);
if (in.Recover)
close_file(&levRefNum);
if (in.Recover)
unlink_file(lock);
}
}
if (in.Recover == MAX_RECOVER_COUNT)
close_file(&saveRefNum);
if (in.Recover)
in.Recover++;
}
static long
read_levelfile(short rdRefNum, Ptr bufPtr, long count)
{
OSErr rdErr;
long rdCount = count;
if ((rdErr = FSRead(rdRefNum, &rdCount, bufPtr)) && (rdErr != eofErr))
{
endRecover();
note(noErr, alidNote, "\pSorry: File Read Error");
return (-1L);
}
return rdCount;
}
static long
write_savefile(short wrRefNum, Ptr bufPtr, long count)
{
long wrCount = count;
if (FSWrite(wrRefNum, &wrCount, bufPtr))
{
endRecover();
note(noErr, alidNote, "\pSorry: File Write Error");
return (-1L);
}
return wrCount;
}
static void
close_file(short *pFRefNum)
{
if (FSClose(*pFRefNum) || FlushVol((StringPtr) 0L, vRefNum))
{
endRecover();
note(noErr, alidNote, "\pSorry: File Close Error");
return;
}
*pFRefNum = -1;
}
static void
unlink_file(unsigned char *filename)
{
if (HDelete(vRefNum, dirID, filename))
{
endRecover();
note(noErr, alidNote, "\pSorry: File Delete Error");
return;
}
}
|