TH1/Unity/Assets/Scripts/TH1_Logic/Steam/TH1SteamManager.cs
2025-09-30 17:06:03 +08:00

400 lines
14 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// using System.Collections.Generic;
// using System.IO;
// using UnityEngine;
// using Steamworks;
// using Logic.CrashSight;
// using TH1_Logic.Net;
//
//
// namespace TH1_Logic.Steam
// {
// public class TH1SteamManager
// {
// public static TH1SteamManager Instance { get; } = new TH1SteamManager();
//
// [Header("Steam 连接状态")]
// public bool IsSteamInitialized = false;
// public bool IsLoggedIn = false;
// public string CurrentUserName = "";
// public CSteamID CurrentUserID;
//
// [Header("房间测试")]
// private int _maxLobbyMembers = 4;
// private SteamLobbyManager _lobby;
// private GameObject _guiObj;
//
//
// // 初始化Steam
// public void Init()
// {
// LogSystem.LogInfo("开始初始化Steam...");
// LogSystem.LogInfo($"启动方式检测: {(SteamAPI.RestartAppIfNecessary(new Steamworks.AppId_t(480)) ? "需要通过Steam启动" : "直接启动或已通过Steam启动")}");
//
// // 检查Steam是否运行
// if (!SteamAPI.IsSteamRunning())
// {
// LogSystem.LogError("Steam客户端未运行请先启动Steam。");
// return;
// }
//
// // 检查steam_appid.txt文件状态
// CheckSteamAppIdFile();
//
// // 初始化Steam API
// IsSteamInitialized = SteamAPI.Init();
//
// if (!IsSteamInitialized)
// {
// LogSystem.LogError("Steam API初始化失败");
// LogSystem.LogError("可能的原因:");
// LogSystem.LogError("1. Steam客户端未运行");
// LogSystem.LogError("2. steam_appid.txt文件配置错误开发环境");
// LogSystem.LogError("3. 没有以Steam方式启动应用开发环境");
// LogSystem.LogError("4. Steam App ID不匹配");
// return;
// }
// LogSystem.LogInfo("Steam API初始化成功");
//
// // 检查用户登录状态
// try
// {
// CheckUserLoginStatus();
// }
// catch (System.Exception e)
// {
// LogSystem.LogError($"检查用户登录状态异常: {e.Message}");
// }
//
// // 显示启动信息
// DisplayLaunchInfo();
//
// // 初始化Steam房间管理器
// try
// {
// InitializeSteamLobbyManager();
// }
// catch (System.Exception e)
// {
// LogSystem.LogError($"Steam房间管理器初始化异常: {e.Message}");
// }
//
// LogSystem.LogInfo("Steam测试环境初始化完成");
//
// // 构建 GUI 输入 Mono
// _guiObj = new GameObject();
// _guiObj.name = "SteamGUI";
// _guiObj.AddComponent<SteamGUIMono>();
// _guiObj.SetActive(true);
// }
//
// // 定时更新
// public void Update()
// {
// if (!IsSteamInitialized || !IsLoggedIn) return;
//
// // 更新Steam回调
// SteamAPI.RunCallbacks();
//
// // 更新P2P消息
// _lobby.Update();
// }
//
// /// <summary>
// /// 检查steam_appid.txt文件状态
// /// </summary>
// private void CheckSteamAppIdFile()
// {
// string steamAppIdPath = Path.Combine(Directory.GetCurrentDirectory(), "steam_appid.txt");
//
// if (File.Exists(steamAppIdPath))
// {
// try
// {
// string content = File.ReadAllText(steamAppIdPath).Trim();
// LogSystem.LogInfo($"发现steam_appid.txt文件App ID: {content}");
// }
// catch (System.Exception e)
// {
// LogSystem.LogWarning($"读取steam_appid.txt失败: {e.Message}");
// }
// }
// else
// {
// LogSystem.LogInfo("未发现steam_appid.txt文件 - 这在Steam平台发布时是正常的");
// }
// }
//
// /// <summary>
// /// 显示启动信息
// /// </summary>
// private void DisplayLaunchInfo()
// {
// if (!IsSteamInitialized) return;
//
// // 获取当前App ID
// var currentAppId = SteamUtils.GetAppID();
// LogSystem.LogInfo($"当前Steam App ID: {currentAppId}");
//
// // 检查启动方式
// bool launchedViaSteam = SteamApps.BIsSubscribedApp(currentAppId);
// LogSystem.LogInfo($"通过Steam启动: {(launchedViaSteam ? "是" : "否")}");
//
// // 显示Steam环境信息
// LogSystem.LogInfo($"Steam语言: {SteamApps.GetCurrentGameLanguage()}");
// LogSystem.LogInfo($"Steam服务器连接: {(SteamUser.BLoggedOn() ? "已连接" : "未连接")}");
//
// // 检查DLC和订阅状态
// if (currentAppId.m_AppId == 480) // Spacewar测试应用
// {
// LogSystem.LogInfo("当前使用Spacewar测试应用 - 适用于开发测试");
// }
// else
// {
// LogSystem.LogInfo($"当前使用正式应用ID: {currentAppId.m_AppId}");
// }
// }
//
// /// <summary>
// /// 检查用户登录状态
// /// </summary>
// private void CheckUserLoginStatus()
// {
// if (!IsSteamInitialized) return;
//
// IsLoggedIn = SteamUser.BLoggedOn();
//
// if (IsLoggedIn)
// {
// CurrentUserID = SteamUser.GetSteamID();
// CurrentUserName = SteamFriends.GetPersonaName();
// LogSystem.LogInfo($"Steam用户已登录: {CurrentUserName} ({CurrentUserID})");
// }
// else
// {
// LogSystem.LogWarning("Steam用户未登录");
// }
// }
//
// /// <summary>
// /// 初始化Steam房间管理器
// /// </summary>
// private void InitializeSteamLobbyManager()
// {
// if (!IsSteamInitialized || !IsLoggedIn) return;
// try
// {
// _lobby ??= new SteamLobbyManager();
// // 初始化房间管理器
// _lobby.InitCallbacks();
//
// // 订阅事件
// _lobby.OnLobbyCreatedEvent += OnLobbyCreated;
// _lobby.OnLobbyEnteredEvent += OnLobbyEntered;
// _lobby.OnLobbyLeftEvent += OnLobbyLeft;
// _lobby.OnMemberJoinedEvent += OnMemberJoined;
// _lobby.OnMemberLeftEvent += OnMemberLeft;
// _lobby.OnHostChangedEvent += OnHostChanged;
// _lobby.OnLobbyErrorEvent += OnLobbyError;
//
// // 订阅P2P事件
// SimpleP2P.Instance.OnPeerConnectedEvent += OnP2PPeerConnected;
// SimpleP2P.Instance.OnPeerDisconnectedEvent += OnP2PPeerDisconnected;
// SimpleP2P.Instance.OnMessageReceivedEvent += OnP2PMessageReceived;
// SimpleP2P.Instance.OnConnectionErrorEvent += OnP2PConnectionError;
//
// LogSystem.LogInfo("Steam房间管理器初始化成功");
// LobbyManager.Instance.Lobby = _lobby;
// }
// catch (System.Exception e)
// {
// LogSystem.LogError($"Steam房间管理器初始化失败: {e.Message}");
// }
// }
//
// private void OnDestroy()
// {
// if (IsSteamInitialized)
// {
// _lobby.Cleanup();
// SteamAPI.Shutdown();
// LogSystem.LogInfo("Steam API已关闭");
// }
// }
//
// #region 房间事件处理
//
// private void OnLobbyCreated(CSteamID lobbyId)
// {
// LogSystem.LogInfo($"[测试] 房间创建成功: {lobbyId}");
// }
//
// private void OnLobbyEntered(CSteamID lobbyId)
// {
// LogSystem.LogInfo($"[测试] 进入房间: {lobbyId}");
// LogSystem.LogInfo($"[测试] 房间成员数: {_lobby.GetMemberCount()}/{_lobby.GetMemberLimit()}");
// }
//
// private void OnLobbyLeft()
// {
// LogSystem.LogInfo("[测试] 离开房间");
// }
//
// private void OnMemberJoined(CSteamID memberId)
// {
// string memberName = SteamFriends.GetFriendPersonaName(memberId);
// LogSystem.LogInfo($"[测试] 成员加入: {memberName} ({memberId})");
// }
//
// private void OnMemberLeft(CSteamID memberId)
// {
// string memberName = SteamFriends.GetFriendPersonaName(memberId);
// LogSystem.LogInfo($"[测试] 成员离开: {memberName} ({memberId})");
// }
//
// private void OnHostChanged(CSteamID oldHost, CSteamID newHost)
// {
// string oldHostName = SteamFriends.GetFriendPersonaName(oldHost);
// string newHostName = SteamFriends.GetFriendPersonaName(newHost);
// LogSystem.LogInfo($"[测试] 房主变更: {oldHostName} -> {newHostName}");
// }
//
// private void OnLobbyError(string error)
// {
// LogSystem.LogError($"[测试] 房间错误: {error}");
// }
//
// #endregion
//
// #region P2P事件处理
//
// private void OnP2PPeerConnected(CSteamID peerId)
// {
// string peerName = SteamFriends.GetFriendPersonaName(peerId);
// LogSystem.LogInfo($"[测试] P2P连接建立: {peerName} ({peerId})");
// }
//
// private void OnP2PPeerDisconnected(CSteamID peerId)
// {
// string peerName = SteamFriends.GetFriendPersonaName(peerId);
// LogSystem.LogInfo($"[测试] P2P连接断开: {peerName} ({peerId})");
// }
//
// private void OnP2PMessageReceived(CSteamID senderId, byte[] data)
// {
// string senderName = SteamFriends.GetFriendPersonaName(senderId);
// string message = System.Text.Encoding.UTF8.GetString(data);
// LogSystem.LogInfo($"[测试] 收到P2P消息 from {senderName}: {message}");
// }
//
// private void OnP2PConnectionError(string error)
// {
// LogSystem.LogError($"[测试] P2P连接错误: {error}");
// }
//
// #endregion
//
// #region 测试方法
//
// // 创建测试房间
// public void CreateLobby()
// {
// if (!CanPerformLobbyAction()) return;
//
// LogSystem.LogInfo("[测试] 创建房间...");
// _lobby.CreateFriendsLobby(_maxLobbyMembers);
// }
//
// // 离开房间
// public void LeaveLobby()
// {
// if (!_lobby.IsInLobby())
// {
// LogSystem.LogWarning("[测试] 当前不在房间中");
// return;
// }
//
// LogSystem.LogInfo("[测试] 离开房间...");
// _lobby.LeaveLobby();
// }
//
// // 解散房间
// public void DisbandLobby()
// {
// if (!_lobby.IsInLobby())
// {
// LogSystem.LogWarning("[测试] 当前不在房间中");
// return;
// }
//
// if (!_lobby.IsLobbyOwner())
// {
// LogSystem.LogWarning("[测试] 只有房主可以解散房间");
// return;
// }
//
// LogSystem.LogInfo("[测试] 解散房间...");
// _lobby.DisbandLobby();
// }
//
// // 发送测试消息
// public void SendMessage()
// {
// if (!_lobby.IsInLobby())
// {
// LogSystem.LogWarning("[测试] 当前不在房间中,无法发送消息");
// return;
// }
//
// string testMessage = $"测试消息 from {CurrentUserName} at {System.DateTime.Now:HH:mm:ss}";
// GameNetSender.Instance.BroadcastString(testMessage);
// LogSystem.LogInfo($"[测试] 广播消息: {testMessage}");
// }
//
// // 显示在线好友
// public void ShowOnlineFriends()
// {
// if (!CanPerformLobbyAction()) return;
//
// var friends = _lobby.GetOnlineFriends();
// LogSystem.LogInfo($"[测试] 在线好友数量: {friends.Count}");
//
// foreach (var friend in friends)
// {
// LogSystem.LogInfo($"[测试] 好友: {friend.name} ({friend.id})");
// }
// }
//
// // 打开邀请界面
// public void OpenInviteOverlay()
// {
// if (!_lobby.IsInLobby())
// {
// LogSystem.LogWarning("[测试] 当前不在房间中,无法邀请好友");
// return;
// }
//
// LogSystem.LogInfo("[测试] 打开Steam邀请界面...");
// _lobby.OpenInviteOverlay();
// }
//
// private bool CanPerformLobbyAction()
// {
// if (!IsSteamInitialized)
// {
// LogSystem.LogError("[测试] Steam未初始化");
// return false;
// }
//
// if (!IsLoggedIn)
// {
// LogSystem.LogError("[测试] Steam用户未登录");
// return false;
// }
//
// return true;
// }
//
// #endregion
// }
// }