Valhalla Legends Forums Archive | Visual Basic Programming | I need help converting code

AuthorMessageTime
botmaster
[code]
// iid.h

// Original code is from ackmed's pickit module.
// Main.h
// ackmed@gotwalls.com

// Modified for my needs (blue mind)

#pragma once

DWORD Debug;
char DebugBuffer[1024];
extern FUNCTIONENTRYPOINTS *server;

#define VERSION_MAJOR 0
#define VERSION_MINOR 4

#define DEBUG_OFF 0
#define DEBUG_USER 1
#define DEBUG_DEVEL 2
#define DEBUG_PACKET 3

// defines
#define ITEM_MESSAGEID_DROP 0x9C

#define ITEM_ACTION_NEW_GROUND 0x00
#define ITEM_ACTION_PICK_UP 0x01
#define ITEM_ACTION_DROP 0x02
#define ITEM_ACTION_OLD_GROUND 0x03
#define ITEM_ACTION_TO_STORAGE 0x04
#define ITEM_ACTION_FROM_STORAGE 0x05
#define ITEM_ACTION_TO_STORE 0x0B
#define ITEM_ACTION_FROM_STORE 0x0C
#define ITEM_ACTION_PLACE_BELT 0x0E

#define ITEM_ITEMTYPE_HELM 0x00
#define ITEM_ITEMTYPE_ARMOR 0x01
#define ITEM_ITEMTYPE_WEAPON 0x05
#define ITEM_ITEMTYPE_BOW 0x06
#define ITEM_ITEMTYPE_SHIELD 0x07
#define ITEM_ITEMTYPE_EXPANSION 0x0A // heads
#define ITEM_ITEMTYPE_OTHER 0x10

#define ITEM_LOCATION_STORED 0x00
#define ITEM_LOCATION_WORN 0x01
#define ITEM_LOCATION_BELT 0x02
#define ITEM_LOCATION_GROUND 0x03
#define ITEM_LOCATION_TOHAND 0x04

#define ITEM_LEVEL_INFERIOR 0x01
#define ITEM_LEVEL_NORMAL 0x02
#define ITEM_LEVEL_SUPERIOR 0x03
#define ITEM_LEVEL_MAGIC 0x04
#define ITEM_LEVEL_SET 0x05
#define ITEM_LEVEL_RARE 0x06
#define ITEM_LEVEL_UNIQUE 0x07
#define ITEM_LEVEL_CRAFT 0x08

#define ITEM_INFERIORTYPE_CRUDE 0x00
#define ITEM_INFERIORTYPE_CRACKED 0x01
#define ITEM_INFERIORTYPE_DAMAGED 0x02
#define ITEM_INFERIORTYPE_LOWQUALITY 0x04

#define ITEM_SUPERIOR_AR 0x00 // attack rating
#define ITEM_SUPERIOR_MAXDMG 0x01 // max damage
#define ITEM_SUPERIOR_AC 0x02
#define ITEM_SUPERIOR_AR_MAXDMG 0x03
#define ITEM_SUPERIOR_DUR 0x04
#define ITEM_SUPERIOR_DUR_AR 0x05
#define ITEM_SUPERIOR_DUR_MAXDMG 0x06
#define ITEM_SUPERIOR_DUR_AC 0x07

#define ITEM_GOLDSIZE_12BIT 0x00
#define ITEM_GOLDSIZE_32BIT 0x01

#define ITEM_UNSET 0xFF
/*enum IBOOL {
ITEM_FALSE = 0,
ITEM_TRUE,
ITEM_UNSET
}; */

// item struct
typedef struct ItemStruct_t {

BYTE MessageID;
BYTE Action;
BYTE MessageSize;
BYTE ItemType;
DWORD ItemID;

// flags
BOOL isSocketsFull;
BOOL isIdentified;
BOOL isEthereal;
BOOL isSwitchin;
BOOL isSwitchout;
BOOL isBroken;
BOOL fromBelt;
BOOL hasSockets;
BOOL isJustGenerated;
BOOL isEar;
BOOL isStartitem;
BOOL isMiscItem; // unverified
BOOL isPersonalized;
BOOL isGamble;
BOOL isRuneWord; // unverified
BOOL isMagicExtra; // unverified

WORD MPQVersionField;
BYTE Location;
BYTE BodyCode;
WORD PositionX;
WORD PositionY;
BYTE StorageCode;
char ItemCode[5];
BYTE Ilvl;
BYTE ItemQuality;

// gold specific
BOOL GoldSize;
DWORD GoldAmount;

// ear specific
BYTE PlayerClass;
BYTE PlayerLevel;
char PlayerName[16];

} ITEMSTRUCT;
//-----------------------------------------------------------------------------

// function definations
//extern char Sections[9][9];
// total amount in Sections[] - 1 (subtracting all).
//Mainly use with <= SECTIONSNUM for testing for a section
//#define SECTIONSNUM 8
ITEMSTRUCT *ParseItemDrop(BYTE *Packet, DWORD Length);
void DebugItemBits(BYTE data, BYTE size, DWORD pos);
DWORD GetBitField(BYTE* data, DWORD pos, DWORD len, DWORD max);
BOOL PRIVATE Invert(char** argv, int argc);
BOOL PRIVATE ShowAll(char** argv, int argc);
BOOL PRIVATE ShowStatus(char** argv, int argc);
BOOL PRIVATE ShowConfig(char** argv, int argc);
BOOL PRIVATE StartStop(char** argv, int argc);
BOOL PRIVATE LoadConfig();
BOOL PRIVATE AddItem(char** argv, int argc);
BOOL PRIVATE Reload(char** argv, int argc);
INT PRIVATE GetSection(char*** argv, int sub);
//-----------------------------------------------------------------------------

// from mousepad with some small mods (by FallNAngel ?)
struct BitFields {
BYTE *data;
DWORD pos;
DWORD max;
BitFields(BYTE *d, DWORD mx) {
data = d;
pos = 0;
max = (mx * 8); // convert to max bits
}
DWORD GetField(DWORD len) {
return GetBitField(data, (pos+=len)-len, len, max);
}
};
//-----------------------------------------------------------------------------

DWORD GetBitField(BYTE* data, DWORD pos, DWORD len, DWORD max) {
// trying to read beyond the end of the packet

if(pos + len > max) {

// if we are already at or beyond the end of packet, return 0;
if(pos >= max) {
return 0;
}
// otherwise adjust len to all remaining bits
len = max - pos;
}

return (DWORD)(*(unsigned __int64 *)(data+pos/8)<<(64-len-(pos&7))>>(64-len));
}
//-----------------------------------------------------------------------------

ITEMSTRUCT *ParseItemDrop(BYTE *Packet, DWORD Length) {
ITEMSTRUCT *item = new ITEMSTRUCT;

BitFields iPacket(Packet,Length);

// dump the packet

item->MessageID = iPacket.GetField(8);

if(item->MessageID != ITEM_MESSAGEID_DROP)
{
delete item;
return NULL;
}

item->Action = iPacket.GetField(8);
if(item->Action != ITEM_ACTION_NEW_GROUND && item->Action != ITEM_ACTION_DROP
&& item->Action != ITEM_ACTION_OLD_GROUND && item->Action != ITEM_ACTION_TO_STORAGE
&& item->Action != ITEM_ACTION_TO_STORE) {
// we dont handle parsing these packets.
delete item;
return NULL;
}

item->MessageSize = iPacket.GetField(8);
item->ItemType = iPacket.GetField(8);
item->ItemID = iPacket.GetField(32);

// flags
item->isSocketsFull = iPacket.GetField(1);
DebugItemBits(iPacket.GetField(3),3,iPacket.pos);
item->isIdentified = iPacket.GetField(1);
DebugItemBits(iPacket.GetField(1),1,iPacket.pos);
item->isSwitchin = iPacket.GetField(1);
item->isSwitchout = iPacket.GetField(1);
item->isBroken = iPacket.GetField(1);
DebugItemBits(iPacket.GetField(1),1,iPacket.pos);
item->fromBelt = iPacket.GetField(1);
item->hasSockets = iPacket.GetField(1);
DebugItemBits(iPacket.GetField(1),1,iPacket.pos);
item->isJustGenerated = iPacket.GetField(1);
DebugItemBits(iPacket.GetField(2),2,iPacket.pos);
item->isEar = iPacket.GetField(1);
item->isStartitem = iPacket.GetField(1);
DebugItemBits(iPacket.GetField(3),3,iPacket.pos);
item->isMiscItem = iPacket.GetField(1);
item->isEthereal = iPacket.GetField(1);
DebugItemBits(iPacket.GetField(1),1,iPacket.pos);
item->isPersonalized = iPacket.GetField(1);
item->isGamble = iPacket.GetField(1);
item->isRuneWord = iPacket.GetField(1);

DebugItemBits(iPacket.GetField(5),5,iPacket.pos);
item->MPQVersionField = iPacket.GetField(10);

item->Location = iPacket.GetField(3);

switch(item->Location) {
case 3: case 5:
item->PositionX = iPacket.GetField(16);
item->PositionY = iPacket.GetField(16);
break;
default:
item->BodyCode = iPacket.GetField(4);
item->PositionX = iPacket.GetField(4);
item->PositionY = iPacket.GetField(4);
item->StorageCode = iPacket.GetField(3);
}

// isEar need to return now, until ear packets are decoded
if(item->isEar) {
item->PlayerClass = iPacket.GetField(3);
item->PlayerLevel = iPacket.GetField(7);
BYTE nextchar=0,i=0;
//for(nextchar=iPacket.GetField(7);nextchar!=0;i++) {
for(i=0;i<16;i++) {
item->PlayerName[i] = iPacket.GetField(7);
}
return item;
}

// read in itemcode, its a string;
item->ItemCode[0] = iPacket.GetField(8);
item->ItemCode[1] = iPacket.GetField(8);
item->ItemCode[2] = iPacket.GetField(8);
item->ItemCode[3] = iPacket.GetField(8);

// chomp off the space if it exists
if(item->ItemCode[3] == ' ') {
item->ItemCode[3] = '\0';
} else {
item->ItemCode[4] = '\0';
}

// gold is a special case
if(strcmp(item->ItemCode,"gld") == 0) {
item->GoldSize = iPacket.GetField(1);
if(item->GoldSize == ITEM_GOLDSIZE_12BIT) {
item->GoldAmount = iPacket.GetField(12);
} else {
item->GoldAmount = iPacket.GetField(32);
}
item->ItemQuality = ITEM_UNSET;
item->Ilvl = ITEM_UNSET;
return item;
}

// more special cases need to look into properly decoding them
if(strcmp(item->ItemCode,"ibk") == 0 ||
strcmp(item->ItemCode,"tbk") == 0 ||
strcmp(item->ItemCode,"key") == 0) {
item->ItemQuality = ITEM_UNSET;
item->Ilvl = ITEM_UNSET;
return item;
}


// packet not long enough to read ItemQuality
if(iPacket.pos + 14 >= iPacket.max) {
// causes way to much spam
/* if(Debug == DEBUG_DEVEL) {
server->GamePrintInfo("ParseItemDrop: Return before ItemQuality");
}
*/
item->Ilvl = ITEM_UNSET;
item->ItemQuality = ITEM_UNSET;
return item;
}

iPacket.GetField(3);
item->Ilvl = iPacket.GetField(7);
item->ItemQuality = iPacket.GetField(3);
return item;
}
//-----------------------------------------------------------------------------

void DebugItemBits(BYTE data, BYTE size, DWORD pos) {
BYTE n;

if(Debug == DEBUG_PACKET)
{
server->GamePrintInfo("In DebugItemBits");
sprintf(DebugBuffer,"%d: ",pos);
if(size > 8)
size = 8;
if(size != 8)
data = data<<(8 - size);
for(n = 0;n < size;n++)
{
if((data & 0x80) !=0)
sprintf(DebugBuffer,"%s1",DebugBuffer);
else
sprintf(DebugBuffer,"%s0",DebugBuffer);
data = data<<1;
}
server->GamePrintInfo(DebugBuffer);
}
}
[/code]

What I am really trying to figure out is this part

[code]
item->ItemID = iPacket.GetField(32);


return (DWORD)(*(unsigned __int64 *)(data+pos/8)<<(64-len-(pos&7))>>(64-len));

[/code]

Trying to get the item name such as clb,gld there are many many more if anyone could help with this or even send me in the right way that would be every so helpful. Or if anyone can make this into a dll that visual basic can use that would be very helpful aswell.
July 20, 2004, 8:42 AM
UserLoser.
Something like:

whatever = CLng(CDbl(ShiftLeft(Mid$(Data, (pos / 8), 4), ShiftRight(64 - len - (pos And 7)), (64-len))))

You'll have to create your own bitwise shift functions, ShiftLeft (<< C++ operator) and ShiftRight (>> C++ operator) functions.

If you need help on bitwise shifts, see this page.
July 20, 2004, 1:08 PM
Newby
Heh, my C++ book says that shifting is essencially 2 to the xth power, where X looks something like ## <</>> X :

i.e 2 << 2 == 2 * (2*2)

Correct me if I read incorrectly ><
July 20, 2004, 10:33 PM
ChR0NiC
Most of these conversions can be easily figured out by searching Google

Some you may have a little trouble with, but if you get a basic understanding of the C++ operators from here, you shouldn't have too much trouble.
July 21, 2004, 8:26 AM
botmaster
I would like to thank all of you that are helping me I found some bitshifting code on psc

[code]

Public Enum dcShiftDirection
Left = -1
Right = 0
End Enum

'=======================================
' ==========
'Public Function Shift(ByVal lValue As L
' ong, ByVal lNumberOfBitsToShift As Long,
' ByVal lDirectionToShift As dcShiftDirect
' ion) As Long
'Author: Donald Moore (MindRape)
'E-mail: moore@futureone.com
' Date: 06/16/99
'Enters:
' lValue as Long
' lNumberOfBitsToShift as Long
' lDirectionToShift as Long
'Returns:
' Long - shifted value
'Purpose:
' Shift the given value by the given num
' ber of bits to shift in the given direct
' ion.
' Shifting bits to the left acts as a mu
' ltiplier and to the right divides.

Public Function Shift(ByVal lValue As Long, ByVal lNumberOfBitsToShift As Long, ByVal lDirectionToShift As dcShiftDirection) As Long

Const ksCallname As String = "Shift"
On Error GoTo Procedure_Error
Dim LShift As Long

If lDirectionToShift Then 'shift left
LShift = lValue * (2 ^ lNumberOfBitsToShift)
Else 'shift right
LShift = lValue \ (2 ^ lNumberOfBitsToShift)
End If


Procedure_Exit:
Shift = LShift
Exit Function

Procedure_Error:
Err.Raise Err.Number, ksCallname, Err.Description, Err.HelpFile, Err.HelpContext
Resume Procedure_Exit
End Function

'=======================================
' ==========
'Public Function LShift(ByVal lValue As
' Long, ByVal lNumberOfBitsToShift As Long
' ) As Long
'Author: Donald Moore (MindRape)
'E-mail: moore@futureone.com
' Date: 06/16/99
'Enters:
' lValue as Long
' lNumberOfBitsToShift as Long
'Returns:
' Long - shifted value
'Purpose:
' Shift the given value by the given num
' ber of bits left

Public Function LShift(ByVal lValue As Long, ByVal lNumberOfBitsToShift As Long) As Long

Const ksCallname As String = "LShift"
On Error GoTo Procedure_Error
LShift = Shift(lValue, lNumberOfBitsToShift, Left)

Procedure_Exit:
Exit Function

Procedure_Error:
Err.Raise Err.Number, ksCallname, Err.Description, Err.HelpFile, Err.HelpContext
Resume Procedure_Exit
End Function

'=======================================
' ==========
'Public Function RShift(ByVal lValue As
' Long, ByVal lNumberOfBitsToShift As Long
' ) As Long
'Author: Donald Moore (MindRape)
'E-mail: moore@futureone.com
' Date: 06/16/99
'Enters:
' lValue as Long
' lNumberOfBitsToShift as Long
'Returns:
' Long - shifted value
'Purpose:
' Shift the given value by the given num
' ber of bits right

Public Function RShift(ByVal lValue As Long, ByVal lNumberOfBitsToShift As Long) As Long
On Error Resume Next
Const ksCallname As String = "RShift"
On Error GoTo Procedure_Error
RShift = Shift(lValue, lNumberOfBitsToShift, Right)

Procedure_Exit:
Exit Function

Procedure_Error:
Err.Raise Err.Number, ksCallname, Err.Description, Err.HelpFile, Err.HelpContext
Resume Procedure_Exit
End Function

[/code]

I then wrote this with the help of UserLoser

[code]
Public Function doshift(data As String, pos As Integer, strlen As Integer)
Dim whatever As Long
whatever = CLng(CDbl(LShift(Mid(data, Val(pos / 8), 4), RShift(Val(64 - strlen - (pos And 7)), Val(64 - strlen)))))
End Function
[/code]

This is not working at all if anyone as anymore ideas of what i am doing wrong let me know

9C 02 22 10 B4 BD 81 5D 10 20 80 00 65 0C DE E2 4B 42 2E CD 0D 04 07 2A 00 18 86 85 00 86 00 A8 C0 7F
that is the packet I am trying to decode it should have the item code "clb" in it.

thanks for the help again.


this is what i am trying to get

[code]
   item->ItemCode[0] = iPacket.GetField(8);
   item->ItemCode[1] = iPacket.GetField(8);
   item->ItemCode[2] = iPacket.GetField(8);
   item->ItemCode[3] = iPacket.GetField(8);
[/code]
July 21, 2004, 9:30 AM
botmaster
I went to make it a dll

[code]
ITEMSTRUCT *ParseItemDrop(BYTE *Packet, DWORD Length) {
ITEMSTRUCT *item = new ITEMSTRUCT;

BitFields iPacket(Packet,Length);

// dump the packet

   
// read in itemcode, its a string;
item->ItemCode[0] = iPacket.GetField(8);
item->ItemCode[1] = iPacket.GetField(8);
item->ItemCode[2] = iPacket.GetField(8);
item->ItemCode[3] = iPacket.GetField(8);


return item;
}

[/code]

that is the C++ dll returning the item code


now when i do this in vb I am not sure what I am doing really kinda new to this...


[code]
Public Type ItemStruct_t

MessageID As Byte
Action As Byte
MessageSize As Byte
ItemType As Byte
ItemID As String


isSocketsFull As Boolean
isIdentified As Boolean
isEthereal As Boolean
isSwitchin As Boolean
isSwitchout As Boolean

isBroken As Boolean
fromBelt As Boolean
hasSockets As Boolean
isJustGenerated As Boolean
isEar As Boolean
isStartitem As Boolean
isMiscItem As Boolean
isPersonalized As Boolean
isGamble As Boolean
isRuneWord As Boolean
isMagicExtra As Boolean
MPQVersionField As String
Location As Byte
PositionX As String
PositionY As String
ItemCode(5) As String
ItemLevel As Byte


GoldSize As Boolean

GoldAmount As Integer
End Type



Public Declare Function ParseItemDrop Lib "d2gs.dll" (ByRef data() As Byte, ByVal strlen As Long) As ItemStruct_t


Dim sString As String
Dim bytStringValue() As Byte
Dim intArraySize As Long
Dim i As Integer
sString = Replace("9C 02 22 10 B4 BD 81 5D 10 20 80 00 65 0C DE E2 4B 42 2E CD 0D 04 07 2A 00 18 86 85 00 86 00 A8 C0 7F", " ", "")
intArraySize = CInt((Len(sString) + 1) / 2) - 1
ReDim bytStringValue(intArraySize)
For i = 0 To intArraySize
bytStringValue(i) = "&H" & Mid(sString, i * 2 + 1, 2)

Next i

item = ParseItemDrop(bytStringValue, intArraySize + 1)
MsgBox item.ItemCode


[/code]

I either get a missmatch error or bad dll calling error. Help me out please :(
July 21, 2004, 11:24 AM
Eli_1
I believe your For loop should be:
[code]
For i = 1 To Len(sString) Step 2
bytStringValue(i) = Chr("&H" & Mid(sString, i, 2))
Next i
[/code]
July 21, 2004, 5:12 PM

Search