Merge pull request #18 from G-e-n-e-v-e-n-s-i-S/file-exists

Add exists_in_package script function
This commit is contained in:
GenevensiS
2024-09-26 15:22:15 +02:00
committed by GitHub
8 changed files with 79 additions and 4 deletions
+9 -1
View File
@@ -13,6 +13,7 @@
#include <util/tagged_string.hpp>
#include <util/spec_sort.hpp>
#include <util/error.hpp>
#include <util/io/package_manager.hpp>
#include <data/set.hpp>
#include <data/card.hpp>
#include <data/game.hpp>
@@ -62,7 +63,13 @@ SCRIPT_FUNCTION(error) {
queue_message(MESSAGE_ERROR, input);
}
return script_nil;
}
}
SCRIPT_FUNCTION(exists_in_package) {
SCRIPT_PARAM_C(String, input);
bool result = package_manager.existsInPackage(input);
SCRIPT_RETURN(result);
}
// ----------------------------------------------------------------------------- : Conversion
@@ -749,6 +756,7 @@ void init_script_basic_functions(Context& ctx) {
ctx.setVariable(_("trace"), script_trace);
ctx.setVariable(_("warning"), script_warning);
ctx.setVariable(_("error"), script_error);
ctx.setVariable(_("exists_in_package"), script_exists_in_package);
// conversion
ctx.setVariable(_("to_string"), script_to_string);
ctx.setVariable(_("to_int"), script_to_int);
+29
View File
@@ -186,6 +186,35 @@ public:
// ----------------------------------------------------------------------------- : Package : inside
bool Package::existsIn(const String& file) {
FileInfos::iterator it = files.find(normalize_internal_filename(file));
if (it == files.end()) {
// does it look like a relative filename?
if (filename.find(_(".mse-")) != String::npos) {
return false;
}
}
unique_ptr<wxInputStream> stream;
if (it != files.end() && it->second.wasWritten()) {
// written to this file, open the temp file
stream = make_unique<wxFileInputStream>(it->second.tempName);
}
else if (wxFileExists(filename + _("/") + file)) {
// a file in directory package
stream = make_unique<wxFileInputStream>(filename + _("/") + file);
}
else if (wxFileExists(filename) && it != files.end() && it->second.zipEntry) {
// a file in a zip archive
stream = make_unique<ZipFileInputStream>(filename, it->second.zipEntry);
}
else {
// shouldn't happen, packaged changed by someone else since opening it
return false;
}
return stream && stream->IsOk();
}
unique_ptr<wxInputStream> Package::openIn(const String& file) {
if (!file.empty() && file.GetChar(0) == _('/')) {
// absolute path, open file from another package
+7 -1
View File
@@ -132,10 +132,16 @@ public:
// --------------------------------------------------- : Managing the inside of the package
/// Check if a file is in the package.
bool existsIn(const String& file);
inline bool existsIn(const LocalFileName& file) {
return existsIn(file.fn);
}
/// Open an input stream for a file in the package.
unique_ptr<wxInputStream> openIn(const String& file);
inline unique_ptr<wxInputStream> openIn(const LocalFileName& file) {
return openIn(file.fn);
return openIn(file.fn);
}
/// Open an output stream for a file in the package.
+14
View File
@@ -95,6 +95,20 @@ void PackageManager::findMatching(const String& pattern, vector<PackagedP>& out)
}
}
bool PackageManager::existsInPackage(const String& name) {
if (!name.empty() && name.GetChar(0) == _('/')) {
// break name
size_t start = name.find_first_not_of(_("/\\"), 1); // allow "//package/name" from incorrect scripts
size_t pos = name.find_first_of(_("/\\"), start);
if (start < pos && pos != String::npos) {
// open package
PackagedP p = openAny(name.substr(start, pos - start));
return p->existsIn(name.substr(pos + 1));
}
}
throw FileNotFoundError(name, _("No package name specified, use '/package/filename'"));
}
pair<unique_ptr<wxInputStream>,Packaged*> PackageManager::openFileFromPackage(Packaged* package, const String& name) {
if (!name.empty() && name.GetChar(0) == _('/')) {
// absolute name; break name
+5 -2
View File
@@ -139,7 +139,10 @@ public:
/// Find all packages that match a filename pattern, store them in out
/** Only reads the package headers */
void findMatching(const String& pattern, vector<PackagedP>& out);
/// Check if a file exists in a package
bool existsInPackage(const String& name);
/// Open a file from a package, with a name encoded as "/package/file"
/** If 'package' is set then:
* - tries to open a relative file from the package if the name is "file"
@@ -148,7 +151,7 @@ public:
* Returns the opened file and the package it is in
*/
pair<unique_ptr<wxInputStream>,Packaged*> openFileFromPackage(Packaged* package, const String& name);
/// Get a filename to open from a package
/** WARNING: this is a bit of a hack, since not all package types support names in this way.
* It is needed for third party libraries (i.e. hunspell) that load stuff from files.