Skip to content

Instantly share code, notes, and snippets.

@TheBuzzSaw
Created September 24, 2017 06:45
Show Gist options
  • Select an option

  • Save TheBuzzSaw/68368d1090469c6d390ccc144e096436 to your computer and use it in GitHub Desktop.

Select an option

Save TheBuzzSaw/68368d1090469c6d390ccc144e096436 to your computer and use it in GitHub Desktop.
Takes card name and filters it down to proper image title
using System;
using System.Linq;
using System.Text;
using System.IO;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace TheGreatRename
{
class Program
{
static readonly Encoding TheEncoding = new UTF8Encoding(false);
static bool IsGood(char c)
{
return c != ',' && c != '\'' && c != '!' && c != '"' && c != '?' && c != ':' && c != '.';
}
static string Sanitize(string text)
{
text = text.Replace('&', '_');
int index = text.IndexOf(" (");
if (index >= 0)
text = text.Substring(0, index);
return new string(text.Where(IsGood).ToArray());
}
static void ReadFile(string path)
{
Console.WriteLine("Loading " + path);
var text = File.ReadAllText(path, TheEncoding);
Console.WriteLine("Parsing " + path);
var array = JArray.Parse(text);
Console.WriteLine("Processing " + path);
int cardCount = 0;
int imageCount = 0;
using (var streamWriter = new StreamWriter(path + ".processed.json", false, TheEncoding))
using (var jsonWriter = new JsonTextWriter(streamWriter))
{
jsonWriter.Formatting = Formatting.Indented;
jsonWriter.WriteStartArray();
foreach (var token in array)
{
int loopCount = 1;
var cardType = (string)token["CardType"];
if (cardType == "Objective")
loopCount = 2;
for (int i = 0; i < loopCount; ++i)
{
++cardCount;
jsonWriter.WriteStartObject();
jsonWriter.WritePropertyName("Id");
jsonWriter.WriteValue((string)token["id"]);
string cardName;
if (loopCount > 1)
{
cardName = (string)token[i == 0 ? "ObjectiveFrontName" : "ObjectiveBackName"];
}
else
{
cardName = (string)token["CardName"];
}
jsonWriter.WritePropertyName("CardName");
jsonWriter.WriteValue(cardName);
var grouping = (string)token["Grouping"];
jsonWriter.WritePropertyName("Grouping");
jsonWriter.WriteValue(grouping);
var expansion = (string)token["Expansion"];
jsonWriter.WritePropertyName("Expansion");
jsonWriter.WriteValue(expansion);
var proposedPath = Path.Combine(
"images",
Sanitize(expansion) + " " + grouping,
Sanitize(cardName) + ".tif");
jsonWriter.WritePropertyName("ProposedPath");
jsonWriter.WriteValue(proposedPath);
var truePath = Path.Combine("..", proposedPath);
var exists = File.Exists(truePath);
jsonWriter.WritePropertyName("Exists");
jsonWriter.WriteValue(exists);
if (exists)
++imageCount;
jsonWriter.WriteEndObject();
}
}
jsonWriter.WriteEndArray();
}
Console.WriteLine("Checked " + cardCount + " cards.");
Console.WriteLine("Found " + imageCount + " images.");
Console.WriteLine("Unable to find " + (cardCount - imageCount) + " images.");
}
static void Main(string[] args)
{
try
{
foreach (var arg in args)
ReadFile(arg);
}
catch (Exception e)
{
Console.WriteLine(e.GetType());
Console.WriteLine(e.Message);
Console.WriteLine(e.StackTrace);
}
finally
{
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment