From 49e30c93cdae281ef90d0106a552c38ca9b0b1e7 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Lars=20Sundstr=C3=B6m?= <l.sundstrom@gmail.com>
Date: Thu, 20 Jul 2023 22:07:59 +0200
Subject: [PATCH] AGS: Remove function FileCloseNotify

In AGS there is a possibility to define a function, FileCloseNotify
with arguments, which is called when closing a file. This possibility
seems not to be used by any of the engines supported by ScummVM since
it's set to nullptr. This is not a problem in itself. Hoewever when
enabling compiler optimisiations the compiler may optimise away the
FileCloseNotify definition. This was discovered when compiling iOS
with Apple Clang in "Release" mode that the if-case in the code block
below was removed while the args parameter was left:
    if (FileCloseNotify) {
         CloseNotifyArgs args;
         args.Filepath = _fileName;
         args.WorkMode = _workMode;
         FileCloseNotify(args);
     }

This caused a nullptr exception since FileCloseNotify was nullptr.
A bit strange behavior by the compiler which could remove the entire
code block above.

Since the FileCloseNotify is not used in ScummVM, remove it as it
only seems to be used in upstream by the emscripten code which is
not part of the ScummVM repository.
---
 engines/ags/shared/util/file_stream.cpp | 8 --------
 engines/ags/shared/util/file_stream.h   | 4 ----
 2 files changed, 12 deletions(-)

diff --git a/engines/ags/shared/util/file_stream.cpp b/engines/ags/shared/util/file_stream.cpp
index 4b24c767302..fdf7bcba4aa 100644
--- a/engines/ags/shared/util/file_stream.cpp
+++ b/engines/ags/shared/util/file_stream.cpp
@@ -48,12 +48,6 @@ bool FileStream::HasErrors() const {
 void FileStream::Close() {
 	delete _file;
 	_file = nullptr;
-	if (FileCloseNotify) {
-		CloseNotifyArgs args;
-		args.Filepath = _fileName;
-		args.WorkMode = _workMode;
-		FileCloseNotify(args);
-	}
 }
 
 bool FileStream::Flush() {
@@ -200,8 +194,6 @@ void FileStream::Open(const String &file_name, FileOpenMode open_mode, FileWorkM
 	}
 }
 
-FileStream::FFileCloseNotify FileStream::FileCloseNotify = nullptr;
-
 String FileStream::getSaveName(const String &filename) {
 	return String(filename.GetCStr() + strlen(SAVE_FOLDER_PREFIX));
 }
diff --git a/engines/ags/shared/util/file_stream.h b/engines/ags/shared/util/file_stream.h
index d2aa6a7d016..aad5f2cfdc0 100644
--- a/engines/ags/shared/util/file_stream.h
+++ b/engines/ags/shared/util/file_stream.h
@@ -39,10 +39,6 @@ public:
 		FileWorkMode WorkMode;
 	};
 
-	// definition of function called when file closes
-	typedef std::function<void(const CloseNotifyArgs &args)> FFileCloseNotify;
-
-	static FFileCloseNotify FileCloseNotify;
 	// Represents an open file object
 	// The constructor may raise std::runtime_error if
 	// - there is an issue opening the file (does not exist, locked, permissions, etc)