Refactor AddonInfo::getAddonInfo
This commit is contained in:
parent
a83b308300
commit
ba0a75881a
lib
|
@ -63,12 +63,47 @@ namespace {
|
||||||
std::string scriptFile;
|
std::string scriptFile;
|
||||||
std::string args;
|
std::string args;
|
||||||
|
|
||||||
|
std::string getFullPath(const std::string &fileName, const std::string &exename) const {
|
||||||
|
if (Path::fileExists(fileName))
|
||||||
|
return fileName;
|
||||||
|
|
||||||
|
const std::string exepath = Path::getPathFromFilename(exename);
|
||||||
|
if (Path::fileExists(exepath + fileName))
|
||||||
|
return exepath + fileName;
|
||||||
|
if (Path::fileExists(exepath + "addons/" + fileName))
|
||||||
|
return exepath + "addons/" + fileName;
|
||||||
|
|
||||||
|
#ifdef CFGDIR
|
||||||
|
if (Path::fileExists(CFGDIR + fileName))
|
||||||
|
return CFGDIR + fileName;
|
||||||
|
if (Path::fileExists(CFGDIR + ("../addons/" + fileName)))
|
||||||
|
return CFGDIR + ("../addons/" + fileName);
|
||||||
|
#endif
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
std::string getAddonInfo(const std::string &fileName, const std::string &exename) {
|
std::string getAddonInfo(const std::string &fileName, const std::string &exename) {
|
||||||
if (!endsWith(fileName, ".json", 5)) {
|
if (endsWith(fileName, ".py", 3)) {
|
||||||
name = fileName;
|
scriptFile = getFullPath(fileName, exename);
|
||||||
scriptFile = Path::getPathFromFilename(exename) + "addons/" + fileName + ".py";
|
if (scriptFile.empty())
|
||||||
|
return "Did not find addon " + fileName;
|
||||||
|
|
||||||
|
std::string::size_type pos1 = scriptFile.rfind("/");
|
||||||
|
if (pos1 == std::string::npos)
|
||||||
|
pos1 = 0;
|
||||||
|
else
|
||||||
|
pos1++;
|
||||||
|
std::string::size_type pos2 = scriptFile.rfind(".");
|
||||||
|
if (pos2 < pos1)
|
||||||
|
pos2 = std::string::npos;
|
||||||
|
name = scriptFile.substr(pos1, pos2 - pos1);
|
||||||
|
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!endsWith(fileName, ".json", 5))
|
||||||
|
return "Failed to open addon " + fileName;
|
||||||
|
|
||||||
std::ifstream fin(fileName);
|
std::ifstream fin(fileName);
|
||||||
if (!fin.is_open())
|
if (!fin.is_open())
|
||||||
return "Failed to open " + fileName;
|
return "Failed to open " + fileName;
|
||||||
|
@ -83,9 +118,8 @@ namespace {
|
||||||
for (const picojson::value &v : obj["args"].get<picojson::array>())
|
for (const picojson::value &v : obj["args"].get<picojson::array>())
|
||||||
args += " " + v.get<std::string>();
|
args += " " + v.get<std::string>();
|
||||||
}
|
}
|
||||||
name = obj["script"].get<std::string>();
|
|
||||||
scriptFile = Path::getPathFromFilename(exename) + "addons/" + name + ".py";
|
return getAddonInfo(obj["script"].get<std::string>(), exename);
|
||||||
return "";
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue