aboutsummaryrefslogtreecommitdiff
path: root/src/mod-injector.c
blob: 849b9e62c8632cd2e9d38e44b7fa823c99418ec7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/*
    Copyright (C) 2026 iceyrazor

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; version 2 of the License.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License along
    with this program; if not, see <https://www.gnu.org/licenses/>.
*/

#include <stdio.h>
#include <string.h>
#include <dirent.h>

#define str_size 30000

void str_replace(char* str, char* replace, char* new)
{
    char* findstr = strstr(str, replace);
    if (findstr) {
        char right[str_size];
        strncpy(right, &findstr[strlen(replace)], str_size);
        findstr[0] = '\0';
        strncat(str, new, str_size);
        strncat(str, right, str_size);
    }
}

int main(int argc, char** argv)
{
    // open mod-loader.def.c
    char buffer[str_size] = { 0 };
    FILE* mod_loader_file;
    mod_loader_file = fopen("src/mod-loader.def.c", "r");
    fread(buffer, str_size, 1, mod_loader_file);
    fclose(mod_loader_file);
    buffer[str_size - 1] = '\0';


    //look for mods.

    DIR* dir;
    struct dirent* ent;
    char include_mods_str[str_size];
    char init_mods_str[str_size];
    if ((dir = opendir("src/mods")) != NULL) {
        /* print all the files and directories within directory */
        while ((ent = readdir(dir)) != NULL) {
            if( strcmp(ent->d_name,".") == 0 || strcmp(ent->d_name,"..") == 0 ) continue;
            strncat(include_mods_str,"#include \"mods/",str_size);
            strncat(include_mods_str,ent->d_name,str_size);
            strncat(include_mods_str,"\"\n",str_size);

            char *name=&(ent->d_name[4]);
            char *name_stripped=strstr(name,".c");
            name_stripped[0]='\0';
            strncat(init_mods_str,"init_",str_size);
            strncat(init_mods_str,name,str_size);
            strncat(init_mods_str,"();\n",str_size);
        }
        closedir(dir);
    } else {
        /* could not open directory */
        perror("");
    }

    str_replace(buffer, "//<INCLUDE_MODS_REPLACE>", include_mods_str);
    str_replace(buffer, "//<INIT_MODS_REPLACE>", init_mods_str);

    mod_loader_file=fopen("src/mod-loader.c","w");
    fwrite(buffer,sizeof(char),strlen(buffer),mod_loader_file);
    fclose(mod_loader_file);

    return 0;
}