From 09813aeb8657768159f9b44123051a514562585c Mon Sep 17 00:00:00 2001 From: NovaRain Date: Mon, 8 Nov 2021 13:45:35 +0800 Subject: [PATCH] Code edits to ScriptExtender.cpp/SpeedPatch.cpp Minor edits to some files. --- artifacts/ddraw.ini | 6 +-- sfall/IniReader.cpp | 2 +- sfall/Modules/ScriptExtender.cpp | 6 +-- sfall/Modules/SpeedPatch.cpp | 73 +++++++++++++++++--------------- sfall/SafeWrite.cpp | 6 +-- sfall/Utils.cpp | 18 ++++---- sfall/Utils.h | 4 +- 7 files changed, 59 insertions(+), 56 deletions(-) diff --git a/artifacts/ddraw.ini b/artifacts/ddraw.ini index 0ce3397b..b9f92c3b 100644 --- a/artifacts/ddraw.ini +++ b/artifacts/ddraw.ini @@ -26,7 +26,7 @@ UseCommandLine=0 ;XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX [Speed] -;Set to 0 to disable everything in this section +;Set to 1 to enable options related to the game speed adjustment (SpeedMulti#, SpeedKey#, SpeedModKey, and SpeedToggleKey) Enable=1 ;The speeds corresponding to each slot in percent. (i.e. 100 is normal speed) @@ -620,9 +620,9 @@ AttackComplexFix=0 ;Note that enabling this option may cause unexpected results with some existing game scripts CreateObjectSidFix=0 -;Set to 1 to make grave type containers in the open state run the 'use_p_proc' procedure of attached script when being used +;Set to 1 to fix the execution of the 'use_p_proc' procedure for grave type containers when they are in the open state ;By default, using open graves does not run the 'use_p_proc' procedure of attached script like other containers -;Note that enabling this option will cause problems for many existing graves +;Note that enabling this option will cause problems for existing grave scripts GraveUseProcFix=0 ;Set to 1 to fix the issue with the division operator treating negative integers as unsigned diff --git a/sfall/IniReader.cpp b/sfall/IniReader.cpp index d2ca1e3a..4024e8fb 100644 --- a/sfall/IniReader.cpp +++ b/sfall/IniReader.cpp @@ -46,7 +46,7 @@ static std::string getString(const char* section, const char* setting, const cha static std::vector getList(const char* section, const char* setting, const char* defaultValue, size_t bufSize, char delimiter, const char* iniFile) { auto list = split(getString(section, setting, defaultValue, bufSize, iniFile), delimiter); - std::transform(list.cbegin(), list.cend(), list.begin(), trim); + std::transform(list.cbegin(), list.cend(), list.begin(), (std::string (*)(const std::string&))trim); return list; } diff --git a/sfall/Modules/ScriptExtender.cpp b/sfall/Modules/ScriptExtender.cpp index ec360a84..b8d8201d 100644 --- a/sfall/Modules/ScriptExtender.cpp +++ b/sfall/Modules/ScriptExtender.cpp @@ -430,6 +430,7 @@ void __fastcall SetSelfObject(fo::Program* script, fo::GameObject* obj) { // loads script from .int file into a ScriptProgram struct, filling script pointer and proc lookup table void InitScriptProgram(ScriptProgram &prog, const char* fileName, bool fullPath) { + prog.initialized = false; fo::Program* scriptPtr = (fullPath) ? fo::func::allocateProgram(fileName) : fo::func::loadProgram(fileName); @@ -441,14 +442,13 @@ void InitScriptProgram(ScriptProgram &prog, const char* fileName, bool fullPath) for (int i = 0; i < fo::Scripts::ScriptProc::count; ++i) { prog.procLookup[i] = fo::func::interpretFindProcedure(prog.ptr, procTable[i]); } - prog.initialized = false; } else { prog.ptr = nullptr; } } void RunScriptProgram(ScriptProgram &prog) { - if (!prog.initialized) { + if (!prog.initialized && prog.ptr) { fo::func::runProgram(prog.ptr); fo::func::interpret(prog.ptr, -1); prog.initialized = true; @@ -565,8 +565,8 @@ static void PrepareGlobalScriptsListByMask() { } } fo::func::db_free_file_list(&filenames, 0); - globalScripts.reserve(globalScriptFilesList.size()); } + globalScripts.reserve(globalScriptFilesList.size()); } // this runs before the game was loaded/started diff --git a/sfall/Modules/SpeedPatch.cpp b/sfall/Modules/SpeedPatch.cpp index beffe54d..ccac714d 100644 --- a/sfall/Modules/SpeedPatch.cpp +++ b/sfall/Modules/SpeedPatch.cpp @@ -176,6 +176,8 @@ void TimerInit() { GetLocalTime(&time); SystemTimeToFileTime(&time, (FILETIME*)&startTime); + if (!modKey[0]) return; + speed = new SpeedCfg[10]; char buf[2], spKey[10] = "SpeedKey#"; @@ -189,45 +191,46 @@ void TimerInit() { } void SpeedPatch::init() { + int init = IniReader::GetConfigInt("Speed", "SpeedMultiInitial", 100); + if (IniReader::GetConfigInt("Speed", "Enable", 0)) { modKey[0] = IniReader::GetConfigInt("Input", "SpeedModKey", 0); - int init = IniReader::GetConfigInt("Speed", "SpeedMultiInitial", 100); - if (init == 100 && !modKey[0]) return; - - dlog("Applying speed patch.", DL_INIT); - - switch (modKey[0]) { - case -1: - modKey[0] = DIK_LCONTROL; - modKey[1] = DIK_RCONTROL; - break; - case -2: - modKey[0] = DIK_LMENU; - modKey[1] = DIK_RMENU; - break; - case -3: - modKey[0] = DIK_LSHIFT; - modKey[1] = DIK_RSHIFT; - break; - default: - modKey[1] = 0; - } - - multi = init / 100.0f; toggleKey = IniReader::GetConfigInt("Input", "SpeedToggleKey", 0); - - getTickCountOffs = (DWORD)&FakeGetTickCount; - getLocalTimeOffs = (DWORD)&FakeGetLocalTime; - - for (int i = 0; i < sizeof(offsets) / 4; i++) { - SafeWrite32(offsets[i], (DWORD)&getTickCountOffs); - } - SafeWrite32(0x4FDF58, (DWORD)&getLocalTimeOffs); - HookCall(0x4A433E, scripts_check_state_hook); - - TimerInit(); - dlogr(" Done", DL_INIT); } + if (init == 100 && !modKey[0]) return; + + dlog("Applying speed patch.", DL_INIT); + + switch (modKey[0]) { + case -1: + modKey[0] = DIK_LCONTROL; + modKey[1] = DIK_RCONTROL; + break; + case -2: + modKey[0] = DIK_LMENU; + modKey[1] = DIK_RMENU; + break; + case -3: + modKey[0] = DIK_LSHIFT; + modKey[1] = DIK_RSHIFT; + break; + default: + modKey[1] = 0; + } + + multi = init / 100.0f; + + getTickCountOffs = (DWORD)&FakeGetTickCount; + getLocalTimeOffs = (DWORD)&FakeGetLocalTime; + + for (int i = 0; i < sizeof(offsets) / 4; i++) { + SafeWrite32(offsets[i], (DWORD)&getTickCountOffs); + } + SafeWrite32(0x4FDF58, (DWORD)&getLocalTimeOffs); + HookCall(0x4A433E, scripts_check_state_hook); + + TimerInit(); + dlogr(" Done", DL_INIT); } void SpeedPatch::exit() { diff --git a/sfall/SafeWrite.cpp b/sfall/SafeWrite.cpp index 5ace5e00..e8bdb3a1 100644 --- a/sfall/SafeWrite.cpp +++ b/sfall/SafeWrite.cpp @@ -24,9 +24,9 @@ static __declspec(noinline) void __stdcall SafeWriteFunc(BYTE code, DWORD addr, *((BYTE*)addr) = code; *((DWORD*)(addr + 1)) = data; - for (size_t i = 0; i < len; i++) { - *((BYTE*)(addrMem + i)) = CodeType::Nop; - } + do { + *((BYTE*)addrMem++) = CodeType::Nop; + } while (--len); VirtualProtect((void*)addr, protectLen, oldProtect, &oldProtect); CheckConflict(addr, protectLen); diff --git a/sfall/Utils.cpp b/sfall/Utils.cpp index b5d6bdc7..b19b8a73 100644 --- a/sfall/Utils.cpp +++ b/sfall/Utils.cpp @@ -29,15 +29,7 @@ std::string trim(const std::string& str) { return str.substr(first, (last - first + 1)); } -void ToLowerCase(std::string& line) { - std::transform(line.begin(), line.end(), line.begin(), ::tolower); -} - -bool isSpace(char c) { - return (c == ' ' || c == '\t' /*|| c == '\n' || c == '\r'*/); -} - -void strtrim(char* str) { +void trim(char* str) { if (str[0] == 0) return; int len = strlen(str) - 1; @@ -55,6 +47,14 @@ void strtrim(char* str) { } } +void ToLowerCase(std::string& line) { + std::transform(line.begin(), line.end(), line.begin(), ::tolower); +} + +bool isSpace(char c) { + return (c == ' ' || c == '\t' /*|| c == '\n' || c == '\r'*/); +} + // returns position, find word must be lowercase const char* strfind(const char* source, const char* word) { if (source == 0 || word == 0 || *word == 0) return 0; diff --git a/sfall/Utils.h b/sfall/Utils.h index 4ec24d87..16c51359 100644 --- a/sfall/Utils.h +++ b/sfall/Utils.h @@ -29,12 +29,12 @@ std::vector split(const std::string &s, char delim); std::string trim(const std::string& str); +void trim(char* str); + void ToLowerCase(std::string& line); bool isSpace(char c); -void strtrim(char* str); - const char* strfind(const char* source, const char* word); void StrNormalizePath(char* path);