@ -193,10 +193,28 @@ namespace Hack.Xenosaga.Process
return size ;
}
private static int findFile ( pathElement entry )
{
// Check if the file is in the INSERT directory
if ( File . Exists ( Variables . dirInsert + entry . Name ) )
return 1 ;
// Check if the file is in the UNPACK directory
if ( File . Exists ( Variables . dirUnpack + entry . FullPath ) )
return 2 ;
// Otherwise, get the file from iso files
return - 1 ;
}
#endregion
#region Public methods
/// <summary>
/// Unpack all files from an index
/// </summary>
/// <param name="indexName">Pathname of the index</param>
public static void unpackIsoFiles ( string indexName )
{
BinaryReader br = null ;
@ -211,7 +229,7 @@ namespace Hack.Xenosaga.Process
listPathElement index = new listPathElement ( ) ;
Console . WriteLine ( "Traitement de l'index {0} ", indexName ) ;
Trace . Write ( string . Format ( "Reading index ({0}) : ", indexName ) ) ;
// Lecture de l'index
try
@ -221,19 +239,23 @@ namespace Hack.Xenosaga.Process
}
catch ( Exception ex )
{
Consol e. WriteLine ( " (EE) Erreur lors de la lecture de l'index : {0}", ex . Message ) ;
Trac e. WriteLine ( string . Format ( "ERROR : {0}", ex . Message ) ) ;
return ;
}
Trace . WriteLine ( "OK" ) ;
iIndexSize = bIndexNbSector * _sectorSize ;
try
{
Trace . WriteLine ( "Extracting files" ) ;
Trace . Indent ( ) ;
foreach ( pathElement entryPath in index . getEntries ( ) )
{
if ( ! entryPath . IsDirectory )
{
Consol e. WriteLine ( " Ecriture du fichier {0}", entryPath . FullPath ) ;
Trac e. WriteLine ( string . Format ( "Create file {0}", entryPath . FullPath ) ) ;
Directory . CreateDirectory ( directoryName + Path . GetDirectoryName ( entryPath . FullPath ) ) ;
int id = getIdFile ( entryPath . Position , iIndexSize ) + numFileIndex ;
@ -281,11 +303,20 @@ namespace Hack.Xenosaga.Process
}
catch ( Exception ex )
{
Consol e. WriteLine ( " (EE) Erreur lors de l'extraction des fichiers : {0}", ex . Message ) ;
Trac e. WriteLine ( string . Format ( "==> ERROR : {0}", ex . Message ) ) ;
return ;
}
finally
{
Trace . Unindent ( ) ;
}
}
/// <summary>
/// Pack all files to index
/// </summary>
/// <param name="indexName">Pathname of the index</param>
/// <param name="regroup">Used to know if the program pack files in 1 ou more files</param>
public static void packIsoFiles ( string indexName , bool regroup )
{
BinaryReader br = null ;
@ -297,12 +328,13 @@ namespace Hack.Xenosaga.Process
int numFileWrite = - 1 ;
int idSave = - 1 ;
string fileNameBase = Path . GetFileNameWithoutExtension ( indexName ) ;
string filename = "" ;
int . TryParse ( Path . GetExtension ( indexName ) . Substring ( 1 ) , out numFileIndex ) ;
listPathElement index = new listPathElement ( ) ;
Console . WriteLine ( "Traitement de l'index {0} ", indexName ) ;
Trace . Write ( string . Format ( "Reading index ({0}) : ", indexName ) ) ;
// Lecture de l'index
try
@ -312,7 +344,7 @@ namespace Hack.Xenosaga.Process
}
catch ( Exception ex )
{
Consol e. WriteLine ( " (EE) Erreur lors de la lecture de l'index : {0}", ex . Message ) ;
Trac e. WriteLine ( string . Format ( "ERROR : {0}", ex . Message ) ) ;
return ;
}
@ -323,6 +355,9 @@ namespace Hack.Xenosaga.Process
numFileWrite = numFileIndex + 1 ;
try
{
Trace . WriteLine ( "Inserting files" ) ;
Trace . Indent ( ) ;
string s_pathname = string . Format ( "{0}{1}." , Variables . dirPack , fileNameBase ) ;
Directory . CreateDirectory ( Variables . dirPack ) ;
@ -332,11 +367,24 @@ namespace Hack.Xenosaga.Process
{
long size = 0 ;
if ( File . Exists ( Variables . dirInsert + entryPath . Name ) )
int idFile = findFile ( entryPath ) ;
if ( idFile = = 1 | | idFile = = 2 )
{
Console . WriteLine ( " Insertion du fichier {0}" , entryPath . FullPath ) ;
if ( idFile = = 1 )
{
Trace . WriteLine ( string . Format ( "From {0} : file {1}" , Variables . dirInsert , entryPath . FullPath ) ) ;
filename = Variables . dirInsert + entryPath . Name ;
}
else
{
Trace . WriteLine ( string . Format ( "From {0} : file {1}" , Variables . dirUnpack , entryPath . FullPath ) ) ;
filename = Variables . dirUnpack + entryPath . FullPath ;
}
using ( BinaryReader brFile = new BinaryReader ( File . Open ( Variables . dirInsert + entryPath . Name , FileMode . Open ) ) )
using ( BinaryReader brFile = new BinaryReader ( File . Open ( filen ame, FileMode . Open ) ) )
{
size = brFile . BaseStream . Length ;
@ -361,11 +409,12 @@ namespace Hack.Xenosaga.Process
}
else
{
Console . WriteLine ( " Copie du fichier {0}" , entryPath . FullPath ) ;
int id = getIdFile ( entryPath . Position , iIndexSize ) + numFileIndex ;
filename = string . Format ( "{0}.{1:D2}" , fileNameBase , id ) ;
size = entryPath . SizeIn ;
Trace . WriteLine ( string . Format ( "From {0} : file {1}" , filename , entryPath . FullPath ) ) ;
int id = getIdFile ( entryPath . Position , iIndex Size) + numFile Index ;
size = entryPath . SizeIn ;
double pos = ( entryPath . Position - iIndexSize ) % _maxSizeFile ;
@ -380,7 +429,7 @@ namespace Hack.Xenosaga.Process
if ( br = = null )
{
br = new BinaryReader ( File . Open ( string . Format ( "{0}.{1:D2}" , fileNameBase , id ) , FileMode . Open ) ) ;
br = new BinaryReader ( File . Open ( filename , FileMode . Open ) ) ;
idSave = id ;
}
@ -402,7 +451,8 @@ namespace Hack.Xenosaga.Process
br . Dispose ( ) ;
}
br = new BinaryReader ( File . Open ( string . Format ( "{0}.{1:D2}" , fileNameBase , num ) , FileMode . Open ) ) ;
filename = string . Format ( "{0}.{1:D2}" , fileNameBase , num ) ;
br = new BinaryReader ( File . Open ( filename , FileMode . Open ) ) ;
idSave = num ;
sizeTemp = AddFileToStream ( s_pathname , ref numFileWrite , br , ( int ) entryPath . SizeIn - size_rest , regroup ) ;
@ -423,13 +473,22 @@ namespace Hack.Xenosaga.Process
}
catch ( Exception ex )
{
Consol e. WriteLine ( "(EE) Erreur lors de l'insertion des fichiers : {0}", ex . Message ) ;
Trac e. WriteLine ( string . Format ( "==> ERROR : {0}", ex . Message ) ) ;
return ;
}
finally
{
Trace . Unindent ( ) ;
}
}
/// <summary>
/// List files of an index
/// </summary>
/// <param name="indexName">Pathname of the index</param>
public static void listFiles ( string indexName )
{
string outputName ;
byte bIndexNbSector = 0 ;
int iIndexSize ;
int numFile ;
@ -437,16 +496,16 @@ namespace Hack.Xenosaga.Process
listPathElement index = new listPathElement ( ) ;
Console . WriteLine ( "Traitement de l'index {0} ", indexName ) ;
Trace . Write ( string . Format ( "Reading index file ({0}) : ", indexName ) ) ;
try
{
Directory . CreateDirectory ( Variables . dirUnpack ) ;
outputName = string . Format ( "{0}{1}{2}" , Variables . dirUnpack , indexName , _listExtension ) ;
using ( StreamWriter sw = new StreamWriter ( string . Format ( "{0}{1}{2}" , Variables . dirUnpack , indexName , _listExtension ) ) )
using ( StreamWriter sw = new StreamWriter ( outputName ) )
{
TextWriterTraceListener twtl_trace = new TextWriterTraceListener ( sw ) ;
Trace . Listeners . Add ( twtl_trace ) ;
Functions . ManageListener ( false , true , sw ) ;
bIndexNbSector = readIndex ( indexName , index ) ;
index . SortBySector ( ) ;
@ -462,11 +521,15 @@ namespace Hack.Xenosaga.Process
Trace . WriteLine ( string . Format ( "{0,-36}Sector={1,-15}SizeIn={2,-15}SizeOut={3,-15}File=xenosaga.{4:D2}" , entryPath . FullPath , entryPath . Sector , entryPath . SizeIn , entryPath . SizeOut , id + numFile ) ) ;
}
}
Functions . ManageListener ( true , false ) ;
}
Trace . WriteLine ( "OK" ) ;
}
catch ( Exception ex )
{
Consol e. WriteLine ( " (EE) Erreur lors de la lecture de l'index : {0}", ex . Message ) ;
Trac e. WriteLine ( string . Format ( "ERROR : {0}", ex . Message ) ) ;
return ;
}
}