Tanoda
pb_SaveSceneButton.cs
Go to the documentation of this file.
1using UnityEngine;
2using System.Collections;
3using System.Runtime.InteropServices;
4using System.Text;
5using GILES.Interface;
6
7namespace GILES
8{
13 {
15
17
18 public override string tooltip { get { return /*Macro.T*/(""); }
19 }
20
24 public void OpenSavePanel()
25 {
26 //var content = Encoding.Default.GetBytes(stageManager.HasStages() ? stageManager.GetStagesString() : "[" + pb_Scene.SaveLevel() + "]");
27 //DownloadFile(content, content.Length, "level.json");
28 //return;
29
30#if UNITY_WEBGL
31 LoadingManager.instance.SetSyncing();
32 LoadingManager.instance.ShowWindow();
33 StartCoroutine(WebGLSave());
34 return;
35#endif
36
37 pb_FileDialog dlog = GameObject.Instantiate(dialogPrefab);
38 dlog.SetDirectory(System.IO.Directory.GetCurrentDirectory());
39 dlog.isFileBrowser = true;
40 dlog.filePattern = "*.json|*.env";
41 dlog.AddOnSaveListener(OnSave);
42
43 pb_ModalWindow.SetContent(dlog.gameObject);
44 pb_ModalWindow.SetTitle("BTN_SAVE_SCENE");
46 }
47
48 private void OnSave(string path)
49 {
50 Save(path);
51 }
52
53 public void Save(string path)
54 {
55#if !UNITY_WEBGL
56 string san = pb_FileUtility.SanitizePath(path);
57
58 if(!san.EndsWith(".json") && !san.EndsWith(".env"))
59 san += ".json";
60
61 if(!(pb_FileUtility.IsValidPath(san, ".json") || pb_FileUtility.IsValidPath(san, ".env") ))
62 {
63 Debug.LogWarning(san + " is not a valid path.");
64 return;
65 }
66 //#if UNITY_WEBGL
67 // StartCoroutine(NetworkManager.instance.SaveCourse(NetworkManager.instance.selectedLevel.id, pb_Scene.SaveLevel(),
68 // course =>
69 // {
70 // Debug.Log("Save successful!");
71 // }));
72 //#else
73 if (System.IO.File.Exists(san))
74 {
75 YesNoPopupManager.instance.ShowPopup("FILE_EXISTS", "FILE_OVERWRITE_CONFIRM");
76 YesNoPopupManager.instance.InvokeOnYes(() => { StartCoroutine(RealSave(san)); });
77 YesNoPopupManager.instance.InvokeOnNo(() => { PopupManager.instance.ShowPopup("SAVE_CANCELED", "FILE_NOT_SAVED"); });
78 }
79 else
80 {
81 //pb_FileUtility.SaveFile(san, pb_Scene.SaveLevel());
82 StartCoroutine(RealSave(san));
83 }
84 //#endif
85
86#if PB_DEBUG
87 if(System.IO.File.Exists(path))
88 System.Diagnostics.Process.Start(path);
89#endif
90#endif
91 }
92
93#if !UNITY_WEBGL
94 private IEnumerator RealSave(string san)
95 {
96 var currentAction = FindObjectOfType<StartAction>();
97 if (!currentAction)
98 {
99 Debug.LogWarning("No actions found, trying to fix it.");
101 yield return null;
102 }
103 currentAction = FindObjectOfType<StartAction>();
104 if (!currentAction)
105 {
106 Debug.LogError("No actions found, can't save!");
107 }
108 if (currentAction || san.EndsWith("env"))
109 {
110 LoadingManager.instance.SetLoading();
111 LoadingManager.instance.ShowWindow();
112 if (currentAction)
113 {
114 Controller.Instance.ClickedOn(currentAction.name);
115 while (Controller.Instance.fastForwarding)
116 {
117 yield return null;
118 }
119 }
120 pb_FileUtility.SaveFile(san, pb_Scene.SaveLevel());
121 LoadingManager.instance.HideWindow();
122 PopupManager.instance.ShowPopup("SAVED", "SAVED");
123 }
124 }
125#endif
126#if UNITY_WEBGL
127 private IEnumerator WebGLSave()
128 {
129 yield return Controller.Instance.RestoreOnly();
130 yield return NetworkManager.instance.SaveCourse(NetworkManager.instance.selectedLevel.id, pb_Scene.SaveLevel(),
131 course =>
132 {
133 Debug.Log("Save successful, starting file upload!");
134 StartCoroutine(FindObjectOfType<UploadLevelObjects>().UploadAllFiles());
135 HttpCookie.RemoveCookie("unsavedChanges");
136 HttpCookie.RemoveRootCookie("unsavedChanges");
137 });
138 }
139#endif
140 }
141}
UnityEngine.Debug Debug
Definition: TanodaServer.cs:19
IEnumerator RestoreOnly()
Definition: Controller.cs:960
void OnStageManagerToggle()
Definition: Controller.cs:539
static Controller Instance
Definition: Controller.cs:16
void ClickedOn(string id)
Definition: Controller.cs:975
string filePattern
If isFileBrowser is true, this string my be used to filter file results (see https://msdn....
void AddOnSaveListener(Callback< string > listener)
void SetDirectory(string directory)
static void SetTitle(string title)
static void SetContent(GameObject prefab)