From 747692bf1d7a837799816dc7197b6014b3d64db8 Mon Sep 17 00:00:00 2001 From: Donovan Watteau Date: Sat, 20 Mar 2021 12:52:37 +0100 Subject: [PATCH 1/2] feat(scummfont): Rename the -new file ourselves, don't ask users to do it manually --- src/ScummFont/scummfont.cpp | 22 +++++++++++++++++++--- src/ScummRp/toolbox.cpp | 3 ++- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/src/ScummFont/scummfont.cpp b/src/ScummFont/scummfont.cpp index 36b8356..8b54191 100644 --- a/src/ScummFont/scummfont.cpp +++ b/src/ScummFont/scummfont.cpp @@ -129,7 +129,7 @@ static int usage() std::cout << " scummfont {i|o} font bitmap.bmp" << std::endl; std::cout << std::endl; std::cout << " o: Export bitmap.bmp from font" << std::endl; - std::cout << " i: Import bitmap.bmp into font (result written in font-new)" << std::endl; + std::cout << " i: Import bitmap.bmp into font" << std::endl; std::cout << std::endl; std::cout << "\"font\" is either a CHAR block extracted with scummrp, or an LFL file" << std::endl; @@ -241,6 +241,15 @@ static std::string tmpPath(const char *path) return s; } +static void renameAndRemoveOriginal(const char *from, const char *to) +{ + if (remove(to) != 0) // for Win32 + throw std::runtime_error("Can't replace the existing font: remove failed"); + + if (rename(from, to) != 0) + throw std::runtime_error("Can't replace the existing font: rename failed"); +} + static inline int roundTo4(int i) { return (i + 3) & ~0x3; @@ -308,6 +317,8 @@ static void saveBmp(const char *path) memcpy(buf + i * roundTo4(glWidth), glFontBitmap + (glHeight - i - 1) * glWidth, glWidth); file.write((char *)buf, roundTo4(glWidth) * glHeight); + + file.close(); } static void loadBmp(const char *path) @@ -360,6 +371,8 @@ static void loadBmp(const char *path) file.read((char *)buf, roundTo4(glWidth) * glHeight); for (int i = 0; i < glHeight; ++i) memcpy(glFontBitmap + i * glWidth, buf + (glHeight - i - 1) * roundTo4(glWidth), glWidth); + + file.close(); } static void saveFont(const char *path) @@ -522,6 +535,8 @@ static void saveFont(const char *path) ++endOffset; } } + file.close(); + if (baseOffset == 8) // block header { tmpFile.seekp(0x4, std::ios::beg); @@ -550,9 +565,9 @@ static void saveFont(const char *path) numChars = newNumChars; tmpFile.write((char *)&numChars, 2); } + tmpFile.close(); - std::cout << "Output file has been written to " << tmpfilepath << std::endl; - std::cout << "Don't forget to replace your original file with the new version" << std::endl; + renameAndRemoveOriginal(path, tmpfilepath.c_str()); } } @@ -652,6 +667,7 @@ static void loadFont(const char *path) mask = b = p = 0; } } + file.close(); } int main(int argc, char **argv) try diff --git a/src/ScummRp/toolbox.cpp b/src/ScummRp/toolbox.cpp index d6a6471..9f7a7fd 100644 --- a/src/ScummRp/toolbox.cpp +++ b/src/ScummRp/toolbox.cpp @@ -77,7 +77,8 @@ void xremove(const char *path) } } -// note: newname must not already exist. call xremove() beforehand if necessary. +// note: because of Win32, newname must not already exist. Call xremove() +// beforehand if necessary. void xrename(const char *oldname, const char *newname) { if (rename(oldname, newname) != 0) From f8db3448dfbeb575dbd54dc90effd3f73953fb26 Mon Sep 17 00:00:00 2001 From: Donovan Watteau Date: Sat, 20 Mar 2021 17:38:37 +0100 Subject: [PATCH 2/2] feat(man): Drop -new suffix from scummfont.1 too --- man/scummfont.1 | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/man/scummfont.1 b/man/scummfont.1 index fefa0bd..fa50837 100644 --- a/man/scummfont.1 +++ b/man/scummfont.1 @@ -24,9 +24,7 @@ Import the content of an .Pa image.bmp bitmap file into the specified .Ar FONTFILE -SCUMM resource, with a -.Dq -new -suffix added to it. +SCUMM resource. .It Cm o Extract a font from a .Ar FONTFILE