2025-05-21 04:49:23 -07:00
|
|
|
/* Copyright (C) 2025 Wildfire Games.
|
2023-12-02 16:30:12 -08:00
|
|
|
* This file is part of 0 A.D.
|
2009-04-18 10:00:33 -07:00
|
|
|
*
|
2023-12-02 16:30:12 -08:00
|
|
|
* 0 A.D. is free software: you can redistribute it and/or modify
|
2009-04-18 10:00:33 -07:00
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation, either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
2023-12-02 16:30:12 -08:00
|
|
|
* 0 A.D. is distributed in the hope that it will be useful,
|
2009-04-18 10:00:33 -07:00
|
|
|
* 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
|
2023-12-02 16:30:12 -08:00
|
|
|
* along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
|
2009-04-18 10:00:33 -07:00
|
|
|
*/
|
|
|
|
|
|
2006-04-23 16:14:18 -07:00
|
|
|
#include "precompiled.h"
|
|
|
|
|
|
2010-02-17 16:06:50 -08:00
|
|
|
#include "ps/Util.h"
|
|
|
|
|
|
2008-01-07 12:03:19 -08:00
|
|
|
#include "lib/allocators/shared_ptr.h"
|
2007-12-20 12:21:45 -08:00
|
|
|
#include "lib/tex/tex.h"
|
2010-11-20 17:25:16 -08:00
|
|
|
#include "ps/CLogger.h"
|
2007-12-20 12:21:45 -08:00
|
|
|
#include "ps/Filesystem.h"
|
2020-01-02 10:07:40 -08:00
|
|
|
#include "ps/Pyrogenesis.h"
|
2006-04-23 16:14:18 -07:00
|
|
|
|
2023-12-04 12:23:37 -08:00
|
|
|
#include <iomanip>
|
2025-05-21 04:49:23 -07:00
|
|
|
#include <utility>
|
2023-12-04 12:23:37 -08:00
|
|
|
|
2006-05-17 07:48:18 -07:00
|
|
|
// not thread-safe!
|
2006-04-23 16:14:18 -07:00
|
|
|
static const wchar_t* HardcodedErrorString(int err)
|
|
|
|
|
{
|
2009-11-03 13:46:35 -08:00
|
|
|
static wchar_t description[200];
|
2011-05-03 05:38:42 -07:00
|
|
|
StatusDescription((Status)err, description, ARRAY_SIZE(description));
|
2009-11-03 13:46:35 -08:00
|
|
|
return description;
|
2006-04-23 16:14:18 -07:00
|
|
|
}
|
|
|
|
|
|
2006-05-17 07:48:18 -07:00
|
|
|
// not thread-safe!
|
2006-04-23 16:14:18 -07:00
|
|
|
const wchar_t* ErrorString(int err)
|
|
|
|
|
{
|
|
|
|
|
// language file not available (yet)
|
|
|
|
|
return HardcodedErrorString(err);
|
|
|
|
|
|
|
|
|
|
// TODO: load from language file
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-12 10:39:05 -07:00
|
|
|
CStr GetStatusAsString(Status status)
|
|
|
|
|
{
|
|
|
|
|
return utf8_from_wstring(ErrorString(status));
|
|
|
|
|
}
|
2007-12-22 10:15:52 -08:00
|
|
|
|
|
|
|
|
// write the specified texture to disk.
|
|
|
|
|
// note: <t> cannot be made const because the image may have to be
|
|
|
|
|
// transformed to write it out in the format determined by <fn>'s extension.
|
2011-05-03 05:38:42 -07:00
|
|
|
Status tex_write(Tex* t, const VfsPath& filename)
|
2007-12-22 10:15:52 -08:00
|
|
|
{
|
|
|
|
|
DynArray da;
|
2014-03-12 19:37:05 -07:00
|
|
|
RETURN_STATUS_IF_ERR(t->encode(filename.Extension(), &da));
|
2007-12-22 10:15:52 -08:00
|
|
|
|
|
|
|
|
// write to disk
|
2011-05-03 05:38:42 -07:00
|
|
|
Status ret = INFO::OK;
|
2007-12-22 10:15:52 -08:00
|
|
|
{
|
2021-05-22 12:28:40 -07:00
|
|
|
std::shared_ptr<u8> file = DummySharedPtr(da.base);
|
2008-01-07 12:03:19 -08:00
|
|
|
const ssize_t bytes_written = g_VFS->CreateFile(filename, file, da.pos);
|
2007-12-22 10:15:52 -08:00
|
|
|
if(bytes_written > 0)
|
2011-04-30 06:01:45 -07:00
|
|
|
ENSURE(bytes_written == (ssize_t)da.pos);
|
2007-12-22 10:15:52 -08:00
|
|
|
else
|
2011-05-03 05:38:42 -07:00
|
|
|
ret = (Status)bytes_written;
|
2007-12-22 10:15:52 -08:00
|
|
|
}
|
|
|
|
|
|
2025-05-21 04:49:23 -07:00
|
|
|
std::ignore = da_free(&da);
|
2007-12-22 10:15:52 -08:00
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-15 07:57:23 -08:00
|
|
|
/**
|
|
|
|
|
* Return an unused directory, based on date and index (for example 2016-02-09_0001)
|
|
|
|
|
*/
|
2016-05-15 17:56:07 -07:00
|
|
|
OsPath createDateIndexSubdirectory(const OsPath& parentDir)
|
2016-02-15 07:57:23 -08:00
|
|
|
{
|
|
|
|
|
const std::time_t timestamp = std::time(nullptr);
|
|
|
|
|
const struct std::tm* now = std::localtime(×tamp);
|
|
|
|
|
|
2016-02-19 03:22:32 -08:00
|
|
|
// Two processes executing this simultaneously might attempt to create the same directory.
|
|
|
|
|
int tries = 0;
|
|
|
|
|
const int maxTries = 10;
|
|
|
|
|
|
2016-02-15 07:57:23 -08:00
|
|
|
int i = 0;
|
|
|
|
|
OsPath path;
|
|
|
|
|
char directory[256];
|
|
|
|
|
|
|
|
|
|
do
|
|
|
|
|
{
|
2024-12-04 10:05:21 -08:00
|
|
|
sprintf_s(directory, ARRAY_SIZE(directory), "%04d-%02d-%02d_%04d", now->tm_year+1900, now->tm_mon+1, now->tm_mday, ++i);
|
2016-02-15 07:57:23 -08:00
|
|
|
path = parentDir / CStr(directory);
|
2016-02-19 03:22:32 -08:00
|
|
|
|
|
|
|
|
if (DirectoryExists(path) || FileExists(path))
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
if (CreateDirectories(path, 0700, ++tries > maxTries) == INFO::OK)
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
} while(tries <= maxTries);
|
2016-02-15 07:57:23 -08:00
|
|
|
|
|
|
|
|
return path;
|
|
|
|
|
}
|
2007-12-22 10:15:52 -08:00
|
|
|
|
2016-08-08 05:50:39 -07:00
|
|
|
std::string Hexify(const std::string& s)
|
|
|
|
|
{
|
|
|
|
|
std::stringstream str;
|
|
|
|
|
str << std::hex;
|
|
|
|
|
for (const char& c : s)
|
2018-08-08 05:59:05 -07:00
|
|
|
str << std::setfill('0') << std::setw(2) << static_cast<int>(static_cast<unsigned char>(c));
|
|
|
|
|
return str.str();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::string Hexify(const u8* s, size_t length)
|
|
|
|
|
{
|
|
|
|
|
std::stringstream str;
|
|
|
|
|
str << std::hex;
|
|
|
|
|
for (size_t i = 0; i < length; ++i)
|
|
|
|
|
str << std::setfill('0') << std::setw(2) << static_cast<int>(s[i]);
|
2016-08-08 05:50:39 -07:00
|
|
|
return str.str();
|
|
|
|
|
}
|