| 258 | // *********************************************************************** |
| 259 | |
| 260 | struct LuaFileResolver : Luau::FileResolver { |
| 261 | std::optional<Luau::SourceCode> readSource(const Luau::ModuleName& name) override { |
| 262 | Luau::SourceCode::Type sourceType; |
| 263 | std::optional<std::string> source = std::nullopt; |
| 264 | |
| 265 | File file = OpenFile(String(name.c_str()), FM_READ); |
| 266 | defer(CloseFile(file)); |
| 267 | |
| 268 | if (!IsValid(file)) |
| 269 | return std::nullopt; |
| 270 | |
| 271 | i64 sourceSize = GetFileSize(file); |
| 272 | std::string result(sourceSize, 0); |
| 273 | ReadFromFile(file, result.data(), sourceSize); |
| 274 | |
| 275 | source = result; |
| 276 | sourceType = Luau::SourceCode::Module; |
| 277 | |
| 278 | if (!source) |
| 279 | return std::nullopt; |
| 280 | |
| 281 | return Luau::SourceCode{*source, sourceType}; |
| 282 | } |
| 283 | |
| 284 | std::optional<Luau::ModuleInfo> resolveModule(const Luau::ModuleInfo* context, Luau::AstExpr* node) override { |
| 285 | if (Luau::AstExprConstantString* expr = node->as<Luau::AstExprConstantString>()) { |
| 286 | StringBuilder builder(g_pArenaFrame); |
| 287 | builder.AppendChars(expr->value.data, expr->value.size); |
| 288 | |
| 289 | // we expect include calls to give the exact filename |
| 290 | // so we won't construct any extensions or what have you |
| 291 | String includeName = builder.CreateString(g_pArenaFrame, false); |
| 292 | String realPath; |
| 293 | VFSPathToRealPath(includeName, realPath, g_pArenaFrame); |
| 294 | |
| 295 | if (!FileExists(realPath)) { |
| 296 | // check if the include is in system/shared/ (assuming it's a relative path) |
| 297 | if (includeName[0] != '/') { |
| 298 | realPath = TempPrint("system/shared/%S", includeName); |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | Luau::ModuleName name = std::string(realPath.pData, realPath.length); |
| 303 | return {{name}}; |
| 304 | } |
| 305 | |
| 306 | return std::nullopt; |
| 307 | } |
| 308 | |
| 309 | std::string getHumanReadableModuleName(const Luau::ModuleName& name) const override { |
| 310 | String vfsBasePath = TempPrint("system/%s/", Cpu::GetAppName().pData); |
| 311 | std::string newName = std::string(name.c_str()+vfsBasePath.length, name.size()-vfsBasePath.length); |
| 312 | return newName; |
| 313 | } |
| 314 | }; |
| 315 | |
| 316 | // *********************************************************************** |
| 317 | |