forked from noah-/d2bs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
JSParty.cpp
169 lines (142 loc) · 3.38 KB
/
JSParty.cpp
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
#include "JSParty.h"
#include "D2Structs.h"
#include "D2Helpers.h"
#include "D2Ptrs.h"
#include "JSUnit.h"
#include "JSGlobalClasses.h"
EMPTY_CTOR(party)
JSAPI_PROP(party_getProperty)
{
RosterUnit* pUnit = (RosterUnit*)JS_GetPrivate(cx, obj);
if(!pUnit)
return JS_TRUE;
jsval ID;
JS_IdToValue(cx,id,&ID);
JS_BeginRequest(cx);
switch(JSVAL_TO_INT(ID))
{
case PARTY_NAME:
vp.setString(JS_NewStringCopyZ(cx, pUnit->szName));
break;
case PARTY_X:
vp.setInt32(pUnit->Xpos);
break;
case PARTY_Y:
vp.setInt32(pUnit->Ypos);
break;
case PARTY_AREA:
vp.setInt32(pUnit->dwLevelId);
break;
case PARTY_GID:
vp.setNumber((jsdouble)pUnit->dwUnitId);
break;
case PARTY_LIFE:
vp.setInt32(pUnit->dwPartyLife);
break;
case PARTY_CLASSID:
vp.setInt32(pUnit->dwClassId);
break;
case PARTY_LEVEL:
vp.setInt32(pUnit->wLevel);
break;
case PARTY_FLAG:
vp.setInt32(pUnit->dwPartyFlags);
break;
case PARTY_ID:
vp.setInt32(pUnit->wPartyId);
break;
default:
break;
}
JS_EndRequest(cx);
return JS_TRUE;
}
JSAPI_FUNC(party_getNext)
{
if(!WaitForGameReady())
THROW_WARNING(cx, vp, "Game not ready");
RosterUnit *pUnit = (RosterUnit*)JS_GetPrivate(cx, JS_THIS_OBJECT(cx, vp));
if(!pUnit)
{
JS_SET_RVAL(cx, vp, JSVAL_FALSE);
return JS_TRUE;
}
pUnit = pUnit->pNext;
if(pUnit)
{
JS_SetPrivate(cx, JS_THIS_OBJECT(cx, vp), pUnit);
JS_SET_RVAL(cx, vp, OBJECT_TO_JSVAL(JS_THIS_OBJECT(cx, vp)));
}
else
{
//JSObject* obj = JS_THIS_OBJECT(cx, vp);
// JS_ClearScope(cx, obj);
//if(JS_ValueToObject(cx, JSVAL_NULL, &obj))
JS_SET_RVAL(cx, vp, JSVAL_FALSE);
}
return JS_TRUE;
}
JSAPI_FUNC(my_getParty)
{
if(!WaitForGameReady())
THROW_WARNING(cx, vp, "Game not ready");
JS_SET_RVAL(cx, vp, JSVAL_VOID);
RosterUnit* pUnit = *p_D2CLIENT_PlayerUnitList;
if(!pUnit)
return JS_TRUE;
if(argc == 1)
{
UnitAny* inUnit = NULL;
char* nPlayerName = "";
uint32 nPlayerId = NULL;
if(JSVAL_IS_STRING(JS_ARGV(cx, vp)[0]))
{
nPlayerName = JS_EncodeString(cx,JSVAL_TO_STRING(JS_ARGV(cx, vp)[0]));
}
else if(JSVAL_IS_INT(JS_ARGV(cx, vp)[0]))
{
JS_BeginRequest(cx);
if(!JS_ConvertArguments(cx, argc, JS_ARGV(cx, vp), "u", &nPlayerId))
{
JS_EndRequest(cx);
THROW_ERROR(cx, "Unable to get ID");
}
JS_EndRequest(cx);
}
else if(JSVAL_IS_OBJECT(JS_ARGV(cx, vp)[0]))
{
myUnit* lpUnit = (myUnit*)JS_GetPrivate(cx, JSVAL_TO_OBJECT(JS_ARGV(cx, vp)[0]));
if(!lpUnit)
return JS_TRUE;
inUnit = D2CLIENT_FindUnit(lpUnit->dwUnitId, lpUnit->dwType);
if(!inUnit)
THROW_ERROR(cx, "Unable to get Unit");
nPlayerId = inUnit->dwUnitId;
}
if(!nPlayerName && !nPlayerId)
return JS_TRUE;
BOOL bFound = FALSE;
for(RosterUnit* pScan = pUnit; pScan; pScan = pScan->pNext)
{
if(nPlayerId && pScan->dwUnitId == nPlayerId)
{
bFound = TRUE;
pUnit = pScan;
break;
}
if(nPlayerName && _stricmp( pScan->szName, nPlayerName) == 0)
{
bFound = TRUE;
pUnit = pScan;
break;
}
}
if(!bFound)
return JS_TRUE;
}
JSObject* jsUnit = BuildObject(cx, &party_class, party_methods, party_props, pUnit);
if(!jsUnit)
return JS_TRUE;
JS_SET_RVAL(cx, vp, OBJECT_TO_JSVAL(jsUnit));
return JS_TRUE;
}