Browse Source

PACK - check if file exists in UNPACK directory

Rewrite some little things
master
BahaBulle 8 years ago
parent
commit
cd422bdf70
6 changed files with 162 additions and 44 deletions
  1. +7
    -1
      .gitignore
  2. +52
    -15
      Hack.Xenosaga/Common/Functions.cs
  3. +4
    -6
      Hack.Xenosaga/Common/Variables.cs
  4. +10
    -0
      Hack.Xenosaga/Hack.Xenosaga.csproj
  5. +83
    -20
      Hack.Xenosaga/Process/Unpack.cs
  6. +6
    -2
      Hack.Xenosaga/Xenosaga.cs

+ 7
- 1
.gitignore View File

@ -242,4 +242,10 @@ ModelManifest.xml
.paket/paket.exe .paket/paket.exe
# FAKE - F# Make # FAKE - F# Make
.fake/
.fake/
/Hack.Xenosaga/Include/cCalculation.dll
/Hack.Xenosaga/Process/Unpack_copie_depuis_iso.cs
/Hack.Xenosaga/Process/Scripts.cs
/Hack.Xenosaga/Include/cTable.dll
/Hack.Xenosaga/Include/cPointers.dll
/Hack.Xenosaga/App.config

+ 52
- 15
Hack.Xenosaga/Common/Functions.cs View File

@ -1,5 +1,6 @@
using System;
using System.Diagnostics;
using System.IO; using System.IO;
using System.Text;
namespace Hack.Xenosaga.Common namespace Hack.Xenosaga.Common
{ {
@ -7,49 +8,85 @@ namespace Hack.Xenosaga.Common
{ {
public static void usage() public static void usage()
{ {
Console.WriteLine("xenosaga <option> <file> [regroup]");
Console.WriteLine(" option : -l = List files from index");
Console.WriteLine(" -u = Unpack files from index");
Console.WriteLine(" -p = Pack files from index");
Console.WriteLine(" file : Index file (the first one of each decade : xenosaga.00, xenosaga.10, xenosaga.20...)");
Console.WriteLine(" regroup : true to regroup all files in only one (ex: 11, 12, 13 in 11) ; false or empty (default) to keep the same system of increment files");
Trace.WriteLine("");
Trace.WriteLine("xenosaga <option> <file> [regroup]");
Trace.WriteLine(" option : -l = List files from index");
Trace.WriteLine(" -u = Unpack files from index");
Trace.WriteLine(" -p = Pack files from index");
Trace.WriteLine(" file : Index file (the first one of each decade : xenosaga.00, xenosaga.10, xenosaga.20...)");
Trace.WriteLine(" regroup : true to regroup all files in only one (ex: 11, 12, 13 in 11) ; false or empty (default) to keep the same system of increment files");
} }
public static bool checkArgs(string[] args, out Variables.stArgs listArgs)
public static bool CheckArgs(string[] args, out Variables.stArgs listArgs)
{ {
listArgs = new Variables.stArgs(); listArgs = new Variables.stArgs();
Trace.Write("Checking arguments : ");
if (args.Length != 2 && args.Length != 3) if (args.Length != 2 && args.Length != 3)
{ {
Console.WriteLine("Incorrect number of parameter!");
Trace.WriteLine("Incorrect number of parameter!");
return false; return false;
} }
if (args[0] != "-l" && args[0] != "-p" && args[0] != "-u")
if (args[0] != "-l" && args[0] != "-p" && args[0] != "-u" && args[0] != "-e")
{ {
Console.WriteLine("Incorrect parameter 1!");
Trace.WriteLine("Incorrect parameter - unknown <option>");
return false; return false;
} }
if (!File.Exists(args[1])) if (!File.Exists(args[1]))
{ {
Console.WriteLine("Incorrect parameter 2 : unknown file");
Trace.WriteLine("Incorrect parameter - unknown <file>");
return false; return false;
} }
if (args.Length == 3 && (args[2] != "true" && args[2] != "false"))
if ((args.Length == 3 && args[0] == "-p") && (args[2] != "true" && args[2] != "false"))
{ {
Console.WriteLine("Incorrect parameter 3!");
Trace.WriteLine("Incorrect parameter - unknown <regroup>");
return false; return false;
} }
listArgs.option = args[0]; listArgs.option = args[0];
listArgs.filename = args[1]; listArgs.filename = args[1];
if (args.Length == 3)
if (args.Length == 3 && listArgs.option == "-p")
listArgs.regroup = args[2] == "true" ? true : false; listArgs.regroup = args[2] == "true" ? true : false;
Trace.WriteLine("OK");
return true; return true;
} }
public static void ManageListener(bool consoleTrace, bool fileTrace = false, StreamWriter sw = null)
{
string traceFileName = "TraceFile";
string traceConsoleName = "TraceConsole";
for (int i = 0; i < Trace.Listeners.Count; i++)
{
TraceListener trace = Trace.Listeners[i];
if (trace.Name == "Default" || (trace.Name == traceConsoleName && !consoleTrace) || (trace.Name == traceFileName && !fileTrace))
{
Trace.Listeners.Remove(trace);
trace.Dispose();
}
}
if (consoleTrace)
{
ConsoleTraceListener traceConsole = new ConsoleTraceListener();
traceConsole.Name = traceConsoleName;
Trace.Listeners.Add(traceConsole);
}
if (fileTrace)
{
TextWriterTraceListener traceFile = new TextWriterTraceListener(sw);
traceFile.Name = traceFileName;
Trace.Listeners.Add(traceFile);
}
}
} }
} }

+ 4
- 6
Hack.Xenosaga/Common/Variables.cs View File

@ -1,9 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Hack.Xenosaga.Common namespace Hack.Xenosaga.Common
{ {
public class Variables public class Variables
@ -19,5 +14,8 @@ namespace Hack.Xenosaga.Common
public const string dirExtract = "02-EXTRACT/"; public const string dirExtract = "02-EXTRACT/";
public const string dirInsert = "03-INSERT/"; public const string dirInsert = "03-INSERT/";
public const string dirPack = "04-PACK/"; public const string dirPack = "04-PACK/";
public const string tblCard = "TABLES/card_ANSI.tbl";
} }
} }

+ 10
- 0
Hack.Xenosaga/Hack.Xenosaga.csproj View File

@ -39,6 +39,15 @@
<StartupObject>Hack.Xenosaga.Xenosaga</StartupObject> <StartupObject>Hack.Xenosaga.Xenosaga</StartupObject>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<Reference Include="cCalculation">
<HintPath>Include\cCalculation.dll</HintPath>
</Reference>
<Reference Include="cPointers">
<HintPath>Include\cPointers.dll</HintPath>
</Reference>
<Reference Include="cTable">
<HintPath>Include\cTable.dll</HintPath>
</Reference>
<Reference Include="System" /> <Reference Include="System" />
<Reference Include="System.Core" /> <Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" /> <Reference Include="System.Xml.Linq" />
@ -51,6 +60,7 @@
<Compile Include="Common\Functions.cs" /> <Compile Include="Common\Functions.cs" />
<Compile Include="Common\PathElement.cs" /> <Compile Include="Common\PathElement.cs" />
<Compile Include="Common\Variables.cs" /> <Compile Include="Common\Variables.cs" />
<Compile Include="Process\Scripts.cs" />
<Compile Include="Process\Unpack.cs" /> <Compile Include="Process\Unpack.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Xenosaga.cs" /> <Compile Include="Xenosaga.cs" />


+ 83
- 20
Hack.Xenosaga/Process/Unpack.cs View File

@ -193,10 +193,28 @@ namespace Hack.Xenosaga.Process
return size; 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 #endregion
#region Public methods #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) public static void unpackIsoFiles(string indexName)
{ {
BinaryReader br = null; BinaryReader br = null;
@ -211,7 +229,7 @@ namespace Hack.Xenosaga.Process
listPathElement index = new listPathElement(); listPathElement index = new listPathElement();
Console.WriteLine("Traitement de l'index {0}", indexName);
Trace.Write(string.Format("Reading index ({0}) : ", indexName));
// Lecture de l'index // Lecture de l'index
try try
@ -221,19 +239,23 @@ namespace Hack.Xenosaga.Process
} }
catch (Exception ex) catch (Exception ex)
{ {
Console.WriteLine(" (EE) Erreur lors de la lecture de l'index : {0}", ex.Message);
Trace.WriteLine(string.Format("ERROR : {0}", ex.Message));
return; return;
} }
Trace.WriteLine("OK");
iIndexSize = bIndexNbSector * _sectorSize; iIndexSize = bIndexNbSector * _sectorSize;
try try
{ {
Trace.WriteLine("Extracting files");
Trace.Indent();
foreach (pathElement entryPath in index.getEntries()) foreach (pathElement entryPath in index.getEntries())
{ {
if (!entryPath.IsDirectory) if (!entryPath.IsDirectory)
{ {
Console.WriteLine(" Ecriture du fichier {0}", entryPath.FullPath);
Trace.WriteLine(string.Format("Create file {0}", entryPath.FullPath));
Directory.CreateDirectory(directoryName + Path.GetDirectoryName(entryPath.FullPath)); Directory.CreateDirectory(directoryName + Path.GetDirectoryName(entryPath.FullPath));
int id = getIdFile(entryPath.Position, iIndexSize) + numFileIndex; int id = getIdFile(entryPath.Position, iIndexSize) + numFileIndex;
@ -281,11 +303,20 @@ namespace Hack.Xenosaga.Process
} }
catch (Exception ex) catch (Exception ex)
{ {
Console.WriteLine(" (EE) Erreur lors de l'extraction des fichiers : {0}", ex.Message);
Trace.WriteLine(string.Format("==> ERROR : {0}", ex.Message));
return; 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) public static void packIsoFiles(string indexName, bool regroup)
{ {
BinaryReader br = null; BinaryReader br = null;
@ -297,12 +328,13 @@ namespace Hack.Xenosaga.Process
int numFileWrite = -1; int numFileWrite = -1;
int idSave = -1; int idSave = -1;
string fileNameBase = Path.GetFileNameWithoutExtension(indexName); string fileNameBase = Path.GetFileNameWithoutExtension(indexName);
string filename = "";
int.TryParse(Path.GetExtension(indexName).Substring(1), out numFileIndex); int.TryParse(Path.GetExtension(indexName).Substring(1), out numFileIndex);
listPathElement index = new listPathElement(); listPathElement index = new listPathElement();
Console.WriteLine("Traitement de l'index {0}", indexName);
Trace.Write(string.Format("Reading index ({0}) : ", indexName));
// Lecture de l'index // Lecture de l'index
try try
@ -312,7 +344,7 @@ namespace Hack.Xenosaga.Process
} }
catch (Exception ex) catch (Exception ex)
{ {
Console.WriteLine(" (EE) Erreur lors de la lecture de l'index : {0}", ex.Message);
Trace.WriteLine(string.Format("ERROR : {0}", ex.Message));
return; return;
} }
@ -323,6 +355,9 @@ namespace Hack.Xenosaga.Process
numFileWrite = numFileIndex + 1; numFileWrite = numFileIndex + 1;
try try
{ {
Trace.WriteLine("Inserting files");
Trace.Indent();
string s_pathname = string.Format("{0}{1}.", Variables.dirPack, fileNameBase); string s_pathname = string.Format("{0}{1}.", Variables.dirPack, fileNameBase);
Directory.CreateDirectory(Variables.dirPack); Directory.CreateDirectory(Variables.dirPack);
@ -332,11 +367,24 @@ namespace Hack.Xenosaga.Process
{ {
long size = 0; 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(filename, FileMode.Open)))
{ {
size = brFile.BaseStream.Length; size = brFile.BaseStream.Length;
@ -361,11 +409,12 @@ namespace Hack.Xenosaga.Process
} }
else 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, iIndexSize) + numFileIndex;
size = entryPath.SizeIn;
double pos = (entryPath.Position - iIndexSize) % _maxSizeFile; double pos = (entryPath.Position - iIndexSize) % _maxSizeFile;
@ -380,7 +429,7 @@ namespace Hack.Xenosaga.Process
if (br == null) 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; idSave = id;
} }
@ -402,7 +451,8 @@ namespace Hack.Xenosaga.Process
br.Dispose(); 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; idSave = num;
sizeTemp = AddFileToStream(s_pathname, ref numFileWrite, br, (int)entryPath.SizeIn - size_rest, regroup); sizeTemp = AddFileToStream(s_pathname, ref numFileWrite, br, (int)entryPath.SizeIn - size_rest, regroup);
@ -423,13 +473,22 @@ namespace Hack.Xenosaga.Process
} }
catch (Exception ex) catch (Exception ex)
{ {
Console.WriteLine("(EE) Erreur lors de l'insertion des fichiers : {0}", ex.Message);
Trace.WriteLine(string.Format("==> ERROR : {0}", ex.Message));
return; 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) public static void listFiles(string indexName)
{ {
string outputName;
byte bIndexNbSector = 0; byte bIndexNbSector = 0;
int iIndexSize; int iIndexSize;
int numFile; int numFile;
@ -437,16 +496,16 @@ namespace Hack.Xenosaga.Process
listPathElement index = new listPathElement(); listPathElement index = new listPathElement();
Console.WriteLine("Traitement de l'index {0}", indexName);
Trace.Write(string.Format("Reading index file ({0}) : ", indexName));
try try
{ {
Directory.CreateDirectory(Variables.dirUnpack); 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); bIndexNbSector = readIndex(indexName, index);
index.SortBySector(); 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)); 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) catch (Exception ex)
{ {
Console.WriteLine(" (EE) Erreur lors de la lecture de l'index : {0}", ex.Message);
Trace.WriteLine(string.Format("ERROR : {0}", ex.Message));
return; return;
} }
} }


+ 6
- 2
Hack.Xenosaga/Xenosaga.cs View File

@ -1,4 +1,5 @@
using Hack.Xenosaga.Common;
using System.Diagnostics;
using Hack.Xenosaga.Common;
using Hack.Xenosaga.Process; using Hack.Xenosaga.Process;
namespace Hack.Xenosaga namespace Hack.Xenosaga
@ -8,8 +9,11 @@ namespace Hack.Xenosaga
public static void Main(string[] args) public static void Main(string[] args)
{ {
Variables.stArgs listArgs; Variables.stArgs listArgs;
Functions.ManageListener(true);
if (!Functions.checkArgs(args, out listArgs))
Trace.WriteLine("Hack.Xenosaga - (c) 2016 BahaBulle\n");
if (!Functions.CheckArgs(args, out listArgs))
{ {
Functions.usage(); Functions.usage();
return; return;


Loading…
Cancel
Save