public bool Open(FileAccess access, int archiveHeader, string headerFile, string packageFile)
{
if (LogManager != null) {
LogManager.DebugFormat("Opening FileSystem: access={0}, archiveHeader={1}, headerFile={2}, packageFile={3}",
access, archiveHeader, headerFile, packageFile);
}
if (_IsOpened) {
return false;
}
if (!File.Exists(headerFile) || !File.Exists(packageFile)) {
if (LogManager != null) {
LogManager.Error("FileSystem open failed (FileNotFoundException)");
}
throw new FileNotFoundException();
}
if (IsFileLocked(headerFile) || IsFileLocked(packageFile)) {
if (LogManager != null) {
LogManager.Error("FileSystem open failed (UnauthorizedAccessException)");
}
throw new UnauthorizedAccessException();
}
this.Access = access;
this.ArchiveHeader = archiveHeader;
this.HeaderFile = headerFile;
this.PackageFile = packageFile;
this.ArchiveEntries.Clear();
using (BinaryReader reader = new BinaryReader(File.OpenRead(headerFile), Encoding.Default)) {
if (reader.ReadUInt32() != archiveHeader) {
if (LogManager != null) {
LogManager.Error("FileSystem open failed (FileFormatException)");
}
throw new FileFormatException();
}
uint entryCount = reader.ReadUInt32();
FileEntry entry;
for (uint e = 0; e < entryCount; e++) {
entry = new FileEntry();
if (reader.ReadUInt32() != 1) {
if (LogManager != null) {
LogManager.Error("FileSystem open failed (FileFormatException)");
}
throw new FileFormatException();
}
entry.SizeCurrent = reader.ReadUInt32();
entry.SizeAvailable = reader.ReadUInt32();
entry.Id = reader.ReadUInt32();
entry.Offset = reader.ReadInt64();
ArchiveEntries.Add(entry);
}
}
if (access == FileAccess.ReadWrite || access == FileAccess.Write) {
try {
MapWriter = new BinaryWriter(File.Open(HeaderFile, FileMode.OpenOrCreate, access, FileShare.None));
ArchiveStream = File.Open(PackageFile, FileMode.OpenOrCreate, access, FileShare.None);
} catch {
throw;
}
}
_IsOpened = true;
return true;
}