// Copyright Epic Games, Inc. All Rights Reserved.
using System;
using System.IO;
using System.Linq;
namespace Gauntlet
{
///
/// The class for reading folder and file structure
///
public static class ContentFolderReader
{
///
/// Reads folder and file structure from a specific directory and represents it using class.
/// Reading is recursive with read depth limit.
///
///
///
///
///
public static ContentFolder ReadFolderStructure(string Dir, int Depth)
{
if (!Directory.Exists(Dir))
{
throw new ArgumentException($"The cooked content path does not exist {Dir}");
}
ContentFolder RootFolder = new ContentFolder(Path.GetFileName(Dir));
PopulateFolderDetails(RootFolder, Dir, Depth);
return RootFolder;
}
private static void PopulateFolderDetails(ContentFolder Folder, string Dir, int Depth)
{
if (Depth < 0)
{
return;
}
try
{
Folder.Files = Directory.GetFiles(Dir).Select(Path.GetFileName).ToList();
Folder.SubFolders = Directory.GetDirectories(Dir).Select(D =>
{
ContentFolder SubFolder = new ContentFolder(Path.GetFileName(D));
PopulateFolderDetails(SubFolder, D, Depth - 1);
return SubFolder;
}).ToList();
}
catch (Exception E)
{
Log.Warning(E.Message);
}
}
}
}