view API/Loading/LoadableObjectSourceDirectory.cs @ 482:1ed2f3ab5e35

Re #419: Remove assumptions of a file-based install * Swap API to using new "loadable object" and "loadable object source" wrappers to allow file-based or memory-based loading
author IBBoard <dev@ibboard.co.uk>
date Sat, 07 Jul 2012 21:01:32 +0100
parents
children
line wrap: on
line source

// This file (LoadableObjectSourceDirectory.cs) is a part of the IBBoard.WarFoundry.API project and is copyright 2012 IBBoard
//
// The file and the library/program it is in are licensed and distributed, without warranty, under the GNU Affero GPL license, either version 3 of the License or (at your option) any later version. Please see COPYING for more information and the full license.
using System;
using System.IO;
using System.Collections.Generic;

namespace IBBoard.WarFoundry.API.Loading
{
	public class LoadableObjectSourceDirectory : ILoadableObjectSource
	{
		private DirectoryInfo dir;

		public LoadableObjectSourceDirectory(string directory) : this(new DirectoryInfo(directory))
		{
			//Do nothing extra
		}

		public LoadableObjectSourceDirectory(DirectoryInfo directory)
		{
			dir = directory;
		}

		public ICollection<ILoadableObject> GetLoadableObjects()
		{
			ICollection<ILoadableObject> loadables;
			dir.Refresh();

			if (dir.Exists)
			{
				loadables = GetLoadableObjectsFromDirectory(dir);
			}
			else
			{
				loadables = new List<ILoadableObject>();
			}

			return loadables;
		}

		private ICollection<ILoadableObject> GetLoadableObjectsFromDirectory(DirectoryInfo directory)
		{
			List<ILoadableObject> loadables = new List<ILoadableObject>();

			foreach (FileInfo file in directory.GetFiles())
			{
				loadables.Add(new LoadableFileObject(file));
			}

			foreach (DirectoryInfo subdir in directory.GetDirectories())
			{
				loadables.AddRange(GetLoadableObjectsFromDirectory(subdir));
			}

			return loadables;
		}

		public override bool Equals(object obj)
		{			
			if (obj == null || !obj.GetType().Equals(GetType()))
			{
				return false;
			}

			LoadableObjectSourceDirectory other = (LoadableObjectSourceDirectory)obj;
			return this.dir.FullName.Equals(other.dir.FullName);
		}

		public override int GetHashCode()
		{
			return dir.FullName.GetHashCode() + 17;
		}
	}
}