provide a way for embedded Accessors returned from a FileSystemObjectModel to access files relative to their location (not just physical files)

This commit is contained in:
Michael Becker 2020-05-31 19:36:11 -04:00
parent 47b478c1a8
commit 54e11b8da6
No known key found for this signature in database
GPG Key ID: 506F54899E2BFED7
16 changed files with 149 additions and 36 deletions

View File

@ -257,6 +257,12 @@ namespace UniversalEditor
{
if (_positions.Count > 0) _positions.Clear();
}
protected abstract Accessor GetRelativeInternal(string filename, string prefix = null);
public Accessor GetRelative(string filename, string prefix = null)
{
return GetRelativeInternal(filename, prefix);
}
#endregion
}
}

View File

@ -161,5 +161,24 @@ namespace UniversalEditor.Accessors
sb.Append(mvarFileName);
return sb.ToString();
}
protected override Accessor GetRelativeInternal(string filename, string prefix = null)
{
if (prefix == null)
{
string fn = GetFileName();
if (fn != null)
{
prefix = System.IO.Path.GetDirectoryName(fn);
}
}
string fullyQualifiedPath = MBS.Framework.IO.File.Find(System.IO.Path.Combine(new string[] { prefix, filename }), MBS.Framework.IO.CaseSensitiveHandling.CaseInsensitive);
if (System.IO.File.Exists(fullyQualifiedPath))
{
return new FileAccessor(fullyQualifiedPath);
}
return null;
}
}
}

View File

@ -174,5 +174,11 @@ namespace UniversalEditor.Accessors
if (mvarFileName != null) return System.IO.Path.GetFileName(mvarFileName);
return base.GetFileTitle();
}
protected override Accessor GetRelativeInternal(string filename, string prefix = null)
{
// default implementation is unavailable because there is no parent for a MemoryAccessor
return null;
}
}
}

View File

@ -114,5 +114,11 @@ namespace UniversalEditor.Accessors
{
mvarBaseStream.Flush();
}
protected override Accessor GetRelativeInternal(string filename, string prefix = null)
{
// FIXME: not implemented
return null;
}
}
}

View File

@ -145,5 +145,11 @@ namespace UniversalEditor.Accessors
protected override void CloseInternal()
{
}
protected override Accessor GetRelativeInternal(string filename, string prefix = null)
{
// FIXME: not implemented
return null;
}
}
}

View File

@ -0,0 +1,46 @@
//
// EmbeddedFileAccessor.cs
//
// Author:
// Michael Becker <alcexhim@gmail.com>
//
// Copyright (c) 2020 Mike Becker's Software
//
// 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 <http://www.gnu.org/licenses/>.
using System;
using UniversalEditor.ObjectModels.FileSystem;
namespace UniversalEditor.Accessors
{
public class EmbeddedFileAccessor : MemoryAccessor
{
public File File { get; private set; } = null;
public EmbeddedFileAccessor(File file) : base(file.GetData(), file.Name)
{
File = file;
}
protected override Accessor GetRelativeInternal(string filename, string prefix = null)
{
if (File.Parent != null)
{
File fRelative = File.Parent.FileSystem.FindFile(filename, MBS.Framework.IO.CaseSensitiveHandling.CaseInsensitive);
if (fRelative != null)
return new EmbeddedFileAccessor(fRelative);
}
return base.GetRelativeInternal(filename, prefix);
}
}
}

View File

@ -81,13 +81,14 @@ namespace UniversalEditor.ObjectModels.FileSystem
}
#endif
public File this[string Name]
public File this[string Name, MBS.Framework.IO.CaseSensitiveHandling caseSensitiveHandling = MBS.Framework.IO.CaseSensitiveHandling.System]
{
get
{
bool caseSensitive = (caseSensitiveHandling == MBS.Framework.IO.CaseSensitiveHandling.CaseSensitive || (caseSensitiveHandling == MBS.Framework.IO.CaseSensitiveHandling.System && System.Environment.OSVersion.Platform == PlatformID.Unix));
foreach (File file in this)
{
if (file.Name == Name)
if ((caseSensitive && file.Name == Name) || (!caseSensitive && file.Name.ToUpper() == Name.ToUpper()))
{
return file;
}
@ -101,8 +102,8 @@ namespace UniversalEditor.ObjectModels.FileSystem
return (this[Name] != null);
}
private Folder mvarParent = null;
public FileCollection(Folder parent = null)
private IFileSystemContainer mvarParent = null;
public FileCollection(IFileSystemContainer parent = null)
{
mvarParent = parent;
}
@ -111,6 +112,7 @@ namespace UniversalEditor.ObjectModels.FileSystem
{
base.InsertItem(index, item);
item.Parent = mvarParent;
item.FileSystem = mvarParent.FileSystem;
}
protected override void RemoveItem(int index)
{
@ -348,8 +350,10 @@ namespace UniversalEditor.ObjectModels.FileSystem
/// </summary>
public FileSource Source { get { return mvarSource; } set { mvarSource = value; } }
private Folder mvarParent = null;
public Folder Parent { get { return mvarParent; } internal set { mvarParent = value; } }
private IFileSystemContainer mvarParent = null;
public IFileSystemContainer Parent { get { return mvarParent; } internal set { mvarParent = value; } }
public FileSystemObjectModel FileSystem { get; private set; } = null;
// The amount of working set to allocate to each block.
public const int BLOCK_FRACTION = 4;

View File

@ -66,6 +66,8 @@ namespace UniversalEditor.ObjectModels.FileSystem
}
}
public FileSystemObjectModel FileSystem { get { return this; } }
public static FileSystemObjectModel FromFiles(string[] fileNames)
{
// TODO: This doesn't work because GetAvailableObjectModel returns an
@ -108,8 +110,14 @@ namespace UniversalEditor.ObjectModels.FileSystem
// return FromFiles(files);
}
public File.FileCollection Files { get; } = new File.FileCollection();
public Folder.FolderCollection Folders { get; } = new Folder.FolderCollection();
public FileSystemObjectModel()
{
Files = new File.FileCollection(this);
Folders = new Folder.FolderCollection(this);
}
public File.FileCollection Files { get; private set; } = null;
public Folder.FolderCollection Folders { get; private set; } = null;
/// <summary>
/// The unique ID associated with this file system. Not supported by all <see cref="DataFormat" />s.
@ -128,29 +136,29 @@ namespace UniversalEditor.ObjectModels.FileSystem
return (FindFile(path) != null);
}
public File FindFile(string path)
public File FindFile(string path, MBS.Framework.IO.CaseSensitiveHandling caseSensitiveHandling = MBS.Framework.IO.CaseSensitiveHandling.System)
{
string[] pathParts = path.Split(PathSeparators);
if (pathParts.Length == 1)
{
File file = Files[pathParts[0]];
File file = Files[pathParts[0], caseSensitiveHandling];
if (file != null) return file;
}
else
{
Folder parentFolder = Folders[pathParts[0]];
Folder parentFolder = Folders[pathParts[0], caseSensitiveHandling];
if (parentFolder == null) return null;
for (int i = 1; i < pathParts.Length; i++)
{
if (i < pathParts.Length - 1)
{
parentFolder = parentFolder.Folders[pathParts[i]];
parentFolder = parentFolder.Folders[pathParts[i], caseSensitiveHandling];
if (parentFolder == null) return null;
}
else
{
return parentFolder.Files[pathParts[i]];
return parentFolder.Files[pathParts[i], caseSensitiveHandling];
}
}
}

View File

@ -32,8 +32,8 @@ namespace UniversalEditor.ObjectModels.FileSystem
public class FolderCollection
: System.Collections.ObjectModel.Collection<Folder>
{
private Folder mvarParent = null;
public FolderCollection(Folder parent = null)
private IFileSystemContainer mvarParent = null;
public FolderCollection(IFileSystemContainer parent)
{
mvarParent = parent;
}
@ -46,13 +46,15 @@ namespace UniversalEditor.ObjectModels.FileSystem
return folder;
}
public Folder this[string Name]
public Folder this[string Name, MBS.Framework.IO.CaseSensitiveHandling caseSensitiveHandling = MBS.Framework.IO.CaseSensitiveHandling.System]
{
get
{
bool caseSensitive = (caseSensitiveHandling == MBS.Framework.IO.CaseSensitiveHandling.CaseSensitive || (caseSensitiveHandling == MBS.Framework.IO.CaseSensitiveHandling.System && System.Environment.OSVersion.Platform == PlatformID.Unix));
for (int i = 0; i < Count; i++)
{
if (this[i].Name.Equals(Name)) return this[i];
if ((caseSensitive && this[i].Name.Equals(Name)) || (!caseSensitive && this[i].Name.ToUpper() == Name.ToUpper()))
return this[i];
}
return null;
}
@ -84,6 +86,7 @@ namespace UniversalEditor.ObjectModels.FileSystem
{
base.InsertItem(index, item);
item.Parent = mvarParent;
item.FileSystem = mvarParent?.FileSystem;
}
protected override void RemoveItem(int index)
{
@ -103,13 +106,15 @@ namespace UniversalEditor.ObjectModels.FileSystem
mvarFiles = new File.FileCollection(this);
}
public FileSystemObjectModel FileSystem { get; private set; } = null;
private FolderCollection _parentCollection = null;
private string mvarName = String.Empty;
public string Name { get { return mvarName; } set { mvarName = value; } }
private Folder mvarParent = null;
public Folder Parent { get { return mvarParent; } private set { mvarParent = value; } }
private IFileSystemContainer mvarParent = null;
public IFileSystemContainer Parent { get { return mvarParent; } private set { mvarParent = value; } }
private FolderCollection mvarFolders = null;
public FolderCollection Folders { get { return mvarFolders; } }

View File

@ -31,5 +31,7 @@ namespace UniversalEditor.ObjectModels.FileSystem
string GetNewFolderName();
File AddFile(string name, byte[] fileData = null);
FileSystemObjectModel FileSystem { get; }
}
}

View File

@ -27,6 +27,8 @@ namespace UniversalEditor.ObjectModels.FileSystem
public interface IFileSystemObject
{
string Name { get; set; }
FileSystemObjectModel FileSystem { get; }
}
/// <summary>
/// Represents a <see cref="System.Collections.ObjectModel.Collection{IFileSystemObject}" /> of <see cref="IFileSystemObject" />s.

View File

@ -199,6 +199,7 @@
<Compile Include="DataFormats\JSON\JSONPresetSettings.cs" />
<Compile Include="DataFormats\JSON\JSONSettings.cs" />
<Compile Include="ObjectModels\PropertyList\PropertyListItem.cs" />
<Compile Include="Accessors\EmbeddedFileAccessor.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\UniversalEditor.Core\UniversalEditor.Core.csproj">
@ -224,6 +225,7 @@
<Folder Include="ObjectModels\Database\" />
<Folder Include="DataFormats\Binary\PEM\" />
<Folder Include="DataFormats\JSON\" />
<Folder Include="Accessors\" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.

View File

@ -189,7 +189,7 @@ namespace UniversalEditor.Editors.FileSystem
{
File f = (fso as File);
MemoryAccessor ma = new MemoryAccessor(f.GetData(), f.Name);
EmbeddedFileAccessor ma = new EmbeddedFileAccessor(f);
Document doc = new Document(ma);
HostApplication.CurrentWindow.OpenFile(doc);
}

View File

@ -103,5 +103,10 @@ namespace UniversalEditor.Accessors.MTP
{
throw new NotImplementedException();
}
protected override Accessor GetRelativeInternal(string filename, string prefix = null)
{
return null;
}
}
}

View File

@ -413,27 +413,16 @@ namespace UniversalEditor.DataFormats.NWCSceneLayout.NewWorldComputing.BIN
private PictureObjectModel LoadICN(string icnFileName, int icnIndex)
{
if (icnDataDir == null)
{
icnDataDir = ICNDataDirectory;
if (icnDataDir == null)
{
string fn = Accessor.GetFileName();
if (fn != null)
{
icnDataDir = System.IO.Path.GetDirectoryName(fn);
}
}
}
// TODO: figure out how to access the parent file system of a given Accessor to load files adjacent to the current file in the current folder
// i.e. somefile.bin > somefile.icn regardless of whether the file is in the local file system (This code here) or being loaded directly from the FileSystemEditor
Accessor icnRelated = Accessor.GetRelative(icnFileName, icnDataDir);
PictureObjectModel pic = null;
string icnFullyQualifiedPath = MBS.Framework.IO.File.Find(System.IO.Path.Combine(new string[] { icnDataDir, icnFileName }), MBS.Framework.IO.CaseSensitiveHandling.CaseInsensitive);
if (System.IO.File.Exists(icnFullyQualifiedPath))
if (icnRelated != null)
{
PictureCollectionObjectModel pcom = new PictureCollectionObjectModel();
try
{
Document.Load(pcom, icndf, new FileAccessor(icnFullyQualifiedPath));
Document.Load(pcom, icndf, icnRelated);
pic = pcom.Pictures[icnIndex];
}
catch

View File

@ -168,5 +168,12 @@ namespace UniversalEditor.Accessors
}
return -1;
}
protected override Accessor GetRelativeInternal(string filename, string prefix = null)
{
HTTPAccessor acc = new HTTPAccessor();
acc.FileName = String.Concat(this.GetFileName(), "/../", filename);
return acc;
}
}
}