Added Package::saveCopy, which is used to implement the write_set_file script function.

git-svn-id: svn://svn.code.sf.net/p/magicseteditor/code/trunk@1169 0fc631ac-6414-0410-93d0-97cfa31319b6
This commit is contained in:
twanvl
2008-08-24 19:48:51 +00:00
parent 2a4a896540
commit 42d40dbd1e
12 changed files with 119 additions and 32 deletions
+3 -3
View File
@@ -48,12 +48,12 @@ String export_formats(const Game& game) {
return type_strings;
}
void export_set(Set& set, const String& filename, size_t format_type) {
FileFormatP format = file_formats.at(format_type);
void export_set(Set& set, const String& filename, size_t format_index, bool is_copy) {
FileFormatP format = file_formats.at(format_index);
if (!format->canExport(*set.game)) {
throw InternalError(_("File format doesn't apply to set"));
}
format->exportSet(set, filename);
format->exportSet(set, filename, is_copy);
}
SetP import_set(String name) {
+4 -3
View File
@@ -37,7 +37,8 @@ class FileFormat : public IntrusivePtrVirtualBase {
throw InternalError(_("Import not supported by this file format"));
}
/// Export using this filter
virtual void exportSet(Set& set, const String& filename) {
/** If is_copy, then the set should not be modified */
virtual void exportSet(Set& set, const String& filename, bool is_copy = false) {
throw InternalError(_("Export not supported by this file format"));
}
};
@@ -71,9 +72,9 @@ String export_formats(const Game& game);
SetP import_set(String name);
/// Save a set under the specified name.
/** filterType specifies what format to use for saving, used as index in the list of file formats
/** format_index specifies what format to use for saving, used as index in the list of file formats
*/
void export_set(Set& set, const String& filename, size_t format_type);
void export_set(Set& set, const String& filename, size_t format_index, bool is_copy = false);
// ----------------------------------------------------------------------------- : The formats
+8 -4
View File
@@ -26,10 +26,14 @@ class MSE2FileFormat : public FileFormat {
settings.addRecentFile(filename);
return set;
}
virtual void exportSet(Set& set, const String& filename) {
set.saveAs(filename);
settings.addRecentFile(filename);
set.actions.setSavePoint();
virtual void exportSet(Set& set, const String& filename, bool is_copy) {
if (is_copy) {
set.saveCopy(filename);
} else {
set.saveAs(filename);
settings.addRecentFile(filename);
set.actions.setSavePoint();
}
}
};
+13 -1
View File
@@ -431,6 +431,18 @@ SCRIPT_FUNCTION(write_image_file) {
SCRIPT_RETURN(file);
}
SCRIPT_FUNCTION(write_set_file) {
guard_export_info(_("write_set_file"));
// output path
SCRIPT_PARAM(String, file); // file to write to
String out_path = get_export_full_path(file);
// export
SCRIPT_PARAM_C(Set*, set);
set->saveCopy(out_path); // TODO: use export_set instead?
SCRIPT_RETURN(file);
}
// ----------------------------------------------------------------------------- : Init
void init_script_export_functions(Context& ctx) {
@@ -440,5 +452,5 @@ void init_script_export_functions(Context& ctx) {
ctx.setVariable(_("copy file"), script_copy_file);
ctx.setVariable(_("write text file"), script_write_text_file);
ctx.setVariable(_("write image file"), script_write_image_file);
//ctx.setVariable(_("write set file"), script_write_set_file);//TODO
ctx.setVariable(_("write set file"), script_write_set_file);
}
+47 -17
View File
@@ -86,6 +86,18 @@ void Package::open(const String& n) {
}
}
void Package::reopen() {
if (wxDirExists(filename)) {
// make sure we have no zip open
delete zipStream; zipStream = nullptr;
delete fileStream; fileStream = nullptr;
} else {
// reopen only needed for zipfile
openZipfile();
}
}
void Package::save(bool remove_unused) {
assert(!needSaveAs());
saveAs(filename, remove_unused);
@@ -94,11 +106,21 @@ void Package::save(bool remove_unused) {
void Package::saveAs(const String& name, bool remove_unused) {
// type of package
if (wxDirExists(name)) {
saveToDirectory(name, remove_unused);
saveToDirectory(name, remove_unused, false);
} else {
saveToZipfile (name, remove_unused);
saveToZipfile (name, remove_unused, false);
}
filename = name;
removeTempFiles(remove_unused);
reopen();
}
void Package::saveCopy(const String& name) {
saveToZipfile(name, true, true);
clearKeepFlag();
}
void Package::removeTempFiles(bool remove_unused) {
// cleanup : remove temp files, remove deleted files from the list
FileInfos::iterator it = files.begin();
while (it != files.end()) {
@@ -108,9 +130,9 @@ void Package::saveAs(const String& name, bool remove_unused) {
}
if (!it->second.keep && remove_unused) {
// also remove the record of deleted files
FileInfos::iterator toRemove = it;
FileInfos::iterator to_remove = it;
++it;
files.erase(toRemove);
files.erase(to_remove);
} else {
// free zip entry, we will reopen the file
it->second.keep = false;
@@ -120,13 +142,11 @@ void Package::saveAs(const String& name, bool remove_unused) {
++it;
}
}
// reopen only needed for zipfile
if (!wxDirExists(name)) {
openZipfile();
} else {
// make sure we have no zip open
delete zipStream; zipStream = nullptr;
delete fileStream; fileStream = nullptr;
}
void Package::clearKeepFlag() {
FOR_EACH(f, files) {
f.second.keep = false;
}
}
@@ -356,7 +376,7 @@ void Package::openZipfile() {
loadZipStream();
}
void Package::saveToDirectory(const String& saveAs, bool remove_unused) {
void Package::saveToDirectory(const String& saveAs, bool remove_unused, bool is_copy) {
// write to a directory
FOR_EACH(f, files) {
if (!f.second.keep && remove_unused) {
@@ -366,7 +386,8 @@ void Package::saveToDirectory(const String& saveAs, bool remove_unused) {
} else if (f.second.wasWritten()) {
// move files that were updated
wxRemoveFile(saveAs+_("/")+f.first);
if (!wxRenameFile(f.second.tempName, saveAs+_("/")+f.first)) {
if (!(is_copy ? wxCopyFile (f.second.tempName, saveAs+_("/")+f.first)
: wxRenameFile(f.second.tempName, saveAs+_("/")+f.first))) {
throw PackageError(_ERROR_("unable to store file"));
}
} else if (filename != saveAs) {
@@ -380,7 +401,7 @@ void Package::saveToDirectory(const String& saveAs, bool remove_unused) {
}
}
void Package::saveToZipfile(const String& saveAs, bool remove_unused) {
void Package::saveToZipfile(const String& saveAs, bool remove_unused, bool is_copy) {
// create a temporary zip file name
String tempFile = saveAs + _(".tmp");
wxRemoveFile(tempFile);
@@ -395,8 +416,9 @@ void Package::saveToZipfile(const String& saveAs, bool remove_unused) {
FOR_EACH(f, files) {
if (!f.second.keep && remove_unused) {
// to remove a file simply don't copy it
} else if (f.second.zipEntry && !f.second.wasWritten()) {
} else if (!is_copy && f.second.zipEntry && !f.second.wasWritten()) {
// old file, was also in zip, not changed
// can't do this when saving a copy, since it destroys the zip entry
zipStream->CloseEntry();
newZip->CopyEntry(f.second.zipEntry, *zipStream);
f.second.zipEntry = 0;
@@ -408,8 +430,10 @@ void Package::saveToZipfile(const String& saveAs, bool remove_unused) {
}
}
// close the old file
delete zipStream; zipStream = nullptr;
delete fileStream; fileStream = nullptr;
if (!is_copy) {
delete zipStream; zipStream = nullptr;
delete fileStream; fileStream = nullptr;
}
} catch (Error e) {
// when things go wrong delete the temp file
wxRemoveFile(tempFile);
@@ -540,6 +564,12 @@ void Packaged::saveAs(const String& package, bool remove_unused) {
referenceFile(typeName());
Package::saveAs(package, remove_unused);
}
void Packaged::saveCopy(const String& package) {
WITH_DYNAMIC_ARG(writing_package, this);
writeFile(typeName(), *this, fileVersion());
referenceFile(typeName());
Package::saveCopy(package);
}
void Packaged::validate(Version) {
// a default for the short name
+9 -2
View File
@@ -83,6 +83,9 @@ class Package : public IntrusivePtrVirtualBase {
/// Saves the package under a different filename
void saveAs(const String& package, bool remove_unused = true);
/// Saves the package under a different filename, but keep the old one open
void saveCopy(const String& package);
// --------------------------------------------------- : Managing the inside of the package
@@ -175,8 +178,11 @@ class Package : public IntrusivePtrVirtualBase {
void openDirectory();
void openSubdir(const String&);
void openZipfile();
void saveToZipfile(const String&, bool);
void saveToDirectory(const String&, bool);
void reopen();
void removeTempFiles(bool remove_unused);
void clearKeepFlag();
void saveToZipfile(const String&, bool remove_unused, bool is_copy);
void saveToDirectory(const String&, bool remove_unused, bool is_copy);
FileInfos::iterator addFile(const String& file);
};
@@ -219,6 +225,7 @@ class Packaged : public Package {
void loadFully();
void save();
void saveAs(const String& package, bool remove_unused = true);
void saveCopy(const String& package);
/// Check if this package lists a dependency on the given package
/** This is done to force people to fill in the dependencies */