/// <summary> /// Deserializes a given JSON file into the given type. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="fileName"></param> /// <returns></returns> public static T DeserializeFromFile <T>(string fileName, PathType pathType = PathType.PersistentDataPath, bool useEncryption = false) { // Need to handle resource folder files a bit differently. if (pathType == PathType.ResourcesFolder) { TextAsset raw = Resources.Load(fileName) as TextAsset; string rawString = null; if (raw != null) { rawString = raw.ToString(); } if (string.IsNullOrEmpty(rawString)) { return(GetNullOrEmptyOfType <T>()); } else { return(DeserializeString <T>(rawString, fileName, useEncryption)); } } string path = GetPathForType(pathType); string serializedString = ""; if ((pathType == PathType.PersistentDataPath) && Application.isPlaying) { TBLogging.LogWarning("Deserializing files from the persistent data path at runtime will not work on all platforms! Use the Async subclass methods instead."); } serializedString = LoadStringFromStorageDefault(path + fileName); return(DeserializeString <T>(serializedString, fileName, useEncryption)); }
/// <summary> /// Loads a button set from JSON by its filename. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="buttons"></param> /// <param name="fileName"></param> /// <returns></returns> public static List <ButtonDef <T> > LoadButtonDefs <T>(List <ButtonDef <T> > buttons, string fileName) { if (Application.isPlaying) { TBLogging.LogMessage("Loading button maps from " + TBSettings.settingsFolder + fileName + "..."); } SerializedButtonDef[] serializedButtonDefs = TBDataManager.FromJsonWrapper <SerializedButtonDef>(TBDataManager.DeserializeFromFile <TBDataManager.Wrapper <TBInput.SerializedButtonDef> >(TBSettings.settingsFolder + fileName, TBDataManager.PathType.ResourcesFolder)); if (serializedButtonDefs == null) { if (Application.isPlaying) { TBLogging.LogWarning("Serialized button maps for " + fileName + " were invalid (empty file). Using defaults."); } return(buttons); } if (serializedButtonDefs.Length != buttons.Count) { TBLogging.LogError("Serialized button maps for " + fileName + " exist, but are invalid! (Input count mismatch). You need to clear the JSON file or manually fix it."); } for (int i = 0; i < buttons.Count; i++) { buttons[i].virtualButtons = serializedButtonDefs[i].virtualButtons; } if (Application.isPlaying) { TBLogging.LogMessage("Done! Found maps for " + buttons.Count + " buttons in " + TBSettings.settingsFolder + fileName + "."); } return(buttons); }
private void ToggleVRMode(bool on) // TODO { if (on) { if (!UsingVRMode()) { if (!_initialized) { } if (Events.OnVRModeEnabled != null) { Events.OnVRModeEnabled(true); } } else { TBLogging.LogWarning("Tried to enter VR mode, but we were already using VR Mode."); } } else { if (UsingVRMode()) { TBLogging.LogWarning("Tried to exit VR mode, but we were already out of VR Mode."); } else { if (Events.OnVRModeEnabled != null) { Events.OnVRModeEnabled(false); } } } }
public static bool HasDataFile(string fileName, PathType pathType = PathType.PersistentDataPath) { if (Application.isPlaying) { TBLogging.LogWarning("TBDataManager.HasDataFile is not safe to call on all platforms! Use the Async subclass."); } return(File.Exists(GetPathForType(pathType) + fileName)); }
public bool GetState(TBXInputButton button, GamepadReading gamepadState) { switch (button) { case TBXInputButton.ButtonA: return(GamepadButtons.A == (gamepadState.Buttons & GamepadButtons.A)); case TBXInputButton.ButtonB: return(GamepadButtons.B == (gamepadState.Buttons & GamepadButtons.B)); case TBXInputButton.ButtonX: return(GamepadButtons.X == (gamepadState.Buttons & GamepadButtons.X)); case TBXInputButton.ButtonY: return(GamepadButtons.Y == (gamepadState.Buttons & GamepadButtons.Y)); case TBXInputButton.LeftStick: return(GamepadButtons.LeftThumbstick == (gamepadState.Buttons & GamepadButtons.LeftThumbstick)); case TBXInputButton.RightStick: return(GamepadButtons.RightThumbstick == (gamepadState.Buttons & GamepadButtons.RightThumbstick)); case TBXInputButton.DpadUp: return(GamepadButtons.DPadUp == (gamepadState.Buttons & GamepadButtons.DPadUp)); case TBXInputButton.DpadRight: return(GamepadButtons.DPadRight == (gamepadState.Buttons & GamepadButtons.DPadRight)); case TBXInputButton.DpadDown: return(GamepadButtons.DPadDown == (gamepadState.Buttons & GamepadButtons.DPadDown)); case TBXInputButton.DpadLeft: return(GamepadButtons.DPadLeft == (gamepadState.Buttons & GamepadButtons.DPadLeft)); case TBXInputButton.Back: return(GamepadButtons.View == (gamepadState.Buttons & GamepadButtons.View)); case TBXInputButton.Start: return(GamepadButtons.Menu == (gamepadState.Buttons & GamepadButtons.Menu)); case TBXInputButton.RightBumper: return(GamepadButtons.RightShoulder == (gamepadState.Buttons & GamepadButtons.RightShoulder)); case TBXInputButton.LeftBumper: return(GamepadButtons.LeftShoulder == (gamepadState.Buttons & GamepadButtons.LeftShoulder)); case TBXInputButton.LeftTrigger: return(gamepadState.LeftTrigger > 0); case TBXInputButton.RightTrigger: return(gamepadState.RightTrigger > 0); default: TBLogging.LogWarning("Could not find requested Windows Input type."); return(false); } }
public static VRHeadset GetActiveHeadset() { if (_settingsBase != null) { return(_settingsBase.GetActiveHeadset()); } else { TBLogging.LogWarning("Attempted to read active headset before it was initialized."); return(VRHeadset.None); } }
public uint GetSteamVRControllerID(TBInput.Controller controller) { switch (controller) { case TBInput.Controller.RHandController: return(TBSteamVRDeviceManager.instance.GetRightControllerID()); case TBInput.Controller.LHandController: return(TBSteamVRDeviceManager.instance.GetLeftControllerID()); } TBLogging.LogWarning("Requested device index for controller type " + controller + " that is not supported by Steam VR. Assuming device ID is 0"); return(0); }
public virtual VRController GetControllerModel(TBInput.Controller controller) { if (controller == TBInput.Controller.Active) { controller = GetActiveController(); } if (GetControllerForType(controller) != null) { return(GetControllerForType(controller).GetModel()); } TBLogging.LogWarning("Attempted to get controller model for " + controller + " but controller is undefined!"); return(VRController.None); }
public virtual string GetControllerName(TBInput.Controller controller) { if (controller == TBInput.Controller.Active) { controller = GetActiveController(); } if (GetControllerForType(controller) != null) { return(GetControllerForType(controller).GetName()); } TBLogging.LogWarning("Attempted to read controller name, but controller is undefined!"); return("Undefined"); }
public static int GetNumAchievements() { if (_service == VRService.None) { TBLogging.LogWarning("Tried to get achievement list, but no service is initialized.", null, "via TButt.TBServiceManager"); return(0); } if (_achievementList == null) { return(0); } return(_achievementList.Count); }
public override Dictionary <string, bool> GetAchievementDictionary() { Dictionary <string, bool> achievements = new Dictionary <string, bool>(); Achievements.GetAllDefinitions().OnComplete((Message <AchievementDefinitionList> msg) => { if (msg.IsError) { TBLogging.LogWarning("Failed to get Oculus achievement dictionary from server"); } else { TBLogging.LogMessage("Received Oculus achievement dictionary from server with " + msg.Data.Count + " entries"); } for (int i = 0; i < msg.Data.Count; i++) { achievements.Add(msg.Data[i].Name, false); } Achievements.GetAllProgress().OnComplete((Message <AchievementProgressList> msg2) => { if (msg2.IsError) { TBLogging.LogWarning("Failed to get Oculus achievement progress from server"); } else { TBLogging.LogMessage("Received Oculus achievement progress dictionary from server with " + msg.Data.Count + " entries"); } for (int j = 0; j < msg2.Data.Count; j++) { bool unlocked = false; if (achievements.TryGetValue(msg2.Data[j].Name, out unlocked)) { achievements[msg2.Data[j].Name] = unlocked; TBLogging.LogMessage("Achievement " + msg2.Data[j].Name + " is unlocked."); } } TBServiceManager.SetAchievementList(achievements); }); }); return(achievements); }
/// <summary> /// Unlocks an achievement if it has not already been unlocked. /// </summary> /// <param name="token"></param> public static void UnlockAchievement(string token) { if (_service == VRService.None) { TBLogging.LogWarning("Tried to unlock achievement " + token + " but no service is initialized.", null, "via TButt.TBServiceManager"); return; } if (!IsAchievementUnlocked(token)) { _activeService.UnlockAchievement(token); if (Events.OnAchievementUnlocked != null) { Events.OnAchievementUnlocked(token); } } }
public static void ResetTracking() { switch (TBCore.GetActivePlatform()) { case VRPlatform.None: TBLogging.LogWarning("Cannot handle recenter event when no HMD is loaded."); break; default: UnityEngine.XR.InputTracking.Recenter(); break; } TBLogging.LogMessage("Reset tracking at Core level."); if (Events.OnTrackingReset != null) { Events.OnTrackingReset(); } }
/// <summary> /// Serializes an object to JSON and writes it to a file. /// </summary> /// <param name="obj"></param> /// <param name="fileName"></param> /// <param name="pathType"></param> /// <param name="useEncryption"></param> public static void SerializeObjectToFile(System.Object obj, string fileName, PathType pathType = PathType.PersistentDataPath, bool useEncryption = false) { if (obj == null) { Debug.LogError("Attempted to serialize an object, but the object was null."); return; } string serializedString = SerializeString(obj, useEncryption); string path = GetPathForType(pathType); if ((pathType == PathType.PersistentDataPath) && Application.isPlaying) { TBLogging.LogWarning("Sserializing files from the persistent data path at runtime will not work on all platforms! Use the Async subclass methods instead."); } SaveStringToStorageDefault(serializedString, path + fileName); }
/// <summary> /// Checks if an achievement has been unlocked. /// </summary> /// <param name="token"></param> /// <returns></returns> public static bool IsAchievementUnlocked(string token) { if (_service == VRService.None) { TBLogging.LogWarning("Tried to check status of achievement " + token + " but no service is initialized.", null, "via TButt.TBServiceManager"); } if (_achievementList == null) { TBLogging.LogWarning("Tried to check unlock status of achievement " + token + " but the dictionary wasn't initialized!", null, "via TButt.TBServiceManager"); return(true); } bool unlocked; if (_achievementList.TryGetValue(token, out unlocked)) { return(unlocked); } else { TBLogging.LogWarning("Tried to check unlock status of achievement " + token + " but it was not found in the achievement dictionary!", null, "via TButt.TBServiceManager"); return(true); // Tell the game that the achievement is already unlocked so that it gets skipped from the SDK calls. } }
public static int GetNumAchievements(bool unlocked) { if (_service == VRService.None) { TBLogging.LogWarning("Tried to get achievement list, but no service is initialized.", null, "via TButt.TBServiceManager"); return(0); } if (_achievementList == null) { return(0); } int num = 0; foreach (KeyValuePair <string, bool> entry in _achievementList) { if (entry.Value == unlocked) { num++; } } return(num); }
public ButtonState GetState(TBXInputButton button, GamePadState gamepadState) { switch (button) { case TBXInputButton.ButtonA: return(gamepadState.Buttons.A); case TBXInputButton.ButtonB: return(gamepadState.Buttons.B); case TBXInputButton.ButtonX: return(gamepadState.Buttons.X); case TBXInputButton.ButtonY: return(gamepadState.Buttons.Y); case TBXInputButton.LeftStick: return(gamepadState.Buttons.LeftStick); case TBXInputButton.RightStick: return(gamepadState.Buttons.RightStick); case TBXInputButton.DpadUp: return(gamepadState.DPad.Up); case TBXInputButton.DpadRight: return(gamepadState.DPad.Right); case TBXInputButton.DpadDown: return(gamepadState.DPad.Down); case TBXInputButton.DpadLeft: return(gamepadState.DPad.Left); case TBXInputButton.Back: return(gamepadState.Buttons.Back); case TBXInputButton.Guide: return(gamepadState.Buttons.Guide); case TBXInputButton.Start: return(gamepadState.Buttons.Start); case TBXInputButton.RightBumper: return(gamepadState.Buttons.RightShoulder); case TBXInputButton.LeftBumper: return(gamepadState.Buttons.LeftShoulder); case TBXInputButton.LeftTrigger: if (gamepadState.Triggers.Left > 0) { return(ButtonState.Pressed); } else { return(ButtonState.Released); } case TBXInputButton.RightTrigger: if (gamepadState.Triggers.Right > 0) { return(ButtonState.Pressed); } else { return(ButtonState.Released); } default: TBLogging.LogWarning("Could not find requested XInput type."); return(ButtonState.Released); } }
void InteractionManager_SourceLost(InteractionSourceLostEventArgs state) { TBLogging.LogWarning("Motion controller disconnected"); RefreshSource(state.state); }
public static void CheckoutAndSaveJSONFile(string fileName, object obj, TBDataManager.PathType pathType = TBDataManager.PathType.PersistentDataPath, bool skipVersioning = false, bool isString = false) { bool addToVersionControl = false; if (Provider.isActive) // Is version control enabled in the project? { if (Provider.hasCheckoutSupport) // Does the project's version control settings support auto-checkout? { Asset targetAsset = Provider.GetAssetByPath(fileName); if (targetAsset != null) // Does the thing we want to checkout exist? { if (!Provider.IsOpenForEdit(targetAsset)) // Is it already checked out? { Task checkoutTask = Provider.Checkout(targetAsset, CheckoutMode.Both); checkoutTask.Wait(); // Wait for checkout to finish } } else { addToVersionControl = true; } } } if (!Directory.Exists("Assets/Resources/")) { Directory.CreateDirectory("Assets/Resources/"); } if (!Directory.Exists("Assets/Resources/" + TBSettings.settingsFolder)) { Directory.CreateDirectory("Assets/Resources/" + TBSettings.settingsFolder); } if (!isString) { TBDataManager.SerializeObjectToFile(obj, fileName, pathType); // Save the file } else { TBDataManager.WriteStringToFile((string)obj, fileName, pathType); } AssetDatabase.Refresh(); if (skipVersioning) { return; } if (addToVersionControl) { Asset asset = Provider.GetAssetByPath(fileName); if (asset == null) { TBLogging.LogWarning("A new file was created but could not be automatically added to version control: " + fileName); return; } if (Provider.AddIsValid(new AssetList() { asset })) { Task addTask = Provider.Add(asset, false); addTask.Wait(); } else { TBLogging.LogWarning("A new file was created but could not be automatically added to version control: " + fileName); } } }