-
Notifications
You must be signed in to change notification settings - Fork 3
/
wrapper_maker.idc
163 lines (141 loc) · 3.96 KB
/
wrapper_maker.idc
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
//
// wrapper_maker.idc
//by tomsons26
// Converts a demangled name to a wrapper call
//
// Written for at least a little automating writing wrappers
// for unimplamented functions, likely has issues and
// likely works only for class functions in Watcom
#include <idc.idc>
static Is_Void(checktype)
{
if (checktype == "void const" ){
return 1;
}
if (checktype == "void *") {
return 0;
}
if (checktype == "void ") {
return 1;
}
if (checktype == "void") {
return 1;
}
return 0;
}
static Print_Prototype(classtype, prottype)
{
Message("(");
//check for a class
if (classtype != "")
{
//print class name
Message("%s *", classtype);
}
//check if return is void
if (Is_Void(prottype)){
//if so end proto definition
Message(")");
}
else
{
//check for class
if (classtype != "")
{
//if so since we have more proto members
Message(", ");
}
//print the proto
Message("%s)", prottype);
}
}
static Count_Prototype_Members(prottype)
{
auto count, i;
count = 0;
if (!Is_Void(prottype)) {
count++;
}
for (i = 0; i < strlen(prottype); i++) {
if (prottype[i] == ",") {
count++;
}
}
return count;
}
static Clean_Up_Return(rettype)
{
return substr(rettype, 0, strstr(rettype, "const"));
}
static main()
{
//Fetch the current function name
auto name = Demangle(GetTrueName(GetFunctionAttr(here, FUNCATTR_START)), INF_LONG_DN);
auto nearloc, memb_count;
auto rettype, funcname, prottype, classtype;
//hack, relies on near being in string to work
nearloc = strstr(name, "near");
if (nearloc != -1) {
//get return type
rettype = substr(name, 0, nearloc);
//get function name
funcname = substr(name, nearloc + 5, strstr(name, "("));
//get prototype
prottype = substr(name, strstr(name, "(") + 1, strstr(name, ")"));
//get class if there is one
if (strstr(name, "::") != -1)
{
classtype = substr(name, strstr(name, "near ") + 5, strstr(name, "::"));
}
// print function definition
Message("%s%s", Clean_Up_Return(rettype), funcname);
//print proto
Message("(");
if (!Is_Void(prottype)){
Message("%s", prottype);
}
Message(")\n");
//print wrapper
Message("{\n#ifndef CHRONOSHIFT_STANDALONE\n ");
Message("%s(*func)", Clean_Up_Return(rettype));
Print_Prototype(classtype, prottype);
// print function definition
Message(" = reinterpret_cast<%s(*)", Clean_Up_Return(rettype));
Print_Prototype(classtype, prottype);
//print address of function
Message(">(0x%08X);\n", GetFunctionAttr(here, FUNCATTR_START));
//print call
Message(" ");//spacing
if (!Is_Void(rettype)){
Message("return ");
}
Message("func(");
if (classtype != "") {
Message("this");
}
//write dummy args
memb_count = Count_Prototype_Members(prottype);
if (memb_count != 0) {
if (classtype != "") {
Message(", ");
}
auto i;
for (i = 0; i < memb_count; i++) {
Message("a%d", i+1);
if (i != (memb_count - 1)) {
Message(", ");
}
}
}
Message(");\n");
//print end of function
Message("#else\n");
Message(" DEBUG_ASSERT_PRINT(false, \"Unimplemented function called!\\n\");\n");
if (!Is_Void(rettype)){
Message(" return 0;\n");
}
Message("#endif\n}\n");
} else {
Message("near not found!\n");
}
}