void mame_machine_manager::start_luaengine() { if (options().plugins()) { path_iterator iter(options().plugins_path()); std::string pluginpath; while (iter.next(pluginpath)) { m_plugins->parse_json(pluginpath); } std::vector<std::string> include = split(options().plugin(),','); std::vector<std::string> exclude = split(options().no_plugin(),','); { // parse the file std::string error; // attempt to open the output file emu_file file(options().ini_path(), OPEN_FLAG_READ); if (file.open("plugin.ini") == osd_file::error::NONE) { bool result = m_plugins->parse_ini_file((util::core_file&)file, OPTION_PRIORITY_MAME_INI, OPTION_PRIORITY_DRIVER_INI, error); if (!result) osd_printf_error("**Error loading plugin.ini**"); } } for (auto &curentry : *m_plugins) { if (!curentry.is_header()) { if (std::find(include.begin(), include.end(), curentry.name()) != include.end()) { std::string error_string; m_plugins->set_value(curentry.name(), "1", OPTION_PRIORITY_CMDLINE, error_string); } if (std::find(exclude.begin(), exclude.end(), curentry.name()) != exclude.end()) { std::string error_string; m_plugins->set_value(curentry.name(), "0", OPTION_PRIORITY_CMDLINE, error_string); } } } } m_lua->initialize(); { emu_file file(options().plugins_path(), OPEN_FLAG_READ); osd_file::error filerr = file.open("boot.lua"); if (filerr == osd_file::error::NONE) { std::string exppath; osd_subst_env(exppath, std::string(file.fullpath())); m_lua->load_script(exppath.c_str()); } } }
osd_file::error osd_file::open(std::string const &path, std::uint32_t openflags, ptr &file, std::uint64_t &filesize) { std::string dst; if (posix_check_socket_path(path)) return posix_open_socket(path, openflags, file, filesize); else if (posix_check_ptty_path(path)) return posix_open_ptty(openflags, file, filesize, dst); // select the file open modes int access; if (openflags & OPEN_FLAG_WRITE) { access = (openflags & OPEN_FLAG_READ) ? O_RDWR : O_WRONLY; access |= (openflags & OPEN_FLAG_CREATE) ? (O_CREAT | O_TRUNC) : 0; } else if (openflags & OPEN_FLAG_READ) { access = O_RDONLY; } else { return error::INVALID_ACCESS; } #if defined(WIN32) access |= O_BINARY; #endif // convert the path into something compatible dst = path; #if defined(WIN32) for (auto it = dst.begin(); it != dst.end(); ++it) *it = (INVPATHSEPCH == *it) ? PATHSEPCH : *it; #endif osd_subst_env(dst, dst); // attempt to open the file int fd = -1; #if defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__bsdi__) || defined(__DragonFly__) || defined(__HAIKU__) || defined(WIN32) || defined(SDLMAME_NO64BITIO) || defined(__ANDROID__) fd = ::open(dst.c_str(), access, 0666); #else fd = ::open64(dst.c_str(), access, 0666); #endif if (fd < 0) { // create the path if necessary if ((openflags & OPEN_FLAG_CREATE) && (openflags & OPEN_FLAG_CREATE_PATHS)) { auto const pathsep = dst.rfind(PATHSEPCH); if (pathsep != std::string::npos) { // create the path up to the file osd_file::error const error = create_path_recursive(dst.substr(0, pathsep)); // attempt to reopen the file if (error == osd_file::error::NONE) { #if defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__bsdi__) || defined(__DragonFly__) || defined(__HAIKU__) || defined(WIN32) || defined(SDLMAME_NO64BITIO) || defined(__ANDROID__) fd = ::open(dst.c_str(), access, 0666); #else fd = ::open64(dst.c_str(), access, 0666); #endif } } } // if we still failed, clean up and osd_free if (fd < 0) { return errno_to_file_error(errno); } } // get the file size #if defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__bsdi__) || defined(__DragonFly__) || defined(__HAIKU__) || defined(WIN32) || defined(SDLMAME_NO64BITIO) || defined(__ANDROID__) struct stat st; if (::fstat(fd, &st) < 0) #else struct stat64 st; if (::fstat64(fd, &st) < 0) #endif { int const error = errno; ::close(fd); return errno_to_file_error(error); } filesize = std::uint64_t(std::make_unsigned_t<decltype(st.st_size)>(st.st_size)); try { file = std::make_unique<posix_osd_file>(fd); return error::NONE; } catch (...) { ::close(fd); return error::OUT_OF_MEMORY; } }
file_error osd_open(const char *orig_path, UINT32 openflags, osd_file **file, UINT64 *filesize) { file_error filerr = FILERR_NONE; char *path = nullptr; osd_subst_env(&path, orig_path); // convert path to TCHAR TCHAR *t_path = tstring_from_utf8(path); if (t_path == NULL) { filerr = FILERR_OUT_OF_MEMORY; goto error; } // allocate a file object, plus space for the converted filename *file = (osd_file *)osd_malloc_array(sizeof(**file) + sizeof(TCHAR) * _tcslen(t_path)); if (*file == NULL) { filerr = FILERR_OUT_OF_MEMORY; goto error; } memset(*file, 0x00, sizeof(**file) + sizeof(TCHAR) * _tcslen(t_path)); if (win_check_socket_path(path)) { (*file)->type = WINFILE_SOCKET; filerr = win_open_socket(path, openflags, file, filesize); goto error; } if (strncmp(path, winfile_ptty_identifier, strlen(winfile_ptty_identifier)) == 0) { (*file)->type = WINFILE_PTTY; filerr = win_open_ptty(path, openflags, file, filesize); goto error; } (*file)->type = WINFILE_FILE; // convert the path into something Windows compatible { TCHAR *dst = (*file)->filename; for (TCHAR const *src = t_path; *src != 0; src++) *dst++ = *src;//(*src == '/') ? '\\' : *src; *dst++ = 0; } // select the file open modes DWORD disposition, access, sharemode; if (openflags & OPEN_FLAG_WRITE) { disposition = (!is_path_to_physical_drive(path) && (openflags & OPEN_FLAG_CREATE)) ? CREATE_ALWAYS : OPEN_EXISTING; access = (openflags & OPEN_FLAG_READ) ? (GENERIC_READ | GENERIC_WRITE) : GENERIC_WRITE; sharemode = FILE_SHARE_READ; } else if (openflags & OPEN_FLAG_READ) { disposition = OPEN_EXISTING; access = GENERIC_READ; sharemode = FILE_SHARE_READ; } else { filerr = FILERR_INVALID_ACCESS; goto error; } // attempt to open the file (*file)->handle = CreateFile((*file)->filename, access, sharemode, NULL, disposition, 0, NULL); if ((*file)->handle == INVALID_HANDLE_VALUE) { DWORD error = GetLastError(); // create the path if necessary if (error == ERROR_PATH_NOT_FOUND && (openflags & OPEN_FLAG_CREATE) && (openflags & OPEN_FLAG_CREATE_PATHS)) { TCHAR *pathsep = _tcsrchr((*file)->filename, '\\'); if (pathsep != NULL) { // create the path up to the file *pathsep = 0; error = create_path_recursive((*file)->filename); *pathsep = '\\'; // attempt to reopen the file if (error == NO_ERROR) (*file)->handle = CreateFile((*file)->filename, access, sharemode, NULL, disposition, 0, NULL); } } // if we still failed, clean up and free if ((*file)->handle == INVALID_HANDLE_VALUE) { filerr = win_error_to_file_error(error); goto error; } } // get the file size DWORD upper; *filesize = GetFileSize((*file)->handle, &upper); *filesize |= (UINT64)upper << 32; error: // cleanup if (filerr != FILERR_NONE && *file != NULL) { osd_free(*file); *file = NULL; } osd_free(t_path); osd_free(path); return filerr; }
osd_file::error osd_file::open(std::string const &orig_path, UINT32 openflags, ptr &file, std::uint64_t &filesize) { std::string path; try { osd_subst_env(path, orig_path); } catch (...) { return error::OUT_OF_MEMORY; } if (win_check_socket_path(path)) return win_open_socket(path, openflags, file, filesize); else if (win_check_ptty_path(path)) return win_open_ptty(path, openflags, file, filesize); // convert path to TCHAR TCHAR *t_path = tstring_from_utf8(path.c_str()); osd_disposer<TCHAR> t_path_disposer(t_path); if (!t_path) return error::OUT_OF_MEMORY; // convert the path into something Windows compatible for (TCHAR *src = t_path; *src != 0; src++) *src = /* ('/' == *src) ? '\\' : */ *src; // select the file open modes DWORD disposition, access, sharemode; if (openflags & OPEN_FLAG_WRITE) { disposition = (!is_path_to_physical_drive(path.c_str()) && (openflags & OPEN_FLAG_CREATE)) ? CREATE_ALWAYS : OPEN_EXISTING; access = (openflags & OPEN_FLAG_READ) ? (GENERIC_READ | GENERIC_WRITE) : GENERIC_WRITE; sharemode = FILE_SHARE_READ; } else if (openflags & OPEN_FLAG_READ) { disposition = OPEN_EXISTING; access = GENERIC_READ; sharemode = FILE_SHARE_READ; } else { return error::INVALID_ACCESS; } // attempt to open the file HANDLE h = CreateFile(t_path, access, sharemode, nullptr, disposition, 0, nullptr); if (INVALID_HANDLE_VALUE == h) { DWORD err = GetLastError(); // create the path if necessary if ((ERROR_PATH_NOT_FOUND == err) && (openflags & OPEN_FLAG_CREATE) && (openflags & OPEN_FLAG_CREATE_PATHS)) { TCHAR *pathsep = _tcsrchr(t_path, '\\'); if (pathsep != nullptr) { // create the path up to the file *pathsep = 0; err = create_path_recursive(t_path); *pathsep = '\\'; // attempt to reopen the file if (err == NO_ERROR) { h = CreateFile(t_path, access, sharemode, nullptr, disposition, 0, nullptr); err = GetLastError(); } } } // if we still failed, clean up and free if (INVALID_HANDLE_VALUE == h) return win_error_to_file_error(err); } // get the file size DWORD upper, lower; lower = GetFileSize(h, &upper); if (INVALID_FILE_SIZE == lower) { DWORD const err = GetLastError(); if (NO_ERROR != err) { CloseHandle(h); return win_error_to_file_error(err); } } try { file = std::make_unique<win_osd_file>(h); filesize = (std::uint64_t(upper) << 32) | lower; return error::NONE; } catch (...) { CloseHandle(h); return error::OUT_OF_MEMORY; } }
osd_file::error osd_file::open(std::string const &orig_path, uint32_t openflags, ptr &file, std::uint64_t &filesize) { std::string path; try { osd_subst_env(path, orig_path); } catch (...) { return error::OUT_OF_MEMORY; } if (win_check_socket_path(path)) return win_open_socket(path, openflags, file, filesize); else if (win_check_ptty_path(path)) return win_open_ptty(path, openflags, file, filesize); // convert path to TCHAR osd::text::tstring t_path = osd::text::to_tstring(path); // convert the path into something Windows compatible (the actual interesting part appears // to have been commented out???) for (auto iter = t_path.begin(); iter != t_path.end(); iter++) *iter = /* ('/' == *iter) ? '\\' : */ *iter; // select the file open modes DWORD disposition, access, sharemode; if (openflags & OPEN_FLAG_WRITE) { disposition = (!is_path_to_physical_drive(path.c_str()) && (openflags & OPEN_FLAG_CREATE)) ? CREATE_ALWAYS : OPEN_EXISTING; access = (openflags & OPEN_FLAG_READ) ? (GENERIC_READ | GENERIC_WRITE) : GENERIC_WRITE; sharemode = FILE_SHARE_READ; } else if (openflags & OPEN_FLAG_READ) { disposition = OPEN_EXISTING; access = GENERIC_READ; sharemode = FILE_SHARE_READ; } else { return error::INVALID_ACCESS; } // attempt to open the file HANDLE h = CreateFile(t_path.c_str(), access, sharemode, nullptr, disposition, 0, nullptr); if (INVALID_HANDLE_VALUE == h) { DWORD err = GetLastError(); // create the path if necessary if ((ERROR_PATH_NOT_FOUND == err) && (openflags & OPEN_FLAG_CREATE) && (openflags & OPEN_FLAG_CREATE_PATHS)) { auto pathsep = t_path.rfind('\\'); if (pathsep != decltype(t_path)::npos) { // create the path up to the file t_path[pathsep] = 0; err = create_path_recursive(&t_path[0]); t_path[pathsep] = '\\'; // attempt to reopen the file if (err == NO_ERROR) { h = CreateFile(t_path.c_str(), access, sharemode, nullptr, disposition, 0, nullptr); err = GetLastError(); } } } // if we still failed, clean up and free if (INVALID_HANDLE_VALUE == h) return win_error_to_file_error(err); } // get the file size DWORD upper, lower; lower = GetFileSize(h, &upper); if (INVALID_FILE_SIZE == lower) { DWORD const err = GetLastError(); if (NO_ERROR != err) { CloseHandle(h); return win_error_to_file_error(err); } } try { file = std::make_unique<win_osd_file>(h); filesize = (std::uint64_t(upper) << 32) | lower; return error::NONE; } catch (...) { CloseHandle(h); return error::OUT_OF_MEMORY; } }