-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
it's starting to grow (like my cock)
- Loading branch information
Showing
5 changed files
with
618 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
using Bluscream; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using System.Web; | ||
|
||
namespace PCGWMetaData.Classes | ||
{ | ||
public class Cache | ||
{ | ||
internal List<Game> games; | ||
internal DirectoryInfo cacheDir; | ||
internal PCGWMetaDataPlugin plugin; | ||
|
||
public Cache(PCGWMetaDataPlugin plugin) | ||
{ | ||
this.plugin = plugin; | ||
cacheDir = new DirectoryInfo(plugin.GetPluginUserDataPath()).Combine("cache"); | ||
cacheDir.Create(); | ||
games = new List<Game>(); | ||
refresh(); | ||
} | ||
|
||
public List<Game> refresh() | ||
{ | ||
games.Clear(); | ||
foreach (var file in cacheDir.EnumerateFiles("*.json", SearchOption.TopDirectoryOnly)) | ||
{ | ||
games.Add(new Game(file.FileNameWithoutExtension(), this)); | ||
} | ||
// games = .Select(f => f.FileNameWithoutExtension()).ToList(); | ||
return games; | ||
} | ||
|
||
private Game addGame(string name) | ||
{ | ||
var game = new Game(name, this); | ||
var json = plugin.webClient.DownloadString(string.Format(PCGWMetaDataPlugin.url_base, game.EncodedName())); | ||
game.File().WriteAllText(json); | ||
games.Add(game); | ||
return game; | ||
} | ||
|
||
private Game _getGame(string name) | ||
{ | ||
return games.FirstOrDefault(g => g.Name == name.ToLowerInvariant()); | ||
} | ||
|
||
public Game getGame(string name) | ||
{ | ||
var game = _getGame(name); | ||
if (game is null) addGame(name); | ||
return game; | ||
} | ||
|
||
public void Purge() | ||
{ | ||
foreach (var file in cacheDir.EnumerateFiles()) | ||
file.Delete(); | ||
refresh(); | ||
} | ||
|
||
public void PurgeOutdated() | ||
{ | ||
foreach (var game in games) | ||
{ | ||
if (game.isOutdated()) | ||
game.File().Delete(); | ||
} | ||
refresh(); | ||
} | ||
} | ||
|
||
public class Game | ||
{ | ||
public string Name; | ||
private Cache _cache; | ||
|
||
public Game(string Name, Cache _cache) | ||
{ | ||
this.Name = Name.ToLowerInvariant(); this._cache = _cache; | ||
} | ||
|
||
public FileInfo File() | ||
{ | ||
return _cache.cacheDir.CombineFile(string.Format("{0}.json", this.EncodedName())); | ||
} | ||
|
||
public ApiResult Data() | ||
{ | ||
return Newtonsoft.Json.JsonConvert.DeserializeObject<ApiResult>(File().ReadAllText()); | ||
} | ||
|
||
public string EncodedName() => HttpUtility.UrlEncode(this.Name); | ||
// public string DecodedName() => HttpUtility.HtmlDecode(this.Name); | ||
|
||
public bool isOutdated() | ||
{ | ||
return DateTime.Now - System.IO.File.GetLastWriteTime(File().FullName) > TimeSpan.FromDays(30); // option | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.