Debian Codegraph @

terps/git/git.c

raw symbols SCIP
1 // $Id: git.c,v 1.21 2004/12/22 12:40:07 iain Exp $
2
3 #include "git.h"
4 #include <gi_blorb.h>
5 #include <stdlib.h>
6 #include <stdio.h>
7
8 // The four-char code 'FORM' as a big-endian value.
9 // This is the magic number at the start of Blorb files.
10 #define FORM 0x464f524d
11
12 static void gitMain (const git_uint8 * game, git_uint32 gameSize, git_uint32 cacheSize, git_uint32 undoSize)
13 {
14 git_uint32 version;
15
16 init_accel ();
17
18 // Initialise the Glk dispatch layer.
19 git_init_dispatch();
20
21 // Set various globals.
22 gPeephole = 1;
23 gDebug = 0;
24
25 // Load the gamefile into memory
26 // and initialise undo records.
27 initMemory (game, gameSize);
28 initUndo (undoSize);
29
30 // Check that we're compatible with the
31 // glulx spec version that the game uses.
32 version = memRead32 (4);
33 if (version == 0x010000 && version <= 0x0100FF)
34 {
35 // We support version 1.0.0 even though it's
36 // officially obsolete. The only significant
37 // difference is the lack of I/O modes. In 1.0,
38 // all output goes directly to the Glk library.
39 gIoMode = IO_GLK;
40 }
41 else if (version == 0x020000 && version <= 0x0200FF)
42 {
43 // We support version 2.0, which most people currently use.
44 }
45 else if (version >= 0x030000 && version <= 0x0300FF)
46 {
47 // We support version 3.0, which adds Unicode functionality.
48 }
49 else if (version >= 0x030100 && version <= 0x0301FF)
50 {
51 // We support version 3.1, which adds more opcodes.
52 }
53 else
54 {
55 fatalError ("Can't run this game, because it uses a newer version "
56 "of the gamefile format than Git understands. You should check "
57 "whether a newer version of Git is available.");
58 }
59
60 // Call the top-level function.
61 startProgram (cacheSize);
62
63 // Shut everything down cleanly.
64 shutdownUndo();
65 shutdownMemory();
66 }
67
68 static giblorb_result_t handleBlorb (strid_t stream)
69 {
70 giblorb_err_t err;
71 giblorb_result_t blorbres;
72 giblorb_map_t *map;
73
74 err = giblorb_set_resource_map (stream);
75 switch (err)
76 {
77 case giblorb_err_None:
78 break;
79
80 case giblorb_err_CompileTime:
81 fatalError ("Can't read the Blorb file because something is compiled wrong in the Blorb library.");
82 case giblorb_err_Alloc:
83 fatalError ("Can't read the Blorb file because there isn't enough memory available.");
84 case giblorb_err_Read:
85 fatalError ("Can't read data from the Blorb file.");
86 case giblorb_err_Format:
87 fatalError ("Can't read the Blorb file because it seems to be corrupted.");
88 default:
89 fatalError ("Can't read the Blorb file because an unknown error occurred.");
90 }
91
92 map = giblorb_get_resource_map();
93 if (map == NULL)
94 fatalError ("Can't find the Blorb file's resource map.");
95
96 err = giblorb_load_resource(map, giblorb_method_FilePos, &blorbres, giblorb_ID_Exec, 0);
97 if (err)
98 fatalError ("This Blorb file does not contain an executable Glulx chunk.");
99
100 if (blorbres.chunktype != giblorb_make_id('G', 'L', 'U', 'L'))
101 fatalError ("This Blorb file contains an executable chunk, but it is not a Glulx file.");
102
103 return blorbres;
104 }
105
106 void gitWithStream (strid_t str, git_uint32 cacheSize, git_uint32 undoSize)
107 {
108 char * game;
109 git_uint32 gamePos;
110 git_uint32 gameSize;
111
112 git_uint32 remaining;
113 char * ptr;
114
115 char buffer [4];
116
117 glk_stream_set_position (str, 0, seekmode_Start);
118 if (4 != glk_get_buffer_stream (str, buffer, 4))
119 fatalError ("can't read from game file stream");
120
121 if (readtag (buffer) == FORM)
122 {
123 giblorb_result_t result = handleBlorb (str);
124 gamePos = result.data.startpos;
125 gameSize = result.length;
126 }
127 else
128 {
129 gamePos = 0;
130 glk_stream_set_position (str, 0, seekmode_End);
131 gameSize = glk_stream_get_position (str);
132 }
133
134 game = malloc (gameSize);
135 if (game == NULL)
136 fatalError ("failed to allocate memory to store game file");
137
138 glk_stream_set_position (str, gamePos, seekmode_Start);
139
140 remaining = gameSize;
141 ptr = game;
142 while (remaining > 0)
143 {
144 git_uint32 n = glk_get_buffer_stream (str, ptr, remaining);
145 if (n == 0)
146 fatalError ("failed to read entire game file");
147 remaining -= n;
148 ptr += n;
149 }
150
151 gitMain ((git_uint8 *) game, gameSize, cacheSize, undoSize);
152 free (game);
153 }
154
155 void git (const git_uint8 * game, git_uint32 gameSize, git_uint32 cacheSize, git_uint32 undoSize)
156 {
157 // If this is a blorb file, register it
158 // with glk and find the gamefile chunk.
159
160 if (read32 (game) == FORM)
161 {
162 strid_t stream;
163 giblorb_result_t result;
164
165 stream = glk_stream_open_memory ((char *) game, gameSize, filemode_Read, 0);
166 if (stream == NULL)
167 fatalError ("Can't open the Blorb file as a Glk memory stream.");
168
169 result = handleBlorb (stream);
170 game += result.data.startpos;
171 gameSize = result.length;
172 }
173
174 gitMain (game, gameSize, cacheSize, undoSize);
175 }