Ejemplo n.º 1
0
 private static unsafe extern void TransferData(MonsterloopData data, byte *results);
Ejemplo n.º 2
0
        // Stores all charms and pieces in a memory region the dll can read.
        // Allocates a region of memory where the dll can write down the results.
        private unsafe void buildDataForDll()
        {
            // Put the pieces in memory using a format suitable for c++ (using BasicPiece)
            BasicPiece[] _all   = new BasicPiece[allPieces[0].Length + allPieces[1].Length + allPieces[2].Length + allPieces[3].Length + allPieces[4].Length];
            var          handle = GCHandle.Alloc(_all, GCHandleType.Pinned); // Won't be released since the dll needs it until the whole thing shuts down.

            pPieces = handle.AddrOfPinnedObject();
            int n = 0;

            for (int i = 0; i < allPieces.Length; ++i)
            {
                for (int j = 0; j < allPieces[i].Length; ++j)
                {
                    ArmorPiece piece = allPieces[i][j];
                    BasicPiece p;
                    p.s1      = (piece.skill1 == null) ? 0 : piece.skill1.skillId.id;
                    p.r1      = (piece.skill1 == null) ? 0 : piece.skill1.rank;
                    p.s2      = (piece.skill2 == null) ? 0 : piece.skill2.skillId.id;
                    p.r2      = (piece.skill2 == null) ? 0 : piece.skill2.rank;
                    p.gem     = piece.slots.AsInt();
                    _all[n++] = p;
                }
            }

            // Put the charms in memory using a format suitable for c++ (using BasicCharm)
            BasicCharm[] _allCharms  = new BasicCharm[allCharms.Length];
            var          handleCharm = GCHandle.Alloc(_allCharms, GCHandleType.Pinned);

            pCharms = handleCharm.AddrOfPinnedObject();
            for (int i = 0; i < allCharms.Length; ++i)
            {
                BasicCharm p;
                p.s1          = (byte)allCharms[i].skill1.skillId.id;
                p.r1          = (byte)allCharms[i].skill1.rank;
                p.s2          = (allCharms[i].skill2 == null) ? (byte)0 : (byte)allCharms[i].skill2.skillId.id;
                p.r2          = (allCharms[i].skill2 == null) ? (byte)0 : (byte)allCharms[i].skill2.rank;
                _allCharms[i] = p;
            }

            // Allocates an array for storing results
            Results = new byte[6 * MonsterGear.MAX_RESULTS];
            var resHandle = GCHandle.Alloc(Results, GCHandleType.Pinned);

            pResults = resHandle.AddrOfPinnedObject();

            // Prepare the parameters for calling TransferData
            MonsterloopData data = new MonsterloopData();

            data.maxResults = MonsterGear.MAX_RESULTS;
            data.nCharms    = allCharms.Length;
            data.nHelms     = allPieces[0].Length;
            data.nChests    = allPieces[1].Length;
            data.nGloves    = allPieces[2].Length;
            data.nWaists    = allPieces[3].Length;
            data.nLegs      = allPieces[4].Length;
            data.charms     = (BasicCharm *)pCharms;
            data.pieces     = (BasicPiece *)pPieces;

            // Give everything to the dll
            TransferData(data, (byte *)pResults);
        }