Skip to content

Commit

Permalink
Cache mod_ids_to_names.json
Browse files Browse the repository at this point in the history
  • Loading branch information
maddie480 committed Aug 26, 2024
1 parent 2fa703a commit 7b1cad4
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 26 deletions.
3 changes: 1 addition & 2 deletions changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,4 @@ and only contains the latest changes.
Its purpose is to be shown in Olympus when updating.

#changelog#
∙ Improved Celeste install detection to scan more Steam library folders
∙ Added mod names from GameBanana in the "Manage Installed Mods" screen
∙ Manage Installed Mods screen: download mod name list and cache it to disk so that it doesn't affect the screen's loading time
58 changes: 58 additions & 0 deletions sharp/CmdGetModIdToNameMap.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;

namespace Olympus {
public class CmdGetModIdToNameMap : Cmd<string, bool> {
public override bool Taskable => true;

private static string cacheLocation;

public override bool Run(string cacheLocation) {
CmdGetModIdToNameMap.cacheLocation = cacheLocation;
Console.Error.WriteLine($"[CmdGetIdToNameMap] Cache location set to: {cacheLocation}");
GetModIDsToNamesMap(ignoreCache: true);
return true;
}

private static readonly object locker = new object();

internal static Dictionary<string, string> GetModIDsToNamesMap(bool ignoreCache = false) {
if (!ignoreCache && File.Exists(cacheLocation)) {
Console.Error.WriteLine($"[CmdGetIdToNameMap] Loading mod IDs from {cacheLocation}");
lock (locker) {
using (Stream inputStream = new FileStream(cacheLocation, FileMode.Open)) {
Dictionary<string, string> map = getModIDsToNamesMap(inputStream);
if (map.Count > 0) return map;
}
}
}

using (HttpWebResponse res = Connect("https://maddie480.ovh/celeste/mod_ids_to_names.json"))
using (Stream inputStream = res.GetResponseStream()) {
Console.Error.WriteLine($"[CmdGetIdToNameMap] Loading mod IDs from maddie480.ovh");
Dictionary<string, string> map = getModIDsToNamesMap(inputStream);
lock (locker) {
if (map.Count > 0) File.WriteAllText(cacheLocation, JsonConvert.SerializeObject(map));
return map;
}
}
}

private static Dictionary<string, string> getModIDsToNamesMap(Stream inputStream) {
try {
using (TextReader textReader = new StreamReader(inputStream, Encoding.UTF8))
using (JsonTextReader jsonTextReader = new JsonTextReader(textReader)) {
return new JsonSerializer().Deserialize<Dictionary<string, string>>(jsonTextReader);
}
} catch (Exception e) {
Console.Error.WriteLine("Error loading mod IDs to names list");
Console.Error.WriteLine(e);
return new Dictionary<string, string>();
}
}
}
}
19 changes: 1 addition & 18 deletions sharp/CmdModList.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
using Newtonsoft.Json;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Net;
using System.Security.Cryptography;
using System.Text;
using YYProject.XXHash;

namespace Olympus {
Expand Down Expand Up @@ -35,7 +32,7 @@ public override IEnumerator Run(string root, bool readYamls, bool computeHashes,
updaterBlacklist = new List<string>();

Dictionary<string, string> modIDsToNamesMap = null;
if (readYamls) modIDsToNamesMap = getModIDsToNamesMap();
if (readYamls) modIDsToNamesMap = CmdGetModIdToNameMap.GetModIDsToNamesMap();

if (!onlyUpdatable) {
// === mod directories
Expand Down Expand Up @@ -136,20 +133,6 @@ public override IEnumerator Run(string root, bool readYamls, bool computeHashes,
}
}

private static Dictionary<string, string> getModIDsToNamesMap() {
try {
using (HttpWebResponse res = Connect("https://maddie480.ovh/celeste/mod_ids_to_names.json"))
using (TextReader textReader = new StreamReader(res.GetResponseStream(), Encoding.UTF8))
using (JsonTextReader jsonTextReader = new JsonTextReader(textReader)) {
return new JsonSerializer().Deserialize<Dictionary<string, string>>(jsonTextReader);
}
} catch (Exception e) {
Console.Error.WriteLine("Error loading mod IDs to names list");
Console.Error.WriteLine(e);
return new Dictionary<string, string>();
}
}

public class ModInfo {
public string Path;
public string Hash;
Expand Down
7 changes: 1 addition & 6 deletions src/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -203,12 +203,7 @@ function love.load(args)
love.window.showMessageBox("Olympus.Sharp Startup Error", "Failed loading Olympus.Sharp: " .. tostring(sharpError), "error")
else
threader.routine(function()
for i = 1, 4 do
for j = 1, 10 do
sharp.echo("warmup " .. tostring(i) .. " " .. tostring(j)):result()
end
threader.sleep(0.01)
end
sharp.getModIdToNameMap(fs.joinpath(fs.getStorageDir(), "cached-mod-ids-to-names.json"))
end)
end

Expand Down

0 comments on commit 7b1cad4

Please sign in to comment.