Skip to content

Commit

Permalink
Some simplification for translations support code
Browse files Browse the repository at this point in the history
  • Loading branch information
Yuubari committed Jan 29, 2016
1 parent 05ce248 commit 4e74504
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 25 deletions.
52 changes: 34 additions & 18 deletions source/Grabacr07.KanColleWrapper/TranslationDataProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,10 @@
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.Serialization.Json;
using System.Web.Configuration;
using System.Xml.Linq;
using System.Xml.Serialization;
using Grabacr07.KanColleWrapper.Models;
using Grabacr07.KanColleWrapper.Models.Raw;
using Grabacr07.KanColleWrapper.Models.Translations;
using Livet;

namespace Grabacr07.KanColleWrapper
{
Expand All @@ -34,7 +29,7 @@ private set
currentCulture = value ?? CultureInfo.CurrentCulture.Name;
currentCulture = (currentCulture.StartsWith("en")) ? "en" : currentCulture;
currentCulture = (currentCulture.StartsWith("ja")) ? "ja" : currentCulture;
// If culture is set to "(auto)", check if we support the translations for current the system-wide culture.
// If culture is set to "(auto)", check if we support the translations for the current system-wide culture.
if ((value == null) && !IsCultureSupported(CultureInfo.CurrentCulture.Name)) currentCulture = "en";
}
}
Expand All @@ -53,12 +48,12 @@ static TranslationDataProvider()
CurrentCulture = KanColleClient.Current?.Settings?.Culture;
}

//private static string FilePath(TranslationProviderType type, string culture)
// => Path.Combine(translationsPath, culture, type.ToString() + ".xml");

private static string SerialisationPath(TranslationProviderType type, string culture)
=> Path.Combine(translationsPath, culture, type.ToString() + ".xml");

private static string SerialisationPath(TranslationProviderType type)
=> SerialisationPath(type, CurrentCulture);

/// <summary>
/// Changes culture and loads or re-loads translation files.
/// </summary>
Expand All @@ -74,7 +69,7 @@ public static void ChangeCulture(string culture)
Debug.WriteLine("TranslationDataProvider: got <" + culture + "> in a culture change request.");

CurrentCulture = culture;
LoadLocalTranslations(culture);
LoadLocalTranslations();
KanColleClient.Current.Translations.ChangeCulture();
KanColleClient.Current.Updater.ChangeCulture();
}
Expand All @@ -86,7 +81,7 @@ public static void ChangeCulture(string culture)
/// <param name="culture">Culture</param>
/// <param name="jsonBytes">JSON data</param>
/// <returns></returns>
public static bool LoadJson(TranslationProviderType type, string culture, byte[] jsonBytes)
private static bool LoadJson(TranslationProviderType type, string culture, byte[] jsonBytes)
{
Debug.WriteLine("TranslationDataProvider: Provider {0}: JSON parsing was requested for culture <{1}>.", type, culture);

Expand Down Expand Up @@ -121,7 +116,10 @@ public static bool LoadJson(TranslationProviderType type, string culture, byte[]
return false;
}

public static string Lookup(TranslationType type, string culture, object rawData)
public static bool LoadJson(TranslationProviderType type, byte[] jsonBytes)
=> LoadJson(type, CurrentCulture, jsonBytes);

private static string Lookup(TranslationType type, string culture, object rawData)
{
string lookupData;

Expand Down Expand Up @@ -152,15 +150,18 @@ public static string Lookup(TranslationType type, string culture, object rawData
return null;
}

var result = LookupInsideProvider(type, culture, lookupData);
var result = LookupInsideProvider(type, lookupData);
if ((result == null) && EnableSubmission)
{
ProcessUnknown?.Invoke(null, new ProcessUnknownEventArgs(TypeToProviderType(type), culture, rawData));
}
return result;
}

public static string LookupInsideProvider(TranslationType type, string culture, string key)
public static string Lookup(TranslationType type, object rawData)
=> Lookup(type, CurrentCulture, rawData);

private static string LookupInsideProvider(TranslationType type, string culture, string key)
{
var accessor = Tuple.Create(TypeToProviderType(type), culture);

Expand Down Expand Up @@ -201,19 +202,25 @@ public static string LookupInsideProvider(TranslationType type, string culture,
return !string.IsNullOrEmpty(result) ? result: null;
}

public static string Version(TranslationProviderType type, string culture)
public static string LookupInsideProvider(TranslationType type, string key)
=> LookupInsideProvider(type, CurrentCulture, key);

private static string Version(TranslationProviderType type, string culture)
{
var accessor = Tuple.Create(type, culture);
if (!translationSets.ContainsKey(accessor)) return null;
return translationSets[accessor]?.version;
}

public static string Version(TranslationProviderType type)
=> Version(type, CurrentCulture);

/// <summary>
/// Serialise data to local storage
/// </summary>
/// <param name="type"></param>
/// <param name="culture"></param>
public static void SaveXml(TranslationProviderType type, string culture)
private static void SaveXml(TranslationProviderType type, string culture)
{
if (culture.StartsWith("ja")) return;

Expand Down Expand Up @@ -252,7 +259,10 @@ public static void SaveXml(TranslationProviderType type, string culture)
}
}

public static bool LoadXml(TranslationProviderType type, string culture)
public static void SaveXml(TranslationProviderType type)
=> SaveXml(type, CurrentCulture);

private static bool LoadXml(TranslationProviderType type, string culture)
{
if (!File.Exists(SerialisationPath(type, culture)) || culture.StartsWith("ja"))
return false;
Expand Down Expand Up @@ -288,10 +298,13 @@ public static bool LoadXml(TranslationProviderType type, string culture)
}
}

public static bool LoadXml(TranslationProviderType type)
=> LoadXml(type, CurrentCulture);

/// <summary>
/// Deserialises translations from local storage. Should only be called during culture change requests.
/// </summary>
public static void LoadLocalTranslations(string culture)
private static void LoadLocalTranslations(string culture)
{
if (culture.StartsWith("ja")) return;

Expand All @@ -303,6 +316,9 @@ public static void LoadLocalTranslations(string culture)
}
}

public static void LoadLocalTranslations()
=> LoadLocalTranslations(CurrentCulture);

/// <summary>
/// Are translations for the given culture supported?
/// Currently a stub.
Expand Down
2 changes: 1 addition & 1 deletion source/Grabacr07.KanColleWrapper/Translations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public string Lookup(TranslationType type, object rawData)
return null;
}

return TranslationDataProvider.Lookup(type, CurrentCulture, rawData);
return TranslationDataProvider.Lookup(type, rawData);
}
}
}
20 changes: 14 additions & 6 deletions source/Grabacr07.KanColleWrapper/Updater.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ public class Updater : NotificationObject
/// </summary>
private const string apiVersion = "1";

private readonly TimeSpan updateCheckFrequency = new TimeSpan(0, 120, 0);

#if !DEBUG
private readonly TimeSpan submissionFrequency = new TimeSpan(0, 15, 0);
#else
private readonly TimeSpan submissionFrequency = new TimeSpan(0, 1, 0);
#endif

/// <summary>
/// URL for version checks.
/// </summary>
Expand Down Expand Up @@ -87,10 +95,10 @@ public Updater(string apiurl, string culture)
TranslationDataProvider.ProcessUnknown += ProcessUnknown;

this.updateCheckTimer.Tick += this.dispatcherTimerHandler;
this.updateCheckTimer.Interval = new TimeSpan(0, 120, 0);
this.updateCheckTimer.Interval = updateCheckFrequency;

this.autosubmitTimer.Tick += this.autosubmitTimerHandler;
this.autosubmitTimer.Interval = new TimeSpan(0, 15, 0);
this.autosubmitTimer.Interval = submissionFrequency;

this.apiVersionCheckUrl = apiurl;
this.ChangeCulture();
Expand Down Expand Up @@ -204,8 +212,8 @@ private void UpdateAsNeeded()
{
if (!this.IsUpToDate(version.Key))
{
Debug.WriteLine("Updater: {0} needs update; local version: {1}, remote: {2}.", version.Key, (version.Key != TranslationProviderType.App) ? TranslationDataProvider.Version(version.Key, CurrentCulture) : Assembly.GetEntryAssembly().GetName().Version.ToString(), string.IsNullOrEmpty(version.Value) ? "N/A" : version.Value);
if ((version.Key != TranslationProviderType.App) && this.FetchTranslations(version.Key)) TranslationDataProvider.SaveXml(version.Key, CurrentCulture);
Debug.WriteLine("Updater: {0} needs update; local version: {1}, remote: {2}.", version.Key, (version.Key != TranslationProviderType.App) ? TranslationDataProvider.Version(version.Key) : Assembly.GetEntryAssembly().GetName().Version.ToString(), string.IsNullOrEmpty(version.Value) ? "N/A" : version.Value);
if ((version.Key != TranslationProviderType.App) && this.FetchTranslations(version.Key)) TranslationDataProvider.SaveXml(version.Key);
}
}
}
Expand All @@ -232,7 +240,7 @@ public bool IsUpToDate(TranslationProviderType type)
int verRemote, verLocal;

if (!int.TryParse(versions[type], out verRemote)) return true;
if (!int.TryParse(TranslationDataProvider.Version(type, CurrentCulture), out verLocal)) return false;
if (!int.TryParse(TranslationDataProvider.Version(type), out verLocal)) return false;

return (verRemote <= verLocal);
}
Expand Down Expand Up @@ -267,7 +275,7 @@ private bool FetchTranslations(TranslationProviderType type)
return false;
}

return TranslationDataProvider.LoadJson(type, CurrentCulture, responseBytes);
return TranslationDataProvider.LoadJson(type, responseBytes);
}
}

Expand Down

0 comments on commit 4e74504

Please sign in to comment.