Example #1
0
        public void CreateModPackFile()
        {
            PackFileService packFileService = new PackFileService(new PackFileDataBase(), null);
            var             packContainer   = packFileService.CreateNewPackFileContainer("MyTestPackFile", PackFileCAType.MOD);


            var packFile = new PackFile("ExampleFile.txt", new FileSystemSource(@"Data\FolderData\SubFolder0\Subfolder_0_file0.txt"));

            packFileService.AddFileToPack(packContainer, @"data\content\files", packFile);

            //var packFileContent = Encoding.UTF8.GetString(packFile.DataSource.ReadData());

            using (MemoryStream ms0 = new MemoryStream())
            {
                using (BinaryWriter writer = new BinaryWriter(ms0))
                    packFileService.Save(packContainer, writer);

                // Load it again
                var orgData = ms0.ToArray();
                using (MemoryStream ms1 = new MemoryStream(orgData))
                {
                    Assert.AreEqual(ms1.Length, orgData.Length);

                    using (BinaryReader reader = new BinaryReader(ms1))
                    {
                        var loadedPackFile = new PackFileContainer("File", reader);

                        Assert.AreEqual(packContainer.Header.Version, loadedPackFile.Header.Version);
                        Assert.AreEqual(packContainer.Header.FileType, loadedPackFile.Header.FileType);
                        Assert.AreEqual(1, loadedPackFile.Header.FileCount);
                        Assert.AreEqual(0, loadedPackFile.Header.ReferenceFileCount);
                    }
                }
            }
        }
Example #2
0
        public void AddFolderContent(PackFileContainer container, string path, string folderDir)
        {
            var originalFilePaths = Directory.GetFiles(folderDir, "*", SearchOption.AllDirectories);
            var filePaths         = originalFilePaths.Select(x => x.Replace(folderDir + "\\", "")).ToList();

            if (!string.IsNullOrWhiteSpace(path))
            {
                path += "\\";
            }

            var filesAdded = new List <PackFile>();

            for (int i = 0; i < filePaths.Count; i++)
            {
                var currentPath = filePaths[i];
                var filename    = Path.GetFileName(currentPath);

                var source = MemorySource.FromFile(originalFilePaths[i]);
                var file   = new PackFile(filename, source);
                filesAdded.Add(file);

                container.FileList[path.ToLower() + currentPath.ToLower()] = file;
            }

            _skeletonAnimationLookUpHelper.UnloadAnimationFromContainer(this, container);
            _skeletonAnimationLookUpHelper.LoadFromPackFileContainer(this, container);

            Database.TriggerPackFileAdded(container, filesAdded);
        }
Example #3
0
        public List <PackFile> FindAllFilesInDirectory(string dir, PackFileContainer packFileContainer = null)
        {
            dir = dir.ToLower();
            List <PackFile> output = new List <PackFile>();

            if (packFileContainer == null)
            {
                foreach (var pf in Database.PackFiles)
                {
                    foreach (var file in pf.FileList)
                    {
                        if (file.Key.IndexOf(dir) == 0)
                        {
                            output.Add(file.Value as PackFile);
                        }
                    }
                }
            }
            else
            {
                foreach (var file in packFileContainer.FileList)
                {
                    if (file.Key.IndexOf(dir) == 0)
                    {
                        output.Add(file.Value as PackFile);
                    }
                }
            }

            return(output);
        }
Example #4
0
        public PackFileContainer Load(BinaryReader binaryReader, string packFileSystemPath)
        {
            var pack = new PackFileContainer(packFileSystemPath, binaryReader);

            Database.AddPackFile(pack);
            _skeletonAnimationLookUpHelper.LoadFromPackFileContainer(this, pack);
            return(pack);
        }
Example #5
0
        // Add
        // ---------------------------
        public PackFileContainer CreateNewPackFileContainer(string name, PackFileCAType type)
        {
            var newPackFile = new PackFileContainer(name)
            {
                Header = new PFHeader("PFH5", type),
            };

            Database.AddPackFile(newPackFile);
            return(newPackFile);
        }
Example #6
0
        public bool LoadAllCaFiles(string gameDataFolder)
        {
            try
            {
                _logger.Here().Information($"Loading all ca packfiles located in {gameDataFolder}");
                var allCaPackFiles = GetPackFilesFromManifest(gameDataFolder);
                var packList       = new List <PackFileContainer>();
                foreach (var packFilePath in allCaPackFiles)
                {
                    var path = gameDataFolder + "\\" + packFilePath;
                    if (File.Exists(path))
                    {
                        using (var fileStram = File.OpenRead(path))
                        {
                            using (var reader = new BinaryReader(fileStram, Encoding.ASCII))
                            {
                                var pack = new PackFileContainer(path, reader);
                                packList.Add(pack);
                                _skeletonAnimationLookUpHelper.LoadFromPackFileContainer(this, pack);
                            }
                        }
                    }
                    else
                    {
                        _logger.Here().Warning($"Ca packfile '{path}' not found, loading skipped");
                    }
                }

                PackFileContainer caPackFileContainer = new PackFileContainer("All CA packs");
                caPackFileContainer.IsCaPackFile = true;
                var packFilesOrderedByGroup = packList
                                              .GroupBy(x => x.Header.LoadOrder)
                                              .OrderBy(x => x.Key);

                foreach (var group in packFilesOrderedByGroup)
                {
                    var packFilesOrderedByName = group.OrderBy(x => x.Name);
                    foreach (var packfile in packFilesOrderedByName)
                    {
                        caPackFileContainer.MergePackFileContainer(packfile);
                    }
                }

                Database.AddPackFile(caPackFileContainer);
            }
            catch (Exception e)
            {
                _logger.Here().Error($"Trying to all ca packs in {gameDataFolder}. Error : {e.ToString()}");
                return(false);
            }

            return(true);
        }
        public void UnloadAnimationFromContainer(PackFileService pfs, PackFileContainer packFileContainer)
        {
            int itemsRemoved = 0;
            var s            = _skeletonNameToAnimationMap.Select(skeleton => (skeleton.Key, skeleton.Value.Where(animations => animations.Container == packFileContainer))).ToList();

            foreach (var key in s)
            {
                var copy = key.Item2.Select(x => x).ToList();
                foreach (var toRemove in copy)
                {
                    _skeletonNameToAnimationMap[key.Key].Remove(toRemove);
                    itemsRemoved++;
                }
            }
        }
Example #8
0
        public IPackFile FindFile(string path, PackFileContainer container)
        {
            var lowerPath = path.Replace('/', '\\').ToLower();

            _logger.Here().Information($"Searching for file {lowerPath}");

            if (container.FileList.ContainsKey(lowerPath))
            {
                _logger.Here().Information($"File found");
                return(container.FileList[lowerPath]);
            }

            _logger.Here().Warning($"File not found");
            return(null);
        }
Example #9
0
        public void DeleteFile(PackFileContainer pf, IPackFile file)
        {
            if (pf.IsCaPackFile)
            {
                throw new Exception("Can not add files to ca pack file");
            }

            var key = pf.FileList.FirstOrDefault(x => x.Value == file).Key;

            _logger.Here().Information($"Deleting file {key}");

            Database.TriggerPackFileRemoved(pf, new List <PackFile>()
            {
                file as PackFile
            });
            pf.FileList.Remove(key);
        }
Example #10
0
        public void DeleteFolder(PackFileContainer pf, string folder)
        {
            if (pf.IsCaPackFile)
            {
                throw new Exception("Can not add files to ca pack file");
            }

            var folderLower   = folder.ToLower();
            var itemsToDelete = pf.FileList.Where(x => x.Key.StartsWith(folderLower));

            Database.TriggerPackFileFolderRemoved(pf, folder);

            foreach (var item in itemsToDelete)
            {
                _logger.Here().Information($"Deleting file {item.Key} in directory {folder}");
                pf.FileList.Remove(item.Key);
            }
        }
Example #11
0
        public void CopyFileFromOtherPackFile(PackFileContainer source, string path, PackFileContainer target)
        {
            var lowerPath = path.ToLower();

            if (source.FileList.ContainsKey(lowerPath))
            {
                var file    = source.FileList[lowerPath] as PackFile;
                var newFile = new PackFile(file.Name, file.DataSource);
                target.FileList[lowerPath] = newFile;


                Database.TriggerPackFileAdded(target, new List <PackFile>()
                {
                    newFile as PackFile
                });
            }

            //Database.TriggerContainerUpdated(target);
        }
Example #12
0
        public void Save(PackFileContainer pf, string path, bool createBackup)
        {
            if (pf.IsCaPackFile)
            {
                throw new Exception("Can not save ca pack file");
            }
            if (createBackup)
            {
                SaveHelper.CreateFileBackup(path);
            }

            pf.SystemFilePath = path;
            using (MemoryStream memoryStream = new MemoryStream())
            {
                using (BinaryWriter writer = new BinaryWriter(memoryStream))
                    Save(pf, writer);

                File.WriteAllBytes(path, memoryStream.ToArray());
                pf.UpdateAllDataSourcesAfterSave();
            }
        }
Example #13
0
        // Modify
        // ---------------------------
        public void RenameFile(PackFileContainer pf, IPackFile file, string newName)
        {
            if (pf.IsCaPackFile)
            {
                throw new Exception("Can not add files to ca pack file");
            }

            var key = pf.FileList.FirstOrDefault(x => x.Value == file).Key;

            pf.FileList.Remove(key);

            var dir = Path.GetDirectoryName(key);

            file.Name = newName;
            pf.FileList[dir + "\\" + file.Name] = file;

            Database.TriggerPackFilesUpdated(pf, new List <PackFile>()
            {
                file as PackFile
            });
        }
        public void LoadFromPackFileContainer(PackFileService pfs, PackFileContainer packFileContainer)
        {
            var allAnimations = pfs.FindAllWithExtentionIncludePaths(".anim", packFileContainer);

            foreach (var animation in allAnimations)
            {
                try
                {
                    var animationSkeletonName = AnimationFile.GetAnimationHeader(animation.Item2).SkeletonName;
                    if (_skeletonNameToAnimationMap.ContainsKey(animationSkeletonName) == false)
                    {
                        _skeletonNameToAnimationMap.Add(animationSkeletonName, new ObservableCollection <AnimationReference>());
                    }

                    _skeletonNameToAnimationMap[animationSkeletonName].Add(new AnimationReference(pfs.GetFullPath(animation.Item2, packFileContainer), packFileContainer));
                }
                catch (Exception e)
                {
                    _logger.Here().Error("Parsing failed for " + pfs.GetFullPath(animation.Item2, packFileContainer) + "\n" + e.ToString());
                }
            }

            var allNormalSkeletons = allAnimations.Where(x => x.Item1.Contains("animations\\skeletons", StringComparison.InvariantCultureIgnoreCase));

            foreach (var item in allNormalSkeletons)
            {
                SkeletonFileNames.Add(item.Item1);
            }

            var techSkeletons = allAnimations.Where(x => x.Item1.Contains("tech", StringComparison.InvariantCultureIgnoreCase));

            foreach (var item in techSkeletons)
            {
                SkeletonFileNames.Add(item.Item1);
            }

            _logger.Here().Information("Animations found =" + allAnimations.Count());
            _logger.Here().Information("Skeletons found =" + SkeletonFileNames.Count());
        }
Example #15
0
        public void AddFileToPack(PackFileContainer container, string directoryPath, IPackFile newFile)
        {
            if (container.IsCaPackFile)
            {
                throw new Exception("Can not add files to ca pack file");
            }

            if (!string.IsNullOrWhiteSpace(directoryPath))
            {
                directoryPath += "\\";
            }
            directoryPath += newFile.Name;
            container.FileList[directoryPath.ToLower()] = newFile;

            _skeletonAnimationLookUpHelper.UnloadAnimationFromContainer(this, container);
            _skeletonAnimationLookUpHelper.LoadFromPackFileContainer(this, container);

            Database.TriggerPackFileAdded(container, new List <PackFile>()
            {
                newFile as PackFile
            });
        }
Example #16
0
 public string GetFullPath(PackFile file, PackFileContainer container = null)
 {
     if (container == null)
     {
         foreach (var pf in Database.PackFiles)
         {
             var res = pf.FileList.FirstOrDefault(x => x.Value == file).Key;
             if (string.IsNullOrWhiteSpace(res) == false)
             {
                 return(res);
             }
         }
     }
     else
     {
         var res = container.FileList.FirstOrDefault(x => x.Value == file).Key;
         if (string.IsNullOrWhiteSpace(res) == false)
         {
             return(res);
         }
     }
     throw new Exception("Unknown path for " + file.Name);
 }
 public AnimationReference(string animationFile, PackFileContainer container)
 {
     AnimationFile = animationFile;
     Container     = container;
 }
Example #18
0
        public List <ValueTuple <string, PackFile> > FindAllWithExtentionIncludePaths(string extention, PackFileContainer packFileContainer = null)
        {
            extention = extention.ToLower();
            var output = new List <ValueTuple <string, PackFile> >();

            if (packFileContainer == null)
            {
                foreach (var pf in Database.PackFiles)
                {
                    foreach (var file in pf.FileList)
                    {
                        var fileExtention = Path.GetExtension(file.Key);
                        if (fileExtention == extention)
                        {
                            output.Add(new ValueTuple <string, PackFile>(file.Key, file.Value as PackFile));
                        }
                    }
                }
            }
            else
            {
                foreach (var file in packFileContainer.FileList)
                {
                    var fileExtention = Path.GetExtension(file.Key);
                    if (fileExtention == extention)
                    {
                        output.Add(new ValueTuple <string, PackFile>(file.Key, file.Value as PackFile));
                    }
                }
            }

            return(output);
        }
Example #19
0
 public List <PackFile> FindAllWithExtention(string extention, PackFileContainer packFileContainer = null)
 {
     return(FindAllWithExtentionIncludePaths(extention, packFileContainer).Select(x => x.Item2).ToList());
 }
Example #20
0
 public void Save(PackFileContainer pf, BinaryWriter writer)
 {
     pf.SaveToByteArray(writer);
     _skeletonAnimationLookUpHelper.UnloadAnimationFromContainer(this, pf);
     _skeletonAnimationLookUpHelper.LoadFromPackFileContainer(this, pf);
 }
Example #21
0
 // Remove
 // ---------------------------
 public void UnloadPackContainer(PackFileContainer pf)
 {
     _skeletonAnimationLookUpHelper.UnloadAnimationFromContainer(this, pf);
     Database.RemovePackFile(pf);
 }
Example #22
0
 public void SetEditablePack(PackFileContainer pf)
 {
     Database.PackSelectedForEdit = pf;
     Database.TriggerContainerUpdated(pf);
 }