/* plFileInfo */ plFileInfo::plFileInfo(const plFileName &filename) : fFileSize(-1), fCreateTime(), fModifyTime(), fFlags() { if (!filename.IsValid()) return; #if HS_BUILD_FOR_WIN32 struct __stat64 info; if (_wstat64(filename.AsString().ToWchar(), &info) != 0) return; #else struct stat info; if (stat(filename.AsString().c_str(), &info) != 0) return; #endif fFlags |= kEntryExists; fFileSize = info.st_size; fCreateTime = info.st_ctime; fModifyTime = info.st_mtime; if (info.st_mode & S_IFDIR) fFlags |= kIsDirectory; if (info.st_mode & S_IFREG) fFlags |= kIsNormalFile; }
bool plFileSystem::SetCWD(const plFileName &cwd) { #if HS_BUILD_FOR_WIN32 return SetCurrentDirectoryW(cwd.AsString().ToWchar()); #else return (chdir(cwd.AsString().c_str()) == 0); #endif }
bool plFileSystem::Move(const plFileName &from, const plFileName &to) { #if HS_BUILD_FOR_WIN32 return MoveFileExW(from.AsString().ToWchar(), to.AsString().ToWchar(), MOVEFILE_REPLACE_EXISTING); #else if (!Copy(from, to)) return false; return Unlink(from); #endif }
bool plFileSystem::Unlink(const plFileName &filename) { #if HS_BUILD_FOR_WIN32 plStringBuffer<wchar_t> wfilename = filename.AsString().ToWchar(); _wchmod(wfilename, S_IWRITE); return _wunlink(wfilename) == 0; #else chmod(filename.AsString().c_str(), S_IWRITE); return unlink(filename.AsString().c_str()) == 0; #endif }
static bool InitPhysX() { #ifdef HS_BUILD_FOR_WIN32 plSimulationMgr::Init(); if (!plSimulationMgr::GetInstance()) { if (plFileInfo(s_physXSetupExe).Exists()) { // launch the PhysX installer SHELLEXECUTEINFOW info; memset(&info, 0, sizeof(info)); info.cbSize = sizeof(info); info.lpFile = s_physXSetupExe.AsString().to_wchar(); info.fMask = SEE_MASK_NOCLOSEPROCESS | SEE_MASK_NOASYNC; ShellExecuteExW(&info); // wait for completion WaitForSingleObject(info.hProcess, INFINITE); // cleanup CloseHandle(info.hProcess); } else { hsMessageBox("You must install PhysX before you can play URU.", "Error", hsMessageBoxNormal, hsMessageBoxIconError); return false; } } if (plSimulationMgr::GetInstance()) { plSimulationMgr::GetInstance()->Suspend(); return true; } else { hsMessageBox("PhysX install failed. You will not be able to play URU.", "Error", hsMessageBoxNormal, hsMessageBoxIconError); return false; } #else return false; #endif // HS_BUILD_FOR_WIN32 }
static void IAuthThingDownloadCB(ENetError result, void* param, const plFileName& filename, hsStream* writer) { pfPatcherWorker* patcher = static_cast<pfPatcherWorker*>(param); if (IS_NET_SUCCESS(result)) { PatcherLogGreen("\tDownloaded Legacy File '%s'", filename.AsString().c_str()); patcher->IssueRequest(); // Now, we pass our RAM-backed file to the game code handlers. In the main client, // this will trickle down and add a new friend to plStreamSource. This should never // happen in any other app... writer->Rewind(); patcher->WhitelistFile(filename, true, writer); } else { PatcherLogRed("\tDownloaded Failed: File '%s'", filename.AsString().c_str()); patcher->EndPatch(result, filename.AsString()); } }
void plExportDlgImp::IInitDlg(HWND hDlg) { // Set the client path plFileName path = plMaxConfig::GetClientPath(false, true); SetDlgItemText(hDlg, IDC_CLIENT_PATH, path.AsString().c_str()); // Set the preshade button CheckDlgButton(hDlg, IDC_PRESHADE_CHECK, fPreshade ? BST_CHECKED : BST_UNCHECKED); CheckDlgButton(hDlg, IDC_PHYSICAL_CHECK, fPhysicalsOnly ? BST_CHECKED : BST_UNCHECKED); CheckDlgButton(hDlg, IDC_LIGHTMAP_CHECK, fLightMap ? BST_CHECKED : BST_UNCHECKED); char buf[256]; sprintf(buf, "Last export took %d:%02d", fLastExportTime/60, fLastExportTime%60); SetDlgItemText(hDlg, IDC_LAST_EXPORT, buf); SetWindowPos(hDlg, NULL, fXPos, fYPos, 0, 0, SWP_NOSIZE | SWP_NOZORDER); // // Get the names of all the pages in this scene and put them in the combo // HWND hPages = GetDlgItem(hDlg, IDC_PAGE_COMBO); ComboBox_AddString(hPages, kAllPages); bool foundPage = false; CompSet comps; GetPagesRecur((plMaxNode*)GetCOREInterface()->GetRootNode(), comps); for (CompSet::iterator it = comps.begin(); it != comps.end(); it++) { const char* page = LocCompGetPage(*it); if (page) { int idx = ComboBox_AddString(hPages, page); if (!strcmp(page, fExportPage)) { foundPage = true; ComboBox_SetCurSel(hPages, idx); } } } if (!foundPage) { fExportPage[0] = '\0'; ComboBox_SetCurSel(hPages, 0); } CheckRadioButton(hDlg, IDC_RADIO_FILE, IDC_RADIO_DIR, IDC_RADIO_FILE); IGetRadio(hDlg); SetDlgItemTextW(hDlg, IDC_EXPORT_PATH, fExportSourceDir.AsString().ToWchar()); }
bool plFileSystem::Copy(const plFileName &from, const plFileName &to) { #if HS_BUILD_FOR_WIN32 return CopyFileW(from.AsString().ToWchar(), to.AsString().ToWchar(), FALSE); #else typedef std::unique_ptr<FILE, std::function<int (FILE *)>> _FileRef; _FileRef ffrom(Open(from, "rb"), fclose); _FileRef fto(Open(to, "wb"), fclose); if (!ffrom.get() || !fto.get()) return false; size_t count; uint8_t buffer[4096]; while (!feof(ffrom.get())) { count = fread(buffer, sizeof(uint8_t), arrsize(buffer), ffrom.get()); if (ferror(ffrom.get())) return false; fwrite(buffer, sizeof(uint8_t), count, fto.get()); } return true; #endif }
std::vector<plFileName> plFileSystem::ListDir(const plFileName &path, const char *pattern) { std::vector<plFileName> contents; #if HS_BUILD_FOR_WIN32 if (!pattern || !pattern[0]) pattern = "*"; plFileName searchPattern = plFileName::Join(path, pattern); WIN32_FIND_DATAW findData; HANDLE hFind = FindFirstFileW(searchPattern.AsString().ToWchar(), &findData); if (hFind == INVALID_HANDLE_VALUE) return contents; do { if (findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { // Should also handle . and .. continue; } contents.push_back(plFileName::Join(path, plString::FromWchar(findData.cFileName))); } while (FindNextFileW(hFind, &findData)); FindClose(hFind); #else DIR *dir = opendir(path.AsString().c_str()); if (!dir) return contents; struct dirent *de; while (de = readdir(dir)) { plFileName dir_name = plFileName::Join(path, de->d_name); if (plFileInfo(dir_name).IsDirectory()) { // Should also handle . and .. continue; } if (pattern && pattern[0] && fnmatch(pattern, de->d_name, 0)) contents.push_back(dir_name); else if (!pattern || !pattern[0]) contents.push_back(dir_name); } closedir(dir); #endif return contents; }
std::vector<plFileName> plFileSystem::ListSubdirs(const plFileName &path) { std::vector<plFileName> contents; #if HS_BUILD_FOR_WIN32 plFileName searchPattern = plFileName::Join(path, "*"); WIN32_FIND_DATAW findData; HANDLE hFind = FindFirstFileW(searchPattern.AsString().ToWchar(), &findData); if (hFind == INVALID_HANDLE_VALUE) return contents; do { if (findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { plFileName name = plString::FromWchar(findData.cFileName); if (name != "." && name != "..") contents.push_back(plFileName::Join(path, name)); } } while (FindNextFileW(hFind, &findData)); FindClose(hFind); #else DIR *dir = opendir(path.AsString().c_str()); if (!dir) return contents; struct dirent *de; while (de = readdir(dir)) { if (plFileInfo(de->d_name).IsDirectory()) { plFileName name = de->d_name; if (name != "." && name != "..") contents.push_back(plFileName::Join(path, name)); } } closedir(dir); #endif return contents; }
static void IFileThingDownloadCB(ENetError result, void* param, const plFileName& filename, hsStream* writer) { pfPatcherWorker* patcher = static_cast<pfPatcherWorker*>(param); pfPatcherStream* stream = static_cast<pfPatcherStream*>(writer); stream->Close(); if (IS_NET_SUCCESS(result)) { PatcherLogGreen("\tDownloaded File '%s'", stream->GetFileName().AsString().c_str()); patcher->WhitelistFile(stream->GetFileName(), true); if (patcher->fSelfPatch && stream->IsSelfPatch()) patcher->fSelfPatch(stream->GetFileName()); if (patcher->fRedistUpdateDownloaded && stream->IsRedistUpdate()) patcher->fRedistUpdateDownloaded(stream->GetFileName()); patcher->IssueRequest(); } else { PatcherLogRed("\tDownloaded Failed: File '%s'", stream->GetFileName().AsString().c_str()); stream->Unlink(); patcher->EndPatch(result, filename.AsString()); } delete stream; }
bool pfConsoleEngine::ExecuteFile(const plFileName &fileName) { char string[ 512 ]; int line; hsStream* stream = plEncryptedStream::OpenEncryptedFile(fileName); if( !stream ) { ISetErrorMsg( "Cannot open given file" ); // return false; /// THIS IS BAD: because of the asserts we throw after this if we return false, a missing /// file will throw an assert. This is all well and good except for the age-specific .fni files, /// which aren't required to be there and rely on this functionality to test whether the file is /// present. Once age-specific .fni's are gone, reinstate the return false here. -mcn return true; } for( line = 1; stream->ReadLn( string, arrsize( string ) ); line++ ) { strncpy( fLastErrorLine, string, arrsize( fLastErrorLine ) ); if( !RunCommand( string, DummyPrintFn ) ) { snprintf(string, arrsize(string), "Error in console file %s, command line %d: %s", fileName.AsString().c_str(), line, fErrorMsg); ISetErrorMsg( string ); stream->Close(); delete stream; return false; } } stream->Close(); delete stream; fLastErrorLine[ 0 ] = 0; return true; }
BOOL plExportDlgImp::DlgProc(HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam) { switch (msg) { case WM_INITDIALOG: IInitDlg(hDlg); return TRUE; case WM_COMMAND: { int cmd = HIWORD(wParam); int resID = LOWORD(wParam); if (cmd == BN_CLICKED) { if (resID == IDCANCEL) { IDestroy(); return TRUE; } else if (resID == IDC_EXPORT) { IDoExport(); return TRUE; } else if (resID == IDC_PRESHADE_CHECK) { fPreshade = (IsDlgButtonChecked(hDlg, IDC_PRESHADE_CHECK) == BST_CHECKED); return TRUE; } else if (resID == IDC_PHYSICAL_CHECK) { fPhysicalsOnly = (IsDlgButtonChecked(hDlg, IDC_PHYSICAL_CHECK) == BST_CHECKED); return TRUE; } else if (resID == IDC_LIGHTMAP_CHECK) { fLightMap = (IsDlgButtonChecked(hDlg, IDC_LIGHTMAP_CHECK) == BST_CHECKED); return TRUE; } else if (resID == IDC_DIR) { // Get a new client path plFileName path = plMaxConfig::GetClientPath(true); if (path.IsValid()) SetDlgItemText(hDlg, IDC_CLIENT_PATH, path.AsString().c_str()); return TRUE; } else if (resID == IDC_RADIO_FILE || resID == IDC_RADIO_DIR) { IGetRadio(hDlg); return TRUE; } else if (resID == IDC_BROWSE_EXPORT) { fExportSourceDir = plBrowseFolder::GetFolder(fExportSourceDir, "Choose the source directory", hDlg); SetDlgItemTextW(hDlg, IDC_EXPORT_PATH, fExportSourceDir.AsString().ToWchar()); return TRUE; } } else if (cmd == CBN_SELCHANGE && resID == IDC_PAGE_COMBO) { int sel = ComboBox_GetCurSel((HWND)lParam); // If the user selected a page, save it if (sel != 0 && sel != CB_ERR) ComboBox_GetText((HWND)lParam, fExportPage, sizeof(fExportPage)); // Else, clear it (export all pages) else fExportPage[0] = '\0'; return TRUE; } } break; } return FALSE; }
void WritePythonFile(const plFileName &fileName, const plFileName &path, hsStream *s) { hsUNIXStream pyStream, glueStream; plFileName filePath; ST_ssize_t filestart = fileName.AsString().find_last('.'); if (filestart >= 0) filePath = fileName.AsString().substr(filestart+1); else filePath = fileName; filePath = plFileName::Join(path, filePath + ".py"); if (!pyStream.Open(filePath) || !glueStream.Open(glueFile)) { ST::printf("Unable to open path {}, ", filePath); return; } ST::printf("==Packing {}, ", fileName); pyStream.FastFwd(); uint32_t pyFileSize = pyStream.GetPosition(); pyStream.Rewind(); glueStream.FastFwd(); uint32_t glueFileSize = glueStream.GetPosition(); glueStream.Rewind(); uint32_t totalSize = pyFileSize + glueFileSize + 2; char *code = new char[totalSize]; uint32_t amountRead = pyStream.Read(pyFileSize, code); hsAssert(amountRead == pyFileSize, "Bad read"); code[pyFileSize] = '\n'; amountRead = glueStream.Read(glueFileSize, code+pyFileSize+1); hsAssert(amountRead == glueFileSize, "Bad read"); code[totalSize-1] = '\0'; // remove the CRs, they seem to give Python heartburn int k = 0; for (int i = 0; i < totalSize; i++) { if (code[i] != '\r') // is it not a CR? code[k++] = code[i]; // else // skip the CRs } // import the module first, to make packages work correctly PyImport_ImportModule(fileName.AsString().c_str()); PyObject* pythonCode = PythonInterface::CompileString(code, fileName); if (pythonCode) { // we need to find out if this is PythonFile module // create a module name... with the '.' as an X // and create a python file name that is without the ".py" PyObject* fModule = PythonInterface::CreateModule(fileName.AsString().c_str()); // run the code if (PythonInterface::RunPYC(pythonCode, fModule) ) { // set the name of the file (in the global dictionary of the module) PyObject* dict = PyModule_GetDict(fModule); PyObject* pfilename = PyString_FromString(fileName.AsString().c_str()); PyDict_SetItemString(dict, "glue_name", pfilename); // next we need to: // - create instance of class PyObject* getID = PythonInterface::GetModuleItem("glue_getBlockID",fModule); bool foundID = false; if ( getID!=nil && PyCallable_Check(getID) ) { PyObject* id = PyObject_CallFunction(getID,nil); if ( id && PyInt_Check(id) ) foundID = true; } if ( foundID == false ) // then there was an error or no ID or somethin' { // oops, this is not a PythonFile modifier // re-read the source and compile it without the glue code this time pyStream.Rewind(); amountRead = pyStream.Read(pyFileSize, code); hsAssert(amountRead == pyFileSize, "Bad read"); code[amountRead] = '\n'; code[amountRead+1] = '\0'; k = 0; int len = strlen(code)+1; for (int i = 0; i < len; i++) { if (code[i] != '\r') // is it not a CR? code[k++] = code[i]; // else // skip the CRs } pythonCode = PythonInterface::CompileString(code, fileName); hsAssert(pythonCode,"Not sure why this didn't compile the second time???"); fputs("an import file ", stdout); } else fputs("a PythonFile modifier(tm) ", stdout); } else { fputs("......blast! Error during run-code!\n", stdout); char* errmsg; int chars_read = PythonInterface::getOutputAndReset(&errmsg); if (chars_read > 0) { puts(errmsg); } } } // make sure that we have code to save if (pythonCode) { int32_t size; char* pycode; PythonInterface::DumpObject(pythonCode,&pycode,&size); fputc('\n', stdout); // print any message after each module char* errmsg; int chars_read = PythonInterface::getOutputAndReset(&errmsg); if (chars_read > 0) { puts(errmsg); } s->WriteLE32(size); s->Write(size, pycode); } else { fputs("......blast! Compile error!\n", stdout); s->WriteLE32(0); PyErr_Print(); PyErr_Clear(); char* errmsg; int chars_read = PythonInterface::getOutputAndReset(&errmsg); if (chars_read > 0) { puts(errmsg); } } delete [] code; pyStream.Close(); glueStream.Close(); }