Skip to content

Commit

Permalink
refactoring on loading files
Browse files Browse the repository at this point in the history
  • Loading branch information
katahiromz committed Jul 25, 2024
1 parent 3362971 commit 8ea1628
Showing 1 changed file with 129 additions and 114 deletions.
243 changes: 129 additions & 114 deletions src/RisohEditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2383,6 +2383,9 @@ class MMainWnd : public MWindowBase
void DoLoadLangInfo(VOID);
BOOL DoLoadFile(HWND hwnd, LPCWSTR pszFileName, DWORD nFilterIndex = 0, BOOL bForceDecompress = FALSE);
BOOL DoLoadRC(HWND hwnd, LPCWSTR szRCFile, EntrySet& res);
BOOL DoLoadRES(HWND hwnd, LPCWSTR szPath);
BOOL DoLoadRC2(HWND hwnd, LPCWSTR szPath);
BOOL DoLoadEXE(HWND hwnd, LPCWSTR pszPath, BOOL bForceDecompress);
BOOL DoExtract(const EntryBase *entry, BOOL bExporting);
BOOL DoExportRC(LPCWSTR pszRCFile, LPWSTR pszResHFile = NULL);
BOOL DoExportRC(LPCWSTR pszRCFile, LPWSTR pszResHFile, const EntrySet& found);
Expand Down Expand Up @@ -7695,142 +7698,99 @@ BOOL InitLangListBox(HWND hwnd)
return TRUE;
}

// load a file
BOOL MMainWnd::DoLoadFile(HWND hwnd, LPCWSTR pszFileName, DWORD nFilterIndex, BOOL bForceDecompress)
BOOL MMainWnd::DoLoadRC2(HWND hwnd, LPCWSTR szPath)
{
MWaitCursor wait;
WCHAR szPath[MAX_PATH], szResolvedPath[MAX_PATH], *pchPart;

enum LoadFilterIndex // see also: IDS_EXERESRCFILTER
{
LFI_NONE = 0,
LFI_LOADABLE = 1,
LFI_EXECUTABLE = 2,
LFI_RES = 3,
LFI_RC = 4,
LFI_ALL = 5
};

// if it was a shortcut file, then resolve it.
// pszFileName --> szPath
if (GetPathOfShortcutDx(hwnd, pszFileName, szResolvedPath))
{
GetFullPathNameW(szResolvedPath, _countof(szPath), szPath, &pchPart);
}
else
{
GetFullPathNameW(pszFileName, _countof(szPath), szPath, &pchPart);
}
// reload the resource.h if necessary
UnloadResourceH(hwnd);
if (g_settings.bAutoLoadNearbyResH)
CheckResourceH(hwnd, szPath);

// find the dot extension
LPWSTR pch = PathFindExtensionW(szPath);
if (pch && (nFilterIndex == LFI_NONE || nFilterIndex == LFI_ALL || nFilterIndex == LFI_LOADABLE))
// load the RC file to the res variable
EntrySet res;
if (!DoLoadRC(hwnd, szPath, res))
{
if (lstrcmpiW(pch, L".res") == 0) nFilterIndex = LFI_RES;
else if (lstrcmpiW(pch, L".rc") == 0) nFilterIndex = LFI_RC;
else if (lstrcmpiW(pch, L".exe") == 0 || lstrcmpiW(pch, L".dll") == 0 ||
lstrcmpiW(pch, L".ocx") == 0 || lstrcmpiW(pch, L".cpl") == 0 ||
lstrcmpiW(pch, L".scr") == 0 || lstrcmpiW(pch, L".mui") == 0)
{
nFilterIndex = LFI_EXECUTABLE;
}
else
{
nFilterIndex = LFI_NONE;
}
ErrorBoxDx(IDS_CANNOTOPEN);
return FALSE;
}

if (nFilterIndex == LFI_RES) // .res files
// load it now
m_bLoading = TRUE;
{
// reload the resource.h if necessary
UnloadResourceH(hwnd);
if (g_settings.bAutoLoadNearbyResH)
CheckResourceH(hwnd, szPath);
ShowLangArrow(FALSE);

// do import to the res variable
EntrySet res;
if (!res.import_res(szPath))
{
ErrorBoxDx(IDS_CANNOTOPEN);
return FALSE;
}
// renewal
g_res.delete_all();
g_res.merge(res);

// load it now
m_bLoading = TRUE;
{
ShowLangArrow(FALSE);
// clean up
res.delete_all();
}
m_bLoading = FALSE;

// renewal
g_res.delete_all();
g_res.merge(res);
// update the file info
UpdateFileInfo(FT_RC, szPath, FALSE);

// clean up
res.delete_all();
}
m_bLoading = FALSE;
// show ID list if necessary
if (m_szResourceH[0] && g_settings.bAutoShowIDList)
{
ShowIDList(hwnd, TRUE);
}

// update the file info
UpdateFileInfo(FT_RES, szPath, FALSE);
// select none
SelectTV(NULL, FALSE);

// show ID list if necessary
if (m_szResourceH[0] && g_settings.bAutoShowIDList)
{
ShowIDList(hwnd, TRUE);
}
DoSetFileModified(FALSE);
return TRUE;
}

// select none
SelectTV(NULL, FALSE);
BOOL MMainWnd::DoLoadRES(HWND hwnd, LPCWSTR szPath)
{
// reload the resource.h if necessary
UnloadResourceH(hwnd);
if (g_settings.bAutoLoadNearbyResH)
CheckResourceH(hwnd, szPath);

DoSetFileModified(FALSE);
return TRUE;
// do import to the res variable
EntrySet res;
if (!res.import_res(szPath))
{
ErrorBoxDx(IDS_CANNOTOPEN);
return FALSE;
}

if (nFilterIndex == LFI_RC) // .rc files
// load it now
m_bLoading = TRUE;
{
// reload the resource.h if necessary
UnloadResourceH(hwnd);
if (g_settings.bAutoLoadNearbyResH)
CheckResourceH(hwnd, szPath);

// load the RC file to the res variable
EntrySet res;
if (!DoLoadRC(hwnd, szPath, res))
{
ErrorBoxDx(IDS_CANNOTOPEN);
return FALSE;
}

// load it now
m_bLoading = TRUE;
{
ShowLangArrow(FALSE);
ShowLangArrow(FALSE);

// renewal
g_res.delete_all();
g_res.merge(res);
// renewal
g_res.delete_all();
g_res.merge(res);

// clean up
res.delete_all();
}
m_bLoading = FALSE;
// clean up
res.delete_all();
}
m_bLoading = FALSE;

// update the file info
UpdateFileInfo(FT_RC, szPath, FALSE);
// update the file info
UpdateFileInfo(FT_RES, szPath, FALSE);

// show ID list if necessary
if (m_szResourceH[0] && g_settings.bAutoShowIDList)
{
ShowIDList(hwnd, TRUE);
}
// show ID list if necessary
if (m_szResourceH[0] && g_settings.bAutoShowIDList)
{
ShowIDList(hwnd, TRUE);
}

// select none
SelectTV(NULL, FALSE);
// select none
SelectTV(NULL, FALSE);

DoSetFileModified(FALSE);
return TRUE;
}
DoSetFileModified(FALSE);
return TRUE;
}

LPWSTR pszPath = szPath; // the real path
BOOL MMainWnd::DoLoadEXE(HWND hwnd, LPCWSTR pszPath, BOOL bForceDecompress)
{
WCHAR szPath[MAX_PATH];

// check whether it was compressed
MStringW strToOpen = pszPath;
Expand Down Expand Up @@ -7899,7 +7859,8 @@ BOOL MMainWnd::DoLoadFile(HWND hwnd, LPCWSTR pszFileName, DWORD nFilterIndex, BO
if (hMod)
{
// ok, succeeded
StringCchCopy(pszPath, _countof(szPath), strToOpen.c_str());
StringCchCopyW(szPath, _countof(szPath), strToOpen.c_str());
pszPath = szPath;
}
else
{
Expand Down Expand Up @@ -7960,7 +7921,61 @@ BOOL MMainWnd::DoLoadFile(HWND hwnd, LPCWSTR pszFileName, DWORD nFilterIndex, BO
// update language arrow
PostUpdateLangArrow(m_hwnd);

return TRUE; // success
return TRUE; // success
}

// load a file
BOOL MMainWnd::DoLoadFile(HWND hwnd, LPCWSTR pszFileName, DWORD nFilterIndex, BOOL bForceDecompress)
{
MWaitCursor wait;
WCHAR szPath[MAX_PATH], szResolvedPath[MAX_PATH], *pchPart;

enum LoadFilterIndex // see also: IDS_EXERESRCFILTER
{
LFI_NONE = 0,
LFI_LOADABLE = 1,
LFI_EXECUTABLE = 2,
LFI_RES = 3,
LFI_RC = 4,
LFI_ALL = 5
};

// if it was a shortcut file, then resolve it.
// pszFileName --> szPath
if (GetPathOfShortcutDx(hwnd, pszFileName, szResolvedPath))
{
GetFullPathNameW(szResolvedPath, _countof(szPath), szPath, &pchPart);
}
else
{
GetFullPathNameW(pszFileName, _countof(szPath), szPath, &pchPart);
}

// find the dot extension
LPWSTR pch = PathFindExtensionW(szPath);
if (pch && (nFilterIndex == LFI_NONE || nFilterIndex == LFI_ALL || nFilterIndex == LFI_LOADABLE))
{
if (lstrcmpiW(pch, L".res") == 0) nFilterIndex = LFI_RES;
else if (lstrcmpiW(pch, L".rc") == 0) nFilterIndex = LFI_RC;
else if (lstrcmpiW(pch, L".exe") == 0 || lstrcmpiW(pch, L".dll") == 0 ||
lstrcmpiW(pch, L".ocx") == 0 || lstrcmpiW(pch, L".cpl") == 0 ||
lstrcmpiW(pch, L".scr") == 0 || lstrcmpiW(pch, L".mui") == 0)
{
nFilterIndex = LFI_EXECUTABLE;
}
else
{
nFilterIndex = LFI_NONE;
}
}

if (nFilterIndex == LFI_RES) // .res files
return DoLoadRES(hwnd, szPath);

if (nFilterIndex == LFI_RC) // .rc files
return DoLoadRC2(hwnd, szPath);

return DoLoadEXE(hwnd, szPath, bForceDecompress);
}

// unload resource.h
Expand Down

0 comments on commit 8ea1628

Please sign in to comment.