Skip to content

Commit

Permalink
automatic safe mode!
Browse files Browse the repository at this point in the history
to-do: test on Windows...
  • Loading branch information
tildearrow committed Oct 16, 2023
1 parent 1cf519c commit e633550
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 4 deletions.
21 changes: 20 additions & 1 deletion src/engine/engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3634,14 +3634,22 @@ bool DivEngine::deinitAudioBackend(bool dueToSwitchMaster) {
return true;
}

void DivEngine::preInit() {
bool DivEngine::preInit(bool noSafeMode) {
bool wantSafe=false;
// register systems
if (!systemsRegistered) registerSystems();

// init config
initConfDir();
logD("config path: %s",configPath.c_str());

if (!noSafeMode) {
String safeModePath=configPath+DIR_SEPARATOR_STR+"safemode";
if (touchFile(safeModePath.c_str())==-EEXIST) {
wantSafe=true;
}
}

String logPath=configPath+DIR_SEPARATOR_STR+"furnace.log";
startLogFile(logPath.c_str());

Expand All @@ -3655,6 +3663,17 @@ void DivEngine::preInit() {
SDL_SetHint("SDL_HINT_AUDIODRIVER",audioDriver.c_str());
}
#endif

if (wantSafe) {
logW("requesting safe mode.");
}

return wantSafe;
}

void DivEngine::everythingOK() {
String safeModePath=configPath+DIR_SEPARATOR_STR+"safemode";
deleteFile(safeModePath.c_str());
}

bool DivEngine::init() {
Expand Down
7 changes: 5 additions & 2 deletions src/engine/engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -1196,12 +1196,15 @@ class DivEngine {
// quit dispatch
void quitDispatch();

// pre-initialize the engine.
void preInit();
// pre-initialize the engine. returns whether Furnace should run in safe mode.
bool preInit(bool noSafeMode=true);

// initialize the engine.
bool init();

// confirm that the engine is running (delete safe mode file).
void everythingOK();

// terminate the engine.
bool quit();

Expand Down
24 changes: 24 additions & 0 deletions src/fileutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include <shlwapi.h>
#else
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/stat.h>
#endif
Expand Down Expand Up @@ -100,3 +101,26 @@ bool makeDir(const char* path) {
return (mkdir(path,0755)==0);
#endif
}

int touchFile(const char* path) {
#ifdef _WIN32
HANDLE h=CreateFileW(utf8To16(path).c_str(),GENERIC_WRITE,FILE_SHARE_DELETE|FILE_SHARE_READ|FILE_SHARE_WRITE,NULL,CREATE_NEW,FILE_ATTRIBUTE_TEMPORARY,NULL);
if (h==INVALID_HANDLE_VALUE) {
switch (GetLastError()) {
case ERROR_FILE_EXISTS:
return -EEXIST;
break;
}
return -EPERM;
}
if (CloseHandle(h)==0) {
return -EBADF;
}
return 0;
#else
int fd=open(path,O_CREAT|O_WRONLY|O_TRUNC|O_EXCL,0666);
if (fd<0) return -errno;
close(fd);
return 0;
#endif
}
1 change: 1 addition & 0 deletions src/fileutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,6 @@ bool deleteFile(const char* path);
int fileExists(const char* path);
bool dirExists(const char* what);
bool makeDir(const char* path);
int touchFile(const char* path);

#endif
6 changes: 6 additions & 0 deletions src/gui/gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3455,6 +3455,11 @@ bool FurnaceGUI::loop() {
logD("key input: main thread");
}

if (safeMode) {
showError("Furnace has been started in Safe Mode.\nthis means that:\n\n- software rendering is being used\n- audio output may not work\n- font loading is disabled\n\ncheck any settings which may have made Furnace start up in this mode.\nfont loading is one of these.");
settingsOpen=true;
}

while (!quit) {
SDL_Event ev;
if (e->isPlaying()) {
Expand Down Expand Up @@ -6244,6 +6249,7 @@ bool FurnaceGUI::loop() {
if (mustClear) {
rend->clear(ImVec4(0,0,0,0));
mustClear--;
if (mustClear==0) e->everythingOK();
} else {
if (initialScreenWipe>0.0f && !settings.disableFadeIn) {
WAKE_UP;
Expand Down
31 changes: 30 additions & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,14 +131,24 @@ TAParamResult pConsole(String val) {
}

TAParamResult pSafeMode(String val) {
#ifdef HAVE_GUI
safeMode=true;
return TA_PARAM_SUCCESS;
#else
logE("Furnace was compiled without the GUI. safe mode is pointless.");
return TA_PARAM_ERROR;
#endif
}

TAParamResult pSafeModeAudio(String val) {
#ifdef HAVE_GUI
safeMode=true;
safeModeWithAudio=true;
return TA_PARAM_SUCCESS;
#else
logE("Furnace was compiled without the GUI. safe mode is pointless.");
return TA_PARAM_ERROR;
#endif
}

TAParamResult pBinary(String val) {
Expand Down Expand Up @@ -515,7 +525,24 @@ int main(int argc, char** argv) {
return 1;
}

e.preInit();
#ifdef HAVE_GUI
if (e.preInit(false)) {
if (consoleMode || benchMode || outName!="" || vgmOutName!="" || cmdOutName!="") {
logW("engine wants safe mode, but Furnace GUI is not going to start.");
} else {
safeMode=true;
}
}
#else
if (e.preInit(true)) {
logW("engine wants safe mode, but Furnace GUI is not available.");
}
#endif

if (safeMode && (consoleMode || benchMode || outName!="" || vgmOutName!="" || cmdOutName!="")) {
logE("you can't use safe mode and console/export mode together.");
return 0;
}

if (safeMode && !safeModeWithAudio) {
e.setAudio(DIV_AUDIO_DUMMY);
Expand Down Expand Up @@ -684,6 +711,7 @@ int main(int argc, char** argv) {
if (!g.init()) {
reportError(g.getLastError());
finishLogFile();
e.everythingOK();
return 1;
}

Expand Down Expand Up @@ -720,5 +748,6 @@ int main(int argc, char** argv) {
CoUninitialize();
}
#endif
e.everythingOK();
return 0;
}

0 comments on commit e633550

Please sign in to comment.