Rename to FoCAPI

This commit is contained in:
2026-03-01 17:10:27 -06:00
parent f7024acd8b
commit 952c233592
14 changed files with 354 additions and 241 deletions

37
functions/file_io.cpp Normal file
View File

@@ -0,0 +1,37 @@
#include "functions.h"
#include "../lua_hook.h"
#include <windows.h>
// WriteToFile(path, text) - appends text to a file
static int L_WriteToFile(lua_State* L) {
const char* path = pfn_tostring(L, 1);
const char* text = pfn_tostring(L, 2);
if (!path || !text) {
return 0;
}
HANDLE hFile = CreateFileA(
path,
GENERIC_WRITE,
FILE_SHARE_READ,
NULL,
OPEN_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
NULL);
if (hFile != INVALID_HANDLE_VALUE) {
DWORD written;
SetFilePointer(hFile, 0, NULL, FILE_END);
WriteFile(hFile, text, lstrlenA(text), &written, NULL);
WriteFile(hFile, "\n", 1, &written, NULL);
CloseHandle(hFile);
}
return 0;
}
void Register_FileIO(lua_State* L) {
register_global(L, "WriteToFile", L_WriteToFile);
}

13
functions/functions.cpp Normal file
View File

@@ -0,0 +1,13 @@
#include "functions.h"
// To add a new Lua function module:
// 1. Create functions/<module>.cpp with L_* implementation(s) and Register_<Module>()
// 2. Declare void Register_<Module>(lua_State* L); in functions.h
// 3. Call Register_<Module>(L); below
// 4. Add new RVAs (if needed) to rvas.h in the relevant section
// 5. Add functions\<module>.cpp to FoCAPI.vcxproj and .vcxproj.filters
void Register_All(lua_State* L) {
Register_FileIO(L);
Register_Players(L);
}

12
functions/functions.h Normal file
View File

@@ -0,0 +1,12 @@
#pragma once
struct lua_State;
// Registers all custom Lua functions into the given state.
// Called once per lua_open() from Hook_lua_open().
void Register_All(lua_State* L);
// Per-module registration functions.
// Each checks its own required function pointer guards before registering.
void Register_FileIO(lua_State* L);
void Register_Players(lua_State* L);

71
functions/players.cpp Normal file
View File

@@ -0,0 +1,71 @@
#include "functions.h"
#include "../lua_hook.h"
#include <windows.h>
// Offsets for reading PlayersByID count directly from the PlayerList object.
// PlayersByID (DynamicVectorClass) is at +0x18 within PlayerListClass.
// The active count field is at +0x10 within DynamicVectorClass.
static constexpr int OFFSET_PLAYERS_BY_ID = 0x18;
static constexpr int OFFSET_VECTOR_COUNT = 0x10;
// Get_All_Players() - returns a 1-based Lua table of PlayerWrappers for all players,
// iterable with ipairs(). Identical wrapper type to Find_Player().
static int L_Get_All_Players(lua_State* L) {
int count = *(int*)((char*)g_player_list + OFFSET_PLAYERS_BY_ID + OFFSET_VECTOR_COUNT);
if (pfn_debug_print) {
char msg[64];
wsprintfA(msg, "[FoCAPI] Get_All_Players: PlayersByID count = %d\n", count);
pfn_debug_print(msg);
}
if (count <= 0 || count > 64) {
if (pfn_debug_print) pfn_debug_print("[FoCAPI] Get_All_Players: count out of expected range, aborting\n");
pfn_newtable(L);
return 1;
}
if (pfn_debug_print) pfn_debug_print("[FoCAPI] Get_All_Players: calling Get_Script_From_State\n");
void* script = pfn_get_script_from_state(L);
if (pfn_debug_print) {
char msg[64];
wsprintfA(msg, "[FoCAPI] Get_All_Players: script = %p\n", script);
pfn_debug_print(msg);
}
if (pfn_debug_print) pfn_debug_print("[FoCAPI] Get_All_Players: calling lua_newtable\n");
pfn_newtable(L);
if (pfn_debug_print) pfn_debug_print("[FoCAPI] Get_All_Players: lua_newtable OK\n");
for (int i = 0; i < count; i++) {
if (pfn_debug_print) {
char msg[64];
wsprintfA(msg, "[FoCAPI] Get_All_Players: Get_Player_By_Index(%d)\n", i);
pfn_debug_print(msg);
}
void* player = pfn_get_player_by_index(g_player_list, i);
if (!player) continue;
if (pfn_debug_print) pfn_debug_print("[FoCAPI] Get_All_Players: PlayerWrapper::Create\n");
void* wrapper = pfn_player_wrapper_create(player, script);
if (pfn_debug_print) {
char msg[128];
wsprintfA(msg, "[FoCAPI] Get_All_Players: wrapper = %p, player = %p\n", wrapper, player);
pfn_debug_print(msg);
}
if (!wrapper) continue;
if (pfn_debug_print) pfn_debug_print("[FoCAPI] Get_All_Players: Map_Var_To_Lua\n");
pfn_map_var_to_lua(L, wrapper);
if (pfn_debug_print) pfn_debug_print("[FoCAPI] Get_All_Players: lua_rawseti\n");
pfn_rawseti(L, -2, i + 1);
}
return 1;
}
void Register_Players(lua_State* L) {
if (!pfn_map_var_to_lua || !pfn_newtable || !pfn_rawseti) return;
register_global(L, "Get_All_Players", L_Get_All_Players);
}