EDITOR: Replace ddio_FindFileStart() with ddio_DoForeachFile()

Applied some minor additional code optimization.
This commit is contained in:
Azamat H. Hackimov 2024-07-20 01:14:36 +03:00
parent 61ed7cedcf
commit 7396ddd576
7 changed files with 65 additions and 190 deletions

View File

@ -1567,7 +1567,7 @@ void CLightingDialog::OnCombineFaces() {
}
// Takes a bitmap name and resaves it
void ResaveBitmap(char *name) {
void ResaveBitmap(const char *name) {
int bm_handle = bm_FindBitmapName(name);
if (bm_handle >= 0) {
char search[256];
@ -1598,7 +1598,7 @@ void ResaveBitmap(char *name) {
}
// Takes a vclip name and resaves it
void ResaveVClip(char *name) {
void ResaveVClip(const char *name) {
int bm_handle = FindVClipName(name);
if (bm_handle >= 0) {
char search[256];
@ -1623,32 +1623,10 @@ void ResaveVClip(char *name) {
}
void ResaveAllBitmaps() {
char buffer[_MAX_PATH];
char search[256];
ddio_MakePath(search, LocalD3Dir, "data", "graphics", "*.ogf", NULL);
if (ddio_FindFileStart(search, buffer)) {
ResaveBitmap(buffer);
while ((ddio_FindNextFile(buffer))) {
ResaveBitmap(buffer);
}
}
ddio_FindFileClose();
ddio_MakePath(search, LocalD3Dir, "data", "graphics", "*.oaf", NULL);
if (ddio_FindFileStart(search, buffer)) {
ResaveVClip(buffer);
while ((ddio_FindNextFile(buffer))) {
ResaveVClip(buffer);
}
}
ddio_FindFileClose();
ddio_DoForeachFile(std::filesystem::path(LocalD3Dir) / "data" / "graphics", std::regex(".+\\.ogf"),
[](const std::filesystem::path &path) { ResaveBitmap(path.filename().u8string().c_str()); });
ddio_DoForeachFile(std::filesystem::path(LocalD3Dir) / "data" / "graphics", std::regex(".+\\.oaf"),
[](const std::filesystem::path &path) { ResaveVClip(path.filename().u8string().c_str()); });
}
void CLightingDialog::OnVolumeLights() {

View File

@ -115,6 +115,9 @@
* $NoKeywords: $
*/
#include <filesystem>
#include <regex>
#include "stdafx.h"
#include "ObjCScript.h"
#include "editor.h"
@ -543,27 +546,6 @@ char *GotoScriptInText(char *text, const char *script) {
return NULL;
}
// These functions manage enumeration of all script files located in the script directory
static int hGameFile = -1, hInitialFile = -1;
static char TempStr[256];
char *StartScriptFileList() {
char path[_MAX_PATH];
ddio_MakePath(path, LocalLevelsDir, "*.scr", NULL);
if (!ddio_FindFileStart(path, TempStr))
return NULL;
return TempStr;
}
char *GetNextScriptFile() {
if (!ddio_FindNextFile(TempStr))
return NULL;
return TempStr;
}
void EndScriptFileList() { ddio_FindFileClose(); }
//////////////////////////////////////////////////////////////////////////////
// Script source management
@ -706,28 +688,18 @@ char *AddEventBlockToScript(char *script, const char *evtname, const char *scrip
// generates script list from all script files.
void GenerateScriptListFromAllFiles(int mask) {
char *filename;
//@@ tD3XProgram *m_ScriptCode;
//@@ m_ScriptCode = D3XReallocProgram(NULL, 0, 0, 0);
// initialize script list.
ResetScriptList();
std::filesystem::path dir = std::filesystem::path(LocalLevelsDir);
// compile all script files and place into script list.
filename = StartScriptFileList();
while (filename) {
// compile script first. if we failed, then display a messagebox giving the warning
if (!stricmp(filename, DEFAULT_SCRIPT_NAME) && (mask & DEFAULT_SCRIPT_MASK))
GenerateScriptListFromFile(filename);
else if ((mask & CUSTOM_SCRIPT_MASK) && stricmp(filename, DEFAULT_SCRIPT_NAME))
GenerateScriptListFromFile(filename);
filename = GetNextScriptFile();
}
EndScriptFileList();
//@@ D3XFreeProgram(m_ScriptCode);
ddio_DoForeachFile(dir, std::regex(".+\\.scr"), [&mask](const std::filesystem::path& path){
std::filesystem::path file = path.filename();
if (!stricmp(file.u8string().c_str(), DEFAULT_SCRIPT_NAME) && (mask & DEFAULT_SCRIPT_MASK))
GenerateScriptListFromFile(file.u8string().c_str());
else if ((mask & CUSTOM_SCRIPT_MASK) && stricmp(file.u8string().c_str(), DEFAULT_SCRIPT_NAME))
GenerateScriptListFromFile(file.u8string().c_str());
});
}
// generates script list from one file

View File

@ -41,6 +41,9 @@
// OrphanRemoveDlg.cpp : implementation file
//
#include <filesystem>
#include <regex>
#include "stdafx.h"
#include "editor.h"
#include "OrphanRemoveDlg.h"
@ -96,7 +99,7 @@ bool HasFilesCheckedOut(void) {
return false;
}
bool IsFileInGameFile(char *filename) {
bool IsFileInGameFile(const char *filename) {
int i;
for (i = 0; i < MAX_GAMEFILES; i++) {
@ -109,7 +112,7 @@ bool IsFileInGameFile(char *filename) {
return false;
}
bool IsFileInDoorPage(char *filename) {
bool IsFileInDoorPage(const char *filename) {
mngs_door_page doorpage;
int i;
@ -127,7 +130,7 @@ bool IsFileInDoorPage(char *filename) {
return false;
}
bool IsFileInGenericPage(char *filename) {
bool IsFileInGenericPage(const char *filename) {
mngs_generic_page genericpage;
int i;
int j, x;
@ -179,7 +182,7 @@ bool IsFileInGenericPage(char *filename) {
return false;
}
bool IsFileInShipPage(char *filename) {
bool IsFileInShipPage(const char *filename) {
mngs_ship_page shippage;
int i;
int j;
@ -219,7 +222,7 @@ bool IsFileInShipPage(char *filename) {
return false;
}
bool IsFileInSoundPage(char *filename) {
bool IsFileInSoundPage(const char *filename) {
mngs_sound_page soundpage;
int i;
@ -234,7 +237,7 @@ bool IsFileInSoundPage(char *filename) {
return false;
}
bool IsFileInTexturePage(char *filename) {
bool IsFileInTexturePage(const char *filename) {
mngs_texture_page texpage;
int i;
@ -255,7 +258,7 @@ bool IsFileInTexturePage(char *filename) {
return false;
}
bool IsFileInWeaponPage(char *filename) {
bool IsFileInWeaponPage(const char *filename) {
mngs_weapon_page weappage;
int i, j;
@ -306,7 +309,7 @@ void COrphanRemoveDlg::Enable(bool enable) {
wnd->EnableWindow(enable);
}
void COrphanRemoveDlg::SetStatus(char *status) {
void COrphanRemoveDlg::SetStatus(const char *status) {
CWnd *stat = (CWnd *)GetDlgItem(IDC_STATUS);
stat->SetWindowText(status);
Descent->defer();
@ -348,14 +351,14 @@ void COrphanRemoveDlg::OnOK() {
OnSelchangeDirectory();
}
char *orphan_ignore[] = {
const char *orphan_ignore[] = {
".", "..", "dallasfuncs.cpp", "osiris_common.h", "osiris_import.h", "osiris_vector.h",
"anarchy.str", "coop.str", "ctf.str", "entropy.str", "dmfc.str", "d3.str",
"hoard.str", "dp_modem.str", "dp_serial.str", "hyper.str", "ipxclient.str", "lanclient.str",
"monster.str", "mtclient.str", "tanarchy.str"};
int num_orphan_ignore = sizeof(orphan_ignore) / sizeof(char *);
bool IsFileUsed(char *filename) {
bool IsFileUsed(const char *filename) {
bool found = false;
for (int i = 0; i < num_orphan_ignore; i++) {
@ -386,10 +389,8 @@ done:
return found;
}
int filelen(char *filename, char *dir) {
char path[_MAX_PATH];
ddio_MakePath(path, dir, filename, NULL);
CFILE *cf = cfopen(filename, "rb");
int filelen(const std::filesystem::path &filename, const std::filesystem::path &dir) {
CFILE *cf = cfopen(dir / filename, "rb");
if (!cf)
return 0;
@ -398,19 +399,6 @@ int filelen(char *filename, char *dir) {
return len;
}
bool isdirectory(char *filename, char *basepath) {
char fullpath[_MAX_PATH];
ddio_MakePath(fullpath, basepath, filename, NULL);
char olddir[_MAX_PATH];
ddio_GetWorkingDir(olddir, _MAX_PATH);
if (ddio_SetWorkingDir(fullpath)) {
ddio_SetWorkingDir(olddir);
return true;
}
return false;
}
void COrphanRemoveDlg::OnSelchangeDirectory() {
CString dir;
CComboBox *combo = (CComboBox *)GetDlgItem(IDC_DIRECTORY);
@ -425,13 +413,6 @@ void COrphanRemoveDlg::OnSelchangeDirectory() {
combo->GetLBText(sel, dir);
m_List.ResetContent();
char fullpath[_MAX_PATH];
ddio_MakePath(fullpath, LocalD3Dir, "data", dir.GetBuffer(0), NULL);
char oldpath[_MAX_PATH];
ddio_GetWorkingDir(oldpath, _MAX_PATH);
ddio_SetWorkingDir(fullpath);
char filename[_MAX_PATH];
int num_files = 0;
int total_filesize = 0;
CWnd *stats = (CWnd *)GetDlgItem(IDC_STATS);
@ -440,28 +421,18 @@ void COrphanRemoveDlg::OnSelchangeDirectory() {
sprintf(data, "Num Files: %d\r\nSize: %dK", 0, 0);
stats->SetWindowText(data);
if (ddio_FindFileStart("*.*", filename)) {
if (!isdirectory(filename, fullpath) && !IsFileUsed(filename)) {
m_List.AddString(filename);
num_files++;
total_filesize += filelen(filename, fullpath);
sprintf(data, "Num Files: %d\r\nSize: %dK", num_files, total_filesize / 1024);
stats->SetWindowText(data);
}
std::filesystem::path fullpath = std::filesystem::path(LocalD3Dir) / "data" / dir.GetBuffer(0);
while (ddio_FindNextFile(filename)) {
if (!isdirectory(filename, fullpath) && !IsFileUsed(filename)) {
m_List.AddString(filename);
num_files++;
total_filesize += filelen(filename, fullpath);
sprintf(data, "Num Files: %d\r\nSize: %dK", num_files, total_filesize / 1024);
stats->SetWindowText(data);
}
}
}
ddio_FindFileClose();
ddio_SetWorkingDir(oldpath);
ddio_DoForeachFile(fullpath, std::regex(".+"),
[this, &num_files, &total_filesize, &data, &stats](const std::filesystem::path &path) {
if (!IsFileUsed(path.filename().u8string().c_str())) {
m_List.AddString(path.filename().u8string().c_str());
num_files++;
total_filesize += filelen(path.filename(), path.parent_path());
sprintf(data, "Num Files: %d\r\nSize: %dK", num_files, total_filesize / 1024);
stats->SetWindowText(data);
}
});
// select all
for (int i = 0; i < num_files; i++) {

View File

@ -51,7 +51,7 @@ class COrphanRemoveDlg : public CDialog {
// Construction
public:
COrphanRemoveDlg(CWnd *pParent = NULL); // standard constructor
void SetStatus(char *status);
void SetStatus(const char *status);
void Enable(bool enable);
// Dialog Data

View File

@ -115,6 +115,9 @@
// ScriptLevelInterface.cpp : implementation file
//
#include <filesystem>
#include <regex>
#include "stdafx.h"
#include "editor.h"
#include "ScriptLevelInterface.h"
@ -131,7 +134,6 @@
#include "mem.h"
#include "ScriptCompilerAPI.h"
#include "AppDatabase.h"
#include "Descent.h"
#include "ScriptSyncDialog.h"
#ifdef _DEBUG
@ -200,7 +202,7 @@ END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CScriptLevelInterface message handlers
void CScriptLevelInterface::SetStatus(char *str) {
void CScriptLevelInterface::SetStatus(const char *str) {
CWnd *wnd = (CWnd *)GetDlgItem(IDC_STATUS);
wnd->SetWindowText(str);
Descent->defer();
@ -606,40 +608,19 @@ void CScriptLevelInterface::UpdateScriptListWithLevels() {
int length, i;
if (m_ShowNonCheckedOut) {
char olddir[_MAX_PATH];
ddio_GetWorkingDir(olddir, _MAX_PATH);
ddio_SetWorkingDir(LocalScriptDir);
for (i = 0; i < MAX_GAMEFILES; i++) {
if (Gamefiles[i].used) {
// look for d3l's
length = strlen(Gamefiles[i].name);
if (length > 4 && !stricmp(&Gamefiles[i].name[length - 4], ".d3l")) {
strcpy(buffer, Gamefiles[i].name);
buffer[length - 4] = '\0';
strcat(buffer, ".cpp");
std::filesystem::path gamefile = std::filesystem::path(Gamefiles[i].name);
if (gamefile.extension() == ".d3l") {
// ok we have a d3l, look to see if there is a script available for it
char temp[_MAX_PATH];
if (ddio_FindFileStart(buffer, temp)) {
combo->AddString(buffer);
gamefile.replace_extension(".cpp");
if (std::filesystem::is_regular_file(std::filesystem::path(LocalScriptDir) / gamefile)) {
combo->AddString(gamefile.u8string().c_str());
}
ddio_FindFileClose();
/*
for ( int j=0; j<MAX_GAMEFILES;j++){
if(!stricmp(Gamefiles[j].name,buffer)){
//we found a corresponding script
combo->AddString(buffer);
break;
}
}
*/
}
}
}
ddio_SetWorkingDir(olddir);
} else {
for (i = 0; i < MAX_TRACKLOCKS; i++) {
@ -687,34 +668,9 @@ void CScriptLevelInterface::UpdateScriptListWithScripts() {
int length, i;
if (m_ShowNonCheckedOut) {
char olddir[_MAX_PATH];
ddio_GetWorkingDir(olddir, _MAX_PATH);
ddio_SetWorkingDir(LocalScriptDir);
if (ddio_FindFileStart("*.cpp", buffer)) {
combo->AddString(buffer);
while (ddio_FindNextFile(buffer)) {
combo->AddString(buffer);
}
}
ddio_FindFileClose();
ddio_SetWorkingDir(olddir);
/*
for (i=0;i<MAX_GAMEFILES;i++){
if(Gamefiles[i].used){
//look for cpp's
length = strlen(Gamefiles[i].name);
if(length>4 && !stricmp(&Gamefiles[i].name[length-4],".cpp")){
strcpy(buffer,Gamefiles[i].name);
combo->AddString(buffer);
}
}
}
*/
ddio_DoForeachFile(std::filesystem::path(LocalScriptDir), std::regex(".+\\.cpp"), [&combo](const std::filesystem::path& path){
combo->AddString(path.filename().u8string().c_str());
});
} else {

View File

@ -101,7 +101,7 @@ protected:
// Implementation
protected:
void SetStatus(char *str);
void SetStatus(const char *str);
void UpdateAvailableWithLevels();
void UpdateAvailableWithScripts();
void UpdateCheckedOutWithLevels();

View File

@ -88,6 +88,9 @@
// ScriptWizard.cpp : implementation file
//
#include <filesystem>
#include <regex>
#include "stdafx.h"
#include "editor.h"
#include "ScriptWizard.h"
@ -159,16 +162,11 @@ BOOL CScriptWizard::OnInitDialog() {
ResetScriptList();
// place all filenames of scripts into combo box
char scrfilename[64];
char *filename;
scrfilename[0] = 0;
filename = StartScriptFileList();
while (filename) {
strcpy(scrfilename, filename);
modcbox->AddString(filename);
filename = GetNextScriptFile();
}
EndScriptFileList();
std::filesystem::path dir = std::filesystem::path(LocalLevelsDir);
ddio_DoForeachFile(dir, std::regex(".+\\.scr"), [&modcbox](const std::filesystem::path& path){
std::filesystem::path file = path.filename();
modcbox->AddString(file.u8string().c_str());
});
modcbox->SelectString(-1, LEVEL_SCRIPT_NAME);