Advertisement
ihatetn931

Untitled

Jan 6th, 2020
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.61 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using Oculus.Newtonsoft.Json;
  5. using Oculus.Newtonsoft.Json.Linq;
  6. using UnityEngine;
  7.  
  8. namespace SubnauticaBZRP
  9. {
  10.     public class Json : MonoBehaviour
  11.     {
  12.         public class Root
  13.         {
  14.             public List<BiomeNames> BiomeNames { get; set; }
  15.         }
  16.  
  17.         public class BiomeNames
  18.         {
  19.             public int BiomeId { get; set; }
  20.             public string Biomename { get; set; }
  21.             public string TimeDateFound { get; set; }
  22.             public string Location { get; set; }
  23.         }
  24.         public static bool CheckIfBiomeExists(string bName)
  25.         {
  26.             bool found = false;
  27.             var fileContent = File.ReadAllText(ConfigFile.lightStatePath);
  28.             var accountsFromFile = JsonConvert.DeserializeObject<Root>(fileContent);
  29.             foreach (var c in accountsFromFile.BiomeNames)
  30.             {
  31.                 if (!c.Biomename.Equals(bName))
  32.                 {
  33.                     found = true;
  34.                     break;    
  35.                 }
  36.             }
  37.             return found;
  38.         }
  39.         public static void CreateJson()
  40.         {
  41.             var setting = new JsonSerializerSettings();
  42.             setting.Formatting = Formatting.Indented;
  43.             setting.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
  44.             setting.CheckAdditionalContent = true;
  45.             var jsonString = "{" +
  46.                 "\"BiomeNames\": [" +
  47.                 "{\"BiomeId\": \"0\"," +
  48.                 "\"Biomename\": \"First\"" +
  49.                 ",\"TimeDateFound\": \"Now\"" +
  50.                 ",\"Location\": \"Here\"}," +
  51.                 "]}";
  52.             var damn = JsonConvert.DeserializeObject(jsonString);
  53.             var shit = JsonConvert.SerializeObject(damn, setting);
  54.             File.AppendAllText(ConfigFile.lightStatePath, shit);
  55.         }
  56.         public static void AddNewBiome(string bName)
  57.         {
  58.             if(!File.Exists(ConfigFile.lightStatePath))
  59.             {
  60.                 CreateJson();
  61.             }
  62.             else if (CheckIfBiomeExists(bName))
  63.             {
  64.                 int biomeId = 0;
  65.                 var setting = new JsonSerializerSettings();
  66.                 setting.Formatting = Formatting.Indented;
  67.                 setting.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
  68.                 setting.CheckAdditionalContent = true;
  69.                 int locX = Mathf.FloorToInt(Player.main.rigidBody.velocity.x);
  70.                 int locY = Mathf.FloorToInt(Player.main.rigidBody.velocity.y);
  71.                 int locZ = Mathf.FloorToInt(Player.main.rigidBody.velocity.z);
  72.                 string locXYZ = $"{locX},{locY},{locZ}";
  73.  
  74.                 // var fileContent = File.ReadAllText(ConfigFile.lightStatePath);
  75.                 //var accountsFromFile = JsonConvert.DeserializeObject<Root>(fileContent);
  76.  
  77.                 JObject root = (JObject)JsonConvert.DeserializeObject(File.ReadAllText(ConfigFile.lightStatePath));
  78.                 JArray biomeNames = (Oculus.Newtonsoft.Json.Linq.JArray)root["BiomeNames"];
  79.  
  80.                 JObject newItem = new JObject();
  81.                 newItem["BiomeId"] = biomeId;
  82.                 newItem["Biomename"] = bName;
  83.                 newItem["TimeDateFound"] = DateTime.Now.ToString();
  84.                 newItem["Location"] = locXYZ;
  85.                 // ...
  86.                 biomeNames.Add(newItem);
  87.                 biomeId++;
  88.                 var json = JsonConvert.SerializeObject(biomeNames, setting);
  89.                 File.AppendAllText(ConfigFile.lightStatePath, json);
  90.             }
  91.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement