Просмотр исходного кода

Minor enhancement on the PackageTool to have output option.

Yao Wei Tjong 姚伟忠 9 лет назад
Родитель
Сommit
c28634c206

+ 11 - 0
Docs/Reference.dox

@@ -3066,6 +3066,17 @@ Basepath is an optional prefix that will be added to the file entries.
 
 \endverbatim
 
+Alternate output usage:
+
+\verbatim
+PackageTool <output option> <package name>
+
+Output option:
+-i      Output package file information
+-l      Output file names (including its path) contained in the package
+
+\endverbatim
+
 When PackageTool runs, it will go inside the source directory, then look for subdirectories and any files. Paths inside the package will by default be relative to the source directory, but if an extra path prefix is desired, it can be specified by the optional basepath argument.
 
 For example, this would convert all the resource files inside the Urho3D Data directory into a package called Data.pak (execute the command from the bin directory)

+ 58 - 23
Source/Tools/PackageTool/PackageTool.cpp

@@ -25,6 +25,7 @@
 #include <Urho3D/Core/ProcessUtils.h>
 #include <Urho3D/IO/File.h>
 #include <Urho3D/IO/FileSystem.h>
+#include <Urho3D/IO/PackageFile.h>
 
 #ifdef WIN32
 #include <windows.h>
@@ -92,11 +93,16 @@ void Run(const Vector<String>& arguments)
             "-c      Enable package file LZ4 compression\n"
             "-q      Enable quiet mode\n"
             "\n"
-            "Basepath is an optional prefix that will be added to the file entries.\n"
+            "Basepath is an optional prefix that will be added to the file entries.\n\n"
+            "Alternative output usage: PackageTool <output option> <package name>\n"
+            "Output option:\n"
+            "-i      Output package file information\n"
+            "-l      Output file names (including its path) contained in the package\n"
         );
 
     const String& dirName = arguments[0];
     const String& packageName = arguments[1];
+    bool isOutputMode = arguments[0].Length() == 2 && arguments[0][0] == '-';
     if (arguments.Size() > 2)
     {
         for (unsigned i = 2; i < arguments.Size(); ++i)
@@ -115,39 +121,66 @@ void Run(const Vector<String>& arguments)
                     case 'q':
                         quiet_ = true;
                         break;
+                    default: break;
                     }
                 }
             }
         }
     }
 
-    if (!quiet_)
-        PrintLine("Scanning directory " + dirName + " for files");
+    if (!isOutputMode)
+    {
+        if (!quiet_)
+            PrintLine("Scanning directory " + dirName + " for files");
 
-   // Get the file list recursively
-    Vector<String> fileNames;
-    fileSystem_->ScanDir(fileNames, dirName, "*.*", SCAN_FILES, true);
-    if (!fileNames.Size())
-        ErrorExit("No files found");
+        // Get the file list recursively
+        Vector<String> fileNames;
+        fileSystem_->ScanDir(fileNames, dirName, "*.*", SCAN_FILES, true);
+        if (!fileNames.Size())
+            ErrorExit("No files found");
 
-    // Check for extensions to ignore
-    for (unsigned i = fileNames.Size() - 1; i < fileNames.Size(); --i)
-    {
-        String extension = GetExtension(fileNames[i]);
-        for (unsigned j = 0; ignoreExtensions_[j].Length(); ++j)
+        // Check for extensions to ignore
+        for (unsigned i = fileNames.Size() - 1; i < fileNames.Size(); --i)
         {
-            if (extension == ignoreExtensions_[j])
+            String extension = GetExtension(fileNames[i]);
+            for (unsigned j = 0; ignoreExtensions_[j].Length(); ++j)
             {
-                fileNames.Erase(fileNames.Begin() + i);
-                break;
+                if (extension == ignoreExtensions_[j])
+                {
+                    fileNames.Erase(fileNames.Begin() + i);
+                    break;
+                }
             }
         }
-    }
 
-    for (unsigned i = 0; i < fileNames.Size(); ++i)
-        ProcessFile(fileNames[i], dirName);
+        for (unsigned i = 0; i < fileNames.Size(); ++i)
+            ProcessFile(fileNames[i], dirName);
 
-    WritePackageFile(packageName, dirName);
+        WritePackageFile(packageName, dirName);
+    }
+    else
+    {
+        SharedPtr<PackageFile> packageFile(new PackageFile(context_, packageName));
+        switch (arguments[0][1])
+        {
+        case 'i':
+            PrintLine("Number of files: " + String(packageFile->GetNumFiles()));
+            PrintLine("File data size: " + String(packageFile->GetTotalDataSize()));
+            PrintLine("Package size: " + String(packageFile->GetTotalSize()));
+            PrintLine("Checksum: " + String(packageFile->GetChecksum()));
+            PrintLine("Compressed: " + String(packageFile->IsCompressed() ? "yes" : "no"));
+            break;
+        case 'l':
+            {
+                const HashMap<String, PackageEntry>& entries = packageFile->GetEntries();
+                for (HashMap<String, PackageEntry>::ConstIterator i = entries.Begin(); i != entries.End(); ++i)
+                    PrintLine(i->first_);
+            }
+            break;
+        default:
+            ErrorExit("Unrecognized output option");
+        }
+    }
 }
 
 void ProcessFile(const String& fileName, const String& rootDir)
@@ -268,9 +301,11 @@ void WritePackageFile(const String& fileName, const String& rootDir)
 
     if (!quiet_)
     {
-        PrintLine("Number of files " + String(entries_.Size()));
-        PrintLine("File data size " + String(totalDataSize));
-        PrintLine("Package size " + String(dest.GetSize()));
+        PrintLine("Number of files: " + String(entries_.Size()));
+        PrintLine("File data size: " + String(totalDataSize));
+        PrintLine("Package size: " + String(dest.GetSize()));
+        PrintLine("Checksum: " + String(checksum_));
+        PrintLine("Compressed: " + String(compress_ ? "yes" : "no"));
     }
 }
 

+ 1 - 0
Source/Urho3D/AngelScript/IOAPI.cpp

@@ -378,6 +378,7 @@ static void RegisterPackageFile(asIScriptEngine* engine)
     engine->RegisterObjectMethod("PackageFile", "const String& get_name() const", asMETHOD(PackageFile, GetName), asCALL_THISCALL);
     engine->RegisterObjectMethod("PackageFile", "uint get_numFiles() const", asMETHOD(PackageFile, GetNumFiles), asCALL_THISCALL);
     engine->RegisterObjectMethod("PackageFile", "uint get_totalSize() const", asMETHOD(PackageFile, GetTotalSize), asCALL_THISCALL);
+    engine->RegisterObjectMethod("PackageFile", "uint get_totalDataSize() const", asMETHOD(PackageFile, GetTotalDataSize), asCALL_THISCALL);
     engine->RegisterObjectMethod("PackageFile", "uint get_checksum() const", asMETHOD(PackageFile, GetChecksum), asCALL_THISCALL);
     engine->RegisterObjectMethod("PackageFile", "bool compressed() const", asMETHOD(PackageFile, IsCompressed), asCALL_THISCALL);
     engine->RegisterObjectMethod("PackageFile", "Array<String>@ GetEntryNames() const", asFUNCTION(PackageFileGetEntryNames), asCALL_CDECL_OBJLAST);

+ 3 - 1
Source/Urho3D/IO/PackageFile.cpp

@@ -32,6 +32,7 @@ namespace Urho3D
 PackageFile::PackageFile(Context* context) :
     Object(context),
     totalSize_(0),
+    totalDataSize_(0),
     checksum_(0),
     compressed_(false)
 {
@@ -40,6 +41,7 @@ PackageFile::PackageFile(Context* context) :
 PackageFile::PackageFile(Context* context, const String& fileName, unsigned startOffset) :
     Object(context),
     totalSize_(0),
+    totalDataSize_(0),
     checksum_(0),
     compressed_(false)
 {
@@ -104,7 +106,7 @@ bool PackageFile::Open(const String& fileName, unsigned startOffset)
         String entryName = file->ReadString();
         PackageEntry newEntry;
         newEntry.offset_ = file->ReadUInt() + startOffset;
-        newEntry.size_ = file->ReadUInt();
+        totalDataSize_ += (newEntry.size_ = file->ReadUInt());
         newEntry.checksum_ = file->ReadUInt();
         if (!compressed_ && newEntry.offset_ + newEntry.size_ > totalSize_)
         {

+ 5 - 0
Source/Urho3D/IO/PackageFile.h

@@ -73,6 +73,9 @@ public:
     /// Return total size of the package file.
     unsigned GetTotalSize() const { return totalSize_; }
 
+    /// Return total data size from all the file entries in the package file.
+    unsigned GetTotalDataSize() const { return totalDataSize_; }
+
     /// Return checksum of the package file contents.
     unsigned GetChecksum() const { return checksum_; }
 
@@ -91,6 +94,8 @@ private:
     StringHash nameHash_;
     /// Package file total size.
     unsigned totalSize_;
+    /// Total data size in the package using each entry's actual size if it is a compressed package file.
+    unsigned totalDataSize_;
     /// Package file checksum.
     unsigned checksum_;
     /// Compressed flag.

+ 5 - 3
Source/Urho3D/LuaScript/pkgs/IO/PackageFile.pkg

@@ -12,16 +12,17 @@ class PackageFile : public Object
     PackageFile();
     PackageFile(const String fileName, unsigned startOffset = 0);
     ~PackageFile();
-    
+
     bool Open(const String fileName, unsigned startOffset = 0);
     bool Exists(const String fileName) const;
     const PackageEntry* GetEntry(const String fileName) const;
     const HashMap<String, PackageEntry>& GetEntries() const;
-    
+
     const String GetName() const;
     StringHash GetNameHash() const;
     unsigned GetNumFiles() const;
     unsigned GetTotalSize() const;
+    unsigned GetTotalDataSize() const;
     unsigned GetChecksum() const;
     bool IsCompressed() const;
 
@@ -29,6 +30,7 @@ class PackageFile : public Object
     tolua_readonly tolua_property__get_set StringHash nameHash;
     tolua_readonly tolua_property__get_set unsigned numFiles;
     tolua_readonly tolua_property__get_set unsigned totalSize;
+    tolua_readonly tolua_property__get_set unsigned totalDataSize;
     tolua_readonly tolua_property__get_set unsigned checksum;
     tolua_readonly tolua_property__is_set bool compressed;
 };
@@ -98,4 +100,4 @@ static int tolua_IOLuaAPI_PackageFile_new01_local(lua_State* tolua_S)
 tolua_lerror:
  return tolua_IOLuaAPI_PackageFile_new00_local(tolua_S);
 }
-$}
+$}