Advertisement
CassataGames

Untitled

Mar 25th, 2020
493
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 12.10 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using System;
  4. using System.Runtime.Serialization.Formatters.Binary;
  5. using System.IO;
  6. using Steamworks;
  7. using System.Collections.Generic;
  8. public class GameControl : MonoBehaviour {
  9.  
  10.     public static GameControl Access;
  11.     #region DebugVariables
  12.     public bool DisplayDebug;
  13.     int DebugAccess;
  14.     public string DebugText;
  15.     #endregion
  16.     public bool save;
  17.     public bool load;
  18.     public bool delete;
  19.     protected Callback<GameOverlayActivated_t> m_GameOverlayActivated;
  20.     protected Callback<AvatarImageLoaded_t> m_AvatarImageLoaded;
  21.  
  22.     public SteamInformation SteamInfo;
  23.     public int PowerCubeCount;
  24.  
  25.  
  26.     public LevelData LoadedLevel;
  27.     public string FromEditorFilename;
  28.     public bool PlayingFromEditor;
  29.  
  30.     /// <summary>
  31.     /// This is the current level in the package.
  32.     /// </summary>
  33.     public int PackageIndex;
  34.     /// <summary>
  35.     /// This is how many levels are in the package we're playing.
  36.     /// </summary>
  37.     public int PackageMax;
  38.     public LevelPackage LoadedPackage;
  39.  
  40.     public bool AvatarLoaded;
  41.  
  42.     public List<bool> PackageLevelsFinished;
  43.  
  44.     public bool IsCampaign;
  45.     public int Campaign_Section;
  46.     public int Campaign_Level;
  47.  
  48.     private void OnGameOverlayActivated(GameOverlayActivated_t pCallback)
  49.     {
  50.         if (pCallback.m_bActive != 0)
  51.         {
  52.             Debug.Log("Steam Overlay has been activated");
  53.             MusicController.Access.OverlayActive = true;
  54.         }
  55.         else
  56.         {
  57.             Debug.Log("Steam Overlay has been closed");
  58.             MusicController.Access.OverlayActive = false;
  59.         }
  60.     }
  61.  
  62.     public void SubmitFeedback()
  63.     {
  64.         SteamFriends.ActivateGameOverlayToWebPage("http://feedback.officialdiamondgame.com");
  65.  
  66.         if (Application.isEditor)
  67.             Application.OpenURL("http://feedback.officialdiamondgame.com");
  68.     }
  69.  
  70.     public void PlayLevel(LevelData Level, int Camp_Section = 0, int Camp_Level = 0, bool InternalTest = false)
  71.     {
  72.         Campaign_Section = Camp_Section;
  73.         Campaign_Level = Camp_Level;
  74.         IsCampaign = (Campaign_Section != 0 && Campaign_Level != 0);
  75.  
  76.         LoadedLevel = Level;
  77.         LoadedLevel.OriginalSteamID = Level.OriginalSteamID;
  78.         LoadedLevel.LevelDescription = Level.LevelDescription;
  79.         LoadedLevel.m_PublishedFileId = Level.m_PublishedFileId;
  80.         LoadedLevel.EligibleForLeaderboard = Level.EligibleForLeaderboard;
  81.  
  82.         PlayGlobals.Access.IsPackage = false;
  83.  
  84.         try
  85.         {
  86.             T_LoadScene.Access.LoadLevel("Player");
  87.         }
  88.         catch
  89.         {
  90.             Debug.Log("Unable to load async. Force loading.");
  91.             if (!InternalTest)
  92.                 UnityEngine.SceneManagement.SceneManager.LoadScene("Player");
  93.             PlayGlobals.Access.Restarting = true;
  94.         }
  95.  
  96.         PlayGlobals.Access.LoadData = true;
  97.     }
  98.  
  99.     /// <summary>
  100.     /// Play a package.
  101.     /// </summary>
  102.     /// <param name="Package">Package being loaded.</param>
  103.     /// <param name="Index">Level in package to be loaded.</param>
  104.     public void PlayPackage(LevelPackage Package, int Index = 0)
  105.     {
  106.         LoadedPackage = Package;
  107.         LoadedPackage.OriginalSteamID = Package.OriginalSteamID;
  108.         LoadedPackage.PackageDescription = Package.PackageDescription;
  109.         LoadedPackage.m_PublishedFileId = Package.m_PublishedFileId;
  110.  
  111.         LoadedLevel = Package.PackageLevels[Index];
  112.         LoadedLevel.OriginalSteamID = Package.PackageLevels[Index].OriginalSteamID;
  113.         LoadedLevel.LevelDescription = Package.PackageLevels[Index].LevelDescription;
  114.         LoadedLevel.m_PublishedFileId = Package.PackageLevels[Index].m_PublishedFileId;
  115.  
  116.         PackageIndex = 0;
  117.         PackageMax = Package.PackageLevels.Count - 1;
  118.         PackageLevelsFinished.Clear();
  119.         foreach(LevelData Level in Package.PackageLevels)
  120.         {
  121.             PackageLevelsFinished.Add(false);
  122.         }
  123.  
  124.         PlayGlobals.Access.IsPackage = true;
  125.         T_LoadScene.Access.LoadLevel("Player");
  126.         PlayGlobals.Access.LoadData = true;
  127.     }
  128.  
  129.     public void CampaignLevelSwitch(LevelData Level, int Camp_Section = 0, int Camp_Level = 0)
  130.     {
  131.         Campaign_Section = Camp_Section;
  132.         Campaign_Level = Camp_Level;
  133.         IsCampaign = (Campaign_Section != 0 && Campaign_Level != 0);
  134.  
  135.         LoadedLevel = Level;
  136.         LoadedLevel.OriginalSteamID = Level.OriginalSteamID;
  137.         LoadedLevel.LevelDescription = Level.LevelDescription;
  138.         LoadedLevel.m_PublishedFileId = Level.m_PublishedFileId;
  139.         LoadedLevel.EligibleForLeaderboard = Level.EligibleForLeaderboard;
  140.  
  141.         PlayGlobals.Access.IsPackage = false;
  142.         PlayGlobals.Access.ReloadLevel();
  143.     }
  144.  
  145.     public void SwitchLevel(LevelData Level, int Index)
  146.     {
  147.         LoadedLevel = Level;
  148.         LoadedLevel.OriginalSteamID = Level.OriginalSteamID;
  149.         LoadedLevel.LevelDescription = Level.LevelDescription;
  150.         LoadedLevel.m_PublishedFileId = Level.m_PublishedFileId;
  151.  
  152.         PackageIndex = Index;
  153.         PlayGlobals.Access.IsPackage = true;
  154.         PlayGlobals.Access.ReloadLevel();
  155.     }
  156.  
  157.     ControllerHandle_t[] sHandle;
  158.     public int Num;
  159.  
  160.     void Start()
  161.     {
  162.         if (SteamManager.Initialized)
  163.         {
  164.             SteamInfo.Initialized = SteamManager.Initialized;
  165.             SteamInfo.UserName = SteamFriends.GetPersonaName();
  166.             CSteamID m_Friend = SteamUser.GetSteamID();
  167.             SteamInfo.UserCID = m_Friend;
  168.             SteamInfo.UserID = Convert.ToString( m_Friend );
  169.             #region Load Avatar
  170.             int FriendAvatar = SteamFriends.GetLargeFriendAvatar(m_Friend);
  171.             AvatarLoaded = (FriendAvatar > 0);
  172.             m_GameOverlayActivated = Callback<GameOverlayActivated_t>.Create(OnGameOverlayActivated);
  173.             m_AvatarImageLoaded = Callback<AvatarImageLoaded_t>.Create(OnAvatarImageLoaded);
  174.             uint ImageWidth;
  175.             uint ImageHeight;
  176.             bool ret = SteamUtils.GetImageSize(FriendAvatar, out ImageWidth, out ImageHeight);
  177.  
  178.             if (FriendAvatar == -1)
  179.                 Debug.Log("Large avatar is still loading...");
  180.                        
  181.             if (ret && ImageWidth > 0 && ImageHeight > 0)
  182.             {
  183.                 byte[] Image = new byte[ImageWidth * ImageHeight * 4];
  184.  
  185.                 ret = SteamUtils.GetImageRGBA(FriendAvatar, Image, (int)(ImageWidth * ImageHeight * 4));
  186.                 if (ret)
  187.                 {
  188.                     SteamInfo.UserAvatar = new Texture2D((int)ImageWidth, (int)ImageHeight, TextureFormat.RGBA32, false, true);
  189.                     SteamInfo.UserAvatar.LoadRawTextureData(Image);
  190.                     SteamInfo.UserAvatar.Apply();
  191.                 }
  192.             }
  193.             #endregion
  194.             //Load();
  195.         }
  196.     }
  197.  
  198.     void OnAvatarImageLoaded(AvatarImageLoaded_t pCallback)
  199.     {
  200.         Debug.Log("[" + AvatarImageLoaded_t.k_iCallback + " - AvatarImageLoaded] - " + pCallback.m_steamID + " -- " + pCallback.m_iImage + " -- " + pCallback.m_iWide + " -- " + pCallback.m_iTall);
  201.         int FriendAvatar = SteamFriends.GetLargeFriendAvatar(SteamInfo.UserCID);
  202.         m_GameOverlayActivated = Callback<GameOverlayActivated_t>.Create(OnGameOverlayActivated);
  203.         m_AvatarImageLoaded = Callback<AvatarImageLoaded_t>.Create(OnAvatarImageLoaded);
  204.         uint ImageWidth;
  205.         uint ImageHeight;
  206.         bool ret = SteamUtils.GetImageSize(FriendAvatar, out ImageWidth, out ImageHeight);
  207.  
  208.         if (ret && ImageWidth > 0 && ImageHeight > 0)
  209.         {
  210.             byte[] Image = new byte[ImageWidth * ImageHeight * 4];
  211.  
  212.             ret = SteamUtils.GetImageRGBA(FriendAvatar, Image, (int)(ImageWidth * ImageHeight * 4));
  213.             if (ret)
  214.             {
  215.                 SteamInfo.UserAvatar = new Texture2D((int)ImageWidth, (int)ImageHeight, TextureFormat.RGBA32, false, true);
  216.                 SteamInfo.UserAvatar.LoadRawTextureData(Image);
  217.                 SteamInfo.UserAvatar.Apply();
  218.             }
  219.         }
  220.         AvatarLoaded = true;
  221.     }
  222.  
  223.    
  224.  
  225.     void OnEnable ()
  226.     {
  227.         if (Access == null)
  228.         {
  229.             DontDestroyOnLoad(gameObject);
  230.             Access = this;
  231.         }        
  232.             else if (Access != this)
  233.         {
  234.             Destroy(gameObject);
  235.         }
  236.     }
  237.  
  238.     void Update ()
  239.     {
  240.         SteamAPI.RunCallbacks();
  241.  
  242.         #region Accessing Debug
  243.         if (Input.GetKeyDown(KeyCode.BackQuote))
  244.             DebugAccess++;
  245.         if (!Input.GetKey(KeyCode.BackQuote) && Input.anyKey)
  246.         {
  247.             DebugAccess = 0;
  248.         }
  249.         if (DebugAccess >= 3)
  250.         {
  251.             DebugAccess = 0;
  252.             DisplayDebug = !DisplayDebug;
  253.         }
  254.         #endregion
  255.         #region Obsolete FileTesting
  256.         //if (save)
  257.         //{
  258.         //    save = false;
  259.         //    Save();
  260.         //}
  261.         //if (load)
  262.         //{
  263.         //    load = false;
  264.         //    Load();
  265.         //}
  266.         //if (delete)
  267.         //{
  268.         //    delete = false;
  269.         //    Delete();
  270.         //}
  271.         #endregion
  272.        
  273.     }
  274.  
  275.     #region Obsolete Save System
  276.     //public void Delete()
  277.     //{
  278.     //    if (File.Exists(Application.persistentDataPath +"/" + SteamInfo.UserID + ".dat"))
  279.     //    {
  280.     //        File.Delete(Application.persistentDataPath +"/" + SteamInfo.UserID + ".dat");
  281.     //        Debug.Log("Deleted file at " + Application.persistentDataPath);
  282.     //        AchievementUnlocked("Savage");            
  283.     //    }
  284.     //    else
  285.     //        Debug.Log("File not found.");
  286.     //}
  287.  
  288.     //public void Save(bool NewFile = false)
  289.     //{
  290.     //    if (SteamInfo.Initialized)
  291.     //    {
  292.     //        BinaryFormatter formatter = new BinaryFormatter();
  293.     //        FileStream file = File.Create(Application.persistentDataPath + "/" + SteamInfo.UserID + ".dat");
  294.  
  295.     //        UserData data = new UserData();
  296.     //        if (!NewFile)
  297.     //        {
  298.     //            //data.cubecount = PowerCubeCount;
  299.     //        }
  300.     //        formatter.Serialize(file, data);
  301.     //        file.Close();
  302.     //        Debug.Log("Finished saving to " + Application.persistentDataPath);
  303.     //    }
  304.     //    else
  305.     //        Debug.Log("You must be logged into Steam to save.");
  306.     //}
  307.  
  308.     //public void Load()
  309.     //{
  310.     //    if (SteamInfo.Initialized)
  311.     //    {
  312.     //        if (File.Exists(Application.persistentDataPath + "/" + SteamInfo.UserID + ".dat"))
  313.     //        {
  314.     //            BinaryFormatter formatter = new BinaryFormatter();
  315.     //            FileStream file = File.Open(Application.persistentDataPath + "/" + SteamInfo.UserID + ".dat", FileMode.Open);
  316.     //            UserData data = (UserData)formatter.Deserialize(file);
  317.     //            file.Close();
  318.  
  319.     //            PowerCubeCount = data.cubecount;
  320.     //            Debug.Log("Load successful for: " + SteamInfo.UserName);
  321.     //        }
  322.     //        else
  323.     //        {
  324.     //            Debug.Log("No save file found for: " + SteamInfo.UserName);
  325.     //        }
  326.     //    }
  327.     //    else
  328.     //    {
  329.     //        Debug.Log("You must be logged into Steam to load.");
  330.     //    }
  331.     //}
  332.     #endregion
  333.  
  334.     public void DebugInfo(string Display)
  335.     {
  336.         DebugText = Display;
  337.     }
  338.  
  339.     public void ClearStats(bool NotAchievements = false)
  340.     {
  341.         SteamUserStats.ResetAllStats(!NotAchievements);
  342.         if (NotAchievements)
  343.             Debug.Log("You cleared all stats.");
  344.         else
  345.             Debug.Log("You've cleared all stats and achievements.");
  346.     }
  347.  
  348.     public void AchievementUnlocked(string AchievementName)
  349.     {
  350.         SteamUserStats.SetAchievement(AchievementName);
  351.         Debug.Log("Achievement Unlocked: " + AchievementName);
  352.         SteamUserStats.StoreStats();
  353.     }
  354. }
  355.  
  356. [Serializable]
  357. class UserData
  358. {
  359.     public int cubecount;
  360. }
  361.  
  362. [System.Serializable]
  363. public class SteamInformation
  364. {
  365.     public bool Initialized;
  366.     public string UserName;
  367.     public CSteamID UserCID;
  368.     public string UserID;
  369.     public Texture2D UserAvatar;
  370. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement