Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- using System;
- using System.Runtime.Serialization.Formatters.Binary;
- using System.IO;
- using Steamworks;
- using System.Collections.Generic;
- public class GameControl : MonoBehaviour {
- public static GameControl Access;
- #region DebugVariables
- public bool DisplayDebug;
- int DebugAccess;
- public string DebugText;
- #endregion
- public bool save;
- public bool load;
- public bool delete;
- protected Callback<GameOverlayActivated_t> m_GameOverlayActivated;
- protected Callback<AvatarImageLoaded_t> m_AvatarImageLoaded;
- public SteamInformation SteamInfo;
- public int PowerCubeCount;
- public LevelData LoadedLevel;
- public string FromEditorFilename;
- public bool PlayingFromEditor;
- /// <summary>
- /// This is the current level in the package.
- /// </summary>
- public int PackageIndex;
- /// <summary>
- /// This is how many levels are in the package we're playing.
- /// </summary>
- public int PackageMax;
- public LevelPackage LoadedPackage;
- public bool AvatarLoaded;
- public List<bool> PackageLevelsFinished;
- public bool IsCampaign;
- public int Campaign_Section;
- public int Campaign_Level;
- private void OnGameOverlayActivated(GameOverlayActivated_t pCallback)
- {
- if (pCallback.m_bActive != 0)
- {
- Debug.Log("Steam Overlay has been activated");
- MusicController.Access.OverlayActive = true;
- }
- else
- {
- Debug.Log("Steam Overlay has been closed");
- MusicController.Access.OverlayActive = false;
- }
- }
- public void SubmitFeedback()
- {
- SteamFriends.ActivateGameOverlayToWebPage("http://feedback.officialdiamondgame.com");
- if (Application.isEditor)
- Application.OpenURL("http://feedback.officialdiamondgame.com");
- }
- public void PlayLevel(LevelData Level, int Camp_Section = 0, int Camp_Level = 0, bool InternalTest = false)
- {
- Campaign_Section = Camp_Section;
- Campaign_Level = Camp_Level;
- IsCampaign = (Campaign_Section != 0 && Campaign_Level != 0);
- LoadedLevel = Level;
- LoadedLevel.OriginalSteamID = Level.OriginalSteamID;
- LoadedLevel.LevelDescription = Level.LevelDescription;
- LoadedLevel.m_PublishedFileId = Level.m_PublishedFileId;
- LoadedLevel.EligibleForLeaderboard = Level.EligibleForLeaderboard;
- PlayGlobals.Access.IsPackage = false;
- try
- {
- T_LoadScene.Access.LoadLevel("Player");
- }
- catch
- {
- Debug.Log("Unable to load async. Force loading.");
- if (!InternalTest)
- UnityEngine.SceneManagement.SceneManager.LoadScene("Player");
- PlayGlobals.Access.Restarting = true;
- }
- PlayGlobals.Access.LoadData = true;
- }
- /// <summary>
- /// Play a package.
- /// </summary>
- /// <param name="Package">Package being loaded.</param>
- /// <param name="Index">Level in package to be loaded.</param>
- public void PlayPackage(LevelPackage Package, int Index = 0)
- {
- LoadedPackage = Package;
- LoadedPackage.OriginalSteamID = Package.OriginalSteamID;
- LoadedPackage.PackageDescription = Package.PackageDescription;
- LoadedPackage.m_PublishedFileId = Package.m_PublishedFileId;
- LoadedLevel = Package.PackageLevels[Index];
- LoadedLevel.OriginalSteamID = Package.PackageLevels[Index].OriginalSteamID;
- LoadedLevel.LevelDescription = Package.PackageLevels[Index].LevelDescription;
- LoadedLevel.m_PublishedFileId = Package.PackageLevels[Index].m_PublishedFileId;
- PackageIndex = 0;
- PackageMax = Package.PackageLevels.Count - 1;
- PackageLevelsFinished.Clear();
- foreach(LevelData Level in Package.PackageLevels)
- {
- PackageLevelsFinished.Add(false);
- }
- PlayGlobals.Access.IsPackage = true;
- T_LoadScene.Access.LoadLevel("Player");
- PlayGlobals.Access.LoadData = true;
- }
- public void CampaignLevelSwitch(LevelData Level, int Camp_Section = 0, int Camp_Level = 0)
- {
- Campaign_Section = Camp_Section;
- Campaign_Level = Camp_Level;
- IsCampaign = (Campaign_Section != 0 && Campaign_Level != 0);
- LoadedLevel = Level;
- LoadedLevel.OriginalSteamID = Level.OriginalSteamID;
- LoadedLevel.LevelDescription = Level.LevelDescription;
- LoadedLevel.m_PublishedFileId = Level.m_PublishedFileId;
- LoadedLevel.EligibleForLeaderboard = Level.EligibleForLeaderboard;
- PlayGlobals.Access.IsPackage = false;
- PlayGlobals.Access.ReloadLevel();
- }
- public void SwitchLevel(LevelData Level, int Index)
- {
- LoadedLevel = Level;
- LoadedLevel.OriginalSteamID = Level.OriginalSteamID;
- LoadedLevel.LevelDescription = Level.LevelDescription;
- LoadedLevel.m_PublishedFileId = Level.m_PublishedFileId;
- PackageIndex = Index;
- PlayGlobals.Access.IsPackage = true;
- PlayGlobals.Access.ReloadLevel();
- }
- ControllerHandle_t[] sHandle;
- public int Num;
- void Start()
- {
- if (SteamManager.Initialized)
- {
- SteamInfo.Initialized = SteamManager.Initialized;
- SteamInfo.UserName = SteamFriends.GetPersonaName();
- CSteamID m_Friend = SteamUser.GetSteamID();
- SteamInfo.UserCID = m_Friend;
- SteamInfo.UserID = Convert.ToString( m_Friend );
- #region Load Avatar
- int FriendAvatar = SteamFriends.GetLargeFriendAvatar(m_Friend);
- AvatarLoaded = (FriendAvatar > 0);
- m_GameOverlayActivated = Callback<GameOverlayActivated_t>.Create(OnGameOverlayActivated);
- m_AvatarImageLoaded = Callback<AvatarImageLoaded_t>.Create(OnAvatarImageLoaded);
- uint ImageWidth;
- uint ImageHeight;
- bool ret = SteamUtils.GetImageSize(FriendAvatar, out ImageWidth, out ImageHeight);
- if (FriendAvatar == -1)
- Debug.Log("Large avatar is still loading...");
- if (ret && ImageWidth > 0 && ImageHeight > 0)
- {
- byte[] Image = new byte[ImageWidth * ImageHeight * 4];
- ret = SteamUtils.GetImageRGBA(FriendAvatar, Image, (int)(ImageWidth * ImageHeight * 4));
- if (ret)
- {
- SteamInfo.UserAvatar = new Texture2D((int)ImageWidth, (int)ImageHeight, TextureFormat.RGBA32, false, true);
- SteamInfo.UserAvatar.LoadRawTextureData(Image);
- SteamInfo.UserAvatar.Apply();
- }
- }
- #endregion
- //Load();
- }
- }
- void OnAvatarImageLoaded(AvatarImageLoaded_t pCallback)
- {
- Debug.Log("[" + AvatarImageLoaded_t.k_iCallback + " - AvatarImageLoaded] - " + pCallback.m_steamID + " -- " + pCallback.m_iImage + " -- " + pCallback.m_iWide + " -- " + pCallback.m_iTall);
- int FriendAvatar = SteamFriends.GetLargeFriendAvatar(SteamInfo.UserCID);
- m_GameOverlayActivated = Callback<GameOverlayActivated_t>.Create(OnGameOverlayActivated);
- m_AvatarImageLoaded = Callback<AvatarImageLoaded_t>.Create(OnAvatarImageLoaded);
- uint ImageWidth;
- uint ImageHeight;
- bool ret = SteamUtils.GetImageSize(FriendAvatar, out ImageWidth, out ImageHeight);
- if (ret && ImageWidth > 0 && ImageHeight > 0)
- {
- byte[] Image = new byte[ImageWidth * ImageHeight * 4];
- ret = SteamUtils.GetImageRGBA(FriendAvatar, Image, (int)(ImageWidth * ImageHeight * 4));
- if (ret)
- {
- SteamInfo.UserAvatar = new Texture2D((int)ImageWidth, (int)ImageHeight, TextureFormat.RGBA32, false, true);
- SteamInfo.UserAvatar.LoadRawTextureData(Image);
- SteamInfo.UserAvatar.Apply();
- }
- }
- AvatarLoaded = true;
- }
- void OnEnable ()
- {
- if (Access == null)
- {
- DontDestroyOnLoad(gameObject);
- Access = this;
- }
- else if (Access != this)
- {
- Destroy(gameObject);
- }
- }
- void Update ()
- {
- SteamAPI.RunCallbacks();
- #region Accessing Debug
- if (Input.GetKeyDown(KeyCode.BackQuote))
- DebugAccess++;
- if (!Input.GetKey(KeyCode.BackQuote) && Input.anyKey)
- {
- DebugAccess = 0;
- }
- if (DebugAccess >= 3)
- {
- DebugAccess = 0;
- DisplayDebug = !DisplayDebug;
- }
- #endregion
- #region Obsolete FileTesting
- //if (save)
- //{
- // save = false;
- // Save();
- //}
- //if (load)
- //{
- // load = false;
- // Load();
- //}
- //if (delete)
- //{
- // delete = false;
- // Delete();
- //}
- #endregion
- }
- #region Obsolete Save System
- //public void Delete()
- //{
- // if (File.Exists(Application.persistentDataPath +"/" + SteamInfo.UserID + ".dat"))
- // {
- // File.Delete(Application.persistentDataPath +"/" + SteamInfo.UserID + ".dat");
- // Debug.Log("Deleted file at " + Application.persistentDataPath);
- // AchievementUnlocked("Savage");
- // }
- // else
- // Debug.Log("File not found.");
- //}
- //public void Save(bool NewFile = false)
- //{
- // if (SteamInfo.Initialized)
- // {
- // BinaryFormatter formatter = new BinaryFormatter();
- // FileStream file = File.Create(Application.persistentDataPath + "/" + SteamInfo.UserID + ".dat");
- // UserData data = new UserData();
- // if (!NewFile)
- // {
- // //data.cubecount = PowerCubeCount;
- // }
- // formatter.Serialize(file, data);
- // file.Close();
- // Debug.Log("Finished saving to " + Application.persistentDataPath);
- // }
- // else
- // Debug.Log("You must be logged into Steam to save.");
- //}
- //public void Load()
- //{
- // if (SteamInfo.Initialized)
- // {
- // if (File.Exists(Application.persistentDataPath + "/" + SteamInfo.UserID + ".dat"))
- // {
- // BinaryFormatter formatter = new BinaryFormatter();
- // FileStream file = File.Open(Application.persistentDataPath + "/" + SteamInfo.UserID + ".dat", FileMode.Open);
- // UserData data = (UserData)formatter.Deserialize(file);
- // file.Close();
- // PowerCubeCount = data.cubecount;
- // Debug.Log("Load successful for: " + SteamInfo.UserName);
- // }
- // else
- // {
- // Debug.Log("No save file found for: " + SteamInfo.UserName);
- // }
- // }
- // else
- // {
- // Debug.Log("You must be logged into Steam to load.");
- // }
- //}
- #endregion
- public void DebugInfo(string Display)
- {
- DebugText = Display;
- }
- public void ClearStats(bool NotAchievements = false)
- {
- SteamUserStats.ResetAllStats(!NotAchievements);
- if (NotAchievements)
- Debug.Log("You cleared all stats.");
- else
- Debug.Log("You've cleared all stats and achievements.");
- }
- public void AchievementUnlocked(string AchievementName)
- {
- SteamUserStats.SetAchievement(AchievementName);
- Debug.Log("Achievement Unlocked: " + AchievementName);
- SteamUserStats.StoreStats();
- }
- }
- [Serializable]
- class UserData
- {
- public int cubecount;
- }
- [System.Serializable]
- public class SteamInformation
- {
- public bool Initialized;
- public string UserName;
- public CSteamID UserCID;
- public string UserID;
- public Texture2D UserAvatar;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement