Esempio n. 1
0
        public void TestStoreDictionary()
        {
            byte[]             bytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };
            HuffmanCompression huff  = new HuffmanCompression(bytes);

            huff.Compress("..\\..\\..\\TestImages\\huff_dictionary_test.bi");
        }
        private void CommitTLK(object obj)
        {
            HuffmanCompression huff = new HuffmanCompression();

            huff.LoadInputData(LoadedStrings);
            huff.serializeTLKStrListToExport(CurrentLoadedExport);
            FileModified = false;
        }
        private void Evt_ImportXML(object sender, RoutedEventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog
            {
                Multiselect = false,
                Filter      = "XML Files (*.xml)|*.xml"
            };

            if (openFileDialog.ShowDialog() == true)
            {
                HuffmanCompression compressor = new HuffmanCompression();
                compressor.LoadInputData(openFileDialog.FileName);
                compressor.serializeTLKStrListToExport(CurrentLoadedExport);
                FileModified = true; //this is not always technically true, but we'll assume it is
            }
        }
Esempio n. 4
0
        public void TestDecompression()
        {
            byte[]             bytes = new byte[] { 1, 1, 1, 2, 3, 23, 4, 65, 3, 5, 3, 6, 4, 78, 5, 9, 56, 5, 8, 5, 8, 4, 8, 43, 78, 4, 87 };
            HuffmanCompression huff  = new HuffmanCompression(bytes);

            huff.Compress("..\\..\\..\\TestImages\\huffman_compressed_data.bi");
            byte[] storedBytes = File.ReadAllBytes("..\\..\\..\\TestImages\\huffman_compressed_data.bi");
            HuffmanDecompression decompression = new HuffmanDecompression("..\\..\\..\\TestImages\\huffman_compressed_data.bi");

            byte[] decompressedBytes = decompression.Decompress();

            Assert.Equal(bytes.Length, decompressedBytes.Length);
            for (int i = 0; i < decompressedBytes.Length; i++)
            {
                Assert.Equal(bytes[i], decompressedBytes[i]);
            }
        }
Esempio n. 5
0
        /// <summary>
        /// Installs a TLK merge. Returns null if OK, otherwise returns an error string.
        /// </summary>
        /// <param name="tlkXmlName"></param>
        /// <param name="gameFileMapping"></param>
        /// <returns></returns>
        public string InstallTLKMerge(string tlkXmlName, Dictionary <string, string> gameFileMapping, bool savePackage, PackageCache cache, GameTarget target, Mod modBeingInstalled, Action <BasegameFileIdentificationService.BasegameCloudDBFile> addCloudDBEntry)
        {
            // Need to load file into memory
            string xmlContents;
            var    sourcePath = FilesystemInterposer.PathCombine(IsInArchive, ModPath, Mod.Game1EmbeddedTlkFolderName, tlkXmlName);

            if (Archive != null)
            {
                var ms = new MemoryStream();
                Archive.ExtractFile(sourcePath, ms);
                ms.Position = 0;
                xmlContents = new StreamReader(ms).ReadToEnd();
            }
            else
            {
                xmlContents = File.ReadAllText(sourcePath);
            }

            var tlkDoc      = XDocument.Parse(xmlContents);
            var stringNodes = tlkDoc.Root.Descendants(@"string").ToList();

            if (stringNodes.Any())
            {
                // Open package
                var packageName = tlkXmlName.Substring(0, tlkXmlName.IndexOf('.'));
                var exportPath  = Path.GetFileNameWithoutExtension(tlkXmlName.Substring(packageName.Length + 1));

                string packagePath = null;;

                if (Game is MEGame.LE1)
                {
                    gameFileMapping.TryGetValue(packageName + @".pcc", out packagePath);
                }
                else if (Game is MEGame.ME1)
                {
                    gameFileMapping.TryGetValue(packageName + @".sfm", out packagePath);
                    if (packagePath == null)
                    {
                        gameFileMapping.TryGetValue(packageName + @".u", out packagePath);
                    }
                    if (packagePath == null)
                    {
                        gameFileMapping.TryGetValue(packageName + @".upk", out packagePath);
                    }
                }

                if (packagePath != null)
                {
                    var package = cache.GetCachedPackage(packagePath);
                    var exp     = package.FindExport(exportPath);
                    if (exp == null)
                    {
                        // WRONGLY NAMED EXPORT!
                        Log.Error($@"Could not find export in package {packagePath} for TLK merge: {exportPath}");
                        return(M3L.GetString(M3L.string_interp_tlkmerge_couldNotFindExportInPackage, packagePath, exportPath));
                    }

                    var talkFile = package.LocalTalkFiles.FirstOrDefault(x => x.UIndex == exp.UIndex);
                    var strRefs  = talkFile.StringRefs.ToList();
                    int numDone  = 0;
                    foreach (var node in stringNodes)
                    {
                        var tlkId = int.Parse(node.Element(@"id").Value);
                        var flags = int.Parse(node.Element(@"flags").Value);
                        var data  = node.Element(@"data").Value;

                        var strRef = talkFile.StringRefs.FirstOrDefault(x => x.StringID == tlkId);
                        if (strRef == null)
                        {
                            CLog.Information($@"Adding new TLK id {tlkId}", Settings.LogModInstallation);
                            strRefs.Add(new TLKStringRef(tlkId, data, flags));
                        }
                        else
                        {
                            //if (numDone <= 25)
                            //{
                            //    //CLog.Information($@"Updating TLK id {tlkId}", Settings.LogModInstallation);
                            //    if (numDone == 25)
                            //    {
                            //        //CLog.Information($@"Remaining updates will not be logged for this TLK to trim log size...", Settings.LogModInstallation);
                            //    }
                            //}
                            strRef.Data = data;
                            // Flags update was removed here
                        }

                        numDone++;
                    }

                    HuffmanCompression huff = new HuffmanCompression();
                    huff.LoadInputData(strRefs);
                    huff.SerializeTalkfileToExport(exp);
                    if (savePackage && package.IsModified)
                    {
                        Log.Information($@"Saving TLKMerged package {packagePath}");
                        package.Save();
                        addCloudDBEntry?.Invoke(new BasegameFileIdentificationService.BasegameCloudDBFile(package.FilePath, (int)new FileInfo(package.FilePath).Length, target, modBeingInstalled));
                        cache.DropPackageFromCache(packagePath); // we are not doing more operations on this file so drop it out
                    }
                }
            }
            // Logic subject to change in future!
            return(null);
        }