Пример #1
0
        public void Save(string filePath, ObjectDatabase objectDatabase, TextureDatabase textureDatabase,
                         BoneDatabase boneDatabase)
        {
            // Assume it's being exported for F2nd PS3
            if (BinaryFormatUtilities.IsClassic(Format) &&
                filePath.EndsWith(".osd", StringComparison.OrdinalIgnoreCase))
            {
                Format     = BinaryFormat.F2nd;
                Endianness = Endianness.BigEndian;
            }

            // Or reverse
            else if (BinaryFormatUtilities.IsModern(Format) &&
                     filePath.EndsWith(".bin", StringComparison.OrdinalIgnoreCase))
            {
                Format     = BinaryFormat.DT;
                Endianness = Endianness.LittleEndian;
            }

            string fileName = Path.GetFileName(filePath);

            bool exported = false;

            if (objectDatabase != null && TextureSet != null)
            {
                var objectEntry = objectDatabase.GetObjectByFileName(fileName);
                if (objectEntry != null)
                {
                    string textureOutputPath =
                        Path.Combine(Path.GetDirectoryName(filePath), objectEntry.TextureFileName);

                    TextureSet.Save(textureOutputPath);
                    exported = true;
                }
            }

            if (!exported && TextureSet != null)
            {
                string textureOutputPath = string.Empty;

                // Try to assume a texture output name
                if (filePath.EndsWith("_obj.bin", StringComparison.OrdinalIgnoreCase))
                {
                    textureOutputPath = $"{filePath.Substring( 0, filePath.Length - 8 )}_tex.bin";
                }

                else if (filePath.EndsWith(".osd", StringComparison.OrdinalIgnoreCase))
                {
                    textureOutputPath = Path.ChangeExtension(filePath, "txd");
                }

                if (!string.IsNullOrEmpty(textureOutputPath))
                {
                    TextureSet.Save(textureOutputPath);
                }
            }

            using (var destination = File.Create(filePath))
                Save(destination, objectDatabase, textureDatabase, boneDatabase);
        }
Пример #2
0
        public override void Write(EndianBinaryWriter writer, ISection section = null)
        {
            // Update the format if needed for future parsing
            if (!ScriptUtilities.GetScriptFormats(Format).Contains(ScriptFormat))
            {
                ScriptFormat = ScriptUtilities.GetDefaultScriptFormat(Format);
            }

            bool modern = BinaryFormatUtilities.IsModern(Format);

            if (modern)
            {
                // TODO: write header
                throw new NotImplementedException();
            }
            else
            {
                writer.Write(ScriptFormat);
            }

            WriteCommands(writer);

            if (modern)
            {
                // TODO: write EOFC
                throw new NotImplementedException();
            }
        }
Пример #3
0
        protected override void InitializeCore()
        {
            RegisterExportHandler <TextureDatabase>((path) =>
            {
                // Assume it's being exported for F2nd PS3
                if (BinaryFormatUtilities.IsClassic(Data.Format) && path.EndsWith(".txi", StringComparison.OrdinalIgnoreCase))
                {
                    Data.Format     = BinaryFormat.F2nd;
                    Data.Endianness = Endianness.BigEndian;
                }

                // Or reverse
                else if (BinaryFormatUtilities.IsModern(Data.Format) && path.EndsWith(".bin", StringComparison.OrdinalIgnoreCase))
                {
                    Data.Format     = BinaryFormat.DT;
                    Data.Endianness = Endianness.LittleEndian;
                }

                Data.Save(path);
            });
            RegisterReplaceHandler <TextureDatabase>(BinaryFile.Load <TextureDatabase>);
            RegisterDataUpdateHandler(() =>
            {
                var data        = new TextureDatabase();
                data.Format     = Format;
                data.Endianness = Endianness;
                data.Textures.AddRange(Textures.Data);
                return(data);
            });
        }
Пример #4
0
 public BinaryFileSection(SectionMode mode, T dataObject = default(T)) : base(mode, dataObject)
 {
     if (mode == SectionMode.Write)
     {
         Endianness   = dataObject.Endianness;
         AddressSpace = BinaryFormatUtilities.GetAddressSpace(dataObject.Format);
     }
 }
Пример #5
0
        protected override void OnReplace(object oldData)
        {
            Model oldDataT = ( Model )oldData;

            // Replace the mesh name with the one we replaced.
            if (oldDataT.Meshes.Count == Data.Meshes.Count)
            {
                Data.Meshes[0].Name = oldDataT.Meshes[0].Name;
                Data.Meshes[0].ID   = oldDataT.Meshes[0].ID;
            }

            // Pass the ex data to meshes if they don't have them.
            // Game crashes without the ex data if it tries to make use of them.
            // SMH sega, can't even do a null check!!
            foreach (var mesh in Data.Meshes)
            {
                var oldMesh = oldDataT.Meshes.FirstOrDefault(x => x.Name.Equals(mesh.Name, StringComparison.OrdinalIgnoreCase));
                if (oldMesh != null)
                {
                    if (mesh.Skin != null && mesh.Skin.ExData == null)
                    {
                        mesh.Skin.ExData = oldMesh.Skin?.ExData;
                    }

                    mesh.Name = oldMesh.Name;
                    mesh.ID   = oldMesh.ID;
                }
            }

            // Pass the format/endianness
            Data.Format     = oldDataT.Format;
            Data.Endianness = oldDataT.Endianness;

            // Randomize texture IDs if we are modern
            if (BinaryFormatUtilities.IsModern(Data.Format) && Data.TextureSet != null)
            {
                var newIDs = new List <int>(Data.TextureSet.Textures.Count);

                var random = new Random();
                for (int i = 0; i < Data.TextureSet.Textures.Count; i++)
                {
                    int currentID;
                    do
                    {
                        currentID = random.Next(int.MinValue, int.MaxValue);
                    } while (newIDs.Contains(currentID));

                    newIDs.Add(currentID);
                }

                TextureUtilities.ReAssignTextureIDs(Data, newIDs);
            }

            Textures?.Replace(Data.TextureSet);

            base.OnReplace(oldData);
        }
Пример #6
0
        protected BinaryFileSection(SectionMode mode, T data = default) : base(mode, data)
        {
            if (mode != SectionMode.Write)
            {
                return;
            }

            Endianness   = data.Endianness;
            AddressSpace = BinaryFormatUtilities.GetAddressSpace(data.Format);
        }
        public override void Save(string filePath)
        {
            // Assume it's being exported for F2nd PS3
            if (BinaryFormatUtilities.IsClassic(Format) && filePath.EndsWith(".txi", StringComparison.OrdinalIgnoreCase))
            {
                Format     = BinaryFormat.F2nd;
                Endianness = Endianness.BigEndian;
            }

            // Or reverse
            else if (BinaryFormatUtilities.IsModern(Format) && filePath.EndsWith(".bin", StringComparison.OrdinalIgnoreCase))
            {
                Format     = BinaryFormat.DT;
                Endianness = Endianness.LittleEndian;
            }

            base.Save(filePath);
        }
Пример #8
0
        protected override void InitializeViewCore()
        {
            if (Parent != null && BinaryFormatUtilities.IsModern(Format))
            {
                var fileName = Path.ChangeExtension(Name, "txi");
                TextureDatabaseNode = ( TextureDatabaseNode )Parent.FindNode <TextureDatabase>(fileName, StringComparison.OrdinalIgnoreCase);

                if (TextureDatabaseNode != null)
                {
                    var textureDatabase = TextureDatabaseNode.Data;

                    // Pass the IDs and the names to the set
                    for (int i = 0; i < Math.Min(textureDatabase.Textures.Count, Data.Textures.Count); i++)
                    {
                        Data.Textures[i].ID   = textureDatabase.Textures[i].ID;
                        Data.Textures[i].Name = textureDatabase.Textures[i].Name;
                    }
                }
            }

            Add(Textures = new ListNode <Texture>(nameof(Data.Textures), Data.Textures));
        }
        public override void Save(string filePath)
        {
            // Assume it's being exported for F2nd PS3
            if (BinaryFormatUtilities.IsClassic(Format) &&
                filePath.EndsWith(".txd", StringComparison.OrdinalIgnoreCase))
            {
                Format     = BinaryFormat.F2nd;
                Endianness = Endianness.Big;
            }

            // Or vice versa
            else if (BinaryFormatUtilities.IsModern(Format) &&
                     filePath.EndsWith(".bin", StringComparison.OrdinalIgnoreCase))
            {
                Format     = BinaryFormat.DT;
                Endianness = Endianness.Little;
            }

            // Save a TXI if we are modern
            if (filePath.EndsWith(".txd", StringComparison.OrdinalIgnoreCase))
            {
                var textureDatabase = new TextureDatabase();

                foreach (var texture in Textures)
                {
                    textureDatabase.Textures.Add(new TextureInfo
                    {
                        Id   = texture.Id,
                        Name = texture.Name ?? Guid.NewGuid().ToString()
                    });
                }

                textureDatabase.Format     = Format;
                textureDatabase.Endianness = Endianness;
                textureDatabase.Save(Path.ChangeExtension(filePath, "txi"));
            }

            base.Save(filePath);
        }
Пример #10
0
 public static AddressSpace GetAddressSpace(this BinaryFormat format) =>
 BinaryFormatUtilities.GetAddressSpace(format);
Пример #11
0
 public static bool IsModern(this BinaryFormat format) =>
 BinaryFormatUtilities.IsModern(format);
Пример #12
0
 public static bool IsClassic(this BinaryFormat format) =>
 BinaryFormatUtilities.IsClassic(format);
Пример #13
0
        protected override void InitializeCore()
        {
            RegisterReplaceHandler <TextureSet>(BinaryFile.Load <TextureSet>);
            RegisterExportHandler <TextureSet>((path) =>
            {
                // Assume it's being exported for F2nd PS3
                if (BinaryFormatUtilities.IsClassic(Data.Format) && path.EndsWith(".txd", StringComparison.OrdinalIgnoreCase))
                {
                    Data.Format     = BinaryFormat.F2nd;
                    Data.Endianness = Endianness.BigEndian;
                }

                // Or reverse
                else if (BinaryFormatUtilities.IsModern(Data.Format) && path.EndsWith(".bin", StringComparison.OrdinalIgnoreCase))
                {
                    Data.Format     = BinaryFormat.DT;
                    Data.Endianness = Endianness.LittleEndian;
                }

                Data.Save(path);
            });
            RegisterImportHandler <Texture>((path) =>
            {
                var texture = TextureEncoder.Encode(path);
                var node    = DataNodeFactory.Create <Texture>(Path.GetFileNameWithoutExtension(path), texture);
                Textures.Add(node);
            });
            RegisterImportHandler <Bitmap>((path) =>
            {
                var texture = TextureEncoder.Encode(path);
                var node    = DataNodeFactory.Create <Texture>(Path.GetFileNameWithoutExtension(path), texture);
                Textures.Add(node);
            });
            RegisterCustomHandler("Export All", () =>
            {
                using (var saveFileDialog = new SaveFileDialog())
                {
                    saveFileDialog.AutoUpgradeEnabled = true;
                    saveFileDialog.CheckPathExists    = true;
                    saveFileDialog.Title    = "Select a folder to export textures to.";
                    saveFileDialog.FileName = "Enter into a directory and press Save";

                    if (saveFileDialog.ShowDialog() == DialogResult.OK)
                    {
                        var outputDirectory = Path.GetDirectoryName(saveFileDialog.FileName);
                        foreach (var texture in Data.Textures)
                        {
                            if (!TextureFormatUtilities.IsCompressed(texture.Format) || texture.IsYCbCr)
                            {
                                TextureDecoder.DecodeToPNG(texture, Path.Combine(outputDirectory, texture.Name + ".png"));
                            }
                            else
                            {
                                TextureDecoder.DecodeToDDS(texture, Path.Combine(outputDirectory, texture.Name + ".dds"));
                            }
                        }
                    }
                }
            }, Keys.Control | Keys.Shift | Keys.E);
            RegisterDataUpdateHandler(() =>
            {
                var data        = new TextureSet();
                data.Format     = Format;
                data.Endianness = Endianness;
                data.Textures.AddRange(Textures.Data);
                return(data);
            });
        }
        private static void Main(string[] args)
        {
            string sourceFileName      = null;
            string destinationFileName = null;

            foreach (string arg in args)
            {
                if (sourceFileName == null)
                {
                    sourceFileName = arg;
                }

                else if (destinationFileName == null)
                {
                    destinationFileName = arg;
                }
            }

            if (sourceFileName == null)
            {
                Console.WriteLine(Resources.HelpText);
                Console.ReadLine();
                return;
            }

            if (destinationFileName == null)
            {
                destinationFileName = sourceFileName;
            }

            var databaseInfo = GetDatabaseInfo(sourceFileName);

            if (databaseInfo != null)
            {
                if (sourceFileName.EndsWith("xml", StringComparison.OrdinalIgnoreCase))
                {
                    var serializer = new XmlSerializer(databaseInfo.Type);

                    IBinaryFile database;

                    using (var source = File.OpenText(sourceFileName))
                        database = ( IBinaryFile )serializer.Deserialize(source);

                    if (BinaryFormatUtilities.IsModern(database.Format) && !string.IsNullOrEmpty(databaseInfo.ModernFileExtension))
                    {
                        destinationFileName = Path.ChangeExtension(destinationFileName, null);
                    }

                    else
                    {
                        destinationFileName = Path.ChangeExtension(destinationFileName, "bin");
                    }

                    database.Save(destinationFileName);
                }
                else
                {
                    var database = ( IBinaryFile )Activator.CreateInstance(databaseInfo.Type);
                    database.Load(sourceFileName);

                    if (BinaryFormatUtilities.IsModern(database.Format))
                    {
                        destinationFileName = Path.ChangeExtension(destinationFileName, databaseInfo.ModernFileExtension) + ".xml";
                    }

                    else
                    {
                        destinationFileName = Path.ChangeExtension(destinationFileName, "xml");
                    }

                    var serializer = new XmlSerializer(databaseInfo.Type);

                    using (var destination = File.CreateText(destinationFileName))
                        serializer.Serialize(destination, database);
                }
            }
            else
            {
                throw new InvalidDataException("Database type could not be detected");
            }
        }