Esempio n. 1
0
        private void ReloadIndex(string rootPath)
        {
            string indexPath = Path.Combine(rootPath, "index2.rif");

            if (!File.Exists(indexPath))
            {
                return;
            }

            RiffFile rif      = RiffFile.FromFile(indexPath);
            Index2   newIndex = rif.Objects.FirstOrDefault(x => x is Index2) as Index2;

            if (newIndex == null)
            {
                return;
            }

            // Updates index2 object
            if (Index == null || newIndex.Version >= Index.Version)
            {
                Index = newIndex;

                // Creates directory paths for entries
                foreach (Index2Entry entry in Index.Entries)
                {
                    entry.FilePath.GetParentDirectory();
                }
            }
        }
Esempio n. 2
0
        public void LoadPackage(string rootPath)
        {
            string fullRootPath = Path.GetFullPath(rootPath);
            string defDirectory = Path.Combine(rootPath, "packagedefs");

            // Loads PackageDef
            if (!Directory.Exists(fullRootPath) || !Directory.Exists(defDirectory))
            {
                return;
            }

            string[] packageRifs = Directory.GetFiles(defDirectory, "*.rif", SearchOption.AllDirectories);
            if (packageRifs.Length <= 0)
            {
                return;
            }

            foreach (string packageRif in packageRifs)
            {
                RiffFile   rif        = RiffFile.FromFile(packageRif);
                PackageDef newPackage = rif.Objects.FirstOrDefault(x => x is PackageDef) as PackageDef;
                if (newPackage == null)
                {
                    continue;
                }

                // Updates package path
                if (_packagePaths.ContainsKey(newPackage.FilePath))
                {
                    _packagePaths.Remove(newPackage.FilePath);
                }
                _packagePaths.Add(newPackage.FilePath, fullRootPath);

                CreateHKeys(newPackage.PackageName);
                newPackage.Entries.ForEach(x => CreateHKeys(x));

                // Adds package definition to collection
                _packageDefinitions.RemoveAll(x => x.Version == newPackage.Version);
                _packageDefinitions.Add(newPackage);
            }

            ReloadIndex(fullRootPath);
        }
Esempio n. 3
0
        private bool LoadRiffFile(string path, Index2PackageEntry packageEntry)
        {
            RiffFile rif = RiffFile.FromFile(path);

            if (rif == null)
            {
                return(false);
            }

            _tempObjects.Clear();
            _tempObjectsPackageEntry = packageEntry;

            // Loads all zobjects from riff file
            foreach (ZObject obj in rif.Objects.Where(x => !(x is StringTable)))
            {
                CreateStringTablePaths(obj.DirectoryPath); // Creates string table path
                _tempObjects.Add(obj);
            }

            return(true);
        }
Esempio n. 4
0
        private static RiffFile FromStream(Stream stream)
        {
            RiffFile      riff = new RiffFile();
            AwesomeReader ar   = new AwesomeReader(stream);

            // Checks for "RIFF" magic.
            switch (ar.ReadInt32())
            {
            case MAGIC_RIFF:
                ar.BigEndian = false;
                break;

            case MAGIC_RIFF_R:
                ar.BigEndian = true;
                break;

            default:
                throw new Exception("Invalid magic. Expected \"RIFF\"");
            }

            riff.BigEndian          = ar.BigEndian; // Sets endianess
            ar.BaseStream.Position += 4;            // Skips total size

            int chunkType = GetChunkType(ar);

            if (chunkType != MAGIC_INDX)
            {
                throw new Exception("First chunk was not an Index!");
            }

            Index index = new Index(ar);

            foreach (IndexEntry entry in index.Entries)
            {
                ar.BaseStream.Position = entry.Offset; // Jumps to offset
                chunkType = GetChunkType(ar);

                if (chunkType != MAGIC_STBL && chunkType != MAGIC_ZOBJ)
                {
                    continue;
                }

                // Reads header info
                HKey filePath      = new HKey(ar.ReadUInt64());
                HKey directoryPath = new HKey(ar.ReadUInt64());
                HKey type          = new HKey(ar.ReadUInt64());
                ar.BaseStream.Position += 8;

                if (chunkType == MAGIC_STBL)
                {
                    // Gets localization
                    if (!StringTable.IsValidLocalization(type))
                    {
                        continue;
                    }

                    // Loads string table
                    StringTable table = new StringTable(filePath, directoryPath, StringTable.GetLocalization(type));
                    table.ReadData(ar);
                    riff._objects.Add(table);
                }
                else if (chunkType == MAGIC_ZOBJ)
                {
                    if (!Global.ZObjectTypes.ContainsKey(type))
                    {
                        continue;                                         // Unsupported type
                    }
                    // Loads zobject
                    ZObject obj = Activator.CreateInstance(Global.ZObjectTypes[type], new object[] { filePath, directoryPath }) as ZObject;
                    obj.ReadData(ar);
                    riff._objects.Add(obj);
                }
                else
                {
                    // Unknown chunk
                    continue;
                }
            }

            return(riff);
        }
Esempio n. 5
0
        public void SavePendingChanges()
        {
            if (!PendingChanges)
            {
                return;
            }

            // Groups objects by package
            var packages = _pendingChanges.Select(obj => new
            {
                ZObject      = obj,
                PackageEntry = Index.Entries.First(idx => idx.FilePath == obj.FilePath).PackageEntries.First()
            }).OrderBy(x => x.PackageEntry.ExternalFilePath).GroupBy(x => x.PackageEntry.Package);

            foreach (var package in packages)
            {
                HKey   packagePath         = package.Key;
                string physicalPackagePath = _packagePaths[packagePath];

                var fusedFiles = package.GroupBy(x => x.PackageEntry.ExternalFilePath);

                foreach (var fusedFile in fusedFiles)
                {
                    string   fusedFilePath = Path.Combine(physicalPackagePath, fusedFile.Key);
                    RiffFile rif           = null;

                    // TODO: Find a better solution (Less ugly)
                    if (File.Exists(fusedFilePath))
                    {
                        try
                        {
                            // Opens existing file to preserve zobjects
                            rif = RiffFile.FromFile(fusedFilePath);
                        }
                        catch
                        {
                        }
                        finally
                        {
                            if (rif == null)
                            {
                                rif = new RiffFile();
                            }
                        }
                    }
                    else
                    {
                        rif = new RiffFile();
                    }

                    foreach (var zobject in fusedFile.Select(x => x.ZObject))
                    {
                        // Removes existing zobject if present
                        rif.Objects.RemoveAll(x => x.FilePath == zobject.FilePath);

                        // Adds new zobject
                        rif.Objects.Add(zobject);
                    }

                    // Saves new rif file
                    rif.WriteToFile(fusedFilePath);
                }
            }

            // Sorts index entries by file paths
            Index.Entries.Sort((x, y) => string.Compare(x.FilePath, y.FilePath, true));

            // Saves index for current package only
            RiffFile indexRif = new RiffFile();

            indexRif.Objects.Add(Index);
            indexRif.WriteToFile(Path.Combine(CurrentPackageDirectory, "index2.rif"));

            _pendingChanges.Clear();
        }