Add exists_in_package script function (#86)

* add exists_in_package script function

* Update index.txt
This commit is contained in:
GenevensiS
2024-09-27 04:03:27 +02:00
committed by GitHub
parent d0522f6a09
commit e72ee85525
8 changed files with 80 additions and 5 deletions
+13
View File
@@ -0,0 +1,13 @@
Function: exists_in_package
--Usage--
> exists_in_package("/package/filename")
Check if a file exists in a given package.
--Parameters--
! Parameter Type Description
| @input@ [[type:string]] Path of the file, starting from the data directory.
--Examples--
> exists_in_package("/magic-modules.mse-include/watermarks/custom_user_watermark.png") == true
+1
View File
@@ -112,3 +112,4 @@ These functions are built into the program, other [[type:function]]s can be defi
| [[fun:assert]] Check a condition for debugging purposes. | [[fun:assert]] Check a condition for debugging purposes.
| [[fun:warning]] Output a warning message. | [[fun:warning]] Output a warning message.
| [[fun:error]] Output an error message. | [[fun:error]] Output an error message.
| [[fun:exists_in_package]] Checks if a file exists in a package.
+9 -1
View File
@@ -13,6 +13,7 @@
#include <util/tagged_string.hpp> #include <util/tagged_string.hpp>
#include <util/spec_sort.hpp> #include <util/spec_sort.hpp>
#include <util/error.hpp> #include <util/error.hpp>
#include <util/io/package_manager.hpp>
#include <data/set.hpp> #include <data/set.hpp>
#include <data/card.hpp> #include <data/card.hpp>
#include <data/game.hpp> #include <data/game.hpp>
@@ -62,7 +63,13 @@ SCRIPT_FUNCTION(error) {
queue_message(MESSAGE_ERROR, input); queue_message(MESSAGE_ERROR, input);
} }
return script_nil; return script_nil;
} }
SCRIPT_FUNCTION(exists_in_package) {
SCRIPT_PARAM_C(String, input);
bool result = package_manager.existsInPackage(input);
SCRIPT_RETURN(result);
}
// ----------------------------------------------------------------------------- : Conversion // ----------------------------------------------------------------------------- : Conversion
@@ -738,6 +745,7 @@ void init_script_basic_functions(Context& ctx) {
ctx.setVariable(_("trace"), script_trace); ctx.setVariable(_("trace"), script_trace);
ctx.setVariable(_("warning"), script_warning); ctx.setVariable(_("warning"), script_warning);
ctx.setVariable(_("error"), script_error); ctx.setVariable(_("error"), script_error);
ctx.setVariable(_("exists_in_package"), script_exists_in_package);
// conversion // conversion
ctx.setVariable(_("to_string"), script_to_string); ctx.setVariable(_("to_string"), script_to_string);
ctx.setVariable(_("to_int"), script_to_int); ctx.setVariable(_("to_int"), script_to_int);
+29
View File
@@ -186,6 +186,35 @@ public:
// ----------------------------------------------------------------------------- : Package : inside // ----------------------------------------------------------------------------- : 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) { unique_ptr<wxInputStream> Package::openIn(const String& file) {
if (!file.empty() && file.GetChar(0) == _('/')) { if (!file.empty() && file.GetChar(0) == _('/')) {
// absolute path, open file from another package // absolute path, open file from another package
+7 -1
View File
@@ -132,10 +132,16 @@ public:
// --------------------------------------------------- : Managing the inside of the package // --------------------------------------------------- : 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. /// Open an input stream for a file in the package.
unique_ptr<wxInputStream> openIn(const String& file); unique_ptr<wxInputStream> openIn(const String& file);
inline unique_ptr<wxInputStream> openIn(const LocalFileName& 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. /// 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) { pair<unique_ptr<wxInputStream>,Packaged*> PackageManager::openFileFromPackage(Packaged* package, const String& name) {
if (!name.empty() && name.GetChar(0) == _('/')) { if (!name.empty() && name.GetChar(0) == _('/')) {
// absolute name; break name // 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 /// Find all packages that match a filename pattern, store them in out
/** Only reads the package headers */ /** Only reads the package headers */
void findMatching(const String& pattern, vector<PackagedP>& out); 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" /// Open a file from a package, with a name encoded as "/package/file"
/** If 'package' is set then: /** If 'package' is set then:
* - tries to open a relative file from the package if the name is "file" * - 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 * Returns the opened file and the package it is in
*/ */
pair<unique_ptr<wxInputStream>,Packaged*> openFileFromPackage(Packaged* package, const String& name); pair<unique_ptr<wxInputStream>,Packaged*> openFileFromPackage(Packaged* package, const String& name);
/// Get a filename to open from a package /// 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. /** 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. * It is needed for third party libraries (i.e. hunspell) that load stuff from files.
@@ -101,6 +101,7 @@ $built_in_functions = array(
// other // other
'trace' =>'', 'trace' =>'',
'assert' =>'', 'assert' =>'',
'exists_in_package' =>'',
); );
@@ -232,4 +233,4 @@ function highlight_script_string($code) {
return $code; return $code;
} }
?> ?>