Skip to content

Check Existing File Before Saving Skins #2803 #35

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: community
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions src/base/system.c
Original file line number Diff line number Diff line change
Expand Up @@ -1870,6 +1870,31 @@ int fs_file_time(const char *name, time_t *created, time_t *modified)
return 0;
}

int fs_file_name(const char *name, char *buffer, int length)
{
#if defined(CONF_FAMILY_WINDOWS)
WIN32_FIND_DATA finddata;
HANDLE handle;

handle = FindFirstFileA(name, &finddata);

if (handle == INVALID_HANDLE_VALUE)
return -1;

str_copy(buffer, finddata.cFileName, length);

FindClose(handle);
return 0;
#else
struct stat sb;
if (stat(name, &sb) == -1)
return -1;

str_copy(buffer, name, length);
return 0;
#endif
}

void swap_endian(void *data, unsigned elem_size, unsigned num)
{
char *src = (char*) data;
Expand Down
16 changes: 14 additions & 2 deletions src/base/system.h
Original file line number Diff line number Diff line change
Expand Up @@ -1358,8 +1358,6 @@ typedef struct
time_t m_TimeModified; // seconds since UNIX Epoch
} CFsFileInfo;

/* Group: Filesystem */

/*
Function: fs_listdir_fileinfo
Lists the files in a directory and gets additional file information
Expand Down Expand Up @@ -1550,6 +1548,20 @@ char *fs_read_str(const char *name);
*/
int fs_file_time(const char *name, time_t *created, time_t *modified);

/*
Function: fs_file_name
Gets the stored name in filesystem of a file.

Parameters:
name - The filename
buffer - The buffer to store the real filename
length - size of the buffer

Returns:
0 on success non-zero on failure
*/
int fs_file_name(const char *name, char *buffer, int length);

/*
Group: Undocumented
*/
Expand Down
8 changes: 8 additions & 0 deletions src/engine/shared/storage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -608,6 +608,14 @@ class CStorage : public IStorage
return !fs_file_time(aBuf, pCreated, pModified);
}

virtual bool GetFileSystemFileName(const char *pFilename, int StorageType, char *pBuffer, int Length)
{
char aBuf[IO_MAX_PATH_LENGTH];
GetCompletePath(StorageType, pFilename, aBuf, sizeof(aBuf));

return !fs_file_name(aBuf, pBuffer, Length);
}

static IStorage *Create(const char *pApplicationName, int StorageType, int NumArgs, const char **ppArguments)
{
CStorage *p = new CStorage();
Expand Down
1 change: 1 addition & 0 deletions src/engine/storage.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class IStorage : public IInterface
virtual void GetCompletePath(int Type, const char *pDir, char *pBuffer, unsigned BufferSize) = 0;
virtual bool GetHashAndSize(const char *pFilename, int StorageType, SHA256_DIGEST *pSha256, unsigned *pCrc, unsigned *pSize) = 0;
virtual bool GetFileTime(const char *pFilename, int StorageType, time_t *pCreated, time_t *pModified) = 0;
virtual bool GetFileSystemFileName(const char *pFilename, int StorageType, char *pBuffer, int Length) = 0;
};

IStorage *CreateStorage(const char *pApplicationName, int StorageType, int NumArgs, const char **ppArguments);
Expand Down
13 changes: 12 additions & 1 deletion src/game/client/components/skins.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,13 @@ void CSkins::SaveSkinfile(const char *pSaveSkinName)
{
char aBuf[IO_MAX_PATH_LENGTH];
str_format(aBuf, sizeof(aBuf), "skins/%s.json", pSaveSkinName);

char aFileSystemFileName[IO_MAX_PATH_LENGTH];
if(Storage()->GetFileSystemFileName(aBuf, IStorage::TYPE_SAVE, aFileSystemFileName, IO_MAX_PATH_LENGTH))
str_format(aBuf, sizeof(aBuf), "skins/%s", aFileSystemFileName);
else
str_format(aFileSystemFileName, sizeof(aFileSystemFileName), "%s.json", pSaveSkinName);

IOHANDLE File = Storage()->OpenFile(aBuf, IOFLAG_WRITE, IStorage::TYPE_SAVE);
if(!File)
return;
Expand Down Expand Up @@ -603,6 +610,10 @@ void CSkins::SaveSkinfile(const char *pSaveSkinName)
Writer.EndObject();
Writer.EndObject();

// remove file extension
int Length = str_length(aFileSystemFileName);
aFileSystemFileName[Length-5] = 0;

// add new skin to the skin list
AddSkin(pSaveSkinName);
AddSkin(aFileSystemFileName);
}