Skip to content

Commit

Permalink
pcompiler-like absolute path namespace resolution
Browse files Browse the repository at this point in the history
  • Loading branch information
nikitalita committed Sep 29, 2023
1 parent 179520b commit e56c2fa
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 4 deletions.
26 changes: 24 additions & 2 deletions Caprica/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ static const std::unordered_set FAKE_SKYRIM_SCRIPTS_SET = {
"fake://skyrim/__ScriptObject.psc",
"fake://skyrim/DLC1SCWispWallScript.psc",
};

static caseless_unordered_identifier_set abs_import_dirs {};
bool handleImports(const std::vector<std::string> &f, caprica::CapricaJobManager *jobManager);

PapyrusCompilationNode *getNode(const PapyrusCompilationNode::NodeType &nodeType,
Expand Down Expand Up @@ -308,12 +308,14 @@ bool handleImports(const std::vector<std::string> &f, caprica::CapricaJobManager
}
std::cout << "Importing files..." << std::endl;
int i = 0;
abs_import_dirs.reserve(f.size());
for (auto& dir : f) {
std::string ns = "";
if (conf::Papyrus::game > GameID::Skyrim)
ns = "!!temp" + std::to_string(i++);
if (!addFilesFromDirectory(dir, true, "", jobManager, PapyrusCompilationNode::NodeType::PapyrusImport, ns))
return false;
abs_import_dirs.emplace(std::filesystem::absolute(dir).string());
}
CapricaStats::outputImportedCount();
caprica::papyrus::PapyrusCompilationContext::RenameImports(jobManager);
Expand Down Expand Up @@ -349,7 +351,27 @@ bool addSingleFile(const std::string &f,
auto ppath = caprica::FSUtils::parentPathAsRef(f);
if (ppath.compare(filename.c_str()) != 0) {
namespaceDir += ppath;
absBaseDir = absBaseDir.find(namespaceDir) != std::string::npos ? absBaseDir.substr(0, absBaseDir.find(namespaceDir)) : absBaseDir;
absBaseDir = absBaseDir.rfind(namespaceDir) != std::string::npos
? absBaseDir.substr(0, absBaseDir.rfind(namespaceDir))
: absBaseDir;
}
} else {
// PCompiler-like namespace resolution by scanning imports
bool found = false;
for (auto& dir : abs_import_dirs) {
auto relPath = std::filesystem::relative(f, dir);
// check if relpath begins with ".."
if (relPath != path && !relPath.string().starts_with("..")) {
namespaceDir += relPath.parent_path().string();
absBaseDir = dir;
found = true;
break;
}
}
if (!found) {
std::cout << "ERROR: Absolute file path '" << f << "' is not in an import directory, cannot resolve namespace!"
<< std::endl;
return false;
}
}
auto namespaceName = namespaceDir;
Expand Down
2 changes: 1 addition & 1 deletion Caprica/main_options.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -538,7 +538,7 @@ bool parseCommandLineArguments(int argc, char* argv[], caprica::CapricaJobManage

parseUserFlags(std::move(flagsPath));
} else {
if (conf::Papyrus::game == GameID::Starfield){
if (conf::Papyrus::game == GameID::Starfield) {
std::cout << "No flags specified, Using default Starfield flags file." << std::endl;
parseUserFlags("fake://Starfield/Starfield_Papyrus_Flags.flg");
}
Expand Down
1 change: 0 additions & 1 deletion Caprica/papyrus/PapyrusCompilationContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -545,7 +545,6 @@ static void renameMap(const PapyrusNamespace* child, TempRenameMap& tempRenameMa
}

void PapyrusCompilationContext::RenameImports(CapricaJobManager* jobManager) {
// TODO: Make sure that this is actually idempotent; we call it again in main()
if (conf::General::compileInParallel)
jobManager->startup((uint32_t)std::thread::hardware_concurrency());

Expand Down

0 comments on commit e56c2fa

Please sign in to comment.