Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.IO;
- using System.Collections.Generic;
- using System;
- using Newtonsoft.Json.Linq;
- public class JsonFileWriter : MonoBehaviour
- {
- public GameController gameController;
- public Settings settings;
- public List<Dictionary<string, string>> allQuestions = new List<Dictionary<string, string>>();
- public string path;
- public string pathGameData;
- public void Start()
- {
- path = "jar:file://" + Application.dataPath + "!/assets/data.json";
- #if UNITY_EDITOR
- path = Path.Combine(Application.streamingAssetsPath, "data.json");
- #endif
- pathGameData = Path.Combine(Application.persistentDataPath, "gameData.json");
- //List<JsonString> temp = new List<JsonString>();
- //temp.Add(new JsonString(new string[]{ "The question","The correct answer","Answer 1","Answer 2","Answer 3"}));
- //SerializeData();
- DeserializeQuestions();
- }
- public void SerializeGameData(GameData data)
- {
- System.DateTime epochStart = new System.DateTime(1970, 1, 1, 0, 0, 0, System.DateTimeKind.Utc);
- string cur_time = (int)(System.DateTime.UtcNow - epochStart).TotalSeconds + "";
- data.date = cur_time;
- if (!File.Exists(pathGameData))
- {
- File.WriteAllText(pathGameData, "");
- }
- string containerJson = File.ReadAllText(pathGameData);
- GameDataContainer container = new GameDataContainer();
- container.container = new List<GameData>();
- if(containerJson != "")
- {
- container = JsonUtility.FromJson<GameDataContainer>(containerJson);
- }
- container.container.Add(data);
- if(container.container.Count > settings.save_this_many_games)
- {
- int lowest = (int)(System.DateTime.UtcNow - epochStart).TotalSeconds;
- foreach(GameData s in container.container)
- {
- if(Int32.Parse(s.date) < lowest)
- {
- lowest = Int32.Parse(s.date);
- }
- }
- for (int i = container.container.Count - 1; i >= 0; i--)
- {
- if(Int32.Parse(container.container[i].date) == lowest)
- {
- container.container.Remove(container.container[i]);
- }
- }
- }
- containerJson = JsonUtility.ToJson(container);
- File.WriteAllText(pathGameData, containerJson);
- }
- public void DeserializeQuestions()
- {
- string json;
- if (Application.platform == RuntimePlatform.Android)
- {
- WWW reader = new WWW(path);
- while (!reader.isDone) { }
- json = reader.text;
- return;
- }
- json = File.ReadAllText(path);
- JObject jo = JObject.Parse(json);
- Dictionary<string, List<string>> values = jo.SelectToken("Questions", false).ToObject<Dictionary<string, List<string>>>();
- foreach(var kv in values)
- {
- Dictionary<string, string> temp = new Dictionary<string, string>();
- int i = 0;
- foreach(string s in kv.Value)
- {
- temp.Add(i + "", s);
- i++;
- }
- allQuestions.Add(temp);
- }
- }
- }
- [Serializable]
- public class GameData
- {
- public string date;
- public string questions;
- public List<string> playerScoresNames;
- public List<int> playerScoresScores;
- public List<string> playerStatesNames;
- public List<int> playerStatesStates;
- }
- [Serializable]
- public class GameDataContainer
- {
- public List<GameData> container;
- }
Advertisement
Add Comment
Please, Sign In to add comment