using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace SCJMapper_V2.SC
{
///
/// Finds and returns the Mappings from SC Bundle
/// it is located in the Mappings Path
///
class SCMappings
{
private static readonly log4net.ILog log = log4net.LogManager.GetLogger( System.Reflection.MethodBase.GetCurrentMethod( ).DeclaringType );
public const String c_MapStartsWith = "layout_"; // we only allow those mapping names
private const String c_UserMapStartsWith = c_MapStartsWith + "my_"; // we only allow those mapping names
static private List m_scMappings = new List( );
///
/// Returns true if a mapping file exists
///
/// The mapping name
/// True if the file exists
static public Boolean MappingFileExists( String mapName )
{
Boolean retVal = false;
if ( Directory.Exists( SCPath.SCClientMappingPath ) ) {
retVal = File.Exists( Path.Combine( SCPath.SCClientMappingPath, mapName + ".xml" ) );
}
return retVal;
}
///
/// Returns the mapping file name + path into our user MAPPING dir (TODO possibly to change??)
///
/// The mapping name
/// A fully qualified filename
static public String MappingFileName( String mapName )
{
return Path.Combine( SCPath.SCClientMappingPath, mapName + ".xml" );
}
///
/// Returns true if a mapping name is considered a user mapping
///
/// The mapping name
/// True if it is a user mapping name
static public Boolean IsUserMapping( String mapName )
{
return mapName.StartsWith( c_UserMapStartsWith );
}
///
/// Check if we may use that name - we allow only names like "layout_my_XYZ"
///
/// A map name
/// True if valid
static public Boolean IsValidMappingName( String mapName )
{
Boolean retVal = true; // for now
retVal &= mapName.StartsWith( c_UserMapStartsWith );
retVal &= ( mapName.IndexOfAny( new char[] { ' ', '\t', '\n', '\r', '\0' } ) < 0 ); // make sure we don't have spaces etc.
return retVal;
}
static public void UpdateMappingNames( )
{
if ( Directory.Exists( SCPath.SCClientMappingPath ) ) {
m_scMappings.Clear( );
m_scMappings = ( List )Directory.EnumerateFiles( SCPath.SCClientMappingPath ).ToList( );
}
else {
log.Warn( "UpdateMappingNames - cannot find SC Mapping directory" );
}
}
///
/// Returns a list of files found
///
/// A list of filenames - can be empty
static public List MappingNames
{
get
{
log.Debug( "MappingNames - Entry" );
if ( m_scMappings.Count == 0 ) {
UpdateMappingNames( );
}
return m_scMappings;
}
}
///
/// Returns the sought default profile as string from GameData.pak
///
/// The filename of the profile to be extracted
/// A string containing the file contents
static public String Mapping( String mappingName )
{
String retVal = "";
String mFile = Path.Combine( SCPath.SCClientMappingPath, ( mappingName + ".xml" ) );
if ( File.Exists( mFile ) ) {
using ( StreamReader sr = new StreamReader( mFile ) ) {
retVal = sr.ReadToEnd( );
}
}
return retVal;
}
}
}