]> git.draconx.ca Git - upkg.git/blob - src/uobject/module.c
Improve GOB rebuild rules for VPATH builds.
[upkg.git] / src / uobject / module.c
1 /*
2  *  upkg: tool for manipulating Unreal Tournament packages.
3  *  Copyright © 2009-2011 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 <http://www.gnu.org/licenses/>.
17  */
18
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <ctype.h>
23 #include <glib-object.h>
24 #include <ltdl.h>
25
26 #include <uobject/module.h>
27 #include <uobject/uobject.h>
28 #include <uobject/package.h>
29 #include <upkg.h>
30 #include "avl.h"
31
32 static unsigned initialized;
33 static struct avl_table *package_tree;
34
35 static char *str_cpy_lower(char *dst, const char *src)
36 {
37         size_t i;
38
39         for (i = 0; src[i]; i++)
40                 dst[i] = tolower(src[i]);
41         dst[i] = 0;
42
43         return dst;
44 }
45
46 static int modcmp(const void *a, const void *b, void *_data)
47 {
48         const GTypeModule *ma = a;
49         const GTypeModule *mb = b;
50
51         return strcmp(ma->name, mb->name);
52 }
53
54 int u_object_module_init(void)
55 {
56         if (!initialized) {
57                 package_tree = avl_create(modcmp, NULL, NULL);
58                 if (!package_tree) {
59                         fprintf(stderr, "%s: failed to create package tree.\n", __func__);
60                         return -1;
61                 }
62
63                 g_type_init();
64         }
65
66         initialized++;
67         return 0;
68 }
69
70 int u_object_module_exit(void)
71 {
72         if (--initialized)
73                 return 0;
74
75         avl_destroy(package_tree, NULL);
76
77         return 0;
78 }
79
80 static int lookup_module(GTypeModule **out, const char *name)
81 {
82         struct { GTypeModule pkg; char buf[]; } *search_key;
83         GTypeModule *result;
84
85         search_key = malloc(sizeof *search_key + strlen(name) + 1);
86         if (!search_key)
87                 return -1;
88
89         search_key->pkg = (GTypeModule) {
90                 .name = str_cpy_lower(search_key->buf, name),
91         };
92
93         result = avl_find(package_tree, search_key);
94         free(search_key);
95
96         *out = result;
97         return 0;
98 }
99
100 GTypeModule *
101 u_module_get_from_import(GTypeModule *pkg, const struct upkg_import *i)
102 {
103         GTypeModule *result;
104
105         while (i->parent != NULL)
106                 i = i->parent;
107
108         if (strcmp(i->class_package, "Core") != 0
109             || strcmp(i->class_name, "Package") != 0) {
110                 u_err(pkg, "import root must be an instance of Core.Package");
111                 return NULL;
112         }
113
114         if (lookup_module(&result, i->name) != 0) {
115                 u_err(pkg, "fatal error performing package lookup");
116                 return NULL;
117         }
118
119         if (!result) {
120                 void **p;
121
122                 result = u_pkg_open(i->name);
123                 if (!result) {
124                         u_warn(pkg, "failed to open package: %s", i->name);
125                         return NULL;
126                 }
127
128                 p = avl_probe(package_tree, result);
129                 if (!p) {
130                         u_err(pkg, "fatal error inserting package");
131                         g_object_unref(result);
132                         return NULL;
133                 }
134         }
135
136         return result;
137 }
138
139 GType u_object_module_get_class(GTypeModule *pkg, long class)
140 {
141         GTypeModule *mod;
142         const char *classname;
143
144         if (class == 0) {
145                 u_warn(pkg, "class implementations not supported yet");
146                 return 0;
147         } else if (class < 0) {
148                 const struct upkg_import *import;
149
150                 import = upkg_get_import(U_PKG(pkg)->pkg, -(class+1));
151                 if (!import) {
152                         u_err(pkg, "invalid package import: %ld", -(class+1));
153                         return 0;
154                 }
155
156                 mod = u_module_get_from_import(pkg, import);
157                 if (!mod)
158                         return 0;
159
160                 classname = import->name;
161         } else if (class > 0) {
162                 const struct upkg_export *export;
163
164                 export = upkg_get_export(U_PKG(pkg)->pkg, class-1);
165                 if (!export) {
166                         u_err(pkg, "invalid package export: %ld", class-1);
167                         return 0;
168                 }
169
170                 mod = pkg;
171                 classname = export->name;
172         }
173
174         if (!g_type_module_use(mod))
175                 return 0;
176
177         /* XXX: Do this better. */
178         char typename[strlen(mod->name) + strlen(classname) + 1];
179         str_cpy_lower(typename, mod->name);
180         str_cpy_lower(typename+strlen(mod->name), classname);
181         typename[0] = toupper(typename[0]);
182         typename[strlen(mod->name)] = toupper(typename[strlen(mod->name)]);
183
184         return g_type_from_name(typename);
185 }