public BifFile Read(Stream s, List<KeyBifResource2> resources, List<IEFileType> fileTypes)
        {
            BinaryReader br = new BinaryReader(s);
            try
            {
                var header = (BifHeaderBinary)Common.ReadStruct(br, typeof(BifHeaderBinary));

                if ((header.ftype.ToString() == "BIFC") && (header.fversion.ToString() == "V1.0"))
                {
                    br.BaseStream.Seek(0, SeekOrigin.Begin);
                    var headerC = (BifCHeaderV1Binary)Common.ReadStruct(br, typeof(BifCHeaderV1Binary));

                    var m = new MemoryStream();
                    int size = 0;
                    var theseBytes = new byte[headerC.FileLength];
                    while (size < headerC.FileLength)
                    {
                        var decompressedLength = br.ReadInt32();
                        var compressedLength = br.ReadInt32();
                        var bytes = br.ReadBytes(compressedLength);

                        m.Write(Ionic.Zlib.ZlibStream.UncompressBuffer(bytes), 0, decompressedLength);
                        size = size + decompressedLength;
                    }
                    /*
                    using (FileStream fs = new FileStream(@"D:\out.bif", FileMode.Create))
                    {
                        m.CopyTo(fs);
                        fs.Flush();
                    }
                    */

                    if (br != null)
                    {
                        ((IDisposable)br).Dispose();
                    }
                    s = new MemoryStream();
                    m.Position = 0;
                    m.CopyTo(s);
                    s.Position = 0;

                    br = new BinaryReader(s);
                    header = (BifHeaderBinary)Common.ReadStruct(br, typeof(BifHeaderBinary));
                }

                Debug.WriteLine("Bif is uncompressed - reading");

                br.BaseStream.Seek(header.FileOffset, SeekOrigin.Begin);
                for (int i = 0; i < header.FileCount; i++)
                {
                    var resource = (BifFileEntryBinary)Common.ReadStruct(br, typeof(BifFileEntryBinary));
                    fileStructs.Add(resource);
                }

                for (int i = 0; i < header.TilesetCount; i++)
                {
                    var resource = (BifTilesetEntryBinary)Common.ReadStruct(br, typeof(BifTilesetEntryBinary));
                    tileStructs.Add(resource);
                }

                int ix = 0;
                foreach (var f in fileStructs)
                {
                    if (fileTypes.Contains((IEFileType)f.resourceType))
                    {
                        br.BaseStream.Seek(f.resourceOffset, SeekOrigin.Begin);
                        var file = br.ReadBytes(f.resourceSize);
                        using (MemoryStream ms = new MemoryStream(file))
                        {
                            KeyBifResource2 resource;
                            switch ((IEFileType)f.resourceType)
                            {
                                case IEFileType.Are:
                                    try
                                    {
                                        AreFileBinaryReader are = new AreFileBinaryReader();
                                        var area = (AreFile)are.Read(ms);
                                        resource = resources.Where(a => a.NonTileSetIndex == (f.resourceLocator & 0xFFF)).SingleOrDefault();
                                        if (resource != null)
                                        {
                                            area.Filename = resource.ResourceName + "." + resource.ResourceType;
                                        }
                                        areas.Add(area);
                                    }
                                    catch (Exception ex) { Trace.WriteLine(ex.ToString()); }
                                    break;

                                case IEFileType.Pro:
                                    try
                                    {
                                        ProFileBinaryReader pro = new ProFileBinaryReader();
                                        var projectile = (ProFile)pro.Read(ms);
                                        resource = resources.Where(a => a.NonTileSetIndex == (f.resourceLocator & 0xFFF)).SingleOrDefault();
                                        if (resource != null)
                                        {
                                            projectile.Filename = resource.ResourceName + "." + resource.ResourceType;
                                        }
                                        projectiles.Add(projectile);
                                    }
                                    catch (Exception ex) { Trace.WriteLine(ex.ToString()); }
                                    break;

                                case IEFileType.Ids:
                                    try
                                    {
                                        IdsFileReader ids = new IdsFileReader();
                                        var identifier = (IdsFile)ids.Read(ms);
                                        resource = resources.Where(a => a.NonTileSetIndex == (f.resourceLocator & 0xFFF)).SingleOrDefault();
                                        if (resource != null)
                                        {
                                            identifier.Filename = resource.ResourceName + "." + resource.ResourceType;
                                        }
                                        identifiers.Add(identifier);
                                    }
                                    catch (Exception ex) { Trace.WriteLine(ex.ToString()); }
                                    break;

                                case IEFileType.DimensionalArray:
                                    try
                                    {
                                        DimensionalArrayFileReader twoDeeAy = new DimensionalArrayFileReader();
                                        var dimensionalArray = (DimensionalArrayFile)twoDeeAy.Read(ms);
                                        resource = resources.Where(a => a.NonTileSetIndex == (f.resourceLocator & 0xFFF)).SingleOrDefault();
                                        if (resource != null)
                                        {
                                            // We special case this filename, as we can't set the FileType identifier to start with a number
                                            dimensionalArray.Filename = resource.ResourceName + ".2da";
                                        }
                                        dimensionalArrays.Add(dimensionalArray);
                                    }
                                    catch (Exception ex) { Trace.WriteLine(ex.ToString()); }
                                    break;

                                case IEFileType.Cre:
                                    try
                                    {
                                        CreFileBinaryReader cre = new CreFileBinaryReader();
                                        var creature = (CreFile)cre.Read(ms);
                                        resource = resources.Where(a => a.NonTileSetIndex == (f.resourceLocator & 0xFFF)).SingleOrDefault();
                                        if (resource != null)
                                        {
                                            creature.Filename = resource.ResourceName + "." + resource.ResourceType;
                                        }
                                        creatures.Add(creature);
                                    }
                                    catch (Exception ex) { Trace.WriteLine(ex.ToString()); }
                                    break;

                                case IEFileType.Eff:
                                    try
                                    {
                                        EffFileBinaryReader eff = new EffFileBinaryReader();
                                        var effect = (EffFile)eff.Read(ms);
                                        resource = resources.Where(a => a.NonTileSetIndex == (f.resourceLocator & 0xFFF)).SingleOrDefault();
                                        if (resource != null)
                                        {
                                            effect.Filename = resource.ResourceName + "." + resource.ResourceType;
                                        }
                                        effs.Add(effect);
                                    }
                                    catch (Exception ex) { Trace.WriteLine(ex.ToString()); }
                                    break;

                                case IEFileType.Itm:
                                    try
                                    {
                                        ItmFileBinaryReader itm = new ItmFileBinaryReader();
                                        itm.TlkFile = TlkFile;
                                        var item = (ItmFile)itm.Read(ms);
                                        resource = resources.Where(a => a.NonTileSetIndex == (f.resourceLocator & 0xFFF)).SingleOrDefault();
                                        if (resource != null)
                                        {
                                            item.Filename = resource.ResourceName + "." + resource.ResourceType;
                                        }
                                        items.Add(item);
                                    }
                                    catch (Exception ex) { Trace.WriteLine(ex.ToString()); }
                                    break;

                                case IEFileType.Spl:
                                    try
                                    {
                                        SplFileBinaryReader spl = new SplFileBinaryReader();
                                        var spell = (SplFile)spl.Read(ms);
                                        resource = resources.Where(a => a.NonTileSetIndex == (f.resourceLocator & 0xFFF)).SingleOrDefault();
                                        if (resource != null)
                                        {
                                            spell.Filename = resource.ResourceName + "." + resource.ResourceType;
                                        }
                                        spells.Add(spell);
                                    }
                                    catch (Exception ex) { Trace.WriteLine(ex.ToString()); }
                                    break;

                                case IEFileType.Sto:
                                    try
                                    {
                                        StoFileBinaryReader sto = new StoFileBinaryReader();
                                        var store = (StoFile)sto.Read(ms);
                                        resource = resources.Where(a => a.NonTileSetIndex == (f.resourceLocator & 0xFFF)).SingleOrDefault();
                                        if (resource != null)
                                        {
                                            store.Filename = resource.ResourceName + "." + resource.ResourceType;
                                        }
                                        stores.Add(store);
                                    }
                                    catch (Exception ex) { Trace.WriteLine(ex.ToString()); }
                                    break;

                                case IEFileType.Vvc:
                                    try
                                    {
                                        VvcFileBinaryReader vvc = new VvcFileBinaryReader();
                                        var vvcFile = (VvcFile)vvc.Read(ms);
                                        resource = resources.Where(a => a.NonTileSetIndex == (f.resourceLocator & 0xFFF)).SingleOrDefault();
                                        if (resource != null)
                                        {
                                            vvcFile.Filename = resource.ResourceName + "." + resource.ResourceType;
                                        }
                                        vvcs.Add(vvcFile);
                                    }
                                    catch (Exception ex) { Trace.WriteLine(ex.ToString()); }
                                    break;

                                case IEFileType.Wfx:
                                    try
                                    {
                                        WfxFileBinaryReader wfx = new WfxFileBinaryReader();
                                        var wfxfile = (WfxFile)wfx.Read(ms);
                                        resource = resources.Where(a => a.NonTileSetIndex == (f.resourceLocator & 0xFFF)).SingleOrDefault();
                                        if (resource != null)
                                        {
                                            wfxfile.Filename = resource.ResourceName + "." + resource.ResourceType;
                                        }
                                        wfxs.Add(wfxfile);
                                    }
                                    catch (Exception ex) { Trace.WriteLine(ex.ToString()); }
                                    break;

                                case IEFileType.Wmp:
                                    try
                                    {
                                        WmpFileBinaryReader wmp = new WmpFileBinaryReader();
                                        var worldmap = (WmpFile)wmp.Read(ms);
                                        resource = resources.Where(a => a.NonTileSetIndex == (f.resourceLocator & 0xFFF)).SingleOrDefault();
                                        if (resource != null)
                                        {
                                            worldmap.Filename = resource.ResourceName + "." + resource.ResourceType;
                                        }
                                        worldmaps.Add(worldmap);
                                    }
                                    catch (Exception ex) { Trace.WriteLine(ex.ToString()); }
                                    break;
                            }
                        }
                    }
                    ix++;
                }

                BifFile bifFile = new BifFile();
                bifFile.items = items;
                bifFile.spells = spells;
                bifFile.effects = effs;
                bifFile.wfx = wfxs;
                bifFile.stores = stores;
                bifFile.creatures = creatures;
                bifFile.projectiles = projectiles;
                bifFile.identifiers = identifiers;
                bifFile.dimensionalArrays = dimensionalArrays;
                bifFile.areas = areas;
                bifFile.worldmaps = worldmaps;
                bifFile.vvcs = vvcs;
                return bifFile;
            }
            finally
            {
                if (br != null)
                {
                    ((IDisposable)br).Dispose();
                }
            }
        }
Beispiel #2
0
        public void ProcessItemWithStrings()
        {
            // If we want to load and edit a file directory, but amend the strings, we can pass the Tlk file in
            var tlkReader = new TlkFileBinaryReader();
            var tlkFile = tlkReader.Read(@"D:\games\bg2\dialog.tlk");

            var reader2 = new ItmFileBinaryReader();
            reader2.TlkFile = tlkFile;
            var itm2 = reader2.Read(@"D:\games\bg2\override\hamm09.ITM");
            itm2.IdentifiedName.Strref = Common.NewString;
            itm2.IdentifiedName.Text = "Really awesome hammer";

            var writer2 = new ItmFileBinaryWriter();
            writer2.TlkFile = tlkFile;
            writer2.Write(@"D:\games\bg2\modified_items.itm", itm2);

            TlkFileBinaryWriter tlkWriter = new TlkFileBinaryWriter();
            tlkWriter.Write(@"D:\games\bg2\dialog.tlk", tlkFile);
        }
Beispiel #3
0
        private void Form1_Load(object sender, EventArgs e)
        {
            try
            {
                var languageFile = @"D:\MyMod\Languages\English\Main.tra";

                // Load all the resources in a game (excluding the override directory)
                Game game = new Game(@"D:\Games\ie\bg2\main");
                game.backupManager = new BackupManager(@"D:\Games\ie\bg2\main\mod_backup");
                game.LoadResources(IEFileType.Itm);

                Console.Write(String.Empty);

                //BcsDecompiler decom = new BcsDecompiler(game.Identifiers);
                //var output = decom.Decompile(@"D:\1output.baf");
                //var output = decom.Decompile(@"D:\output.bcs");
                //File.WriteAllLines(@"D:\output.baf", output.ToArray());

                //BcsCompiler compiler = new BcsCompiler(game.Identifiers);
                //var output = compiler.Compile(@"D:\Anomen.baf");
                //var output = compiler.Compile(@"D:\Games\ie\bg2\main\test.baf");
                //File.WriteAllLines(@"D:\output.baf", output.ToArray());

                // Update every item in the game
                foreach (var item in game.Items)
                {
                    // Set the price of every item to 500
                    item.Price = 500;

                    // Set the description to a translated string (translation files are in standard WeiDu .tra format)
                    item.IdentifiedName.Text = Translator.Text(languageFile, "@11");

                    // The filenames property is automatically loaded into the file, but contains no path element
                    var location = item.Filename;
                    var filename = Path.GetFileName(location);
                    item.Filename = String.Format("D:\\{0}", filename);
                    game.Save<ItmFile>(item);
                }
                game.Save<TlkFile>(game.Tlk);

                // Or, we can just load and edit a file directly (perhaps we don't want to read all files, or we don't have the game installed)
                var reader1 = new ItmFileBinaryReader();
                var itm1 = reader1.Read(@"D:\Games\ie\bg2\main\override\hamm09.ITM");
                itm1.Price = 200;

                var writer1 = new ItmFileBinaryWriter();
                writer1.BackupManger = new BackupManager(@"D:\back");
                writer1.Write(@"D:\out.itm", itm1);

                // If we want to load and edit a file directory, but amend the strings, we can pass the Tlk file in
                var tlkReader = new TlkFileBinaryReader();
                var tlkFile = tlkReader.Read(@"D:\Games\ie\bg2\main\dialog.tlk");

                var reader2 = new ItmFileBinaryReader();
                reader2.TlkFile = tlkFile;
                var itm2 = reader2.Read(@"D:\Games\ie\bg2\main\override\hamm09.ITM");
                itm1.Price = 200;
                itm2.IdentifiedName.Strref = Common.NewString;
                itm2.IdentifiedName.Text = "Really awesome hammer";

                var writer2 = new ItmFileBinaryWriter();
                writer2.TlkFile = tlkFile;
                writer2.Write(@"D:\out.itm", itm2);

                TlkFileBinaryWriter tlkWriter = new TlkFileBinaryWriter();
                tlkWriter.Write(@"D:\dialog.tlk", tlkFile);

                // Or we can just dump files in the override
                IEFileCopier copier = new IEFileCopier();
                copier.CopyFile(@"D:\test.bmp", @"E:\test.bmp");

                // Or we can dump files in the override - but with backup capability
                var bm = new BackupManager(@"D:\back");
                IEFileCopier c = new IEFileCopier();
                c.BackupManager = bm;
                c.CopyFile(@"D:\New Text Document.txt", @"E:\New Text Document.txt");
                // And we can then uninstall the changes
                bm.Uninstall();
            }
            catch (Exception ex)
            {
                Console.Write(ex.Message);
            }
        }
Beispiel #4
0
        public void ProcessItem()
        {
            // We can just load and edit a file directly (perhaps we don't want to read all files, or we don't have the game installed)
            var reader1 = new ItmFileBinaryReader();
            var itm1 = reader1.Read(@"D:\games\bg2\override\hamm09.ITM");
            itm1.Price = 200;

            var writer1 = new ItmFileBinaryWriter();
            writer1.BackupManger = new BackupManager(@"D:\games\bg2\backup");
            writer1.Write(@"D:\games\bg2\modified_items.itm", itm1);
        }