Example #1
0
 public void SetOverIndexThrowsArgumentOutOfRange()
 {
     using (var buffer = new NativeBuffer())
     {
         Assert.Throws<ArgumentOutOfRangeException>(() => { buffer[0] = 0; });
     }
 }
Example #2
0
 public void CanGetSetBytes()
 {
     using (var buffer = new NativeBuffer(1))
     {
         buffer[0] = 0xA;
         Assert.Equal(buffer[0], 0xA);
     }
 }
Example #3
0
 public void EnsureZeroCapacityDoesNotFreeBuffer()
 {
     using (var buffer = new NativeBuffer(10))
     {
         Assert.NotEqual(buffer.GetHandle().DangerousGetHandle(), IntPtr.Zero);
         buffer.EnsureByteCapacity(0);
         Assert.NotEqual(buffer.GetHandle().DangerousGetHandle(), IntPtr.Zero);
     }
 }
Example #4
0
        public void NullSafePointerInTest()
        {
            using (var buffer = new NativeBuffer(0))
            {
                Assert.True(buffer.GetHandle().IsInvalid);
                Assert.Equal((ulong)0, buffer.ByteCapacity);

                // This will throw if we don't put a stub SafeHandle in for the empty buffer
                GetCurrentDirectorySafe((uint)buffer.ByteCapacity, buffer.GetHandle());
            }
        }
        public bool GetControllerState(uint controllerIndex, out SteamControllerState state)
        {
            CheckIfUsable();

            using (NativeBuffer bufferKey = new NativeBuffer(Marshal.SizeOf(typeof(SteamControllerState))))
            {
                bool result = NativeMethods.SteamController_GetControllerState(controllerIndex, bufferKey.UnmanagedMemory);
                state = NativeHelpers.ConvertStruct<SteamControllerState>(bufferKey.UnmanagedMemory, bufferKey.UnmanagedSize);

                return result;
            }
        }
        public bool GetQueryUGCResult(UGCQueryHandle handle, uint index, out UGCDetails details)
        {
            CheckIfUsable();

            using (NativeBuffer bufferKey = new NativeBuffer(Marshal.SizeOf(typeof(UGCDetails))))
            {
                bool result = NativeMethods.UGC_GetQueryUGCResult(handle.AsUInt64, index, bufferKey.UnmanagedMemory);
                details = NativeHelpers.ConvertStruct<UGCDetails>(bufferKey.UnmanagedMemory, bufferKey.UnmanagedSize);

                return result;
            }
        }
        public int GetNextMostAchievedAchievementInfo(int iterator, out string name, out float percent, out bool achieved)
        {
            CheckIfUsable();
            using (NativeBuffer buffer = new NativeBuffer(Constants.Stats.StatNameMax))
            {
                percent = 0.0f;
                achieved = false;
                iterator = NativeMethods.Stats_GetNextMostAchievedAchievementInfo(iterator,
                    buffer.UnmanagedMemory, (uint)buffer.UnmanagedSize, ref percent, ref achieved);

                name = NativeHelpers.ToStringAnsi(buffer.UnmanagedMemory);
                return iterator;
            }
        }
        public bool GetDownloadedLeaderboardEntry(LeaderboardEntriesHandle entries, int index,
            out LeaderboardEntry entry, int[] details)
        {
            CheckIfUsable();
            int numberOfDetails = details == null ? 0 : details.Length;
            using (NativeBuffer entryBuffer = new NativeBuffer(Marshal.SizeOf(typeof(LeaderboardEntry))))
            {
                using (NativeBuffer detailsBuffer = new NativeBuffer(numberOfDetails * sizeof(int)))
                {
                    bool result = NativeMethods.Stats_GetDownloadedLeaderboardEntry(entries.AsUInt64, index,
                        entryBuffer.UnmanagedMemory, detailsBuffer.UnmanagedMemory, numberOfDetails);

                    // Read the entry directly from the unmanaged buffer
                    entry = LeaderboardEntry.Create(entryBuffer.UnmanagedMemory, entryBuffer.UnmanagedSize);

                    for (int i = 0; i < numberOfDetails; i++)
                    {
                        // Read all the detail values from the unmanaged buffer
                        details[i] = Marshal.ReadInt32(detailsBuffer.UnmanagedMemory, sizeof(int) * i);
                    }

                    return result;
                }
            }
        }
        /// <summary>
        /// Invokes LeaderboardScoreUploaded
        /// </summary>
        /// <param name="handle"></param>
        /// <param name="scoreMethod"></param>
        /// <param name="score"></param>
        /// <param name="details"></param>
        public void UploadLeaderboardScore(LeaderboardHandle handle, LeaderboardUploadScoreMethod scoreMethod,
            int score, int[] details)
        {
            CheckIfUsable();
            using (NativeBuffer buffer = new NativeBuffer(NativeBuffer.ToBytes(details)))
            {
                buffer.WriteToUnmanagedMemory();

                NativeMethods.Stats_UploadLeaderboardScore(handle.AsUInt64, (int)scoreMethod, score,
                    buffer.UnmanagedMemory, details.Length);
            }
        }
        public int GetLobbyChatEntry(SteamID steamIDLobby, int chatID, out SteamID steamIDUser, byte[] data, out ChatEntryType chatEntryType)
        {
            CheckIfUsable();
            ulong rawCreator = 0;
            int chatEntryTemp = 0;

            using (NativeBuffer buffer = new NativeBuffer(data))
            {
                int result = NativeMethods.MatchMaking_GetLobbyChatEntry(steamIDLobby.AsUInt64, chatID, ref rawCreator, buffer.UnmanagedMemory, buffer.UnmanagedSize, ref chatEntryTemp);
                buffer.ReadFromUnmanagedMemory(result);

                steamIDUser = new SteamID(rawCreator);
                chatEntryType = (ChatEntryType)chatEntryTemp;
                return result;
            }
        }
        /// <summary>
        /// Invokes LeaderboardScoresDownloaded
        /// </summary>
        /// <param name="handle"></param>
        /// <param name="users"></param>
        public void DownloadLeaderboardEntriesForUsers(LeaderboardHandle handle, SteamID[] users)
        {
            CheckIfUsable();
            // We need to first convert the array of SteamID objects to a byte array
            byte[] rawData = NativeBuffer.ToBytes(users);

            using (NativeBuffer buffer = new NativeBuffer(rawData))
            {
                // Copies the list of user ID's to unmanaged memory
                buffer.WriteToUnmanagedMemory();

                NativeMethods.Stats_DownloadLeaderboardEntriesForUsers(handle.AsUInt64,
                    buffer.UnmanagedMemory, users.Length);
            }
        }
        public bool GetWorldFromHeadPose(float PredictedSecondsFromNow, out HmdMatrix34 Pose, out HmdTrackingResult Result)
        {
            CheckIfUsable();

            using (NativeBuffer buffer = new NativeBuffer(Marshal.SizeOf(typeof(HmdMatrix34))))
            {
                int TempResult = 0;

                bool result = NativeMethods.VR_Hmd_GetWorldFromHeadPose(PredictedSecondsFromNow, buffer.UnmanagedMemory, ref TempResult);
                Pose = NativeHelpers.ConvertStruct<HmdMatrix34>(buffer.UnmanagedMemory, buffer.UnmanagedSize);
                Result = (HmdTrackingResult)TempResult;

                return result;
            }
        }
        public bool ReadP2PPacket(byte[] dest, out uint msgSize, out SteamID steamIDRemote, int channel = 0)
        {
            CheckIfUsable();
            msgSize = 0;

            using (NativeBuffer bufferKey = new NativeBuffer(dest))
            {
                ulong rawCreator = 0;
                bool returnvalue = NativeMethods.Networking_ReadP2PPacket(bufferKey.UnmanagedMemory,
                    (uint)bufferKey.UnmanagedSize, ref msgSize, ref rawCreator, channel);
                steamIDRemote = new SteamID(rawCreator);
                bufferKey.ReadFromUnmanagedMemory((int)msgSize);
                return returnvalue;
            }
        }
        public uint GetDisplayId(out string Buffer)
        {
            CheckIfUsable();

            using (NativeBuffer buffer = new NativeBuffer(Constants.Hmd.MaxIDBufferSize))
            {
                uint result = NativeMethods.VR_Hmd_GetDisplayId(buffer.UnmanagedMemory, (uint)buffer.UnmanagedSize);
                Buffer = NativeHelpers.ToStringAnsi(buffer.UnmanagedMemory);

                return result;
            }
        }
 public bool HandleIncomingPacket(byte[] data, uint ip, ushort port)
 {
     //CheckIfUsable();
     using (NativeBuffer packetBuffer = new NativeBuffer(data))
     {
         packetBuffer.WriteToUnmanagedMemory();
         return NativeMethods.GameServer_HandleIncomingPacket(packetBuffer.UnmanagedMemory,
             packetBuffer.UnmanagedSize, ip, port);
     }
 }
 public int GetFriendMessage(SteamID steamIDFriend, int messageID, int maxMessageSize,
     out string text, out ChatEntryType chatEntryType)
 {
     CheckIfUsable();
     int rawChatType = 0;
     using (NativeBuffer buffer = new NativeBuffer(maxMessageSize))
     {
         var result = NativeMethods.Friends_GetFriendMessage(steamIDFriend.AsUInt64, messageID,
             buffer.UnmanagedMemory, buffer.UnmanagedSize, ref rawChatType);
         chatEntryType = (ChatEntryType)rawChatType;
         text = NativeHelpers.ToStringUtf8(buffer.UnmanagedMemory);
         return result;
     }
 }
 public int GetClanChatMessage(SteamID steamIDClanChat, int message, int maxMessageSize, out string text,
     out ChatEntryType chatEntryType, out SteamID sender)
 {
     CheckIfUsable();
     int rawChatType = 0;
     ulong rawSender = 0;
     using (NativeBuffer buffer = new NativeBuffer(maxMessageSize))
     {
         var result = NativeMethods.Friends_GetClanChatMessage(steamIDClanChat.AsUInt64, message,
             buffer.UnmanagedMemory, buffer.UnmanagedSize, ref rawChatType, ref rawSender);
         chatEntryType = (ChatEntryType)rawChatType;
         sender = new SteamID(rawSender);
         text = NativeHelpers.ToStringUtf8(buffer.UnmanagedMemory);
         return result;
     }
 }
        public bool GetP2PSessionState(SteamID steamIDRemote, out P2PSessionState connectionState)
        {
            CheckIfUsable();
            using (NativeBuffer bufferKey = new NativeBuffer(Marshal.SizeOf(typeof(P2PSessionState))))
            {
                int rawSize = NativeMethods.Networking_GetP2PSessionStateSize();
                if (rawSize != bufferKey.UnmanagedSize)
                {
                    Error.ThrowError(ErrorCodes.CallbackStructSizeMissmatch, typeof(P2PSessionState).Name);
                }
                bool result = NativeMethods.Networking_GetP2PSessionState(steamIDRemote.AsUInt64, bufferKey.UnmanagedMemory);
                connectionState = NativeHelpers.ConvertStruct<P2PSessionState>(bufferKey.UnmanagedMemory, bufferKey.UnmanagedSize);
                return result;
            }

        }
 public bool SendDataOnSocket(NetSocketHandle socket, byte[] data, bool reliable)
 {
     CheckIfUsable();
     using (NativeBuffer bufferKey = new NativeBuffer(data))
     {
         bufferKey.WriteToUnmanagedMemory();
         return NativeMethods.Networking_SendDataOnSocket(socket.AsUInt32, bufferKey.UnmanagedMemory, (uint)bufferKey.UnmanagedSize, reliable);
     }
 }
        public int UGCRead(SteamTypes.UGCHandle handle, byte[] data, uint offset, UGCReadAction action)
        {
            CheckIfUsable();

            using (NativeBuffer buffer = new NativeBuffer(data))
            {
                int bytesRead = NativeMethods.Cloud_UGCRead(handle.AsUInt64, buffer.UnmanagedMemory,
                    buffer.UnmanagedSize, offset, (int)action);
                buffer.ReadFromUnmanagedMemory(bytesRead);
                return bytesRead;
            }
        }
 public int GetGlobalStatHistory(string name, out double[] data, int historyDays)
 {
     CheckIfUsable();
     byte[] byteData = new byte[historyDays * sizeof(double)];
     using (NativeBuffer buffer = new NativeBuffer(byteData))
     {
         int result = NativeMethods.Stats_GetGlobalStatHistoryDouble(name, buffer.UnmanagedMemory,
             (uint)historyDays);
         buffer.ReadFromUnmanagedMemory(result);
         data = NativeBuffer.ToDouble(byteData);
         return result;
     }
 }
        public void DownloadClanActivityCounts(SteamID[] clanIDs)
        {
            CheckIfUsable();

            byte[] idBuffer = NativeBuffer.ToBytes(clanIDs);

            using (NativeBuffer buffer = new NativeBuffer(idBuffer))
            {
                NativeMethods.Friends_DownloadClanActivityCounts(buffer.UnmanagedMemory, clanIDs.Length);
            }
        }
        public bool GetViewMatrix(float SecondsFromNow, out HmdMatrix44 MatLeftView, out HmdMatrix44 MatRightView, out HmdTrackingResult Result)
        {
            CheckIfUsable();

            using (NativeBuffer LeftViewBuffer = new NativeBuffer(Marshal.SizeOf(typeof(HmdMatrix44))))
            {
                using (NativeBuffer RightViewBuffer = new NativeBuffer(Marshal.SizeOf(typeof(HmdMatrix44))))
                {
                    int TempResult = 0;
                    bool result = NativeMethods.VR_Hmd_GetViewMatrix(SecondsFromNow, LeftViewBuffer.UnmanagedMemory, RightViewBuffer.UnmanagedMemory, ref TempResult);

                    MatLeftView = NativeHelpers.ConvertStruct<HmdMatrix44>(LeftViewBuffer.UnmanagedMemory, LeftViewBuffer.UnmanagedSize);
                    MatRightView = NativeHelpers.ConvertStruct<HmdMatrix44>(RightViewBuffer.UnmanagedMemory, RightViewBuffer.UnmanagedSize);
                    Result = (HmdTrackingResult)TempResult;

                    return result;
                }
            }
        }
        public bool SendLobbyChatMsg(SteamID steamIDLobby, byte[] msgBody)
        {
            CheckIfUsable();

            using (NativeBuffer buffer = new NativeBuffer(msgBody))
            {
                buffer.WriteToUnmanagedMemory();
                return NativeMethods.MatchMaking_SendLobbyChatMsg(steamIDLobby.AsUInt64, buffer.UnmanagedMemory, buffer.UnmanagedSize);
            }
        }
        public bool GetLastWorldFromHeadPose(out HmdMatrix34 Pose)
        {
            CheckIfUsable();

            using (NativeBuffer buffer = new NativeBuffer(Marshal.SizeOf(typeof(HmdMatrix34))))
            {
                bool result = NativeMethods.VR_Hmd_GetLastWorldFromHeadPose(buffer.UnmanagedMemory);
                Pose = NativeHelpers.ConvertStruct<HmdMatrix34>(buffer.UnmanagedMemory, buffer.UnmanagedSize);

                return result;
            }
        }
 public bool RetrieveData(NetListenSocketHandle listenSocket, byte[] dest, out uint msgSize, out NetSocketHandle socket)
 {
     CheckIfUsable();
     msgSize = 0;
     uint rawSocket = 0;
     using (NativeBuffer bufferKey = new NativeBuffer(dest))
     {
         bool result = NativeMethods.Networking_RetrieveData(listenSocket.AsUInt32,
             bufferKey.UnmanagedMemory, (uint)bufferKey.UnmanagedSize, ref msgSize, ref rawSocket);
         bufferKey.ReadFromUnmanagedMemory((int)msgSize);
         socket = new NetSocketHandle(rawSocket);
         return result;
     }
 }
        public bool SendUserConnectAndAuthenticate(uint ipClient, byte[] authenticationBlob, out SteamID steamIDUser)
        {
            //CheckIfUsable();

            using (NativeBuffer blobBuffer = new NativeBuffer(authenticationBlob))
            {
                blobBuffer.WriteToUnmanagedMemory();
                ulong rawCreator = 0;
                bool result = NativeMethods.GameServer_SendUserConnectAndAuthenticate(ipClient,
                    blobBuffer.UnmanagedMemory, (uint)blobBuffer.UnmanagedMemory, ref rawCreator);

                steamIDUser = new SteamID(rawCreator);
                return result;

            }

        }
        private ServerListRequestHandle ServerRequest(ServerRequestType requestType, AppID appID,
            MatchMakingKeyValuePair[] filters, MatchmakingServerListResponse requestResponse)
        {
            // Code reuse FTW!

            // Allocate an array that will hold addresses to native MatchMakingKeyValuePair objects
            using (NativeBuffer arrayBuffer = new NativeBuffer(Marshal.SizeOf(typeof(IntPtr)) * filters.Length))
            {
                NativeBuffer[] nativeObjects = new NativeBuffer[filters.Length];
                // Fill each array slot with an address to an actual object
                try
                {
                    for (int i = 0; i < filters.Length; i++)
                    {
                        // Copy the managed objects to native memory
                        NativeBuffer buffer = NativeBuffer.CopyToNative(filters[i]);
                        nativeObjects[i] = buffer;
                        // Add the native address to the array
                        Marshal.WriteInt32(arrayBuffer.UnmanagedMemory, i * Marshal.SizeOf(typeof(int)), buffer.UnmanagedMemory.ToInt32());
                    }

                    // Now do the actual request

                    switch (requestType)
                    {
                        case ServerRequestType.Internet:
                            return new ServerListRequestHandle(
                                NativeMethods.MatchmakingServers_RequestInternetServerList(appID.AsUInt32,
                                arrayBuffer.UnmanagedMemory, (uint)filters.Length, requestResponse.ObjectId));
                        case ServerRequestType.Friends:
                            return new ServerListRequestHandle(
                                NativeMethods.MatchmakingServers_RequestFriendsServerList(appID.AsUInt32,
                                arrayBuffer.UnmanagedMemory, (uint)filters.Length, requestResponse.ObjectId));
                        case ServerRequestType.Favorites:
                            return new ServerListRequestHandle(
                                NativeMethods.MatchmakingServers_RequestFavoritesServerList(appID.AsUInt32,
                                arrayBuffer.UnmanagedMemory, (uint)filters.Length, requestResponse.ObjectId));
                        case ServerRequestType.History:
                            return new ServerListRequestHandle(
                                NativeMethods.MatchmakingServers_RequestHistoryServerList(appID.AsUInt32,
                                arrayBuffer.UnmanagedMemory, (uint)filters.Length, requestResponse.ObjectId));
                        case ServerRequestType.Spectator:
                            return new ServerListRequestHandle(
                                NativeMethods.MatchmakingServers_RequestSpectatorServerList(appID.AsUInt32,
                                arrayBuffer.UnmanagedMemory, (uint)filters.Length, requestResponse.ObjectId));
                        default:
                            // This should never happen as our code can not be allowed to make this error
                            throw new ArgumentException();
                    }
                }
                finally
                {
                    // Cleanup in all cases. Exception or not.
                    foreach (var item in nativeObjects)
                    {
                        if (item != null)
                        {
                            item.Dispose();
                        }
                    }
                }
            }
        }
 public int GetNextOutgoingPacket(byte[] data, out uint netAdr, out ushort port)
 {
     //CheckIfUsable();
     netAdr = 0;
     port = 0;
     using (NativeBuffer packetBuffer = new NativeBuffer(data))
     {
         int result = NativeMethods.GameServer_GetNextOutgoingPacket(packetBuffer.UnmanagedMemory, packetBuffer.UnmanagedSize, ref netAdr, ref port);
         packetBuffer.ReadFromUnmanagedMemory(result);
         return result;
     }
 }
        public bool GetLobbyDataByIndex(SteamID steamIDLobby, int lobbyData, out string key, out string value)
        {
            CheckIfUsable();

            using (NativeBuffer bufferKey = new NativeBuffer(Constants.Matchmaking.MaxLobbyKeyLength))
            {
                using (NativeBuffer bufferValue = new NativeBuffer(Constants.Matchmaking.MaxLobbyValueLength))
                {
                    bool result = NativeMethods.MatchMaking_GetLobbyDataByIndex(steamIDLobby.AsUInt64,
                        lobbyData, bufferKey.UnmanagedMemory, bufferKey.UnmanagedSize,
                        bufferValue.UnmanagedMemory, bufferValue.UnmanagedSize);

                    key = NativeHelpers.ToStringAnsi(bufferKey.UnmanagedMemory);
                    value = NativeHelpers.ToStringAnsi(bufferValue.UnmanagedMemory);
                    return result;
                }
            }
        }