Skip to content

Commit

Permalink
Added extra libretro NES.dat data.
Browse files Browse the repository at this point in the history
  • Loading branch information
KirovAir committed Sep 26, 2019
1 parent d90c15c commit 6ff6a12
Show file tree
Hide file tree
Showing 6 changed files with 172 additions and 133 deletions.
5 changes: 5 additions & 0 deletions TwilightBoxart/BoxartConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,5 +126,10 @@ public string GetBoxartPath(string root = "")
// SNES
{ConsoleType.SuperNintendoEntertainmentSystem, new Size(158, 115)}
};

public static Dictionary<ConsoleType, string> LibRetroDatUrls = new Dictionary<ConsoleType, string>
{
{ConsoleType.NintendoEntertainmentSystem, "https://github.com/libretro/libretro-database/raw/master/dat/Nintendo%20-%20Nintendo%20Entertainment%20System.dat"}
};
}
}
229 changes: 119 additions & 110 deletions TwilightBoxart/Crawlers/LibRetro/LibRetroDat.cs
Original file line number Diff line number Diff line change
@@ -1,118 +1,127 @@
//using System;
//using System.Collections.Generic;
//using System.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;

//namespace TwilightBoxart.Crawlers.LibRetro
//{
// // Currently unused because of no-intro sources. :)
// public static class LibRetroDat
// {
// /// <summary>
// /// Parses a libretro .dat file. The code is horrible and so is the format ..
// /// y no standards libretro?
// /// </summary>
// /// <typeparam name="T"></typeparam>
// /// <param name="data"></param>
// /// <param name="root"></param>
// /// <returns></returns>
// public static List<T> ParseDat<T>(string data, string root)
// {
// var result = new List<T>();
// var split = new List<string>();
// var instr = false;
// var value = "";
namespace TwilightBoxart.Crawlers.LibRetro
{
public static class LibRetroDat
{
public static List<LibRetroRomData> DownloadDat(string url)
{
using (var wc = new WebClient())
{
var data = wc.DownloadString(url);
return ParseDat<LibRetroRomData>(data, "game");
}
}

// // Remove whitespace and shit. Just get the values.
// foreach (var c in data)
// {
// if (c == '"')
// {
// instr = !instr;
// continue;
// }
// if (!instr && c == ' ' || c == '\t' || c == '\n')
// {
// if (value != "")
// {
// split.Add(value);
// value = "";
// }
// continue;
// }
// value += c;
// }
/// <summary>
/// Parses a libretro .dat file. The code is horrible and so is the format ..
/// y no standards libretro?
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="data"></param>
/// <param name="root"></param>
/// <returns></returns>
public static List<T> ParseDat<T>(string data, string root)
{
var result = new List<T>();
var split = new List<string>();
var instr = false;
var value = "";

// // Parse them.
// var skip = false;
// var indent = 0;
// for (int i = 0; i < split.Count; i++)
// {
// if (!skip)
// {
// if (split[i].ToLower() == root.ToLower())
// {
// i += 2;
// var entity = Activator.CreateInstance<T>();
// ListToObj(entity, split, "", ref i);
// result.Add(entity);
// continue;
// }
// else
// {
// skip = true;
// continue;
// }
// }
// if (split[i] == "(")
// {
// indent++;
// }
// if (split[i] == ")")
// {
// indent--;
// }
// if (skip && indent == 0)
// {
// skip = false;
// }
// }
// Remove whitespace and shit. Just get the values.
foreach (var c in data)
{
if (c == '"')
{
instr = !instr;
continue;
}
if (!instr && c == ' ' || c == '\t' || c == '\n')
{
if (value != "")
{
split.Add(value);
value = "";
}
continue;
}
value += c;
}

// return result;
// }
// Parse them.
var skip = false;
var indent = 0;
for (int i = 0; i < split.Count; i++)
{
if (!skip)
{
if (split[i].ToLower() == root.ToLower())
{
i += 2;
var entity = Activator.CreateInstance<T>();
ListToObj(entity, split, "", ref i);
result.Add(entity);
continue;
}
else
{
skip = true;
continue;
}
}
if (split[i] == "(")
{
indent++;
}
if (split[i] == ")")
{
indent--;
}
if (skip && indent == 0)
{
skip = false;
}
}

// public static void ListToObj<T>(T obj, List<string> values, string key, ref int index)
// {
// var properties = obj.GetType().GetProperties();
// var currentKey = "";
return result;
}

// for (int i = index; i < values.Count; i++)
// {
// var value = values[i];
public static void ListToObj<T>(T obj, List<string> values, string key, ref int index)
{
var properties = obj.GetType().GetProperties();
var currentKey = "";

// if (value == "(")
// {
// i++;
// ListToObj(obj, values, currentKey, ref i);
// continue;
// }
// if (value == ")")
// {
// index = i++;
// return;
// }
// if (currentKey == "")
// {
// currentKey = key + value;
// }
// else
// {
// var property = properties.FirstOrDefault(c => c.Name.ToLower() == currentKey.ToLower());
// if (property != null)
// property.SetValue(obj, value);
for (int i = index; i < values.Count; i++)
{
var value = values[i];

// currentKey = "";
// }
// }
// }
// }
//}
if (value == "(")
{
i++;
ListToObj(obj, values, currentKey, ref i);
continue;
}
if (value == ")")
{
index = i++;
return;
}
if (currentKey == "")
{
currentKey = key + value;
}
else
{
var property = properties.FirstOrDefault(c => c.Name.ToLower() == currentKey.ToLower());
if (property != null)
property.SetValue(obj, value);

currentKey = "";
}
}
}
}
}
23 changes: 23 additions & 0 deletions TwilightBoxart/Crawlers/LibRetro/LibRetroRomData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
namespace TwilightBoxart.Crawlers.LibRetro
{
// game (
// name "2 Game Pack! - Hot Wheels - Stunt Track Challenge + Hot Wheels - World Race (USA, Europe)"
// description "2 Game Pack! - Hot Wheels - Stunt Track Challenge + Hot Wheels - World Race (USA, Europe)"
// rom (name "2 Game Pack! - Hot Wheels - Stunt Track Challenge + Hot Wheels - World Race (USA, Europe).gba" size 16777216 crc 20929EC1 md5 4DAF3D378D5F91277F43A5555829FDC7 sha1 717B2A739C8932374AB48A9C2BBD76A44B4CF2F3 )
// )
public class LibRetroRomData
{
public string Name { get; set; }
public string Description { get; set; }
public string RomName { get; set; }
public string RomSize { get; set; }
public string RomCrc { get; set; }
public string RomMd5 { get; set; }
public string RomSha1 { get; set; }

public override string ToString()
{
return $"{Name} - {RomSize}";
}
}
}
23 changes: 0 additions & 23 deletions TwilightBoxart/Crawlers/LibRetro/RomData.cs

This file was deleted.

25 changes: 25 additions & 0 deletions TwilightBoxart/Data/RomDatabase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Linq;
using KirovAir.Core.Extensions;
using KirovAir.Core.Utilities;
using TwilightBoxart.Crawlers.LibRetro;
using TwilightBoxart.Crawlers.NoIntro;
using TwilightBoxart.Models.Base;
using Utf8Json;
Expand Down Expand Up @@ -71,6 +72,30 @@ public void Initialize(IProgress<string> progress = null)
progress?.Report($"Found {data.Game.Count} roms");
}

progress?.Report("Downloading extra LibRetro data..");

foreach (var map in BoxartConfig.LibRetroDatUrls)
{
progress?.Report($"{map.Key.GetDescription()}.. ");

List<LibRetroRomData> data = null;
_retryHelper.RetryOnException(() => { data = LibRetroDat.DownloadDat(map.Value); });

foreach (var game in data)
{
var rom = new RomMetaData
{
ConsoleType = map.Key,
ConsoleSubType = map.Key,
Name = game.Name,
Sha1 = game.RomSha1.ToLower()
};
_roms.Add(rom);
}

progress?.Report($"Found {data.Count} roms");
}

progress?.Report("Flushing data..");
using (var ms = new MemoryStream())
{
Expand Down
Binary file modified TwilightBoxart/NoIntro.db
Binary file not shown.

0 comments on commit 6ff6a12

Please sign in to comment.