| 1 | /* Mednafen - Multi-system Emulator |
| 2 | * |
| 3 | * This program is free software; you can redistribute it and/or modify |
| 4 | * it under the terms of the GNU General Public License as published by |
| 5 | * the Free Software Foundation; either version 2 of the License, or |
| 6 | * (at your option) any later version. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License |
| 14 | * along with this program; if not, write to the Free Software |
| 15 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 16 | */ |
| 17 | |
| 18 | #include "mednafen.h" |
| 19 | |
| 20 | #include <string.h> |
| 21 | #include <stdarg.h> |
| 22 | #include <errno.h> |
| 23 | #include <list> |
| 24 | #include <algorithm> |
| 25 | |
| 26 | #include "general.h" |
| 27 | |
| 28 | #include "state.h" |
| 29 | #include "video/surface.h" |
| 30 | #include "file.h" |
| 31 | |
| 32 | #include "mempatcher.h" |
| 33 | |
| 34 | #ifdef _MSC_VER |
| 35 | #include "msvc_compat.h" |
| 36 | #endif |
| 37 | |
| 38 | extern MDFNGI EmulatedVB; |
| 39 | MDFNGI *MDFNGameInfo = &EmulatedVB; |
| 40 | |
| 41 | /* forward declarations */ |
| 42 | extern void MDFND_DispMessage(unsigned char *str); |
| 43 | |
| 44 | void MDFN_DispMessage(const char *format, ...) |
| 45 | { |
| 46 | va_list ap; |
| 47 | va_start(ap,format); |
| 48 | char *msg = new char[4096]; |
| 49 | |
| 50 | vsnprintf(msg, 4096, format,ap); |
| 51 | va_end(ap); |
| 52 | |
| 53 | MDFND_DispMessage((uint8_t*)msg); |
| 54 | } |
| 55 | |
| 56 | void MDFN_ResetMessages(void) |
| 57 | { |
| 58 | MDFND_DispMessage(NULL); |
| 59 | } |
| 60 | |
| 61 | |
| 62 | MDFNGI *MDFNI_LoadGame(const char *force_module, const char *name) |
| 63 | { |
| 64 | MDFNFILE GameFile; |
| 65 | std::vector<FileExtensionSpecStruct> valid_iae; |
| 66 | MDFNGameInfo = &EmulatedVB; |
| 67 | |
| 68 | MDFN_printf(_("Loading %s...\n"),name); |
| 69 | |
| 70 | MDFN_indent(1); |
| 71 | |
| 72 | // Construct a NULL-delimited list of known file extensions for MDFN_fopen() |
| 73 | const FileExtensionSpecStruct *curexts = MDFNGameInfo->FileExtensions; |
| 74 | |
| 75 | while(curexts->extension && curexts->description) |
| 76 | { |
| 77 | valid_iae.push_back(*curexts); |
| 78 | curexts++; |
| 79 | } |
| 80 | |
| 81 | if(!GameFile.Open(name, &valid_iae[0], _("game"))) |
| 82 | { |
| 83 | MDFNGameInfo = NULL; |
| 84 | return 0; |
| 85 | } |
| 86 | |
| 87 | MDFN_printf(_("Using module: %s(%s)\n\n"), MDFNGameInfo->shortname, MDFNGameInfo->fullname); |
| 88 | MDFN_indent(1); |
| 89 | |
| 90 | // |
| 91 | // Load per-game settings |
| 92 | // |
| 93 | // Maybe we should make a "pgcfg" subdir, and automatically load all files in it? |
| 94 | // End load per-game settings |
| 95 | // |
| 96 | |
| 97 | if(MDFNGameInfo->Load(name, &GameFile) <= 0) |
| 98 | { |
| 99 | GameFile.Close(); |
| 100 | MDFN_indent(-2); |
| 101 | MDFNGameInfo = NULL; |
| 102 | return(0); |
| 103 | } |
| 104 | |
| 105 | MDFN_LoadGameCheats(NULL); |
| 106 | MDFNMP_InstallReadPatches(); |
| 107 | |
| 108 | MDFN_ResetMessages(); // Save state, status messages, etc. |
| 109 | |
| 110 | MDFN_indent(-2); |
| 111 | |
| 112 | if(!MDFNGameInfo->name) |
| 113 | { |
| 114 | unsigned int x; |
| 115 | char *tmp; |
| 116 | |
| 117 | MDFNGameInfo->name = (uint8_t*)strdup(GetFNComponent(name)); |
| 118 | |
| 119 | for(x=0;x<strlen((char *)MDFNGameInfo->name);x++) |
| 120 | { |
| 121 | if(MDFNGameInfo->name[x] == '_') |
| 122 | MDFNGameInfo->name[x] = ' '; |
| 123 | } |
| 124 | if((tmp = strrchr((char *)MDFNGameInfo->name, '.'))) |
| 125 | *tmp = 0; |
| 126 | } |
| 127 | |
| 128 | return(MDFNGameInfo); |
| 129 | } |
| 130 | |
| 131 | void MDFNI_CloseGame(void) |
| 132 | { |
| 133 | if(!MDFNGameInfo) |
| 134 | return; |
| 135 | |
| 136 | MDFN_FlushGameCheats(0); |
| 137 | |
| 138 | MDFNGameInfo->CloseGame(); |
| 139 | |
| 140 | if(MDFNGameInfo->name) |
| 141 | free(MDFNGameInfo->name); |
| 142 | MDFNGameInfo->name = NULL; |
| 143 | |
| 144 | MDFNMP_Kill(); |
| 145 | |
| 146 | MDFNGameInfo = NULL; |
| 147 | } |
| 148 | |
| 149 | bool MDFNI_InitializeModule(void) |
| 150 | { |
| 151 | return(1); |
| 152 | } |
| 153 | |
| 154 | int MDFNI_Initialize(const char *basedir) |
| 155 | { |
| 156 | return(1); |
| 157 | } |
| 158 | |
| 159 | static int curindent = 0; |
| 160 | |
| 161 | void MDFN_indent(int indent) |
| 162 | { |
| 163 | curindent += indent; |
| 164 | } |
| 165 | |
| 166 | static uint8 lastchar = 0; |
| 167 | |
| 168 | void MDFN_printf(const char *format, ...) |
| 169 | { |
| 170 | char *format_temp; |
| 171 | char *temp; |
| 172 | unsigned int x, newlen; |
| 173 | |
| 174 | va_list ap; |
| 175 | va_start(ap,format); |
| 176 | |
| 177 | |
| 178 | // First, determine how large our format_temp buffer needs to be. |
| 179 | uint8 lastchar_backup = lastchar; // Save lastchar! |
| 180 | for(newlen=x=0;x<strlen(format);x++) |
| 181 | { |
| 182 | if(lastchar == '\n' && format[x] != '\n') |
| 183 | { |
| 184 | int y; |
| 185 | for(y=0;y<curindent;y++) |
| 186 | newlen++; |
| 187 | } |
| 188 | newlen++; |
| 189 | lastchar = format[x]; |
| 190 | } |
| 191 | |
| 192 | format_temp = (char *)malloc(newlen + 1); // Length + NULL character, duh |
| 193 | |
| 194 | // Now, construct our format_temp string |
| 195 | lastchar = lastchar_backup; // Restore lastchar |
| 196 | for(newlen=x=0;x<strlen(format);x++) |
| 197 | { |
| 198 | if(lastchar == '\n' && format[x] != '\n') |
| 199 | { |
| 200 | int y; |
| 201 | for(y=0;y<curindent;y++) |
| 202 | format_temp[newlen++] = ' '; |
| 203 | } |
| 204 | format_temp[newlen++] = format[x]; |
| 205 | lastchar = format[x]; |
| 206 | } |
| 207 | |
| 208 | format_temp[newlen] = 0; |
| 209 | |
| 210 | temp = new char[4096]; |
| 211 | vsnprintf(temp, 4096, format_temp, ap); |
| 212 | free(format_temp); |
| 213 | |
| 214 | MDFND_Message(temp); |
| 215 | free(temp); |
| 216 | |
| 217 | va_end(ap); |
| 218 | } |
| 219 | |
| 220 | void MDFN_PrintError(const char *format, ...) |
| 221 | { |
| 222 | char *temp; |
| 223 | |
| 224 | va_list ap; |
| 225 | |
| 226 | va_start(ap, format); |
| 227 | temp = new char[4096]; |
| 228 | vsnprintf(temp, 4096, format, ap); |
| 229 | MDFND_PrintError(temp); |
| 230 | free(temp); |
| 231 | |
| 232 | va_end(ap); |
| 233 | } |
| 234 | |
| 235 | void MDFN_DebugPrintReal(const char *file, const int line, const char *format, ...) |
| 236 | { |
| 237 | char *temp; |
| 238 | |
| 239 | va_list ap; |
| 240 | |
| 241 | va_start(ap, format); |
| 242 | |
| 243 | temp = new char[4096]; |
| 244 | vsnprintf(temp, 4096, format, ap); |
| 245 | fprintf(stderr, "%s:%d %s\n", file, line, temp); |
| 246 | free(temp); |
| 247 | |
| 248 | va_end(ap); |
| 249 | } |