Spellforce-Spell-framework
Loading...
Searching...
No Matches
sf_modloader.c
Go to the documentation of this file.
1#include <windows.h>
2#include <stdio.h>
3#include <stdlib.h>
4#include <string.h>
5
6#include "sf_wrappers.h"
7#include "sf_modloader.h"
8
10
11typedef void (*InitModuleFunc)(void *);
12typedef SFMod *(*RegisterModFunc)(void *);
15
16void cleanup(void *modHandle)
17{
18 // Free resources (unload mod library using FreeLibrary)
19 if (modHandle)
20 {
21 FreeLibrary((HMODULE)modHandle);
22 }
23}
24
25// Function to extract filename from the path
26const char *get_filename(const char *path)
27{
28 const char *filename = strrchr(path, '\\'); // Find the last occurrence of '\\' in the path
29 return (filename) ? (filename + 1) : path;
30}
31
32void load_mod(const char *modPath, void *pFrameworkAPI)
33{
34 HMODULE modHandle = LoadLibrary(modPath);
35 if (!modHandle)
36 {
37 log_error("| - Failed to load mod library (X_X)");
38 return;
39 }
40
41 RegisterModFunc registerMod =
42 (RegisterModFunc) GetProcAddress(modHandle, "RegisterMod");
43 InitModuleFunc initModule =
44 (InitModuleFunc) GetProcAddress(modHandle, "InitModule");
45
46 if (!initModule)
47 {
48 log_warning(get_filename(modPath));
49 log_error("| - Failed to get address of InitModule (X_X)");
50 cleanup(modHandle);
51 return;
52 }
53
54 if (!registerMod)
55 {
56 char warn[256];
57 snprintf(warn, sizeof(warn),
58 "| - Failed to Initialize %s has erroneous mod data. (0_0)",
59 get_filename(modPath));
60 log_warning(warn);
61 log_error("| - Failed to get address of RegisterMod (X_X)");
62 g_error_count += 1;
63 return;
64 }
65
66 g_current_mod = registerMod(pFrameworkAPI);
67 initModule(pFrameworkAPI);
68 g_mod_count += 1;
69 char infomsg[256];
70 snprintf(infomsg, sizeof(infomsg), "| - [Initialized Mod: %s (Ver. %s)]",
72 log_info(infomsg);
73 return;
74}
75
76void load_all_mods(const char *subfolder, void *pFrameworkAPI)
77{
78 char currentDir[MAX_PATH];
79 GetCurrentDirectory(MAX_PATH, currentDir);
80
81 char modDirectory[MAX_PATH];
82 snprintf(modDirectory, sizeof(modDirectory), "%s\\%s", currentDir,
83 subfolder);
84
85 WIN32_FIND_DATA findFileData;
86 char searchPath[MAX_PATH];
87 snprintf(searchPath, sizeof(searchPath), "%s\\*.sfm", modDirectory);
88
89 HANDLE hFind = FindFirstFile(searchPath, &findFileData);
90 if (hFind != INVALID_HANDLE_VALUE)
91 {
92 do
93 {
94 char modPath[MAX_PATH];
95 snprintf(modPath, sizeof(modPath), "%s\\%s", modDirectory,
96 findFileData.cFileName);
97 load_mod(modPath, pFrameworkAPI);
98 } while (FindNextFile(hFind, &findFileData) != 0);
99 FindClose(hFind);
100 }
101 else
102 {
103 char msgbuf[MAX_PATH];
104 snprintf(msgbuf, sizeof(msgbuf),
105 "| - Failed to find mods in directory: %s", modDirectory);
106 log_error(msgbuf);
107 }
108}
109
111{
112 load_all_mods("sfsf", &frameworkAPI);
113 static char info_str[256];
114 snprintf(info_str, sizeof(info_str),
115 "| - %d Mods Initialized with %d error(s)", g_mod_count,
117 log_info(info_str);
118}
void log_info(const char *message)
Definition sf_wrappers.c:89
void log_warning(const char *message)
Definition sf_wrappers.c:81
void log_error(const char *message)
Definition sf_wrappers.c:97
void load_all_mods(const char *subfolder, void *pFrameworkAPI)
SFMod *(* RegisterModFunc)(void *)
int g_mod_count
void initialize_mods()
void(* InitModuleFunc)(void *)
void load_mod(const char *modPath, void *pFrameworkAPI)
const char * get_filename(const char *path)
int g_error_count
void cleanup(void *modHandle)
SFMod * g_current_mod
SpellforceSpellFramework frameworkAPI
char mod_id[64]
char mod_version[24]
Represents the Spellforce Spell Framework API.This structure serves as the central interface for inte...
Definition sfsf.h:36