Characters
This commit is contained in:
101
Game/Scripts/Networking/SteamBootstrap.cs
Normal file
101
Game/Scripts/Networking/SteamBootstrap.cs
Normal file
@@ -0,0 +1,101 @@
|
||||
using Steamworks;
|
||||
using UnityEngine;
|
||||
|
||||
namespace MegaKoop.Game.Networking
|
||||
{
|
||||
/// <summary>
|
||||
/// Handles SteamAPI initialization and callback pumping. Place once in the initial scene.
|
||||
/// </summary>
|
||||
[DefaultExecutionOrder(-1000)]
|
||||
public class SteamBootstrap : MonoBehaviour
|
||||
{
|
||||
private static bool isInitialized;
|
||||
private static SteamBootstrap instance;
|
||||
|
||||
public static bool IsInitialized => isInitialized;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
if (instance != null)
|
||||
{
|
||||
Destroy(gameObject);
|
||||
return;
|
||||
}
|
||||
|
||||
instance = this;
|
||||
DontDestroyOnLoad(gameObject);
|
||||
|
||||
TryInitializeSteam();
|
||||
}
|
||||
|
||||
private void TryInitializeSteam()
|
||||
{
|
||||
if (isInitialized)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (!Packsize.Test())
|
||||
{
|
||||
Debug.LogError("[SteamBootstrap] Packsize Test returned false. Wrong Steamworks binaries for this platform?");
|
||||
return;
|
||||
}
|
||||
|
||||
if (!DllCheck.Test())
|
||||
{
|
||||
Debug.LogError("[SteamBootstrap] DllCheck Test returned false. Missing Steamworks dependencies.");
|
||||
return;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
isInitialized = SteamAPI.Init();
|
||||
}
|
||||
catch (System.DllNotFoundException e)
|
||||
{
|
||||
Debug.LogError("[SteamBootstrap] Steamworks native binaries not found: " + e.Message);
|
||||
isInitialized = false;
|
||||
}
|
||||
|
||||
if (!isInitialized)
|
||||
{
|
||||
Debug.LogError("[SteamBootstrap] Failed to initialize Steam API.");
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.Log("[SteamBootstrap] Steam API initialized for user " + SteamFriends.GetPersonaName());
|
||||
}
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
if (!isInitialized)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
SteamAPI.RunCallbacks();
|
||||
}
|
||||
|
||||
private void OnDestroy()
|
||||
{
|
||||
if (instance == this)
|
||||
{
|
||||
ShutdownSteam();
|
||||
instance = null;
|
||||
}
|
||||
}
|
||||
|
||||
private void ShutdownSteam()
|
||||
{
|
||||
if (!isInitialized)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
SteamAPI.Shutdown();
|
||||
isInitialized = false;
|
||||
Debug.Log("[SteamBootstrap] Steam API shutdown.");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user