2017-06-12 19:00:13 +00:00
|
|
|
// Adapted from TWMapImagesRecovery by Tardo: https://github.com/Tardo/TWMapImagesRecovery
|
2022-04-22 23:04:48 +00:00
|
|
|
#include <base/logger.h>
|
2017-06-12 19:00:13 +00:00
|
|
|
#include <base/system.h>
|
|
|
|
#include <engine/shared/datafile.h>
|
|
|
|
#include <engine/storage.h>
|
2020-09-26 19:41:58 +00:00
|
|
|
#include <game/mapitems.h>
|
2017-06-12 19:00:13 +00:00
|
|
|
|
2018-02-12 22:14:14 +00:00
|
|
|
#include <pnglite.h>
|
|
|
|
|
2017-06-12 19:00:13 +00:00
|
|
|
bool Process(IStorage *pStorage, const char *pMapName, const char *pPathSave)
|
|
|
|
{
|
|
|
|
CDataFileReader Map;
|
|
|
|
|
2017-07-12 18:15:00 +00:00
|
|
|
if(!Map.Open(pStorage, pMapName, IStorage::TYPE_ABSOLUTE))
|
2017-06-12 19:00:13 +00:00
|
|
|
{
|
|
|
|
dbg_msg("map_extract", "error opening map '%s'", pMapName);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// check version
|
|
|
|
CMapItemVersion *pVersion = (CMapItemVersion *)Map.FindItem(MAPITEMTYPE_VERSION, 0);
|
|
|
|
if(pVersion && pVersion->m_Version != 1)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
dbg_msg("map_extract", "Make sure you have the permission to use these images and sounds in your own maps");
|
|
|
|
|
|
|
|
CMapItemInfo *pInfo = (CMapItemInfo *)Map.FindItem(MAPITEMTYPE_INFO, 0);
|
|
|
|
|
2020-06-20 22:57:11 +00:00
|
|
|
if(pInfo)
|
|
|
|
{
|
|
|
|
dbg_msg("map_extract", "author: %s", (char *)Map.GetData(pInfo->m_Author));
|
|
|
|
dbg_msg("map_extract", "version: %s", (char *)Map.GetData(pInfo->m_MapVersion));
|
|
|
|
dbg_msg("map_extract", "credits: %s", (char *)Map.GetData(pInfo->m_Credits));
|
|
|
|
dbg_msg("map_extract", "license: %s", (char *)Map.GetData(pInfo->m_License));
|
|
|
|
}
|
2017-06-12 19:00:13 +00:00
|
|
|
|
|
|
|
int Start, Num;
|
|
|
|
|
|
|
|
// load images
|
|
|
|
Map.GetType(MAPITEMTYPE_IMAGE, &Start, &Num);
|
|
|
|
|
|
|
|
for(int i = 0; i < Num; i++)
|
|
|
|
{
|
2020-09-26 19:41:58 +00:00
|
|
|
CMapItemImage *pItem = (CMapItemImage *)Map.GetItem(Start + i, 0, 0);
|
2017-06-12 19:00:13 +00:00
|
|
|
char *pName = (char *)Map.GetData(pItem->m_ImageName);
|
|
|
|
|
|
|
|
if(pItem->m_External)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
char aBuf[512];
|
|
|
|
str_format(aBuf, sizeof(aBuf), "%s/%s.png", pPathSave, pName);
|
|
|
|
dbg_msg("map_extract", "writing image: %s (%dx%d)", aBuf, pItem->m_Width, pItem->m_Height);
|
|
|
|
|
|
|
|
// copy image data
|
2021-11-13 20:07:03 +00:00
|
|
|
IOHANDLE File = io_open(aBuf, IOFLAG_WRITE);
|
2021-11-05 16:21:41 +00:00
|
|
|
if(!File)
|
|
|
|
{
|
2021-11-13 20:07:03 +00:00
|
|
|
dbg_msg("map_extract", "failed to open file. filename='%s'", aBuf);
|
2021-11-05 16:21:41 +00:00
|
|
|
continue;
|
|
|
|
}
|
2017-06-12 19:00:13 +00:00
|
|
|
png_t Png;
|
2021-11-05 16:21:41 +00:00
|
|
|
int Error = png_open_write(&Png, 0, File);
|
|
|
|
if(Error != PNG_NO_ERROR)
|
|
|
|
{
|
2021-11-13 20:07:03 +00:00
|
|
|
dbg_msg("map_extract", "failed to write image file. filename='%s', pnglite: %s", aBuf, png_error_string(Error));
|
2021-11-05 16:21:41 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
png_set_data(&Png, pItem->m_Width, pItem->m_Height, 8, PNG_TRUECOLOR_ALPHA, (unsigned char *)Map.GetData(pItem->m_ImageData));
|
|
|
|
}
|
|
|
|
io_close(File);
|
2017-06-12 19:00:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// load sounds
|
|
|
|
Map.GetType(MAPITEMTYPE_SOUND, &Start, &Num);
|
|
|
|
|
|
|
|
for(int i = 0; i < Num; i++)
|
|
|
|
{
|
2020-09-26 19:41:58 +00:00
|
|
|
CMapItemSound *pItem = (CMapItemSound *)Map.GetItem(Start + i, 0, 0);
|
2017-06-12 19:00:13 +00:00
|
|
|
char *pName = (char *)Map.GetData(pItem->m_SoundName);
|
|
|
|
|
|
|
|
if(pItem->m_External)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
char aBuf[512];
|
|
|
|
str_format(aBuf, sizeof(aBuf), "%s/%s.opus", pPathSave, pName);
|
|
|
|
dbg_msg("map_extract", "writing sound: %s (%d B)", aBuf, pItem->m_SoundDataSize);
|
|
|
|
|
|
|
|
IOHANDLE Opus = io_open(aBuf, IOFLAG_WRITE);
|
2020-09-26 19:41:58 +00:00
|
|
|
io_write(Opus, (unsigned char *)Map.GetData(pItem->m_SoundData), pItem->m_SoundDataSize);
|
2017-06-12 19:00:13 +00:00
|
|
|
io_close(Opus);
|
|
|
|
}
|
|
|
|
|
|
|
|
return Map.Close();
|
|
|
|
}
|
|
|
|
|
2021-11-21 17:16:14 +00:00
|
|
|
int main(int argc, const char *argv[])
|
2017-06-12 19:00:13 +00:00
|
|
|
{
|
2021-11-21 17:16:14 +00:00
|
|
|
cmdline_fix(&argc, &argv);
|
2022-04-22 23:04:48 +00:00
|
|
|
log_set_global_logger_default();
|
2017-06-12 19:00:13 +00:00
|
|
|
|
|
|
|
char aMap[512];
|
|
|
|
char aDir[512];
|
|
|
|
|
|
|
|
IStorage *pStorage = CreateLocalStorage();
|
|
|
|
|
2020-09-26 19:41:58 +00:00
|
|
|
if(argc == 2)
|
2017-06-12 19:00:13 +00:00
|
|
|
{
|
|
|
|
str_copy(aMap, argv[1], sizeof(aMap));
|
|
|
|
str_copy(aDir, ".", sizeof(aMap));
|
|
|
|
}
|
2020-09-26 19:41:58 +00:00
|
|
|
else if(argc == 3)
|
2017-06-12 19:00:13 +00:00
|
|
|
{
|
|
|
|
str_copy(aMap, argv[1], sizeof(aMap));
|
|
|
|
str_copy(aDir, argv[2], sizeof(aDir));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-06-12 19:18:21 +00:00
|
|
|
dbg_msg("usage", "%s map [directory]", argv[0]);
|
2017-06-12 19:00:13 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2020-09-26 19:41:58 +00:00
|
|
|
if(!fs_is_dir(aDir))
|
2017-06-12 19:00:13 +00:00
|
|
|
{
|
2017-07-26 01:58:00 +00:00
|
|
|
dbg_msg("usage", "directory '%s' does not exist", aDir);
|
2017-06-12 19:00:13 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
png_init(0, 0);
|
|
|
|
|
2021-11-21 17:16:14 +00:00
|
|
|
int Result = Process(pStorage, aMap, aDir) ? 0 : 1;
|
|
|
|
cmdline_free(argc, argv);
|
|
|
|
return Result;
|
2017-06-12 19:00:13 +00:00
|
|
|
}
|