Exemple #1
0
        public void SaveChange(IffFile file)
        {
            lock (this)
            {
                if (file.RuntimeInfo.State == IffRuntimeState.Standalone)
                {
                    //just save out iff
                    var filename = file.RuntimeInfo.Path;
                    Directory.CreateDirectory(Path.GetDirectoryName(filename));
                    using (var stream = new FileStream(filename, FileMode.Create))
                        file.Write(stream);

                    foreach (var chunk in file.ListAll())
                    {
                        chunk.RuntimeInfo = ChunkRuntimeState.Normal;
                    }
                }
                else
                {
                    string dest = (file.RuntimeInfo.State == IffRuntimeState.PIFFClone) ? "Content/Objects/" : "Content/Patch/User/";
                    Directory.CreateDirectory(dest);
                    if (file.RuntimeInfo.State == IffRuntimeState.ReadOnly)
                    {
                        file.RuntimeInfo.State = IffRuntimeState.PIFFPatch;
                    }

                    var stringResources = new HashSet <Type> {
                        typeof(STR), typeof(CTSS), typeof(TTAs)
                    };
                    var sprites = (file.RuntimeInfo.UseCase == IffUseCase.ObjectSprites);
                    file.RuntimeInfo.Patches.Clear();

                    var piff = FSO.Files.Formats.PiffEncoder.GeneratePiff(file, null, stringResources);

                    string name = file.Filename.Substring(0, file.Filename.Length - 4); //get without extension

                    if (piff != null)
                    {
                        var filename = dest + name + (sprites ? ".spf" : "") + ".piff";
                        using (var stream = new FileStream(filename, FileMode.Create)) piff.Write(stream);
                        file.RuntimeInfo.Patches.Add(piff);
                    }

                    if (!sprites)
                    {
                        piff = FSO.Files.Formats.PiffEncoder.GeneratePiff(file, stringResources, null);
                        if (piff != null)
                        {
                            var filename = dest + name + ".str.piff";
                            using (var stream = new FileStream(filename, FileMode.Create)) piff.Write(stream);
                            file.RuntimeInfo.Patches.Add(piff);
                        }
                    }
                }

                file.RuntimeInfo.Dirty = false;
                ChangedFiles.Remove(file);
            }
        }
Exemple #2
0
        public void SaveChange(IffFile file)
        {
            lock (this)
            {
                if (file.RuntimeInfo.State == IffRuntimeState.Standalone)
                {
                    //just save out iff
                    var filename = file.RuntimeInfo.Path;
                    Directory.CreateDirectory(Path.GetDirectoryName(filename));
                    using (var stream = new FileStream(filename, FileMode.Create))
                        file.Write(stream);

                    foreach (var chunk in file.ListAll())
                    {
                        chunk.RuntimeInfo = ChunkRuntimeState.Normal;
                    }
                }
                else
                {
                    string dest = Path.Combine(FSOEnvironment.ContentDir, ((file.RuntimeInfo.State == IffRuntimeState.PIFFClone) ? "Objects/" : "Patch/User/"));
                    Directory.CreateDirectory(dest);
                    if (file.RuntimeInfo.State == IffRuntimeState.ReadOnly)
                    {
                        file.RuntimeInfo.State = IffRuntimeState.PIFFPatch;
                    }

                    var stringResources = new HashSet<Type> { typeof(STR), typeof(CTSS), typeof(TTAs) };
                    var sprites = (file.RuntimeInfo.UseCase == IffUseCase.ObjectSprites);
                    file.RuntimeInfo.Patches.Clear();

                    var piff = FSO.Files.Formats.PiffEncoder.GeneratePiff(file, null, stringResources);

                    string name = file.Filename.Substring(0, file.Filename.Length - 4); //get without extension

                    if (piff != null)
                    {
                        var filename = dest + name + (sprites ? ".spf" : "") + ".piff";
                        using (var stream = new FileStream(filename, FileMode.Create)) piff.Write(stream);
                        file.RuntimeInfo.Patches.Add(piff);
                    }

                    if (!sprites)
                    {
                        piff = FSO.Files.Formats.PiffEncoder.GeneratePiff(file, stringResources, null);
                        if (piff != null)
                        {
                            var filename = dest + name + ".str.piff";
                            using (var stream = new FileStream(filename, FileMode.Create)) piff.Write(stream);
                            file.RuntimeInfo.Patches.Add(piff);
                        }
                    }
                }

                file.RuntimeInfo.Dirty = false;
                ChangedFiles.Remove(file);
            }
        }
        public static IffFile GeneratePiff(IffFile iff, HashSet <Type> allowedTypes, HashSet <Type> disallowedTypes)
        {
            var piffFile = new IffFile();

            var piff = new PIFF();

            piff.SourceIff = iff.Filename;
            var entries = new List <PIFFEntry>();
            var chunks  = iff.ListAll();

            //write removals first
            foreach (var c in iff.RemovedOriginal)
            {
                lock (c)
                {
                    if ((allowedTypes == null || allowedTypes.Contains(c.GetType())) &&
                        (disallowedTypes == null || !disallowedTypes.Contains(c.GetType())))
                    {
                        entries.Add(new PIFFEntry {
                            Type       = c.ChunkType, ChunkID = c.OriginalID, Delete = true,
                            ChunkLabel = c.ChunkLabel, ChunkFlags = c.ChunkFlags
                        });
                    }
                }
            }

            foreach (var c in chunks)
            {
                lock (c)
                {
                    if ((allowedTypes == null || allowedTypes.Contains(c.GetType())) &&
                        (disallowedTypes == null || !disallowedTypes.Contains(c.GetType())))
                    {
                        if (c.AddedByPatch)
                        {
                            //this chunk has been newly added.
                            var oldParent = c.ChunkParent;
                            piffFile.AddChunk(c);
                            c.ChunkParent = oldParent;
                        }
                        else if ((c.RuntimeInfo == ChunkRuntimeState.Modified || c.RuntimeInfo == ChunkRuntimeState.Patched))
                        {
                            var chunkD = MakeChunkDiff(c);
                            if (chunkD != null && (chunkD.Patches.Length > 0 || c.OriginalLabel != c.ChunkLabel || c.OriginalID != c.ChunkID))
                            {
                                entries.Add(chunkD);
                            }
                            c.RuntimeInfo = ChunkRuntimeState.Patched;
                        }
                    }
                }
            }
            if (entries.Count == 0)
            {
                return(null);                    //no patch data...
            }
            piff.Entries        = entries.ToArray();
            piff.ChunkID        = 256;
            piff.ChunkLabel     = (piff.SourceIff + " patch");
            piff.ChunkProcessed = true;

            piffFile.AddChunk(piff);
            piffFile.Filename = piff.SourceIff.Substring(0, piff.SourceIff.Length - 4) + ".piff";
            return(piffFile);
        }
        public void SaveChange(IffFile file)
        {
            lock (this)
            {
                if (file.RuntimeInfo.State == IffRuntimeState.Standalone)
                {
                    //just save out iff
                    var filename = file.RuntimeInfo.Path;
                    Directory.CreateDirectory(Path.GetDirectoryName(filename));
                    using (var stream = new FileStream(filename, FileMode.Create))
                        file.Write(stream);

                    foreach (var chunk in file.ListAll())
                    {
                        chunk.RuntimeInfo = ChunkRuntimeState.Normal;
                    }
                }
                else
                {
                    string dest = Path.Combine(FSOEnvironment.ContentDir, ((file.RuntimeInfo.State == IffRuntimeState.PIFFClone) ? "Objects/" : "Patch/User/"));
                    Directory.CreateDirectory(dest);
                    if (file.RuntimeInfo.State == IffRuntimeState.ReadOnly)
                    {
                        file.RuntimeInfo.State = IffRuntimeState.PIFFPatch;
                    }

                    var stringResources = new HashSet <Type> {
                        typeof(STR), typeof(CTSS), typeof(TTAs)
                    };
                    var sprites    = (file.RuntimeInfo.UseCase == IffUseCase.ObjectSprites);
                    var oldPatches = file.RuntimeInfo.Patches.ToList();
                    file.RuntimeInfo.Patches.Clear();

                    //find the old piff. should be the piff that doesn't end in .str.piff
                    var oldPIFF = oldPatches.FirstOrDefault(x => !x.Filename.EndsWith(".str.piff"))?.List <PIFF>()?.ElementAt(0);

                    var piff = FSO.Files.Formats.PiffEncoder.GeneratePiff(file, null, stringResources, oldPIFF);

                    string name = file.Filename.Substring(0, file.Filename.Length - 4); //get without extension

                    if (piff != null)
                    {
                        if (piff.Filename == null)
                        {
                            piff.Filename = name + (sprites ? ".spf" : "") + ".piff";
                        }
                        var filename = dest + piff.Filename;
                        using (var stream = new FileStream(filename, FileMode.Create)) piff.Write(stream);
                        file.RuntimeInfo.Patches.Add(piff);
                    }

                    if (!sprites)
                    {
                        oldPIFF = oldPatches.FirstOrDefault(x => x.Filename.EndsWith(".str.piff"))?.List <PIFF>()?.ElementAt(0);
                        piff    = FSO.Files.Formats.PiffEncoder.GeneratePiff(file, stringResources, null, oldPIFF);
                        if (piff != null)
                        {
                            if (piff.Filename == null)
                            {
                                piff.Filename = name + ".str.piff";
                            }
                            var filename = dest + piff.Filename;
                            using (var stream = new FileStream(filename, FileMode.Create)) piff.Write(stream);
                            file.RuntimeInfo.Patches.Add(piff);
                        }
                    }
                }

                file.RuntimeInfo.Dirty = false;
                ChangedFiles.Remove(file);
            }
        }
Exemple #5
0
        public static IffFile GeneratePiff(IffFile iff, HashSet<Type> allowedTypes, HashSet<Type> disallowedTypes)
        {
            var piffFile = new IffFile();

            var piff = new PIFF();
            piff.SourceIff = iff.Filename;
            var entries = new List<PIFFEntry>();
            var chunks = iff.ListAll();

            //write removals first
            foreach (var c in iff.RemovedOriginal)
            {
                lock (c)
                {
                    if ((allowedTypes == null || allowedTypes.Contains(c.GetType()))
                        && (disallowedTypes == null || !disallowedTypes.Contains(c.GetType())))
                    {
                        entries.Add(new PIFFEntry {
                            Type = c.ChunkType, ChunkID = c.OriginalID, Delete = true,
                            ChunkLabel = c.ChunkLabel, ChunkFlags = c.ChunkFlags
                        });
                    }
                }
            }

            foreach (var c in chunks)
            {
                lock (c)
                {
                    if ((allowedTypes == null || allowedTypes.Contains(c.GetType()))
                        && (disallowedTypes == null || !disallowedTypes.Contains(c.GetType())))
                    {
                        if (c.AddedByPatch)
                        {
                            //this chunk has been newly added.
                            var oldParent = c.ChunkParent;
                            piffFile.AddChunk(c);
                            c.ChunkParent = oldParent;
                        }
                        else if ((c.RuntimeInfo == ChunkRuntimeState.Modified || c.RuntimeInfo == ChunkRuntimeState.Patched))
                        {
                            var chunkD = MakeChunkDiff(c);
                            if (chunkD != null && (chunkD.Patches.Length > 0 || c.OriginalLabel != c.ChunkLabel || c.OriginalID != c.ChunkID))
                            {
                                entries.Add(chunkD);
                            }
                            c.RuntimeInfo = ChunkRuntimeState.Patched;
                        }
                    }
                }
            }
            if (entries.Count == 0) return null; //no patch data...
            piff.Entries = entries.ToArray();
            piff.ChunkID = 256;
            piff.ChunkLabel = (piff.SourceIff + " patch");
            piff.ChunkProcessed = true;

            piffFile.AddChunk(piff);
            piffFile.Filename = piff.SourceIff.Substring(0, piff.SourceIff.Length - 4)+".piff";
            return piffFile;
        }
Exemple #6
0
        public static IffFile GeneratePiff(IffFile iff, HashSet <Type> allowedTypes, HashSet <Type> disallowedTypes, PIFF oldPIFF)
        {
            var piffFile = new IffFile();

            var piff = new PIFF();

            piff.SourceIff = iff.Filename;
            if (oldPIFF != null)
            {
                piff.Comment = oldPIFF.Comment;
            }
            var entries = new List <PIFFEntry>();
            var chunks  = iff.ListAll();

            //write removals first
            foreach (var c in iff.RemovedOriginal)
            {
                lock (c)
                {
                    if ((allowedTypes == null || allowedTypes.Contains(c.GetType())) &&
                        (disallowedTypes == null || !disallowedTypes.Contains(c.GetType())))
                    {
                        entries.Add(new PIFFEntry {
                            Type       = c.ChunkType, ChunkID = c.OriginalID, EntryType = PIFFEntryType.Remove,
                            ChunkLabel = c.ChunkLabel, ChunkFlags = c.ChunkFlags, Comment = FindComment(c, oldPIFF)
                        });
                    }
                }
            }

            bool anyAdded = false;

            foreach (var c in chunks)
            {
                //find a comment for this chunk
                lock (c)
                {
                    if ((allowedTypes == null || allowedTypes.Contains(c.GetType())) &&
                        (disallowedTypes == null || !disallowedTypes.Contains(c.GetType())))
                    {
                        if (c.AddedByPatch)
                        {
                            //this chunk has been newly added.
                            var oldParent = c.ChunkParent;
                            anyAdded = true;
                            piffFile.AddChunk(c);
                            c.ChunkParent = oldParent;

                            //make an entry for it!
                            entries.Add(new PIFFEntry()
                            {
                                ChunkID     = c.ChunkID,
                                ChunkLabel  = c.ChunkLabel,
                                ChunkFlags  = c.ChunkFlags,
                                EntryType   = PIFFEntryType.Add,
                                NewDataSize = (uint)(c.ChunkData?.Length ?? 0),
                                Type        = c.ChunkType,
                                Comment     = FindComment(c, oldPIFF)
                            });
                        }
                        else if ((c.RuntimeInfo == ChunkRuntimeState.Modified || c.RuntimeInfo == ChunkRuntimeState.Patched))
                        {
                            var chunkD = MakeChunkDiff(c);
                            if (chunkD != null && (chunkD.Patches.Length > 0 || c.OriginalLabel != c.ChunkLabel || c.OriginalID != c.ChunkID))
                            {
                                chunkD.Comment = FindComment(c, oldPIFF);
                                entries.Add(chunkD);
                            }
                            c.RuntimeInfo = ChunkRuntimeState.Patched;
                        }
                    }
                }
            }
            if (entries.Count == 0 && !anyAdded)
            {
                return(null);                                 //no patch data...
            }
            piff.Entries        = entries.ToArray();
            piff.ChunkID        = 256;
            piff.ChunkLabel     = (piff.SourceIff + " patch");
            piff.ChunkProcessed = true;

            piffFile.AddChunk(piff);
            piffFile.Filename = (oldPIFF != null) ? oldPIFF.ChunkParent.Filename : null; // (piff.SourceIff.Substring(0, piff.SourceIff.Length - 4)+".piff")
            return(piffFile);
        }