Skip to content
This repository has been archived by the owner on Aug 22, 2018. It is now read-only.

Commit

Permalink
Merge pull request #33 in XK/xenko from giovanni/UFileExtension to ma…
Browse files Browse the repository at this point in the history
…ster-1.9

* commit 'd17f8cd44cae64180a64c7193a57392522893b45':
  [Assets.Editor] Unified IsValidName when possible for both DirectoryViewModel and PackageViewModel
  [Design.Tests] Added null test for GetFileNameWithoutExtension
  [Assets] Renamed AssetPath into AssetLocation and used correct method to find asset url in PackageAssetCollection
  [Core.Design] Made UFile behave like default .Net Path in terms of extension exposure, also fixed issues related to dot in asset
  • Loading branch information
Benjamin Litzelmann committed Jan 31, 2017
2 parents 23d93b5 + c488d1e commit ffa2868
Show file tree
Hide file tree
Showing 22 changed files with 93 additions and 72 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ private BuildResultCode BuildGetGraphicsPlatform()
AutoLoadTemporaryAssets = true,
LoadAssemblyReferences = false,
AutoCompileProjects = false,
TemporaryAssetFilter = (asset) => asset.AssetPath == GameSettingsAsset.GameSettingsLocation,
TemporaryAssetFilter = (asset) => asset.AssetLocation == GameSettingsAsset.GameSettingsLocation,
TemporaryAssetsInMsbuild = false,
});

Expand Down
4 changes: 2 additions & 2 deletions sources/assets/SiliconStudio.Assets/Analysis/AssetAnalysis.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public static void Run(AssetItem assetItem, ILogger log, AssetAnalysisParameters
internal static void UpdateAssetReferences(AssetItem assetItem, IEnumerable<AssetReferenceLink> assetReferences, ILogger log, AssetAnalysisParameters parameters)
{
var package = assetItem.Package;
var packageName = package.FullPath?.GetFileName() ?? "(Undefined path)";
var packageName = package.FullPath?.GetFileNameWithoutExtension() ?? "(Undefined path)";
bool shouldSetDirtyFlag = false;

// Update reference
Expand Down Expand Up @@ -137,7 +137,7 @@ internal static void UpdateAssetReferences(AssetItem assetItem, IEnumerable<Asse
var packageFound = package.Session.Packages.FirstOrDefault(x => x.FindAsset(contentReference.Location) != null);
if (packageFound != null)
{
log.Warning(package, contentReference, AssetMessageCode.AssetFoundInDifferentPackage, contentReference, packageFound.FullPath.GetFileName());
log.Warning(package, contentReference, AssetMessageCode.AssetFoundInDifferentPackage, contentReference, packageFound.FullPath.GetFileNameWithoutExtension());
}
}
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public List<Asset> ValidateAssetBase(AssetItem assetItem, ILogger log)
if (!ReferenceEquals(assetItem.Asset, assetItemFound.Asset))
{
var assetReference = assetItem.ToReference();
log.Error(assetItem.Package, assetReference, AssetMessageCode.AssetForPackageNotFound, assetReference, assetItem.Package.FullPath.GetFileName());
log.Error(assetItem.Package, assetReference, AssetMessageCode.AssetForPackageNotFound, assetReference, assetItem.Package.FullPath.GetFileNameWithoutExtension());
return baseItems;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public AssetFileChangedEvent(Package package, AssetFileChangedType changeType, U
{
Package = package;
ChangeType = changeType;
AssetLocation = assetLocation.GetDirectoryAndFileName(); // Make sure we are using the location withint the package without the extension
AssetLocation = assetLocation.GetDirectoryAndFileNameWithoutExtension(); // Make sure we are using the location withint the package without the extension
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public void Run(ILogger log)
// If the package doesn't have a meta name, fix it here
if (string.IsNullOrWhiteSpace(package.Meta.Name) && package.FullPath != null)
{
package.Meta.Name = package.FullPath.GetFileName();
package.Meta.Name = package.FullPath.GetFileNameWithoutExtension();
package.IsDirty = true;
}

Expand Down Expand Up @@ -208,7 +208,7 @@ private void ProcessRootAssetReferences(RootAssetCollection rootAssets, Package
// If asset was not found, remove the reference
if (newItemReference == null)
{
log.Warning(package, rootAsset, AssetMessageCode.AssetForPackageNotFound, rootAsset, package.FullPath.GetFileName());
log.Warning(package, rootAsset, AssetMessageCode.AssetForPackageNotFound, rootAsset, package.FullPath.GetFileNameWithoutExtension());
rootAssets.Remove(rootAsset.Id);
package.IsDirty = true;
continue;
Expand Down
10 changes: 5 additions & 5 deletions sources/assets/SiliconStudio.Assets/Package.cs
Original file line number Diff line number Diff line change
Expand Up @@ -596,7 +596,7 @@ public void Save(ILogger log, PackageSaveParameters saveParameters = null)
new List<KeyValuePair<string, string>>
{
new KeyValuePair<string, string>("Generator", generatorAsset.Generator),
new KeyValuePair<string, string>("LastGenOutput", new UFile(generatedInclude).GetFileNameWithExtension())
new KeyValuePair<string, string>("LastGenOutput", new UFile(generatedInclude).GetFileName())
});

project.AddItem("Compile", generatedInclude,
Expand All @@ -605,7 +605,7 @@ public void Save(ILogger log, PackageSaveParameters saveParameters = null)
new KeyValuePair<string, string>("AutoGen", "True"),
new KeyValuePair<string, string>("DesignTime", "True"),
new KeyValuePair<string, string>("DesignTimeSharedInput", "True"),
new KeyValuePair<string, string>("DependentUpon", new UFile(projectInclude).GetFileNameWithExtension())
new KeyValuePair<string, string>("DependentUpon", new UFile(projectInclude).GetFileName())
});
}
else
Expand Down Expand Up @@ -959,7 +959,7 @@ public void LoadTemporaryAssets(ILogger log, List<PackageLoadingAssetFile> asset
assetFiles.Sort(PackageLoadingAssetFile.FileSizeComparer.Default);
}

var progressMessage = $"Loading Assets from Package [{FullPath.GetFileNameWithExtension()}]";
var progressMessage = $"Loading Assets from Package [{FullPath.GetFileName()}]";

// Display this message at least once if the logger does not log progress (And it shouldn't in this case)
var loggerResult = log as LoggerResult;
Expand Down Expand Up @@ -1034,7 +1034,7 @@ private void LoadAsset(AssetMigrationContext context, PackageLoadingAssetFile as
AssetMigration.MigrateAssetIfNeeded(context, assetFile, PackageStore.Instance.DefaultPackageName);

// Try to load only if asset is not already in the package or assetRef.Asset is null
var assetPath = assetFile.AssetPath;
var assetPath = assetFile.AssetLocation;

var assetFullPath = fileUPath.ToWindowsPath();
var assetContent = assetFile.AssetContent;
Expand Down Expand Up @@ -1205,7 +1205,7 @@ internal void UpdateSourceFolders(IReadOnlyCollection<AssetItem> assets)
if (asset.SourceFolder == null)
{
//var assetProjectFolder = asset.Location.FullPath;
var lib = sharedProfile.ProjectReferences.FirstOrDefault(x => x.Type == ProjectType.Library && asset.Location.FullPath.StartsWith(x.Location.GetFileName()));
var lib = sharedProfile.ProjectReferences.FirstOrDefault(x => x.Type == ProjectType.Library && asset.Location.FullPath.StartsWith(x.Location.GetFileNameWithoutExtension()));
if (lib != null)
{
asset.SourceProject = UPath.Combine(asset.Package.RootDirectory, lib.Location);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class PackageLoadingAssetFile

public bool Deleted;

public UFile AssetPath => FilePath.MakeRelative(SourceFolder).GetDirectoryAndFileName();
public UFile AssetLocation => FilePath.MakeRelative(SourceFolder).GetDirectoryAndFileNameWithoutExtension();

/// <summary>
/// Initializes a new instance of the <see cref="PackageLoadingAssetFile"/> class.
Expand Down Expand Up @@ -66,7 +66,7 @@ public PackageLoadingAssetFile(Package package, UFile filePath, UDirectory sourc

public IReference ToReference()
{
return new AssetReference(AssetId.Empty, AssetPath);
return new AssetReference(AssetId.Empty, AssetLocation);
}

public YamlAsset AsYamlAsset()
Expand Down
4 changes: 2 additions & 2 deletions sources/assets/SiliconStudio.Assets/PackageSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -843,7 +843,7 @@ private static Package PreLoadPackage(PackageSession session, ILogger log, strin
// If the package doesn't have a meta name, fix it here (This is supposed to be done in the above disabled analysis - but we still need to do it!)
if (string.IsNullOrWhiteSpace(package.Meta.Name) && package.FullPath != null)
{
package.Meta.Name = package.FullPath.GetFileName();
package.Meta.Name = package.FullPath.GetFileNameWithoutExtension();
package.IsDirty = true;
}

Expand Down Expand Up @@ -1100,7 +1100,7 @@ private static void PreLoadPackageDependencies(PackageSession session, ILogger l
{
// TODO: We need to support automatic download of packages. This is not supported yet when only Xenko
// package is supposed to be installed, but It will be required for full store
log.Error($"The package {package.FullPath?.GetFileName() ?? "[Untitled]"} depends on package {packageDependency} which is not installed");
log.Error($"The package {package.FullPath?.GetFileNameWithoutExtension() ?? "[Untitled]"} depends on package {packageDependency} which is not installed");
packageDependencyErrors = true;
continue;
}
Expand Down
4 changes: 2 additions & 2 deletions sources/assets/SiliconStudio.Assets/PackageSessionHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ public static void SaveSolution(PackageSession session, ILogger log)
projectInSolution = new Project(solution,
project.Id,
KnownProjectTypeGuid.CSharp,
project.Location.GetFileName(),
project.Location.GetFileNameWithoutExtension(),
projectRelativePath.ToWindowsPath(),
package.Id,
Enumerable.Empty<Section>(),
Expand Down Expand Up @@ -331,7 +331,7 @@ public static void SaveProperties(Package package)
{
// Props file is in the same folder as the xkpkg file, just with a ".props" extension.
var packagePath = package.FullPath;
var propsFilePath = UPath.Combine(packagePath.GetParent(), (UFile)(packagePath.GetFileName() + ".props")) ;
var propsFilePath = UPath.Combine(packagePath.GetParent(), (UFile)(packagePath.GetFileNameWithoutExtension() + ".props")) ;

var projectCollection = new Microsoft.Build.Evaluation.ProjectCollection();
var project = new Microsoft.Build.Evaluation.Project(projectCollection);
Expand Down
2 changes: 1 addition & 1 deletion sources/assets/SiliconStudio.Assets/RawAssetImporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public override IEnumerable<AssetItem> Import(UFile rawAssetPath, AssetImporterP
var asset = new RawAsset { Source = rawAssetPath };

// Creates the url to the raw asset
var rawAssetUrl = new UFile(rawAssetPath.GetFileName());
var rawAssetUrl = new UFile(rawAssetPath.GetFileNameWithoutExtension());

yield return new AssetItem(rawAssetUrl, asset);
}
Expand Down
25 changes: 13 additions & 12 deletions sources/common/core/SiliconStudio.Core.Design.Tests/TestUPathOld.cs
Original file line number Diff line number Diff line change
Expand Up @@ -344,10 +344,11 @@ public void TestNormalize()
[Test]
public void TestFileExtension()
{
Assert.AreEqual("test", new UFile("test.txt").GetFileName());
Assert.AreEqual("test", new UFile("test.txt").GetFileNameWithoutExtension());
Assert.AreEqual(".txt", new UFile("test.txt").GetFileExtension());
Assert.AreEqual(null, new UFile(".txt").GetFileNameWithoutExtension());

Assert.AreEqual("test.another", new UFile("test.another.txt").GetFileName());
Assert.AreEqual("test.another", new UFile("test.another.txt").GetFileNameWithoutExtension());
Assert.AreEqual(".txt", new UFile("test.another.txt").GetFileExtension());

Assert.AreEqual(".txt", new UFile(".txt").GetFileExtension());
Expand All @@ -362,7 +363,7 @@ public void TestIsDirectoryOnly()

var filePath = new UFile("/test.txt");
Assert.AreEqual("/", filePath.GetDirectory());
Assert.AreEqual("test.txt", filePath.GetFileNameWithExtension());
Assert.AreEqual("test.txt", filePath.GetFileName());
}

[Test]
Expand All @@ -382,9 +383,9 @@ public void TestWithSimplePath()
{
var assetPath = new UFile("/a/b/c");
Assert.AreEqual("/a/b", assetPath.GetDirectory());
Assert.AreEqual("c", assetPath.GetFileName());
Assert.AreEqual("c", assetPath.GetFileNameWithoutExtension());
Assert.AreEqual(null, assetPath.GetFileExtension());
Assert.AreEqual("/a/b/c", assetPath.GetDirectoryAndFileName());
Assert.AreEqual("/a/b/c", assetPath.GetDirectoryAndFileNameWithoutExtension());
Assert.AreEqual("/a/b/c", assetPath.FullPath);
}

Expand All @@ -393,9 +394,9 @@ public void TestWithSimplePathWithExtension()
{
var assetPath = new UFile("/a/b/c.txt");
Assert.AreEqual("/a/b", assetPath.GetDirectory());
Assert.AreEqual("c", assetPath.GetFileName());
Assert.AreEqual("c", assetPath.GetFileNameWithoutExtension());
Assert.AreEqual(".txt", assetPath.GetFileExtension());
Assert.AreEqual("/a/b/c", assetPath.GetDirectoryAndFileName());
Assert.AreEqual("/a/b/c", assetPath.GetDirectoryAndFileNameWithoutExtension());
Assert.AreEqual("/a/b/c.txt", assetPath.FullPath);
}

Expand All @@ -404,16 +405,16 @@ public void TestWithNormalization()
{
var assetPath = new UFile("/a/b/.././././//c.txt");
Assert.AreEqual("/a", assetPath.GetDirectory());
Assert.AreEqual("c", assetPath.GetFileName());
Assert.AreEqual("c", assetPath.GetFileNameWithoutExtension());
Assert.AreEqual(".txt", assetPath.GetFileExtension());
Assert.AreEqual("/a/c", assetPath.GetDirectoryAndFileName());
Assert.AreEqual("/a/c", assetPath.GetDirectoryAndFileNameWithoutExtension());
Assert.AreEqual("/a/c.txt", assetPath.FullPath);

assetPath = new UFile("../.././././//c.txt");
Assert.AreEqual("../..", assetPath.GetDirectory());
Assert.AreEqual("c", assetPath.GetFileName());
Assert.AreEqual("c", assetPath.GetFileNameWithoutExtension());
Assert.AreEqual(".txt", assetPath.GetFileExtension());
Assert.AreEqual("../../c", assetPath.GetDirectoryAndFileName());
Assert.AreEqual("../../c", assetPath.GetDirectoryAndFileNameWithoutExtension());
Assert.AreEqual("../../c.txt", assetPath.FullPath);

assetPath = new UFile("a/../../../c.txt");
Expand All @@ -435,7 +436,7 @@ public void TestEquals()
assetPath1 = new UFile("/a/b/c.txt");
assetPath2 = new UFile("/a/b/d/../c.png");
Assert.AreNotEqual(assetPath1, assetPath2);
Assert.AreEqual(assetPath1.GetDirectoryAndFileName(), assetPath2.GetDirectoryAndFileName());
Assert.AreEqual(assetPath1.GetDirectoryAndFileNameWithoutExtension(), assetPath2.GetDirectoryAndFileNameWithoutExtension());
}

[Test]
Expand Down
58 changes: 39 additions & 19 deletions sources/common/core/SiliconStudio.Core.Design/IO/UFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,51 +22,71 @@ public UFile(string filePath)
}

/// <summary>
/// Gets the file path (<see cref="UPath.GetDirectory()"/> + '/' + <see cref="UFile.GetFileName()"/>) without the extension or drive. Can be an null if no filepath.
/// Gets the name of the file with its extension. Can be null.
/// </summary>
/// <returns>The path.</returns>
public string GetDirectoryAndFileName()
/// <returns>The name.</returns>
public string GetFileName()
{
var span = DirectorySpan;
if (NameSpan.IsValid)
var span = NameSpan;
if (ExtensionSpan.IsValid)
{
span.Length = NameSpan.Next - span.Start;
span.Length = ExtensionSpan.Next - span.Start;
}
return span.IsValid ? FullPath.Substring(span) : null;


}

/// <summary>
/// Gets the name of the file without its extension. Can be null.
/// Gets the name of the file without its extension.
/// </summary>
/// <returns>The name.</returns>
public string GetFileName()
/// <value>The name of file.</value>
public string GetFileNameWithoutExtension()
{
return NameSpan.IsValid ? FullPath.Substring(NameSpan) : null;
}

/// <summary>
/// Gets the extension of the file. Can be null.
/// Gets the file path (<see cref="UPath.GetDirectory()"/> + '/' + <see cref="UFile.GetFileName()"/>) with the extension or drive. Can be an null if no filepath.
/// </summary>
/// <returns>The extension.</returns>
public string GetFileExtension()
/// <returns>The path.</returns>
public string GetDirectoryAndFileName()
{
return ExtensionSpan.IsValid ? FullPath.Substring(ExtensionSpan) : null;
var span = DirectorySpan;
if (ExtensionSpan.IsValid)
{
span.Length = ExtensionSpan.Next - span.Start;
}
else if (NameSpan.IsValid)
{
span.Length = NameSpan.Next - span.Start;
}
return span.IsValid ? FullPath.Substring(span) : null;
}

/// <summary>
/// Gets the name of the file with its extension.
/// Gets the file path (<see cref="UPath.GetDirectory()"/> + '/' + <see cref="UFile.GetFileName()"/>) without the extension or drive. Can be an null if no filepath.
/// </summary>
/// <value>The name of file.</value>
public string GetFileNameWithExtension()
/// <returns>The path.</returns>
public string GetDirectoryAndFileNameWithoutExtension()
{
var span = NameSpan;
if (ExtensionSpan.IsValid)
var span = DirectorySpan;
if (NameSpan.IsValid)
{
span.Length = ExtensionSpan.Next - span.Start;
span.Length = NameSpan.Next - span.Start;
}
return span.IsValid ? FullPath.Substring(span) : null;
}

/// <summary>
/// Gets the extension of the file. Can be null.
/// </summary>
/// <returns>The extension.</returns>
public string GetFileExtension()
{
return ExtensionSpan.IsValid ? FullPath.Substring(ExtensionSpan) : null;
}

/// <summary>
/// Gets the name of the file with its extension.
/// </summary>
Expand Down
4 changes: 2 additions & 2 deletions sources/common/core/SiliconStudio.Core.Design/IO/UPath.cs
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ public IReadOnlyCollection<string> GetComponents()
}

var file = this as UFile;
var fileName = file?.GetFileNameWithExtension();
var fileName = file?.GetFileName();
if (fileName != null)
{
list.Add(fileName);
Expand Down Expand Up @@ -421,7 +421,7 @@ public UPath MakeRelative(UDirectory anchorDirectory)
relativePath.Append(DirectorySeparatorChar);

// Add filename
relativePath.Append(((UFile)absoluteFile).GetFileNameWithExtension());
relativePath.Append(((UFile)absoluteFile).GetFileName());
}
var newPath = relativePath.ToString();
return !IsFile ? (UPath)new UDirectory(newPath) : new UFile(newPath);
Expand Down
Loading

0 comments on commit ffa2868

Please sign in to comment.