4 off_t
ARF_find_code_start(AF
* f
, off_t start
) {
5 if(!AF_set_pos(f
, start
)) return -1LL;
9 if(AF_read(f
, buf
, 1) != 1) break;;
12 if(*buf
== 'S') match
++;
16 if(*buf
== 'C') match
++;
20 if(*buf
== 'O') match
++;
24 if(*buf
== 'M') return AF_get_pos(f
) - 4;
34 static void roomfile_decrypt_text(char *s
, int len
) {
37 *s
+= "Avis Durgan"[i
% 11];
43 char *RoomFile_extract_source(AF
*f
, struct RoomFile
*r
, size_t *sizep
) {
45 off_t pos
= r
->blockpos
[BLOCKTYPE_SCRIPT
];
46 if(!pos
|| !AF_set_pos(f
, pos
)) return 0;
47 int scriptlen
= AF_read_int(f
);
48 assert(r
->blocklen
[BLOCKTYPE_SCRIPT
] == scriptlen
+ 4);
49 char* out
= malloc(scriptlen
+1);
51 if((size_t) -1 == AF_read(f
, out
, scriptlen
)) {
56 roomfile_decrypt_text(out
, scriptlen
);
62 int RoomFile_read(AF
*f
, struct RoomFile
*r
) {
63 if(!AF_set_pos(f
, 0)) return 0;
64 r
->version
= AF_read_short(f
);
66 unsigned char blocktype
;
67 if((size_t) -1 == AF_read(f
, &blocktype
, 1)) return 0;
68 if(blocktype
== BLOCKTYPE_EOF
) break;
69 if(blocktype
< BLOCKTYPE_MIN
|| blocktype
> BLOCKTYPE_MAX
) return 0;
71 if(blocktype
== BLOCKTYPE_EXT
) {
73 fprintf(stderr
, "%s", "error: found blocktype_ext in incompatible room version\n");
79 else if(!strcmp(buf
, "Main"))
80 blocktype
= BLOCKTYPE_MAIN
;
81 else if(!strcmp(buf
, "TextScript"))
82 blocktype
= BLOCKTYPE_SCRIPT
;
83 else if(!strcmp(buf
, "CompScript"))
84 blocktype
= BLOCKTYPE_COMPSCRIPT
;
85 else if(!strcmp(buf
, "CompScript2"))
86 blocktype
= BLOCKTYPE_COMPSCRIPT2
;
87 else if(!strcmp(buf
, "CompScript3"))
88 blocktype
= BLOCKTYPE_COMPSCRIPT3
;
89 else if(!strcmp(buf
, "ObjNames"))
90 blocktype
= BLOCKTYPE_OBJECTNAMES
;
91 else if(!strcmp(buf
, "AnimBg"))
92 blocktype
= BLOCKTYPE_ANIMBKGRND
;
93 else if(!strcmp(buf
, "Properties"))
94 blocktype
= BLOCKTYPE_PROPERTIES
;
95 else if(!strcmp(buf
, "ObjScNames"))
96 blocktype
= BLOCKTYPE_OBJECTSCRIPTNAMES
;
98 if(r
->version
< 32) blocklen
= AF_read_int(f
);
99 else blocklen
= AF_read_longlong(f
);
100 off_t curr_pos
= AF_get_pos(f
), next_block
= curr_pos
+ blocklen
;
101 r
->blockpos
[blocktype
] = curr_pos
;
102 r
->blocklen
[blocktype
] = blocklen
;
104 case BLOCKTYPE_COMPSCRIPT3
:
108 assert(!memcmp(sig
, "SCOM", 4));
111 /* the older script types weren't supported by the released AGS sources ever */
115 if(!AF_set_pos(f
, next_block
)) return 0;