Files
psxt001z/psxt001z.Library/FileTools.cs

144 lines
4.1 KiB
C#
Raw Permalink Normal View History

2023-09-18 00:46:20 -04:00
using System;
using System.IO;
using System.Text;
2024-11-15 23:41:33 -05:00
using SabreTools.IO.Extensions;
2023-09-18 00:46:20 -04:00
namespace psxt001z
{
/// <see href="https://github.com/Dremora/psxt001z/blob/master/main.cpp"/>
2023-11-16 23:29:20 -05:00
public class FileTools(Stream file)
2023-09-18 00:46:20 -04:00
{
2023-11-16 23:10:54 -05:00
private readonly Stream _file = file;
2023-09-18 00:46:20 -04:00
2023-11-16 23:10:54 -05:00
private readonly byte[] _executableName = new byte[20];
2023-09-18 00:46:20 -04:00
2023-11-16 23:10:54 -05:00
private readonly byte[] _dateValue = new byte[11];
2023-09-18 00:46:20 -04:00
2023-11-16 22:55:20 -05:00
public long Size() => _file.Length;
2023-09-18 00:46:20 -04:00
2023-11-16 22:55:20 -05:00
public string GetExecutableName()
2023-09-18 00:46:20 -04:00
{
2023-11-16 22:55:20 -05:00
_file.Seek(51744, SeekOrigin.Begin);
2023-09-18 00:46:20 -04:00
string filename = string.Empty;
while (filename != "SYSTEM.CNF")
{
2024-11-15 23:41:33 -05:00
byte[] buf = _file.ReadBytes(10);
2023-09-18 00:46:20 -04:00
filename = Encoding.ASCII.GetString(buf);
2023-11-16 22:55:20 -05:00
_file.Seek(-9, SeekOrigin.Current);
2023-09-18 00:46:20 -04:00
}
2023-11-16 22:55:20 -05:00
_file.Seek(-32, SeekOrigin.Current);
2024-11-15 23:41:33 -05:00
uint lba = _file.ReadUInt32();
2023-09-18 00:46:20 -04:00
2023-11-16 22:55:20 -05:00
_file.Seek((2352 * lba) + 29, SeekOrigin.Begin);
2024-11-15 23:41:33 -05:00
byte[] buffer = _file.ReadBytes(6);
2023-09-18 00:46:20 -04:00
string iniLine = Encoding.ASCII.GetString(buffer);
while (iniLine != "cdrom:")
{
2023-11-16 22:55:20 -05:00
_file.Seek(-5, SeekOrigin.Current);
2024-11-15 23:41:33 -05:00
buffer = _file.ReadBytes(6);
2023-09-18 00:46:20 -04:00
iniLine = Encoding.ASCII.GetString(buffer);
}
2024-11-15 23:41:33 -05:00
buffer = _file.ReadBytes(1);
2023-09-18 00:46:20 -04:00
if (buffer[0] != '\\')
2023-11-16 22:55:20 -05:00
_file.Seek(-1, SeekOrigin.Current);
2023-09-18 00:46:20 -04:00
int i = -1;
do
{
2024-11-15 23:41:33 -05:00
_ = _file.Read(buffer, ++i, 1);
2023-09-18 00:46:20 -04:00
} while (buffer[i] != ';');
for (long a = 0; a < i; a++)
{
2023-11-16 22:55:20 -05:00
_executableName[a] = (byte)char.ToUpper((char)buffer[a]);
2023-09-18 00:46:20 -04:00
}
2023-11-16 22:55:20 -05:00
return Encoding.ASCII.GetString(_executableName);
2023-09-18 00:46:20 -04:00
}
2023-11-16 22:55:20 -05:00
public string GetDate()
2023-09-18 00:46:20 -04:00
{
2023-11-16 22:55:20 -05:00
_file.Seek(51744, SeekOrigin.Begin);
2023-09-18 00:46:20 -04:00
2024-11-15 23:41:33 -05:00
byte[] buffer = new byte[12];
2023-09-18 00:46:20 -04:00
do
{
2024-11-15 23:41:33 -05:00
_ = _file.Read(buffer, 0, 11);
2023-09-18 00:46:20 -04:00
buffer[11] = 0;
2023-11-16 22:55:20 -05:00
_file.Seek(-10, SeekOrigin.Current);
} while (Encoding.ASCII.GetString(_executableName) != Encoding.ASCII.GetString(buffer));
2023-09-18 00:46:20 -04:00
2023-11-16 22:55:20 -05:00
_file.Seek(-16, SeekOrigin.Current);
2023-09-18 00:46:20 -04:00
2024-11-15 23:41:33 -05:00
byte[] datenofrmt = _file.ReadBytes(3);
2023-09-18 00:46:20 -04:00
if (datenofrmt[0] < 50)
{
byte[] year = Encoding.ASCII.GetBytes($"{2000 + datenofrmt[0]}");
Array.Copy(year, 0, buffer, 0, 4);
}
else
{
byte[] year = Encoding.ASCII.GetBytes($"{1900 + datenofrmt[0]}");
Array.Copy(year, 0, buffer, 0, 4);
}
2023-11-16 22:55:20 -05:00
_dateValue[4] = (byte)'-';
2023-09-18 00:46:20 -04:00
if (datenofrmt[1] < 10)
{
byte[] month = Encoding.ASCII.GetBytes($"0{datenofrmt[1]}");
Array.Copy(month, 0, buffer, 5, 2);
}
else
{
byte[] month = Encoding.ASCII.GetBytes($"{datenofrmt[1]}");
Array.Copy(month, 0, buffer, 5, 2);
}
2023-11-16 22:55:20 -05:00
_dateValue[7] = (byte)'-';
2023-09-18 00:46:20 -04:00
if (datenofrmt[2] < 10)
{
byte[] day = Encoding.ASCII.GetBytes($"0{datenofrmt[2]}");
Array.Copy(day, 0, buffer, 8, 2);
}
else
{
byte[] day = Encoding.ASCII.GetBytes($"{datenofrmt[2]}");
Array.Copy(day, 0, buffer, 8, 2);
}
2023-11-16 22:55:20 -05:00
return Encoding.ASCII.GetString(_dateValue);
2023-09-18 00:46:20 -04:00
}
2023-11-16 22:55:20 -05:00
public int Resize(long newsize)
2023-09-18 00:46:20 -04:00
{
2023-11-16 22:55:20 -05:00
long oldsize = Size();
2023-09-18 00:46:20 -04:00
if (oldsize < newsize)
{
2023-11-16 22:55:20 -05:00
_file.SetLength(newsize);
2023-09-18 00:46:20 -04:00
return 1;
}
else if (oldsize > newsize)
{
2023-11-16 22:55:20 -05:00
_file.SetLength(newsize);
2023-09-18 00:46:20 -04:00
return 2;
}
else
{
return 0;
}
}
2023-11-16 22:55:20 -05:00
public int GetImageSize()
2023-09-18 00:46:20 -04:00
{
2023-11-16 22:55:20 -05:00
_file.Seek(0x9368, SeekOrigin.Begin);
2023-09-18 00:46:20 -04:00
byte[] sizebuf = new byte[4];
2024-11-15 23:41:33 -05:00
_ = _file.Read(sizebuf, 0, 4);
2023-09-18 00:46:20 -04:00
return BitConverter.ToInt32(sizebuf, 0);
}
}
}