public override void Update() { if (url == null || readyToDelete) { readyToDelete = true; return; } if (string.IsNullOrEmpty(www.error)) { if (www.isDone && ready) { Log($"Fetching RainDB data... path [{url}]"); string file = www.text; List <object> json = file.listFromJson(); foreach (object jsonPack in json) { RegionPack regionPack = new RegionPack(""); //CustomWorldMod.Log($"Obtained data [{jsonPack.ToString()}]"); CustomWorldMod.FromDictionaryToPackInfo(jsonPack as Dictionary <string, object>, ref regionPack); try { regionPack.activated = true; //regionPack.activated = CustomWorldMod.installedRegionPacks.ContainsKey(regionPack.name); CustomWorldMod.rainDbPacks.Add(regionPack.name, regionPack); } catch (Exception e) { Log($"Exception when adding fetched region [{e}]", true); } } // CustomWorldMod.rainDbPacks = CustomWorldMod.rainDbPacks.OrderBy(x => x.Value.activated == true); //var sorted = from entry in CustomWorldMod.rainDbPacks orderby entry.Value.activated ascending select entry; //CustomWorldMod.rainDbPacks = CustomWorldMod.rainDbPacks.OrderBy(x => x.Value.activated).ToDictionary(x => x.Key, x => x.Value); Log($"Added fetched regions [{string.Join(", ", CustomWorldMod.rainDbPacks.Keys.ToArray())}]"); ready = false; readyToDelete = true; CustomWorldMod.UpdateLocalPackWithOnline(); CustomWorldMod.LoadThumbnails(); } } else { readyToDelete = true; Log(www.error, true); CustomWorldMod.OfflineMode = true; if (CustomWorldMod.OfflineMode) { Log("RUNNING CUSTOM REGIONS IN OFFLINE MODE"); CustomWorldMod.LoadThumbnails(); } } }
public override void Update() { if (url == null || readyToDelete) { readyToDelete = true; return; } if (string.IsNullOrEmpty(www.error)) { if (www.isDone && ready) { Log($"Fetching RainDB data... path [{url}]"); string file = www.text; List <object> json; try { json = file.listFromJson(); Dictionary <string, RegionPack> tempRainDb = new Dictionary <string, RegionPack>(); foreach (object jsonPack in json) { RegionPack regionPack = new RegionPack(""); //CustomWorldMod.Log($"Obtained data [{jsonPack.ToString()}]"); CustomWorldMod.FromDictionaryToPackInfo(jsonPack as Dictionary <string, object>, ref regionPack, authorative: true); try { regionPack.activated = true; //regionPack.activated = CustomWorldMod.installedRegionPacks.ContainsKey(regionPack.name); tempRainDb.Add(regionPack.name, regionPack); } catch (Exception e) { Log($"Exception when adding fetched region [{e}]", true); } } var date = DateTime.UtcNow.Date; var seed = date.Year * 1000 + date.DayOfYear; var random1 = new System.Random(seed); var seq = Enumerable.Range(0, tempRainDb.Count()).OrderBy(x => random1.Next()).Take(tempRainDb.Count()).ToList(); foreach (int item in seq) { KeyValuePair <string, RegionPack> tempItem = tempRainDb.ElementAt(item); CustomWorldMod.rainDbPacks.Add(tempItem.Key, tempItem.Value); } Log($"Added fetched regions [{string.Join(", ", CustomWorldMod.rainDbPacks.Keys.ToArray())}]"); } catch (Exception e) { Log("Error fetching regions " + e, true); } ready = false; readyToDelete = true; CustomWorldMod.UpdateLocalPackWithOnline(); CustomWorldMod.LoadThumbnails(); } } else { readyToDelete = true; Log(www.error, true); CustomWorldMod.OfflineMode = true; if (CustomWorldMod.OfflineMode) { Log("RUNNING CUSTOM REGIONS IN OFFLINE MODE"); CustomWorldMod.LoadThumbnails(); } } }
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)