From f2140688e781e3fa55ec6e03c12b28b10285faf7 Mon Sep 17 00:00:00 2001 From: Anders Jenbo Date: Wed, 24 Mar 2021 04:25:23 +0100 Subject: [PATCH] Load and save all ini values the same way (#1260) --- Source/diablo.cpp | 50 +++++++++++++++++++------- Source/diablo.h | 37 +++++++++++++++++++ Source/items.cpp | 17 +++------ Source/pack.h | 2 ++ SourceX/DiabloUI/selgame.cpp | 4 +-- SourceX/controls/controller_motion.cpp | 2 +- SourceX/controls/game_controls.cpp | 13 +++---- SourceX/controls/game_controls.h | 2 -- SourceX/controls/modifier_hints.cpp | 2 +- SourceX/controls/touch.cpp | 10 +----- SourceX/display.cpp | 10 ++---- 11 files changed, 91 insertions(+), 58 deletions(-) diff --git a/Source/diablo.cpp b/Source/diablo.cpp index 0451956c5..65521136a 100644 --- a/Source/diablo.cpp +++ b/Source/diablo.cpp @@ -73,14 +73,14 @@ bool gbFriendlyMode = true; /** Specifies players will still damage other players in non-PvP mode. */ bool gbFriendlyFire; /** Default quick messages */ -const char *const spszMsgTbl[4] = { +const char *const spszMsgTbl[] = { "I need help! Come Here!", "Follow me.", "Here's something for you.", "Now you DIE!" }; /** INI files variable names for quick message keys */ -const char *const spszMsgHotKeyTbl[4] = { "F9", "F10", "F11", "F12" }; +const char *const spszMsgHotKeyTbl[] = { "F9", "F10", "F11", "F12" }; /** To know if these things have been done when we get to the diablo_deinit() function */ BOOL was_archives_init = false; @@ -395,6 +395,10 @@ BOOL StartGame(BOOL bNewGame, BOOL bSinglePlayer) */ static void SaveOptions() { + setIniInt("Diablo", "Intro", sgOptions.Diablo.bInto); + setIniInt("Hellfire", "Intro", sgOptions.Hellfire.bInto); + setIniValue("Hellfire", "SItem", sgOptions.Hellfire.szItem); + setIniInt("Audio", "Sound Volume", sgOptions.Audio.nSoundVolume); setIniInt("Audio", "Music Volume", sgOptions.Audio.nMusicVolume); setIniInt("Audio", "Walking Sound", sgOptions.Audio.bWalkingSound); @@ -439,6 +443,17 @@ static void SaveOptions() setIniValue("Network", "Bind Address", sgOptions.Network.szBindAddress); setIniInt("Network", "Port", sgOptions.Network.nPort); + setIniValue("Network", "Previous Host", sgOptions.Network.szPreviousHost); + + for (int i = 0; i < sizeof(spszMsgTbl) / sizeof(spszMsgTbl[0]); i++) + setIniValue("NetMsg", spszMsgHotKeyTbl[i], sgOptions.Chat.szHotKeyMsgs[i]); + + setIniValue("Controller", "Mapping", sgOptions.Controller.szMapping); + setIniInt("Controller", "Swap Shoulder Button Mode", sgOptions.Controller.bSwapShoulderButtonMode); + setIniInt("Controller", "Dpad Hotkeys", sgOptions.Controller.bDpadHotkeys); +#ifdef __vita__ + setIniInt("Controller", "Enable Rear Touchpad", sgOptions.Controller.bRearTouch); +#endif } /** @@ -446,6 +461,10 @@ static void SaveOptions() */ static void LoadOptions() { + sgOptions.Diablo.bInto = getIniBool("Diablo", "Intro", true); + sgOptions.Hellfire.bInto = getIniBool("Hellfire", "Intro", true); + getIniValue("Hellfire", "SItem", sgOptions.Hellfire.szItem, sizeof(sgOptions.Hellfire.szItem), ""); + sgOptions.Audio.nSoundVolume = getIniInt("Audio", "Sound Volume", VOLUME_MAX); sgOptions.Audio.nMusicVolume = getIniInt("Audio", "Music Volume", VOLUME_MAX); sgOptions.Audio.bWalkingSound = getIniBool("Audio", "Walking Sound", true); @@ -495,6 +514,17 @@ static void LoadOptions() getIniValue("Network", "Bind Address", sgOptions.Network.szBindAddress, sizeof(sgOptions.Network.szBindAddress), "0.0.0.0"); sgOptions.Network.nPort = getIniInt("Network", "Port", 6112); + getIniValue("Network", "Previous Host", sgOptions.Network.szPreviousHost, sizeof(sgOptions.Network.szPreviousHost), ""); + + for (int i = 0; i < sizeof(spszMsgTbl) / sizeof(spszMsgTbl[0]); i++) + getIniValue("NetMsg", spszMsgHotKeyTbl[i], sgOptions.Chat.szHotKeyMsgs[i], MAX_SEND_STR_LEN, spszMsgTbl[i]); + + getIniValue("Controller", "Mapping", sgOptions.Controller.szMapping, sizeof(sgOptions.Controller.szMapping), ""); + sgOptions.Controller.bSwapShoulderButtonMode = getIniBool("Controller", "Swap Shoulder Button Mode", false); + sgOptions.Controller.bDpadHotkeys = getIniBool("Controller", "Dpad Hotkeys", false); +#ifdef __vita__ + sgOptions.Controller.bRearTouch = getIniBool("Controller", "Enable Rear Touchpad", true); +#endif } static void diablo_init_screen() @@ -549,13 +579,13 @@ static void diablo_splash() play_movie("gendata\\logo.smk", TRUE); - if (gbIsHellfire && getIniBool("Hellfire", "Intro", true)) { + if (gbIsHellfire && sgOptions.Hellfire.bInto) { play_movie("gendata\\Hellfire.smk", TRUE); - setIniValue("Hellfire", "Intro", "0"); + sgOptions.Hellfire.bInto = false; } - if (!gbIsHellfire && !gbIsSpawn && getIniBool("Diablo", "Intro", true)) { + if (!gbIsHellfire && !gbIsSpawn && sgOptions.Diablo.bInto) { play_movie("gendata\\diablo1.smk", TRUE); - setIniValue("Diablo", "Intro", "0"); + sgOptions.Diablo.bInto = false; } UiTitleDialog(); @@ -847,19 +877,13 @@ void diablo_pause_game() static void diablo_hotkey_msg(DWORD dwMsg) { - char szMsg[MAX_SEND_STR_LEN]; - if (!gbIsMultiplayer) { return; } assert(dwMsg < sizeof(spszMsgTbl) / sizeof(spszMsgTbl[0])); - if (!getIniValue("NetMsg", spszMsgHotKeyTbl[dwMsg], szMsg, MAX_SEND_STR_LEN)) { - snprintf(szMsg, MAX_SEND_STR_LEN, "%s", spszMsgTbl[dwMsg]); - setIniValue("NetMsg", spszMsgHotKeyTbl[dwMsg], szMsg); - } - NetSendCmdString(-1, szMsg); + NetSendCmdString(-1, sgOptions.Chat.szHotKeyMsgs[dwMsg]); } static BOOL PressSysKey(int wParam) diff --git a/Source/diablo.h b/Source/diablo.h index fcc4e45ae..f0288f8b3 100644 --- a/Source/diablo.h +++ b/Source/diablo.h @@ -6,6 +6,7 @@ #ifndef __DIABLO_H__ #define __DIABLO_H__ +#include "pack.h" #ifdef _DEBUG #include "monstdat.h" #endif @@ -23,6 +24,18 @@ extern "C" { #define DEFAULT_HEIGHT 480 #endif +typedef struct DiabloOptions { + /** @brief Play game intro video on startup. */ + bool bInto; +} DiabloOptions; + +typedef struct HellfireOptions { + /** @brief Play game intro video on startup. */ + bool bInto; + /** @brief Corner stone of the world item. */ + char szItem[sizeof(PkItemStruct) * 2 + 1]; +} HellfireOptions; + typedef struct AudioOptions { /** @brief Movie and SFX volume. */ Sint32 nSoundVolume; @@ -102,18 +115,42 @@ typedef struct GameplayOptions { bool bShowMonsterType; } GameplayOptions; +typedef struct ControllerOptions { + /** @brief SDL Controller mapping, see SDL_GameControllerDB. */ + char szMapping[1024]; + /** @brief Use dpad for spell hotkeys without holding "start" */ + bool bDpadHotkeys; + /** @brief Shoulder gamepad shoulder buttons act as potions by default */ + bool bSwapShoulderButtonMode; +#ifdef __vita__ + /** @brief Enable input via rear touchpad */ + bool bRearTouch; +#endif +} ControllerOptions; + typedef struct NetworkOptions { /** @brief Optionally bind to a specific network interface. */ char szBindAddress[129]; + /** @brief Most recently entered Hostname in join dialog. */ + char szPreviousHost[129]; /** @brief What network port to use. */ Uint16 nPort; } NetworkOptions; +typedef struct ChatOptions { + /** @brief Quick chat messages. */ + char szHotKeyMsgs[4][MAX_SEND_STR_LEN]; +} ChatOptions; + typedef struct Options { + DiabloOptions Diablo; + HellfireOptions Hellfire; AudioOptions Audio; GameplayOptions Gameplay; GraphicsOptions Graphics; + ControllerOptions Controller; NetworkOptions Network; + ChatOptions Chat; } Options; extern SDL_Window *ghMainWnd; diff --git a/Source/items.cpp b/Source/items.cpp index 311d04425..0bdc3ef38 100644 --- a/Source/items.cpp +++ b/Source/items.cpp @@ -5,7 +5,6 @@ */ #include #include "all.h" -#include "../3rdParty/Storm/Source/storm.h" DEVILUTION_BEGIN_NAMESPACE @@ -2974,7 +2973,6 @@ void RecreateEar(int ii, WORD ic, int iseed, int Id, int dur, int mdur, int ch, void items_427A72() { PkItemStruct id; - char hexId[sizeof(PkItemStruct) * 2 + 1]; BYTE *buffer; if (CornerStone.activated) { @@ -2982,12 +2980,10 @@ void items_427A72() PackItem(&id, &CornerStone.item); buffer = (BYTE *)&id; for (int i = 0; i < sizeof(PkItemStruct); i++) { - sprintf(&hexId[i * 2], "%02X", buffer[i]); + sprintf(&sgOptions.Hellfire.szItem[i * 2], "%02X", buffer[i]); } - - setIniValue("Hellfire", "SItem", hexId, sizeof(hexId)); } else { - setIniValue("Hellfire", "SItem", "", 1); + sgOptions.Hellfire.szItem[0] = '\0'; } } } @@ -3011,8 +3007,6 @@ void hex2bin(const char *src, int bytes, char *target) void items_427ABA(int x, int y) { PkItemStruct PkSItem; - char hexPkSItem[sizeof(PkItemStruct) * 2 + 1]; - BYTE *buffer; if (CornerStone.activated || x == 0 || y == 0) { return; @@ -3031,13 +3025,10 @@ void items_427ABA(int x, int y) dItem[x][y] = 0; } - if (!getIniValue("Hellfire", "SItem", hexPkSItem, sizeof(hexPkSItem))) - return; - - if (strlen(hexPkSItem) < sizeof(PkItemStruct) * 2) + if (strlen(sgOptions.Hellfire.szItem) < sizeof(PkItemStruct) * 2) return; - hex2bin(hexPkSItem, sizeof(PkItemStruct), (char *)&PkSItem); + hex2bin(sgOptions.Hellfire.szItem, sizeof(PkItemStruct), (char *)&PkSItem); int ii = AllocateItem(); diff --git a/Source/pack.h b/Source/pack.h index 8b2715886..8ea0f8daf 100644 --- a/Source/pack.h +++ b/Source/pack.h @@ -6,6 +6,8 @@ #ifndef __PACK_H__ #define __PACK_H__ +#include "items.h" + DEVILUTION_BEGIN_NAMESPACE #ifdef __cplusplus diff --git a/SourceX/DiabloUI/selgame.cpp b/SourceX/DiabloUI/selgame.cpp index 0550a3a7f..44da9bf4b 100644 --- a/SourceX/DiabloUI/selgame.cpp +++ b/SourceX/DiabloUI/selgame.cpp @@ -69,7 +69,7 @@ void selgame_GameSelection_Init() return; } - getIniValue("Phone Book", "Entry1", selgame_Ip, 128); + strcpy(selgame_Ip, sgOptions.Network.szPreviousHost); selgame_FreeVectors(); @@ -423,7 +423,7 @@ static bool IsGameCompatible(GameData *data) void selgame_Password_Select(int value) { if (selgame_selectedGame) { - setIniValue("Phone Book", "Entry1", selgame_Ip); + strcpy(sgOptions.Network.szPreviousHost, selgame_Ip); if (SNetJoinGame(selgame_selectedGame, selgame_Ip, selgame_Password, NULL, NULL, gdwPlayerId)) { if (!IsGameCompatible(m_client_info->initdata)) { selgame_GameSelection_Select(1); diff --git a/SourceX/controls/controller_motion.cpp b/SourceX/controls/controller_motion.cpp index 46555ffda..aece69f28 100644 --- a/SourceX/controls/controller_motion.cpp +++ b/SourceX/controls/controller_motion.cpp @@ -17,7 +17,7 @@ namespace { // SELECT + D-Pad to simulate right stick movement. bool SimulateRightStickWithDpad(const SDL_Event &event, ControllerButtonEvent ctrl_event) { - if (dpad_hotkeys) + if (sgOptions.Controller.bDpadHotkeys) return false; static bool simulating = false; if (ctrl_event.button == ControllerButton_BUTTON_BACK) { diff --git a/SourceX/controls/game_controls.cpp b/SourceX/controls/game_controls.cpp index e3eb1d649..8b38761d6 100644 --- a/SourceX/controls/game_controls.cpp +++ b/SourceX/controls/game_controls.cpp @@ -15,11 +15,6 @@ namespace dvl { bool start_modifier_active = false; bool select_modifier_active = false; -/** Gamepad dpad acts as hotkeys without holding "start" */ -bool dpad_hotkeys = false; -/** Shoulder gamepad buttons act as potions by default */ -bool switch_potions_and_clicks = false; - namespace { DWORD translate_controller_button_to_key(ControllerButton controller_button) @@ -124,14 +119,14 @@ bool GetGameAction(const SDL_Event &event, ControllerButtonEvent ctrl_event, Gam if (!in_game_menu) { switch (ctrl_event.button) { case ControllerButton_BUTTON_LEFTSHOULDER: - if ((select_modifier_active && !switch_potions_and_clicks) || (switch_potions_and_clicks && !select_modifier_active) ) { + if ((select_modifier_active && !sgOptions.Controller.bSwapShoulderButtonMode) || (sgOptions.Controller.bSwapShoulderButtonMode && !select_modifier_active)) { if (!IsAutomapActive()) *action = GameActionSendMouseClick{ GameActionSendMouseClick::LEFT, ctrl_event.up }; return true; } break; case ControllerButton_BUTTON_RIGHTSHOULDER: - if ((select_modifier_active && !switch_potions_and_clicks) || (switch_potions_and_clicks && !select_modifier_active) ) { + if ((select_modifier_active && !sgOptions.Controller.bSwapShoulderButtonMode) || (sgOptions.Controller.bSwapShoulderButtonMode && !select_modifier_active)) { if (!IsAutomapActive()) *action = GameActionSendMouseClick{ GameActionSendMouseClick::RIGHT, ctrl_event.up }; return true; @@ -161,7 +156,7 @@ bool GetGameAction(const SDL_Event &event, ControllerButtonEvent ctrl_event, Gam default: break; } - if (dpad_hotkeys) { + if (sgOptions.Controller.bDpadHotkeys) { switch (ctrl_event.button) { case ControllerButton_BUTTON_DPAD_UP: if (IsControllerButtonPressed(ControllerButton_BUTTON_BACK)) @@ -352,7 +347,7 @@ bool GetGameAction(const SDL_Event &event, ControllerButtonEvent ctrl_event, Gam AxisDirection GetMoveDirection() { - return controller.GetLeftStickOrDpadDirection(/*allow_dpad=*/!dpad_hotkeys); + return controller.GetLeftStickOrDpadDirection(/*allow_dpad=*/!sgOptions.Controller.bDpadHotkeys); } } // namespace dvl diff --git a/SourceX/controls/game_controls.h b/SourceX/controls/game_controls.h index 6f07f0909..13eb7dac9 100644 --- a/SourceX/controls/game_controls.h +++ b/SourceX/controls/game_controls.h @@ -75,7 +75,5 @@ AxisDirection GetMoveDirection(); extern bool start_modifier_active; extern bool select_modifier_active; -extern bool dpad_hotkeys; -extern bool switch_potions_and_clicks; } // namespace dvl diff --git a/SourceX/controls/modifier_hints.cpp b/SourceX/controls/modifier_hints.cpp index c5290d6d1..10bd33f09 100644 --- a/SourceX/controls/modifier_hints.cpp +++ b/SourceX/controls/modifier_hints.cpp @@ -122,7 +122,7 @@ void DrawSelectModifierMenu(CelOutputBuffer out) { if (!select_modifier_active) return; - if (dpad_hotkeys) + if (sgOptions.Controller.bDpadHotkeys) { static const CircleMenuHint kDpad(/*is_dpad=*/true, /*top=*/"F6", /*right=*/"F8", /*bottom=*/"F7", /*left=*/"F5"); DrawCircleMenuHint(out, kDpad, PANEL_LEFT + kCircleMarginX, PANEL_TOP - kCirclesTop); diff --git a/SourceX/controls/touch.cpp b/SourceX/controls/touch.cpp index 0c588162b..dccb477ef 100644 --- a/SourceX/controls/touch.cpp +++ b/SourceX/controls/touch.cpp @@ -4,11 +4,6 @@ #include "../../defs.h" #include -#ifdef __vita__ -#include "../3rdParty/Storm/Source/storm.h" -static bool back_touch = false; -#endif - static int visible_width; static int visible_height; static int x_borderwidth; @@ -93,9 +88,6 @@ static void init_touch(void) visible_width = (current.h * dvl::gnScreenWidth) / dvl::gnScreenHeight; x_borderwidth = (current.w - visible_width) / 2; y_borderwidth = (current.h - visible_height) / 2; -#ifdef __vita__ - back_touch = dvl::getIniBool("controls", "enable_second_touchscreen", true); -#endif } static void preprocess_events(SDL_Event *event) @@ -114,7 +106,7 @@ static void preprocess_events(SDL_Event *event) SDL_TouchID port = event->tfinger.touchId; if (port != 0) { #ifdef __vita__ - if (back_touch) { + if (dvl::sgOptions.Controller.bRearTouch) { switch (event->type) { case SDL_FINGERDOWN: preprocess_back_finger_down(event); diff --git a/SourceX/display.cpp b/SourceX/display.cpp index 485af7527..c1579c5bd 100644 --- a/SourceX/display.cpp +++ b/SourceX/display.cpp @@ -132,17 +132,11 @@ bool SpawnWindow(const char *lpWindowName) } #ifndef USE_SDL1 - char mapping[1024]; - memset(mapping, 0, 1024); - getIniValue("controls", "sdl2_controller_mapping", mapping, 1024); - if (mapping[0] != '\0') { - SDL_GameControllerAddMapping(mapping); + if (sgOptions.Controller.szMapping[0] != '\0') { + SDL_GameControllerAddMapping(sgOptions.Controller.szMapping); } #endif - dpad_hotkeys = getIniBool("controls", "dpad_hotkeys"); - switch_potions_and_clicks = getIniBool("controls", "switch_potions_and_clicks"); - #ifdef USE_SDL1 SDL_EnableUNICODE(1); #endif