From beadb83d25baedfad242f2845e3baaa6e12fd525 Mon Sep 17 00:00:00 2001 From: Mateus Pires Date: Mon, 17 Oct 2016 18:40:26 -0200 Subject: [PATCH] Dictionary request done --- Assets/Scenes/Main.unity | 11 +++++++++++ Assets/Scripts/Animation List/Dictionary.cs | 199 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Assets/Scripts/Animation List/Dictionary.cs.meta | 12 ++++++++++++ Assets/Scripts/Animation List/ListManager.cs | 17 +++++++++-------- Assets/Scripts/Player Manager/GenericPlayerManager.cs | 2 +- Assets/Scripts/Player Manager/PlayerLogger.cs | 2 +- Assets/Scripts/Player Manager/Utils.cs | 41 +++++++++++++++++++++++++++++++++++++++-- Assets/Scripts/PlayerManager.cs | 103 +++++++++++++------------------------------------------------------------------------------------------ Assets/Scripts/Trie/Trie.cs | 11 ++++------- Assets/Scripts/Utils.cs | 12 ++++++++++++ Assets/Scripts/Utils.cs.meta | 12 ++++++++++++ Assets/Scripts/VisualLogger.cs | 3 ++- 12 files changed, 315 insertions(+), 110 deletions(-) create mode 100644 Assets/Scripts/Animation List/Dictionary.cs create mode 100644 Assets/Scripts/Animation List/Dictionary.cs.meta create mode 100644 Assets/Scripts/Utils.cs create mode 100644 Assets/Scripts/Utils.cs.meta diff --git a/Assets/Scenes/Main.unity b/Assets/Scenes/Main.unity index 5bc8aee..a7ceb6b 100644 --- a/Assets/Scenes/Main.unity +++ b/Assets/Scenes/Main.unity @@ -3546,6 +3546,17 @@ MonoBehaviour: contentPanel: {fileID: 1995982559} scrollView: {fileID: 2128190448} input: {fileID: 168747182} +--- !u!114 &578311384 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_PrefabParentObject: {fileID: 0} + m_PrefabInternal: {fileID: 0} + m_GameObject: {fileID: 578311380} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 87b9a8a309b780346a49c0d7ad7baecb, type: 3} + m_Name: + m_EditorClassIdentifier: --- !u!1 &582478958 GameObject: m_ObjectHideFlags: 0 diff --git a/Assets/Scripts/Animation List/Dictionary.cs b/Assets/Scripts/Animation List/Dictionary.cs new file mode 100644 index 0000000..28be5c2 --- /dev/null +++ b/Assets/Scripts/Animation List/Dictionary.cs @@ -0,0 +1,199 @@ +using LAViD.Structures; +using LAViD.Unity.Utils; +using LAViD.Utils; +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.IO; +using System.Linq; +using System.Text; +using System.Text.RegularExpressions; +using System.Threading; +using UnityEngine; + +namespace LAViD.VLibras.Dictionary +{ + public class Dictionary : MonoBehaviour + { + private readonly string DefaultDictionary = "{}"; // "{\"keys\": {}, \"trie\": {}, \"characters\": []}"; + private readonly char[] CharactersSigns = new char[] { + 'A', 'B', 'C', 'Ç', 'D', 'E', 'F', 'G', 'H', 'I', + 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', + 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', + '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' + }; + + private string path; + private string versionPath; + + private string version = "0.0.0"; + private Trie signs = null; + private volatile bool ready = false; + private event Events.OnLoad onLoadListeners; + + public void AddOnLoadListener(Events.OnLoad onLoad) + { + PlayerLogger.Log("D", "AOLL", "Registering onLoad event."); + + if (ready) + { + PlayerLogger.Log("D", "AOLL", "Ready. Calling onLoad event directly."); + onLoad(); + } + else this.onLoadListeners += onLoad; + } + + public Trie Signs + { + get { return ready ? this.signs : null; } + } + + private void Start() + { + this.path = Application.persistentDataPath + "/dictionary.json"; + this.versionPath = Application.persistentDataPath + "/dictionary_version.txt"; + + PlayerLogger.Log("D", "S", "Dictionary path: " + this.path); + PlayerLogger.Log("D", "S", "Dictionary version path: " + this.versionPath); + + if (!File.Exists(this.versionPath)) + { + File.WriteAllText(this.versionPath, version); + save(DefaultDictionary); + } + else this.version = File.ReadAllText(this.versionPath); + + StartCoroutine(Methods.WaitForResponse( + new WWW("http://atualizacao.vlibras.lavid.ufpb.br/version/dict"), + 10f, + versionRequestSuccess, + delegate (WWW response) + { + PlayerLogger.Log("D", "S", "Connection error. Loading local dictionary version " + this.version + "."); + + if (response.error == null) + PlayerLogger.Log("D", "S", "Timeout."); + else + PlayerLogger.Log("D", "S", "WWW error: " + response.error); + + load(read()); + } + )); + } + + private void Update() + { + if (ready) + { + PlayerLogger.Log("D", "AOLL", "Ready. Calling onLoad events."); + + this.onLoadListeners(); + this.gameObject.SetActive(false); + } + } + + private void versionRequestSuccess(WWW response) + { + PlayerLogger.Log("D", "vRS", "Version request done."); + string newVersion = response.text; + + if (isUpdated(newVersion)) + { + PlayerLogger.Log("D", "vRS", "Version (" + this.version + ") is up to date. Loading local dictionary."); + load(read()); + } + else + { + PlayerLogger.Log("D", "vRS", "Updating dictionary from version " + this.version + " to " + newVersion + "."); + File.WriteAllText(this.versionPath, newVersion); + + StartCoroutine(Methods.WaitForResponse( + new WWW("http://dicionario.vlibras.gov.br/signs"), + 20f, + delegate (WWW jsonResponse) + { + PlayerLogger.Log("D", "vRS", "Trie request done."); + string json = jsonResponse.text; + + save(json); + load(json); + }, + delegate (WWW jsonResponse) + { + if (jsonResponse.error == null) + PlayerLogger.Log("D", "vRS", "Timeout."); + else + PlayerLogger.Log("D", "vRS", "WWW error: " + jsonResponse.error); + + load(read()); + } + )); + } + } + + private bool isUpdated(string newVersion) + { + string[] versionTokens = this.version.Split('.'); + string[] newVersionTokens = newVersion.Split('.'); + + for (int i = 0; i < versionTokens.Length; i++) + if (int.Parse(versionTokens[i]) < int.Parse(newVersionTokens[i])) + return false; + + return true; + } + + private void load(string json) + { + new Thread( + () => { + Thread.CurrentThread.IsBackground = true; + + Stopwatch watch = new Stopwatch(); + watch.Start(); + + try { + PlayerLogger.Log("D", "l", "Starting trie parsing."); + this.signs = Trie.FromJSON(new JSONParser(Regex.Unescape(json))); ; + + foreach (char c in CharactersSigns) + this.signs.Add(c.ToString()); + } + catch (Exception e) { + PlayerLogger.Log("D", "l", "Exception at trie JSON parser: " + e.ToString()); + this.signs = new Trie(); + } + + this.ready = true; + + watch.Stop(); + PlayerLogger.Log("D", "l", "Parsed in " + watch.ElapsedMilliseconds + " ms."); + } + ).Start(); + } + + private bool exists() { + return File.Exists(this.path); + } + + private string read() + { + if (!exists()) + { + PlayerLogger.Log("D", "r", "Error! Local dictionary not found. Loading default."); + return DefaultDictionary; + } + + return File.ReadAllText(this.path); + } + + private void save(string json) + { + File.WriteAllText(this.path, json); + +#if UNITY_EDITOR + UnityEditor.AssetDatabase.Refresh(); +#endif + } + } +} diff --git a/Assets/Scripts/Animation List/Dictionary.cs.meta b/Assets/Scripts/Animation List/Dictionary.cs.meta new file mode 100644 index 0000000..9e4ef63 --- /dev/null +++ b/Assets/Scripts/Animation List/Dictionary.cs.meta @@ -0,0 +1,12 @@ +fileFormatVersion: 2 +guid: 87b9a8a309b780346a49c0d7ad7baecb +timeCreated: 1476733121 +licenseType: Pro +MonoImporter: + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Animation List/ListManager.cs b/Assets/Scripts/Animation List/ListManager.cs index 2ff6f61..628956c 100644 --- a/Assets/Scripts/Animation List/ListManager.cs +++ b/Assets/Scripts/Animation List/ListManager.cs @@ -5,6 +5,8 @@ using System; using System.IO; using System.Collections; using System.Collections.Generic; +using LAViD.VLibras.Dictionary; +using LAViD.Structures; // https://unity3d.com/pt/learn/tutorials/modules/beginner/live-training-archive/creating-scroll-lists-at-run-time @@ -17,8 +19,8 @@ public class ItemData { } public class ListManager : MonoBehaviour { - - private PlayerManager manager; + + private Trie signs; public GameObject sampleItemObject; public GameObject sampleLoadingItemObject; @@ -42,7 +44,6 @@ public class ListManager : MonoBehaviour { void Start() { - this.manager = this.gameObject.GetComponent(); this.scrollView.onValueChanged.AddListener(checkScrollPosition); this.input.onValueChanged.AddListener(inputChanged); @@ -57,14 +58,14 @@ public class ListManager : MonoBehaviour { else this.itemHeight = 90; - StartCoroutine(load()); + this.gameObject.GetComponent().AddOnLoadListener(load); } - public IEnumerator load() + private void load() { - while (manager.signs == null) yield return new WaitForEndOfFrame(); + this.signs = this.gameObject.GetComponent().Signs; - this.itemList = this.manager.signs.StartsWith(""); + this.itemList = this.signs.StartsWith(""); this.index = 0; this.size = itemList.Count; @@ -79,7 +80,7 @@ public class ListManager : MonoBehaviour { public void inputChanged(string text) { - this.itemList = this.manager.signs.StartsWith(text.ToUpper()); + this.itemList = this.signs.StartsWith(text.ToUpper()); this.index = 0; this.size = itemList.Count; diff --git a/Assets/Scripts/Player Manager/GenericPlayerManager.cs b/Assets/Scripts/Player Manager/GenericPlayerManager.cs index a51bb02..0d12e9c 100644 --- a/Assets/Scripts/Player Manager/GenericPlayerManager.cs +++ b/Assets/Scripts/Player Manager/GenericPlayerManager.cs @@ -32,6 +32,7 @@ using System; using System.Threading; using UnityEngine.UI; using LAViD.VLibras.Utils; +using LAViD.Unity.Utils; public abstract class GenericPlayerManager : MonoBehaviour { @@ -86,7 +87,6 @@ public abstract class GenericPlayerManager : MonoBehaviour { subtitles.DefaultFirstLetterSpeed = new DefaultSignSpeed(2.1F, 2.8F); subtitles.DefaultLetterSpeed = new DefaultSignSpeed(3F, 4.3F); subtitles.DefaultNumberSpeed = new DefaultSignSpeed(1.5F, 2.9F); - PlayerLogger.Log("Subtitles object: " + subtitles); AVATAR = GameObject.FindGameObjectWithTag("avatar"); COMPONENT_ANIMATION = AVATAR.GetComponent(); diff --git a/Assets/Scripts/Player Manager/PlayerLogger.cs b/Assets/Scripts/Player Manager/PlayerLogger.cs index 62cbf0b..9aa443d 100644 --- a/Assets/Scripts/Player Manager/PlayerLogger.cs +++ b/Assets/Scripts/Player Manager/PlayerLogger.cs @@ -1,7 +1,7 @@ using UnityEngine; using UnityEngine.UI; -namespace LAViD.VLibras.Utils { +namespace LAViD.Unity.Utils { public abstract class PlayerLogger : MonoBehaviour { diff --git a/Assets/Scripts/Player Manager/Utils.cs b/Assets/Scripts/Player Manager/Utils.cs index 3eb63ba..957ba37 100644 --- a/Assets/Scripts/Player Manager/Utils.cs +++ b/Assets/Scripts/Player Manager/Utils.cs @@ -1,14 +1,51 @@ -using UnityEngine; +using LAViD.VLibras.Utils; +using System.Collections; +using UnityEngine; using UnityEngine.UI; namespace LAViD.Unity.Utils { - public static class Definitions { + public static class Methods { public static Color SetAlpha(this Color color, float alpha) { return new Color(color.r, color.g, color.b, alpha); } + + public static IEnumerator WaitForResponse(WWW www, float timeoutLimit, Events.RequestSuccess success, Events.RequestError error) + { + float time = 0; + + while (!www.isDone) + { + if (time > timeoutLimit) + { + if (error != null) error(www); + yield break; + } + + time += 0.1f; + yield return new WaitForSeconds(0.1f); + } + + if (www.error == null) + { + if (success != null) success(www); + } + else + { + if (error != null) error(www); + } + } + + } + + public static class Events { + + public delegate void RequestSuccess(WWW request); + public delegate void RequestError(WWW request); + + public delegate void OnLoad(); } diff --git a/Assets/Scripts/PlayerManager.cs b/Assets/Scripts/PlayerManager.cs index e0de109..9856cdb 100644 --- a/Assets/Scripts/PlayerManager.cs +++ b/Assets/Scripts/PlayerManager.cs @@ -2,13 +2,13 @@ using UnityEngine; using System.Collections; using System; using UnityEngine.UI; -using LAViD.VLibras.Utils; using System.Text.RegularExpressions; using System.Threading; using LAViD.Utils; using LAViD.Structures; using System.Diagnostics; -using System.Collections.Generic; +using LAViD.Unity.Utils; +using LAViD.VLibras.Dictionary; public class PlayerManager : GenericPlayerManager { @@ -44,9 +44,7 @@ public class PlayerManager : GenericPlayerManager { private string dictWord = null; private string regionPath = ""; private int regionHash = 1; - - public Trie signs = null; - + private Trie signs = null; public static string get_connection_status_message(ERROR_STATUS_MESSAGE msg) { @@ -74,57 +72,14 @@ public class PlayerManager : GenericPlayerManager { base.setRandomAnimations(randomAnimationNames); base.Start(); - voiceRecognizer = new VoiceRecognition(); + this.gameObject.GetComponent().AddOnLoadListener(setSigns); + this.voiceRecognizer = new VoiceRecognition(); + Screen.fullScreen = false; - - StartCoroutine(WaitForResponse( - new WWW("http://dicionario.vlibras.gov.br/signs"), - // Success - delegate(WWW www) - { - string response = www.text; - - new Thread( - () => { - Thread.CurrentThread.IsBackground = true; - - Stopwatch watch = new Stopwatch(); - watch.Start(); - - try { - PlayerLogger.Log("T", "FJ", "Starting trie parsing."); - this.signs = Trie.FromJSON(new JSONParser(Regex.Unescape(response))); - - char[] charactersSigns = new char[] - { - 'A', 'B', 'C', 'Ç', 'D', 'E', 'F', 'G', 'H', 'I', - 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', - 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', - '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' - }; - - foreach (char c in charactersSigns) - { - this.signs.Add(c.ToString()); - } - } catch (Exception e) { - PlayerLogger.Log("PM", "S", "Exception at trie JSON parser: " + e.ToString()); - } - - watch.Stop(); - PlayerLogger.Log("T", "FJ", "Parsed in " + watch.ElapsedMilliseconds + " ms."); - } - ).Start(); - }, - // Error - delegate (WWW www) - { - if (www.error == null) - PlayerLogger.Log("PM", "S", "Timeout."); - else - PlayerLogger.Log("PM", "S", "WWW error: " + www.error); - } - )); + } + + private void setSigns() { + this.signs = this.gameObject.GetComponent().Signs; } public void playDict(string word) @@ -176,7 +131,7 @@ public class PlayerManager : GenericPlayerManager { protected override WWW loadAssetBundle(string aniName) { - if (this.regionHash == 1 && !this.signs.Contains(aniName)) + if (this.regionHash == 1 && this.signs != null && !this.signs.Contains(aniName)) return null; string address = BASE_URL + this.regionPath + WWW.EscapeURL(aniName); @@ -256,41 +211,9 @@ public class PlayerManager : GenericPlayerManager { return false;*/ } - protected delegate void SuccessEvent(WWW request); - protected delegate void ErrorEvent(WWW request); - - protected IEnumerator WaitForResponse(WWW www, SuccessEvent success, ErrorEvent error) + public IEnumerator WaitForResponse(WWW www, Events.RequestSuccess success, Events.RequestError error) { - PlayerLogger.Log("PM", "WFR", "Stating time check."); - - const float timeoutLimit = 20f; - float timer = 0; - - while (!www.isDone) - { - if (timer > timeoutLimit) - { - PlayerLogger.Log("PM", "WFR", "Timeout (" + timer + ")."); - if (error != null) error(www); - yield break; - } - -#if UNITY_ANDROID - timer += Time.deltaTime; - yield return null; -#elif UNITY_IOS - timer += 0.1f; - yield return new WaitForSeconds(0.1f); -#endif - } - - PlayerLogger.Log("PM", "WFR", "Done (" + timer + ")."); - - if (www.error == null) { - if (success != null) success(www); - } else { - if (error != null) error(www); - } + yield return Methods.WaitForResponse(www, 20f, success, error); } protected override IEnumerator WaitForResponse(WWW www) { diff --git a/Assets/Scripts/Trie/Trie.cs b/Assets/Scripts/Trie/Trie.cs index cd0a3f9..2d11ee7 100644 --- a/Assets/Scripts/Trie/Trie.cs +++ b/Assets/Scripts/Trie/Trie.cs @@ -1,11 +1,6 @@ -using LAViD.Utils; -using LAViD.VLibras.Utils; -using System; +using LAViD.Unity.Utils; +using LAViD.Utils; using System.Collections.Generic; -using System.Diagnostics; -using System.Linq; -using System.Text; -using System.Text.RegularExpressions; namespace LAViD.Structures { @@ -56,6 +51,7 @@ namespace LAViD.Structures public bool Contains(string word) { Node node = this.root; + if (node == null) return false; foreach (char c in word) { @@ -73,6 +69,7 @@ namespace LAViD.Structures List list = new List(); Node node = this.root; + if (node == null) return list; foreach (char c in word) { diff --git a/Assets/Scripts/Utils.cs b/Assets/Scripts/Utils.cs new file mode 100644 index 0000000..3f0b313 --- /dev/null +++ b/Assets/Scripts/Utils.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace LAViD.VLibras.Utils +{ + public static class Definitions + { + + } +} diff --git a/Assets/Scripts/Utils.cs.meta b/Assets/Scripts/Utils.cs.meta new file mode 100644 index 0000000..ea3e492 --- /dev/null +++ b/Assets/Scripts/Utils.cs.meta @@ -0,0 +1,12 @@ +fileFormatVersion: 2 +guid: fa6e565b9400d454cac12d1898106fac +timeCreated: 1476733121 +licenseType: Pro +MonoImporter: + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/VisualLogger.cs b/Assets/Scripts/VisualLogger.cs index 86def1b..dbd86a4 100644 --- a/Assets/Scripts/VisualLogger.cs +++ b/Assets/Scripts/VisualLogger.cs @@ -1,4 +1,5 @@ -using System.Collections.Generic; +using LAViD.Unity.Utils; +using System.Collections.Generic; using UnityEngine.UI; namespace LAViD.VLibras.Utils { -- libgit2 0.21.2