2020-01-01 21:29:24 +01:00
|
|
|
#include "FileSystemUtils.h"
|
|
|
|
|
|
|
|
#include <vector>
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2020-01-10 19:59:34 +01:00
|
|
|
#include <SDL.h>
|
2020-01-01 21:29:24 +01:00
|
|
|
#include <physfs.h>
|
|
|
|
|
Replace TiXmlDocument load and save functions by PHYSFS
The TinyXml functions to load and save files don't properly support
unicode file paths on Windows, so in order to support that properly, I
saw no other option than to do the actual loading and saving via PHYSFS
(or to use the Windows API on Windows and retain doc.LoadFile and
doc.SaveFile on other OSes, but that'd be more complicated and
unnecessary, we already have PHYSFS, right?).
There are two new functions in FileSystemUtils:
bool FILESYSTEM_saveTiXmlDocument(const char *name, TiXmlDocument *doc)
bool FILESYSTEM_loadTiXmlDocument(const char *name, TiXmlDocument *doc)
Any instances of doc.SaveFile(<FULL_PATH>) have been replaced by
FILESYSTEM_saveTiXmlDocument(<VVVVVV_FOLDER_PATH>, &doc), where
<FULL_PATH> included the full path to the saves or levels directory,
and <VVVVVV_FOLDER_PATH> only includes the path relative to the VVVVVV
directory.
When loading a document, a TiXmlDocument used to be created with a full
path in its constructor and doc.LoadFile() would then be called, now a
TiXmlDocument is constructed with no path name and
FILESYSTEM_loadTiXmlDocument(<VVVVVV_FOLDER_PATH>, &doc) is called.
2020-01-12 15:17:39 +01:00
|
|
|
#include "tinyxml.h"
|
|
|
|
|
2020-01-01 21:29:24 +01:00
|
|
|
#if defined(_WIN32)
|
|
|
|
#include <windows.h>
|
|
|
|
#include <shlobj.h>
|
2020-01-12 12:37:22 +01:00
|
|
|
int mkdir(char* path, int mode)
|
|
|
|
{
|
|
|
|
WCHAR utf16_path[MAX_PATH];
|
|
|
|
MultiByteToWideChar(CP_UTF8, 0, path, -1, utf16_path, MAX_PATH);
|
|
|
|
return CreateDirectoryW(utf16_path, NULL);
|
|
|
|
}
|
2020-01-01 21:29:24 +01:00
|
|
|
#define VNEEDS_MIGRATION (mkdirResult != 0)
|
2020-01-14 05:31:14 +01:00
|
|
|
#elif defined(__linux__) || defined(__APPLE__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__HAIKU__)
|
2020-01-01 21:29:24 +01:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#define VNEEDS_MIGRATION (mkdirResult == 0)
|
|
|
|
/* These are needed for PLATFORM_* crap */
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <dirent.h>
|
|
|
|
#define MAX_PATH PATH_MAX
|
|
|
|
#endif
|
|
|
|
|
|
|
|
char saveDir[MAX_PATH];
|
|
|
|
char levelDir[MAX_PATH];
|
|
|
|
|
|
|
|
void PLATFORM_getOSDirectory(char* output);
|
|
|
|
void PLATFORM_migrateSaveData(char* output);
|
|
|
|
void PLATFORM_copyFile(const char *oldLocation, const char *newLocation);
|
|
|
|
|
2020-02-09 00:49:03 +01:00
|
|
|
int FILESYSTEM_init(char *argvZero, char* baseDir, char *assetsPath)
|
2020-01-01 21:29:24 +01:00
|
|
|
{
|
|
|
|
char output[MAX_PATH];
|
|
|
|
int mkdirResult;
|
2020-02-09 00:49:03 +01:00
|
|
|
const char* pathSep = PHYSFS_getDirSeparator();
|
2020-01-01 21:29:24 +01:00
|
|
|
|
|
|
|
PHYSFS_init(argvZero);
|
2020-01-12 16:52:42 +01:00
|
|
|
PHYSFS_permitSymbolicLinks(1);
|
2020-01-01 21:29:24 +01:00
|
|
|
|
|
|
|
/* Determine the OS user directory */
|
2020-02-09 00:49:03 +01:00
|
|
|
if (baseDir && strlen(baseDir) > 0)
|
|
|
|
{
|
|
|
|
strcpy(output, baseDir);
|
|
|
|
|
|
|
|
/* We later append to this path and assume it ends in a slash */
|
|
|
|
if (strcmp(std::string(1, output[strlen(output) - 1]).c_str(), pathSep) != 0)
|
|
|
|
{
|
|
|
|
strcat(output, pathSep);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
PLATFORM_getOSDirectory(output);
|
|
|
|
}
|
2020-01-01 21:29:24 +01:00
|
|
|
|
|
|
|
/* Create base user directory, mount */
|
|
|
|
mkdirResult = mkdir(output, 0777);
|
|
|
|
|
|
|
|
/* Mount our base user directory */
|
|
|
|
PHYSFS_mount(output, NULL, 1);
|
Replace TiXmlDocument load and save functions by PHYSFS
The TinyXml functions to load and save files don't properly support
unicode file paths on Windows, so in order to support that properly, I
saw no other option than to do the actual loading and saving via PHYSFS
(or to use the Windows API on Windows and retain doc.LoadFile and
doc.SaveFile on other OSes, but that'd be more complicated and
unnecessary, we already have PHYSFS, right?).
There are two new functions in FileSystemUtils:
bool FILESYSTEM_saveTiXmlDocument(const char *name, TiXmlDocument *doc)
bool FILESYSTEM_loadTiXmlDocument(const char *name, TiXmlDocument *doc)
Any instances of doc.SaveFile(<FULL_PATH>) have been replaced by
FILESYSTEM_saveTiXmlDocument(<VVVVVV_FOLDER_PATH>, &doc), where
<FULL_PATH> included the full path to the saves or levels directory,
and <VVVVVV_FOLDER_PATH> only includes the path relative to the VVVVVV
directory.
When loading a document, a TiXmlDocument used to be created with a full
path in its constructor and doc.LoadFile() would then be called, now a
TiXmlDocument is constructed with no path name and
FILESYSTEM_loadTiXmlDocument(<VVVVVV_FOLDER_PATH>, &doc) is called.
2020-01-12 15:17:39 +01:00
|
|
|
PHYSFS_setWriteDir(output);
|
2020-01-01 21:29:24 +01:00
|
|
|
printf("Base directory: %s\n", output);
|
|
|
|
|
|
|
|
/* Create save directory */
|
|
|
|
strcpy(saveDir, output);
|
|
|
|
strcat(saveDir, "saves");
|
|
|
|
strcat(saveDir, PHYSFS_getDirSeparator());
|
|
|
|
mkdir(saveDir, 0777);
|
|
|
|
printf("Save directory: %s\n", saveDir);
|
|
|
|
|
|
|
|
/* Create level directory */
|
|
|
|
strcpy(levelDir, output);
|
|
|
|
strcat(levelDir, "levels");
|
|
|
|
strcat(levelDir, PHYSFS_getDirSeparator());
|
|
|
|
mkdirResult |= mkdir(levelDir, 0777);
|
|
|
|
printf("Level directory: %s\n", levelDir);
|
|
|
|
|
|
|
|
/* We didn't exist until now, migrate files! */
|
|
|
|
if (VNEEDS_MIGRATION)
|
|
|
|
{
|
|
|
|
PLATFORM_migrateSaveData(output);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Mount the stock content last */
|
2020-02-03 00:28:26 +01:00
|
|
|
if (assetsPath) {
|
|
|
|
strcpy(output, assetsPath);
|
|
|
|
} else {
|
|
|
|
strcpy(output, PHYSFS_getBaseDir());
|
|
|
|
strcat(output, "data.zip");
|
|
|
|
}
|
2020-01-10 19:59:34 +01:00
|
|
|
if (!PHYSFS_mount(output, NULL, 1))
|
|
|
|
{
|
2020-01-10 22:04:04 +01:00
|
|
|
puts("Error: data.zip missing!");
|
|
|
|
puts("You do not have data.zip!");
|
|
|
|
puts("Grab it from your purchased copy of the game,");
|
|
|
|
puts("or get it from the free Make and Play Edition.");
|
|
|
|
|
2020-01-10 19:59:34 +01:00
|
|
|
SDL_ShowSimpleMessageBox(
|
|
|
|
SDL_MESSAGEBOX_ERROR,
|
|
|
|
"data.zip missing!",
|
|
|
|
"You do not have data.zip!"
|
|
|
|
"\n\nGrab it from your purchased copy of the game,"
|
2020-01-10 20:00:45 +01:00
|
|
|
"\nor get it from the free Make and Play Edition.",
|
2020-01-10 19:59:34 +01:00
|
|
|
NULL
|
|
|
|
);
|
2020-01-10 22:04:04 +01:00
|
|
|
return 0;
|
2020-01-10 19:59:34 +01:00
|
|
|
}
|
2020-01-17 18:43:42 +01:00
|
|
|
|
|
|
|
strcpy(output, PHYSFS_getBaseDir());
|
|
|
|
strcpy(output, "gamecontrollerdb.txt");
|
|
|
|
if (SDL_GameControllerAddMappingsFromFile(output) < 0)
|
|
|
|
{
|
|
|
|
printf("gamecontrollerdb.txt not found!\n");
|
|
|
|
}
|
2020-01-10 22:04:04 +01:00
|
|
|
return 1;
|
2020-01-01 21:29:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void FILESYSTEM_deinit()
|
|
|
|
{
|
|
|
|
PHYSFS_deinit();
|
|
|
|
}
|
|
|
|
|
|
|
|
char *FILESYSTEM_getUserSaveDirectory()
|
|
|
|
{
|
|
|
|
return saveDir;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *FILESYSTEM_getUserLevelDirectory()
|
|
|
|
{
|
|
|
|
return levelDir;
|
|
|
|
}
|
|
|
|
|
2020-01-24 21:35:46 +01:00
|
|
|
void FILESYSTEM_loadFileToMemory(const char *name, unsigned char **mem,
|
|
|
|
size_t *len, bool addnull)
|
2020-01-01 21:29:24 +01:00
|
|
|
{
|
|
|
|
PHYSFS_File *handle = PHYSFS_openRead(name);
|
|
|
|
if (handle == NULL)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
PHYSFS_uint32 length = PHYSFS_fileLength(handle);
|
|
|
|
if (len != NULL)
|
|
|
|
{
|
|
|
|
*len = length;
|
|
|
|
}
|
2020-01-24 21:35:46 +01:00
|
|
|
if (addnull)
|
|
|
|
{
|
|
|
|
*mem = (unsigned char *) malloc(length + 1);
|
2020-01-24 22:12:45 +01:00
|
|
|
(*mem)[length] = 0;
|
2020-01-24 21:35:46 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
*mem = (unsigned char*) malloc(length);
|
|
|
|
}
|
2020-01-10 23:59:12 +01:00
|
|
|
PHYSFS_readBytes(handle, *mem, length);
|
2020-01-01 21:29:24 +01:00
|
|
|
PHYSFS_close(handle);
|
|
|
|
}
|
|
|
|
|
|
|
|
void FILESYSTEM_freeMemory(unsigned char **mem)
|
|
|
|
{
|
|
|
|
free(*mem);
|
|
|
|
*mem = NULL;
|
|
|
|
}
|
|
|
|
|
Replace TiXmlDocument load and save functions by PHYSFS
The TinyXml functions to load and save files don't properly support
unicode file paths on Windows, so in order to support that properly, I
saw no other option than to do the actual loading and saving via PHYSFS
(or to use the Windows API on Windows and retain doc.LoadFile and
doc.SaveFile on other OSes, but that'd be more complicated and
unnecessary, we already have PHYSFS, right?).
There are two new functions in FileSystemUtils:
bool FILESYSTEM_saveTiXmlDocument(const char *name, TiXmlDocument *doc)
bool FILESYSTEM_loadTiXmlDocument(const char *name, TiXmlDocument *doc)
Any instances of doc.SaveFile(<FULL_PATH>) have been replaced by
FILESYSTEM_saveTiXmlDocument(<VVVVVV_FOLDER_PATH>, &doc), where
<FULL_PATH> included the full path to the saves or levels directory,
and <VVVVVV_FOLDER_PATH> only includes the path relative to the VVVVVV
directory.
When loading a document, a TiXmlDocument used to be created with a full
path in its constructor and doc.LoadFile() would then be called, now a
TiXmlDocument is constructed with no path name and
FILESYSTEM_loadTiXmlDocument(<VVVVVV_FOLDER_PATH>, &doc) is called.
2020-01-12 15:17:39 +01:00
|
|
|
bool FILESYSTEM_saveTiXmlDocument(const char *name, TiXmlDocument *doc)
|
|
|
|
{
|
|
|
|
/* TiXmlDocument.SaveFile doesn't account for Unicode paths, PHYSFS does */
|
|
|
|
TiXmlPrinter printer;
|
|
|
|
doc->Accept(&printer);
|
|
|
|
PHYSFS_File* handle = PHYSFS_openWrite(name);
|
|
|
|
if (handle == NULL)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
PHYSFS_writeBytes(handle, printer.CStr(), printer.Size());
|
|
|
|
PHYSFS_close(handle);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FILESYSTEM_loadTiXmlDocument(const char *name, TiXmlDocument *doc)
|
|
|
|
{
|
|
|
|
/* TiXmlDocument.SaveFile doesn't account for Unicode paths, PHYSFS does */
|
|
|
|
unsigned char *mem = NULL;
|
2020-01-24 21:35:46 +01:00
|
|
|
FILESYSTEM_loadFileToMemory(name, &mem, NULL, true);
|
Replace TiXmlDocument load and save functions by PHYSFS
The TinyXml functions to load and save files don't properly support
unicode file paths on Windows, so in order to support that properly, I
saw no other option than to do the actual loading and saving via PHYSFS
(or to use the Windows API on Windows and retain doc.LoadFile and
doc.SaveFile on other OSes, but that'd be more complicated and
unnecessary, we already have PHYSFS, right?).
There are two new functions in FileSystemUtils:
bool FILESYSTEM_saveTiXmlDocument(const char *name, TiXmlDocument *doc)
bool FILESYSTEM_loadTiXmlDocument(const char *name, TiXmlDocument *doc)
Any instances of doc.SaveFile(<FULL_PATH>) have been replaced by
FILESYSTEM_saveTiXmlDocument(<VVVVVV_FOLDER_PATH>, &doc), where
<FULL_PATH> included the full path to the saves or levels directory,
and <VVVVVV_FOLDER_PATH> only includes the path relative to the VVVVVV
directory.
When loading a document, a TiXmlDocument used to be created with a full
path in its constructor and doc.LoadFile() would then be called, now a
TiXmlDocument is constructed with no path name and
FILESYSTEM_loadTiXmlDocument(<VVVVVV_FOLDER_PATH>, &doc) is called.
2020-01-12 15:17:39 +01:00
|
|
|
if (mem == NULL)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2020-01-31 19:25:37 +01:00
|
|
|
doc->Parse((const char*)mem, NULL, TIXML_ENCODING_UTF8);
|
Replace TiXmlDocument load and save functions by PHYSFS
The TinyXml functions to load and save files don't properly support
unicode file paths on Windows, so in order to support that properly, I
saw no other option than to do the actual loading and saving via PHYSFS
(or to use the Windows API on Windows and retain doc.LoadFile and
doc.SaveFile on other OSes, but that'd be more complicated and
unnecessary, we already have PHYSFS, right?).
There are two new functions in FileSystemUtils:
bool FILESYSTEM_saveTiXmlDocument(const char *name, TiXmlDocument *doc)
bool FILESYSTEM_loadTiXmlDocument(const char *name, TiXmlDocument *doc)
Any instances of doc.SaveFile(<FULL_PATH>) have been replaced by
FILESYSTEM_saveTiXmlDocument(<VVVVVV_FOLDER_PATH>, &doc), where
<FULL_PATH> included the full path to the saves or levels directory,
and <VVVVVV_FOLDER_PATH> only includes the path relative to the VVVVVV
directory.
When loading a document, a TiXmlDocument used to be created with a full
path in its constructor and doc.LoadFile() would then be called, now a
TiXmlDocument is constructed with no path name and
FILESYSTEM_loadTiXmlDocument(<VVVVVV_FOLDER_PATH>, &doc) is called.
2020-01-12 15:17:39 +01:00
|
|
|
FILESYSTEM_freeMemory(&mem);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-01-01 21:29:24 +01:00
|
|
|
std::vector<std::string> FILESYSTEM_getLevelDirFileNames()
|
|
|
|
{
|
|
|
|
std::vector<std::string> list;
|
|
|
|
char **fileList = PHYSFS_enumerateFiles("/levels");
|
|
|
|
char **i;
|
|
|
|
std::string builtLocation;
|
|
|
|
|
|
|
|
for (i = fileList; *i != NULL; i++)
|
|
|
|
{
|
|
|
|
if (strcmp(*i, "data") == 0)
|
|
|
|
{
|
|
|
|
continue; /* FIXME: lolwut -flibit */
|
|
|
|
}
|
|
|
|
builtLocation = "levels/";
|
|
|
|
builtLocation += *i;
|
|
|
|
list.push_back(builtLocation);
|
|
|
|
}
|
|
|
|
|
|
|
|
PHYSFS_freeList(fileList);
|
|
|
|
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PLATFORM_getOSDirectory(char* output)
|
|
|
|
{
|
2020-01-11 17:29:07 +01:00
|
|
|
#ifdef _WIN32
|
|
|
|
/* This block is here for compatibility, do not touch it! */
|
2020-01-12 12:37:22 +01:00
|
|
|
WCHAR utf16_path[MAX_PATH];
|
|
|
|
SHGetFolderPathW(NULL, CSIDL_PERSONAL, NULL, SHGFP_TYPE_CURRENT, utf16_path);
|
|
|
|
WideCharToMultiByte(CP_UTF8, 0, utf16_path, -1, output, MAX_PATH, NULL, NULL);
|
2020-01-01 21:29:24 +01:00
|
|
|
strcat(output, "\\VVVVVV\\");
|
|
|
|
#else
|
2020-01-11 17:29:07 +01:00
|
|
|
strcpy(output, PHYSFS_getPrefDir("distractionware", "VVVVVV"));
|
2020-01-01 21:29:24 +01:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void PLATFORM_migrateSaveData(char* output)
|
|
|
|
{
|
|
|
|
char oldLocation[MAX_PATH];
|
|
|
|
char newLocation[MAX_PATH];
|
|
|
|
char oldDirectory[MAX_PATH];
|
2020-01-14 05:31:14 +01:00
|
|
|
#if defined(__linux__) || defined(__APPLE__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__HAIKU__)
|
2020-01-01 21:29:24 +01:00
|
|
|
DIR *dir = NULL;
|
|
|
|
struct dirent *de = NULL;
|
|
|
|
DIR *subDir = NULL;
|
|
|
|
struct dirent *subDe = NULL;
|
|
|
|
char subDirLocation[MAX_PATH];
|
|
|
|
const char *homeDir = getenv("HOME");
|
|
|
|
if (homeDir == NULL)
|
|
|
|
{
|
|
|
|
/* Uhh, I don't want to get near this. -flibit */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
strcpy(oldDirectory, homeDir);
|
2020-01-14 05:31:14 +01:00
|
|
|
#if defined(__linux__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__HAIKU__)
|
2020-01-01 21:29:24 +01:00
|
|
|
strcat(oldDirectory, "/.vvvvvv/");
|
|
|
|
#elif defined(__APPLE__)
|
|
|
|
strcat(oldDirectory, "/Documents/VVVVVV/");
|
|
|
|
#endif
|
|
|
|
dir = opendir(oldDirectory);
|
|
|
|
if (!dir)
|
|
|
|
{
|
|
|
|
printf("Could not find directory %s\n", oldDirectory);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("Migrating old savedata to new location...\n");
|
|
|
|
for (de = readdir(dir); de != NULL; de = readdir(dir))
|
|
|
|
{
|
|
|
|
if ( strcmp(de->d_name, "..") == 0 ||
|
|
|
|
strcmp(de->d_name, ".") == 0 )
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
#define COPY_SAVEFILE(name) \
|
|
|
|
else if (strcmp(de->d_name, name) == 0) \
|
|
|
|
{ \
|
|
|
|
strcpy(oldLocation, oldDirectory); \
|
|
|
|
strcat(oldLocation, name); \
|
|
|
|
strcpy(newLocation, output); \
|
|
|
|
strcat(newLocation, "saves/"); \
|
|
|
|
strcat(newLocation, name); \
|
|
|
|
PLATFORM_copyFile(oldLocation, newLocation); \
|
|
|
|
}
|
|
|
|
COPY_SAVEFILE("unlock.vvv")
|
|
|
|
COPY_SAVEFILE("tsave.vvv")
|
|
|
|
COPY_SAVEFILE("qsave.vvv")
|
|
|
|
#undef COPY_SAVEFILE
|
|
|
|
else if (strstr(de->d_name, ".vvvvvv.vvv") != NULL)
|
|
|
|
{
|
|
|
|
strcpy(oldLocation, oldDirectory);
|
|
|
|
strcat(oldLocation, de->d_name);
|
|
|
|
strcpy(newLocation, output);
|
|
|
|
strcat(newLocation, "saves/");
|
|
|
|
strcat(newLocation, de->d_name);
|
|
|
|
PLATFORM_copyFile(oldLocation, newLocation);
|
|
|
|
}
|
|
|
|
else if (strstr(de->d_name, ".vvvvvv") != NULL)
|
|
|
|
{
|
|
|
|
strcpy(oldLocation, oldDirectory);
|
|
|
|
strcat(oldLocation, de->d_name);
|
|
|
|
strcpy(newLocation, output);
|
|
|
|
strcat(newLocation, "levels/");
|
|
|
|
strcat(newLocation, de->d_name);
|
|
|
|
PLATFORM_copyFile(oldLocation, newLocation);
|
|
|
|
}
|
|
|
|
else if (strcmp(de->d_name, "Saves") == 0)
|
|
|
|
{
|
|
|
|
strcpy(subDirLocation, oldDirectory);
|
|
|
|
strcat(subDirLocation, "Saves/");
|
|
|
|
subDir = opendir(subDirLocation);
|
|
|
|
if (!subDir)
|
|
|
|
{
|
|
|
|
printf("Could not open Saves/ subdir!\n");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
for (
|
|
|
|
subDe = readdir(subDir);
|
|
|
|
subDe != NULL;
|
|
|
|
subDe = readdir(subDir)
|
|
|
|
) {
|
|
|
|
#define COPY_SAVEFILE(name) \
|
|
|
|
(strcmp(subDe->d_name, name) == 0) \
|
|
|
|
{ \
|
|
|
|
strcpy(oldLocation, subDirLocation); \
|
|
|
|
strcat(oldLocation, name); \
|
|
|
|
strcpy(newLocation, output); \
|
|
|
|
strcat(newLocation, "saves/"); \
|
|
|
|
strcat(newLocation, name); \
|
|
|
|
PLATFORM_copyFile(oldLocation, newLocation); \
|
|
|
|
}
|
|
|
|
if COPY_SAVEFILE("unlock.vvv")
|
|
|
|
else if COPY_SAVEFILE("tsave.vvv")
|
|
|
|
else if COPY_SAVEFILE("qsave.vvv")
|
|
|
|
#undef COPY_SAVEFILE
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#elif defined(_WIN32)
|
|
|
|
WIN32_FIND_DATA findHandle;
|
|
|
|
HANDLE hFind = NULL;
|
|
|
|
char fileSearch[MAX_PATH];
|
|
|
|
|
|
|
|
/* Same place, different layout. */
|
|
|
|
strcpy(oldDirectory, output);
|
|
|
|
|
|
|
|
/* In theory we don't need to worry about this, thanks case insensitivity!
|
|
|
|
sprintf(fileSearch, "%s\\Saves\\*.vvv", oldDirectory);
|
|
|
|
hFind = FindFirstFile(fileSearch, &findHandle);
|
|
|
|
if (hFind == INVALID_HANDLE_VALUE)
|
|
|
|
{
|
|
|
|
printf("Could not find directory %s\\Saves\\\n", oldDirectory);
|
|
|
|
}
|
|
|
|
else do
|
|
|
|
{
|
|
|
|
if ((findHandle.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0)
|
|
|
|
{
|
|
|
|
#define COPY_SAVEFILE(name) \
|
|
|
|
(strcmp(findHandle.cFileName, name) == 0) \
|
|
|
|
{ \
|
|
|
|
strcpy(oldLocation, oldDirectory); \
|
|
|
|
strcat(oldLocation, "Saves\\"); \
|
|
|
|
strcat(oldLocation, name); \
|
|
|
|
strcpy(newLocation, output); \
|
|
|
|
strcat(newLocation, "saves\\"); \
|
|
|
|
strcat(newLocation, name); \
|
|
|
|
PLATFORM_copyFile(oldLocation, newLocation); \
|
|
|
|
}
|
|
|
|
if COPY_SAVEFILE("unlock.vvv")
|
|
|
|
else if COPY_SAVEFILE("tsave.vvv")
|
|
|
|
else if COPY_SAVEFILE("qsave.vvv")
|
|
|
|
#undef COPY_SAVEFILE
|
|
|
|
}
|
|
|
|
} while (FindNextFile(hFind, &findHandle));
|
|
|
|
*/
|
|
|
|
|
|
|
|
sprintf(fileSearch, "%s\\*.vvvvvv", oldDirectory);
|
|
|
|
hFind = FindFirstFile(fileSearch, &findHandle);
|
|
|
|
if (hFind == INVALID_HANDLE_VALUE)
|
|
|
|
{
|
|
|
|
printf("Could not find directory %s\n", oldDirectory);
|
|
|
|
}
|
|
|
|
else do
|
|
|
|
{
|
|
|
|
if ((findHandle.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0)
|
|
|
|
{
|
|
|
|
strcpy(oldLocation, oldDirectory);
|
|
|
|
strcat(oldLocation, findHandle.cFileName);
|
|
|
|
strcpy(newLocation, output);
|
|
|
|
strcat(newLocation, "levels\\");
|
|
|
|
strcat(newLocation, findHandle.cFileName);
|
|
|
|
PLATFORM_copyFile(oldLocation, newLocation);
|
|
|
|
}
|
|
|
|
} while (FindNextFile(hFind, &findHandle));
|
|
|
|
#else
|
|
|
|
#error See PLATFORM_migrateSaveData
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void PLATFORM_copyFile(const char *oldLocation, const char *newLocation)
|
|
|
|
{
|
|
|
|
char *data;
|
|
|
|
long int length;
|
|
|
|
|
|
|
|
/* Read data */
|
|
|
|
FILE *file = fopen(oldLocation, "rb");
|
|
|
|
if (!file)
|
|
|
|
{
|
|
|
|
printf("Cannot open/copy %s\n", oldLocation);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
fseek(file, 0, SEEK_END);
|
|
|
|
length = ftell(file);
|
|
|
|
fseek(file, 0, SEEK_SET);
|
|
|
|
data = (char*) malloc(length);
|
|
|
|
fread(data, 1, length, file);
|
|
|
|
fclose(file);
|
|
|
|
|
|
|
|
/* Write data */
|
|
|
|
file = fopen(newLocation, "wb");
|
|
|
|
if (!file)
|
|
|
|
{
|
|
|
|
printf("Could not write to %s\n", newLocation);
|
|
|
|
free(data);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
fwrite(data, 1, length, file);
|
|
|
|
fclose(file);
|
|
|
|
free(data);
|
|
|
|
|
|
|
|
/* WTF did we just do */
|
|
|
|
printf("Copied:\n\tOld: %s\n\tNew: %s\n", oldLocation, newLocation);
|
|
|
|
}
|