Skip to content

Instantly share code, notes, and snippets.

@dpogue
Last active August 20, 2020 15:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dpogue/197d7764ca98a9d7f819add5d1b9359b to your computer and use it in GitHub Desktop.
Save dpogue/197d7764ca98a9d7f819add5d1b9359b to your computer and use it in GitHub Desktop.
A (hopefully) VS 2010 & CWE-ou compatible version of SoundDecompress.cpp
/*==LICENSE==*
CyanWorlds.com Engine - MMOG client, server and tools
Copyright (C) 2011 Cyan Worlds, Inc.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
Additional permissions under GNU GPL version 3 section 7
If you modify this Program, or any covered work, by linking or
combining it with any of RAD Game Tools Bink SDK, Autodesk 3ds Max SDK,
NVIDIA PhysX SDK, Microsoft DirectX SDK, OpenSSL library, Independent
JPEG Group JPEG library, Microsoft Windows Media SDK, or Apple QuickTime SDK
(or a modified version of those libraries),
containing parts covered by the terms of the Bink SDK EULA, 3ds Max EULA,
PhysX SDK EULA, DirectX SDK EULA, OpenSSL and SSLeay licenses, IJG
JPEG Library README, Windows Media SDK EULA, or QuickTime SDK EULA, the
licensors of this Program grant you additional
permission to convey the resulting work. Corresponding Source for a
non-source form of such a combination shall include the source code for
the parts of OpenSSL and IJG JPEG Library used as well as that of the covered
work.
You can contact Cyan Worlds, Inc. by email legal@cyan.com
or by snail mail at:
Cyan Worlds, Inc.
14617 N Newport Hwy
Mead, WA 99021
*==LICENSE==*/
#include <utility>
#include "plResMgr/plResManager.h"
#include "plResMgr/plResMgrSettings.h"
#include "plResMgr/plRegistryHelpers.h"
#include "plResMgr/plRegistryNode.h"
#include "plAgeDescription/plAgeManifest.h"
#include "plAudioCore/plSoundBuffer.h"
#include "hsUtils.h"
#include "plFile/hsFiles.h"
#include "plFile/plFileUtils.h"
typedef std::set<std::pair<std::string, unsigned short>> SoundSet;
enum OutputStyle
{
kSilent,
kProgress,
kVerbose
};
//// plSoundBufferCollector //////////////////////////////////////////////////
// Page iterator that collects all the plSoundBuffers in all of our pages
class plSoundBufferCollector : public plRegistryPageIterator, public plKeyCollector
{
public:
plSoundBufferCollector(std::set<plKey>& keyArray)
: plKeyCollector(keyArray) {}
bool EatPage(plRegistryPageNode* page)
{
if (page->IsValid())
{
page->LoadKeys();
return page->IterateKeys(this, plSoundBuffer::Index());
}
else
{
fprintf(stderr, "INVALID PAGE: %s\n", page->GetPagePath());
return true;
}
}
};
void PrintHelp()
{
printf("Plasma Sound Decompressor\n");
printf("-------------------------\n");
printf("-s, --silent Run silently, no output\n");
printf("-v, --verbose Print each filename when decompressing\n");
printf("-f, --force Force decompressing existing files\n");
}
bool CollectSounds(plResManager* rm, SoundSet& sfxArray)
{
std::set<plKey> soundKeys;
plSoundBufferCollector soundCollector(soundKeys);
rm->IterateAllPages(&soundCollector);
for (auto it = soundKeys.begin(); it != soundKeys.end(); ++it)
{
plSoundBuffer* buffer = plSoundBuffer::ConvertNoRef((*it)->VerifyLoaded());
if (buffer)
{
// Ref it...
buffer->GetKey()->RefObject();
// Get the filename from it and add that file if necessary
const char* filename = buffer->GetFileName();
if (filename)
{
unsigned short flags = 0;
if (stricmp(plFileUtils::GetFileExt(filename), "wav") != 0)
{
if (buffer->HasFlag(plSoundBuffer::kOnlyLeftChannel) ||
buffer->HasFlag(plSoundBuffer::kOnlyRightChannel))
hsSetBits(flags, plManifestFile::kSndFlagCacheSplit);
else if (buffer->HasFlag(plSoundBuffer::kStreamCompressed))
hsSetBits(flags, plManifestFile::kSndFlagStreamCompressed);
else
hsSetBits(flags, plManifestFile::kSndFlagCacheStereo);
}
std::pair<std::string, unsigned short> pair = std::make_pair(filename, flags);
sfxArray.insert(pair);
}
// Unref the object so it goes away
buffer->GetKey()->UnRefObject();
}
}
soundKeys.clear();
plIndirectUnloadIterator iter;
rm->IterateAllPages(&iter);
return true;
}
void DecompressSounds(SoundSet& sounds, bool overwrite, OutputStyle verbosity)
{
int total = sounds.size();
int curr = 0;
if (verbosity == kVerbose)
{
printf("There are %d sounds\n\n", total);
}
for (auto it = sounds.begin(); it != sounds.end(); ++it)
{
curr++;
char path[256];
strcat(path, "sfx\\");
strcat(path, it->first.c_str());
if (verbosity == kVerbose)
{
printf("%s\n", path);
}
else if (verbosity == kProgress)
{
int percent = (100 * curr) / total;
int progress = int((float(curr) / total) * 75);
fprintf(stdout, "\r%3d%% ", percent);
for (int i = 0; i < progress; i++)
{
fprintf(stdout, "=");
}
fflush(stdout);
}
if (hsCheckBits(it->second, plManifestFile::kSndFlagCacheSplit))
{
plAudioFileReader::CacheFile(path, true, !overwrite);
}
else if (hsCheckBits(it->second, plManifestFile::kSndFlagCacheStereo))
{
plAudioFileReader::CacheFile(path, false, !overwrite);
}
}
if (verbosity == kProgress)
{
// Hack to ensure we always end with 100%
fprintf(stdout, "\r100%%\n");
}
}
int main(int argc, const char** argv)
{
bool overwrite = false;
OutputStyle verbosity = kProgress;
for (int arg = 1; arg < argc; arg++)
{
if (!strcmp(argv[arg], "-h") || !strcmp(argv[arg], "--help"))
{
PrintHelp();
return 0;
}
else if (!strcmp(argv[arg], "-s") || !strcmp(argv[arg], "--silent"))
{
verbosity = kSilent;
}
else if (!strcmp(argv[arg], "-v") || !strcmp(argv[arg], "--verbose"))
{
verbosity = kVerbose;
}
else if (!strcmp(argv[arg], "-f") || !strcmp(argv[arg], "--force"))
{
overwrite = true;
}
}
// Init our special resMgr
plResMgrSettings::Get().SetFilterNewerPageVersions(false);
plResMgrSettings::Get().SetFilterOlderPageVersions(false);
plResMgrSettings::Get().SetLoadPagesOnInit(true);
plResManager* rm = new plResManager();
rm->SetDataPath("dat");
hsgResMgr::Init(rm);
SoundSet sounds;
CollectSounds(rm, sounds);
DecompressSounds(sounds, overwrite, verbosity);
hsgResMgr::Shutdown();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment