1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
|
using System.Collections;
using System.Collections.Generic;
using System;
using System.IO;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
using VRC.Core;
#if UNITY_EDITOR
using UnityEditor;
#endif
namespace VRCSDK2
{
#if UNITY_EDITOR
public class RuntimeAPICreation : MonoBehaviour
{
public VRC.Core.PipelineManager pipelineManager;
protected bool forceNewFileCreation = false;
protected bool useFileApi = false;
protected bool isUploading = false;
protected float uploadProgress = 0f;
protected string uploadMessage;
protected string uploadTitle;
protected string uploadVrcPath;
protected string uploadUnityPackagePath;
protected string cloudFrontAssetUrl;
protected string cloudFrontImageUrl;
protected string cloudFrontUnityPackageUrl;
protected CameraImageCapture imageCapture;
private bool cancelRequested = false;
public static bool publishingToCommunityLabs = false;
private Dictionary<string, string> mRetryState = new Dictionary<string, string>();
protected bool isUpdate { get { return pipelineManager.completedSDKPipeline; } }
protected void Start()
{
if (!Application.isEditor || !Application.isPlaying)
return;
PipelineSaver ps = GameObject.FindObjectOfType<PipelineSaver>();
pipelineManager = ps.gameObject.GetComponent<PipelineManager>();
imageCapture = GetComponent<CameraImageCapture>();
imageCapture.shotCamera = GameObject.Find("VRCCam").GetComponent<Camera>();
LoadUploadRetryStateFromCache();
forceNewFileCreation = UnityEditor.EditorPrefs.GetBool("forceNewFileCreation", true);
useFileApi = UnityEditor.EditorPrefs.GetBool("useFileApi", false);
API.SetOnlineMode(true);
}
protected void Update()
{
if (isUploading)
{
bool cancelled = UnityEditor.EditorUtility.DisplayCancelableProgressBar(uploadTitle, uploadMessage, uploadProgress);
if (cancelled)
{
cancelRequested = true;
}
if (EditorApplication.isPaused)
EditorApplication.isPaused = false;
}
}
protected void LoadUploadRetryStateFromCache()
{
try
{
string json = File.ReadAllText(GetUploadRetryStateFilePath());
mRetryState = VRC.Tools.ObjDictToStringDict(VRC.Tools.JsonDecode(json) as Dictionary<string, object>);
Debug.LogFormat("<color=yellow> loaded retry state: {0}</color>", json);
}
catch (Exception)
{
// normal case
return;
}
Debug.Log("Loaded upload retry state from: " + GetUploadRetryStateFilePath());
}
protected void SaveUploadRetryState(string key, string val)
{
if (string.IsNullOrEmpty(val))
return;
mRetryState[key] = val;
SaveUploadRetryState();
}
protected void SaveUploadRetryState()
{
try
{
Directory.CreateDirectory(Path.GetDirectoryName(GetUploadRetryStateFilePath()));
string json = VRC.Tools.JsonEncode(mRetryState);
File.WriteAllText(GetUploadRetryStateFilePath(), json);
Debug.LogFormat("<color=yellow> wrote retry state: {0}</color>", json);
}
catch (Exception e)
{
Debug.LogError("Couldn't save upload retry state: " + GetUploadRetryStateFilePath() + "\n" + e.Message);
return;
}
Debug.Log("Saved upload retry state to: " + GetUploadRetryStateFilePath());
}
protected void ClearUploadRetryState()
{
try
{
if (!File.Exists(GetUploadRetryStateFilePath()))
return;
File.Delete(GetUploadRetryStateFilePath());
}
catch (Exception e)
{
Debug.LogError("Couldn't delete upload retry state: " + GetUploadRetryStateFilePath() + "\n" + e.Message);
return;
}
Debug.Log("Cleared upload retry state at: " + GetUploadRetryStateFilePath());
}
protected string GetUploadRetryStateFilePath()
{
string id = UnityEditor.AssetDatabase.AssetPathToGUID(SceneManager.GetActiveScene().path);
return Path.Combine(VRC.Tools.GetTempFolderPath(id), "upload_retry.dat");
}
protected string GetUploadRetryStateValue(string key)
{
return mRetryState.ContainsKey(key) ? mRetryState[key] : "";
}
protected virtual void DisplayUpdateCompletedDialog(string contentUrl=null)
{
if (UnityEditor.EditorUtility.DisplayDialog("VRChat SDK", "Update Complete! Launch VRChat to see your uploaded content." + (null==contentUrl ? "" : "\n\nManage content at: " + contentUrl ), (null == contentUrl) ? "Okay" : CommunityLabsConstants.MANAGE_WORLD_IN_BROWSER_STRING, (null == contentUrl) ? "" : "Done" ))
{
if (null!=contentUrl)
{
Application.OpenURL(contentUrl);
}
}
}
protected void OnSDKPipelineComplete(string contentUrl=null)
{
VRC.Core.Logger.Log("OnSDKPipelineComplete", DebugLevel.All);
isUploading = false;
pipelineManager.completedSDKPipeline = true;
ClearUploadRetryState();
UnityEditor.EditorPrefs.SetBool("forceNewFileCreation", false);
UnityEditor.EditorApplication.isPaused = false;
UnityEditor.EditorApplication.isPlaying = false;
UnityEditor.EditorUtility.ClearProgressBar();
DisplayUpdateCompletedDialog(contentUrl);
}
protected void OnSDKPipelineError(string error, string details)
{
VRC.Core.Logger.Log("OnSDKPipelineError: " + error + " - " + details, DebugLevel.All);
isUploading = false;
pipelineManager.completedSDKPipeline = true;
UnityEditor.EditorApplication.isPaused = false;
UnityEditor.EditorApplication.isPlaying = false;
UnityEditor.EditorUtility.ClearProgressBar();
if (cancelRequested)
UnityEditor.EditorUtility.DisplayDialog("VRChat SDK", "The update was cancelled.", "Okay");
else
UnityEditor.EditorUtility.DisplayDialog("VRChat SDK", "Error updating content. " + error + "\n" + details, "Okay");
}
protected void SetUploadProgress(string title, string message, float progress)
{
uploadTitle = title;
uploadMessage = message;
uploadProgress = progress;
}
protected bool WasCancelRequested(ApiFile apiFile)
{
return cancelRequested;
}
protected void PrepareUnityPackageForS3(string packagePath, string blueprintId, int version, AssetVersion assetVersion)
{
uploadUnityPackagePath = Application.temporaryCachePath + "/" + blueprintId + "_" + version.ToString() + "_" + Application.unityVersion + "_" + assetVersion.ApiVersion + "_" + VRC.Tools.Platform +
"_" + API.GetServerEnvironmentForApiUrl() + ".unitypackage";
uploadUnityPackagePath.Trim();
uploadUnityPackagePath.Replace(' ', '_');
if (System.IO.File.Exists(uploadUnityPackagePath))
System.IO.File.Delete(uploadUnityPackagePath);
System.IO.File.Copy(packagePath, uploadUnityPackagePath);
}
protected void PrepareVRCPathForS3(string abPath, string blueprintId, int version, AssetVersion assetVersion)
{
uploadVrcPath = Application.temporaryCachePath + "/" + blueprintId + "_" + version.ToString() + "_" + Application.unityVersion + "_" + assetVersion.ApiVersion + "_" + VRC.Tools.Platform + "_" + API.GetServerEnvironmentForApiUrl() + System.IO.Path.GetExtension(abPath);
uploadVrcPath.Trim();
uploadVrcPath.Replace(' ', '_');
if (System.IO.File.Exists(uploadVrcPath))
System.IO.File.Delete(uploadVrcPath);
System.IO.File.Copy(abPath, uploadVrcPath);
}
protected IEnumerator UploadFile(string filename, string existingFileUrl, string friendlyFilename, string fileType, Action<string> onSuccess)
{
if (string.IsNullOrEmpty(filename))
yield break;
VRC.Core.Logger.Log("Uploading " + fileType + "(" + filename + ") ...", DebugLevel.All);
SetUploadProgress("Uploading " + fileType + "...", "", 0.0f);
string fileId = GetUploadRetryStateValue(filename);
if (string.IsNullOrEmpty(fileId))
fileId = isUpdate ? ApiFile.ParseFileIdFromFileAPIUrl(existingFileUrl) : "";
string errorStr = "";
string newFileUrl = "";
yield return StartCoroutine(ApiFileHelper.Instance.UploadFile(filename, forceNewFileCreation ? "" : fileId, friendlyFilename,
delegate (ApiFile apiFile, string message)
{
newFileUrl = apiFile.GetFileURL();
if (VRC.Core.Logger.DebugLevelIsEnabled(DebugLevel.API))
VRC.Core.Logger.Log(fileType + " upload succeeded: " + message + " (" + filename +
") => " + apiFile.ToString(), DebugLevel.API);
else
VRC.Core.Logger.Log(fileType + " upload succeeded ", DebugLevel.Always);
},
delegate (ApiFile apiFile, string error)
{
SaveUploadRetryState(filename, apiFile.id);
errorStr = error;
Debug.LogError(fileType + " upload failed: " + error + " (" + filename +
") => " + apiFile.ToString());
},
delegate (ApiFile apiFile, string status, string subStatus, float pct)
{
SetUploadProgress("Uploading " + fileType + "...", status + (!string.IsNullOrEmpty(subStatus) ? " (" + subStatus + ")" : ""), pct);
},
WasCancelRequested
));
if (!string.IsNullOrEmpty(errorStr))
{
OnSDKPipelineError(fileType + " upload failed.", errorStr);
yield break;
}
if (onSuccess != null)
onSuccess(newFileUrl);
}
protected IEnumerator UpdateImage(string existingFileUrl, string friendlyFileName)
{
string imagePath = imageCapture.TakePicture();
if (!string.IsNullOrEmpty(imagePath))
{
yield return StartCoroutine(UploadFile(imagePath, existingFileUrl, friendlyFileName, "Image",
delegate (string fileUrl)
{
cloudFrontImageUrl = fileUrl;
}
));
}
}
protected virtual IEnumerator CreateBlueprint()
{
throw new NotImplementedException();
}
protected virtual IEnumerator UpdateBlueprint()
{
throw new NotImplementedException();
}
protected bool ValidateNameInput(InputField nameInput)
{
bool isValid = true;
if (string.IsNullOrEmpty(nameInput.text))
{
isUploading = false;
UnityEditor.EditorUtility.DisplayDialog("Invalid Input", "Cannot leave the name field empty.", "OK");
isValid = false;
}
return isValid;
}
protected bool ValidateAssetBundleBlueprintID(string blueprintID)
{
string lastBuiltID = UnityEditor.EditorPrefs.GetString("lastBuiltAssetBundleBlueprintID", "");
return !string.IsNullOrEmpty(lastBuiltID) && lastBuiltID == blueprintID;
}
}
#endif
}
|