Guest User

Untitled

a guest
Mar 10th, 2019
670
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.65 KB | None | 0 0
  1. using UnityEngine;
  2. using System.IO;
  3. using System.Collections.Generic;
  4. using System;
  5. using Newtonsoft.Json.Linq;
  6.  
  7. public class JsonFileWriter : MonoBehaviour
  8. {
  9.     public GameController gameController;
  10.     public Settings settings;
  11.     public List<Dictionary<string, string>> allQuestions = new List<Dictionary<string, string>>();
  12.     public string path;
  13.     public string pathGameData;
  14.  
  15.     public void Start()
  16.     {
  17.  
  18.         path = "jar:file://" + Application.dataPath + "!/assets/data.json";
  19. #if UNITY_EDITOR
  20.         path = Path.Combine(Application.streamingAssetsPath, "data.json");
  21. #endif
  22.         pathGameData = Path.Combine(Application.persistentDataPath, "gameData.json");
  23.         //List<JsonString> temp = new List<JsonString>();
  24.         //temp.Add(new JsonString(new string[]{ "The question","The correct answer","Answer 1","Answer 2","Answer 3"}));
  25.         //SerializeData();
  26.         DeserializeQuestions();
  27.     }
  28.  
  29.     public void SerializeGameData(GameData data)
  30.     {
  31.         System.DateTime epochStart = new System.DateTime(1970, 1, 1, 0, 0, 0, System.DateTimeKind.Utc);
  32.         string cur_time = (int)(System.DateTime.UtcNow - epochStart).TotalSeconds + "";
  33.         data.date = cur_time;
  34.         if (!File.Exists(pathGameData))
  35.         {
  36.             File.WriteAllText(pathGameData, "");
  37.         }
  38.         string containerJson = File.ReadAllText(pathGameData);
  39.         GameDataContainer container = new GameDataContainer();
  40.         container.container = new List<GameData>();
  41.         if(containerJson != "")
  42.         {
  43.             container = JsonUtility.FromJson<GameDataContainer>(containerJson);
  44.         }
  45.         container.container.Add(data);
  46.         if(container.container.Count > settings.save_this_many_games)
  47.         {
  48.             int lowest = (int)(System.DateTime.UtcNow - epochStart).TotalSeconds;
  49.             foreach(GameData s in container.container)
  50.             {
  51.                 if(Int32.Parse(s.date) < lowest)
  52.                 {
  53.                     lowest = Int32.Parse(s.date);
  54.                 }
  55.             }
  56.             for (int i = container.container.Count - 1; i >= 0; i--)
  57.             {
  58.                 if(Int32.Parse(container.container[i].date) == lowest)
  59.                 {
  60.                     container.container.Remove(container.container[i]);
  61.                 }
  62.             }
  63.         }
  64.         containerJson = JsonUtility.ToJson(container);
  65.         File.WriteAllText(pathGameData, containerJson);
  66.     }
  67.  
  68.     public void DeserializeQuestions()
  69.     {
  70.         string json;
  71.  
  72.         if (Application.platform == RuntimePlatform.Android)
  73.         {
  74.             WWW reader = new WWW(path);
  75.             while (!reader.isDone) { }
  76.  
  77.             json = reader.text;
  78.  
  79.             return;
  80.         }
  81.  
  82.         json = File.ReadAllText(path);
  83.        
  84.  
  85.         JObject jo = JObject.Parse(json);
  86.         Dictionary<string, List<string>> values = jo.SelectToken("Questions", false).ToObject<Dictionary<string, List<string>>>();
  87.         foreach(var kv in values)
  88.         {
  89.             Dictionary<string, string> temp = new Dictionary<string, string>();
  90.             int i = 0;
  91.             foreach(string s in kv.Value)
  92.             {
  93.                 temp.Add(i + "", s);
  94.                 i++;
  95.             }
  96.             allQuestions.Add(temp);
  97.         }
  98.     }
  99. }
  100.  
  101. [Serializable]
  102. public class GameData
  103. {
  104.     public string date;
  105.     public string questions;
  106.     public List<string> playerScoresNames;
  107.     public List<int> playerScoresScores;
  108.     public List<string> playerStatesNames;
  109.     public List<int> playerStatesStates;
  110. }
  111.  
  112. [Serializable]
  113. public class GameDataContainer
  114. {
  115.     public List<GameData> container;
  116. }
Advertisement
Add Comment
Please, Sign In to add comment