fixed deadlock
[picodrive.git] / Pico / Cart.c
index 0762473..1cf8dec 100644 (file)
@@ -1,7 +1,7 @@
 // This is part of Pico Library\r
 \r
 // (c) Copyright 2004 Dave, All rights reserved.\r
-// (c) Copyright 2006 notaz, All rights reserved.\r
+// (c) Copyright 2006-2007, Grazvydas "notaz" Ignotas\r
 // Free for non-commercial use.\r
 \r
 // For commercial use, separate licencing terms must be obtained.\r
 #include "../unzip/unzip.h"\r
 #include "../unzip/unzip_stream.h"\r
 \r
+\r
 static char *rom_exts[] = { "bin", "gen", "smd", "iso" };\r
 \r
+void (*PicoCartLoadProgressCB)(int percent) = NULL;\r
+\r
 \r
 pm_file *pm_open(const char *path)\r
 {\r
@@ -78,6 +81,9 @@ zip_failed:
   f = fopen(path, "rb");\r
   if (f == NULL) return NULL;\r
 \r
+  /* we use our own buffering */\r
+  setvbuf(f, NULL, _IONBF, 0);\r
+\r
   file = malloc(sizeof(*file));\r
   if (file == NULL) {\r
     fclose(f);\r
@@ -120,10 +126,16 @@ int pm_seek(pm_file *stream, long offset, int whence)
 {\r
   if (stream->type == PMT_UNCOMPRESSED)\r
   {\r
-    return fseek(stream->file, offset, whence);\r
+    fseek(stream->file, offset, whence);\r
+    return ftell(stream->file);\r
   }\r
   else if (stream->type == PMT_ZIP)\r
   {\r
+    if (PicoMessage != NULL && offset > 6*1024*1024) {\r
+      long pos = gztell((gzFile) stream->param);\r
+      if (offset < pos || offset - pos > 6*1024*1024)\r
+        PicoMessage("Decompressing data...");\r
+    }\r
     return gzseek((gzFile) stream->param, offset, whence);\r
   }\r
   else\r
@@ -234,7 +246,7 @@ static unsigned char *PicoCartAlloc(int filesize)
 \r
 int PicoCartLoad(pm_file *f,unsigned char **prom,unsigned int *psize)\r
 {\r
-  unsigned char *rom=NULL; int size;\r
+  unsigned char *rom=NULL; int size, bytes_read;\r
   if (f==NULL) return 1;\r
 \r
   size=f->size;\r
@@ -243,9 +255,35 @@ int PicoCartLoad(pm_file *f,unsigned char **prom,unsigned int *psize)
 \r
   // Allocate space for the rom plus padding\r
   rom=PicoCartAlloc(size);\r
-  if (rom==NULL) return 1; // { fclose(f); return 1; }\r
+  if (rom==NULL) {\r
+    printf("out of memory (wanted %i)\n", size);\r
+    return 1;\r
+  }\r
 \r
-  pm_read(rom,size,f); // Load up the rom\r
+  if (PicoCartLoadProgressCB != NULL)\r
+  {\r
+    // read ROM in blocks, just for fun\r
+    int ret;\r
+    unsigned char *p = rom;\r
+    bytes_read=0;\r
+    do\r
+    {\r
+      int todo = size - bytes_read;\r
+      if (todo > 256*1024) todo = 256*1024;\r
+      ret = pm_read(p,todo,f);\r
+      bytes_read += ret;\r
+      p += ret;\r
+      PicoCartLoadProgressCB(bytes_read * 100 / size);\r
+    }\r
+    while (ret > 0);\r
+  }\r
+  else\r
+    bytes_read = pm_read(rom,size,f); // Load up the rom\r
+  if (bytes_read <= 0) {\r
+    printf("read failed\n");\r
+    free(rom);\r
+    return 1;\r
+  }\r
 \r
   // maybe we are loading MegaCD BIOS?\r
   if (!(PicoMCD&1) && size == 0x20000 && (!strncmp((char *)rom+0x124, "BOOT", 4) || !strncmp((char *)rom+0x128, "BOOT", 4))) {\r