Example #1
0
        static void Main(string[] args)
        {
            Console.WriteLine("IMG file location: ");
            string fileLocation = Console.ReadLine();

            if (File.Exists(@fileLocation))
            {
                Console.WriteLine("Reading the IMG file!");
                IMGArchive        imgArchive        = IMGFile.Open(@fileLocation, EIMGArchiveMode.Read);
                IMGArchiveEntry[] iMGArchiveEntries = imgArchive.Entries.OrderBy(x => x.Name).ToArray();
                imgArchive.Dispose();
                imgArchive.Entries = iMGArchiveEntries;

                for (int i = 0; i < imgArchive.Entries.Length; i++)
                {
                    IMGArchiveEntry entry = imgArchive.Entries[i];
                    Console.WriteLine("Entry file " + i + " name is: " + entry.Name);
                }

                Console.WriteLine("Total entries is: " + imgArchive.Entries.Length);
                Console.ReadLine();
            }
            else
            {
                Console.WriteLine("File doesn't exist");
                Console.ReadLine();
            }
        }
        private static void DoLoadIMG()
        {
            var t = DateTime.Now;

            foreach (string imgFileName in imgFileNames)
            {
                string fname = Path.Combine(Properties.Settings.Default.GTASAPath, imgFileName);
                using (var imgArchive = new IMGArchive(fname))
                {
                    Debug.WriteLine($"Loading '{fname}'...");
                    foreach (DirEntry entry in imgArchive.DirEntries)
                    {
                        if (Files.ContainsKey(entry.FileName))
                        {
                            if (imgOverwriteDuplicates)
                            {
                                Files.Remove(entry.FileName);
                            }
                            else
                            {
                                DirEntry otherEntry;
                                Files.TryGetValue(entry.FileName, out otherEntry);
                                Debug.WriteLine($"Error! Cannot add file '{entry.FileName}' from {entry.ArchiveName} because it was already added to the dictionary from {otherEntry.ArchiveName}.");
                                continue;
                            }
                        }
                        Files.Add(entry.FileName, entry);
                        //Debug.WriteLine($"* {entry.FileName}"); // printing slows down a lot
                    }
                }
            }
            Debug.WriteLine($"Total time elapsed: {DateTime.Now - t}");
            IMGLoadCompleted?.Invoke(typeof(MasterDictionary), new EventArgs());
        }
Example #3
0
        /// <summary>
        /// Parses a RenderWare TXD texture dictionary from an IMG archive, given the index of the file.
        /// </summary>
        /// <param name="imgArchive">The object representing the IMG archive containing the TXD file.</param>
        /// <param name="index">The index of the file inside the archive.</param>
        /// <returns>A new ExtendedSection object containing the TXD data read from the archive.</returns>
        /// <remarks>
        /// The IMGArchive must not be already disposed when calling this function.
        /// </remarks>
        /// <example>
        /// var fileIdx = 1244;
        /// using (var imgArchive = new IMGArchive("gta3.img"))
        /// {
        ///     RenderWareTextureDictionary textureDictionary = new RenderWareTextureDictionary(TxdParser.Parse(imgArchive, fileIdx));
        /// }
        /// </example>
        public ExtendedSection Parse(IMGArchive imgArchive, int index)
        {
            IMGArchiveFile?archiveFile = imgArchive.GetFileById(index);

            if (archiveFile.HasValue)
            {
                if (Path.GetExtension(archiveFile.Value.FileEntry.FileName) == ".txd")
                {
                    return(Parse(archiveFile.Value.FileByteBuffer));
                }
            }
            return(null);
        }
Example #4
0
        /// <summary>
        /// Parses a RenderWare DFF model from an IMG archive, given the name of the file.
        /// </summary>
        /// <param name="imgArchive">The object representing the IMG archive containing the DFF file.</param>
        /// <param name="fileName">The name of the file inside the archive.</param>
        /// <returns>A new ExtendedSection object containing the DFF data read from the archive.</returns>
        /// <remarks>
        /// The IMGArchive must not be already disposed when calling this function.
        /// </remarks>
        /// <example>
        /// var fileName = "shamal.dff";
        /// using (var imgArchive = new IMGArchive("gta3.img"))
        /// {
        ///     RenderWareModel model = new RenderWareModel(DffParser.Parse(imgArchive, fileIdx));
        /// }
        /// </example>
        public ExtendedSection Parse(IMGArchive imgArchive, string fileName)
        {
            IMGArchiveFile?archiveFile = imgArchive.GetFileByName(fileName);

            if (archiveFile.HasValue)
            {
                if (Path.GetExtension(archiveFile.Value.FileEntry.FileName) == ".dff")
                {
                    return(Parse(archiveFile.Value.FileByteBuffer));
                }
            }
            return(null);
        }
Example #5
0
 public void CommitToIMGFile()
 {
     InitArchives();
     if (File.Exists("test3.img"))
     {
         File.Delete("test3.img");
     }
     File.Copy("test1.img", "test3.img");
     using (IMGArchive archive = IMGFile.Open("./test3.img", EIMGArchiveMode.Update))
     {
         Assert.IsNotNull(archive);
         IMGArchiveEntry[] entries = archive.Entries;
         int entry_count           = entries.Length;
         Assert.IsTrue(entry_count > 0);
         IMGArchiveEntry entry      = entries[0];
         string          entry_name = entry.FullName;
         Console.WriteLine("Unpacking file \"" + entries[0].FullName + "\"");
         if (!(Directory.Exists("test")))
         {
             Directory.CreateDirectory("test");
         }
         long entry_size = 0;
         using (Stream entry_stream = entry.Open())
         {
             Assert.IsNotNull(entry_stream);
             entry_size = entry_stream.Length;
             Assert.AreEqual(entry_size, (long)(entry.Length));
             entry_stream.Seek(0L, SeekOrigin.End);
             for (int i = 0; i < 2048; i++)
             {
                 entry_stream.WriteByte(0);
             }
             entry_size += 2048;
         }
         entries = archive.Entries;
         Assert.AreEqual(entry_count, entries.Length);
         entry = archive.GetEntry(entry_name);
         Assert.IsNotNull(entry);
         using (Stream entry_stream = entry.Open())
         {
             Assert.AreEqual(entry_size, entry_stream.Length);
             Assert.AreEqual(entry_size, entry.Length);
             if (entry_size >= 2048)
             {
                 entry_size -= 2048;
                 entry_stream.SetLength(entry_size);
             }
         }
     }
 }
Example #6
0
 public void CreateReadIMGFiles()
 {
     InitArchives();
     using (IMGArchive archive = IMGFile.Open("./test1.img", EIMGArchiveMode.Read))
     {
         Assert.IsNotNull(archive);
         Assert.IsTrue(archive.Entries.Length > 0);
     }
     using (IMGArchive archive = IMGFile.Open("./test2.img", EIMGArchiveMode.Read))
     {
         Assert.IsNotNull(archive);
         Assert.IsTrue(archive.Entries.Length > 0);
     }
 }
Example #7
0
        /// <summary>
        /// Open script directory
        /// </summary>
        /// <param name="directory">Script directory</param>
        /// <param name="game">Game</param>
        /// <param name="filesMode">Script files mode</param>
        /// <returns>Script files if successful, otherwise "null"</returns>
        public static GTA3ScriptFiles OpenScriptDirectory(string directory, EGame game, EGTA3ScriptFilesMode filesMode)
        {
            GTA3ScriptFiles ret = null;

            if (Directory.Exists(directory))
            {
                string main_scm_path   = Path.Combine(directory, "main.scm");
                string script_img_path = Path.Combine(directory, "script.img");
                if (File.Exists(main_scm_path) && File.Exists(script_img_path))
                {
                    List <Stream> streams = new List <Stream>();
                    try
                    {
                        streams.Add(File.Open(main_scm_path, FileMode.Open, FileAccess.Read));
                        using (IMGArchive img_archive = IMGFile.OpenRead(script_img_path))
                        {
                            IMGArchiveEntry[] entries = img_archive.Entries;
                            foreach (IMGArchiveEntry entry in entries)
                            {
                                if (entry != null)
                                {
                                    Stream stream = entry.Open();
                                    if (stream != null)
                                    {
                                        streams.Add(stream);
                                    }
                                }
                            }
                        }
                        AGTA3Script[] scripts = new AGTA3Script[streams.Count];
                        for (int i = 0; i < scripts.Length; i++)
                        {
                            scripts[i] = new SCM(game, streams[i]);
                        }
                        ret = new GTA3ScriptFiles(game, scripts);
                    }
                    catch (Exception e)
                    {
                        Console.Error.WriteLine(e);
                        foreach (Stream stream in streams)
                        {
                            stream.Dispose();
                        }
                    }
                    streams.Clear();
                }
            }
            return(ret);
        }
Example #8
0
        public void ExtractToTestDirectory()
        {
            int entry_count = 0;

            InitArchives();
            using (IMGArchive archive = IMGFile.Open("./test1.img", EIMGArchiveMode.Read))
            {
                entry_count = archive.Entries.Length;
            }
            IMGFile.ExtractToDirectory("test1.img", "test1");
            Assert.IsTrue(entry_count <= Directory.GetFiles("test1", "*", SearchOption.AllDirectories).Length);
            using (IMGArchive archive = IMGFile.Open("./test2.img", EIMGArchiveMode.Read))
            {
                entry_count = archive.Entries.Length;
            }
            IMGFile.ExtractToDirectory("test2.img", "test2");
            Assert.IsTrue(entry_count <= Directory.GetFiles("test2", "*", SearchOption.AllDirectories).Length);
        }
Example #9
0
        /// <summary>
        /// Parses all RenderWare TXD textures from an IMG archive.
        /// </summary>
        /// <param name="imgArchive">The object representing the IMG archive containing the TXD files.</param>
        /// <returns>A List of all the RenderWareTextures read from the IMG archive.</returns>
        /// <remarks>
        /// The IMGArchive must not be already disposed when calling this function. Only the files having
        /// an extension of '.txd' will be considered TXD textures, and thus parsed and added to the returned List.
        /// </remarks>
        /// <example>
        /// using (var imgArchive = new IMGArchive("gta3.img"))
        /// {
        ///     List&lt;ExtendedSection&gt; textureData = TxdParser.ParseAll(imgArchive);
        /// }
        /// </example>
        public List <ExtendedSection> ParseAll(IMGArchive imgArchive)
        {
            var list = new List <ExtendedSection>();

            for (int i = 0; i < imgArchive.DirEntries.Count; i++)
            {
                IMGArchiveFile?archiveFile = imgArchive.GetFileById(i, false);
                if (archiveFile.HasValue)
                {
                    if (Path.GetExtension(archiveFile.Value.FileEntry.FileName) == ".txd")
                    {
                        archiveFile = imgArchive.GetFileById(i, true);
                        Debug.WriteLine(archiveFile.Value.FileEntry.FileName);
                        list.Add(Parse(archiveFile.Value.FileByteBuffer));
                    }
                }
            }
            return(list);
        }