Added functions for reading from sfall INI files more easily;

Refactor Karma module init: use new INI functions, get rid of hacky string split code, removed KarmaFRMsCount (it's redundant)
This commit is contained in:
phobos2077
2017-03-04 19:55:37 +07:00
parent b5a85fd57c
commit 32c1de3a34
16 changed files with 143 additions and 79 deletions
+1 -1
View File
@@ -403,7 +403,7 @@ ExtraSaveSlots=0
SpeedInterfaceCounterAnims=0
;These lines allow you to control the karma frm's displayed on the character screen
;KarmaFRMsCount=3
;Number of KarmaPoints should be 1 less than number of KarmaFRMs
;KarmaFRMs=47,48,49
;KarmaPoints=-100,100
+15
View File
@@ -3,18 +3,25 @@
#include <functional>
#include <vector>
// Simple multi-cast Delegate implementation.
// Removing specific functions after they were added is not supported yet.
template <typename ...ArgT>
class Delegate {
public:
// The type of functions that can be stored in this delegate.
using Functor = std::function<void(ArgT...)>;
// Function collection type.
using FunctorCollection = std::vector<Functor>;
// Creates an empty delegate.
Delegate<ArgT...>() {}
// Add a function to the delegate
void add(Functor func) {
_functors.emplace_back(std::move(func));
}
// Add all functions from another delegate of the same type.
void add(const Delegate<ArgT...>& other)
{
for (auto& func : other.functors())
@@ -23,42 +30,50 @@ public:
}
}
// Remove all functions from this delegate.
void clear() {
_functors.clear();
}
// Invoke all functions in the delegate, passing the arguments provided (if any).
void invoke(ArgT... args) {
for (auto& func : _functors) {
func(args...);
}
}
// The list of currently registered delegates.
const FunctorCollection& functors() const {
return _functors;
}
// Replaces the current list of functions with single function provided.
Delegate operator =(Functor func) {
clear();
add(std::move(func));
return *this;
}
// Removes all functions.
Delegate operator=(std::nullptr_t) {
clear();
return *this;
}
// Adds a single function to the list.
Delegate operator +=(Functor func) {
add(std::move(func));
return *this;
}
// Adds all functions from another delegate.
Delegate operator +=(const Delegate other) {
add(other);
return *this;
}
private:
// The list of functions
FunctorCollection _functors;
};
+7 -7
View File
@@ -29,20 +29,20 @@ static int DebugTypes=0;
static ofstream Log;
void dlog(const char* a, int type) {
if (IsDebug && (type == DL_MAIN || (type & DebugTypes))) {
if (isDebug && (type == DL_MAIN || (type & DebugTypes))) {
Log << a;
Log.flush();
}
}
void dlogr(const char* a, int type) {
if (IsDebug && (type == DL_MAIN || (type & DebugTypes))) {
if (isDebug && (type == DL_MAIN || (type & DebugTypes))) {
Log << a << "\r\n";
Log.flush();
}
}
void dlog_f(const char *fmt, int type, ...) {
if (IsDebug) {
if (isDebug) {
va_list args;
va_start(args, type);
char buf[4096];
@@ -55,16 +55,16 @@ void dlog_f(const char *fmt, int type, ...) {
void LoggingInit() {
Log.open("sfall-log.txt", ios_base::out | ios_base::trunc);
if (GetPrivateProfileIntA("Debugging", "Init", 0, ".\\ddraw.ini")) {
if (GetPrivateProfileIntA("Debugging", "Init", 0, ddrawIni)) {
DebugTypes |= DL_INIT;
}
if (GetPrivateProfileIntA("Debugging", "Hook", 0, ".\\ddraw.ini")) {
if (GetPrivateProfileIntA("Debugging", "Hook", 0, ddrawIni)) {
DebugTypes |= DL_HOOK;
}
if (GetPrivateProfileIntA("Debugging", "Script", 0, ".\\ddraw.ini")) {
if (GetPrivateProfileIntA("Debugging", "Script", 0, ddrawIni)) {
DebugTypes |= DL_SCRIPT;
}
if (GetPrivateProfileIntA("Debugging", "Criticals", 0, ".\\ddraw.ini")) {
if (GetPrivateProfileIntA("Debugging", "Criticals", 0, ddrawIni)) {
DebugTypes |= DL_CRITICALS;
}
}
+2 -2
View File
@@ -77,7 +77,7 @@ void CRC(const char* filepath) {
DWORD size = GetFileSize(h, 0), crc;
bool sizeMatch = (size == ExpectedSize);
if (IsDebug && !sizeMatch && GetPrivateProfileIntA("Debugging", "SkipSizeCheck", 0, ".\\ddraw.ini")) {
if (isDebug && !sizeMatch && GetPrivateProfileIntA("Debugging", "SkipSizeCheck", 0, ".\\ddraw.ini")) {
sizeMatch = true;
}
@@ -91,7 +91,7 @@ void CRC(const char* filepath) {
bool matchedCRC = false;
if (IsDebug && GetPrivateProfileStringA("Debugging", "ExtraCRC", "", buf, 512, ".\\ddraw.ini") > 0) {
if (isDebug && GetPrivateProfileStringA("Debugging", "ExtraCRC", "", buf, 512, ".\\ddraw.ini") > 0) {
char *TestCRC;
TestCRC = strtok(buf, ",");
while (TestCRC) {
+1 -1
View File
@@ -80,7 +80,7 @@ void CritLoad() {
CritStruct& defaultEffect = VarPtr::crit_succ_eff[critter][part][crit];
for (int i = 0; i < 7; i++) {
newEffect.values[i] = GetPrivateProfileIntA(section, CritNames[i], defaultEffect.values[i], ".\\CriticalOverrides.ini");
if (IsDebug) {
if (isDebug) {
char logmsg[256];
if (newEffect.values[i] != defaultEffect.values[i]) {
sprintf_s(logmsg, "Entry %s value %d changed from %d to %d", section, i, defaultEffect.values[i], newEffect.values[i]);
+1 -1
View File
@@ -959,7 +959,7 @@ HRESULT _stdcall FakeDirectDrawCreate2(void*, IDirectDraw** b, void*) {
return DD_OK;
}
static DWORD fadeMulti;
static double fadeMulti;
static __declspec(naked) void FadeHook() {
__asm {
pushf;
+32 -35
View File
@@ -18,22 +18,32 @@
#include <math.h>
#include <stdio.h>
#include <string>
#include <vector>
#include "..\main.h"
#include "..\FalloutEngine\Fallout2.h"
#include "Karma.h"
static DWORD KarmaFrmCount;
static DWORD* KarmaFrms;
static int* KarmaPoints;
struct KarmaFrmSetting {
DWORD frm;
int points;
};
static std::vector<KarmaFrmSetting> karmaFrms;
static std::string karmaGainMsg;
static std::string karmaLossMsg;
static DWORD _stdcall DrawCardHook2() {
int reputation = VarPtr::game_global_vars[GVAR_PLAYER_REPUTATION];
for (DWORD i = 0; i < KarmaFrmCount - 1; i++) {
if (reputation < KarmaPoints[i]) return KarmaFrms[i];
for (auto& info : karmaFrms) {
if (reputation < info.points) {
return info.frm;
}
}
return KarmaFrms[KarmaFrmCount - 1];
return karmaFrms.end()->frm;
}
static void __declspec(naked) DrawCardHook() {
@@ -52,17 +62,15 @@ skip:
}
}
static char KarmaGainMsg[128];
static char KarmaLossMsg[128];
static void _stdcall SetKarma(int value) {
int old = VarPtr::game_global_vars[GVAR_PLAYER_REPUTATION];
old = value - old;
char buf[64];
char buf[128];
if (old == 0) return;
if (old > 0) {
sprintf_s(buf, KarmaGainMsg, old);
sprintf_s(buf, karmaGainMsg.c_str(), old);
} else {
sprintf_s(buf, KarmaLossMsg, -old);
sprintf_s(buf, karmaLossMsg.c_str(), -old);
}
Wrapper::display_print(buf);
}
@@ -83,39 +91,28 @@ end:
void ApplyDisplayKarmaChangesPatch() {
if (GetPrivateProfileInt("Misc", "DisplayKarmaChanges", 0, ini)) {
dlog("Applying display karma changes patch.", DL_INIT);
GetPrivateProfileString("sfall", "KarmaGain", "You gained %d karma.", KarmaGainMsg, 128, translationIni);
GetPrivateProfileString("sfall", "KarmaLoss", "You lost %d karma.", KarmaLossMsg, 128, translationIni);
karmaGainMsg = Translate("sfall", "KarmaGain", "You gained %d karma.");
karmaLossMsg = Translate("sfall", "KarmaLoss", "You lost %d karma.");
HookCall(0x455A6D, SetGlobalVarWrapper);
dlogr(" Done", DL_INIT);
}
}
void ApplyKarmaFRMsPatch() {
KarmaFrmCount = GetPrivateProfileIntA("Misc", "KarmaFRMsCount", 0, ini);
if (KarmaFrmCount) {
KarmaFrms = new DWORD[KarmaFrmCount];
KarmaPoints = new int[KarmaFrmCount - 1];
auto karmaFrmList = GetConfigList("Misc", "KarmaFRMs", "", 512);
if (karmaFrmList.size() > 0) {
dlog("Applying karma frm patch.", DL_INIT);
char buf[512];
GetPrivateProfileStringA("Misc", "KarmaFRMs", "", buf, 512, ini);
char *ptr = buf, *ptr2;
for (DWORD i = 0; i < KarmaFrmCount - 1; i++) {
ptr2 = strchr(ptr, ',');
*ptr2 = '\0';
KarmaFrms[i] = atoi(ptr);
ptr = ptr2 + 1;
auto karmaPointsList = GetConfigList("Misc", "KarmaPoints", "", 512);
karmaFrms.resize(karmaFrmList.size());
for (size_t i = 0; i < karmaFrmList.size(); i++) {
karmaFrms[i].frm = atoi(karmaFrmList[i].c_str());
karmaFrms[i].points = (karmaPointsList.size() > i)
? atoi(karmaPointsList[i].c_str())
: INT_MAX;
}
KarmaFrms[KarmaFrmCount - 1] = atoi(ptr);
GetPrivateProfileStringA("Misc", "KarmaPoints", "", buf, 512, ini);
ptr = buf;
for (DWORD i = 0; i < KarmaFrmCount - 2; i++) {
ptr2 = strchr(ptr, ',');
*ptr2 = '\0';
KarmaPoints[i] = atoi(ptr);
ptr = ptr2 + 1;
}
KarmaPoints[KarmaFrmCount - 2] = atoi(ptr);
HookCall(0x4367A9, DrawCardHook);
dlogr(" Done", DL_INIT);
}
}
+2 -2
View File
@@ -367,7 +367,7 @@ static const DWORD EncounterTableSize[] = {
};
void DebugModePatch() {
if (IsDebug) {
if (isDebug) {
DWORD dbgMode = GetPrivateProfileIntA("Debugging", "DebugMode", 0, ".\\ddraw.ini");
if (dbgMode) {
dlog("Applying debugmode patch.", DL_INIT);
@@ -658,7 +658,7 @@ void DialogueFix() {
}
void DontDeleteProtosPatch() {
if (IsDebug && GetPrivateProfileIntA("Debugging", "DontDeleteProtos", 0, ".\\ddraw.ini")) {
if (isDebug && GetPrivateProfileIntA("Debugging", "DontDeleteProtos", 0, ".\\ddraw.ini")) {
dlog("Applying permanent protos patch.", DL_INIT);
SafeWrite8(0x48007E, 0xeb);
dlogr(" Done", DL_INIT);
+1 -1
View File
@@ -183,7 +183,7 @@ void __declspec(naked) defaultOpcodeHandler() {
}
void InitNewOpcodes() {
bool AllowUnsafeScripting = IsDebug
bool AllowUnsafeScripting = isDebug
&& GetPrivateProfileIntA("Debugging", "AllowUnsafeScripting", 0, ".\\ddraw.ini") != 0;
dlogr("Adding additional opcodes", DL_SCRIPT);
+3 -3
View File
@@ -49,17 +49,17 @@ skip:
void Sound::init() {
int tmp;
if (tmp = GetPrivateProfileIntA("Sound", "NumSoundBuffers", 0, ini)) {
if (tmp = GetConfigInt("Sound", "NumSoundBuffers", 0)) {
SafeWrite8(0x451129, (BYTE)tmp);
}
if (GetPrivateProfileIntA("Sound", "AllowSoundForFloats", 0, ini)) {
if (GetConfigInt("Sound", "AllowSoundForFloats", 0)) {
HookCall(0x42B7C7, MsgCopy);
HookCall(0x42B849, DisplayMsg);
}
//Yes, I did leave this in on purpose. Will be of use to anyone trying to add in the sound effects
if (GetPrivateProfileIntA("Sound", "Test_ForceFloats", 0, ini)) {
if (GetConfigInt("Sound", "Test_ForceFloats", 0)) {
SafeWrite8(0x42B772, 0xeb);
}
+9
View File
@@ -0,0 +1,9 @@
#include <iterator>
#include "Utils.h"
std::vector<std::string> split(const std::string &s, char delim) {
std::vector<std::string> elems;
split(s, delim, std::back_inserter(elems));
return elems;
}
+19
View File
@@ -0,0 +1,19 @@
#pragma once
#include <string>
#include <sstream>
#include <vector>
// splits a string by given delimeter
// taken from: http://stackoverflow.com/a/236803/4331475
template<typename Out>
void split(const std::string &s, char delim, Out result) {
std::stringstream ss;
ss.str(s);
std::string item;
while (std::getline(ss, item, delim)) {
*(result++) = item;
}
}
std::vector<std::string> split(const std::string &s, char delim);
+2
View File
@@ -279,6 +279,7 @@
<ClInclude Include="Modules\Sound.h" />
<ClInclude Include="Modules\Stats.h" />
<ClInclude Include="Modules\Tiles.h" />
<ClInclude Include="Utils.h" />
<ClInclude Include="Version.h" />
<ClInclude Include="Logging.h" />
<ClInclude Include="resource.h" />
@@ -357,6 +358,7 @@
<ClCompile Include="Modules\Tiles.cpp" />
<ClCompile Include="Logging.cpp" />
<ClCompile Include="Modules\ExtraSaveSlots.cpp" />
<ClCompile Include="Utils.cpp" />
</ItemGroup>
<ItemGroup>
<None Include="exports.def" />
+2
View File
@@ -238,6 +238,7 @@
<ClInclude Include="Modules\Input.h">
<Filter>Modules</Filter>
</ClInclude>
<ClInclude Include="Utils.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="main.cpp" />
@@ -444,6 +445,7 @@
<ClCompile Include="Modules\Input.cpp">
<Filter>Modules</Filter>
</ClCompile>
<ClCompile Include="Utils.cpp" />
</ItemGroup>
<ItemGroup>
<ResourceCompile Include="version.rc" />
+35 -24
View File
@@ -16,8 +16,6 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "main.h"
#include <stdio.h>
#include <memory>
@@ -68,13 +66,41 @@
#include "SimplePatch.h"
#include "Logging.h"
#include "Utils.h"
#include "Version.h"
bool IsDebug = false;
#include "main.h"
bool isDebug = false;
const char ddrawIni[] = ".\\ddraw.ini";
char ini[65];
char translationIni[65];
unsigned int GetConfigInt(const char* section, const char* setting, int defaultValue) {
return GetPrivateProfileIntA(section, setting, defaultValue, ini);
}
std::string GetIniString(const char* section, const char* setting, const char* defaultValue, size_t bufSize, const char* iniFile) {
char* buf = new char[bufSize];
GetPrivateProfileStringA(section, setting, defaultValue, buf, bufSize, iniFile);
std::string str(buf);
delete[] buf;
return str;
}
std::string GetConfigString(const char* section, const char* setting, const char* defaultValue, size_t bufSize) {
return GetIniString(section, setting, defaultValue, bufSize, ::ini);
}
std::vector<std::string> GetConfigList(const char* section, const char* setting, const char* defaultValue, size_t bufSize) {
return split(GetConfigString(section, setting,defaultValue, bufSize), ',');
}
std::string Translate(const char* section, const char* setting, const char* defaultValue, size_t bufSize) {
return GetIniString(section, setting, defaultValue, bufSize, ::translationIni);
}
static void DllMain2() {
dlogr("In DllMain2", DL_MAIN);
@@ -127,19 +153,6 @@ static void DllMain2() {
dlogr("Leave DllMain2", DL_MAIN);
}
// TODO: remove
static void _stdcall OnExit() {
}
static void __declspec(naked) OnExitFunc() {
__asm {
pushad;
call OnExit;
popad;
jmp FuncOffs::DOSCmdLineDestroy_;
}
}
static void CompatModeCheck(HKEY root, const char* filepath, int extra) {
HKEY key;
char buf[MAX_PATH];
@@ -167,19 +180,17 @@ static void CompatModeCheck(HKEY root, const char* filepath, int extra) {
bool _stdcall DllMain(HANDLE hDllHandle, DWORD dwReason, LPVOID lpreserved) {
if (dwReason == DLL_PROCESS_ATTACH) {
// enabling debugging features
IsDebug = (GetPrivateProfileIntA("Debugging", "Enable", 0, ".\\ddraw.ini") != 0);
if (IsDebug) {
isDebug = (GetPrivateProfileIntA("Debugging", "Enable", 0, ddrawIni) != 0);
if (isDebug) {
LoggingInit();
}
HookCall(0x4DE7D2, &OnExitFunc);
char filepath[MAX_PATH];
GetModuleFileName(0, filepath, MAX_PATH);
CRC(filepath);
if (!IsDebug || !GetPrivateProfileIntA("Debugging", "SkipCompatModeCheck", 0, ".\\ddraw.ini")) {
if (!isDebug || !GetPrivateProfileIntA("Debugging", "SkipCompatModeCheck", 0, ddrawIni)) {
int is64bit;
typedef int (_stdcall *chk64bitproc)(HANDLE, int*);
HMODULE h=LoadLibrary("Kernel32.dll");
@@ -195,7 +206,7 @@ bool _stdcall DllMain(HANDLE hDllHandle, DWORD dwReason, LPVOID lpreserved) {
// ini file override
bool cmdlineexists = false;
char* cmdline = GetCommandLineA();
if (GetPrivateProfileIntA("Main", "UseCommandLine", 0, ".\\ddraw.ini")) {
if (GetPrivateProfileIntA("Main", "UseCommandLine", 0, ddrawIni)) {
while (cmdline[0] == ' ') cmdline++;
bool InQuote = false;
int count = -1;
@@ -225,10 +236,10 @@ bool _stdcall DllMain(HANDLE hDllHandle, DWORD dwReason, LPVOID lpreserved) {
else {
MessageBox(0, "You gave a command line argument to fallout, but it couldn't be matched to a file\n" \
"Using default ddraw.ini instead", "Warning", MB_TASKMODAL);
strcpy_s(ini, ".\\ddraw.ini");
strcpy_s(ini, ddrawIni);
}
} else {
strcpy_s(ini, ".\\ddraw.ini");
strcpy_s(ini, ddrawIni);
}
GetPrivateProfileStringA("Main", "TranslationsINI", "./Translations.ini", translationIni, 65, ini);
+11 -2
View File
@@ -20,15 +20,24 @@
#include <assert.h>
//#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#include <string>
#include <vector>
#include "SafeWrite.h"
#include "Logging.h"
// global flag, indicating that debugging features of Sfall are enabled
#ifndef NO_SFALL_DEBUG
extern bool IsDebug;
extern bool isDebug;
#else
#define IsDebug false
#define isDebug false
#endif
unsigned int GetConfigInt(const char* section, const char* setting, int defaultValue);
std::string GetConfigString(const char* section, const char* setting, const char* defaultValue, size_t bufSize = 128);
std::vector<std::string> GetConfigList(const char* section, const char* setting, const char* defaultValue, size_t bufSize = 128);
std::string Translate(const char* section, const char* setting, const char* defaultValue, size_t bufSize = 128);
extern const char ddrawIni[];
extern char ini[65];
extern char translationIni[65];