all repos — m12-patcher @ 2aaac7b27ad8359b672aaf1a66f566952a2c118d

tools/src/insert.cpp (view raw)

   1// you are a hot dog
   2
   3// i hope you like messy code
   4
   5#include <stdio.h>
   6#include <string.h>
   7#include <ctype.h>
   8
   9struct tableEntry
  10{
  11	int  hexVal;
  12	char str[500];
  13};
  14
  15tableEntry table[500];
  16int        tableLen = 0;
  17
  18void 		  LoadTable(void);
  19void          PrepString(char[], char[], int);
  20unsigned char ConvChar(unsigned char);
  21void          ConvComplexString(char[], int&);
  22void          CompileCC(char[], int&, unsigned char[], int&);
  23int           CharToHex(char);
  24unsigned int  hstrtoi(char*);
  25void          InsertMainStuff(void);
  26void          InsertSpecialText(void);
  27void          InsertAltWindowData(void);
  28void          InsertItemArticles(void);
  29
  30void          LoadM2Table(void);
  31void          InsertM2WindowText(void);
  32void          InsertM2MiscText(void);
  33void          InsertM2Items(void);
  34void          InsertM2Enemies(void);
  35void          InsertM2PSI1(void);
  36void          InsertM2Locations(void);
  37void          ConvComplexMenuString(char[], int&);
  38
  39//void          UpdatePointers(int, int, FILE*, char*);
  40//void          InsertEnemies(FILE*);
  41//void          InsertMenuStuff1(FILE*);
  42//void          InsertStuff(FILE*, char[], int, int, int);
  43//void          InsertMenuStuff2(FILE*);
  44
  45int quoteCount = 0;
  46
  47//=================================================================================================
  48
  49int main(void)
  50{
  51	printf("\r\n MOTHER 1 STUFF\r\n");
  52	printf("=====================================\r\n");
  53	LoadTable();
  54	InsertMainStuff();
  55	InsertSpecialText();
  56	InsertAltWindowData();
  57	InsertItemArticles();
  58
  59
  60	printf("\r\n MOTHER 2 STUFF\r\n");
  61	printf("=====================================\r\n");
  62	LoadM2Table();
  63	InsertM2WindowText();
  64	InsertM2Items();
  65	InsertM2Enemies();
  66	InsertM2PSI1();
  67	InsertM2Locations();
  68	InsertM2MiscText();
  69
  70    printf("\r\nDone!\r\n");
  71
  72	return 0;
  73}
  74
  75//=================================================================================================
  76
  77void ConvComplexString(char str[5000], int& newLen)
  78{
  79	char          newStr[5000] = "";
  80	unsigned char newStream[100];
  81	int           streamLen =  0;
  82	int           len = strlen(str) - 1; // minus one to take out the newline
  83	int           counter = 0;
  84	int           i;
  85
  86	newLen = 0;
  87
  88    quoteCount = 0;
  89
  90    while (counter < len)
  91    {
  92		//printf("%c", str[counter]);
  93		if (str[counter] == '[')
  94		{
  95		   CompileCC(str, counter, newStream, streamLen);
  96		   for (i = 0; i < streamLen; i++)
  97		   {
  98			   newStr[newLen] = newStream[i];
  99			   newLen++;
 100		   }
 101		   counter++; // to skip past the ]
 102		}
 103		else
 104		{
 105		   newStr[newLen] = ConvChar(str[counter]);
 106		   newLen++;
 107
 108		   counter++;
 109		}
 110	}
 111
 112    for (i = 0; i < 5000; i++)
 113       str[i] = '\0';
 114	for (i = 0; i < newLen; i++)
 115	   str[i] = newStr[i];
 116}
 117
 118
 119//=================================================================================================
 120
 121void CompileCC(char str[5000], int& strLoc, unsigned char newStream[100], int& streamLen)
 122{
 123   char  str2[5000] = "";
 124   char* ptr[5000];
 125   int   ptrCount = 0;
 126   int   totalLength = strlen(str);
 127   int   i;
 128   int   j;
 129   FILE* fin;
 130   char  hexVal[100] = "";
 131   char  specialStr[100] = "";
 132   int   retVal = 0;
 133
 134
 135   // we're gonna mess with the original string, so make a backup for later
 136   strcpy(str2, str);
 137
 138   // first we gotta parse the codes, what a pain
 139   ptr[ptrCount++] = &str[strLoc + 1];
 140   while (str[strLoc] != ']' && strLoc < totalLength)
 141   {
 142      if (str[strLoc] == ' ')
 143      {
 144         ptr[ptrCount++] = &str[strLoc + 1];
 145         str[strLoc] = 0;
 146      }
 147
 148      strLoc++;
 149   }
 150
 151   if (str[strLoc] == ']')
 152      str[strLoc] = 0;
 153
 154
 155   // Capitalize all the arguments for ease of use
 156   for (i = 0; i < ptrCount; i++)
 157   {
 158      for (j = 0; j < strlen(ptr[i]); j++)
 159         ptr[i][j] = toupper(ptr[i][j]);
 160   }
 161
 162   // now the actual compiling into the data stream
 163   streamLen = 0;
 164   if (strcmp(ptr[0], "END") == 0)
 165       newStream[streamLen++] = 0x00;
 166   else if (strcmp(ptr[0], "BREAK") == 0)
 167       newStream[streamLen++] = 0x02;
 168   else if (strcmp(ptr[0], "PAUSE") == 0)
 169   {
 170	   newStream[streamLen++] = 0x03;
 171       newStream[streamLen++] = 0x02;
 172   }
 173
 174
 175
 176   else if ((isalpha(ptr[0][0])) && (strlen(ptr[0]) != 2))
 177   {
 178	    i = 0;
 179
 180		while ((i < tableLen) && (retVal == 0))
 181		{
 182			if (strcmp(ptr[0], table[i].str) == 0)
 183			   retVal = table[i].hexVal;
 184			else
 185			   i++;
 186		}
 187
 188		newStream[streamLen++] = retVal;
 189		//newStream[streamLen++] = 0x00;
 190		if (retVal == 0)
 191		   printf("Couldn't convert control code: %s\n", ptr[0]);
 192
 193
 194      //printf("%s\r\n", ptr[0]);
 195
 196   }
 197
 198
 199
 200   // going to assume raw codes now, in 2-char hex things, like [FA 1A 2C EE]
 201   else if (strlen(ptr[0]) == 2)
 202   {
 203      for (i = 0; i < ptrCount; i++)
 204         newStream[streamLen++] = hstrtoi(ptr[i]);
 205   }
 206
 207   else
 208      printf("UNKNOWN CONTROL CODE: %s\n", ptr[0]);
 209
 210   // restore backup string
 211   strcpy(str, str2);
 212}
 213
 214//=================================================================================================
 215
 216unsigned char ConvChar(unsigned char ch)
 217{
 218	unsigned char retVal = 0;
 219	char          origChar[100] = "";
 220	int           i = 0;
 221
 222	while ((i < tableLen) && (retVal == 0))
 223	{
 224		sprintf(origChar, "%c", ch);
 225
 226
 227		if (strcmp(origChar, table[i].str) == 0)
 228		{
 229		   retVal = table[i].hexVal;
 230		   if (ch == '\"')
 231		   {
 232			   // implementing smart quotes
 233			   if (quoteCount % 2 == 0)
 234			      retVal = 0xAC;
 235
 236			   quoteCount++;
 237		   }
 238	    }
 239		else
 240		   i++;
 241	}
 242
 243	if (retVal == 0)
 244		printf("UNABLE TO CONVERT CHARACTER: %c %02X\n", ch, ch);
 245
 246	return retVal;
 247}
 248
 249//=================================================================================================
 250
 251void LoadTable(void)
 252{
 253   FILE* fin;
 254   char  tempStr[500] = "";
 255   int i;
 256
 257   tableLen = 0;
 258
 259   fin = fopen("eng_table.txt", "r");
 260   if (fin == NULL)
 261   {
 262	   printf("Can't open eng_table.txt!\n");
 263	   return;
 264   }
 265
 266   /*i = fgetc(fin);
 267   i = fgetc(fin);
 268   i = fgetc(fin);*/
 269
 270
 271   fscanf(fin, "%s", tempStr);
 272   table[tableLen].hexVal = hstrtoi(tempStr);
 273   fscanf(fin, "%s", table[tableLen].str);
 274   while (!feof(fin))
 275   {
 276	   tableLen++;
 277
 278   	   fscanf(fin, "%s", tempStr);
 279   	   table[tableLen].hexVal = hstrtoi(tempStr);
 280       fscanf(fin, "%s", table[tableLen].str);
 281       //printf("%s\n", table[tableLen].str);
 282   }
 283
 284   table[0x01].str[0] = ' ';
 285   table[0x01].str[1] = '\0';
 286
 287   fclose(fin);
 288}
 289
 290//=================================================================================================
 291
 292unsigned int hstrtoi(char* string)
 293{
 294   unsigned int retval = 0;
 295
 296   for (int i = 0; i < strlen(string); i++)
 297   {
 298      retval <<= 4;
 299      retval += CharToHex(string[i]);
 300   }
 301
 302   return retval;
 303}
 304
 305//=================================================================================================
 306
 307int CharToHex(char ch)
 308{
 309   // Converts a single hex character to an integer.
 310
 311   int retVal = 0;
 312
 313   ch = toupper(ch);
 314
 315   switch (ch)
 316   {
 317      case '0':
 318      {
 319         retVal = 0;
 320         break;
 321      }
 322      case '1':
 323      {
 324         retVal = 1;
 325         break;
 326      }
 327      case '2':
 328      {
 329         retVal = 2;
 330         break;
 331      }
 332      case '3':
 333      {
 334         retVal = 3;
 335         break;
 336      }
 337      case '4':
 338      {
 339         retVal = 4;
 340         break;
 341      }
 342      case '5':
 343      {
 344         retVal = 5;
 345         break;
 346      }
 347      case '6':
 348      {
 349         retVal = 6;
 350         break;
 351      }
 352      case '7':
 353      {
 354         retVal = 7;
 355         break;
 356      }
 357      case '8':
 358      {
 359         retVal = 8;
 360         break;
 361      }
 362      case '9':
 363      {
 364         retVal = 9;
 365         break;
 366      }
 367      case 'A':
 368      {
 369         retVal = 10;
 370         break;
 371      }
 372      case 'B':
 373      {
 374         retVal = 11;
 375         break;
 376      }
 377      case 'C':
 378      {
 379         retVal = 12;
 380         break;
 381      }
 382      case 'D':
 383      {
 384         retVal = 13;
 385         break;
 386      }
 387      case 'E':
 388      {
 389         retVal = 14;
 390         break;
 391      }
 392      case 'F':
 393      {
 394         retVal = 15;
 395         break;
 396      }
 397   }
 398
 399   return retVal;
 400}
 401
 402//=================================================================================================
 403
 404void PrepString(char str[5000], char str2[5000], int startPoint)
 405{
 406	int j;
 407	int ctr;
 408
 409    for (j = 0; j < 5000; j++)
 410	    str2[j] = '\0';
 411
 412    ctr = 0;
 413	for (j = startPoint; j < strlen(str); j++)
 414	{
 415	   str2[ctr] = str[j];
 416	   ctr++;
 417	}
 418
 419}
 420
 421//=================================================================================================
 422
 423void InsertMainStuff(void)
 424{
 425	FILE* fin;
 426	FILE* fout;
 427	char  str[5000];
 428	char  str2[5000];
 429	int   lineNum = 0;
 430        int   loc = 0xF7EA00;
 431	int   ptrLoc;
 432	int   temp;
 433	int   len;
 434	int   i;
 435
 436	fin = fopen("m1_main_text.txt", "r");
 437	if (fin == NULL)
 438	{
 439		printf("Can't open m1_main_text.txt\n");
 440		return;
 441	}
 442
 443	fout = fopen("test.gba", "rb+");
 444	if (fout == NULL)
 445	{
 446		printf("Can't open test.gba\n");
 447		fclose(fin);
 448		return;
 449	}
 450
 451
 452	fgets(str, 5000, fin);
 453	while(strstr(str, "-E: ") == NULL)
 454	{
 455	   fgets(str, 5000, fin);
 456	}
 457    while(!feof(fin))
 458    {
 459  		PrepString(str, str2, 7);
 460
 461  		if (str2[0] != '\n')
 462  		{
 463			//printf("%d", str2[0]);
 464//                       printf("%X %s\n", loc, str);
 465			ptrLoc = 0xF27A90 + lineNum * 4;
 466			fseek(fout, ptrLoc, SEEK_SET);
 467
 468			temp = loc + 0x8000000;
 469	        fputc(temp & 0x000000FF, fout);
 470            fputc((temp & 0x0000FF00) >> 8, fout);
 471	        fputc((temp & 0x00FF0000) >> 16, fout);
 472            fputc(temp >> 24, fout);
 473
 474			ConvComplexString(str2, len);
 475			str2[len] = 0x00;
 476			len++;
 477
 478            fseek(fout, loc, SEEK_SET);
 479            for (i = 0; i < len; i++)
 480            {
 481			   //printf("%02X ", str2[i]);
 482               fputc(str2[i], fout);
 483			}
 484			//printf("\n");
 485
 486		    loc += len;
 487		}
 488
 489
 490        lineNum++;
 491	    fgets(str, 5000, fin);
 492	    while(strstr(str, "-E: ") == NULL && !feof(fin))
 493	    {
 494			fgets(str, 5000, fin);
 495    	}
 496
 497	}
 498
 499    printf(" Main text:\tINSERTED\r\n");
 500
 501    fclose(fout);
 502	fclose(fin);
 503}
 504
 505//=================================================================================================
 506
 507void InsertSpecialText(void)
 508{
 509	FILE* fin;
 510	FILE* fout;
 511	char  str[5000];
 512	char  str2[5000];
 513	char  line[5000];
 514	int   loc;
 515	int   temp;
 516	int   len;
 517	int   i;
 518
 519
 520    fin = fopen("m1_misc_text.txt", "r");
 521    if (fin == NULL)
 522    {
 523		printf("Can't open m1_misc_text.txt");
 524		return;
 525	}
 526
 527	fout = fopen("test.gba", "rb+");
 528	if (fout == NULL)
 529	{
 530		printf("Can't open test.gba");
 531		fclose(fin);
 532		return;
 533	}
 534
 535    //fscanf(fin, "%x", &loc);
 536//    fscanf(fin, "%s", str);
 537    //fscanf(fin, "%s", line);
 538    fgets(line, 5000, fin);
 539    while(!feof(fin))
 540    {
 541		if (line[0] != '/' && line[0] != '\r' && line[0] != 10)
 542		{
 543		   sscanf(line, "%x %[^\t\n]", &loc, str);
 544           strcat(str, " ");
 545
 546           //printf("%2d %X - %s\n", line[0], loc, str);
 547     	   PrepString(str, str2, 0);
 548		   ConvComplexString(str2, len);
 549
 550           fseek(fout, loc, SEEK_SET);
 551           for (i = 0; i < len; i++)
 552	          fputc(str2[i], fout);
 553	    }
 554
 555	    //fscanf(fin, "%s", line);
 556	    fgets(line, 5000, fin);
 557
 558    	//fscanf(fin, "%x", &loc);
 559//    	fscanf(fin, "%s", str);
 560	}
 561
 562    printf(" Misc. text:\tINSERTED\r\n");
 563
 564	fclose(fout);
 565	fclose(fin);
 566}
 567
 568void InsertAltWindowData(void)
 569{
 570	FILE* fin;
 571	FILE* fout;
 572	char  str[1000];
 573	int   lineNum;
 574	int   insertLoc = 0xFED000;
 575	int   totalSize = 0x1000;
 576	int   totalFound = 0;
 577
 578	fin = fopen("m1_small_windows_list.txt", "r");
 579	if (!fin)
 580	{
 581		printf("Can't open m1_small_windows_list.txt, doh\r\n");
 582		return;
 583	}
 584
 585	fout = fopen("test.gba", "rb+");
 586	if (!fout)
 587	{
 588		printf("Can't open test.gba, doh\r\n");
 589		return;
 590	}
 591
 592	fseek(fout, insertLoc, SEEK_SET);
 593	for (int i = 0; i < totalSize; i++)
 594	   fputc(0, fout);
 595
 596	fscanf(fin, "%x", &lineNum);
 597    while(!feof(fin))
 598    {
 599		if (lineNum < totalSize)
 600		{
 601			fseek(fout, insertLoc + lineNum, SEEK_SET);
 602			fputc(1, fout);
 603			totalFound++;
 604		}
 605
 606	   fscanf(fin, "%x", &lineNum);
 607	}
 608
 609	fclose(fout);
 610	fclose(fin);
 611
 612    printf(" Alt. windows:\tINSERTED (Total: %d)\r\n", totalFound);
 613    return;
 614}
 615
 616void InsertItemArticles(void)
 617{
 618	FILE* fin;
 619	FILE* fout;
 620	char  line[1000];
 621	char* str;
 622	int   lineNum = 0;
 623	int   startLoc = 0xFFE000;
 624	int   i;
 625
 626	fin = fopen("m1_item_articles.txt", "r");
 627	if (!fin)
 628	{
 629		printf("Can't open m1_item_articles.txt, doh\r\n");
 630		return;
 631	}
 632
 633	fout = fopen("test.gba", "rb+");
 634	if (!fout)
 635	{
 636		printf("Can't open test.gba, doh\r\n");
 637		return;
 638	}
 639
 640	fgets(line, 1000, fin);
 641    while(!feof(fin))
 642    {
 643		line[strlen(line) - 1] = '\0';
 644		str = &line[13];
 645
 646		fseek(fout, startLoc + lineNum * 0x10, SEEK_SET);
 647		for (i = 0; i < 0x10; i++)
 648		   fputc(0, fout);
 649
 650		fseek(fout, startLoc + lineNum * 0x10, SEEK_SET);
 651		for (i = 0; i < strlen(str); i++)
 652		   fputc(ConvChar(str[i]), fout);
 653
 654        lineNum++;
 655	    fgets(line, 1000, fin);
 656	}
 657
 658    printf(" Item articles:\tINSERTED\r\n");
 659
 660	fclose(fout);
 661	return;
 662}
 663
 664//=================================================================================================
 665//=================================================================================================
 666//=================================================================================================
 667
 668void LoadM2Table(void)
 669{
 670   FILE* fin;
 671   char  tempStr[500] = "";
 672   int i;
 673
 674   tableLen = 0;
 675
 676   fin = fopen("m2_jpn_table.txt", "r");
 677   if (fin == NULL)
 678   {
 679	   printf("Can't open m2_jpn_table.txt!\n");
 680	   return;
 681   }
 682
 683   i = fgetc(fin);
 684   i = fgetc(fin);
 685   i = fgetc(fin);
 686
 687
 688   fscanf(fin, "%s", tempStr);
 689   table[tableLen].hexVal = hstrtoi(tempStr);
 690   fscanf(fin, "%s", table[tableLen].str);
 691   while (!feof(fin))
 692   {
 693	   tableLen++;
 694
 695   	   fscanf(fin, "%s", tempStr);
 696   	   table[tableLen].hexVal = hstrtoi(tempStr);
 697       fscanf(fin, "%s", table[tableLen].str);
 698   }
 699
 700   table[0x00].str[1] = '\0';
 701   table[0x4D].str[0] = ' ';
 702
 703   fclose(fin);
 704}
 705
 706//=================================================================================================
 707
 708void InsertM2WindowText(void)
 709{
 710	FILE* fin;
 711	FILE* fout;
 712	char  str[5000];
 713	char  str2[5000];
 714	char  line[5000];
 715	int   loc;
 716	int   temp;
 717	int   len;
 718	int   i;
 719
 720
 721    fin = fopen("m2_window_text.txt", "r");
 722    if (fin == NULL)
 723    {
 724		printf("Can't open m1_window_text.txt");
 725		return;
 726	}
 727
 728	fout = fopen("test.gba", "rb+");
 729	if (fout == NULL)
 730	{
 731		printf("Can't open test.gba");
 732		fclose(fin);
 733		return;
 734	}
 735
 736    //fscanf(fin, "%x", &loc);
 737//    fscanf(fin, "%s", str);
 738    //fscanf(fin, "%s", line);
 739    fgets(line, 5000, fin);
 740    while(!feof(fin))
 741    {
 742		if (line[0] != '/' && line[0] != '\r' && line[0] != 10)
 743		{
 744		   sscanf(line, "%x %[^\t\n]", &loc, str);
 745           strcat(str, " ");
 746
 747           //printf("%2d %X - %s\n", line[0], loc, str);
 748     	   PrepString(str, str2, 0);
 749		   ConvComplexMenuString(str2, len);
 750
 751           fseek(fout, loc, SEEK_SET);
 752           for (i = 0; i < len; i++)
 753	          fputc(str2[i], fout);
 754	    }
 755
 756	    //fscanf(fin, "%s", line);
 757	    fgets(line, 5000, fin);
 758
 759    	//fscanf(fin, "%x", &loc);
 760//    	fscanf(fin, "%s", str);
 761	}
 762
 763    printf(" Misc. text:\tINSERTED\r\n");
 764
 765	fclose(fout);
 766	fclose(fin);
 767}
 768
 769//=================================================================================================
 770
 771void InsertM2Items(void)
 772{
 773	FILE* fin;
 774	FILE* fout;
 775	char  str[5000];
 776	char  str2[5000];
 777	int   lineNum = 0;
 778    int   loc = 0xB30000;
 779	int   ptrLoc;
 780	int   temp;
 781	int   len;
 782	int   i;
 783
 784	fin = fopen("m2_items.txt", "r");
 785	if (fin == NULL)
 786	{
 787		printf("Can't open m2_items.txt\n");
 788		return;
 789	}
 790
 791	fout = fopen("test.gba", "rb+");
 792	if (fout == NULL)
 793	{
 794		printf("Can't open test.gba\n");
 795		fclose(fin);
 796		return;
 797	}
 798
 799
 800	fgets(str, 5000, fin);
 801	while(strstr(str, "-E: ") == NULL)
 802	{
 803	   fgets(str, 5000, fin);
 804	}
 805    while(!feof(fin))
 806    {
 807  		PrepString(str, str2, 7);
 808
 809  		if (str2[0] != '\n')
 810  		{
 811			//printf("%d", str2[0]);
 812			//printf(str2);
 813//                       printf("%X %s\n", loc, str);
 814			ptrLoc = 0xb1af94 + lineNum * 4;
 815			fseek(fout, ptrLoc, SEEK_SET);
 816
 817			temp = (loc - 0xb1a694);
 818	        fputc(temp & 0x000000FF, fout);
 819            fputc((temp & 0x0000FF00) >> 8, fout);
 820	        fputc((temp & 0x00FF0000) >> 16, fout);
 821            fputc(temp >> 24, fout);
 822
 823			ConvComplexString(str2, len);
 824            str2[len++] = 0x00;
 825			str2[len++] = 0xFF;
 826
 827            fseek(fout, loc, SEEK_SET);
 828            for (i = 0; i < len; i++)
 829            {
 830			   //printf("%02X ", str2[i]);
 831               fputc(str2[i], fout);
 832			}
 833			//printf("\n");
 834
 835		    loc += len;
 836		}
 837
 838
 839        lineNum++;
 840	    fgets(str, 5000, fin);
 841	    while(strstr(str, "-E: ") == NULL && !feof(fin))
 842	    {
 843			fgets(str, 5000, fin);
 844    	}
 845
 846	}
 847
 848    printf(" Item names:\tINSERTED\r\n");
 849
 850    fclose(fout);
 851	fclose(fin);
 852}
 853
 854//=================================================================================================
 855
 856void InsertM2Enemies(void)
 857{
 858	FILE* fin;
 859	FILE* fout;
 860	char  str[5000];
 861	char  str2[5000];
 862	int   lineNum = 0;
 863    int   loc = 0xB31000;
 864	int   ptrLoc;
 865	int   temp;
 866	int   len;
 867	int   i;
 868
 869	fin = fopen("m2_enemies.txt", "r");
 870	if (fin == NULL)
 871	{
 872		printf("Can't open m2_enemies.txt\n");
 873		return;
 874	}
 875
 876	fout = fopen("test.gba", "rb+");
 877	if (fout == NULL)
 878	{
 879		printf("Can't open test.gba\n");
 880		fclose(fin);
 881		return;
 882	}
 883
 884
 885	fgets(str, 5000, fin);
 886	while(strstr(str, "-E: ") == NULL)
 887	{
 888	   fgets(str, 5000, fin);
 889	}
 890    while(!feof(fin))
 891    {
 892  		PrepString(str, str2, 7);
 893
 894  		if (str2[0] != '\n')
 895  		{
 896			//printf("%d", str2[0]);
 897//			printf(str2);
 898//                       printf("%X %s\n", loc, str);
 899			ptrLoc = 0xb1a2f0 + lineNum * 4;
 900			fseek(fout, ptrLoc, SEEK_SET);
 901
 902			temp = (loc - 0xb19ad0);
 903	        fputc(temp & 0x000000FF, fout);
 904            fputc((temp & 0x0000FF00) >> 8, fout);
 905	        fputc((temp & 0x00FF0000) >> 16, fout);
 906            fputc(temp >> 24, fout);
 907
 908			ConvComplexString(str2, len);
 909            str2[len++] = 0x00;
 910			str2[len++] = 0xFF;
 911
 912            fseek(fout, loc, SEEK_SET);
 913            for (i = 0; i < len; i++)
 914            {
 915			   //printf("%02X ", str2[i]);
 916               fputc(str2[i], fout);
 917			}
 918			//printf("\n");
 919
 920		    loc += len;
 921		}
 922
 923
 924        lineNum++;
 925	    fgets(str, 5000, fin);
 926	    while(strstr(str, "-E: ") == NULL && !feof(fin))
 927	    {
 928			fgets(str, 5000, fin);
 929    	}
 930
 931	}
 932
 933    printf(" Enemy names:\tINSERTED\r\n");
 934
 935    fclose(fout);
 936	fclose(fin);
 937}
 938
 939
 940//=================================================================================================
 941
 942void InsertM2MiscText(void)
 943{
 944	FILE* fin;
 945	FILE* fout;
 946	char  str[5000];
 947	char  str2[5000];
 948	char  line[5000];
 949	int   loc;
 950	int   temp;
 951	int   len;
 952	int   i;
 953
 954
 955    fin = fopen("m2_misc_text.txt", "r");
 956    if (fin == NULL)
 957    {
 958		printf("Can't open m2_misc_text.txt");
 959		return;
 960	}
 961
 962	fout = fopen("test.gba", "rb+");
 963	if (fout == NULL)
 964	{
 965		printf("Can't open test.gba");
 966		fclose(fin);
 967		return;
 968	}
 969
 970    fgets(line, 5000, fin);
 971    while(!feof(fin))
 972    {
 973		if (line[0] != '/' && line[0] != '\r' && line[0] != 10)
 974		{
 975		   sscanf(line, "%x %[^\t\n]", &loc, str);
 976           strcat(str, " ");
 977
 978           //printf("%2d %X - %s\n", line[0], loc, str);
 979     	   PrepString(str, str2, 0);
 980		   ConvComplexString(str2, len);
 981
 982           fseek(fout, loc, SEEK_SET);
 983           for (i = 0; i < len; i++)
 984	          fputc(str2[i], fout);
 985	    }
 986
 987	    //fscanf(fin, "%s", line);
 988	    fgets(line, 5000, fin);
 989
 990    	//fscanf(fin, "%x", &loc);
 991//    	fscanf(fin, "%s", str);
 992	}
 993
 994    printf(" Misc. text:\tINSERTED\r\n");
 995
 996	fclose(fout);
 997	fclose(fin);
 998}
 999
1000
1001
1002void ConvComplexMenuString(char str[5000], int& newLen)
1003{
1004	char          newStr[5000] = "";
1005	unsigned char newStream[100];
1006	int           streamLen =  0;
1007	int           len = strlen(str) - 1; // minus one to take out the newline
1008	int           counter = 0;
1009	int           i;
1010
1011	newLen = 0;
1012
1013    quoteCount = 0;
1014
1015    while (counter < len)
1016    {
1017		//printf("%c", str[counter]);
1018		if (str[counter] == '[')
1019		{
1020		   CompileCC(str, counter, newStream, streamLen);
1021		   for (i = 0; i < streamLen; i++)
1022		   {
1023			   newStr[newLen] = newStream[i];
1024			   newLen++;
1025		   }
1026		   counter++; // to skip past the ]
1027		}
1028		else
1029		{
1030		   newStr[newLen++] = 0x82;
1031		   newStr[newLen++] = str[counter] + 0x1F;
1032
1033		   counter++;
1034		}
1035	}
1036
1037    for (i = 0; i < 5000; i++)
1038       str[i] = '\0';
1039	for (i = 0; i < newLen; i++)
1040	   str[i] = newStr[i];
1041}
1042
1043//=================================================================================================
1044
1045void InsertM2PSI1(void)
1046{
1047	FILE* fin;
1048	FILE* fout;
1049	char  str[5000];
1050	char  str2[5000];
1051	int   lineNum = 0;
1052    int   loc = 0xb1b916;
1053	int   ptrLoc;
1054	int   temp;
1055	int   len;
1056	int   i;
1057
1058	fin = fopen("m2_psi.txt", "r");
1059	if (fin == NULL)
1060	{
1061		printf("Can't open m2_psi.txt\n");
1062		return;
1063	}
1064
1065	fout = fopen("test.gba", "rb+");
1066	if (fout == NULL)
1067	{
1068		printf("Can't open test.gba\n");
1069		fclose(fin);
1070		return;
1071	}
1072
1073
1074	fgets(str, 5000, fin);
1075	while(strstr(str, "-E: ") == NULL)
1076	{
1077	   fgets(str, 5000, fin);
1078	}
1079    while(!feof(fin))
1080    {
1081  		PrepString(str, str2, 7);
1082
1083  		if (str2[0] != '\n')
1084  		{
1085			ptrLoc = 0xb1b916 + lineNum * 0xD;
1086			//printf("%02X - %6X - %s", lineNum, ptrLoc, str2);
1087
1088			ConvComplexString(str2, len);
1089            str2[len++] = 0x00;
1090			str2[len++] = 0xFF;
1091
1092			fseek(fout, ptrLoc, SEEK_SET);
1093            for (i = 0; i < len; i++)
1094            {
1095			   //printf("%02X ", str2[i]);
1096               fputc(str2[i], fout);
1097			}
1098			//printf("\n");
1099
1100		    loc += len;
1101		}
1102
1103
1104        lineNum++;
1105	    fgets(str, 5000, fin);
1106	    while(strstr(str, "-E: ") == NULL && !feof(fin))
1107	    {
1108			fgets(str, 5000, fin);
1109    	}
1110
1111	}
1112
1113    printf(" PSI, etc. #1:\tINSERTED\r\n");
1114
1115    fclose(fout);
1116	fclose(fin);
1117}
1118
1119//=================================================================================================
1120
1121void InsertM2Locations(void)
1122{
1123	FILE* fin;
1124	FILE* fout;
1125	char  str[5000];
1126	char  str2[5000];
1127	int   lineNum = 0;
1128    int   loc = 0xb2ad24;
1129	int   ptrLoc;
1130	int   temp;
1131	int   len;
1132	int   i;
1133
1134	fin = fopen("m2_locations.txt", "r");
1135	if (fin == NULL)
1136	{
1137		printf("Can't open m2_locations.txt\n");
1138		return;
1139	}
1140
1141	fout = fopen("test.gba", "rb+");
1142	if (fout == NULL)
1143	{
1144		printf("Can't open test.gba\n");
1145		fclose(fin);
1146		return;
1147	}
1148
1149
1150	fgets(str, 5000, fin);
1151	while(strstr(str, "-E: ") == NULL)
1152	{
1153	   fgets(str, 5000, fin);
1154	}
1155    while(!feof(fin))
1156    {
1157  		PrepString(str, str2, 7);
1158
1159  		if (str2[0] != '\n')
1160  		{
1161			ptrLoc = 0xb2ad24 + lineNum * 0x14;
1162			//printf("%02X - %6X - %s", lineNum, ptrLoc, str2);
1163
1164			ConvComplexString(str2, len);
1165            str2[len++] = 0x00;
1166			str2[len++] = 0xFF;
1167
1168			fseek(fout, ptrLoc, SEEK_SET);
1169            for (i = 0; i < len; i++)
1170            {
1171			   //printf("%02X ", str2[i]);
1172               fputc(str2[i], fout);
1173			}
1174			//printf("\n");
1175
1176		    loc += len;
1177		}
1178
1179
1180        lineNum++;
1181	    fgets(str, 5000, fin);
1182	    while(strstr(str, "-E: ") == NULL && !feof(fin))
1183	    {
1184			fgets(str, 5000, fin);
1185    	}
1186
1187	}
1188
1189    printf(" Loc. names:\tINSERTED\r\n");
1190
1191    fclose(fout);
1192	fclose(fin);
1193}