Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using SimpleJSON;
- using UnityEngine;
- using System.Collections;
- using MeshVR;
- public class AutoSaver : MVRScript {
- // private float _autosaveRate = 60f * 5f; // default save every five minutes
- private float _autosaveRate = 5f;
- private UIDynamicSlider _rateSlider;
- private bool _developmentMode = false;
- // How often to trigger autosave.
- private JSONStorableFloat _autosaveRateJSON;
- // If autosave rate should be considered as seconds or minutes
- private JSONStorableBool _secondsInsteadOfMinutesJSON;
- // Which directory to use for autosaving
- private JSONStorableString _autoSavePathJSON;
- // If autosave is currently active
- private JSONStorableBool _autoSaveActiveJSON;
- private bool _autoSaveActive;
- // If we should capture the screenshot automatically
- private JSONStorableBool _autoCaptureScreenshot;
- // If "Auto Capture Screenshot" is enabled, this toggle decides if to reuse the last position of the screenshot camera
- private JSONStorableBool _reuseLastScreenshotPosition;
- public override void Init() {
- // CreateTextField(new JSONStorableString("Autosave Directory", "Autosave Directory"));
- var defaultScenePath = SuperController.singleton.savesDir + "autosaves\\";
- MVR.FileManagementSecure.FileManagerSecure.CreateDirectory(defaultScenePath);
- StringTextbox(ref _autoSavePathJSON, "Path on disk to autosave to", defaultScenePath, false);
- var autoSaveDirectoryBtn = CreateButton("Select Autosave Directory", false);
- autoSaveDirectoryBtn.button.onClick.AddListener(chooseDirectory);
- setupAutosaveRate("minutes");
- CreateSpacer();
- _secondsInsteadOfMinutesJSON = new JSONStorableBool("Use seconds instead of minutes", false, toggleSecondsInsteadOfMinutes);
- setupJSONBool(_secondsInsteadOfMinutesJSON);
- _autoSaveActiveJSON = new JSONStorableBool("Autosaving Enabled", false, toggleAutosave);
- setupJSONBool(_autoSaveActiveJSON);
- _autoCaptureScreenshot = new JSONStorableBool("Auto Capture Screenshot", true, (bool newVal) => {
- _autoCaptureScreenshot.SetVal(newVal);
- if (newVal) {
- log("Capturing screenshots automatically from now on");
- _reuseLastScreenshotPosition.toggle.interactable = true;
- } else {
- log("Will no longer capture screenshots automatically");
- _reuseLastScreenshotPosition.SetVal(false);
- _reuseLastScreenshotPosition.toggle.interactable = false;
- }
- });
- setupJSONBool(_autoCaptureScreenshot);
- _reuseLastScreenshotPosition = new JSONStorableBool("Reuse last screenshot position", false, (bool newVal) => {
- _reuseLastScreenshotPosition.SetVal(newVal);
- if (newVal) {
- log("Will always reuse last capture position from now on");
- } else {
- log("Will no longer reuse last capture position automatically");
- }
- });
- setupJSONBool(_reuseLastScreenshotPosition);
- if (_developmentMode) {
- var testSaveBtn = CreateButton("Test Save JSON", true);
- testSaveBtn.button.onClick.AddListener(() => { doSaveJSON(getTimestamp()); });
- var testScreenshotBtn = CreateButton("Test Screenshot", true);
- testScreenshotBtn.button.onClick.AddListener(() => { doSaveScreenshot(getTimestamp()); });
- var testBtn = CreateButton("Test Save JSON & Screenshot", true);
- testBtn.button.onClick.AddListener(doSave);
- } else {
- var text = @"<color=#000000><size=40><b>AutoSaver v0.0.1</b></size>
- This plugin allows you to configure VaM to automatically save your scenes on selected intervals, handy to recover from mistakes as there is no undo.</color>
- <color=#0000FF><size=30><b>Select Autosave Directory</b></size></color>
- Brings up the Save Scene dialog so you can choose where to autosave your scene. AutoSaver will automatically assign a scene name for you.
- <color=#0000FF><size=30><b>Autosave Rate</b></size></color>
- How often AutoSaver will try to do the saving. Can be switching between minutes and seconds.
- <color=#0000FF><size=30><b>Autosaving Enabled</b></size></color>
- Simple, should we autosave or not?
- <color=#0000FF><size=30><b>Auto Capture Screenshot</b></size></color>
- When enabled, AutoSaver will automatically capture the screenshot for you. When disabled, AutoSaver will wait for you to confirm the angle/position of the screenshot before saving.
- <color=#0000FF><size=30><b>Reuse Last Screenshot Position</b></size></color>
- When enabled, AutoSaver will try to reuse the position of the last screenshot you did. If disabled, the angle/position will be your current PoV. If Auto Capture Screenshot is disabled, this setting gets disabled too.
- ";
- JSONStorableString storable = new JSONStorableString("Info", text);
- UIDynamic textfield = CreateTextField(storable, true);
- textfield.height = 1200f;
- }
- }
- public void setupJSONBool(JSONStorableBool storable) {
- RegisterBool(storable);
- CreateToggle(storable);
- }
- public void log(string str) {
- SuperController.LogMessage($"{nameof(AutoSaver)} {str}");
- }
- public void OnEnable() {
- try {
- if (_autoSaveActive) {
- StartCoroutine("AutosaveInterval");
- } else {
- StopCoroutine("AutosaveInterval");
- }
- } catch (Exception e) {
- SuperController.LogError($"{nameof(AutoSaver)}.{nameof(OnEnable)}: {e}");
- }
- }
- private void StringTextbox(ref JSONStorableString output, string name, string start, bool rhs) {
- output = new JSONStorableString(name, start);
- RegisterString(output);
- var textfield = CreateTextField(output, rhs);
- var input = textfield.gameObject.AddComponent<UnityEngine.UI.InputField>();
- input.textComponent = textfield.UItext;
- output.inputField = input;
- }
- IEnumerator AutosaveInterval() {
- for(;;) {
- float saveRate;
- if (_secondsInsteadOfMinutesJSON.val) {
- saveRate = _autosaveRateJSON.val;
- } else {
- saveRate = _autosaveRateJSON.val * 60f;
- }
- yield return new WaitForSeconds(saveRate);
- doSave();
- }
- }
- private string getFilePath(string timestamp, string suffix) {
- return _autoSavePathJSON.val + timestamp + "." + suffix;
- }
- private void doSaveJSON(string timestamp) {
- var saveName = getFilePath(timestamp, "json");
- log($"{nameof(AutoSaver)} Saving to {saveName}");
- JSONClass saveJSON = SuperController.singleton.GetSaveJSON(null, true, true);
- SaveJSON(saveJSON, saveName);
- }
- private void doSaveScreenshot(string timestamp) {
- var saveName = getFilePath(timestamp, "jpg");
- SuperController.singleton.DoSaveScreenshot(saveName);
- // If we don't wait any time, the screenshot camera won't have time to move to new position it
- // seems, so we can reuse the last position if wanted.
- if (_reuseLastScreenshotPosition.val) {
- // log("Reusing last position");
- triggerLeftSelect();
- } else {
- // log("New capture position");
- if (_autoCaptureScreenshot.val) {
- Invoke("triggerLeftSelect", 0.01f);
- }
- }
- }
- private void triggerLeftSelect() {
- SuperController.singleton.SetLeftSelect();
- }
- private string getTimestamp() {
- return DateTime.Now.ToFileTime().ToString();
- }
- private void doSave() {
- var timestamp = getTimestamp();
- doSaveJSON(timestamp);
- doSaveScreenshot(timestamp);
- }
- private void toggleAutosave(bool val) {
- _autoSaveActiveJSON.SetVal(val);
- _autoSaveActive = val;
- if (val) {
- log("Autosaving active");
- StartCoroutine("AutosaveInterval");
- } else {
- log("Autosaving disabled");
- StopCoroutine("AutosaveInterval");
- }
- }
- private void toggleSecondsInsteadOfMinutes(bool val) {
- _secondsInsteadOfMinutesJSON.SetVal(val);
- var dynamicSlider = _rateSlider.GetComponent<UIDynamicSlider>();
- if (val) {
- log("Using Seconds instead of Minutes for Autosave Rate");
- dynamicSlider.labelText.text = "Autosave Rate (Seconds)";
- _autosaveRateJSON.SetVal(60f);
- _autosaveRate = 60f;
- StopCoroutine("AutosaveInterval");
- StartCoroutine("AutosaveInterval");
- } else {
- log("Using Minutes instead of Seconds for Autosave Rate");
- dynamicSlider.labelText.text = "Autosave Rate (Minutes)";
- _autosaveRateJSON.SetVal(1f);
- _autosaveRate = 1f;
- StopCoroutine("AutosaveInterval");
- StartCoroutine("AutosaveInterval");
- }
- }
- private void chooseDirectory() {
- SuperController.singleton.SaveSceneDialog((filePath) =>
- {
- if (String.IsNullOrEmpty(filePath))
- {
- return;
- }
- string directoryPath = filePath.Remove(filePath.LastIndexOf('\\')) + "\\";
- log(directoryPath + " selected for autosaving");
- _autoSavePathJSON.SetVal(directoryPath);
- });
- }
- private void setupAutosaveRate(string intervalType) {
- _autosaveRateJSON = new JSONStorableFloat("Autosave Rate ("+intervalType+")", 5f, (float newVal) => {
- double rounded = Math.Round(newVal);
- // if (_secondsInsteadOfMinutesJSON.val) {
- // log("Setting autosave rate to " + rounded.ToString() + " seconds");
- // } else {
- // log("Setting autosave rate to " + rounded.ToString() + " minutes");
- // }
- _autosaveRateJSON.val = (float) rounded;
- StopCoroutine("AutosaveInterval");
- StartCoroutine("AutosaveInterval");
- }, 1f, 60f, true, true);
- // DeregisterFloat(_autosaveRateJSON);
- RegisterFloat(_autosaveRateJSON);
- // RemoveSlider(_autosaveRateJSON);
- _rateSlider = CreateSlider(_autosaveRateJSON);
- }
- public void OnDisable() {
- try {
- StopCoroutine("AutosaveInterval");
- } catch (Exception e) {
- SuperController.LogError($"{nameof(AutoSaver)}.{nameof(OnDisable)}: {e}");
- }
- }
- public void OnDestroy() {
- try {
- StopCoroutine("AutosaveInterval");
- } catch (Exception e) {
- SuperController.LogError($"{nameof(AutoSaver)}.{nameof(OnDestroy)}: {e}");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement