reman3/Rayman_X/cpa/tempgrp/CRB/CRB_Main.c

707 lines
17 KiB
C
Raw Blame History

#include <windows.h>
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "conio.h"
#include "CRB_Main.h"
//#include "CRB_Log.h"
#define __CRB_Includes_SNA__
#include "sna/snammg.h"
#undef __CRB_Includes_SNA__
tdstBloc *g_p_stMemory0[C_CRB_NBMAXBLOC];
tdstBloc *g_p_stMemory1[C_CRB_NBMAXBLOC];
tdstBloc *g_p_stPtrMemory0[C_CRB_NBMAXBLOC];
tdstBloc *g_p_stPtrMemory1[C_CRB_NBMAXBLOC];
unsigned long g_ulNbBloc;
unsigned long g_ulBlocPtrTable[1000000];
unsigned char g_ucBlocPtrDestTable[1000000];
unsigned long g_ulNbPtr,g_ulNbBadPtr;
unsigned char g_ucAvoidAllEntries;
// For debug purpose
long g_ulBadBlocDiff[100];
unsigned long g_ulBadBlocDiffOccur[100];
unsigned long g_ulNbBadBlocDiff;
// End for debug purpose
char g_bGlideMode;
void CRB_InitArray()
{
}
char SNA_fn_bSaveThisModuleBlocInFix( tdstBloc *_p_stBlock )
{
return(_p_stBlock->ucLocation == SNA_C_ucBlockOfFix);
}
char SNA_fn_bSaveThisModuleBlocInLevel( tdstBloc *_p_stBlock )
{
return(_p_stBlock->ucLocation == SNA_C_ucBlockOfLevel);
}
unsigned long CRB_fn_ulNbOfFixBlockIn( tdstBloc **_p_a_stBlocks, unsigned long _ulArraySize )
{
unsigned long i, ulNb = 0;
for( i=0; i<_ulArraySize; i++ )
{
if( SNA_fn_bSaveThisModuleBlocInFix( *_p_a_stBlocks ) )
ulNb++;
_p_a_stBlocks++;
}
return ulNb;
}
unsigned long CRB_fn_ulNbOfLevelBlockIn( tdstBloc **_p_a_stBlocks, unsigned long _ulArraySize )
{
unsigned long i, ulNb = 0;
for( i=0; i<_ulArraySize; i++ )
{
if( SNA_fn_bSaveThisModuleBlocInLevel( *_p_a_stBlocks ) )
ulNb++;
_p_a_stBlocks++;
}
return ulNb;
}
unsigned char CRB_fn_bDestinationBloc( unsigned long _ulPtrValue )
{
unsigned char i;
for(i=0;i<g_ulNbBloc;i++)
{
if( (_ulPtrValue>=g_p_stMemory0[i]->ulBeginBloc) && (_ulPtrValue<g_p_stMemory0[i]->ulEndBloc) )
{
return i;
}
}
return -1;
}
// Add a bad bloc displacement to the array of bad bloc disp.
void CRB_fn_bAddBadBlocDiff(long ucI)
{
unsigned char i;
for(i=0;i<g_ulNbBadBlocDiff;i++)
{
if(ucI==g_ulBadBlocDiff[i])
{
g_ulBadBlocDiffOccur[i]++;
return;
}
}
g_ulBadBlocDiff[g_ulNbBadBlocDiff]=ucI;
g_ulNbBadBlocDiff++;
return;
}
// Open file for writing PtrTable.
HANDLE CRB_fn_vOpenPtrTable(char *szFilename,unsigned long ulNbBloc)
{
HANDLE hFile;
unsigned char ucNb;
DWORD dwWrite;
ucNb=(unsigned char)ulNbBloc;
hFile = CreateFile (szFilename , GENERIC_WRITE, 0 , NULL , CREATE_ALWAYS , FILE_ATTRIBUTE_NORMAL , NULL);
WriteFile (hFile , &ucNb , 1 , &dwWrite , NULL);
return(hFile);
}
// Close file for PtrTable.
void CRB_fn_vClosePtrTable(HANDLE hFile)
{
CloseHandle (hFile);
}
// Save PtrTable for a bloc to disc.
//void CRB_fn_vSavePtrTable(unsigned long ulBlocNb,FILE *fFile)
void CRB_fn_vSavePtrTable(unsigned long ulBlocNb,HANDLE hFile)
{
unsigned long i;
unsigned char ucNb;
DWORD dwWrite;
tdstPtr *p_stPtr;
tdstPtr *p_stPointer;
// N<> Module
ucNb=(unsigned char)g_p_stMemory0[ulBlocNb]->ulModuleNumber;
WriteFile (hFile , &ucNb , 1 , &dwWrite , NULL);
// N<> Bloc
ucNb=(unsigned char)g_p_stMemory0[ulBlocNb]->ulBlocNumber;
WriteFile (hFile , &ucNb , 1 , &dwWrite , NULL);
// Nb of Ptr
WriteFile (hFile , &(g_ulNbPtr) , 4 , &dwWrite , NULL);
/*
for(i=0;i<g_ulNbPtr;i++)
{
WriteFile (hFile , &(g_ulBlocPtrTable[i]) , 4 , &dwWrite , NULL);
if(g_ucBlocPtrDestTable[i]<0xf0)
{
ucNb=(unsigned char)g_p_stMemory0[g_ucBlocPtrDestTable[i]]->ulModuleNumber;
}
else
{
ucNb=g_ucBlocPtrDestTable[i];
}
WriteFile (hFile , &ucNb , 1 , &dwWrite , NULL);
if(g_ucBlocPtrDestTable[i]<0xf0)
{
ucNb=(unsigned char)g_p_stMemory0[g_ucBlocPtrDestTable[i]]->ulBlocNumber;
}
else
{
ucNb=g_ucBlocPtrDestTable[i];
}
WriteFile (hFile , &ucNb , 1 , &dwWrite , NULL);
}
*/
p_stPointer = (tdstPtr*) malloc (sizeof(tdstPtr) * g_ulNbPtr);
p_stPtr = p_stPointer;
for(i=0;i<g_ulNbPtr;i++,p_stPtr++)
{
//
p_stPtr -> p_vPtr = g_ulBlocPtrTable[i];
//
if(g_ucBlocPtrDestTable[i]<0xf0)
{
p_stPtr -> ucTargetModule = (unsigned char)g_p_stMemory0[g_ucBlocPtrDestTable[i]]->ulModuleNumber;
}
else
{
p_stPtr -> ucTargetModule = g_ucBlocPtrDestTable[i];
}
//
if(g_ucBlocPtrDestTable[i]<0xf0)
{
p_stPtr -> ucTargetBlock = (unsigned char)g_p_stMemory0[g_ucBlocPtrDestTable[i]]->ulBlocNumber;
}
else
{
p_stPtr -> ucTargetBlock = g_ucBlocPtrDestTable[i];
}
}
WriteFile (hFile , p_stPointer , sizeof(tdstPtr) * g_ulNbPtr , &dwWrite , NULL);
free (p_stPointer);
}
void CRB_fn_vBuildPointerTableForFix(unsigned long ulNbBloc,char *szFilename)
{
unsigned long i,j;
unsigned long *p_b0,*p_b1,ulLastBadPtr;
unsigned char ucDestBloc;
//FILE *fFile;
HANDLE hFile;
hFile = CRB_fn_vOpenPtrTable (szFilename , CRB_fn_ulNbOfFixBlockIn (g_p_stMemory0 , ulNbBloc));
// For stats
// fn_vBeginLogPointersInfos( "Fix" );
g_ulNbBadPtr=0;
ulLastBadPtr=0;
for(i=0;i<ulNbBloc;i++)
{
g_ulNbPtr=0;
// GuS: Don't compute relocation for empty blocs.
if( g_p_stMemory0[i]->ulSize == 0 ) continue;
p_b0=(unsigned long *)g_p_stMemory0[i]->cMemory;
p_b1=(unsigned long *)g_p_stMemory1[i]->cMemory;
for(j=0;j<((g_p_stMemory0[i]->ulSize)>>2);j++)
{
if(p_b0[j]!=p_b1[j])
{
ucDestBloc=CRB_fn_bDestinationBloc(p_b0[j]);
// fn_vLogAddPointer( i, ucDestBloc );
if(ucDestBloc!=(unsigned char)-1)
{
// Add store of the address of this ptr and the aim bloc.
g_ulBlocPtrTable[g_ulNbPtr]=g_p_stMemory0[i]->ulBeginBloc+(j<<2);
g_ucBlocPtrDestTable[g_ulNbPtr]=ucDestBloc;
g_ulNbPtr++;
/*
if( g_p_stMemory0[ucDestBloc]->ulModuleNumber == C_ucIdOfModuleTmp )
{
// Pointers to block TMP will be set to NULL.
g_ucBlocPtrDestTable[g_ulNbPtr-1]=0xfe;
}
*/
}
else
{
/* Gus: Uncomment this to take care of pointers to unknown blocs.
if(ulLastBadPtr==(j+1))
{
printf("\nptr Possible");
}
// for debug only
// Pointers to unknow blocks will remain unchanged.
g_ulBlocPtrTable[g_ulNbPtr]=g_p_stMemory0[i]->ulBeginBloc+(j<<2);
g_ucBlocPtrDestTable[g_ulNbPtr]=0xff;
g_ulNbPtr++;
// end debug
g_ulNbBadPtr++;
ulLastBadPtr=j;
// printf("\nWarning - A Pointer (Offset=%lx) in Module %ld Bloc %ld point out of Blocs !",j,g_p_stMemory0[i]->ulModuleNumber,g_p_stMemory0[i]->ulBlocNumber);
*/
}
}
}
// fn_vLogPointersInfosForOneBlock( i );
CRB_fn_vSavePtrTable (i , hFile);
}
// fn_vEndLogPointersInfos();
CRB_fn_vClosePtrTable (hFile);
}
void CRB_fn_vBuildPointerTableForLevel(unsigned long ulNbBloc,char *szFilename)
{
unsigned long i,j;
unsigned long *p_b0,*p_b1,ulLastBadPtr;
unsigned char ucDestBloc;
HANDLE hFile;
hFile = CRB_fn_vOpenPtrTable (szFilename , CRB_fn_ulNbOfLevelBlockIn (g_p_stMemory0 , ulNbBloc));
// For stats
// fn_vBeginLogPointersInfos( "Level" );
g_ulNbBadPtr=0;
ulLastBadPtr=0;
for(i=0;i<ulNbBloc;i++)
{
g_ulNbPtr=0;
// g_ulNbPtrFix=0;
// GuS: Don't compute relocation for empty blocs.
if( g_p_stMemory0[i]->ulSize == 0 ) continue;
p_b0=(unsigned long *)g_p_stMemory0[i]->cMemory;
p_b1=(unsigned long *)g_p_stMemory1[i]->cMemory;
for(j=0;j<((g_p_stMemory0[i]->ulSize)>>2);j++)
{
if(p_b0[j]!=p_b1[j])
{
ucDestBloc=CRB_fn_bDestinationBloc(p_b0[j]);
// fn_vLogAddPointer( i, ucDestBloc );
if(ucDestBloc==(unsigned char)-1)
{
/* Gus: Uncomment this to take care of pointers to unknown blocs.
if(ulLastBadPtr==(j+1))
{
printf("\nptr Possible");
}
// for debug only
g_ulBlocPtrTable[g_ulNbPtr]=g_p_stMemory0[i]->ulBeginBloc+(j<<2);
g_ucBlocPtrDestTable[g_ulNbPtr]=0xff;
g_ulNbPtr++;
// end debug
g_ulNbBadPtr++;
ulLastBadPtr=j;
// printf("\nWarning - A Pointer (Offset=%lx) in Module %ld Bloc %ld point out of Blocs !",j,g_p_stMemory0[i]->ulModuleNumber,g_p_stMemory0[i]->ulBlocNumber);
*/
}
else
{
if( SNA_fn_bSaveThisModuleBlocInFix( g_p_stMemory0[i] ) )
{
if( SNA_fn_bSaveThisModuleBlocInFix( g_p_stMemory0[ucDestBloc] ) )
{
// Pointer from fix to fix: case is already handled by CRB_fn_vBuildPointerTableForFix
j=j;
}
else if( SNA_fn_bSaveThisModuleBlocInLevel( g_p_stMemory0[ucDestBloc] ) )
{
// Pointeur from fix to level
g_ulBlocPtrTable[g_ulNbPtr]=g_p_stMemory0[i]->ulBeginBloc+(j<<2);
g_ucBlocPtrDestTable[g_ulNbPtr]=ucDestBloc;
g_ulNbPtr++;
}
}
else // Pointeur from level to anywhere
{
// Add store of the address of this ptr and the aim bloc.
g_ulBlocPtrTable[g_ulNbPtr]=g_p_stMemory0[i]->ulBeginBloc+(j<<2);
g_ucBlocPtrDestTable[g_ulNbPtr]=ucDestBloc;
g_ulNbPtr++;
/*
if( g_p_stMemory0[ucDestBloc]->ulModuleNumber == C_ucIdOfModuleTmp )
{
// Pointers to block TMP will be set to NULL.
g_ucBlocPtrDestTable[g_ulNbPtr-1]=0xfe;
}
*/
}
}
}
}
if( SNA_fn_bSaveThisModuleBlocInFix( g_p_stMemory0[i] ) )
{
}
else
{
// fn_vLogPointersInfosForOneBlock( i );
CRB_fn_vSavePtrTable (i , hFile);
}
}
// fn_vEndLogPointersInfos();
CRB_fn_vClosePtrTable (hFile);
}
void CRB_fn_vBuildPointerTableForPtr(unsigned long ulNbBloc,char *szFilename, char *szType )
{
unsigned long i,j;
unsigned long *p_b0,*p_b1;
unsigned char ucDestBloc,br=0;
HANDLE hFile;
hFile = CRB_fn_vOpenPtrTable (szFilename , ulNbBloc);
// For stats
// fn_vBeginLogPointersInfosForGlobalPointers( szType );
g_ulNbBadPtr=0;
for(i=0;i<ulNbBloc;i++)
{
g_ulNbPtr=0;
// GuS: Don't compute relocation for empty blocs.
if( g_p_stPtrMemory0[i]->ulSize == 0 ) continue;
p_b0=(unsigned long *)g_p_stPtrMemory0[i]->cMemory;
p_b1=(unsigned long *)g_p_stPtrMemory1[i]->cMemory;
for(j=0;j<((g_p_stPtrMemory0[i]->ulSize)>>2);j++)
{
if(p_b0[j]!=p_b1[j])
{
ucDestBloc=CRB_fn_bDestinationBloc(p_b0[j]);
// fn_vLogAddPointer( i, ucDestBloc );
if(ucDestBloc!=(unsigned char)-1)
{
// Add store of the address of this ptr and the aim bloc.
g_ulBlocPtrTable[g_ulNbPtr]=p_b0[j];
g_ucBlocPtrDestTable[g_ulNbPtr]=ucDestBloc;
g_ulNbPtr++;
}
else
{
// for debug only
// g_ulBlocPtrTable[g_ulNbPtr]=g_p_stMemory0[i]->ulBeginBloc+(j<<2);
// g_ucBlocPtrDestTable[g_ulNbPtr]=0;
// g_ulNbPtr++;
// end debug
g_ulNbBadPtr++;
if(g_ucAvoidAllEntries==0)
{
if(br!=2)
printf("\nWarning - A GlobalPointer (%8ld:%8lx/%8lx) point out of Blocs !",j,p_b0[j],p_b1[j]);
if((j%24==0)&&(br==0))
{
char c;
printf("\nStop On Next Bad Pointer (Y/N/Skip all) :");
c=getch();
c=toupper(c);
if(c=='N')
br=1;
if(c=='S')
br=2;
}
}
}
}
}
// fn_vLogPointersInfosForOneBlock( i );
CRB_fn_vSavePtrTable (i , hFile);
}
// fn_vEndLogPointersInfos();
CRB_fn_vClosePtrTable (hFile);
}
// Build and save relocation table for sound pointers.
// Sound datas require a special function because they contain
// some data that are not pointers but change at each load.
void CRB_fn_vBuildPointerTableForSndPtr(unsigned long ulNbBloc,char *szFilename, char *szType)
{
unsigned long i,j;
char *p_b0,*p_b1;
unsigned char ucDestBloc,ucNotPossible;
HANDLE hFile;
hFile = CRB_fn_vOpenPtrTable (szFilename , ulNbBloc);
// For stats
// fn_vBeginLogPointersInfosForGlobalPointers( szType );
ucNotPossible=1;
g_ulNbBadPtr=0;
for(i=0;i<ulNbBloc;i++)
{
// GuS: Don't compute relocation for empty blocs.
if( g_p_stPtrMemory0[i]->ulSize == 0 ) continue;
g_ulNbPtr=0;
p_b0=(unsigned char *)g_p_stPtrMemory0[i]->cMemory;
p_b1=(unsigned char *)g_p_stPtrMemory1[i]->cMemory;
for(j=4;j<g_p_stPtrMemory0[i]->ulSize;j=j+8)
{
//lDiff=(*(unsigned long *)(p_b1+j))-(*(unsigned long*)(p_b0+j));
ucDestBloc=CRB_fn_bDestinationBloc(*((unsigned long *)(p_b0+j)));
// fn_vLogAddPointer( i, ucDestBloc );
if(ucDestBloc!=(unsigned char)-1)
{
// Add store of the address of this ptr and the aim bloc.
g_ulBlocPtrTable[g_ulNbPtr]=*(unsigned long *)(p_b0+j);
g_ucBlocPtrDestTable[g_ulNbPtr]=ucDestBloc;
g_ulNbPtr++;
}
else
{
g_ulNbBadPtr++;
printf("\nWarning - A SoundPointer (%ld) point out of Blocs !",(j-4)>>3);
}
}
// fn_vLogPointersInfosForOneBlock( i );
CRB_fn_vSavePtrTable (i , hFile);
}
/// fn_vEndLogPointersInfos();
CRB_fn_vClosePtrTable (hFile);
}
// Build correspondance table for bloc number in the 2 SNA files.
void CRB_fn_vTestBlocTable(unsigned long ulNbBloc)
{
unsigned long i,OK;
OK=1;
for(i=0;i<ulNbBloc;i++)
{
if( (g_p_stMemory1[i]->ulBeginBloc - g_p_stMemory0[i]->ulBeginBloc) == 0 )
{
printf("\nFATAL ERROR - Module %d Bloc %d has not moved ! \n Relocation files may be incorrect",g_p_stMemory0[i]->ulModuleNumber,g_p_stMemory0[i]->ulBlocNumber);
OK=0;
}
}
if(OK==0)
{
getchar();
exit(3);
}
}
int main(int argc, char *argv[])
{
char szFilename0[255],szFilename1[255];
char *p_cl;
unsigned char ucFix;
unsigned long ulNbBloc;
// getch();
g_ucAvoidAllEntries=0;
g_bGlideMode=1;
if(argv[2])
{
strcpy(szFilename0,argv[2]);
p_cl=strstr(szFilename0,"-");
if( toupper(*(p_cl+1))=='S' )
g_ucAvoidAllEntries=1;
if (toupper(*(p_cl+1))=='D' )
g_bGlideMode=0;
}
if(argv[3])
{
strcpy(szFilename0,argv[3]);
p_cl=strstr(szFilename0,"-");
if( toupper(*(p_cl+1))=='S' )
g_ucAvoidAllEntries=1;
if (toupper(*(p_cl+1))=='D' )
g_bGlideMode=0;
}
CRB_InitArray();
if( strcmp(_strupr(argv[1]),"FIX") == 0 )
{
// Read blocks from the fix
ulNbBloc=CRB_fn_bReadAllMemoryBlocks( "Fix0.sna", g_p_stMemory0, C_CRB_NEW );
g_ulNbBloc=CRB_fn_bReadAllMemoryBlocks( "Fix1.sna", g_p_stMemory1, C_CRB_NEW );
ucFix = 1;
}
else
{
// Read blocs of the level if we compute relocation for level.
// Those block also contains reloc infos about blocks in the fix.
strcpy(szFilename0,argv[1]);
strcat(szFilename0,"0.sna");
ulNbBloc=CRB_fn_bReadAllMemoryBlocks(szFilename0,g_p_stMemory0,C_CRB_ADD);
strcpy(szFilename1,argv[1]);
strcat(szFilename1,"1.sna");
g_ulNbBloc=CRB_fn_bReadAllMemoryBlocks(szFilename1,g_p_stMemory1,C_CRB_ADD);
ucFix = 0;
}
if(g_ulNbBloc!=ulNbBloc)
{
printf("\nERROR - Incompatible SNA Files, Number of Bloc is Different !");
getchar();
exit(4);
}
// Build correspondance table and write pointer table to disc.
CRB_fn_vTestBlocTable(ulNbBloc);
strcpy(szFilename0,argv[1]);
strcat(szFilename0,".rtb");
if(ucFix==1)
CRB_fn_vBuildPointerTableForFix(ulNbBloc,szFilename0);
else
CRB_fn_vBuildPointerTableForLevel(ulNbBloc,szFilename0);
// Relocation table for global pointers
strcpy(szFilename0,argv[1]);
strcat(szFilename0,"0.gpt");
ulNbBloc=CRB_fn_bReadPtrBlocks(szFilename0,g_p_stPtrMemory0, "Global");
strcpy(szFilename1,argv[1]);
strcat(szFilename1,"1.gpt");
ulNbBloc=CRB_fn_bReadPtrBlocks(szFilename1,g_p_stPtrMemory1, "Global");
strcpy(szFilename0,argv[1]);
strcat(szFilename0,".rtp");
CRB_fn_vBuildPointerTableForPtr(ulNbBloc,szFilename0, "Global");
// Relocation table for Texture pointers
strcpy(szFilename0,argv[1]);
strcat(szFilename0,"0.ptx");
ulNbBloc=CRB_fn_bReadPtrBlocks(szFilename0 ,g_p_stPtrMemory0, "Texture");
strcpy(szFilename0,argv[1]);
strcat(szFilename0,"1.ptx");
ulNbBloc=CRB_fn_bReadPtrBlocks(szFilename0 ,g_p_stPtrMemory1, "Texture");
strcpy(szFilename0,argv[1]);
strcat(szFilename0,".rtt");
CRB_fn_vBuildPointerTableForPtr(ulNbBloc,szFilename0, "Texture");
// Relocation table for sound pointers
strcpy(szFilename0,argv[1]);
strcat(szFilename0,"0.snd");
ulNbBloc=CRB_fn_bReadPtrBlocks(szFilename0,g_p_stPtrMemory0, "Sound" );
strcpy(szFilename1,argv[1]);
strcat(szFilename1,"1.snd");
ulNbBloc=CRB_fn_bReadPtrBlocks(szFilename1,g_p_stPtrMemory1, "Sound" );
strcpy(szFilename0,argv[1]);
strcat(szFilename0,".rts");
CRB_fn_vBuildPointerTableForSndPtr(ulNbBloc,szFilename0, "Sound" );
// Relocation table for vignette pointers
if( ucFix==0 )
{
strcpy(szFilename0,argv[1]);
strcat(szFilename0,"0.vgm");
ulNbBloc=CRB_fn_bReadPtrBlocks(szFilename0,g_p_stPtrMemory0, "Vignette" );
strcpy(szFilename1,argv[1]);
strcat(szFilename1,"1.vgm");
ulNbBloc=CRB_fn_bReadPtrBlocks(szFilename1,g_p_stPtrMemory1, "Vignette" );
if( ulNbBloc > 0 )
{
strcpy(szFilename0,argv[1]);
strcat(szFilename0,".rtv");
CRB_fn_vBuildPointerTableForPtr(ulNbBloc,szFilename0, "Vignette" );
}
}
// MR0912
if(ucFix!=0)
{
strcpy(szFilename0,argv[1]);
strcat(szFilename0,"0.lsy");
ulNbBloc=CRB_fn_bReadPtrBlocks(szFilename0,g_p_stPtrMemory0, "LipsSync" );
strcpy(szFilename1,argv[1]);
strcat(szFilename1,"1.lsy");
ulNbBloc=CRB_fn_bReadPtrBlocks(szFilename1,g_p_stPtrMemory1, "LipsSync" );
if( ulNbBloc > 0 )
{
strcpy(szFilename0,argv[1]);
strcat(szFilename0,".rtl");
CRB_fn_vBuildPointerTableForPtr(ulNbBloc,szFilename0, "LipsSync" );
}
}
printf("\n\nConversion Completed OK !!! \n");
// getch();
/*
if(g_ucAvoidAllEntries==0)
getch();
*/
return 0;
}