Commit beadb83d25baedfad242f2845e3baaa6e12fd525

Authored by Mateus Lustosa
1 parent cc4de742

Dictionary request done

Assets/Scenes/Main.unity
... ... @@ -3546,6 +3546,17 @@ MonoBehaviour:
3546 3546 contentPanel: {fileID: 1995982559}
3547 3547 scrollView: {fileID: 2128190448}
3548 3548 input: {fileID: 168747182}
  3549 +--- !u!114 &578311384
  3550 +MonoBehaviour:
  3551 + m_ObjectHideFlags: 0
  3552 + m_PrefabParentObject: {fileID: 0}
  3553 + m_PrefabInternal: {fileID: 0}
  3554 + m_GameObject: {fileID: 578311380}
  3555 + m_Enabled: 1
  3556 + m_EditorHideFlags: 0
  3557 + m_Script: {fileID: 11500000, guid: 87b9a8a309b780346a49c0d7ad7baecb, type: 3}
  3558 + m_Name:
  3559 + m_EditorClassIdentifier:
3549 3560 --- !u!1 &582478958
3550 3561 GameObject:
3551 3562 m_ObjectHideFlags: 0
... ...
Assets/Scripts/Animation List/Dictionary.cs 0 → 100644
... ... @@ -0,0 +1,199 @@
  1 +using LAViD.Structures;
  2 +using LAViD.Unity.Utils;
  3 +using LAViD.Utils;
  4 +using System;
  5 +using System.Collections.Generic;
  6 +using System.Diagnostics;
  7 +using System.IO;
  8 +using System.Linq;
  9 +using System.Text;
  10 +using System.Text.RegularExpressions;
  11 +using System.Threading;
  12 +using UnityEngine;
  13 +
  14 +namespace LAViD.VLibras.Dictionary
  15 +{
  16 + public class Dictionary : MonoBehaviour
  17 + {
  18 + private readonly string DefaultDictionary = "{}"; // "{\"keys\": {}, \"trie\": {}, \"characters\": []}";
  19 + private readonly char[] CharactersSigns = new char[] {
  20 + 'A', 'B', 'C', 'Ç', 'D', 'E', 'F', 'G', 'H', 'I',
  21 + 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S',
  22 + 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
  23 + '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'
  24 + };
  25 +
  26 + private string path;
  27 + private string versionPath;
  28 +
  29 + private string version = "0.0.0";
  30 + private Trie signs = null;
  31 + private volatile bool ready = false;
  32 + private event Events.OnLoad onLoadListeners;
  33 +
  34 + public void AddOnLoadListener(Events.OnLoad onLoad)
  35 + {
  36 + PlayerLogger.Log("D", "AOLL", "Registering onLoad event.");
  37 +
  38 + if (ready)
  39 + {
  40 + PlayerLogger.Log("D", "AOLL", "Ready. Calling onLoad event directly.");
  41 + onLoad();
  42 + }
  43 + else this.onLoadListeners += onLoad;
  44 + }
  45 +
  46 + public Trie Signs
  47 + {
  48 + get { return ready ? this.signs : null; }
  49 + }
  50 +
  51 + private void Start()
  52 + {
  53 + this.path = Application.persistentDataPath + "/dictionary.json";
  54 + this.versionPath = Application.persistentDataPath + "/dictionary_version.txt";
  55 +
  56 + PlayerLogger.Log("D", "S", "Dictionary path: " + this.path);
  57 + PlayerLogger.Log("D", "S", "Dictionary version path: " + this.versionPath);
  58 +
  59 + if (!File.Exists(this.versionPath))
  60 + {
  61 + File.WriteAllText(this.versionPath, version);
  62 + save(DefaultDictionary);
  63 + }
  64 + else this.version = File.ReadAllText(this.versionPath);
  65 +
  66 + StartCoroutine(Methods.WaitForResponse(
  67 + new WWW("http://atualizacao.vlibras.lavid.ufpb.br/version/dict"),
  68 + 10f,
  69 + versionRequestSuccess,
  70 + delegate (WWW response)
  71 + {
  72 + PlayerLogger.Log("D", "S", "Connection error. Loading local dictionary version " + this.version + ".");
  73 +
  74 + if (response.error == null)
  75 + PlayerLogger.Log("D", "S", "Timeout.");
  76 + else
  77 + PlayerLogger.Log("D", "S", "WWW error: " + response.error);
  78 +
  79 + load(read());
  80 + }
  81 + ));
  82 + }
  83 +
  84 + private void Update()
  85 + {
  86 + if (ready)
  87 + {
  88 + PlayerLogger.Log("D", "AOLL", "Ready. Calling onLoad events.");
  89 +
  90 + this.onLoadListeners();
  91 + this.gameObject.SetActive(false);
  92 + }
  93 + }
  94 +
  95 + private void versionRequestSuccess(WWW response)
  96 + {
  97 + PlayerLogger.Log("D", "vRS", "Version request done.");
  98 + string newVersion = response.text;
  99 +
  100 + if (isUpdated(newVersion))
  101 + {
  102 + PlayerLogger.Log("D", "vRS", "Version (" + this.version + ") is up to date. Loading local dictionary.");
  103 + load(read());
  104 + }
  105 + else
  106 + {
  107 + PlayerLogger.Log("D", "vRS", "Updating dictionary from version " + this.version + " to " + newVersion + ".");
  108 + File.WriteAllText(this.versionPath, newVersion);
  109 +
  110 + StartCoroutine(Methods.WaitForResponse(
  111 + new WWW("http://dicionario.vlibras.gov.br/signs"),
  112 + 20f,
  113 + delegate (WWW jsonResponse)
  114 + {
  115 + PlayerLogger.Log("D", "vRS", "Trie request done.");
  116 + string json = jsonResponse.text;
  117 +
  118 + save(json);
  119 + load(json);
  120 + },
  121 + delegate (WWW jsonResponse)
  122 + {
  123 + if (jsonResponse.error == null)
  124 + PlayerLogger.Log("D", "vRS", "Timeout.");
  125 + else
  126 + PlayerLogger.Log("D", "vRS", "WWW error: " + jsonResponse.error);
  127 +
  128 + load(read());
  129 + }
  130 + ));
  131 + }
  132 + }
  133 +
  134 + private bool isUpdated(string newVersion)
  135 + {
  136 + string[] versionTokens = this.version.Split('.');
  137 + string[] newVersionTokens = newVersion.Split('.');
  138 +
  139 + for (int i = 0; i < versionTokens.Length; i++)
  140 + if (int.Parse(versionTokens[i]) < int.Parse(newVersionTokens[i]))
  141 + return false;
  142 +
  143 + return true;
  144 + }
  145 +
  146 + private void load(string json)
  147 + {
  148 + new Thread(
  149 + () => {
  150 + Thread.CurrentThread.IsBackground = true;
  151 +
  152 + Stopwatch watch = new Stopwatch();
  153 + watch.Start();
  154 +
  155 + try {
  156 + PlayerLogger.Log("D", "l", "Starting trie parsing.");
  157 + this.signs = Trie.FromJSON(new JSONParser(Regex.Unescape(json))); ;
  158 +
  159 + foreach (char c in CharactersSigns)
  160 + this.signs.Add(c.ToString());
  161 + }
  162 + catch (Exception e) {
  163 + PlayerLogger.Log("D", "l", "Exception at trie JSON parser: " + e.ToString());
  164 + this.signs = new Trie();
  165 + }
  166 +
  167 + this.ready = true;
  168 +
  169 + watch.Stop();
  170 + PlayerLogger.Log("D", "l", "Parsed in " + watch.ElapsedMilliseconds + " ms.");
  171 + }
  172 + ).Start();
  173 + }
  174 +
  175 + private bool exists() {
  176 + return File.Exists(this.path);
  177 + }
  178 +
  179 + private string read()
  180 + {
  181 + if (!exists())
  182 + {
  183 + PlayerLogger.Log("D", "r", "Error! Local dictionary not found. Loading default.");
  184 + return DefaultDictionary;
  185 + }
  186 +
  187 + return File.ReadAllText(this.path);
  188 + }
  189 +
  190 + private void save(string json)
  191 + {
  192 + File.WriteAllText(this.path, json);
  193 +
  194 +#if UNITY_EDITOR
  195 + UnityEditor.AssetDatabase.Refresh();
  196 +#endif
  197 + }
  198 + }
  199 +}
... ...
Assets/Scripts/Animation List/Dictionary.cs.meta 0 → 100644
... ... @@ -0,0 +1,12 @@
  1 +fileFormatVersion: 2
  2 +guid: 87b9a8a309b780346a49c0d7ad7baecb
  3 +timeCreated: 1476733121
  4 +licenseType: Pro
  5 +MonoImporter:
  6 + serializedVersion: 2
  7 + defaultReferences: []
  8 + executionOrder: 0
  9 + icon: {instanceID: 0}
  10 + userData:
  11 + assetBundleName:
  12 + assetBundleVariant:
... ...
Assets/Scripts/Animation List/ListManager.cs
... ... @@ -5,6 +5,8 @@ using System;
5 5 using System.IO;
6 6 using System.Collections;
7 7 using System.Collections.Generic;
  8 +using LAViD.VLibras.Dictionary;
  9 +using LAViD.Structures;
8 10  
9 11 // https://unity3d.com/pt/learn/tutorials/modules/beginner/live-training-archive/creating-scroll-lists-at-run-time
10 12  
... ... @@ -17,8 +19,8 @@ public class ItemData {
17 19 }
18 20  
19 21 public class ListManager : MonoBehaviour {
20   -
21   - private PlayerManager manager;
  22 +
  23 + private Trie signs;
22 24  
23 25 public GameObject sampleItemObject;
24 26 public GameObject sampleLoadingItemObject;
... ... @@ -42,7 +44,6 @@ public class ListManager : MonoBehaviour {
42 44  
43 45 void Start()
44 46 {
45   - this.manager = this.gameObject.GetComponent<PlayerManager>();
46 47 this.scrollView.onValueChanged.AddListener(checkScrollPosition);
47 48 this.input.onValueChanged.AddListener(inputChanged);
48 49  
... ... @@ -57,14 +58,14 @@ public class ListManager : MonoBehaviour {
57 58 else
58 59 this.itemHeight = 90;
59 60  
60   - StartCoroutine(load());
  61 + this.gameObject.GetComponent<Dictionary>().AddOnLoadListener(load);
61 62 }
62 63  
63   - public IEnumerator load()
  64 + private void load()
64 65 {
65   - while (manager.signs == null) yield return new WaitForEndOfFrame();
  66 + this.signs = this.gameObject.GetComponent<Dictionary>().Signs;
66 67  
67   - this.itemList = this.manager.signs.StartsWith("");
  68 + this.itemList = this.signs.StartsWith("");
68 69 this.index = 0;
69 70 this.size = itemList.Count;
70 71  
... ... @@ -79,7 +80,7 @@ public class ListManager : MonoBehaviour {
79 80  
80 81 public void inputChanged(string text)
81 82 {
82   - this.itemList = this.manager.signs.StartsWith(text.ToUpper());
  83 + this.itemList = this.signs.StartsWith(text.ToUpper());
83 84 this.index = 0;
84 85 this.size = itemList.Count;
85 86  
... ...
Assets/Scripts/Player Manager/GenericPlayerManager.cs
... ... @@ -32,6 +32,7 @@ using System;
32 32 using System.Threading;
33 33 using UnityEngine.UI;
34 34 using LAViD.VLibras.Utils;
  35 +using LAViD.Unity.Utils;
35 36  
36 37 public abstract class GenericPlayerManager : MonoBehaviour {
37 38  
... ... @@ -86,7 +87,6 @@ public abstract class GenericPlayerManager : MonoBehaviour {
86 87 subtitles.DefaultFirstLetterSpeed = new DefaultSignSpeed(2.1F, 2.8F);
87 88 subtitles.DefaultLetterSpeed = new DefaultSignSpeed(3F, 4.3F);
88 89 subtitles.DefaultNumberSpeed = new DefaultSignSpeed(1.5F, 2.9F);
89   - PlayerLogger.Log("Subtitles object: " + subtitles);
90 90  
91 91 AVATAR = GameObject.FindGameObjectWithTag("avatar");
92 92 COMPONENT_ANIMATION = AVATAR.GetComponent<Animation>();
... ...
Assets/Scripts/Player Manager/PlayerLogger.cs
1 1 using UnityEngine;
2 2 using UnityEngine.UI;
3 3  
4   -namespace LAViD.VLibras.Utils {
  4 +namespace LAViD.Unity.Utils {
5 5  
6 6 public abstract class PlayerLogger : MonoBehaviour {
7 7  
... ...
Assets/Scripts/Player Manager/Utils.cs
1   -using UnityEngine;
  1 +using LAViD.VLibras.Utils;
  2 +using System.Collections;
  3 +using UnityEngine;
2 4 using UnityEngine.UI;
3 5  
4 6 namespace LAViD.Unity.Utils {
5 7  
6   - public static class Definitions {
  8 + public static class Methods {
7 9  
8 10 public static Color SetAlpha(this Color color, float alpha)
9 11 {
10 12 return new Color(color.r, color.g, color.b, alpha);
11 13 }
  14 +
  15 + public static IEnumerator WaitForResponse(WWW www, float timeoutLimit, Events.RequestSuccess success, Events.RequestError error)
  16 + {
  17 + float time = 0;
  18 +
  19 + while (!www.isDone)
  20 + {
  21 + if (time > timeoutLimit)
  22 + {
  23 + if (error != null) error(www);
  24 + yield break;
  25 + }
  26 +
  27 + time += 0.1f;
  28 + yield return new WaitForSeconds(0.1f);
  29 + }
  30 +
  31 + if (www.error == null)
  32 + {
  33 + if (success != null) success(www);
  34 + }
  35 + else
  36 + {
  37 + if (error != null) error(www);
  38 + }
  39 + }
  40 +
  41 + }
  42 +
  43 + public static class Events {
  44 +
  45 + public delegate void RequestSuccess(WWW request);
  46 + public delegate void RequestError(WWW request);
  47 +
  48 + public delegate void OnLoad();
12 49  
13 50 }
14 51  
... ...
Assets/Scripts/PlayerManager.cs
... ... @@ -2,13 +2,13 @@ using UnityEngine;
2 2 using System.Collections;
3 3 using System;
4 4 using UnityEngine.UI;
5   -using LAViD.VLibras.Utils;
6 5 using System.Text.RegularExpressions;
7 6 using System.Threading;
8 7 using LAViD.Utils;
9 8 using LAViD.Structures;
10 9 using System.Diagnostics;
11   -using System.Collections.Generic;
  10 +using LAViD.Unity.Utils;
  11 +using LAViD.VLibras.Dictionary;
12 12  
13 13 public class PlayerManager : GenericPlayerManager {
14 14  
... ... @@ -44,9 +44,7 @@ public class PlayerManager : GenericPlayerManager {
44 44 private string dictWord = null;
45 45 private string regionPath = "";
46 46 private int regionHash = 1;
47   -
48   - public Trie signs = null;
49   -
  47 + private Trie signs = null;
50 48  
51 49 public static string get_connection_status_message(ERROR_STATUS_MESSAGE msg)
52 50 {
... ... @@ -74,57 +72,14 @@ public class PlayerManager : GenericPlayerManager {
74 72 base.setRandomAnimations(randomAnimationNames);
75 73 base.Start();
76 74  
77   - voiceRecognizer = new VoiceRecognition();
  75 + this.gameObject.GetComponent<Dictionary>().AddOnLoadListener(setSigns);
  76 + this.voiceRecognizer = new VoiceRecognition();
  77 +
78 78 Screen.fullScreen = false;
79   -
80   - StartCoroutine(WaitForResponse(
81   - new WWW("http://dicionario.vlibras.gov.br/signs"),
82   - // Success
83   - delegate(WWW www)
84   - {
85   - string response = www.text;
86   -
87   - new Thread(
88   - () => {
89   - Thread.CurrentThread.IsBackground = true;
90   -
91   - Stopwatch watch = new Stopwatch();
92   - watch.Start();
93   -
94   - try {
95   - PlayerLogger.Log("T", "FJ", "Starting trie parsing.");
96   - this.signs = Trie.FromJSON(new JSONParser(Regex.Unescape(response)));
97   -
98   - char[] charactersSigns = new char[]
99   - {
100   - 'A', 'B', 'C', 'Ç', 'D', 'E', 'F', 'G', 'H', 'I',
101   - 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S',
102   - 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
103   - '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'
104   - };
105   -
106   - foreach (char c in charactersSigns)
107   - {
108   - this.signs.Add(c.ToString());
109   - }
110   - } catch (Exception e) {
111   - PlayerLogger.Log("PM", "S", "Exception at trie JSON parser: " + e.ToString());
112   - }
113   -
114   - watch.Stop();
115   - PlayerLogger.Log("T", "FJ", "Parsed in " + watch.ElapsedMilliseconds + " ms.");
116   - }
117   - ).Start();
118   - },
119   - // Error
120   - delegate (WWW www)
121   - {
122   - if (www.error == null)
123   - PlayerLogger.Log("PM", "S<d>", "Timeout.");
124   - else
125   - PlayerLogger.Log("PM", "S<d>", "WWW error: " + www.error);
126   - }
127   - ));
  79 + }
  80 +
  81 + private void setSigns() {
  82 + this.signs = this.gameObject.GetComponent<Dictionary>().Signs;
128 83 }
129 84  
130 85 public void playDict(string word)
... ... @@ -176,7 +131,7 @@ public class PlayerManager : GenericPlayerManager {
176 131  
177 132 protected override WWW loadAssetBundle(string aniName)
178 133 {
179   - if (this.regionHash == 1 && !this.signs.Contains(aniName))
  134 + if (this.regionHash == 1 && this.signs != null && !this.signs.Contains(aniName))
180 135 return null;
181 136  
182 137 string address = BASE_URL + this.regionPath + WWW.EscapeURL(aniName);
... ... @@ -256,41 +211,9 @@ public class PlayerManager : GenericPlayerManager {
256 211 return false;*/
257 212 }
258 213  
259   - protected delegate void SuccessEvent(WWW request);
260   - protected delegate void ErrorEvent(WWW request);
261   -
262   - protected IEnumerator WaitForResponse(WWW www, SuccessEvent success, ErrorEvent error)
  214 + public IEnumerator WaitForResponse(WWW www, Events.RequestSuccess success, Events.RequestError error)
263 215 {
264   - PlayerLogger.Log("PM", "WFR", "Stating time check.");
265   -
266   - const float timeoutLimit = 20f;
267   - float timer = 0;
268   -
269   - while (!www.isDone)
270   - {
271   - if (timer > timeoutLimit)
272   - {
273   - PlayerLogger.Log("PM", "WFR", "Timeout (" + timer + ").");
274   - if (error != null) error(www);
275   - yield break;
276   - }
277   -
278   -#if UNITY_ANDROID
279   - timer += Time.deltaTime;
280   - yield return null;
281   -#elif UNITY_IOS
282   - timer += 0.1f;
283   - yield return new WaitForSeconds(0.1f);
284   -#endif
285   - }
286   -
287   - PlayerLogger.Log("PM", "WFR", "Done (" + timer + ").");
288   -
289   - if (www.error == null) {
290   - if (success != null) success(www);
291   - } else {
292   - if (error != null) error(www);
293   - }
  216 + yield return Methods.WaitForResponse(www, 20f, success, error);
294 217 }
295 218  
296 219 protected override IEnumerator WaitForResponse(WWW www) {
... ...
Assets/Scripts/Trie/Trie.cs
1   -using LAViD.Utils;
2   -using LAViD.VLibras.Utils;
3   -using System;
  1 +using LAViD.Unity.Utils;
  2 +using LAViD.Utils;
4 3 using System.Collections.Generic;
5   -using System.Diagnostics;
6   -using System.Linq;
7   -using System.Text;
8   -using System.Text.RegularExpressions;
9 4  
10 5 namespace LAViD.Structures
11 6 {
... ... @@ -56,6 +51,7 @@ namespace LAViD.Structures
56 51 public bool Contains(string word)
57 52 {
58 53 Node node = this.root;
  54 + if (node == null) return false;
59 55  
60 56 foreach (char c in word)
61 57 {
... ... @@ -73,6 +69,7 @@ namespace LAViD.Structures
73 69  
74 70 List<string> list = new List<string>();
75 71 Node node = this.root;
  72 + if (node == null) return list;
76 73  
77 74 foreach (char c in word)
78 75 {
... ...
Assets/Scripts/Utils.cs 0 → 100644
... ... @@ -0,0 +1,12 @@
  1 +using System;
  2 +using System.Collections.Generic;
  3 +using System.Linq;
  4 +using System.Text;
  5 +
  6 +namespace LAViD.VLibras.Utils
  7 +{
  8 + public static class Definitions
  9 + {
  10 +
  11 + }
  12 +}
... ...
Assets/Scripts/Utils.cs.meta 0 → 100644
... ... @@ -0,0 +1,12 @@
  1 +fileFormatVersion: 2
  2 +guid: fa6e565b9400d454cac12d1898106fac
  3 +timeCreated: 1476733121
  4 +licenseType: Pro
  5 +MonoImporter:
  6 + serializedVersion: 2
  7 + defaultReferences: []
  8 + executionOrder: 0
  9 + icon: {instanceID: 0}
  10 + userData:
  11 + assetBundleName:
  12 + assetBundleVariant:
... ...
Assets/Scripts/VisualLogger.cs
1   -using System.Collections.Generic;
  1 +using LAViD.Unity.Utils;
  2 +using System.Collections.Generic;
2 3 using UnityEngine.UI;
3 4  
4 5 namespace LAViD.VLibras.Utils {
... ...