using System; using System.ComponentModel; using System.IO; using System.Linq; using System.Text; using JD.Core.ProjectManaging; using Packager.Parameters; namespace Packager.Tasks { [DisplayName("Enumerate Map List")] [Category("Map List Generation")] [Description("Extracts the map names specified by the user. \nSpecified maps can be either direct .isc files or text files containing a list of .isc files.")] public class MapListTask: Task { [Input("MapList", IOCardinality.Multi, IOType.File)] public ReferenceList MapList { get; set; } [Output("Maps", IOType.File)] public ReferenceList Maps = new ReferenceList(); [Output("Names", IOType.String)] public ReferenceList Names = new ReferenceList(); [Output("Paths as RegEx", IOType.String)] public SingleReference PathsAsRegex = new SingleReference(); public MapListTask() { MapList = new ReferenceList(); } protected override void StartInternal(BuildConfiguration profile, cookerWrapper cooker) { try { var mapFiles = MapList.Where(file => Path.GetExtension(file) != ".isc").ToList(); var mapList = MapList.Except(mapFiles).Select(FormatMapPath).ToList(); foreach (var file in mapFiles) { mapList.AddRange(ReadMapFile(file)); } if(mapList.Any(string.IsNullOrEmpty)) { ErrorCode = -2; mapList = mapList.Where(file => !string.IsNullOrEmpty(file)).ToList(); } Maps.Add(mapList.ToArray()); Names.Add(mapList.Select(Path.GetFileNameWithoutExtension).ToArray()); var paths = mapList.Select(Path.GetDirectoryName); var filterString = new StringBuilder(); foreach (string s in paths) filterString.Append(s.Replace("\\", "/") + "/*"); PathsAsRegex.Value = filterString.ToString(); } catch (Exception ex) { LogMessage(true, ex.Message); } } private string[] ReadMapFile(string path) { if(!File.Exists(path)) throw new Exception($"Invalid file \"{path}\""); var files = File.ReadAllLines(path); return files.Select(FormatMapPath).ToArray(); } private string FormatMapPath(string path) { try { path = path.TrimEnd(); if (Path.GetExtension(path) != ".isc") { throw new Exception($"Invalid extension for the map \"{path}\""); } if(Path.IsPathRooted(path)) { string dataDirectory = Path.GetFullPath(DataPath.Value); if (!dataDirectory.EndsWith("\\", StringComparison.OrdinalIgnoreCase)) { dataDirectory += '\\'; } path = Path.GetFullPath(path); if (!path.StartsWith(dataDirectory, StringComparison.OrdinalIgnoreCase)) { throw new Exception($"Map \"{path}\" is not in the data folder \"{dataDirectory}\"."); } path = path.Substring(dataDirectory.Length); } if (!File.Exists(Path.Combine(DataPath.Value, path))) { throw new Exception($"Can't find the map \"{path}\""); } return path.Replace('\\', '/'); } catch (Exception ex) { LogMessage(true, ex.Message); return ""; } } } }