2 * upkg: tool for manipulating Unreal Tournament packages.
3 * Copyright © 2009-2012, 2022 Nick Bowler
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.
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.
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/>.
24 #include <glib-object.h>
27 #include <uobject/module.h>
28 #include <uobject/uobject.h>
29 #include <uobject/package.h>
33 static unsigned initialized;
34 static struct avl_table *package_tree;
36 static char *str_cpy_lower(char *dst, const char *src)
40 for (i = 0; src[i]; i++)
41 dst[i] = tolower(src[i]);
47 static int modcmp(const void *a, const void *b, void *_data)
49 const GTypeModule *ma = a;
50 const GTypeModule *mb = b;
52 return strcmp(ma->name, mb->name);
55 int u_object_module_init(void)
58 package_tree = avl_create(modcmp, NULL, NULL);
60 fprintf(stderr, "%s: failed to create package tree.\n", __func__);
71 int u_object_module_exit(void)
76 avl_destroy(package_tree, NULL);
81 static int lookup_module(GTypeModule **out, const char *name)
83 struct { GTypeModule pkg; char buf[]; } *search_key;
86 search_key = malloc(sizeof *search_key + strlen(name) + 1);
90 search_key->pkg = (GTypeModule) {
91 .name = str_cpy_lower(search_key->buf, name),
94 result = avl_find(package_tree, search_key);
102 u_module_get_from_import(GTypeModule *pkg, const struct upkg_import *i)
106 while (i->parent != NULL)
109 if (strcmp(i->class_package, "Core") != 0
110 || strcmp(i->class_name, "Package") != 0) {
111 u_err(pkg, "import root must be an instance of Core.Package");
115 if (lookup_module(&result, i->name) != 0) {
116 u_err(pkg, "fatal error performing package lookup");
123 result = u_pkg_open(i->name);
125 u_warn(pkg, "failed to open package: %s", i->name);
129 p = avl_probe(package_tree, result);
131 u_err(pkg, "fatal error inserting package");
132 g_object_unref(result);
140 GType u_object_module_get_class(GTypeModule *pkg, long class)
143 const char *classname;
146 u_warn(pkg, "class implementations not supported yet");
148 } else if (class < 0) {
149 const struct upkg_import *import;
151 import = upkg_get_import(U_PKG(pkg)->pkg, -(class+1));
153 u_err(pkg, "invalid package import: %ld", -(class+1));
157 mod = u_module_get_from_import(pkg, import);
161 classname = import->name;
162 } else if (class > 0) {
163 const struct upkg_export *export;
165 export = upkg_get_export(U_PKG(pkg)->pkg, class-1);
167 u_err(pkg, "invalid package export: %ld", class-1);
172 classname = export->name;
175 if (!g_type_module_use(mod))
178 /* XXX: Do this better. */
179 char typename[strlen(mod->name) + strlen(classname) + 1];
180 str_cpy_lower(typename, mod->name);
181 str_cpy_lower(typename+strlen(mod->name), classname);
182 typename[0] = toupper(typename[0]);
183 typename[strlen(mod->name)] = toupper(typename[strlen(mod->name)]);
185 return g_type_from_name(typename);