home *** CD-ROM | disk | FTP | other *** search
/ Command & Craft / COMCRAFT.iso / comcraft / c&c / launcher / cc_miss / ccmiss.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1995-10-29  |  3.4 KB  |  149 lines

  1. // version 1.3
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <conio.h>
  6.  
  7. #define MISSION1 0x21e  // offset from the end of the file
  8.  
  9. #define NAME_OFS 0x21a  // offset to first byte of name - used to check
  10.                                                 // whether its gdi or nod file
  11.  
  12. void SeekAddress();
  13.  
  14. long int ofs, m1, m2;
  15.                                                 
  16. FILE *file;
  17.  
  18. void main(int argc, char *argv[])
  19. {
  20.     int newmis;
  21.     char name;
  22.  
  23.     
  24.     if (argc != 2)
  25.     {
  26.         printf("Usage : cced <savegame.???>\n");
  27.         exit(0);
  28.     }
  29.  
  30.     file = fopen(argv[1], "rb+");
  31.     if (file == NULL)
  32.     {
  33.         printf("Could not open file - exiting\n");
  34.         exit(0);
  35.     }
  36.  
  37.     fseek(file, -NAME_OFS, SEEK_END);
  38.     fread(&name, sizeof(name), 1, file);
  39.  
  40.     // this is the same for both cases
  41.     fseek(file, -MISSION1, SEEK_END); // read in the current mission
  42.     fread(&m1, sizeof(m1), 1, file);  // using negative offsets because
  43.                                                                         // we are starting from the end
  44.                                                                         // of the file.
  45.  
  46.     SeekAddress();
  47.  
  48.     if (ofs == MISSION1)
  49.     {
  50.         printf("Could not locate value to change. Back at original position.\n");
  51.         printf("Please email me at: buggy@adam.com.au if you get this message.\n");
  52.         printf("Try using this on a different file. Exiting.\n");
  53.         fclose(file);
  54.         exit(0);
  55.     }
  56.     
  57.     clrscr();
  58.     printf("Command and Conquer Misson Selector   written by buggy@adam.com.au\n");
  59.     printf("Version 1.4\n\n");
  60.     printf("Editing file: %s\n\n", argv[1]);    
  61.     if (name == 'G')
  62.         printf("GDI savegame file\n");
  63.     else if (name == 'N')
  64.         printf("NOD savegame file\n");
  65.     printf("Current mission: %d\n", m1); // show what the current mission is
  66.  
  67.     printf("Enter new mission (-1 to exit with no change): ");
  68.     scanf("%i", &newmis);
  69.  
  70.     if (name == 'G')
  71.     {    // 15 possible mission for nod
  72.         if (newmis > 0 && newmis < 16)
  73.         {       // write to the file and exit
  74.             fseek(file, -MISSION1, SEEK_END);
  75.             fwrite(&newmis, sizeof(newmis), 1, file);
  76.             fseek(file, -ofs, SEEK_END);
  77.             fwrite(&newmis, sizeof(newmis), 1, file);
  78.             printf("Changes written to file.\n");
  79.             printf("Remember: load the mission, then abort->restart it to use new mission\n");
  80.         }
  81.         else
  82.             printf("No changes made to the file.\n");
  83.     }
  84.     else if (name == 'N')
  85.     {
  86.         if (newmis > 0 && newmis < 14)
  87.         {
  88.             fseek(file, -MISSION1, SEEK_END);
  89.             fwrite(&newmis, sizeof(newmis), 1, file);
  90.             fseek(file, -ofs, SEEK_END);
  91.             fwrite(&newmis, sizeof(newmis), 1, file);
  92.             printf("Changes written to file.\n");
  93.             printf("Remember: load the mission, then abort->restart it to use new mission\n");
  94.         }
  95.         else
  96.             printf("No changes made to the file.\n");
  97.     }
  98.     
  99.     fclose(file);
  100.  
  101. }
  102.  
  103.  
  104. void SeekAddress()
  105. {
  106.     long int i;
  107.     char cont;
  108.  
  109.     cont = 1;
  110.     
  111.     i = MISSION1 + 1;
  112.  
  113.     // look backwards for 0x1d
  114.     while (cont)
  115.     {
  116.         if (i > 0x320)        // a large number never reached, make sure it doesn't
  117.         {                                    // loop forever
  118.             printf("Could not locate start value. Failed to initialise offset\n");
  119.             printf("for editing.\n");
  120.             fclose(file);
  121.             exit(0);
  122.         }
  123.         fseek(file, -i, SEEK_END);
  124.         fread(&m2, sizeof(m2), 1, file);
  125.         if (m2 == 0x1d) cont = 0;
  126.         else ++i;
  127.     }
  128.  
  129.     cont = 1;
  130.  
  131.     // skip a little bit (for the sake of the first mission with 0x01 as mission number
  132.     // now count backwards
  133.     i -= 11;
  134.     
  135.     while (cont)
  136.     {
  137.         if (i == MISSION1)    // back to starting position
  138.             cont = 0;
  139.         fseek(file, -i, SEEK_END);
  140.         fread(&m2, sizeof(m2), 1, file);
  141.         if(m2 == m1)
  142.         {    // only reach here if we found the variable
  143.             ofs = i;
  144.             cont = 0;
  145.         }
  146.         else --i;
  147.     }
  148. }
  149.