Fix rename park in sequence

This commit is contained in:
Ted John 2016-12-07 12:00:02 +00:00
parent ca27018231
commit 067d364002
7 changed files with 73 additions and 18 deletions

View File

@ -38,6 +38,11 @@ namespace File
return platform_file_delete(path);
}
bool Move(const utf8 * srcPath, const utf8 * dstPath)
{
return platform_file_move(srcPath, dstPath);
}
void * ReadAllBytes(const utf8 * path, size_t * length)
{
void * result = nullptr;

View File

@ -22,5 +22,6 @@ namespace File
{
bool Copy(const utf8 * srcPath, const utf8 * dstPath, bool overwrite);
bool Delete(const utf8 * path);
bool Move(const utf8 * srcPath, const utf8 * dstPath);
void * ReadAllBytes(const utf8 * path, size_t * length);
}

View File

@ -118,6 +118,12 @@ public:
uint64 index = zip_name_locate(_zip, path, 0);
zip_delete(_zip, index);
}
void RenameFile(const utf8 * path, const utf8 * newPath) override
{
uint64 index = zip_name_locate(_zip, path, 0);
zip_file_rename(_zip, index, newPath, ZIP_FL_ENC_GUESS);
}
};
namespace Zip

View File

@ -40,6 +40,7 @@ interface IZipArchive
virtual void SetFileData(const utf8 * path, void * data, size_t dataSize) abstract;
virtual void DeleteFile(const utf8 * path) abstract;
virtual void RenameFile(const utf8 * path, const utf8 * newPath) abstract;
};
enum ZIP_ACCESS

View File

@ -258,9 +258,45 @@ extern "C"
return true;
}
bool TileSequenceRenamePark(TitleSequence * seq, size_t index, const utf8 * name)
{
Guard::Assert(index < seq->NumSaves, GUARD_LINE);
utf8 * oldRelativePath = seq->Saves[index];
if (seq->IsZip)
{
IZipArchive * zip = Zip::TryOpen(seq->Path, ZIP_ACCESS_WRITE);
if (zip == nullptr)
{
Console::Error::WriteLine("Unable to open '%s'", seq->Path);
return false;
}
zip->RenameFile(oldRelativePath, name);
delete zip;
}
else
{
utf8 srcPath[MAX_PATH];
utf8 dstPath[MAX_PATH];
String::Set(srcPath, sizeof(srcPath), seq->Path);
Path::Append(srcPath, sizeof(srcPath), oldRelativePath);
String::Set(dstPath, sizeof(dstPath), seq->Path);
Path::Append(dstPath, sizeof(dstPath), name);
if (!File::Move(srcPath, dstPath))
{
Console::Error::WriteLine("Unable to move '%s' to '%s'", srcPath, dstPath);
return false;
}
}
Memory::Free(seq->Saves[index]);
seq->Saves[index] = String::Duplicate(name);
return true;
}
bool TitleSequenceRemovePark(TitleSequence * seq, size_t index)
{
Guard::Assert(seq->NumSaves > index, GUARD_LINE);
Guard::Assert(index < seq->NumSaves, GUARD_LINE);
// Delete park file
utf8 * relativePath = seq->Saves[index];

View File

@ -95,6 +95,7 @@ extern "C"
void TitleSequenceCloseParkHandle(TitleSequenceParkHandle * handle);
bool TileSequenceSave(TitleSequence * seq);
bool TileSequenceAddPark(TitleSequence * seq, const utf8 * path, const utf8 * name);
bool TileSequenceRenamePark(TitleSequence * seq, size_t index, const utf8 * name);
bool TitleSequenceRemovePark(TitleSequence * seq, size_t index);
bool TitleSequenceIsLoadCommand(const TitleCommand * command);

View File

@ -65,7 +65,7 @@ static void window_title_editor_load_sequence(size_t index);
static ITitleSequencePlayer * window_title_editor_get_player();
static bool window_title_editor_check_can_edit();
static void window_title_editor_add_park_callback(int result, const utf8 * path);
static void window_title_editor_save_sequence();
static void window_title_editor_rename_park(size_t index, const utf8 * name);
static rct_window_event_list window_title_editor_events = {
window_title_editor_close,
@ -399,7 +399,7 @@ static void window_title_editor_mouseup(rct_window *w, int widgetIndex)
if (w->selected_list_item >= (sint16)_editingTitleSequence->NumCommands) {
w->selected_list_item--;
}
window_title_editor_save_sequence();
TileSequenceSave(_editingTitleSequence);
}
}
break;
@ -422,7 +422,7 @@ static void window_title_editor_mouseup(rct_window *w, int widgetIndex)
*a = *b;
*b = tmp;
w->selected_list_item++;
window_title_editor_save_sequence();
TileSequenceSave(_editingTitleSequence);
}
}
break;
@ -435,7 +435,7 @@ static void window_title_editor_mouseup(rct_window *w, int widgetIndex)
*b = *a;
*a = tmp;
w->selected_list_item--;
window_title_editor_save_sequence();
TileSequenceSave(_editingTitleSequence);
}
}
break;
@ -641,17 +641,7 @@ static void window_title_editor_textinput(rct_window *w, int widgetIndex, char *
}
break;
case WIDX_TITLE_EDITOR_RENAME_SAVE:
// if (filename_valid_characters(text)) {
// if (!title_sequence_save_exists(_selectedTitleSequence, text)) {
// title_sequence_rename_save(_selectedTitleSequence, w->selected_list_item, text);
// TileSequenceSave(_editingTitleSequence);
// window_invalidate(w);
// } else {
// window_error_open(STR_ERROR_EXISTING_NAME, STR_NONE);
// }
// } else {
// window_error_open(STR_ERROR_INVALID_CHARACTERS, STR_NONE);
// }
window_title_editor_rename_park(w->selected_list_item, text);
break;
}
}
@ -1024,9 +1014,24 @@ static void window_title_editor_add_park_callback(int result, const utf8 * path)
TileSequenceAddPark(_editingTitleSequence, path, filename);
}
static void window_title_editor_save_sequence()
static void window_title_editor_rename_park(size_t index, const utf8 * name)
{
if (_editingTitleSequence != NULL) {
if (!filename_valid_characters(name)) {
window_error_open(STR_ERROR_INVALID_CHARACTERS, STR_NONE);
return;
}
for (size_t i = 0; i < _editingTitleSequence->NumSaves; i++) {
if (i != index) {
const utf8 * savePath = _editingTitleSequence->Saves[i];
if (_strcmpi(savePath, name) == 0) {
window_error_open(STR_ERROR_EXISTING_NAME, STR_NONE);
return;
}
}
}
if (TileSequenceRenamePark(_editingTitleSequence, index, name)) {
TileSequenceSave(_editingTitleSequence);
}
}