public void DownloadAndInstallOlmod(OlmodRelease release, string localInstallFolder) { string localTempZip = Path.GetTempFileName() + "_OST_Olmod_Update.zip"; string localTempFolder = Path.GetTempFileName() + "_OST_Olmod_Update"; Directory.CreateDirectory(localTempFolder); try { System.Net.ServicePointManager.ServerCertificateValidationCallback = (sender, certificate, chain, errors) => { return(true); }; ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; using (WebClient wc = new WebClient()) { wc.Headers.Add("User-Agent", "Overload Server Tool - user " + WindowsIdentity.GetCurrent().Name); wc.DownloadFile(release.DownloadUrl, localTempZip); } ZipFile.ExtractToDirectory(localTempZip, localTempFolder); // Copy all files to destination, overwriting any existing files. DirectoryInfo dir = new DirectoryInfo(localTempFolder); foreach (FileInfo fi in dir.GetFiles()) { File.Copy(fi.FullName, Path.Combine(localInstallFolder, fi.Name), true); } // Time stamp olmod.exe. File.SetCreationTime(Path.Combine(localInstallFolder, "olmod.exe"), release.Created); File.SetLastWriteTime(Path.Combine(localInstallFolder, "olmod.exe"), release.Created); LogMessage(String.Format($"Olmod has been updated to release {release.Version}")); } catch (Exception ex) { LogErrorMessage(String.Format($"Cannot download Olmod ZIP file from Github: {ex.Message}")); } finally { if (OverloadServerToolApplication.ValidFileName(localTempZip, true)) { try { File.Delete(localTempZip); } catch { } } if (OverloadServerToolApplication.ValidDirectoryName(localTempFolder, true)) { try { OverloadServerToolApplication.RemoveDirectory(localTempFolder); } catch { } } } }
/// <summary> /// Updates both online maps available for download as well as local maps. /// </summary> /// <param name="mapListUrl">URL to online JSON master map list.</param> /// <param name="dlcMaps">Path to local DLC folder (or null).</param> /// <param name="applicationMaps">Path to local application folder (or null).</param> public void UpdateMapList(string mapListUrl = null, string dlcMaps = null, string applicationMaps = null) { // Check for override of default URL to JSON map list. if (String.IsNullOrEmpty(mapListUrl)) { mapListUrl = DefaultOnlineMapListUrl; } // Check DLC/application directory names. if (!String.IsNullOrEmpty(dlcMaps)) { dlcMapFolder = dlcMaps; if (!OverloadServerToolApplication.ValidDirectoryName(dlcMapFolder)) { LogErrorMessage("DLC directory name is invalid!"); return; } } if (!String.IsNullOrEmpty(applicationMaps)) { applicationMapFolder = applicationMaps; if (!OverloadServerToolApplication.ValidDirectoryName(applicationMapFolder)) { LogErrorMessage("Application data directory name is invalid!"); return; } } // Need at least one defined folder to store maps. if (String.IsNullOrEmpty(applicationMapFolder) && String.IsNullOrEmpty(dlcMapFolder)) { LogErrorMessage("No application data directory or DLC directory found!"); return; } // Collect all new online and local maps. SortedList <string, OverloadMap> newMapList = new SortedList <String, OverloadMap>(); // First find all online maps. try { // Get URL base so we can construct full URLs to the online map ZIP files. Uri uri = new Uri(mapListUrl); string baseUrl = String.Concat(uri.Scheme, Uri.SchemeDelimiter, uri.Host); // Get map list and build internal map master list. string jsonMapList = new WebClient().DownloadString(mapListUrl); dynamic mapList = JsonConvert.DeserializeObject(jsonMapList); foreach (var map in mapList) { // Check to make sure it is a ZIP file before adding it to the list. string mapUrl = baseUrl + map.url; Uri mapUri = new Uri(mapUrl); if (mapUri.Segments.Length > 2) { // Get the ZIP file name from URL. string mapZipName = mapUri.Segments[mapUri.Segments.Length - 1]; if (mapZipName.ToLower().EndsWith(".zip")) { // Uppercase first char in map name. mapZipName = mapZipName.Substring(0, 1).ToUpper() + mapZipName.Substring(1); OverloadMap newMap = new OverloadMap(baseUrl + map.url, UnixTimeStampToDateTime(Convert.ToDouble(map.mtime)), Convert.ToInt32(map.size), mapZipName); newMapList.Add(mapZipName.ToLower(), newMap); } } } } catch (Exception ex) { LogErrorMessage(String.Format($"Unable to get online map list: {ex.Message}")); } // Now add any local maps or update info if existing local map file exists. if (OverloadServerToolApplication.ValidDirectoryName(applicationMapFolder)) { try { Directory.CreateDirectory(applicationMapFolder); string[] list = Directory.GetFiles(applicationMapFolder, "*.zip*"); foreach (string mapFileName in list) { if (mapFileName.EndsWith(HiddenMarker) || mapFileName.ToLower().EndsWith(".zip")) { string mapKey = Path.GetFileName(mapFileName).ToLower(); FileInfo fiLocalMap = new FileInfo(mapFileName); OverloadMap newMap = new OverloadMap(null, UnixTimeStampToDateTime(0), Convert.ToInt32(fiLocalMap.Length), mapFileName); bool found = false; foreach (KeyValuePair <string, OverloadMap> map in newMapList) { if (newMap.SameZipFileName(map.Value)) { // We found a local map that matches the ZIP filename as found online. found = true; // Uppercase first char in map name and remove hidden marker if present. string mapZipName = fiLocalMap.Name; mapZipName = mapZipName.Substring(0, 1).ToUpper() + mapZipName.Substring(1); mapZipName = mapZipName.Replace(HiddenMarker, ""); map.Value.ZipName = mapZipName; // Save full path to ZIP file in the application data folder. map.Value.LocalZipFileName = fiLocalMap.FullName; } } if (!found) { newMapList.Add(mapFileName.ToLower(), newMap); } } } } catch (Exception ex) { LogErrorMessage(String.Format($"Unable to get local maps: {ex.Message}")); } } if (OverloadServerToolApplication.ValidDirectoryName(dlcMapFolder)) { try { Directory.CreateDirectory(dlcMapFolder); string[] list = Directory.GetFiles(dlcMapFolder, "*.zip*"); foreach (string mapFileName in list) { if (mapFileName.EndsWith(HiddenMarker) || mapFileName.ToLower().EndsWith(".zip")) { string mapKey = Path.GetFileName(mapFileName).ToLower(); FileInfo fiLocalMap = new FileInfo(mapFileName); OverloadMap newMap = new OverloadMap(null, UnixTimeStampToDateTime(0), Convert.ToInt32(fiLocalMap.Length), mapFileName); bool found = false; foreach (KeyValuePair <string, OverloadMap> map in newMapList) { if (newMap.SameZipFileName(map.Value)) { // We found a local map that matches the ZIP filename as found online. found = true; // Uppercase first char in map name and remove hidden marker if present. string mapZipName = fiLocalMap.Name; mapZipName = mapZipName.Substring(0, 1).ToUpper() + mapZipName.Substring(1); mapZipName = mapZipName.Replace(HiddenMarker, ""); map.Value.ZipName = mapZipName; // Save full path to ZIP file in the DLC folder. map.Value.LocalDLCZipFileName = fiLocalMap.FullName; } } if (!found) { newMapList.Add(mapFileName.ToLower(), newMap); } } } } catch (Exception ex) { LogErrorMessage(String.Format($"Unable to get local maps: {ex.Message}")); } } // Update maps. Maps = newMapList; }