-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathjava_callback.cpp
More file actions
254 lines (197 loc) · 5.58 KB
/
java_callback.cpp
File metadata and controls
254 lines (197 loc) · 5.58 KB
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
/*
Copyright (C) 1996-1997 Id Software, Inc.
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; either version 2
of the License, or (at your option) any later version.
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, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#ifdef JAVA
#include "java_vm.h"
#include "quakedef.h"
char *JavaStringToCString(jstring str){
const jbyte *s;
s = (*j_env)->GetStringUTFChars(j_env, str, NULL);
if (s == NULL){
Con_DPrintf("Java Error: out of memeory");
return NULL; // OutOfMemoryError already thrown
}
return (char *)s;
}
void FreeString(jstring str, const jbyte *s){
(*j_env)->ReleaseStringUTFChars(j_env, str, s);
}
/*
=================
Java_bPrint
broadcast print to everyone on server
=================
*/
void JNICALL Java_bPrint(JNIEnv *env, jclass this, jstring str)
{
const jbyte *s;
s = JavaStringToCString(str);
SV_BroadcastPrintf ("%s", s);
FreeString(str, s);
}
/*
=================
Java_cPrint
center print to client
=================
*/
void JNICALL Java_cPrint(JNIEnv *env, jclass this, jstring str, jint client)
{
const jbyte *s;
//qmb :globot
edict_t *ent;
Con_Printf("Java: center print\n");
ent = G_EDICT(client+1);
if (!ent->bot.isbot)
{
s = JavaStringToCString(str);
if (client < 0 || client >= svs.maxclients)
{
Con_Printf ("Java Quake: tried to sprint to a non-client\n");
return;
}
MSG_WriteChar (&svs.clients[client].message, svc_centerprint);
MSG_WriteString (&svs.clients[client].message, (char *)s);
FreeString(str, s);
}
}
/*
=================
Java_sPrint
sprint to client
=================
*/
void JNICALL Java_sPrint(JNIEnv *env, jclass this, jstring str, jint client)
{
const jbyte *s;
//qmb :globot
edict_t *ent;
Con_Printf("Java: s print\n");
ent = G_EDICT(client+1);
if (!ent->bot.isbot)
{
s = JavaStringToCString(str);
if (client < 0 || client >= svs.maxclients)
{
Con_Printf ("Java Quake: tried to sprint to a non-client\n");
return;
}
MSG_WriteChar (&svs.clients[client].message, svc_print);
MSG_WriteString (&svs.clients[client].message, (char *)s);
FreeString(str, s);
}
}
/*
=================
Java_conPrint
print to console on server
=================
*/
void JNICALL Java_conPrint (JNIEnv *env, jclass this, jstring str){
//get string and pass it on
const jbyte *s;
s = JavaStringToCString(str);
Con_Printf("%s", s);
FreeString(str, s);
}
/*
=================
Java_conPrint
print to console on server
=================
*/
void JNICALL Java_condPrint (JNIEnv *env, jclass this, jstring str){
//get string and pass it on
const jbyte *s;
s = JavaStringToCString(str);
Con_DPrintf("%s", s);
FreeString(str, s);
}
/*
=================
Java_cvar
float cvar (string)
=================
*/
jfloat JNICALL Java_getCVar (JNIEnv *env, jclass this, jstring CVar)
{
const jbyte *cvar_name;
jfloat value;
cvar_name = JavaStringToCString(CVar);
Con_Printf("getting cvar: %s\n", cvar_name);
value = Cvar_VariableValue ((char *)cvar_name);
FreeString(CVar, cvar_name);
return value;
}
/*
=================
Java_cvar_set
float cvar (string)
=================
*/
void JNICALL Java_setCVar (JNIEnv *env, jclass this, jstring CVar, jstring Value)
{
const jbyte *cvar_name, *cvar_value;
cvar_name = JavaStringToCString(CVar);
cvar_value = JavaStringToCString(Value);
Con_Printf("settting cvar: %s\n", cvar_name);
Cvar_Set((char *)cvar_name, (char *)cvar_value);
FreeString(CVar, cvar_name);
FreeString(Value, cvar_value);
}
//java particle stuff
extern void Java_DrawParticle(JNIEnv *env, jclass this, float x, float y, float z, float size, float r, float g, float b, float a, float rotation);
void Java_glTexCoord2f(JNIEnv *env, jclass this, jfloat s, jfloat t){
glTexCoord2f(s,t);
}
void Java_glVertex3f(JNIEnv *env, jclass this, jfloat x, jfloat y, jfloat z){
glVertex3f(x,y,z);
}
//list of native methods: method name, method signiture, native function pointer
JNINativeMethod nm[] = {
"conPrint", "(Ljava/lang/String;)V", Java_conPrint,
"condPrint", "(Ljava/lang/String;)V", Java_condPrint,
"bPrint", "(Ljava/lang/String;)V", Java_bPrint,
"cPrint", "(Ljava/lang/String;I)V", Java_cPrint,
"sPrint", "(Ljava/lang/String;I)V", Java_sPrint,
"getCVar", "(Ljava/lang/String;)F", Java_getCVar,
"setCVar", "(Ljava/lang/String;Ljava/lang/String;)V", Java_setCVar,
"glTexCoord2f", "(FF)V", Java_glTexCoord2f,
"glVertex3f", "(FFF)V", Java_glVertex3f,
"drawParticle", "(FFFFFFFFF)V", Java_DrawParticle,
"","",NULL //MUST BE AT END, used as a marker to see how many functions to add
};
void JVM_SetupCallback(JNIEnv* env) {
jclass quake;
jint ret;
int numMethods;
if(env == NULL) return;
quake = (*env)->FindClass(env, "Quake");
if(quake == NULL)
{
Con_Printf("Error: cannot find class\n");
return;
}
//work out the number of native methods to add
//assumes at least one function
for (numMethods = 1; nm[numMethods].fnPtr != NULL; numMethods++);
Con_Printf("Java: Registering %i callback functions\n", numMethods);
ret = (*env)->RegisterNatives(env, quake, &nm[0], numMethods);
if(ret != 0)
{
Con_Printf("Error: cannot find method, error: %i\n", ret);
return;
}
}
#endif