public static void DisableTogglePack(string packName) { // Disable CustomWorldStructs.RegionPack pack = CustomWorldMod.installedPacks[packName]; pack.activated = !pack.activated; CustomWorldMod.SerializePackInfoJSON(CRExtras.BuildPath(pack.folderName, CRExtras.CustomFolder.None, file: "packInfo.json"), pack); CustomWorldMod.LoadCustomWorldResources(); }
public void Init(string arguments, string executableName) { // Delete old folder if (CustomWorldMod.installedPacks.ContainsKey(this.packName)) { string folderName = CustomWorldMod.installedPacks[this.packName].folderName; string pathToPackFolder = CRExtras.BuildPath(folderName, CRExtras.CustomFolder.None); CustomWorldMod.Log($"Updating pack, check if folder exists at: [{pathToPackFolder}]...", false, CustomWorldMod.DebugLevel.MEDIUM); if (Directory.Exists(pathToPackFolder)) { try { Directory.Delete(pathToPackFolder, true); } catch (Exception e) { CustomWorldMod.Log(e.ToString(), true); } } } base.Init(); Log($"Executing console app [{executableName}]"); ProcessStartInfo processStartInfo = new ProcessStartInfo(executableName, arguments); processStartInfo.UseShellExecute = false; processStartInfo.ErrorDialog = false; processStartInfo.CreateNoWindow = true; //processStartInfo.RedirectStandardInput = true; processStartInfo.RedirectStandardOutput = true; //processStartInfo.RedirectStandardError = true; // Start the process process = new Process(); process.StartInfo = processStartInfo; bool processStarted = process.Start(); process.BeginOutputReadLine(); captureOutput = new StringBuilder(); process.OutputDataReceived += Process_OutputDataReceived; }
public override void Update() { if (urls == null || currentThumb >= this.urls.Count || packNames == null || www == null) { readyToDelete = true; return; } if (string.IsNullOrEmpty(www.error)) { if (www.isDone) { if (!next) { Log($"Downloaded thumb [{packNames[currentThumb]}]"); thumbOutput.Add(packNames[currentThumb], www.bytes); Texture2D tex = null; if (CustomWorldMod.installedPacks.TryGetValue(packNames[currentThumb], out RegionPack value)) { string path = Custom.RootFolderDirectory() + CustomWorldMod.resourcePath + value.folderName + Path.DirectorySeparatorChar + "thumb.png"; if (!File.Exists(path)) { Log($"Saving thumb from [{packNames[currentThumb]}]... Path [{path}]"); tex = new Texture2D(4, 4, TextureFormat.RGBA32, false); tex.LoadImage(www.bytes); tex.Apply(); byte[] file = tex.EncodeToPNG(); File.WriteAllBytes(path, file); } } // Proccess thumbnail if (tex == null) { tex = new Texture2D(4, 4, TextureFormat.RGBA32, false); tex.LoadImage(www.bytes); //..this will auto-resize the texture dimensions. } ProcessedThumbnail procThumb = CRExtras.ProccessThumbnail(tex, www.bytes, packNames[currentThumb]); Log($"Adding processed thumbnail [{packNames[currentThumb]}]"); if (CustomWorldMod.processedThumbnails.ContainsKey(packNames[currentThumb])) { CustomWorldMod.processedThumbnails[packNames[currentThumb]] = procThumb; } else { CustomWorldMod.processedThumbnails.Add(packNames[currentThumb], procThumb); } next = true; currentThumb++; bool shouldDownload = false; while (!shouldDownload && currentThumb < this.urls.Count) { string currentPackName = this.packNames[currentThumb]; DateTime current = DateTime.UtcNow; Log($"[{currentPackName}] Checking if thumbnail needs to be downloaded..."); if (CustomWorldMod.processedThumbnails.TryGetValue(currentPackName, out ProcessedThumbnail tempThumb)) { TimeSpan diff = current - tempThumb.dateDownloaded; this.Log($"[{currentPackName}]'s thumb was downloaded [{diff.TotalMinutes}] mins ago"); if (Math.Abs(diff.TotalMinutes) > 2) { shouldDownload = true; break; } else { currentThumb++; } } else { shouldDownload = true; break; } } } else { this.www = new WWW(urls[currentThumb]); next = false; } } } else { readyToDelete = true; CustomWorldMod.Log(www.error, true); } }
private void CreateRegionPackList(OpTab tab, Dictionary <string, RegionPack> packs, Dictionary <string, byte[]> thumbnails, bool raindb) { //How Many Options int numberOfOptions = packs.Count; if (numberOfOptions < 1) { OpLabel label2 = new OpLabel(new Vector2(100f, 450), new Vector2(400f, 20f), "No regions available.", FLabelAlignment.Center, false); if (raindb && CustomWorldMod.OfflineMode) { label2.text = "Browsing RainDB is not available in offline mode"; } tab.AddItems(label2); return; } int spacing = 25; // SIZES AND POSITIONS OF ALL ELEMENTS // Vector2 rectSize = new Vector2(475, 175); Vector2 thumbSize = new Vector2(225, 156); Vector2 buttonDownloadSize = new Vector2(80, 30); Vector2 labelSize = new Vector2(rectSize.x - thumbSize.x - 1.5f * spacing, 25); Vector2 descripSize = new Vector2(rectSize.x - thumbSize.x - 1.5f * spacing, rectSize.y - labelSize.y - buttonDownloadSize.y); OpScrollBox mainScroll = new OpScrollBox(new Vector2(25, 25), new Vector2(550, 500), (int)(spacing + ((rectSize.y + spacing) * numberOfOptions))); Vector2 rectPos = new Vector2(spacing, mainScroll.contentSize - rectSize.y - spacing); // ---------------------------------- // tab.AddItems(mainScroll); for (int i = 0; i < numberOfOptions; i++) { RegionPack pack = packs.ElementAt(i).Value; bool activated = pack.activated; bool update = false; try { update = raindb && !activated && pack.checksum != null && pack.checksum != string.Empty && !pack.checksum.Equals(CustomWorldMod.installedPacks[pack.name].checksum); } catch { CustomWorldMod.Log("Error checking the checksum for updates"); } Color colorEdge = activated ? Color.white : new Color((108f / 255f), 0.001f, 0.001f); Color colorInverse = Color.white; /* * if (raindb) * { * colorEdge = Color.white; * try * { * // Online checksum is different from local, needs to be updated. * if (!activated && pack.checksum != null && pack.checksum != string.Empty) * { * update = !pack.checksum.Equals(CustomWorldMod.installedPacks[pack.name].checksum); * } * } * catch { CustomWorldMod.Log("Error checking the checksum for updates"); } * } */ // RECTANGLE OpRect rectOption = new OpRect(rectPos, rectSize, 0.2f) { doesBump = activated && !pack.packUrl.Equals(string.Empty) }; mainScroll.AddItems(rectOption); // ---------------------------------- // // REGION NAME LABEL string nameText = pack.name; if (!pack.author.Equals(string.Empty)) { nameText += " [by " + pack.author.ToUpper() + "]"; } OpLabel labelRegionName = new OpLabel(rectPos + new Vector2(thumbSize.x + spacing, 140), labelSize, "", FLabelAlignment.Left) { description = nameText }; // Add load order number if local pack if (!raindb) { nameText = (i + 1).ToString() + "] " + nameText; } // Trim in case of overflow CRExtras.TrimString(ref nameText, labelSize.x, "..."); labelRegionName.text = nameText; mainScroll.AddItems(labelRegionName); // ---------------------------------- // // DESCRIPTION LABEL OpLabelLong labelDesc = new OpLabelLong(rectPos + new Vector2(spacing + thumbSize.x, (rectSize.y - descripSize.y - labelSize.y - 0.5f * spacing)), descripSize, "", true, FLabelAlignment.Left) { text = pack.description, verticalAlignment = OpLabel.LabelVAlignment.Top, allowOverflow = false }; mainScroll.AddItems(labelDesc); // ---------------------------------- // if (thumbnails.TryGetValue(pack.name, out byte[] fileData) && fileData.Length > 0)
public override void Signal(UItrigger trigger, string signal) { base.Signal(trigger, signal); if (signal != null) { CustomWorldMod.Log($"Received menu signal [{signal}]"); // Refresh config menu list if (signal.Equals("refresh")) { ConfigMenu.ResetCurrentConfig(); } // Reload pack list else if (signal.Equals("reloadRegions")) { CustomWorldMod.LoadCustomWorldResources(); } // Downnload a pack X else if (signal.Contains("download") || signal.Contains("update")) { if (CustomWorldMod.scripts.FindAll(x => x is PackDownloader).Count == 0) { // Process ID of Rain World string ID = System.Diagnostics.Process.GetCurrentProcess().Id.ToString(); // Divider used string divider = "<div>"; // Name of the pack to download string packName = signal.Substring(signal.IndexOf("_") + 1); string url = ""; CustomWorldMod.Log($"Download / update signal from [{packName}]"); if (CustomWorldMod.rainDbPacks.TryGetValue(packName, out RegionPack toDownload)) { url = toDownload.packUrl; } if (url != null && url != string.Empty) { string arguments = $"{url}{divider}\"{packName}\"{divider}{ID}{divider}" + @"\" + CustomWorldMod.resourcePath + (signal.Contains("update") ? $"{divider}update" : ""); CustomWorldMod.Log($"Creating pack downloader for [{arguments}]"); CustomWorldMod.scripts.Add(new PackDownloader(arguments, packName)); CRExtras.TryPlayMenuSound(SoundID.MENU_Player_Join_Game); } else { CustomWorldMod.Log($"Error loading pack [{packName}] from raindb pack list", true); } } else { CustomWorldMod.Log("Pack downloader in process"); CRExtras.TryPlayMenuSound(SoundID.MENU_Player_Unjoin_Game); } } // Close the game else if (signal.Equals("close_game")) { CustomWorldMod.Log("Exiting game..."); Application.Quit(); } // Close(hide) pop-up window else if (signal.Equals("close_window")) { if (currentWindowPopUp != null) { OpTab tab = ConfigMenu.currentInterface.Tabs.First(x => x.name.Equals("Browse RainDB")); if (tab != null) { foreach (UIelement item in currentWindowPopUp) { try { item.Hide(); } catch (Exception e) { CustomWorldMod.Log("option " + e, true); } } } } } else { CustomWorldMod.Log($"Unknown signal [{signal}]", true); } } }
public static CustomWorldStructs.ProcessedThumbnail ProccessThumbnail(Texture2D oldTex, byte[] data, string packName)//, bool activated, bool raindb) { Color colorEdge; Texture2D newTex = new Texture2D(oldTex.width, oldTex.height, TextureFormat.RGBA32, false); Color[] convertedImage = oldTex.GetPixels(); List <HSLColor> hslColors = new List <HSLColor>(); int numberOfPixels = convertedImage.Length; for (int c = 0; c < numberOfPixels; c++) { /* * // Change opacity if not active * if (!activated && !raindb) * { * convertedImage[c].a *= 0.65f; * } */ HSLColor hslColor = CRExtras.RGB2HSL(convertedImage[c]); if (hslColor.saturation > 0.25 && hslColor.lightness > 0.25 && hslColor.lightness < 0.75f) { hslColors.Add(hslColor); } } float averageLight = 0f; float averageSat = 0f; float medianHue = 0f; // Calculate average light and sat if (hslColors.Count > 0) { foreach (HSLColor color in hslColors) { averageLight += color.lightness / hslColors.Count; averageSat += color.saturation / hslColors.Count; } } // Calculate median hue int half = hslColors.Count() / 2; var sortedColors = hslColors.OrderBy(x => x.hue); if (half != 0 && half < sortedColors.Count()) { try { if ((hslColors.Count % 2) == 0) { medianHue = (sortedColors.ElementAt(half).hue + sortedColors.ElementAt(half - 1).hue) / 2; } else { medianHue = sortedColors.ElementAt(half).hue; } } catch (Exception e) { CustomWorldMod.Log($"Cannot calculate median hue [{e}] for [{packName}]", true); } } //if ((activated || raindb)) { if (averageSat > 0.15f) { colorEdge = Color.Lerp(Custom.HSL2RGB(medianHue, averageSat, Mathf.Lerp(averageLight, 0.6f, 0.5f)), Color.white, 0.175f); } else { colorEdge = Custom.HSL2RGB(UnityEngine.Random.Range(0.1f, 0.75f), 0.4f, 0.75f); } CustomWorldMod.Log($"Color for [{packName}] - MedianHue [{medianHue}] averageSat [{averageSat}] averagelight [{averageLight}] " + $"- Number of pixels [{numberOfPixels}]]", false, CustomWorldMod.DebugLevel.FULL); } hslColors.Clear(); //newTex.SetPixels(convertedImage); //newTex.Apply(); //TextureScale.Point(newTex, (int)(thumbSize.x), (int)(thumbSize.y)); CustomWorldStructs.ProcessedThumbnail procThumb = new CustomWorldStructs.ProcessedThumbnail(); procThumb.dateDownloaded = DateTime.UtcNow; procThumb.mainColor = colorEdge; procThumb.data = data; CustomWorldMod.Log($"Processed thumbnail for [{packName}] at [{procThumb.dateDownloaded}]", false, CustomWorldMod.DebugLevel.MEDIUM); return(procThumb); }