mirror of
https://github.com/amyinspace/MagicSetEditor2.git
synced 2026-06-10 04:57:00 -04:00
Add exists_in_package script function (#86)
* add exists_in_package script function * Update index.txt
This commit is contained in:
@@ -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
|
||||
@@ -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:warning]] Output a warning message.
|
||||
| [[fun:error]] Output an error message.
|
||||
| [[fun:exists_in_package]] Checks if a file exists in a package.
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -738,6 +745,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);
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -101,6 +101,7 @@ $built_in_functions = array(
|
||||
// other
|
||||
'trace' =>'',
|
||||
'assert' =>'',
|
||||
'exists_in_package' =>'',
|
||||
);
|
||||
|
||||
|
||||
@@ -232,4 +233,4 @@ function highlight_script_string($code) {
|
||||
return $code;
|
||||
}
|
||||
|
||||
?>
|
||||
?>
|
||||
|
||||
Reference in New Issue
Block a user