2017-08-29 22:21:41 +00:00
|
|
|
/* (c) DDNet developers. See licence.txt in the root of the distribution for more information. */
|
|
|
|
/* If you are missing that file, acquire a complete release at teeworlds.com. */
|
2017-08-28 16:06:19 +00:00
|
|
|
|
2022-04-22 23:04:48 +00:00
|
|
|
#include <base/logger.h>
|
2020-09-26 19:41:58 +00:00
|
|
|
#include <base/system.h>
|
2022-06-21 13:26:23 +00:00
|
|
|
#include <engine/gfx/image_loader.h>
|
2020-09-26 19:41:58 +00:00
|
|
|
#include <engine/graphics.h>
|
2017-08-28 16:06:19 +00:00
|
|
|
#include <engine/shared/datafile.h>
|
|
|
|
#include <engine/storage.h>
|
|
|
|
#include <game/mapitems.h>
|
|
|
|
/*
|
2017-08-29 22:34:14 +00:00
|
|
|
Usage: map_replace_image <source map filepath> <dest map filepath> <current image name> <new image filepath>
|
|
|
|
Notes: map filepath must be relative to user default teeworlds folder
|
|
|
|
new image filepath must be absolute or relative to the current position
|
2017-08-28 16:06:19 +00:00
|
|
|
*/
|
|
|
|
|
2017-08-29 22:34:14 +00:00
|
|
|
CDataFileReader g_DataReader;
|
2017-08-28 16:06:19 +00:00
|
|
|
|
|
|
|
// global new image data (set by ReplaceImageItem)
|
2024-03-05 14:44:09 +00:00
|
|
|
int g_NewNameId = -1;
|
2017-08-29 22:21:41 +00:00
|
|
|
char g_aNewName[128];
|
2024-03-05 14:44:09 +00:00
|
|
|
int g_NewDataId = -1;
|
2017-08-29 22:21:41 +00:00
|
|
|
int g_NewDataSize = 0;
|
2022-05-31 21:07:40 +00:00
|
|
|
void *g_pNewData = nullptr;
|
2017-08-28 16:06:19 +00:00
|
|
|
|
2024-04-12 15:22:52 +00:00
|
|
|
bool LoadPng(CImageInfo *pImg, const char *pFilename)
|
2017-08-28 16:06:19 +00:00
|
|
|
{
|
2021-11-05 16:21:41 +00:00
|
|
|
IOHANDLE File = io_open(pFilename, IOFLAG_READ);
|
2022-06-11 07:09:42 +00:00
|
|
|
if(File)
|
2021-11-05 16:21:41 +00:00
|
|
|
{
|
2022-06-11 07:09:42 +00:00
|
|
|
io_seek(File, 0, IOSEEK_END);
|
2024-02-03 18:11:16 +00:00
|
|
|
long int FileSize = io_tell(File);
|
|
|
|
if(FileSize <= 0)
|
|
|
|
{
|
|
|
|
io_close(File);
|
|
|
|
return false;
|
|
|
|
}
|
2022-06-11 07:09:42 +00:00
|
|
|
io_seek(File, 0, IOSEEK_START);
|
|
|
|
TImageByteBuffer ByteBuffer;
|
|
|
|
SImageByteBuffer ImageByteBuffer(&ByteBuffer);
|
2017-08-29 22:34:14 +00:00
|
|
|
|
2022-06-11 07:09:42 +00:00
|
|
|
ByteBuffer.resize(FileSize);
|
|
|
|
io_read(File, &ByteBuffer.front(), FileSize);
|
2017-08-29 22:34:14 +00:00
|
|
|
|
2021-11-05 16:21:41 +00:00
|
|
|
io_close(File);
|
2022-06-11 07:09:42 +00:00
|
|
|
|
|
|
|
uint8_t *pImgBuffer = NULL;
|
|
|
|
EImageFormat ImageFormat;
|
2022-06-27 14:57:23 +00:00
|
|
|
int PngliteIncompatible;
|
2024-04-12 15:22:52 +00:00
|
|
|
if(LoadPng(ImageByteBuffer, pFilename, PngliteIncompatible, pImg->m_Width, pImg->m_Height, pImgBuffer, ImageFormat))
|
2022-06-11 07:09:42 +00:00
|
|
|
{
|
|
|
|
if((ImageFormat == IMAGE_FORMAT_RGBA || ImageFormat == IMAGE_FORMAT_RGB) && pImg->m_Width <= (2 << 13) && pImg->m_Height <= (2 << 13))
|
|
|
|
{
|
|
|
|
pImg->m_pData = pImgBuffer;
|
|
|
|
|
2023-11-20 18:15:04 +00:00
|
|
|
if(ImageFormat == IMAGE_FORMAT_RGB)
|
2022-06-11 07:09:42 +00:00
|
|
|
pImg->m_Format = CImageInfo::FORMAT_RGB;
|
2023-11-20 18:15:04 +00:00
|
|
|
else if(ImageFormat == IMAGE_FORMAT_RGBA)
|
2022-06-11 07:09:42 +00:00
|
|
|
pImg->m_Format = CImageInfo::FORMAT_RGBA;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
free(pImgBuffer);
|
2023-11-20 18:18:45 +00:00
|
|
|
return false;
|
2022-06-11 07:09:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2023-11-20 18:18:45 +00:00
|
|
|
return false;
|
2020-09-30 16:03:06 +00:00
|
|
|
}
|
2020-10-12 16:59:58 +00:00
|
|
|
else
|
2023-11-20 18:18:45 +00:00
|
|
|
return false;
|
|
|
|
return true;
|
2017-08-28 16:06:19 +00:00
|
|
|
}
|
|
|
|
|
2023-08-13 09:50:35 +00:00
|
|
|
void *ReplaceImageItem(int Index, CMapItemImage *pImgItem, const char *pImgName, const char *pImgFile, CMapItemImage *pNewImgItem)
|
2017-08-28 16:06:19 +00:00
|
|
|
{
|
2023-08-13 09:50:35 +00:00
|
|
|
const char *pName = g_DataReader.GetDataString(pImgItem->m_ImageName);
|
|
|
|
if(pName == nullptr || pName[0] == '\0')
|
|
|
|
{
|
|
|
|
dbg_msg("map_replace_image", "failed to load name of image %d", Index);
|
|
|
|
return pImgItem;
|
|
|
|
}
|
2017-08-29 22:21:41 +00:00
|
|
|
|
|
|
|
if(str_comp(pImgName, pName) != 0)
|
2023-04-27 15:13:35 +00:00
|
|
|
return pImgItem;
|
2017-08-28 16:06:19 +00:00
|
|
|
|
2017-08-29 22:21:41 +00:00
|
|
|
dbg_msg("map_replace_image", "found image '%s'", pImgName);
|
2017-08-28 16:06:19 +00:00
|
|
|
|
2017-08-29 22:21:41 +00:00
|
|
|
CImageInfo ImgInfo;
|
2024-04-12 15:22:52 +00:00
|
|
|
if(!LoadPng(&ImgInfo, pImgFile))
|
2017-08-29 22:21:41 +00:00
|
|
|
return 0;
|
2017-08-28 16:06:19 +00:00
|
|
|
|
2023-04-27 15:13:35 +00:00
|
|
|
if(ImgInfo.m_Format != CImageInfo::FORMAT_RGBA)
|
|
|
|
{
|
|
|
|
dbg_msg("map_replace_image", "image '%s' is not in RGBA format", pImgName);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-08-29 22:21:41 +00:00
|
|
|
*pNewImgItem = *pImgItem;
|
2017-08-28 16:06:19 +00:00
|
|
|
|
2020-09-26 19:41:58 +00:00
|
|
|
pNewImgItem->m_Width = ImgInfo.m_Width;
|
2017-08-29 22:21:41 +00:00
|
|
|
pNewImgItem->m_Height = ImgInfo.m_Height;
|
2017-08-28 16:06:19 +00:00
|
|
|
|
2024-03-05 14:44:09 +00:00
|
|
|
g_NewNameId = pImgItem->m_ImageName;
|
2017-08-30 11:59:42 +00:00
|
|
|
IStorage::StripPathAndExtension(pImgFile, g_aNewName, sizeof(g_aNewName));
|
2024-03-05 14:44:09 +00:00
|
|
|
g_NewDataId = pImgItem->m_ImageData;
|
2017-08-29 22:21:41 +00:00
|
|
|
g_pNewData = ImgInfo.m_pData;
|
2024-03-24 13:21:08 +00:00
|
|
|
g_NewDataSize = ImgInfo.DataSize();
|
2017-08-28 16:06:19 +00:00
|
|
|
|
2017-08-29 22:21:41 +00:00
|
|
|
return (void *)pNewImgItem;
|
2017-08-28 16:06:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, const char **argv)
|
|
|
|
{
|
2022-06-13 16:07:29 +00:00
|
|
|
CCmdlineFix CmdlineFix(&argc, &argv);
|
2022-04-22 23:04:48 +00:00
|
|
|
log_set_global_logger_default();
|
2017-08-29 22:21:41 +00:00
|
|
|
|
|
|
|
if(argc != 5)
|
|
|
|
{
|
2017-08-29 22:34:14 +00:00
|
|
|
dbg_msg("map_replace_image", "Invalid arguments");
|
|
|
|
dbg_msg("map_replace_image", "Usage: map_replace_image <source map filepath> <dest map filepath> <current image name> <new image filepath>");
|
|
|
|
dbg_msg("map_replace_image", "Notes: map filepath must be relative to user default teeworlds folder");
|
|
|
|
dbg_msg("map_replace_image", " new image filepath must be absolute or relative to the current position");
|
2017-08-29 22:21:41 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2022-05-31 21:07:40 +00:00
|
|
|
IStorage *pStorage = CreateStorage(IStorage::STORAGETYPE_BASIC, argc, argv);
|
2020-09-26 19:41:58 +00:00
|
|
|
if(!pStorage)
|
2017-08-29 22:21:41 +00:00
|
|
|
{
|
2017-08-29 22:34:14 +00:00
|
|
|
dbg_msg("map_replace_image", "error loading storage");
|
2017-08-29 22:21:41 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *pSourceFileName = argv[1];
|
|
|
|
const char *pDestFileName = argv[2];
|
|
|
|
const char *pImageName = argv[3];
|
|
|
|
const char *pImageFile = argv[4];
|
|
|
|
|
2017-08-29 22:34:14 +00:00
|
|
|
if(!g_DataReader.Open(pStorage, pSourceFileName, IStorage::TYPE_ALL))
|
2017-08-29 22:21:41 +00:00
|
|
|
{
|
|
|
|
dbg_msg("map_replace_image", "failed to open source map. filename='%s'", pSourceFileName);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2022-05-31 21:07:40 +00:00
|
|
|
CDataFileWriter Writer;
|
|
|
|
if(!Writer.Open(pStorage, pDestFileName))
|
2017-08-29 22:21:41 +00:00
|
|
|
{
|
|
|
|
dbg_msg("map_replace_image", "failed to open destination map. filename='%s'", pDestFileName);
|
|
|
|
return -1;
|
|
|
|
}
|
2017-08-28 16:06:19 +00:00
|
|
|
|
2017-08-29 22:21:41 +00:00
|
|
|
// add all items
|
2017-08-29 22:34:14 +00:00
|
|
|
for(int Index = 0; Index < g_DataReader.NumItems(); Index++)
|
2017-08-29 22:21:41 +00:00
|
|
|
{
|
2024-03-05 14:44:09 +00:00
|
|
|
int Type, Id;
|
2023-12-29 15:27:25 +00:00
|
|
|
CUuid Uuid;
|
2024-03-05 14:44:09 +00:00
|
|
|
void *pItem = g_DataReader.GetItem(Index, &Type, &Id, &Uuid);
|
2021-05-03 08:43:00 +00:00
|
|
|
|
2023-12-21 23:10:34 +00:00
|
|
|
// Filter ITEMTYPE_EX items, they will be automatically added again.
|
2023-12-29 15:27:25 +00:00
|
|
|
if(Type == ITEMTYPE_EX)
|
2023-12-21 23:10:34 +00:00
|
|
|
{
|
2021-05-03 08:43:00 +00:00
|
|
|
continue;
|
2023-12-21 23:10:34 +00:00
|
|
|
}
|
2021-05-03 08:43:00 +00:00
|
|
|
|
2023-04-27 15:13:35 +00:00
|
|
|
int Size = g_DataReader.GetItemSize(Index);
|
|
|
|
|
2022-05-31 21:07:40 +00:00
|
|
|
CMapItemImage NewImageItem;
|
2023-04-27 15:13:35 +00:00
|
|
|
if(Type == MAPITEMTYPE_IMAGE)
|
|
|
|
{
|
2023-08-13 09:50:35 +00:00
|
|
|
pItem = ReplaceImageItem(Index, (CMapItemImage *)pItem, pImageName, pImageFile, &NewImageItem);
|
2023-04-27 15:13:35 +00:00
|
|
|
if(!pItem)
|
|
|
|
return -1;
|
|
|
|
Size = sizeof(CMapItemImage);
|
|
|
|
NewImageItem.m_Version = CMapItemImage::CURRENT_VERSION;
|
|
|
|
}
|
2022-05-31 21:07:40 +00:00
|
|
|
|
2024-03-05 14:44:09 +00:00
|
|
|
Writer.AddItem(Type, Id, Size, pItem, &Uuid);
|
2017-08-29 22:21:41 +00:00
|
|
|
}
|
|
|
|
|
2024-03-05 14:44:09 +00:00
|
|
|
if(g_NewDataId == -1)
|
2017-08-29 22:21:41 +00:00
|
|
|
{
|
|
|
|
dbg_msg("map_replace_image", "image '%s' not found on source map '%s'.", pImageName, pSourceFileName);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// add all data
|
2022-03-16 15:40:13 +00:00
|
|
|
for(int Index = 0; Index < g_DataReader.NumData(); Index++)
|
2017-08-29 22:21:41 +00:00
|
|
|
{
|
2022-05-31 21:07:40 +00:00
|
|
|
void *pData;
|
|
|
|
int Size;
|
2024-03-05 14:44:09 +00:00
|
|
|
if(Index == g_NewDataId)
|
2017-08-29 22:21:41 +00:00
|
|
|
{
|
|
|
|
pData = g_pNewData;
|
|
|
|
Size = g_NewDataSize;
|
2017-08-29 22:34:14 +00:00
|
|
|
}
|
2024-03-05 14:44:09 +00:00
|
|
|
else if(Index == g_NewNameId)
|
2017-08-29 22:21:41 +00:00
|
|
|
{
|
|
|
|
pData = (void *)g_aNewName;
|
|
|
|
Size = str_length(g_aNewName) + 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-08-29 22:34:14 +00:00
|
|
|
pData = g_DataReader.GetData(Index);
|
2017-08-30 06:36:17 +00:00
|
|
|
Size = g_DataReader.GetDataSize(Index);
|
2017-08-29 22:21:41 +00:00
|
|
|
}
|
|
|
|
|
2022-05-31 21:07:40 +00:00
|
|
|
Writer.AddData(Size, pData);
|
2017-08-29 22:21:41 +00:00
|
|
|
}
|
|
|
|
|
2017-08-29 22:34:14 +00:00
|
|
|
g_DataReader.Close();
|
2022-05-31 21:07:40 +00:00
|
|
|
Writer.Finish();
|
2017-08-29 22:21:41 +00:00
|
|
|
|
|
|
|
dbg_msg("map_replace_image", "image '%s' replaced", pImageName);
|
|
|
|
return 0;
|
2017-08-28 16:06:19 +00:00
|
|
|
}
|