MagosX.com
July 24, 2014, 08:39:22 AM *
Welcome, Guest. Please login or register.

Login with username, password and session length
News: The only true wisdom is in knowing you know nothing!
 
  Home   Forum   Files Programs Help Search Login Register  
Pages: [1] 2 3
  Print  
Author Topic: MpqLib  (Read 39435 times)
Magos
Godlike
Administrator
Jr. Member
*****
Offline Offline

Posts: 98



View Profile WWW
« on: February 11, 2009, 04:19:24 PM »

MpqLib v1.00

MdxLib is a .NET 3.5 Class Library to manipulate MPQ archives. You can open or create archives, then open, read, write, delete, import, export files from them. It's a wrapper over StormLib to make usage easier. It's mainly aimed for programmers to be used in tools and similar. It was written in C++.NET, but can be used by any of the .NET languages (and perhaps by other languages if you have some skillz).

  • An object oriented approach to manipulating MPQ archives.
  • Open or create MPQ archives.
  • Open, read, write, delete, import, export files in MPQ archives.

Download it here:
MpqLib.zip

Source code (released under GPL):
MpqLibSource.zip
« Last Edit: August 24, 2009, 09:35:07 AM by Magos » Logged

Magos
Godlike
Administrator
Jr. Member
*****
Offline Offline

Posts: 98



View Profile WWW
« Reply #1 on: February 11, 2009, 04:20:39 PM »

Example 1 - Export a file

This first simple example shows the simplicity of using the library. First you need an archive object pointing to the main WarCraft 3 MPQ (War3.mpq). After the archive has been opened (an exception is thrown if an error occurs, like if the archive cannot be found) you can call the method ExportFile to export a file from the archive.

I'm using the "using"-directive on the archive which means it will be automatically closed and disposed when done (even if an exception is thrown). If you prefer you can call Close or Dispose manually.

Code:
//
// Opens the main WarCraft 3 MPQ archive.
//
using(var Archive = new MpqLib.Mpq.CArchive("C:\\Program Files\\Warcraft III\\War3.mpq"))
{
//
// Exports a file from the archive. The first path refers to the path inside the archive.
// The second path refers to where the file should be exported.
//
Archive.ExportFile("Sound\\Music\\mp3Music\\Comradeship.mp3", "C:\\Temp\\Comradeship.mp3");
}

Example 2 - Import a file

This next example imports a file (the file we just exported). Note the new second argument to the archive constructor, it creates a new archive if none exists (if false or left out it will throw an exception if no archive exists).

The import syntax is basically the same as the export syntax, only you have an extra argument telling which compression to use. Not all compressions may work in all cases. Implode is a good general purpose compression though, so we use it.

Finally we open a stream to the file we just imported. We don't read any data, just retrieve its filesize and display it to the user.

Code:
//
// Creates a new MPQ archive. The second parameter decides if a new
// archive should be created (if none exists). If you always want
// to create a new, use System.IO.File.Delete first.
//
using(var Archive = new MpqLib.Mpq.CArchive("C:\\Temp\\Temp.mpq", true))
{
//
// Imports a file into the archive, compressing it using the Implode algorithm
// (Mp3 files are already compressed so the size won't be reduced much).
//
Archive.ImportFile("MyOwnFolder\\Comradeship.mp3", "C:\\Temp\\Comradeship.mp3", MpqLib.Mpq.ECompression.Implode);

//
// Streams the newly imported file, displaying its real size and
// the size it has in the archive (usually differs if compressed).
//
using(var File = new MpqLib.Mpq.CFileStream(Archive, "MyOwnFolder\\Comradeship.mp3"))
{
System.Windows.Forms.MessageBox.Show("Size: " + File.Length + System.Environment.NewLine + "Compressed Size: " + File.CompressedLength);
}
}

Example 3 - Combining MdxLib and MpqLib

In this example we'll open an MDX model with MpqLib then retrieve some information from it using MdxLib. Since the MPQ file stream implements the standard .NET stream functionality it can be used not only in MdxLib but in any function that accepts standard streams.

Code:
//
// Opens an MPQ archive.
//
using(var Archive = new MpqLib.Mpq.CArchive("C:\\Program Files\\Warcraft III\\War3.mpq"))
{
//
// Opens an MDX model.
//
using(var File = new MpqLib.Mpq.CFileStream(Archive, "Units\\Human\\Footman\\Footman.mdx"))
{
//
// Creates a new model and an MDX format object (for loading MDX models).
//
var Model = new MdxLib.Model.CModel();
var MdxFormat = new MdxLib.ModelFormats.CMdx();

//
// Loads the MDX model.
//
MdxFormat.Load("Footman.mdx", File, Model);

//
// Displays the model name and how many textures it has.
//
System.Windows.Forms.MessageBox.Show(Model.Name +  " has " + Model.Textures.Count + " textures!");
}
}
« Last Edit: February 12, 2009, 11:56:13 AM by Magos » Logged

Magos
Godlike
Administrator
Jr. Member
*****
Offline Offline

Posts: 98



View Profile WWW
« Reply #2 on: August 24, 2009, 09:35:34 AM »

Source code released (under GPL). See the first post.
Logged

pha0001
Newbie
*
Offline Offline

Posts: 18

Andy Phan - VB.Net Programmer


View Profile
« Reply #3 on: December 25, 2009, 04:20:29 AM »

Hello Magos,

I was wondering if MpqLib can possibly find files without the internal or external listfile (that is, finding files on it's own). I was wondering if it could do this or if possible be implemented, or otherwise advice. Thanks.
Logged
Magos
Godlike
Administrator
Jr. Member
*****
Offline Offline

Posts: 98



View Profile WWW
« Reply #4 on: December 26, 2009, 05:34:04 AM »

Nope, not possible. No filename information is stored inside the mpq, only hashed values. You have to know the name you're looking for, thus the need for a filelist.
Logged

pha0001
Newbie
*
Offline Offline

Posts: 18

Andy Phan - VB.Net Programmer


View Profile
« Reply #5 on: December 26, 2009, 05:41:59 AM »

Nope, not possible. No filename information is stored inside the mpq, only hashed values. You have to know the name you're looking for, thus the need for a filelist.

Hmm... Thanks for replying, but I was wondering if it is possible to find the filename in the hashed values with the use of Storm.dll or some sort like that. Thanks again.
Logged
Dege
Newbie
*
Offline Offline

Posts: 1


View Profile
« Reply #6 on: December 26, 2009, 09:57:16 AM »

i have another question: how can i delete the (attributes) file in a warcraft 3 map?

i've tried but don't work...
« Last Edit: December 27, 2009, 09:18:36 AM by Dege » Logged
Magos
Godlike
Administrator
Jr. Member
*****
Offline Offline

Posts: 98



View Profile WWW
« Reply #7 on: December 27, 2009, 03:35:27 PM »

If

Code:
RemoveFile("(attributes)");

fails then there's probably a limitation in stormlib. Not much to do.
Logged

pha0001
Newbie
*
Offline Offline

Posts: 18

Andy Phan - VB.Net Programmer


View Profile
« Reply #8 on: December 27, 2009, 08:43:21 PM »

If

Code:
RemoveFile("(attributes)");

fails then there's probably a limitation in stormlib. Not much to do.

I guess, but with ShadowFlare's MPQ assembly it works quite well deleting the listfile and the attributes file too, from what I remember last time.
Logged
sikele2236
Newbie
*
Offline Offline

Posts: 3


View Profile
« Reply #9 on: August 15, 2010, 09:03:59 PM »

Code:


 ........         
  if (mpq.FileExists("scripts\\war3map.j"))
                jpath = "scripts\\war3map.j";

            mpq.ExportFile(jpath, "war3map.j"); // succeed
            mpq.RemoveFile(jpath);
            byte[] bytej = File.ReadAllBytes("war3map.j");
            //do something
            mpq.ImportFile(jpath, utf8.GetBytes(jstring));
            mpq.ExportFile(jpath, "war3map.j"); // succeed
            mpq.RemoveFile(jpath);
            mpq.ImportFile(jpath, utf8.GetBytes(jstring), MpqLib.Mpq.ECompression.ZLib);
            mpq.ExportFile(jpath, "war3map.j"); // unable to export.......

« Last Edit: August 16, 2010, 12:31:49 AM by sikele2236 » Logged
vercas
Newbie
*
Offline Offline

Posts: 15


The last living Asgard...


View Profile
« Reply #10 on: August 15, 2010, 10:37:48 PM »

Magos, I have a retarded exception comming for no reason from ..ctor! (with double dots!)
Please pm me or reply my PM!
Logged

Lol.
Magos
Godlike
Administrator
Jr. Member
*****
Offline Offline

Posts: 98



View Profile WWW
« Reply #11 on: August 16, 2010, 09:52:27 AM »

Can you post the call that fails and the exception that you get?
Logged

qcz
Newbie
*
Offline Offline

Posts: 4


View Profile
« Reply #12 on: August 16, 2010, 02:00:02 PM »

If I import files to archives using MpqLib with Zlib/BZip2 compression, later I won't be able to read that file from the archive.
(Tried to export after import with MpqLib and with Mpq Editor)

Code:
mpqfile.ImportFile(backupMpqFileName, realFileName, ECompression.ZLib);
mpqfile.ExportFile(backupMpqFileName, outFileName);  // won't work
Logged
sikele2236
Newbie
*
Offline Offline

Posts: 3


View Profile
« Reply #13 on: August 16, 2010, 07:03:52 PM »

Code:
mpq.ImportFile(jpath, utf8.GetBytes(jstring), MpqLib.Mpq.ECompression.ZLib);
mpq.ExportFile(jpath, "war3map.j"); // unable to export.......

未处理 System.IO.IOException
  Message="Unable to export \"scripts\\war3map.j\" as \"war3map.j\"!"
  Source="MpqLib"
  StackTrace:
       在 MpqLib.Mpq.CArchive.ExportFile(String FileName, String RealFileName)
Logged
Magos
Godlike
Administrator
Jr. Member
*****
Offline Offline

Posts: 98



View Profile WWW
« Reply #14 on: August 17, 2010, 09:13:26 AM »

Try other compression methods than ZLib (or ignore that parameter and use the default value).
Try specifying the full path (like C:\Folder\File.ext) in the filename.
Logged

Pages: [1] 2 3
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.12 | SMF © 2006-2009, Simple Machines LLC
TinyPortal v0.9.8 © Bloc
Valid XHTML 1.0! Valid CSS!
Page created in 0.097 seconds with 20 queries.