From c1a0f9f59096367d1cac9bbfe6f44bcd3a4488e8 Mon Sep 17 00:00:00 2001 From: Michael Becker Date: Fri, 15 Nov 2019 12:10:22 -0500 Subject: [PATCH] AFS file format loading seems to work OK so far... --- .../FileSystem/Associations/AFS.uexml | 25 ++++ ...lEditor.Content.PlatformIndependent.csproj | 1 + .../FileSystem/AFS/AFSDataFormat.cs | 111 ++++++++++++++++++ .../DataFormats/FileSystem/AFS/AFSFileInfo.cs | 32 +++++ .../UniversalEditor.Plugins.FileSystem.csproj | 3 + 5 files changed, 172 insertions(+) create mode 100644 CSharp/Content/UniversalEditor.Content.PlatformIndependent/Extensions/FileSystem/Associations/AFS.uexml create mode 100644 CSharp/Plugins/UniversalEditor.Plugins.FileSystem/DataFormats/FileSystem/AFS/AFSDataFormat.cs create mode 100644 CSharp/Plugins/UniversalEditor.Plugins.FileSystem/DataFormats/FileSystem/AFS/AFSFileInfo.cs diff --git a/CSharp/Content/UniversalEditor.Content.PlatformIndependent/Extensions/FileSystem/Associations/AFS.uexml b/CSharp/Content/UniversalEditor.Content.PlatformIndependent/Extensions/FileSystem/Associations/AFS.uexml new file mode 100644 index 00000000..f6bc6198 --- /dev/null +++ b/CSharp/Content/UniversalEditor.Content.PlatformIndependent/Extensions/FileSystem/Associations/AFS.uexml @@ -0,0 +1,25 @@ + + + + + + + + *.afs + + + + AFS + + + + + + + + + + + + + \ No newline at end of file diff --git a/CSharp/Content/UniversalEditor.Content.PlatformIndependent/UniversalEditor.Content.PlatformIndependent.csproj b/CSharp/Content/UniversalEditor.Content.PlatformIndependent/UniversalEditor.Content.PlatformIndependent.csproj index 75a3f775..b9b395cc 100644 --- a/CSharp/Content/UniversalEditor.Content.PlatformIndependent/UniversalEditor.Content.PlatformIndependent.csproj +++ b/CSharp/Content/UniversalEditor.Content.PlatformIndependent/UniversalEditor.Content.PlatformIndependent.csproj @@ -651,6 +651,7 @@ + diff --git a/CSharp/Plugins/UniversalEditor.Plugins.FileSystem/DataFormats/FileSystem/AFS/AFSDataFormat.cs b/CSharp/Plugins/UniversalEditor.Plugins.FileSystem/DataFormats/FileSystem/AFS/AFSDataFormat.cs new file mode 100644 index 00000000..41481bd6 --- /dev/null +++ b/CSharp/Plugins/UniversalEditor.Plugins.FileSystem/DataFormats/FileSystem/AFS/AFSDataFormat.cs @@ -0,0 +1,111 @@ +// +// AFSDataFormat.cs +// +// Author: +// Mike Becker +// +// Copyright (c) 2019 Mike Becker +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . +using System; + +using UniversalEditor.IO; +using UniversalEditor.ObjectModels.FileSystem; + +namespace UniversalEditor.DataFormats.FileSystem.AFS +{ + public class AFSDataFormat : DataFormat + { + private static DataFormatReference _dfr = null; + protected override DataFormatReference MakeReferenceInternal() + { + if (_dfr == null) + { + _dfr = base.MakeReferenceInternal(); + _dfr.Capabilities.Add(typeof(FileSystemObjectModel), DataFormatCapabilities.All); + } + return _dfr; + } + + protected override void LoadInternal(ref ObjectModel objectModel) + { + FileSystemObjectModel fsom = (objectModel as FileSystemObjectModel); + if (fsom == null) + throw new ObjectModelNotSupportedException(); + + Reader reader = Accessor.Reader; + string afs = reader.ReadFixedLengthString(4); + if (afs != "AFS\0") + throw new InvalidDataFormatException("file does not begin with \"AFS\\0\""); + + uint fileCount = reader.ReadUInt32(); + AFSFileInfo[] fileinfos = new AFSFileInfo[fileCount]; + + for (int i = 0; i < fileCount; i++) + { + fileinfos[i].offset = reader.ReadUInt32(); + fileinfos[i].length = reader.ReadUInt32(); + } + + uint tocOffset = 0u; + while (Accessor.Position < fileinfos[0].offset && tocOffset == 0) + { + tocOffset = reader.ReadUInt32(); + uint num3 = reader.ReadUInt32(); + } + if (tocOffset == 0) + { + throw new InvalidDataFormatException("table of contents not found"); + } + else + { + reader.Seek(tocOffset, SeekOrigin.Begin); + for (int j = 0; j < fileCount; j++) + { + fileinfos[j].name = reader.ReadFixedLengthString(32).TrimNull(); + + ushort year = reader.ReadUInt16(); + ushort month = reader.ReadUInt16(); + ushort day = reader.ReadUInt16(); + ushort hour = reader.ReadUInt16(); + ushort minute = reader.ReadUInt16(); + ushort second = reader.ReadUInt16(); + fileinfos[j].datetime = new DateTime(year, month, day, hour, minute, second); + fileinfos[j].length2 = reader.ReadUInt32(); + + File f = fsom.AddFile(fileinfos[j].name); + f.Properties.Add("fileinfo", fileinfos[j]); + f.Size = fileinfos[j].length; + f.ModificationTimestamp = fileinfos[j].datetime; + f.DataRequest += f_DataRequest; + } + } + } + + void f_DataRequest(object sender, DataRequestEventArgs e) + { + File f = (sender as File); + AFSFileInfo fileinfo = (AFSFileInfo)f.Properties["fileinfo"]; + + Accessor.Seek(fileinfo.offset, SeekOrigin.Begin); + e.Data = Accessor.Reader.ReadBytes(fileinfo.length); + } + + + protected override void SaveInternal(ObjectModel objectModel) + { + throw new NotImplementedException(); + } + } +} diff --git a/CSharp/Plugins/UniversalEditor.Plugins.FileSystem/DataFormats/FileSystem/AFS/AFSFileInfo.cs b/CSharp/Plugins/UniversalEditor.Plugins.FileSystem/DataFormats/FileSystem/AFS/AFSFileInfo.cs new file mode 100644 index 00000000..470a4c63 --- /dev/null +++ b/CSharp/Plugins/UniversalEditor.Plugins.FileSystem/DataFormats/FileSystem/AFS/AFSFileInfo.cs @@ -0,0 +1,32 @@ +// +// AFSFileInfo.cs +// +// Author: +// Mike Becker +// +// Copyright (c) 2019 Mike Becker +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . +using System; +namespace UniversalEditor.DataFormats.FileSystem.AFS +{ + public struct AFSFileInfo + { + public string name; + public uint offset; + public DateTime datetime; + public uint length; + public uint length2; + } +} diff --git a/CSharp/Plugins/UniversalEditor.Plugins.FileSystem/UniversalEditor.Plugins.FileSystem.csproj b/CSharp/Plugins/UniversalEditor.Plugins.FileSystem/UniversalEditor.Plugins.FileSystem.csproj index 5ff1d0e7..a42950f4 100644 --- a/CSharp/Plugins/UniversalEditor.Plugins.FileSystem/UniversalEditor.Plugins.FileSystem.csproj +++ b/CSharp/Plugins/UniversalEditor.Plugins.FileSystem/UniversalEditor.Plugins.FileSystem.csproj @@ -232,6 +232,8 @@ + + @@ -270,6 +272,7 @@ +