2013-08-08 21:01:20 -03:00
|
|
|
/* Aseprite
|
2013-01-27 12:13:13 -03:00
|
|
|
* Copyright (C) 2001-2013 David Capello
|
2010-07-16 16:56:45 -03:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
|
|
|
|
2013-08-05 21:20:19 -03:00
|
|
|
#ifdef HAVE_CONFIG_H
|
2010-07-16 16:56:45 -03:00
|
|
|
#include "config.h"
|
2013-08-05 21:20:19 -03:00
|
|
|
#endif
|
2010-07-16 16:56:45 -03:00
|
|
|
|
|
|
|
#include <allegro.h>
|
2010-07-23 16:51:11 -03:00
|
|
|
#include <cstdio>
|
2010-07-16 16:56:45 -03:00
|
|
|
|
2013-08-05 21:20:19 -03:00
|
|
|
#include "app/resource_finder.h"
|
2013-10-14 19:58:11 -03:00
|
|
|
#include "base/fs.h"
|
|
|
|
#include "base/path.h"
|
2013-08-05 21:20:19 -03:00
|
|
|
|
|
|
|
namespace app {
|
2010-07-16 16:56:45 -03:00
|
|
|
|
|
|
|
ResourceFinder::ResourceFinder()
|
2014-04-12 13:35:55 -03:00
|
|
|
{
|
|
|
|
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()
|
2010-07-16 16:56:45 -03:00
|
|
|
{
|
|
|
|
m_current = 0;
|
2014-04-12 13:35:55 -03:00
|
|
|
return (m_current < (int)m_paths.size());
|
2010-07-16 16:56:45 -03:00
|
|
|
}
|
|
|
|
|
2014-04-12 13:35:55 -03:00
|
|
|
bool ResourceFinder::next()
|
2010-07-16 16:56:45 -03:00
|
|
|
{
|
2014-04-12 13:35:55 -03:00
|
|
|
++m_current;
|
|
|
|
return (m_current < (int)m_paths.size());
|
2010-07-16 16:56:45 -03:00
|
|
|
}
|
|
|
|
|
2014-04-12 13:35:55 -03:00
|
|
|
bool ResourceFinder::findFirst()
|
2010-07-16 16:56:45 -03:00
|
|
|
{
|
2014-04-12 13:35:55 -03:00
|
|
|
while (next()) {
|
|
|
|
PRINTF("Loading resource from \"%s\"...\n", filename().c_str());
|
|
|
|
|
|
|
|
if (base::file_exists(filename())) {
|
|
|
|
PRINTF("- OK\n");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2010-07-16 16:56:45 -03:00
|
|
|
|
2014-04-12 13:35:55 -03:00
|
|
|
PRINTF("- Resource not found.\n");
|
|
|
|
return false;
|
2010-07-16 16:56:45 -03:00
|
|
|
}
|
|
|
|
|
2013-10-14 19:58:11 -03:00
|
|
|
void ResourceFinder::addPath(const std::string& path)
|
2010-07-16 16:56:45 -03:00
|
|
|
{
|
|
|
|
m_paths.push_back(path);
|
|
|
|
}
|
|
|
|
|
2014-04-12 13:35:55 -03:00
|
|
|
void ResourceFinder::includeBinDir(const char* filename)
|
2010-07-16 16:56:45 -03:00
|
|
|
{
|
2013-10-14 19:58:11 -03:00
|
|
|
addPath(base::join_path(base::get_file_path(base::get_app_path()), filename));
|
2010-07-16 16:56:45 -03:00
|
|
|
}
|
|
|
|
|
2014-04-12 13:35:55 -03:00
|
|
|
void ResourceFinder::includeDataDir(const char* filename)
|
2010-07-16 16:56:45 -03:00
|
|
|
{
|
2013-10-14 19:58:11 -03:00
|
|
|
char buf[4096];
|
2010-07-16 16:56:45 -03:00
|
|
|
|
|
|
|
#if defined ALLEGRO_UNIX || defined ALLEGRO_MACOSX
|
|
|
|
|
2010-10-12 17:18:42 -07:00
|
|
|
// $HOME/.aseprite/filename
|
|
|
|
sprintf(buf, ".aseprite/%s", filename);
|
2014-04-12 13:35:55 -03:00
|
|
|
includeHomeDir(buf);
|
2010-07-16 16:56:45 -03:00
|
|
|
|
|
|
|
// $BINDIR/data/filename
|
|
|
|
sprintf(buf, "data/%s", filename);
|
2014-04-12 13:35:55 -03:00
|
|
|
includeBinDir(buf);
|
2010-10-12 17:18:42 -07:00
|
|
|
|
|
|
|
// $BINDIR/../share/aseprite/data/filename
|
|
|
|
sprintf(buf, "../share/aseprite/data/%s", filename);
|
2014-04-12 13:35:55 -03:00
|
|
|
includeBinDir(buf);
|
2010-07-16 16:56:45 -03:00
|
|
|
|
|
|
|
#ifdef ALLEGRO_MACOSX
|
|
|
|
// $BINDIR/aseprite.app/Contents/Resources/data/filename
|
|
|
|
sprintf(buf, "aseprite.app/Contents/Resources/data/%s", filename);
|
2014-04-12 13:35:55 -03:00
|
|
|
includeBinDir(buf);
|
2010-07-16 16:56:45 -03:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#elif defined ALLEGRO_WINDOWS || defined ALLEGRO_DOS
|
|
|
|
|
|
|
|
// $BINDIR/data/filename
|
|
|
|
sprintf(buf, "data/%s", filename);
|
2014-04-12 13:35:55 -03:00
|
|
|
includeBinDir(buf);
|
2010-07-16 16:56:45 -03:00
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
// filename
|
|
|
|
addPath(filename);
|
|
|
|
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-04-12 13:35:55 -03:00
|
|
|
void ResourceFinder::includeDocsDir(const char* filename)
|
2010-07-16 16:56:45 -03:00
|
|
|
{
|
2013-10-14 19:58:11 -03:00
|
|
|
char buf[4096];
|
2010-07-16 16:56:45 -03:00
|
|
|
|
|
|
|
#if defined ALLEGRO_UNIX || defined ALLEGRO_MACOSX
|
|
|
|
|
|
|
|
// $BINDIR/docs/filename
|
|
|
|
sprintf(buf, "docs/%s", filename);
|
2014-04-12 13:35:55 -03:00
|
|
|
includeBinDir(buf);
|
2010-07-16 16:56:45 -03:00
|
|
|
|
2010-10-12 17:18:42 -07:00
|
|
|
// $BINDIR/../share/aseprite/docs/filename
|
|
|
|
sprintf(buf, "../share/aseprite/docs/%s", filename);
|
2014-04-12 13:35:55 -03:00
|
|
|
includeBinDir(buf);
|
2012-01-05 19:45:03 -03:00
|
|
|
|
2010-07-16 16:56:45 -03:00
|
|
|
#ifdef ALLEGRO_MACOSX
|
|
|
|
// $BINDIR/aseprite.app/Contents/Resources/docs/filename
|
|
|
|
sprintf(buf, "aseprite.app/Contents/Resources/docs/%s", filename);
|
2014-04-12 13:35:55 -03:00
|
|
|
includeBinDir(buf);
|
2010-07-16 16:56:45 -03:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#elif defined ALLEGRO_WINDOWS || defined ALLEGRO_DOS
|
|
|
|
|
|
|
|
// $BINDIR/docs/filename
|
|
|
|
sprintf(buf, "docs/%s", filename);
|
2014-04-12 13:35:55 -03:00
|
|
|
includeBinDir(buf);
|
2010-07-16 16:56:45 -03:00
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
// filename
|
|
|
|
addPath(filename);
|
|
|
|
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2014-04-12 13:35:55 -03:00
|
|
|
void ResourceFinder::includeHomeDir(const char* filename)
|
2010-07-16 16:56:45 -03:00
|
|
|
{
|
|
|
|
#if defined ALLEGRO_UNIX || defined ALLEGRO_MACOSX
|
|
|
|
|
2013-10-14 19:58:11 -03:00
|
|
|
char* env = getenv("HOME");
|
|
|
|
char buf[4096];
|
2010-07-16 16:56:45 -03:00
|
|
|
|
|
|
|
if ((env) && (*env)) {
|
|
|
|
// $HOME/filename
|
|
|
|
sprintf(buf, "%s/%s", env, filename);
|
|
|
|
addPath(buf);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
PRINTF("You don't have set $HOME variable\n");
|
|
|
|
addPath(filename);
|
|
|
|
}
|
|
|
|
|
|
|
|
#elif defined ALLEGRO_WINDOWS || defined ALLEGRO_DOS
|
|
|
|
|
|
|
|
// $PREFIX/data/filename
|
2014-04-12 13:35:55 -03:00
|
|
|
includeDataDir(filename);
|
2010-07-16 16:56:45 -03:00
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
// filename
|
|
|
|
addPath(filename);
|
|
|
|
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2014-04-12 13:35:55 -03:00
|
|
|
void ResourceFinder::includeConfFile()
|
2010-07-16 16:56:45 -03:00
|
|
|
{
|
|
|
|
#if defined ALLEGRO_UNIX || defined ALLEGRO_MACOSX
|
|
|
|
|
|
|
|
// $HOME/.asepriterc
|
2014-04-12 13:35:55 -03:00
|
|
|
includeHomeDir(".asepriterc");
|
2010-07-16 16:56:45 -03:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// $BINDIR/aseprite.ini
|
2014-04-12 13:35:55 -03:00
|
|
|
includeBinDir("aseprite.ini");
|
2010-07-16 16:56:45 -03:00
|
|
|
}
|
2014-04-12 13:35:55 -03:00
|
|
|
|
2013-08-05 21:20:19 -03:00
|
|
|
} // namespace app
|