/** * Parse the JSON file that is returned by the web request. * * @param www the web request/response object */ private void ParseJSON(WWW www) { // Success if (string.IsNullOrEmpty(www.error)) { if (this.NeedsManualConfiguration) { this.config.ConnectionSuccessful(true); } // Parse the json file this.logger.Log("Parse the json file."); CompanionData data = JsonUtility.FromJson <CompanionData>(www.text); // Update data if (data.timestamp > this.timestamp) { try { this.UpdateArtists(data.artists); this.UpdateArtworks(data.artworks); this.UpdateVideos(data.videos); this.UpdateAudio(data.audio); } catch (System.Exception ex) { this.logger.Log(ex.Message); return; } // Save the new json to disk System.IO.File.WriteAllText(this.JsonPath, www.text); } else { this.logger.Log("Current data is up to date."); } this.loadDataCounter--; // Error } else { this.logger.Log("Parsing json error: " + www.error); if (this.timestamp != 0) { this.logger.Log("Use last known json instead."); this.loadDataCounter--; } else { this.logger.Log("Wait for manual IP config."); if (this.NeedsManualConfiguration) { this.config.ConnectionSuccessful(false); } else { this.NeedsManualConfiguration = true; } StartCoroutine(this.WaitForIP()); } } }
/** * Use this for initialization. */ private void Start() { // Variable initialization this.Artworks = new Dictionary <int, Artwork>(); this.Artists = new Dictionary <int, Artist>(); this.Videos = new Dictionary <int, Video>(); this.Audio = new Dictionary <int, Audio>(); this.NeedsManualConfiguration = false; this.DataLoadingFinished = false; this.ArtistPath = ""; this.ArtworkPath = ""; this.VideoPath = ""; this.AudioPath = ""; this.JsonPath = ""; this.config = IPConfig.Instance; this.logger = DebugLogger.Instance; this.timestamp = 0; this.loadDataCounter = 0; this.protocolString = (this.protocol == Protocol.HTTP) ? "http://" : "https:://"; // Deactivate this script if necessary components are missing if ((this.config == null) || (this.logger == null)) { Debug.LogError("DataLoader: Script references not set properly."); this.enabled = false; return; } #if ENABLE_WINMD_SUPPORT // Set necessary data paths Windows.Storage.StorageFolder localFolder = Windows.Storage.ApplicationData.Current.LocalFolder; this.ArtistPath = localFolder.Path + PATH_SEPARATOR + ARTIST_DIRECTORY; this.ArtworkPath = localFolder.Path + PATH_SEPARATOR + ARTWORK_DIRECTORY; this.VideoPath = localFolder.Path + PATH_SEPARATOR + VIDEO_DIRECTORY; this.AudioPath = localFolder.Path + PATH_SEPARATOR + AUDIO_DIRECTORY; this.JsonPath = localFolder.Path + PATH_SEPARATOR + JSON_FILE; // Create image directories (if they do not already exist) System.IO.Directory.CreateDirectory(this.ArtistPath); System.IO.Directory.CreateDirectory(this.ArtworkPath); System.IO.Directory.CreateDirectory(this.VideoPath); System.IO.Directory.CreateDirectory(this.AudioPath); // Read current data file this.logger.Log("Read current data file."); if (System.IO.File.Exists(this.JsonPath)) { string json = System.IO.File.ReadAllText(this.JsonPath); CompanionData data = JsonUtility.FromJson <CompanionData>(json); // Save current timestamp this.timestamp = data.timestamp; // Load artists foreach (Artist artist in data.artists) { artist.localImagePath = this.ArtistPath + PATH_SEPARATOR + ARTIST_IMAGE_PREFIX + artist.artistID + IMAGE_FORMAT; this.Artists.Add(artist.artistID, artist); } // Load artworks foreach (Artwork artwork in data.artworks) { artwork.localImagePath = this.ArtworkPath + PATH_SEPARATOR + ARTWORK_IMAGE_PREFIX + artwork.artworkID + IMAGE_FORMAT; this.Artworks.Add(artwork.artworkID, artwork); } // Load videos foreach (Video video in data.videos) { video.localVideoPath = this.VideoPath + PATH_SEPARATOR + VIDEO_PREFIX + video.videoID + VIDEO_FORMAT; video.localImagePath = this.VideoPath + PATH_SEPARATOR + VIDEO_PREFIX + video.videoID + IMAGE_FORMAT; this.Videos.Add(video.videoID, video); } // Load audio foreach (Audio audio in data.audio) { audio.localAudioPath = this.AudioPath + PATH_SEPARATOR + AUDIO_PREFIX + audio.audioID + AUDIO_FORMAT; this.Audio.Add(audio.audioID, audio); } } else { this.logger.Log("No current data present."); } // Load server data this.logger.Log("Load new data."); this.loadDataCounter++; try { WWW request = new WWW(this.protocolString + this.host + ":" + this.port.ToString() + this.path); StartCoroutine(this.WaitForWWW(WebRequestFormat.Json, request)); } catch (System.Exception ex) { this.logger.Log(ex.Message); } #endif }