这个项目能做什么

# Facepunch.Steamworks 另一个 C# Steamworks 实现,旨在作为 Steamworks API 的现代、易用封装。它力求成为一个单一 C# DLL,除 Steam 本身外无其他原生依赖,支持 Windows、Linux 和 MacOS,包括 Unity 和 IL2CPP。 ## 特性 - 跨平台:Windows、Linux、MacOS - 支持 Unity,包括 IL2CPP - 异步回调(Steam callresults)和事件(Steam callbacks) - 单一 C# DLL,无第三方原生 DLL - 采用 MIT 许可证开源 - 支持 32 位操作系统 ## 为什么选择此库 作者发现现有的 C# Steamworks 实现存在不足:它们不够符合 C# 习惯、过时、需要原生 DLL、无法在 Unity 中编译为独立 DLL、不免费或许可证受限。本库旨在以更易用的方式封装 Steamworks API。 ## 使用示例 ### 获取用户信息 ```csharp SteamClient.SteamId // 你的 SteamId SteamClient.Name // 你的名字 ``` ### 好友列表 ```csharp foreach ( var friend in SteamFriends.GetFriends() ) { Console.WriteLine( $"{friend.Id}: {friend.Name}" ); Console.WriteLine( $"{friend.IsOnline} / {friend.SteamLevel}" ); friend.SendMessage( "Hello Friend" ); } ``` ### 应用信息 ```csharp Console.WriteLine( SteamApps.GameLanguage ); var installDir = SteamApps.AppInstallDir( 4000 ); var fileinfo = await SteamApps.GetFileDetailsAsync( "hl2.exe" ); ``` ### 头像 ```csharp var image = await SteamFriends.GetLargeAvatarAsync( steamid ); if ( !image.HasValue ) return DefaultImage; return MakeTextureFromRGBA( image.Value.Data, image.Value.Width, image.Value.Height ); ``` ### 服务器列表 ```csharp using ( var list = new ServerList.Internet() ) { list.AddFilter( "map", "de_dust" ); await list.RunQueryAsync(); foreach ( var server in list.Responsive ) { Console.WriteLine( $"{server.Address} {server.Name}" ); } } ``` ### 成就 列出并解锁成就: ```csharp foreach ( var a in SteamUserStats.Achievements ) { Console.WriteLine( $"{a.Name} ({a.State})" ); } var ach = new Achievement( "GM_PLAYED_WITH_GARRY" ); ach.Trigger(); ``` ### 语音 ```csharp SteamUser.VoiceRecord = KeyDown( "V" ); if ( SteamUser.HasVoiceData ) { var bytesrwritten = SteamUser.ReadVoiceData( stream ); } ``` ### 身份验证 客户端和服务器身份验证: ```csharp var ticket = SteamUser.GetAuthSessionTicket(); SteamServer.OnValidateAuthTicketResponse += ( steamid, ownerid, rsponse ) => { if ( rsponse == AuthResponse.OK ) TellUserTheyCanBeOnServer( steamid ); else KickUser( steamid ); }; if ( !SteamServer.BeginAuthSession( ticketData, clientSteamId ) ) { KickUser( clientSteamId ); } ticket.Cancel(); ``` ### 工具 ```csharp SteamUtils.SecondsSinceAppActive; SteamUtils.SecondsSinceComputerActive; SteamUtils.IpCountry; SteamUtils.UsingBatteryPower; SteamUtils.CurrentBatteryPower; SteamUtils.AppId; SteamUtils.IsOverlayEnabled; SteamUtils.IsSteamRunningInVR; SteamUtils.IsSteamInBigPictureMode; ``` ### 创意工坊 下载、查询和发布创意工坊物品: ```csharp SteamUGC.Download( 1717844711 ); var itemInfo = await Ugc.Item.Get( 1720164672 ); var q = Ugc.Query.All .WithTag( "Fun" ) .WithTag( "Movie" ) .MatchAllTags(); var result = await q.GetPageAsync( 1 ); var q = Ugc.UserQuery.All.CreatedByFriends(); var q = Ugc.UserQuery.All.FromSelf(); var result = await Ugc.Editor.NewCommunityFile .WithTitle( "My New FIle" ) .WithDescription( "This is a description" ) .WithContent( "c:/folder/addon/location" ) .WithTag( "awesome" ) .WithTag( "small" ) .SubmitAsync( iProgressBar ); ``` ### Steam 云 ```csharp SteamRemoteStorage.FileWrite( "file.txt", fileContents ); var fileContents = SteamRemoteStorage.FileRead( "file.txt" ); foreach ( var file in SteamRemoteStorage.Files ) { Console.WriteLine( $"{file} ({SteamRemoteStorage.FileSize(file)} {SteamRemoteStorage.FileTime( file )})" ); } ``` ### Steam 库存 ```csharp foreach ( InventoryDef def in SteamInventory.Definitions ) { Console.WriteLine( $"{def.Name}" ); } var defs = await SteamInventory.GetDefinitionsWithPricesAsync(); var result = await SteamInventory.GetItems(); using ( result ) { var items = result?.GetItems( bWithProperties ); foreach ( InventoryItem item in items ) { Console.WriteLine( $"{item.Id} / {item.Quantity} / {item.Def.Name} " ); } } ``` ## 入门指南 ### 客户端初始化 ```csharp using Steamworks; try { SteamClient.Init( 4000 ); } catch ( System.Exception e ) { // 由于某些原因无法初始化(例如 Steam 已关闭) } // 完成时: SteamClient.Shutdown(); ``` ### 服务器初始化 ```csharp var serverInit = new SteamServerInit( "gmod", "Garry Mode" ) { GamePort = 28015, Secure = true, QueryPort = 28016 }; try { Steamworks.SteamServer.Init( 4000, serverInit ); } catch ( System.Exception ) { // 由于某些原因无法初始化(dll 错误、端口被阻止) } ``` ## 帮助与贡献 欢迎通过 pull requests 和 bug 报告进行贡献。如需帮助和讨论,请访问 [Steamworks Thread](http://steamcommunity.com/groups/steamworks/discussions/0/1319961618833314524/)。还有一个 [wiki](https://wiki.facepunch.com/steamworks/) 包含示例和建议。 ## 许可证 MIT - 随意使用。