mirror of
https://github.com/aseprite/aseprite.git
synced 2025-03-14 04:19:12 +00:00
Simplify ResourceFinder API
This commit is contained in:
parent
852aba87f8
commit
8ae3b3075d
@ -76,15 +76,9 @@ void LaunchCommand::onExecute(Context* context)
|
||||
case FileInDocs:
|
||||
{
|
||||
ResourceFinder rf;
|
||||
rf.findInDocsDir(m_path.c_str());
|
||||
|
||||
while (const char* path = rf.next()) {
|
||||
if (!base::file_exists(path))
|
||||
continue;
|
||||
|
||||
launcher::open_file(path);
|
||||
break;
|
||||
}
|
||||
rf.includeDocsDir(m_path.c_str());
|
||||
if (rf.findFirst())
|
||||
launcher::open_file(rf.filename());
|
||||
}
|
||||
break;
|
||||
|
||||
|
@ -74,11 +74,11 @@ void ConvolutionMatrixStock::reloadStock()
|
||||
|
||||
for (i=0; names[i]; i++) {
|
||||
ResourceFinder rf;
|
||||
rf.findInDataDir(names[i]);
|
||||
rf.includeDataDir(names[i]);
|
||||
|
||||
while (const char* path = rf.next()) {
|
||||
while (rf.next()) {
|
||||
// Open matrices stock file
|
||||
base::FileHandle f = base::open_file(path, "r");
|
||||
base::FileHandle f = base::open_file(rf.filename(), "r");
|
||||
if (!f)
|
||||
continue;
|
||||
|
||||
|
@ -43,27 +43,13 @@ GuiXml::GuiXml()
|
||||
PRINTF("Loading gui.xml file...");
|
||||
|
||||
ResourceFinder rf;
|
||||
rf.findInDataDir("gui.xml");
|
||||
rf.includeDataDir("gui.xml");
|
||||
if (!rf.findFirst())
|
||||
throw base::Exception("gui.xml was not found");
|
||||
|
||||
while (const char* path = rf.next()) {
|
||||
PRINTF("Trying to load GUI definitions from \"%s\"...\n", path);
|
||||
|
||||
// If the file does not exist, just ignore this location (it was
|
||||
// suggested by the ResourceFinder class).
|
||||
if (!base::file_exists(path))
|
||||
continue;
|
||||
|
||||
PRINTF(" - \"%s\" found\n", path);
|
||||
|
||||
// Load the XML file. As we've already checked "path" existence,
|
||||
// in a case of exception we should show the error and stop.
|
||||
m_doc = app::open_xml(path);
|
||||
|
||||
// Done, we load the file successfully.
|
||||
return;
|
||||
}
|
||||
|
||||
throw base::Exception("gui.xml was not found");
|
||||
// Load the XML file. As we've already checked "path" existence,
|
||||
// in a case of exception we should show the error and stop.
|
||||
m_doc = app::open_xml(rf.filename());
|
||||
}
|
||||
|
||||
base::string GuiXml::version()
|
||||
|
@ -38,22 +38,18 @@ static std::string config_filename;
|
||||
ConfigModule::ConfigModule()
|
||||
{
|
||||
ResourceFinder rf;
|
||||
rf.findConfigurationFile();
|
||||
rf.includeConfFile();
|
||||
|
||||
config_filename.clear();
|
||||
|
||||
// Search the configuration file from first to last path
|
||||
while (const char* path = rf.next()) {
|
||||
if (base::file_exists(path)) {
|
||||
config_filename = path;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (rf.findFirst())
|
||||
config_filename = rf.filename();
|
||||
|
||||
// If the file wasn't found, we will create configuration file
|
||||
// in the first path
|
||||
if (config_filename[0] == 0 && rf.first())
|
||||
config_filename = rf.first();
|
||||
config_filename = rf.filename();
|
||||
|
||||
override_config_file(config_filename.c_str());
|
||||
}
|
||||
|
@ -59,8 +59,9 @@ LoggerModule::LoggerModule(bool verbose)
|
||||
|
||||
#ifdef NEED_LOG
|
||||
ResourceFinder rf;
|
||||
rf.findInBinDir("aseprite.log");
|
||||
log_filename = rf.first();
|
||||
rf.includeBinDir("aseprite.log");
|
||||
if (rf.first())
|
||||
log_filename = rf.filename();
|
||||
#endif
|
||||
}
|
||||
|
||||
|
@ -30,24 +30,41 @@
|
||||
namespace app {
|
||||
|
||||
ResourceFinder::ResourceFinder()
|
||||
{
|
||||
m_current = -1;
|
||||
}
|
||||
|
||||
const std::string& ResourceFinder::filename() const
|
||||
{
|
||||
// Throw an exception if we are out of bounds
|
||||
return m_paths.at(m_current);
|
||||
}
|
||||
|
||||
bool ResourceFinder::first()
|
||||
{
|
||||
m_current = 0;
|
||||
return (m_current < (int)m_paths.size());
|
||||
}
|
||||
|
||||
const char* ResourceFinder::first()
|
||||
bool ResourceFinder::next()
|
||||
{
|
||||
if (!m_paths.empty())
|
||||
return m_paths[0].c_str();
|
||||
else
|
||||
return NULL;
|
||||
++m_current;
|
||||
return (m_current < (int)m_paths.size());
|
||||
}
|
||||
|
||||
const char* ResourceFinder::next()
|
||||
bool ResourceFinder::findFirst()
|
||||
{
|
||||
if (m_current == (int)m_paths.size())
|
||||
return NULL;
|
||||
while (next()) {
|
||||
PRINTF("Loading resource from \"%s\"...\n", filename().c_str());
|
||||
|
||||
return m_paths[m_current++].c_str();
|
||||
if (base::file_exists(filename())) {
|
||||
PRINTF("- OK\n");
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
PRINTF("- Resource not found.\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
void ResourceFinder::addPath(const std::string& path)
|
||||
@ -55,12 +72,12 @@ void ResourceFinder::addPath(const std::string& path)
|
||||
m_paths.push_back(path);
|
||||
}
|
||||
|
||||
void ResourceFinder::findInBinDir(const char* filename)
|
||||
void ResourceFinder::includeBinDir(const char* filename)
|
||||
{
|
||||
addPath(base::join_path(base::get_file_path(base::get_app_path()), filename));
|
||||
}
|
||||
|
||||
void ResourceFinder::findInDataDir(const char* filename)
|
||||
void ResourceFinder::includeDataDir(const char* filename)
|
||||
{
|
||||
char buf[4096];
|
||||
|
||||
@ -68,27 +85,27 @@ void ResourceFinder::findInDataDir(const char* filename)
|
||||
|
||||
// $HOME/.aseprite/filename
|
||||
sprintf(buf, ".aseprite/%s", filename);
|
||||
findInHomeDir(buf);
|
||||
includeHomeDir(buf);
|
||||
|
||||
// $BINDIR/data/filename
|
||||
sprintf(buf, "data/%s", filename);
|
||||
findInBinDir(buf);
|
||||
includeBinDir(buf);
|
||||
|
||||
// $BINDIR/../share/aseprite/data/filename
|
||||
sprintf(buf, "../share/aseprite/data/%s", filename);
|
||||
findInBinDir(buf);
|
||||
includeBinDir(buf);
|
||||
|
||||
#ifdef ALLEGRO_MACOSX
|
||||
// $BINDIR/aseprite.app/Contents/Resources/data/filename
|
||||
sprintf(buf, "aseprite.app/Contents/Resources/data/%s", filename);
|
||||
findInBinDir(buf);
|
||||
includeBinDir(buf);
|
||||
#endif
|
||||
|
||||
#elif defined ALLEGRO_WINDOWS || defined ALLEGRO_DOS
|
||||
|
||||
// $BINDIR/data/filename
|
||||
sprintf(buf, "data/%s", filename);
|
||||
findInBinDir(buf);
|
||||
includeBinDir(buf);
|
||||
|
||||
#else
|
||||
|
||||
@ -99,7 +116,7 @@ void ResourceFinder::findInDataDir(const char* filename)
|
||||
}
|
||||
|
||||
|
||||
void ResourceFinder::findInDocsDir(const char* filename)
|
||||
void ResourceFinder::includeDocsDir(const char* filename)
|
||||
{
|
||||
char buf[4096];
|
||||
|
||||
@ -107,23 +124,23 @@ void ResourceFinder::findInDocsDir(const char* filename)
|
||||
|
||||
// $BINDIR/docs/filename
|
||||
sprintf(buf, "docs/%s", filename);
|
||||
findInBinDir(buf);
|
||||
includeBinDir(buf);
|
||||
|
||||
// $BINDIR/../share/aseprite/docs/filename
|
||||
sprintf(buf, "../share/aseprite/docs/%s", filename);
|
||||
findInBinDir(buf);
|
||||
includeBinDir(buf);
|
||||
|
||||
#ifdef ALLEGRO_MACOSX
|
||||
// $BINDIR/aseprite.app/Contents/Resources/docs/filename
|
||||
sprintf(buf, "aseprite.app/Contents/Resources/docs/%s", filename);
|
||||
findInBinDir(buf);
|
||||
includeBinDir(buf);
|
||||
#endif
|
||||
|
||||
#elif defined ALLEGRO_WINDOWS || defined ALLEGRO_DOS
|
||||
|
||||
// $BINDIR/docs/filename
|
||||
sprintf(buf, "docs/%s", filename);
|
||||
findInBinDir(buf);
|
||||
includeBinDir(buf);
|
||||
|
||||
#else
|
||||
|
||||
@ -133,7 +150,7 @@ void ResourceFinder::findInDocsDir(const char* filename)
|
||||
#endif
|
||||
}
|
||||
|
||||
void ResourceFinder::findInHomeDir(const char* filename)
|
||||
void ResourceFinder::includeHomeDir(const char* filename)
|
||||
{
|
||||
#if defined ALLEGRO_UNIX || defined ALLEGRO_MACOSX
|
||||
|
||||
@ -153,7 +170,7 @@ void ResourceFinder::findInHomeDir(const char* filename)
|
||||
#elif defined ALLEGRO_WINDOWS || defined ALLEGRO_DOS
|
||||
|
||||
// $PREFIX/data/filename
|
||||
findInDataDir(filename);
|
||||
includeDataDir(filename);
|
||||
|
||||
#else
|
||||
|
||||
@ -163,17 +180,17 @@ void ResourceFinder::findInHomeDir(const char* filename)
|
||||
#endif
|
||||
}
|
||||
|
||||
void ResourceFinder::findConfigurationFile()
|
||||
void ResourceFinder::includeConfFile()
|
||||
{
|
||||
#if defined ALLEGRO_UNIX || defined ALLEGRO_MACOSX
|
||||
|
||||
// $HOME/.asepriterc
|
||||
findInHomeDir(".asepriterc");
|
||||
includeHomeDir(".asepriterc");
|
||||
|
||||
#endif
|
||||
|
||||
// $BINDIR/aseprite.ini
|
||||
findInBinDir("aseprite.ini");
|
||||
includeBinDir("aseprite.ini");
|
||||
}
|
||||
|
||||
|
||||
} // namespace app
|
||||
|
@ -25,20 +25,36 @@
|
||||
|
||||
namespace app {
|
||||
|
||||
// Helper class to find configuration files in different directories
|
||||
// in a priority order (e.g. first in the $HOME directory, then in
|
||||
// data/ directory, etc.).
|
||||
class ResourceFinder {
|
||||
public:
|
||||
ResourceFinder();
|
||||
|
||||
const char* first();
|
||||
const char* next();
|
||||
// Returns the current possible path. You cannot call this
|
||||
// function if you haven't call first() or next() before.
|
||||
const std::string& filename() const;
|
||||
|
||||
// Goes to the first option in the list of possible paths.
|
||||
// Returns true if there is (at least) one option available
|
||||
// (m_paths.size() != 0).
|
||||
bool first();
|
||||
|
||||
// Goes to next possible path.
|
||||
bool next();
|
||||
|
||||
// Iterates over all possible paths and returns true if the file
|
||||
// is exists. Returns the first existent file.
|
||||
bool findFirst();
|
||||
|
||||
// These functions add possible full paths to find files.
|
||||
void addPath(const std::string& path);
|
||||
|
||||
void findInBinDir(const char* filename);
|
||||
void findInDataDir(const char* filename);
|
||||
void findInDocsDir(const char* filename);
|
||||
void findInHomeDir(const char* filename);
|
||||
void findConfigurationFile();
|
||||
void includeBinDir(const char* filename);
|
||||
void includeDataDir(const char* filename);
|
||||
void includeDocsDir(const char* filename);
|
||||
void includeHomeDir(const char* filename);
|
||||
void includeConfFile();
|
||||
|
||||
private:
|
||||
// Disable copy
|
||||
|
@ -376,25 +376,21 @@ void SkinTheme::reload_skin()
|
||||
|
||||
// Load the skin sheet
|
||||
std::string sheet_filename("skins/" + m_selected_skin + "/sheet.png");
|
||||
{
|
||||
ResourceFinder rf;
|
||||
rf.findInDataDir(sheet_filename.c_str());
|
||||
|
||||
while (const char* path = rf.next()) {
|
||||
if (base::file_exists(path)) {
|
||||
int old_color_conv = _color_conv;
|
||||
set_color_conversion(COLORCONV_NONE);
|
||||
ResourceFinder rf;
|
||||
rf.includeDataDir(sheet_filename.c_str());
|
||||
if (!rf.findFirst())
|
||||
throw base::Exception("File %s not found", sheet_filename.c_str());
|
||||
|
||||
PALETTE pal;
|
||||
m_sheet_bmp = load_png(path, pal);
|
||||
int old_color_conv = _color_conv;
|
||||
set_color_conversion(COLORCONV_NONE);
|
||||
|
||||
set_color_conversion(old_color_conv);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
PALETTE pal;
|
||||
m_sheet_bmp = load_png(rf.filename().c_str(), pal);
|
||||
if (!m_sheet_bmp)
|
||||
throw base::Exception("Error loading %s file", sheet_filename.c_str());
|
||||
|
||||
set_color_conversion(old_color_conv);
|
||||
}
|
||||
|
||||
void SkinTheme::reload_fonts()
|
||||
@ -425,16 +421,11 @@ void SkinTheme::onRegenerate()
|
||||
// Load the skin XML
|
||||
std::string xml_filename = "skins/" + m_selected_skin + "/skin.xml";
|
||||
ResourceFinder rf;
|
||||
rf.findInDataDir(xml_filename.c_str());
|
||||
|
||||
const char* path;
|
||||
while ((path = rf.next()) &&
|
||||
!base::file_exists(path)) {
|
||||
}
|
||||
if (!path) // not found
|
||||
rf.includeDataDir(xml_filename.c_str());
|
||||
if (!rf.findFirst())
|
||||
return;
|
||||
|
||||
XmlDocumentRef doc = open_xml(path);
|
||||
XmlDocumentRef doc = open_xml(rf.filename());
|
||||
TiXmlHandle handle(doc);
|
||||
|
||||
// Load colors
|
||||
@ -2411,22 +2402,23 @@ void SkinTheme::paintIcon(Widget* widget, Graphics* g, IButtonIcon* iconInterfac
|
||||
|
||||
FONT* SkinTheme::loadFont(const char* userFont, const std::string& path)
|
||||
{
|
||||
// Directories
|
||||
ResourceFinder rf;
|
||||
|
||||
// Directories to find the font
|
||||
const char* user_font = get_config_string("Options", userFont, "");
|
||||
if (user_font && *user_font)
|
||||
rf.addPath(user_font);
|
||||
|
||||
rf.findInDataDir(path.c_str());
|
||||
rf.includeDataDir(path.c_str());
|
||||
|
||||
// Try to load the font
|
||||
while (const char* path = rf.next()) {
|
||||
FONT* font = ji_font_load(path);
|
||||
if (font) {
|
||||
if (ji_font_is_scalable(font))
|
||||
ji_font_set_size(font, 8*jguiscale());
|
||||
return font;
|
||||
while (rf.next()) {
|
||||
FONT* f = ji_font_load(rf.filename().c_str());
|
||||
if (f) {
|
||||
if (ji_font_is_scalable(f))
|
||||
ji_font_set_size(f, 8*jguiscale());
|
||||
|
||||
return f;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -39,11 +39,11 @@ WebServer::WebServer()
|
||||
: m_webServer(NULL)
|
||||
{
|
||||
ResourceFinder rf;
|
||||
rf.findInDataDir("www");
|
||||
rf.includeDataDir("www");
|
||||
|
||||
while (const char* path = rf.next()) {
|
||||
if (base::directory_exists(path)) {
|
||||
m_wwwpath = path;
|
||||
while (rf.next()) {
|
||||
if (base::directory_exists(rf.filename())) {
|
||||
m_wwwpath = rf.filename();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
@ -72,28 +72,18 @@ Widget* WidgetLoader::loadWidget(const char* fileName, const char* widgetId)
|
||||
{
|
||||
Widget* widget;
|
||||
std::string buf;
|
||||
bool found = false;
|
||||
|
||||
ResourceFinder rf;
|
||||
|
||||
rf.addPath(fileName);
|
||||
|
||||
buf = "widgets/";
|
||||
buf += fileName;
|
||||
rf.findInDataDir(buf.c_str());
|
||||
rf.includeDataDir(buf.c_str());
|
||||
|
||||
while (const char* path = rf.next()) {
|
||||
if (base::file_exists(path)) {
|
||||
buf = path;
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!found)
|
||||
if (!rf.findFirst())
|
||||
throw WidgetNotFound(widgetId);
|
||||
|
||||
widget = loadWidgetFromXmlFile(buf, widgetId);
|
||||
widget = loadWidgetFromXmlFile(rf.filename(), widgetId);
|
||||
if (!widget)
|
||||
throw WidgetNotFound(widgetId);
|
||||
|
||||
|
@ -63,12 +63,13 @@ namespace {
|
||||
}
|
||||
};
|
||||
|
||||
bool get_memory_dump_filename(std::string& filename)
|
||||
bool getMemoryDumpFilename(std::string& filename)
|
||||
{
|
||||
#ifdef WIN32
|
||||
app::ResourceFinder rf;
|
||||
rf.findInBinDir("aseprite-memory.dmp");
|
||||
filename = rf.first();
|
||||
rf.includeBinDir("aseprite-memory.dmp");
|
||||
if (rf.first())
|
||||
filename = rf.filename();
|
||||
return true;
|
||||
#else
|
||||
return false;
|
||||
@ -97,7 +98,7 @@ int app_main(int argc, char* argv[])
|
||||
// Change the name of the memory dump file
|
||||
{
|
||||
std::string filename;
|
||||
if (get_memory_dump_filename(filename))
|
||||
if (getMemoryDumpFilename(filename))
|
||||
memoryDump.setFileName(filename);
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user