Skip to content

Commit

Permalink
download json asynchronously and show crash reporter if failed
Browse files Browse the repository at this point in the history
  • Loading branch information
g3gg0 committed Feb 12, 2024
1 parent aad7d88 commit b71bb9c
Showing 1 changed file with 110 additions and 65 deletions.
175 changes: 110 additions & 65 deletions TeddyBench/TeddyMain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,11 @@ public partial class TeddyMain : Form
private string CurrentDirectory = null;
private bool AutoOpenDrive = true;
private Dictionary<ListViewTag, ListViewItem> RegisteredItems = new Dictionary<ListViewTag, ListViewItem>();
private static TonieTools.TonieData[] TonieInfos;
private static Dictionary<string, string> CustomTonies = new Dictionary<string, string>();

private static object TonieInfoLock = new object();
private static TonieTools.TonieData[] TonieInfo = new TonieTools.TonieData[0];
private static Dictionary<string, string> TonieInfoCustom = new Dictionary<string, string>();

private Dictionary<string, Tuple<TonieAudio, DateTime>> CachedAudios = new Dictionary<string, Tuple<TonieAudio, DateTime>>();
private ListViewItem LastSelectediItem = null;
private RfidReaderBase RfidReader;
Expand Down Expand Up @@ -149,29 +152,34 @@ public void LoadJson(bool force = false)
}
catch (Exception e)
{
ReportException("Downloader Thread", e);
}
}

try
{
TonieInfos = JsonConvert.DeserializeObject<TonieTools.TonieData[]>(File.ReadAllText("tonies.json"));
}
catch (Exception e)
lock (TonieInfoLock)
{
TonieInfos = new TonieTools.TonieData[0];
}
try
{
TonieInfo = JsonConvert.DeserializeObject<TonieTools.TonieData[]>(File.ReadAllText("tonies.json"));
}
catch (Exception e)
{
TonieInfo = new TonieTools.TonieData[0];
ReportException("Parse tonies.json", e);
}

try
{
CustomTonies = JsonConvert.DeserializeObject<Dictionary<string, string>>(File.ReadAllText("customTonies.json"));
}
catch(FileNotFoundException e)
{
CustomTonies = new Dictionary<string, string>();
}
catch (Exception e)
{
CustomTonies = new Dictionary<string, string>();
try
{
TonieInfoCustom = JsonConvert.DeserializeObject<Dictionary<string, string>>(File.ReadAllText("customTonies.json"));
}
catch (FileNotFoundException e)
{
TonieInfoCustom.Clear();
}
catch (Exception e)
{
TonieInfoCustom.Clear();
}
}
}
catch (Exception e)
Expand All @@ -182,13 +190,16 @@ public void LoadJson(bool force = false)

void SaveJson()
{
try
lock (TonieInfoLock)
{
File.WriteAllText("customTonies.json", JsonConvert.SerializeObject(CustomTonies, Formatting.Indented));
}
catch (Exception e)
{
return;
try
{
File.WriteAllText("customTonies.json", JsonConvert.SerializeObject(TonieInfoCustom, Formatting.Indented));
}
catch (Exception e)
{
return;
}
}
}

Expand All @@ -209,7 +220,7 @@ public TeddyMain()
cmbSorting.SelectedIndex = 2;
Text = TitleString;

LoadJson();

StartThreads();

UpdateCheckThread = new SafeThread(UpdateCheck, "UpdateCheckThread");
Expand Down Expand Up @@ -362,13 +373,16 @@ private void UpdateStatusBar()
text += " | " + RfidReader.HardwareType + " (FW: " + (RfidReader.UnlockSupported ? "SLIX-L enabled" : "stock") + voltageString + ") found at " + RfidReader.CurrentPort + ". The UID of the tag will be automatically used where applicable.";
}

if(TonieInfos.Length == 0)
{
text += " | No tonies.json, consider downloading it";
}
else
lock (TonieInfoLock)
{
text += " | tonies.json has " + TonieInfos.Length + " entries";
if (TonieInfo.Length == 0)
{
text += " | No tonies.json, consider downloading it";
}
else
{
text += " | tonies.json has " + TonieInfo.Length + " entries";
}
}

if (text == "")
Expand Down Expand Up @@ -591,6 +605,12 @@ private void StartThreads()
ScanCardThreadStop = false;
ScanCardThread = new SafeThread(ScanCardMain, "ScanCardThread");
ScanCardThread.Start();


new SafeThread(() =>
{
LoadJson();
}, "JSON Downloader").Start();
}

private void ScanCardMain()
Expand Down Expand Up @@ -846,7 +866,12 @@ private void AnalyzeMain()
LogWindow.Log(LogWindow.eLogLevel.DebugVerbose, "GetTonieAudio... done");
string image = "";
string hash = BitConverter.ToString(dumpFile.Header.Hash).Replace("-", "");
var found = TonieInfos.Where(t => t.Hash.Where(h => h == hash).Count() > 0);
IEnumerable<TonieTools.TonieData> found = new TonieTools.TonieData[0];

lock (TonieInfoLock)
{
found = TonieInfo.Where(t => t.Hash.Where(h => h == hash).Count() > 0);
}
string tonieName = "[" + tag.Uid + "]" + Environment.NewLine + "(unknown: " + dumpFile.Header.AudioId.ToString("X8") + ")";

bool update = false;
Expand All @@ -862,10 +887,13 @@ private void AnalyzeMain()
{
tonieName = info.Model + " - " + tonieName;
}
if (CustomTonies.ContainsKey(hash))
lock (TonieInfoLock)
{
LogWindow.Log(LogWindow.eLogLevel.DebugVerbose, " known tonie, overriding name");
tonieName = CustomTonies[hash];
if (TonieInfoCustom.ContainsKey(hash))
{
LogWindow.Log(LogWindow.eLogLevel.DebugVerbose, " known tonie, overriding name");
tonieName = TonieInfoCustom[hash];
}
}
if (!string.IsNullOrEmpty(info.Pic) && !lstTonies.LargeImageList.Images.ContainsKey(hash))
{
Expand All @@ -886,25 +914,28 @@ private void AnalyzeMain()
tag.Info = new TonieTools.TonieData();
image = "unknown";

if (CustomTonies.ContainsKey(hash))
lock (TonieInfoLock)
{
LogWindow.Log(LogWindow.eLogLevel.DebugVerbose, " known custom tonie");
tonieName = CustomTonies[hash];
tag.Info.Title = tonieName;
image = "custom";
update = true;
}
else if (dumpFile.Header.AudioId < 0x50000000)
{
LogWindow.Log(LogWindow.eLogLevel.DebugVerbose, " unknown custom tonie");
tonieName = "Unnamed Teddy - " + tonieName;
tag.Info.Title = "Unnamed Teddy";
image = "custom";
update = true;
}
else
{
LogWindow.Log(LogWindow.eLogLevel.DebugVerbose, " Not found -> unknown");
if (TonieInfoCustom.ContainsKey(hash))
{
LogWindow.Log(LogWindow.eLogLevel.DebugVerbose, " known custom tonie");
tonieName = TonieInfoCustom[hash];
tag.Info.Title = tonieName;
image = "custom";
update = true;
}
else if (dumpFile.Header.AudioId < 0x50000000)
{
LogWindow.Log(LogWindow.eLogLevel.DebugVerbose, " unknown custom tonie");
tonieName = "Unnamed Teddy - " + tonieName;
tag.Info.Title = "Unnamed Teddy";
image = "custom";
update = true;
}
else
{
LogWindow.Log(LogWindow.eLogLevel.DebugVerbose, " Not found -> unknown");
}
}
}

Expand Down Expand Up @@ -1400,11 +1431,14 @@ private void lstTonies_AfterLabelEdit(object sender, LabelEditEventArgs e)
return;
}

if (!CustomTonies.ContainsKey(tag.Hash))
lock (TonieInfoLock)
{
CustomTonies.Add(tag.Hash, "");
if (!TonieInfoCustom.ContainsKey(tag.Hash))
{
TonieInfoCustom.Add(tag.Hash, "");
}
TonieInfoCustom[tag.Hash] = e.Label;
}
CustomTonies[tag.Hash] = e.Label;

SaveJson();
}
Expand Down Expand Up @@ -1576,7 +1610,12 @@ private void SaveSelected()
tags.Add("TeddyFile=" + file);

string hashString = BitConverter.ToString(dump.Header.Hash).Replace("-", "");
var found = TonieInfos.Where(t => t.Hash.Contains(hashString));
IEnumerable<TonieTools.TonieData> found = new TonieTools.TonieData[0];

lock (TonieInfoLock)
{
found = TonieInfo.Where(t => t.Hash.Contains(hashString));
}
TonieTools.TonieData info = null;

if (found.Count() > 0)
Expand Down Expand Up @@ -1949,12 +1988,15 @@ private async Task<DiagStatus> DiagnosticsSendInfo(string payload, string sender

private void AddInfo(StringBuilder str, ListViewTag tag)
{
string customName = null;
if (CustomTonies.ContainsKey(tag.Hash))
lock (TonieInfoLock)
{
customName = CustomTonies[tag.Hash];
string customName = null;
if (TonieInfoCustom.ContainsKey(tag.Hash))
{
customName = TonieInfoCustom[tag.Hash];
}
TonieTools.DumpInfo(str, TonieTools.eDumpFormat.FormatText, tag.FileName, TonieInfo, customName);
}
TonieTools.DumpInfo(str, TonieTools.eDumpFormat.FormatText, tag.FileName, TonieInfos, customName);
}

private async void readContentToolStripMenuItem_Click(object sender, EventArgs e)
Expand Down Expand Up @@ -2350,13 +2392,16 @@ internal void ReportException(string name, Exception e)

private void downloadToniesjsonOnStartupToolStripMenuItem_Click(object sender, EventArgs e)
{
Settings.DownloadJson = downloadToniesjsonOnStartupToolStripMenuItem.Checked;
Settings.DownloadJson = !downloadToniesjsonOnStartupToolStripMenuItem.Checked;
SaveSettings();
}

private void downloadToniesjsonNowToolStripMenuItem_Click(object sender, EventArgs e)
{
LoadJson(true);
new SafeThread(() =>
{
LoadJson(true);
}, "JSON Downloader").Start();
}
}
}

0 comments on commit b71bb9c

Please sign in to comment.