Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add functionality to pack and retrieve arbitrary files specified in .dna #688

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions Distribution/XmlSchemas/ExcelDna.DnaLibrary.xsd
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,22 @@
</xs:simpleContent>
</xs:complexType>

<xs:complexType name="FileType">
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute name="Name"
use="required"
type="xs:string" />
<xs:attribute name="Path"
use="required"
type="xs:string" />
<xs:attribute name="Pack"
use="optional"
type="Boolean" />
</xs:extension>
</xs:simpleContent>
</xs:complexType>

<xs:complexType name="CustomUIType">
<xs:sequence>
<xs:any maxOccurs="unbounded"
Expand Down Expand Up @@ -240,6 +256,11 @@
minOccurs="0"
type="ImageType">
</xs:element>
<xs:element name="File"
maxOccurs="unbounded"
minOccurs="0"
type="FileType">
</xs:element>
<xs:element name="CustomUI"
maxOccurs="unbounded"
minOccurs="0"
Expand Down
42 changes: 42 additions & 0 deletions Source/ExcelDna.Integration/DnaLibrary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,14 @@ public List<Image> Images
set { _Images = value; }
}

private List<DnaFile> _Files;
[XmlElement("File", typeof(DnaFile))]
public List<DnaFile> Files
{
get { return _Files; }
set { _Files = value; }
}

private string dnaResolveRoot;

// Get projects explicit and implicitly present in the library
Expand Down Expand Up @@ -726,6 +734,40 @@ public Bitmap GetImage(string imageId)
return null;
}

public byte[] GetFileBytes(string fileId)
{
// TODO: Consider if this should be a Stream instead of a byte[]
// This would allow for larger files to be handled more efficiently.

// First check if fileId is in the DnaLibrary's File list.
// DOCUMENT: Case sensitive match.
foreach (DnaFile file in Files)
{
if (file.Name == fileId && file.Path != null)
{
byte[] fileBytes;
if (file.Path.StartsWith("packed:"))
{
string resourceName = file.Path.Substring(7);
fileBytes = ExcelIntegration.GetFileBytes(resourceName);
}
else
{
string filePath = ResolvePath(file.Path);
if (filePath == null)
{
// This is the file but we could not find it !?
Logger.Initialization.Warn("DnaLibrary.GetFile - For file {0} the path resolution failed: {1}", file.Name, file.Path);
return null;
}
fileBytes = File.ReadAllBytes(filePath);
}
return fileBytes;
}
}
return null;
}

}

//public class CustomUI : IXmlSerializable
Expand Down
19 changes: 19 additions & 0 deletions Source/ExcelDna.Integration/Excel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
using System.IO;
using System.Reflection;
using System.Runtime.ExceptionServices;
using System.Collections.Generic;
using System.Linq;

namespace ExcelDna.Integration
{
Expand Down Expand Up @@ -782,6 +784,23 @@ public static bool SupportsDynamicArrays
}
}
#endregion

#region Packed Files
/// <summary>Returns the names of all the resources packed in the XLL.</summary>
/// <returns>An enumerable that contains the names of all the resources.</returns>
public static IEnumerable<string> GetPackedFileNames()
{
return DnaLibrary.CurrentLibrary.Files.Select(f => f.Name);
}

/// <summary>Loads the specified resource from the XLL.</summary>
/// <param name="name">The case-sensitive name of the resource being requested.</param>
/// <returns>The resource; or <see langword="null" /> if the resource does not exist in the XLL.</returns>
public static byte[] GetPackedFileBytes(string name)
{
return DnaLibrary.CurrentLibrary.GetFileBytes(name);
}
#endregion
}

public class ExcelLimits
Expand Down
5 changes: 5 additions & 0 deletions Source/ExcelDna.Integration/ExcelIntegration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,11 @@ internal static byte[] GetImageBytes(string imageName)
return _integrationHost.GetResourceBytes(imageName, 2);
}

internal static byte[] GetFileBytes(string fileName)
{
return _integrationHost.GetResourceBytes(fileName, 2);
}

internal static byte[] GetSourceBytes(string sourceName)
{
return _integrationHost.GetResourceBytes(sourceName, 3);
Expand Down
2 changes: 1 addition & 1 deletion Source/ExcelDna.Integration/IIntegrationHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace ExcelDna.Integration
interface IIntegrationHost
{
XlCall.XlReturn TryExcelImpl(int xlFunction, out object result, params object[] parameters);
byte[] GetResourceBytes(string resourceName, int type); // types: 0 - Assembly, 1 - Dna file, 2 - Image
byte[] GetResourceBytes(string resourceName, int type); // types: 0 - Assembly, 1 - Dna file, 2 - Image or File, 3 - Source, 4 - PDB
Assembly LoadFromAssemblyPath(string assemblyPath);
Assembly LoadFromAssemblyBytes(byte[] assemblyBytes, byte[] pdbBytes);
void RegisterMethods(List<MethodInfo> methods);
Expand Down
20 changes: 19 additions & 1 deletion Source/ExcelDna.Integration/Reference.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public Reference(string path)
{
Path = path;
}
}
}

[Serializable]
[XmlType(AnonymousType = true)]
Expand All @@ -73,4 +73,22 @@ public Image()
}
}

[Serializable]
[XmlType(AnonymousType = true)]
public class DnaFile
{
[XmlAttribute]
public string Name;

[XmlAttribute]
public string Path;

[XmlAttribute]
public bool Pack;

public DnaFile()
{
}
}

}
25 changes: 25 additions & 0 deletions Source/ExcelDna.PackedResources/ExcelDnaPack.cs
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,31 @@ static private byte[] PackDnaLibrary(string dnaPath, byte[] dnaContent, string d
}
}
}
foreach (DnaFile file in dna.Files)
{
if (file.Pack)
{
string path = dna.ResolvePath(file.Path);
if (path == null)
{
var format = " ~~> ERROR: File path {0} NOT RESOLVED.";
errorMessage = string.Format(format, file.Path);
buildLogger.Error(typeof(ExcelDnaPack), format, file.Path);
throw new InvalidOperationException(errorMessage);
}
if (filesToPublish == null)
{
string name = Path.GetFileNameWithoutExtension(path).ToUpperInvariant() + "_" + lastPackIndex++ + Path.GetExtension(path).ToUpperInvariant();
byte[] fileBytes = File.ReadAllBytes(path);
ru.AddFile(fileBytes, name, ResourceHelper.TypeName.FILE, null, compress, multithreading);
file.Path = "packed:" + name;
}
else
{
filesToPublish.Add(path);
}
}
}
foreach (Project project in dna.Projects)
{
foreach (SourceItem source in project.SourceItems)
Expand Down
1 change: 1 addition & 0 deletions Source/ExcelDna.PackedResources/ResourceHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ internal enum TypeName
SOURCE = 3,
PDB = 4,
NATIVE_LIBRARY = 5,
FILE = 6,
}

// TODO: Learn about locales
Expand Down