Skip to content

Commit 383c870

Browse files
Evasion3356Evasion3356
authored andcommitted
Updated LuaFunctions to check if it needs to wait first.
Added timedatestamp to Log function. Added LUA_ERRRUN scenario to ExecuteLua. Updated files.
1 parent 631b8d7 commit 383c870

File tree

4 files changed

+97
-40
lines changed

4 files changed

+97
-40
lines changed

M2DEScriptHook/src/LuaFunctions.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,6 @@ int32_t LuaFunctions::PrintToLog(lua_State *L)
220220

221221
int32_t LuaFunctions::BindKey(lua_State *L)
222222
{
223-
M2DEScriptHook::instance()->Log(__FUNCTION__);
224223
const char *key = "";
225224
const char *context = "";
226225

@@ -238,7 +237,6 @@ int32_t LuaFunctions::BindKey(lua_State *L)
238237

239238
int32_t LuaFunctions::UnbindKey(lua_State *L)
240239
{
241-
M2DEScriptHook::instance()->Log(__FUNCTION__);
242240
const char *key = "";
243241
const char *context = "";
244242

@@ -309,11 +307,12 @@ LuaFunctions::LuaFunctions()
309307
{
310308
M2DEScriptHook::instance()->Log(__FUNCTION__);
311309
// Yep, it's thread blocking, but that's what I want, no processing of other stuff until this shit's ready..
312-
do {
310+
while (!LoadPointers())
311+
{
313312
M2DEScriptHook::instance()->Log(__FUNCTION__ " Game is not ready, script engine not initialized, retry");
314313
std::this_thread::sleep_for(std::chrono::milliseconds(100));
315314
std::this_thread::yield();
316-
} while (!LoadPointers());
315+
}
317316
}
318317

319318
bool LuaFunctions::IsMainScriptMachineReady()

M2DEScriptHook/src/M2DEScriptHook.cpp

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
#include <fstream>
3737
#include <thread>
3838
#include <chrono>
39+
#include <iomanip>
3940
#include <algorithm>
4041

4142
#include <M2DEScriptHook.h>
@@ -111,11 +112,16 @@ void M2DEScriptHook::Log(const char* string, ...)
111112

112113
currentBuffer = (currentBuffer + 1) % BUFFER_COUNT;
113114

114-
const char* msg = &buffer[thisBuffer * BUFFER_LENGTH];
115+
const char* msg = &buffer[thisBuffer * BUFFER_LENGTH];
115116

116117
std::fstream file("ScriptHook.log", std::ios::out | std::ios::app);
117-
file << msg;
118-
file << "\n";
118+
119+
auto now = std::chrono::system_clock::now();
120+
auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch()) % 1000;
121+
auto timer = std::chrono::system_clock::to_time_t(now);
122+
auto local_time = *std::localtime(&timer);
123+
124+
file << "[" << std::put_time(&local_time, "%m/%d/%Y %I:%M:%S") << ":" << std::setfill('0') << std::setw(3) << ms.count() << " " << std::put_time(&local_time, "%p") << "] " << msg << std::endl;
119125
file.close();
120126
}
121127

@@ -215,6 +221,15 @@ bool M2DEScriptHook::ExecuteLua(lua_State *L, const std::string &lua)
215221
this->Log("Error loading Lua code into buffer with (Memory Allocation Error)");
216222
return false;
217223
}
224+
else if (LUA_ERRRUN == result)
225+
{
226+
std::stringstream ss;
227+
ss << "Runtime Lua error. Message: ";
228+
const char* error = lua_tostring_(L, -1);
229+
ss << error;
230+
this->Log(ss.str());
231+
return false;
232+
}
218233
else
219234
{
220235
std::stringstream ss;
@@ -229,12 +244,13 @@ bool M2DEScriptHook::ExecuteLua(lua_State *L, const std::string &lua)
229244
return true;
230245
}
231246

247+
232248
uint32_t WINAPI M2DEScriptHook::mainThread(LPVOID) {
233249
static M2DEScriptHook *instance = M2DEScriptHook::instance();
234250

235251
instance->Log(__FUNCTION__);
236252

237-
std::this_thread::sleep_for(std::chrono::milliseconds(10000));
253+
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
238254

239255
while (!instance->HasEnded()) {
240256
std::this_thread::sleep_for(std::chrono::milliseconds(10));
@@ -274,7 +290,7 @@ void M2DEScriptHook::Shutdown()
274290

275291
void M2DEScriptHook::CreateKeyBind(const char *key, const char *context)
276292
{
277-
this->Log(__FUNCTION__);
293+
M2DEScriptHook::instance()->Log("Binding key %s to function %s", key, context);
278294
std::unique_lock<std::recursive_mutex> lkScr(_keyBindMutex);
279295

280296
bool found = false;
@@ -296,7 +312,7 @@ void M2DEScriptHook::CreateKeyBind(const char *key, const char *context)
296312

297313
void M2DEScriptHook::DestroyKeyBind(const char *key, const char *context)
298314
{
299-
this->Log(__FUNCTION__);
315+
M2DEScriptHook::instance()->Log("Unbinding key %", key);
300316
//std::lock_guard<std::mutex> lk{ _keyBindMutex };
301317
std::unique_lock<std::recursive_mutex> lkScr(_keyBindMutex);
302318

files/README.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ Place the files in the Mafia 2 Definitive Edition *pc* folder so the structure l
22

33
| pc
44
| - Mafia 2 Definitive Edition.exe
5-
| - M2DEScriptHook.dll
6-
| - M2DEScriptHookLauncher.exe
5+
| - dxgi.dll
76
| - scripts/*.lua
87
| - plugins/*.dll
98

files/scripts/dev.lua

Lines changed: 71 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,81 @@
1-
godMode = [[
1+
function DoGodmode()
22
game.game:GetActivePlayer():SetDemigod(true)
33
game.game:GetActivePlayer():EnableInjury(false)
44
game.game:GetActivePlayer().invulnerability = true
5-
]]
6-
7-
8-
allWeapons = [[
9-
game.game:GetActivePlayer():InventoryAddWeapon(2,42)
10-
game.game:GetActivePlayer():InventoryAddWeapon(3,60)
11-
game.game:GetActivePlayer():InventoryAddWeapon(4,56)
12-
game.game:GetActivePlayer():InventoryAddWeapon(5,96)
13-
game.game:GetActivePlayer():InventoryAddWeapon(6,42)
14-
game.game:GetActivePlayer():InventoryAddWeapon(8,56)
15-
game.game:GetActivePlayer():InventoryAddWeapon(9,120)
16-
game.game:GetActivePlayer():InventoryAddWeapon(10,128)
17-
game.game:GetActivePlayer():InventoryAddWeapon(11,150)
18-
game.game:GetActivePlayer():InventoryAddWeapon(12,120)
19-
game.game:GetActivePlayer():InventoryAddWeapon(13,120)
20-
game.game:GetActivePlayer():InventoryAddWeapon(15,40)
21-
game.game:GetActivePlayer():InventoryAddWeapon(17,35)
22-
game.game:GetActivePlayer():InventoryAddWeapon(20,6)
23-
game.game:GetActivePlayer():InventoryAddWeapon(21,6)
24-
]]
25-
26-
addMoney = [[
5+
end
6+
7+
function giveGuns()
8+
local weapons = {
9+
{ ID = 2, ammo = 42 }, -- Model_12_Revolver
10+
{ ID = 3, ammo = 60 }, -- Mauser_C96
11+
{ ID = 4, ammo = 56 }, -- Colt_M1911A1
12+
{ ID = 5, ammo = 92 }, -- Colt_M1911_Special
13+
{ ID = 6, ammo = 42 }, -- Model_19_Revolver
14+
{ ID = 8, ammo = 56 }, -- Remington_Model_870_Field_gun
15+
{ ID = 9, ammo = 120 }, -- M3_Grease_Gun
16+
{ ID = 10, ammo = 128 }, -- MP40
17+
{ ID = 11, ammo = 200 }, -- Thompson_1928
18+
{ ID = 12, ammo = 120 }, -- M1A1_Thompson
19+
{ ID = 13, ammo = 120 }, -- Beretta_Model_38A
20+
{ ID = 15, ammo = 40 }, -- M1_Garand
21+
{ ID = 17, ammo = 35 }, -- Kar98k
22+
{ ID = 20, ammo = 6 }, -- MK2_Frag_Grenade
23+
{ ID = 21, ammo = 6 } -- Molotov_Cocktail
24+
}
25+
26+
local player = game.game:GetActivePlayer()
27+
for _, weapon in pairs(weapons) do
28+
player:InventoryAddWeapon(weapon.ID, weapon.ammo)
29+
player:InventoryReload(weapon.ID, weapon.ammo) --Praise the lord, and pass the ammunition.
30+
end
31+
end
32+
33+
function FixCar()
34+
local veh = game.game:GetActivePlayer():GetOwner()
35+
if veh then
36+
local level = veh:GetActualTuningTable()
37+
if level < 3 then
38+
veh:SetActualTuningTable(3)
39+
end
40+
local damage = veh:GetDamage()
41+
ai.police:ClearKnownCars()
42+
if damage > 0 then
43+
local carSpeed = veh:GetSpeedFloat()
44+
veh:RepairAndClear()
45+
if carSpeed > 1 then
46+
veh:SetSpeed(carSpeed)
47+
end
48+
end
49+
local dirt = veh:GetDirty()
50+
if (dirt > 0) then
51+
veh:SetDirty(0)
52+
end
53+
local motorDamage = veh:GetMotorDamage()
54+
if motorDamage > 0 then
55+
veh:SetMotorDamage(0)
56+
end
57+
end
58+
end
59+
60+
function AddMoneyToPlayer()
2761
game.game:GetActivePlayer():InventoryAddMoney(100*1000000)
28-
]]
62+
end
63+
64+
RepairCar = [[DelayBuffer:Insert(FixCar, nil, 100, 1, false)]] --We must use DelayBuffer, or else the user could execute too many scripts and crash the game.
65+
allWeapons = [[DelayBuffer:Insert(giveGuns, nil, 100, 1, false)]] --We must use DelayBuffer, or else the user could execute too many scripts and crash the game.
66+
godMode = [[DelayBuffer:Insert(DoGodmode, nil, 100, 1, false)]] --We must use DelayBuffer, or else the user could execute too many scripts and crash the game.
67+
addMoney = [[DelayBuffer:Insert(AddMoneyToPlayer, nil, 100, 1, false)]] --We must use DelayBuffer, or else the user could execute too many scripts and crash the game.
68+
69+
--For a list of functions and how to use them, please see this github documentation by NobleFalcon:
70+
--https://github.com/NobleFalcon/mafia2-scripts?tab=readme-ov-file#core-objects
2971

30-
-- More scripting funcs can be found here: https://www.nexusmods.com/mafia2/mods/130
3172

3273
unbindKey("p")
74+
bindKey("p", godMode)
3375
unbindKey("o")
76+
bindKey("o", allWeapons)
3477
unbindKey("i")
78+
bindKey("i", addMoney)
3579

36-
bindKey("p", godMode)
37-
bindKey("o", allWeapons)
38-
bindKey("i", addMoney)
80+
unbindKey("VK_MULTIPLY")
81+
bindKey("VK_MULTIPLY", RepairCar)

0 commit comments

Comments
 (0)