示例#1
0
    public MagitekResult AddFolder(ResourceFolderModel folderModel, string parentNodePath, string diskLocation)
    {
        var folder = new ResourceFolder(folderModel.Name);

        var folderNode = new ResourceFolderNode(folder.Name, folder)
        {
            DiskLocation = diskLocation
        };

        Tree.TryGetNode(parentNodePath, out var parentNode);
        parentNode.AttachChildNode(folderNode);

        return(MagitekResult.SuccessResult);
    }
示例#2
0
 public static ResourceFolder MapToResource(this ResourceFolderModel model) =>
 new ResourceFolder(model.Name);
示例#3
0
    public MagitekResults <ProjectTree> ReadProject(string projectFileName)
    {
        if (string.IsNullOrWhiteSpace(projectFileName))
        {
            throw new ArgumentException($"{nameof(ReadProject)} cannot have a null or empty value for '{nameof(projectFileName)}'");
        }

        _errors = new();

        if (!TryDeserializeXmlFile(projectFileName, _resourceSchema, out var rootModel))
        {
            return(new MagitekResults <ProjectTree> .Failed(_errors));
        }

        if (rootModel is not ImageProjectModel projectModel)
        {
            _errors.Add($"'{projectFileName}' was expected to be a project file");
            return(new MagitekResults <ProjectTree> .Failed(_errors));
        }

        var builder = new ProjectTreeBuilder(_codecFactory, _colorFactory, _globalResources);

        _baseDirectory = Path.GetDirectoryName(projectFileName);
        if (!string.IsNullOrWhiteSpace(projectModel.Root))
        {
            if (Path.IsPathFullyQualified(projectModel.Root))
            {
                _baseDirectory = projectModel.Root;
            }
            else
            {
                _baseDirectory = Path.Combine(_baseDirectory, projectModel.Root);
            }
        }

        builder.AddProject(projectModel, _baseDirectory, projectFileName);

        // Add directories
        var directoryNames = Directory.GetDirectories(_baseDirectory, "*", SearchOption.AllDirectories);

        foreach (var directoryName in directoryNames)
        {
            var folderModel = new ResourceFolderModel();
            folderModel.Name = Path.GetFileName(directoryName);

            var parentDirectory = Directory.GetParent(directoryName).FullName;
            var relativePath    = Path.GetRelativePath(_baseDirectory, parentDirectory);
            var parentKey       = relativePath == "." ? "" : relativePath;
            builder.AddFolder(folderModel, parentKey, Path.GetFullPath(directoryName));
        }

        // Add resources
        string fullProjectFileName = new FileInfo(projectFileName).FullName;
        var    resourceFileNames   = Directory.GetFiles(_baseDirectory, "*.xml", SearchOption.AllDirectories)
                                     .Except(new[] { fullProjectFileName })
                                     .ToList();

        var resourceModels = new List <(ResourceModel model, string pathKey, string fileLocation)>();

        foreach (var resourceFileName in resourceFileNames)
        {
            if (!TryDeserializeXmlFile(resourceFileName, _resourceSchema, out var resourceModel))
            {
                return(new MagitekResults <ProjectTree> .Failed(_errors));
            }

            var pathKey = LocateParentPathKey(resourceFileName);
            if (string.IsNullOrWhiteSpace(pathKey) || pathKey == ".")
            {
                pathKey = string.Empty;
            }
            resourceModels.Add((resourceModel, pathKey, resourceFileName));
        }

        foreach (var(model, pathKey, fileLocation) in resourceModels.Where(x => x.model is DataFileModel))
        {
            var result = builder.AddDataFile(model as DataFileModel, pathKey, fileLocation);
            if (result.HasFailed)
            {
                _errors.Add($"{result.AsT1.Reason}");
            }
        }

        foreach (var(model, pathKey, fileLocation) in resourceModels.Where(x => x.model is PaletteModel))
        {
            var result = builder.AddPalette(model as PaletteModel, pathKey, fileLocation);
            if (result.HasFailed)
            {
                _errors.Add($"{result.AsT1.Reason}");
            }
        }

        foreach (var(model, pathKey, fileLocation) in resourceModels.Where(x => x.model is ScatteredArrangerModel))
        {
            var result = builder.AddScatteredArranger(model as ScatteredArrangerModel, pathKey, fileLocation);
            if (result.HasFailed)
            {
                _errors.Add($"{result.AsT1.Reason}");
            }
        }

        if (_errors.Count > 0)
        {
            return(new MagitekResults <ProjectTree> .Failed(_errors));
        }

        return(new MagitekResults <ProjectTree> .Success(builder.Tree));
    }