public void OnClick_Install() { // Get the cached mission details string json = File.ReadAllText(CachePath.GetMissionDetailsFilePath(missionData.missionID)); MissionData localData = JsonUtility.FromJson <MissionData>(json); string sdkVersion = CachePath.GetSDKVersion(localData); // Do not allow installation if mission files already exist foreach (string fileName in localData.fileNames) { string filePath = Path.Combine(UserPrefs.gameDirectory, fileName); if (File.Exists(filePath)) { InfoDialog.Create("Installation Failed", "The file '" + fileName + "' already exists in your game directory. Please remove it to continue."); return; } } List <string> installedFiles = new List <string>(); // Need to export plugin for standard mission OPM file if (m_IsStandardMission) { string opmFileName = localData.fileNames.FirstOrDefault((string fileName) => Path.GetExtension(fileName).ToLowerInvariant().Contains("opm")); string opmFilePath = Path.Combine(CachePath.GetMissionDirectory(localData.missionID), opmFileName); string pluginFileName = Path.ChangeExtension(opmFileName, ".dll"); string pluginPath = Path.Combine(UserPrefs.gameDirectory, pluginFileName); // Don't allow install if the plugin will overwrite another DLL of the same name if (File.Exists(pluginPath)) { InfoDialog.Create("Install Failed", "There is already a plugin named " + pluginFileName); return; } // Export plugin MissionRoot root = MissionReader.GetMissionData(opmFilePath); PluginExporter.ExportPlugin(pluginPath, root.sdkVersion, root.levelDetails); FileReference.AddReference(pluginFileName); installedFiles.Add(pluginFileName); } // Install mission from cache into game folder foreach (string fileName in localData.fileNames) { string filePath = Path.Combine(CachePath.GetMissionDirectory(localData.missionID), fileName); InstallFile(fileName, filePath); installedFiles.Add(fileName); } // Install SDK if (!string.IsNullOrEmpty(sdkVersion)) { InstallFile(CachePath.GetSDKFileName(sdkVersion), CachePath.GetSDKFilePath(sdkVersion)); InstallFile(CachePath.DotNetInteropFileName, CachePath.GetInteropFilePath(), true); installedFiles.Add(CachePath.GetSDKFileName(sdkVersion)); installedFiles.Add(CachePath.DotNetInteropFileName); } // Write installed files to cache using (FileStream fs = new FileStream(CachePath.GetMissionInstalledMetaFilePath(localData.missionID), FileMode.Create, FileAccess.Write, FileShare.Read)) using (StreamWriter writer = new StreamWriter(fs)) { foreach (string fileName in installedFiles) { writer.WriteLine(fileName); } } // Set buttons m_BtnInstall.gameObject.SetActive(false); m_BtnUninstall.gameObject.SetActive(true); m_BtnDelete.interactable = false; }
private IEnumerator RequestDownloadMission() { ProgressDialog progressDialog = ProgressDialog.Create("Downloading Mission"); foreach (string fileName in missionData.fileNames) { // Perform request string url = WebConfig.webHost + "download/" + missionData.missionID + "/" + fileName; string destPath = Path.Combine(CachePath.GetMissionDirectory(missionData.missionID), fileName); using (UnityWebRequest request = UnityWebRequest.Get(url)) { progressDialog.SetTitle("Downloading " + fileName); progressDialog.SetWebRequest(request); yield return(request.SendWebRequest()); if (!DidDownloadSucceed(request, true)) { progressDialog.Close(); OnClick_Delete(); yield break; } WriteFile(destPath, request.downloadHandler.data); } } // Get SDK path if (!string.IsNullOrEmpty(sdkVersion)) { // Download SDK if it does not exist if (!File.Exists(CachePath.GetSDKFilePath(sdkVersion))) { string url = "https://github.com/TechCor8/OP2DotNetMissionSDK/releases/download/" + sdkVersion + "/" + CachePath.GetSDKFileName(sdkVersion); string destPath = CachePath.GetSDKFilePath(sdkVersion); using (UnityWebRequest request = UnityWebRequest.Get(url)) { progressDialog.SetTitle("Downloading " + CachePath.GetSDKFileName(sdkVersion)); progressDialog.SetWebRequest(request); yield return(request.SendWebRequest()); if (!DidDownloadSucceed(request, true)) { progressDialog.Close(); OnClick_Delete(); yield break; } WriteFile(destPath, request.downloadHandler.data); } } // Download SDK Interop if it does not exist or is from an older SDK if (!File.Exists(CachePath.GetInteropFilePath()) || CachePath.IsNewerVersion(sdkVersion, CacheDetails.interopVersion)) { string url = "https://github.com/TechCor8/OP2DotNetMissionSDK/releases/download/" + sdkVersion + "/" + CachePath.DotNetInteropFileName; string destPath = CachePath.GetInteropFilePath(); using (UnityWebRequest request = UnityWebRequest.Get(url)) { progressDialog.SetTitle("Downloading " + CachePath.DotNetInteropFileName); progressDialog.SetWebRequest(request); yield return(request.SendWebRequest()); if (!DidDownloadSucceed(request, true)) { progressDialog.Close(); OnClick_Delete(); yield break; } WriteFile(destPath, request.downloadHandler.data); } } } progressDialog.Close(); // Mission fully downloaded. // Write mission details string detailsJson = JsonUtility.ToJson(missionData); File.WriteAllText(CachePath.GetMissionDetailsFilePath(missionData.missionID), detailsJson); // Add mission details reference CacheDetails.AddMissionData(missionData); // Set buttons m_BtnDownload.gameObject.SetActive(false); m_BtnDelete.gameObject.SetActive(true); m_BtnInstall.interactable = true; }