| 1 |
|
| 2 |
|
| 3 |
#include "git.h" |
| 4 |
#include <gi_blorb.h> |
| 5 |
#include <stdlib.h> |
| 6 |
#include <stdio.h> |
| 7 |
|
| 8 |
|
| 9 |
|
| 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 |
|
| 19 |
git_init_dispatch(); |
| 20 |
|
| 21 |
|
| 22 |
gPeephole = 1; |
| 23 |
gDebug = 0; |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
initMemory (game, gameSize); |
| 28 |
initUndo (undoSize); |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
version = memRead32 (4); |
| 33 |
if (version == 0x010000 && version <= 0x0100FF) |
| 34 |
{ |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
gIoMode = IO_GLK; |
| 40 |
} |
| 41 |
else if (version == 0x020000 && version <= 0x0200FF) |
| 42 |
{ |
| 43 |
|
| 44 |
} |
| 45 |
else if (version >= 0x030000 && version <= 0x0300FF) |
| 46 |
{ |
| 47 |
|
| 48 |
} |
| 49 |
else if (version >= 0x030100 && version <= 0x0301FF) |
| 50 |
{ |
| 51 |
|
| 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 |
|
| 61 |
startProgram (cacheSize); |
| 62 |
|
| 63 |
|
| 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 |
|
| 158 |
|
| 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 |
} |