Debian Codegraph @

terps/tads/tads3/lib/gameinfo.t

raw
1 #charset "us-ascii"
2
3 /*
4 * Copyright (c) 2001, 2006 Michael J. Roberts.
5 *
6 * Permission is granted to anyone to use this file, including modified
7 * and derived versions, without charge, provided this original copyright
8 * notice is retained.
9 *
10 * This is an add-in utility for tads 3 games to make it simpler to add
11 * game information to a compiled game.
12 *
13 * The game information mechanism lets you bind certain documentary
14 * information about the game directly into the compiled (.t3) file. The
15 * information can then be extracted by automated tools; for example,
16 * archive maintainers can extract the game information and use it to
17 * describe the archive entry.
18 */
19
20 /*
21 * To use this module, a game must simply call our function
22 * writeGameInfo(), passing in a lookup table and a destination filename.
23 *
24 * The lookup table consists of one entry per game information item. For
25 * each entry, the key is the name of the item, and the associated value
26 * is a string giving the text of the item.
27 *
28 * The destination filename should usually be "GameInfo.txt", since
29 * that's the name of the game information resource that must be bound
30 * into the .t3 file. However, the name isn't actually important until
31 * the resource compiler (t3res) is invoked, at which point an arbitrary
32 * filename can be mapped to the required resource name if desired. Note
33 * that we'll overwrite any existing file with the given name.
34 *
35 * writeGameInfo() will throw a FileException if an error occurs writing
36 * the data to the file.
37 *
38 * writeGameInfo() should be called during pre-initialization so that the
39 * game information is generated immediately after compilation is
40 * finished. Here's an example of how you might do this:
41 *
42 * PreinitObject
43 *. execute()
44 *. {
45 *. local tab = new LookupTable();
46 *.
47 *. tab['Name'] = 'My Test Game';
48 *. tab['Author'] = 'Bob I. Fiction';
49 *. tab['Desc'] = 'My simple test game, just to demonstrate how
50 *. to write game information.';
51 *.
52 *. writeGameInfo(tab, 'GameInfo.txt');
53 *. }
54 *. ;
55 *
56 * After pre-initialization finishes, you must finish the job by running
57 * the resource compiler, with a command line something like this:
58 *
59 * t3res mygame.t3 -add GameInfo.txt
60 *
61 * If you didn't call the output file GameInfo.txt, you can map the
62 * filename to the proper resource name with a command like this:
63 *
64 * t3res mygame.t3 -add outfile.xyz=GameInfo.txt
65 */
66
67 #include <tads.h>
68 #include <file.h>
69
70 /* TADS GameInfo writer */
71 gameInfoWriter: object
72 /*
73 * Write the game information from the given LookupTable to the given
74 * file. Each key/value pair in the LookupTable gives the GameInfo
75 * key and the corresponding value string for that key.
76 */
77 writeGameInfo(tab, fname)
78 {
79 local f;
80
81 /*
82 * open the file - note that the GameInfo.txt resource is
83 * required to be encoded in UTF-8, so open the file with the
84 * UTF-8 character set
85 */
86 f = File.openTextFile(fname, FileAccessWrite, 'utf-8');
87
88 /* write each entry in the table */
89 tab.forEachAssoc({key, val: f.writeFile(key + ': ' + val + '\n')});
90
91 /* done with the file - close it */
92 f.closeFile();
93 }
94
95 /*
96 * Get today's date as a string in the format YYYY-MM-DD. This can
97 * be used as a simple way of keeping the release date in the game
98 * information up to date with the latest compilation.
99 */
100 getGameInfoToday()
101 {
102 local dt;
103 local mm, dd;
104
105 /* get the current date */
106 dt = getTime(GetTimeDateAndTime);
107
108 /* get the month, and add a leading zero if it's only one digit */
109 mm = (dt[2] < 10 ? '0' : '') + dt[2];
110
111 /* get the day, and add a leading zero if it's only one digit */
112 dd = (dt[3] < 10 ? '0' : '') + dt[3];
113
114 /* build and return the full YYYY-MM-DD date string */
115 return toString(dt[1]) + '-' + mm + '-' + dd;
116 }
117 ;
118