From 69266ae60d7e68ff22673e3fe1ebad8236658aee Mon Sep 17 00:00:00 2001 From: DeaDDooMER Date: Mon, 2 Dec 2019 17:16:12 +0300 Subject: [PATCH] allow to open any wad type from recent files --- src/editor/f_main.pas | 48 +++++--------- src/shared/wadreader.pas | 136 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 151 insertions(+), 33 deletions(-) create mode 100644 src/shared/wadreader.pas diff --git a/src/editor/f_main.pas b/src/editor/f_main.pas index 7282c78..ca14ebe 100644 --- a/src/editor/f_main.pas +++ b/src/editor/f_main.pas @@ -337,7 +337,7 @@ uses f_options, e_graphics, e_log, GL, Math, f_mapoptions, g_basic, f_about, f_mapoptimization, f_mapcheck, f_addresource_texture, g_textures, - f_activationtype, f_keys, + f_activationtype, f_keys, wadreader, MAPREADER, f_selectmap, f_savemap, WADEDITOR, MAPDEF, g_map, f_saveminimap, f_addresource, CONFIG, f_packmap, f_addresource_sound, f_maptest, f_choosetype, @@ -2583,45 +2583,27 @@ end; procedure TMainForm.aRecentFileExecute(Sender: TObject); var - n, pw: Integer; - s, fn: String; - b: Boolean; + n: Integer; + fn, s: String; begin s := LowerCase((Sender as TMenuItem).Caption); Delete(s, Pos('&', s), 1); s := Trim(Copy(s, 1, 2)); n := StrToIntDef(s, 0) - 1; - - if (n < 0) or (n >= RecentFiles.Count) then - Exit; - - s := RecentFiles[n]; - pw := Pos('.wad:\', LowerCase(s)); - b := False; - - if pw > 0 then - begin // Map name included - fn := Copy(s, 1, pw + 3); - Delete(s, 1, pw + 5); - if (FileExists(fn)) then - begin - OpenMap(fn, s); - b := True; - end; + if (n >= 0) and (n <= RecentFiles.Count) then + begin + fn := g_ExtractWadName(RecentFiles[n]); + if FileExists(fn) then + begin + s := g_ExtractFilePathName(RecentFiles[n]); + OpenMap(fn, s) end - else // Only wad name - if (FileExists(s)) then + else if MessageBox(0, PChar(_lc[I_MSG_DEL_RECENT_PROMT]), PChar(_lc[I_MSG_DEL_RECENT]), MB_ICONQUESTION or MB_YESNO) = idYes then begin - OpenMap(s, ''); - b := True; - end; - - if (not b) and (MessageBox(0, PChar(_lc[I_MSG_DEL_RECENT_PROMT]), - PChar(_lc[I_MSG_DEL_RECENT]), MB_ICONQUESTION or MB_YESNO) = idYes) then - begin - RecentFiles.Delete(n); - RefreshRecentMenu(); - end; + RecentFiles.Delete(n); + RefreshRecentMenu(); + end + end end; procedure TMainForm.aEditorOptionsExecute(Sender: TObject); diff --git a/src/shared/wadreader.pas b/src/shared/wadreader.pas new file mode 100644 index 0000000..72dc64b --- /dev/null +++ b/src/shared/wadreader.pas @@ -0,0 +1,136 @@ +(* Copyright (C) Doom 2D: Forever Developers + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, version 3 of the License ONLY. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + *) +{$INCLUDE a_modes.inc} +unit wadreader; + +interface + +// g_ExtractWadName C:\svr\shit.wad:\MAPS\MAP01 -> C:/svr/shit.wad +function g_ExtractWadName (resourceStr: AnsiString): AnsiString; + +// g_ExtractWadNameNoPath C:\svr\shit.wad:\MAPS\MAP01 -> shit.wad +function g_ExtractWadNameNoPath (resourceStr: AnsiString): AnsiString; + +// g_ExtractFilePath C:\svr\shit.wad:\MAPS\MAP01 -> :/MAPS +function g_ExtractFilePath (resourceStr: AnsiString): AnsiString; + +// g_ExtractFileName C:\svr\shit.wad:\MAPS\MAP01 -> MAP01 +function g_ExtractFileName (resourceStr: AnsiString): AnsiString; // without path + +// g_ExtractFilePathName C:\svr\shit.wad:\MAPS\MAP01 -> MAPS/MAP01 +function g_ExtractFilePathName (resourceStr: AnsiString): AnsiString; + +implementation + +function normSlashes (s: AnsiString): AnsiString; +var + f: Integer; +begin + for f := 1 to length(s) do if s[f] = '\' then s[f] := '/'; + result := s; +end; + +function g_ExtractWadNameNoPath (resourceStr: AnsiString): AnsiString; +var + f, c: Integer; +begin + for f := length(resourceStr) downto 1 do + begin + if resourceStr[f] = ':' then + begin + result := normSlashes(Copy(resourceStr, 1, f-1)); + c := length(result); + while (c > 0) and (result[c] <> '/') do Dec(c); + if c > 0 then result := Copy(result, c+1, length(result)); + exit; + end; + end; + result := ''; +end; + +function g_ExtractWadName (resourceStr: AnsiString): AnsiString; +var + f: Integer; +begin + for f := length(resourceStr) downto 1 do + begin + if resourceStr[f] = ':' then + begin + result := normSlashes(Copy(resourceStr, 1, f-1)); + exit; + end; + end; + result := ''; +end; + +function g_ExtractFilePath (resourceStr: AnsiString): AnsiString; +var + f, lastSlash: Integer; +begin + result := ''; + lastSlash := -1; + for f := length(resourceStr) downto 1 do + begin + if (lastSlash < 0) and (resourceStr[f] = '\') or (resourceStr[f] = '/') then lastSlash := f; + if resourceStr[f] = ':' then + begin + if lastSlash > 0 then + begin + result := normSlashes(Copy(resourceStr, f, lastSlash-f)); + while (length(result) > 0) and (result[1] = '/') do Delete(result, 1, 1); + end; + exit; + end; + end; + if lastSlash > 0 then result := normSlashes(Copy(resourceStr, 1, lastSlash-1)); +end; + +function g_ExtractFileName (resourceStr: AnsiString): AnsiString; // without path +var + f, lastSlash: Integer; +begin + result := ''; + lastSlash := -1; + for f := length(resourceStr) downto 1 do + begin + if (lastSlash < 0) and (resourceStr[f] = '\') or (resourceStr[f] = '/') then lastSlash := f; + if resourceStr[f] = ':' then + begin + if lastSlash > 0 then result := Copy(resourceStr, lastSlash+1, length(resourceStr)); + exit; + end; + end; + if lastSlash > 0 then result := Copy(resourceStr, lastSlash+1, length(resourceStr)); +end; + +function g_ExtractFilePathName (resourceStr: AnsiString): AnsiString; +var + f: Integer; +begin + result := ''; + for f := length(resourceStr) downto 1 do + begin + if resourceStr[f] = ':' then + begin + result := normSlashes(Copy(resourceStr, f+1, length(resourceStr))); + while (length(result) > 0) and (result[1] = '/') do Delete(result, 1, 1); + exit; + end; + end; + result := normSlashes(resourceStr); + while (length(result) > 0) and (result[1] = '/') do Delete(result, 1, 1); +end; + +end. -- 2.29.2