Example #1
0
        private static HoSection[] GetFilesData(string INIFile, ref Section_HEL HEL)
        {
            // Let's get the data from the files now and put them in a dictionary
            Dictionary <uint, byte[]> assetDataDictionary = new Dictionary <uint, byte[]>();

            foreach (string f in Directory.GetDirectories(Path.GetDirectoryName(INIFile)))
            {
                foreach (string i in Directory.GetFiles(f))
                {
                    byte[] file = File.ReadAllBytes(i);
                    try
                    {
                        assetDataDictionary.Add(Convert.ToUInt32(Path.GetFileName(i).Substring(1, 8), 16), file);
                    }
                    catch (Exception e)
                    {
                        SendMessage("Error importing asset " + Path.GetFileName(i) + ": " + e.Message);
                    }
                }
            }

            // Now send all the data to the function that'll put the data in the AHDR then fill the STRM section with that
            return(SetupStream(ref HIPA, ref PACK, ref DICT, ref STRM, false, assetDataDictionary));
        }
Example #2
0
        public static Section_HEL SetupStream(ref Section_HEL HEL, bool alreadyHasData = true, Dictionary <uint, byte[]> assetDataDictionary = null)
        {
            // Let's go through each layer.
            foreach (Section_LHDR LHDR in DICT.LTOC.LHDRList)
            {
                // Sort the LDBG asset IDs. The AHDR data will then be written in this order.
                LHDR.assetIDlist = LHDR.assetIDlist.OrderBy(i => i).ToList();

                for (int i = 0; i < LHDR.assetIDlist.Count; i++)
                {
                    Section_AHDR AHDR = assetDictionary[LHDR.assetIDlist[i]];

                    // Does the AHDR section already have the asset data, or should we get it from the dictionary?
                    // AHDRs from IP will already have data, but the ones from the INI builder won't!
                    if (!alreadyHasData)
                    {
                        if (!assetDataDictionary.Keys.Contains(AHDR.assetID))
                        {
                            SendMessage($"Error: asset with ID [{AHDR.assetID.ToString("X8")}] was not found. The asset will be removed from the archive.");
                            DICT.ATOC.AHDRList.Remove(AHDR);
                            LHDR.assetIDlist.Remove(AHDR.assetID);
                            i--;
                            continue;
                        }
                        AHDR.data = assetDataDictionary[AHDR.assetID];
                    }

                    // Set stream dependant AHDR data...
                    AHDR.fileOffset = newStream.Count + STRM.DPAK.globalRelativeStartOffset;
                    AHDR.fileSize   = AHDR.data.Length;

                    // And add the data to the stream.
                    newStream.AddRange(AHDR.data);

                    // Calculate alignment data which I don't understand, but hey it works.
                    AHDR.plusValue = 0;

                    int alignment = 16;
                    if (currentGame == Game.BFBB)
                    {
                        if (AHDR.assetType == AssetType.CSN |
                            AHDR.assetType == AssetType.SND |
                            AHDR.assetType == AssetType.SNDS)
                        {
                            alignment = 32;
                        }
                        else if (AHDR.assetType == AssetType.CRDT)
                        {
                            alignment = 4;
                        }
                    }

                    int value = AHDR.fileSize % alignment;
                    if (value != 0)
                    {
                        AHDR.plusValue = alignment - value;
                    }
                    for (int j = 0; j < AHDR.plusValue; j++)
                    {
                        newStream.Add(0x33);
                    }
                }

                while ((newStream.Count + STRM.DPAK.globalRelativeStartOffset) % 0x20 != 0)
                {
                    newStream.Add(0x33);
                }
            }

            // Assign list as stream! We're done with the worst part.
            STRM.DPAK.data = newStream.ToArray();

            // I'll create a new PCNT, because I'm sure you'll forget to do so.
            PACK.PCNT = new Section_PCNT(DICT.ATOC.AHDRList.Count, DICT.LTOC.LHDRList.Count, LargestSourceFileAsset, LargestLayer, LargestSourceVirtualAsset);

            // We're done!
            return(new HoSection[] { HIPA, PACK, DICT, STRM });
        }