]> git.draconx.ca Git - upkg.git/blob - src/uobject/module.c
Stop using gnulib's flexmember module.
[upkg.git] / src / uobject / module.c
1 /*
2  * upkg: tool for manipulating Unreal Tournament packages.
3  * Copyright © 2009-2012, 2022 Nick Bowler
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
17  */
18
19 #include <config.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <ctype.h>
24 #include <glib-object.h>
25 #include <ltdl.h>
26
27 #include <uobject/module.h>
28 #include <uobject/uobject.h>
29 #include <uobject/package.h>
30 #include <upkg.h>
31 #include "avl.h"
32
33 static unsigned initialized;
34 static struct avl_table *package_tree;
35
36 static char *str_cpy_lower(char *dst, const char *src)
37 {
38         size_t i;
39
40         for (i = 0; src[i]; i++)
41                 dst[i] = tolower(src[i]);
42         dst[i] = 0;
43
44         return dst;
45 }
46
47 static int modcmp(const void *a, const void *b, void *_data)
48 {
49         const GTypeModule *ma = a;
50         const GTypeModule *mb = b;
51
52         return strcmp(ma->name, mb->name);
53 }
54
55 int u_object_module_init(void)
56 {
57         if (!initialized) {
58                 package_tree = avl_create(modcmp, NULL, NULL);
59                 if (!package_tree) {
60                         fprintf(stderr, "%s: failed to create package tree.\n", __func__);
61                         return -1;
62                 }
63
64                 g_type_init();
65         }
66
67         initialized++;
68         return 0;
69 }
70
71 int u_object_module_exit(void)
72 {
73         if (--initialized)
74                 return 0;
75
76         avl_destroy(package_tree, NULL);
77
78         return 0;
79 }
80
81 static int lookup_module(GTypeModule **out, const char *name)
82 {
83         GTypeModule *result, key = {0};
84         char *buf;
85
86         buf = malloc(strlen(name) + 1);
87         if (!buf)
88                 return -1;
89
90         key.name = str_cpy_lower(buf, name);
91         result = avl_find(package_tree, &key);
92         free(buf);
93
94         *out = result;
95         return 0;
96 }
97
98 GTypeModule *
99 u_module_get_from_import(GTypeModule *pkg, const struct upkg_import *i)
100 {
101         GTypeModule *result;
102
103         while (i->parent != NULL)
104                 i = i->parent;
105
106         if (strcmp(i->class_package, "Core") != 0
107             || strcmp(i->class_name, "Package") != 0) {
108                 u_err(pkg, "import root must be an instance of Core.Package");
109                 return NULL;
110         }
111
112         if (lookup_module(&result, i->name) != 0) {
113                 u_err(pkg, "fatal error performing package lookup");
114                 return NULL;
115         }
116
117         if (!result) {
118                 void **p;
119
120                 result = u_pkg_open(i->name);
121                 if (!result) {
122                         u_warn(pkg, "failed to open package: %s", i->name);
123                         return NULL;
124                 }
125
126                 p = avl_probe(package_tree, result);
127                 if (!p) {
128                         u_err(pkg, "fatal error inserting package");
129                         g_object_unref(result);
130                         return NULL;
131                 }
132         }
133
134         return result;
135 }
136
137 GType u_object_module_get_class(GTypeModule *pkg, long class)
138 {
139         GTypeModule *mod;
140         const char *classname;
141
142         if (class == 0) {
143                 u_warn(pkg, "class implementations not supported yet");
144                 return 0;
145         } else if (class < 0) {
146                 const struct upkg_import *import;
147
148                 import = upkg_get_import(U_PKG(pkg)->pkg, -(class+1));
149                 if (!import) {
150                         u_err(pkg, "invalid package import: %ld", -(class+1));
151                         return 0;
152                 }
153
154                 mod = u_module_get_from_import(pkg, import);
155                 if (!mod)
156                         return 0;
157
158                 classname = import->name;
159         } else if (class > 0) {
160                 const struct upkg_export *export;
161
162                 export = upkg_get_export(U_PKG(pkg)->pkg, class-1);
163                 if (!export) {
164                         u_err(pkg, "invalid package export: %ld", class-1);
165                         return 0;
166                 }
167
168                 mod = pkg;
169                 classname = export->name;
170         }
171
172         if (!g_type_module_use(mod))
173                 return 0;
174
175         /* XXX: Do this better. */
176         char typename[strlen(mod->name) + strlen(classname) + 1];
177         str_cpy_lower(typename, mod->name);
178         str_cpy_lower(typename+strlen(mod->name), classname);
179         typename[0] = toupper(typename[0]);
180         typename[strlen(mod->name)] = toupper(typename[strlen(mod->name)]);
181
182         return g_type_from_name(typename);
183 }