cgfxFindImage.cpp
#include "cgfxShaderCommon.h"
#include "cgfxFindImage.h"
#include <maya/MStringArray.h>
#include <maya/MGlobal.h>
#include <maya/MFileObject.h>
#include <sys/types.h>
#include <sys/stat.h>
#ifdef _WIN32
#else
# include <string.h>
# define MAX_PATH 1024
#endif
MString cgfxFindFile(const MString& name, const MString &searchpath)
{
MString file = name;
struct stat statBuf;
char path[MAX_PATH];
const char * psearchpath = searchpath.asChar();
OutputDebugString("File = ");
OutputDebugString(file.asChar());
OutputDebugString("\n");
if (stat(file.asChar(), &statBuf) == -1)
{
bool found = false;
while (found == false && psearchpath < searchpath.asChar() + searchpath.length())
{
const char * endpath = strchr(psearchpath,';');
if (endpath)
{
strncpy(path,psearchpath, endpath - psearchpath);
path[endpath - psearchpath] = '\0';
}
else
{
strcpy(path,psearchpath);
}
psearchpath += strlen(path)+1;
bool fullPath = (path[0] == '/' ||
path[0] == '\\');
if (strlen(path) > 2)
{
fullPath = fullPath ||
(path[1] == ':' &&
(path[2] == '/' ||
path[2] == '\\'));
}
MString resolvedName ;
if(name.index('/') == 0 || name.index('\\') == 0)
resolvedName = name.substring(1, name.length() - 1);
else
resolvedName = name;
if(path[strlen(path) - 1] != '/')
file = MString(path) + "/" + resolvedName;
else
file = MString(path) + resolvedName;
OutputDebugString("Try File = ");
OutputDebugString(file.asChar());
OutputDebugString("\n");
if (stat(file.asChar(), &statBuf) != -1)
found = true;
else
file = "";
}
}
OutputDebugString("Returning: ");
OutputDebugString(file.asChar());
OutputDebugString("\n");
return file;
}
MString cgfxFindFile(const MString& name, bool projectRelative)
{
MString fileName;
if (name.asChar() != NULL && strcmp(name.asChar(), ""))
{
static MString texturePath( ".");
MString workspace;
MStatus status = MGlobal::executeCommand(MString("workspace -q -rd;"),
workspace);
if ( status == MS::kSuccess)
{
texturePath += ";";
texturePath += workspace;
texturePath += ";";
texturePath += workspace;
texturePath += "/textures;";
texturePath += workspace;
texturePath += "/images;";
texturePath += workspace;
}
char * cgfxPath = getenv("CGFX_TEXTURE_PATH");
if (cgfxPath)
{
texturePath += ";";
texturePath += cgfxPath;
}
else
{
char * cgfxRoot = getenv("CGFX_ROOT");
if (cgfxRoot)
{
texturePath += ";";
texturePath += cgfxRoot;
texturePath += "/textures/2D;";
texturePath += cgfxRoot;
texturePath += "/textures/cubemaps;";
texturePath += cgfxRoot;
texturePath += "/textures/3D;";
texturePath += cgfxRoot;
texturePath += "/textures/rectangles;";
texturePath += cgfxRoot;
texturePath += "/CgFX_Textures;";
texturePath += cgfxRoot;
texturePath += "/CgFX";
}
}
OutputDebugString("CgFX texture path is: ");
OutputDebugString(texturePath.asChar());
OutputDebugString("\n");
fileName = cgfxFindFile(name, texturePath);
int hasFile = fileName.asChar() != NULL && strcmp(fileName.asChar(), "");
if (hasFile == 0)
{
int idx = name.rindex('/');
if (idx == -1)
idx = name.rindex('\\');
if (idx != -1)
{
MString filename = name.substring(idx+1,name.length()-1);
fileName = cgfxFindFile(filename, texturePath);
hasFile = fileName.asChar() != NULL && strcmp(fileName.asChar(), "");
}
}
if( hasFile && projectRelative)
{
if( fileName.length() > workspace.length() &&
workspace.length() > 0 &&
fileName.substring( 0, workspace.length() - 1) == workspace)
fileName = fileName.substring( workspace.length(), fileName.length() - 1);
}
if (hasFile == 0)
OutputDebugString("Error: file not found.\n");
}
return fileName;
}
void
cgfxGetFxIncludePath( const MString &fxFile, MStringArray &pathOptions )
{
MString option;
if (fxFile.length())
{
MFileObject fobject;
fobject.setRawFullName( fxFile );
option = MString("-I") + fobject.resolvedPath();
pathOptions.append( option );
}
char * cgfxRoot = getenv("CGFX_ROOT");
if (cgfxRoot)
{
option = MString("-I") + MString(cgfxRoot);
pathOptions.append( option );
option = MString("-I") + MString(cgfxRoot) + MString("/CgFX");
pathOptions.append( option );
}
char * mayaLocation = getenv("MAYA_LOCATION");
if (mayaLocation)
{
MString mayaCgLocation(MString(mayaLocation) + MString("/bin/Cg/"));
option = MString("-I") + mayaCgLocation;
pathOptions.append( option );
}
}