/// <summary> /// Constructor for the TSUpdater class /// </summary> public TSUpdater() { state = UpdaterState.Waiting; timedCoroutines = new List <TSTimedCoroutine>(); previousTimeSinceStartup = DateTime.Now; LocalVersionJSON local; if (File.Exists(TSConstants.LocalJSONPath)) { local = JsonUtility.FromJson <LocalVersionJSON>(File.ReadAllText(TSConstants.LocalJSONPath)); } else { local = new LocalVersionJSON(); local.beta = true; local.betaSha = ""; local.version = "beta"; local.lastCheck = DateTime.Now.ToString(); File.WriteAllText(TSConstants.LocalJSONPath, JsonUtility.ToJson(local)); } if (local.beta) { updateStream = UpdateStream.Beta; } else { updateStream = UpdateStream.Release; } }
/// <summary> /// Draws the settings GUI /// </summary> private void DrawSettings() { EditorGUI.BeginChangeCheck(); inspectorLevel = (InspectorLevel)EditorGUILayout.EnumPopup(TSConstants.TSWindowLabels.InspectorLevel, inspectorLevel); if (EditorGUI.EndChangeCheck()) { EditorPrefs.SetInt(TSConstants.TSEPInspectorLevel, (int)inspectorLevel); } EditorGUI.BeginChangeCheck(); updater.updateStream = (UpdateStream)EditorGUILayout.EnumPopup(TSConstants.TSWindowLabels.UpdateStream, updater.updateStream); if (EditorGUI.EndChangeCheck()) { LocalVersionJSON local = JsonUtility.FromJson <LocalVersionJSON>(File.ReadAllText(TSConstants.LocalJSONPath)); local.beta = updater.updateStream == UpdateStream.Beta; File.WriteAllText(TSConstants.LocalJSONPath, JsonUtility.ToJson(local)); updater.Reset(); } EditorGUI.BeginChangeCheck(); sectionStyle = (SectionStyle)EditorGUILayout.EnumPopup(TSConstants.TSWindowLabels.SectionStyle, sectionStyle); sectionColor = EditorGUILayout.ColorField(TSConstants.TSWindowLabels.Color, sectionColor); isAutoUpdateDisabled = EditorGUILayout.Toggle(TSConstants.TSWindowLabels.DisableAutoUpdates, isAutoUpdateDisabled); if (EditorGUI.EndChangeCheck()) { TSSettings settings = JsonUtility.FromJson <TSSettings>(File.ReadAllText(TSConstants.SettingsJSONPath)); settings.sectionStyle = (int)sectionStyle; settings.sectionColor = sectionColor; settings.disableUpdates = isAutoUpdateDisabled; File.WriteAllText(TSConstants.SettingsJSONPath, JsonUtility.ToJson(settings)); exampleSection = new Section(new GUIContent("Example Section"), true, delegate(MaterialEditor m){ EditorGUILayout.LabelField("Example content"); }, delegate(bool a, bool b){}); } GUILayout.Space(20); if (exampleSection == null) { exampleSection = new Section(new GUIContent("Example Section"), true, delegate(MaterialEditor m){ EditorGUILayout.LabelField("Example content"); }, delegate(bool a, bool b){}); } exampleSection.DrawSection(null); }
/// <summary> /// Coroutine that downloads the update file and installs it /// </summary> /// <returns></returns> public IEnumerator <float> DownloadUpdate() { if (state == UpdaterState.Ready) { // Creates a web request to the github repository based on the selected update stream if (updateStream == UpdateStream.Beta) { request = new UnityWebRequest("https://github.com/Cibbi/Toony-standard/archive/" + githubBetaJSON.sha + ".zip"); file = new DownloadHandlerFile(Application.dataPath + "/toonyStandard.zip"); } else { request = new UnityWebRequest(githubReleaseJSON.assets[0].browser_download_url); file = new DownloadHandlerFile(Application.dataPath + "/toonyStandard.unitypackage"); } request.method = UnityWebRequest.kHttpVerbGET; file.removeFileOnAbort = true; request.downloadHandler = file; request.SendWebRequest(); state = UpdaterState.Downloading; // Main check cycle that waits for the downloaded file, like the update check execution is paused every cycle to not block // normal window execution while (state == UpdaterState.Downloading) { yield return(0.5f); // Executed if the request is done if (request.isDone) { state = UpdaterState.Downloaded; TSSettings settings = JsonUtility.FromJson <TSSettings>(File.ReadAllText(TSConstants.SettingsJSONPath)); // If the update stream is the beta one the downloaded file is a zip file, meaning that we have to extract it manually, fortunately a guy called Yallie made a simple // extraction class that handles the basic stuff needed here, check him over https://github.com/yallie/unzip if (updateStream == UpdateStream.Beta) { string localFolder = TSConstants.LocalShaderFolder; Unzip zip = new Unzip(Application.dataPath + "/toonyStandard.zip"); // Deleting the old Toony standard version if (Directory.Exists(TSConstants.LocalShaderFolder)) { Directory.Delete(TSConstants.LocalShaderFolder, true); } // For each file in the zip we change the github repository path with the more user friendly one used on the releases, and then extract that file in that path foreach (string fileName in zip.FileNames) { string newDir = fileName.Replace("Toony-standard-" + githubBetaJSON.sha, localFolder); if (!Directory.Exists(Path.GetDirectoryName(newDir))) { Directory.CreateDirectory(Path.GetDirectoryName(newDir)); } zip.Extract(fileName, newDir); } // Disposing of the zip, this is important cause without doing it the zip file cannot be deleted afterwards zip.Dispose(); // Creation of the updated version.json file for this beta version, cause the one that comes in the zip does not contain the sha of the commit used when checking updates // Since it's impossible to know a commit sha before doing such commit. LocalVersionJSON local = new LocalVersionJSON(); local.beta = true; local.betaSha = githubBetaJSON.sha; local.version = "beta"; local.lastCheck = DateTime.Now.ToString(); File.WriteAllText(TSConstants.LocalJSONPath, JsonUtility.ToJson(local)); // The asset database is refreshed to be sure that the zip file is actually detected from the asset database for its deletion File.Delete(Application.dataPath + "/toonyStandard.zip"); AssetDatabase.Refresh(ImportAssetOptions.ImportRecursive); } // If the update stream is the release one the downloaded file is the latest unitypackage that can be found here https://github.com/Cibbi/Toony-standard/releases // Since it's a unitypackage its installation is relatively easy, but we still delete the old version first for safety else { if (Directory.Exists(TSConstants.LocalShaderFolder)) { Directory.Delete(TSConstants.LocalShaderFolder, true); } AssetDatabase.ImportPackage(Application.dataPath + "/toonyStandard.unitypackage", false); AssetDatabase.Refresh(); AssetDatabase.DeleteAsset("Assets/toonyStandard.unitypackage"); } File.WriteAllText(TSConstants.OldSettingsJSONPath, JsonUtility.ToJson(settings)); } // Executed if the request got an error response if (request.isNetworkError || request.isHttpError) { Debug.Log("Toony Standard: network error during downlaod, please retry later"); } } } }
/// <summary> /// Coroutine that checks if there is a new update available /// </summary> /// <returns></returns> public IEnumerator <float> CheckForUpdate() { LocalVersionJSON local; // Checks if there's a dev file in the project to skip the update check if (File.Exists(Application.dataPath + "/DevCheck.json")) { local = JsonUtility.FromJson <LocalVersionJSON>(File.ReadAllText(Application.dataPath + "/DevCheck.json")); if (local.version.Equals("dev")) { state = UpdaterState.doNotUpdate; yield break; } local = null; } // Checks if the version json is present and creates a new one that will trigger an update if not present if (File.Exists(TSConstants.LocalJSONPath)) { local = JsonUtility.FromJson <LocalVersionJSON>(File.ReadAllText(TSConstants.LocalJSONPath)); } else { local = new LocalVersionJSON(); local.beta = true; local.betaSha = ""; local.version = "release"; local.lastCheck = DateTime.Now.AddDays(-2).ToString(); File.WriteAllText(TSConstants.LocalJSONPath, JsonUtility.ToJson(local)); } if (local.lastCheck == null || local.lastCheck.Equals("")) { local.lastCheck = DateTime.Now.AddDays(-2).ToString(); } // If it was checked recently, do not update it if (DateTime.Parse(local.lastCheck).AddDays(1) > DateTime.Now && !isManualUpdate) { state = UpdaterState.doNotUpdate; yield break; } // Creates a web request to the github api dependent to the update stream currently selected if (updateStream == UpdateStream.Beta) { request = new UnityWebRequest("https://api.github.com/repos/Cibbi/Toony-standard/commits/master"); } else { request = new UnityWebRequest("https://api.github.com/repos/Cibbi/Toony-standard/releases/latest"); } request.method = UnityWebRequest.kHttpVerbGET; response = new DownloadHandlerBuffer(); request.downloadHandler = response; request.SendWebRequest(); state = UpdaterState.Fetching; // Main check cycle that waits for a response from the api, execution of this part is paused every cycle for 0.5 seconds in order to avoid // to block the normal window execution while (state == UpdaterState.Fetching) { yield return(0.5f); // Executed if the request got an error response if (request.isHttpError || request.isNetworkError) { state = UpdaterState.Error; Debug.Log(request.error); } // Executed if the request is done if (request.isDone) { if (updateStream == UpdateStream.Beta) { githubBetaJSON = JsonUtility.FromJson <GithubCommitJSON>(response.text); if (local.beta && local.betaSha == githubBetaJSON.sha) { state = UpdaterState.UpToDate; } else if (local.betaSha.Equals("nosha")) { local.betaSha = githubBetaJSON.sha; File.WriteAllText(TSConstants.LocalJSONPath, JsonUtility.ToJson(local)); state = UpdaterState.UpToDate; } else { state = UpdaterState.Ready; } } else { githubReleaseJSON = JsonUtility.FromJson <GithubReleaseJSON>(response.text); if (!local.beta && local.version.Equals(githubReleaseJSON.tag_name)) { state = UpdaterState.UpToDate; } else { state = UpdaterState.Ready; } } // Update the last check time local.lastCheck = DateTime.Now.ToString(); File.WriteAllText(TSConstants.LocalJSONPath, JsonUtility.ToJson(local)); } } }