1+ /*
2+ * Programmer: Hunter Goodin
3+ * Date Created: 09/11/2022 @ 1:00 PM
4+ * Description: This class is the JSON object that gets written
5+ */
6+
7+ using System ;
8+ using System . Collections . Generic ;
9+ using UnityEngine ;
10+ using System . IO ;
11+ using UnityEditor ;
12+
13+ // The JSON itself
14+ [ System . Serializable ]
15+ public class JSON
16+ {
17+ List < JSONBoolean > boolList ;
18+ List < JSONInteger > intList ;
19+ List < JSONFloat > floatList ;
20+ List < JSONString > stringList ;
21+
22+ TextAsset jsonFile ;
23+
24+ public JSON ( )
25+ {
26+ boolList = new List < JSONBoolean > ( ) ;
27+ intList = new List < JSONInteger > ( ) ;
28+ floatList = new List < JSONFloat > ( ) ;
29+ stringList = new List < JSONString > ( ) ;
30+ }
31+
32+ public JSON ( TextAsset jsonFile )
33+ {
34+ boolList = new List < JSONBoolean > ( ) ;
35+ intList = new List < JSONInteger > ( ) ;
36+ floatList = new List < JSONFloat > ( ) ;
37+ stringList = new List < JSONString > ( ) ;
38+ this . jsonFile = jsonFile ;
39+
40+ ParseJSON ( jsonFile ) ;
41+ }
42+
43+ public override string ToString ( )
44+ {
45+ List < string > list = new List < string > ( ) ;
46+
47+ // Print bools
48+ for ( int i = 0 ; i < boolList . Count ; i ++ )
49+ {
50+ list . Add ( $ " \" { boolList [ i ] . name } \" : { boolList [ i ] . value . ToString ( ) . ToLower ( ) } ") ;
51+ }
52+
53+ // Print ints
54+ for ( int i = 0 ; i < intList . Count ; i ++ )
55+ {
56+ list . Add ( $ " \" { intList [ i ] . name } \" : { intList [ i ] . value } ") ;
57+ }
58+
59+ // Print floats
60+ for ( int i = 0 ; i < floatList . Count ; i ++ )
61+ {
62+ list . Add ( $ " \" { floatList [ i ] . name } \" : { floatList [ i ] . value } ") ;
63+ }
64+
65+ // Print strings
66+ for ( int i = 0 ; i < stringList . Count ; i ++ )
67+ {
68+ list . Add ( $ " \" { stringList [ i ] . name } \" : \" { stringList [ i ] . value } \" ") ;
69+ }
70+
71+ // Make string
72+ string str = "{\n " ;
73+
74+ for ( int i = 0 ; i < list . Count ; i ++ )
75+ {
76+ str += ( i == list . Count - 1 ) ? list [ i ] + "\n " : list [ i ] + ",\n " ;
77+ }
78+
79+ str += "}" ;
80+
81+ return str ;
82+ }
83+
84+ void ParseJSON ( TextAsset jsonFile )
85+ {
86+ boolList = new List < JSONBoolean > ( ) ;
87+ intList = new List < JSONInteger > ( ) ;
88+ floatList = new List < JSONFloat > ( ) ;
89+ stringList = new List < JSONString > ( ) ;
90+ this . jsonFile = jsonFile ;
91+
92+ string [ ] lines = jsonFile . text . Split ( new string [ ] { "\r \n " , "\n " } , StringSplitOptions . None ) ;
93+ lines [ lines . Length - 2 ] = ( lines [ lines . Length - 2 ] . EndsWith ( "," ) ) ? lines [ lines . Length - 2 ] : lines [ lines . Length - 2 ] + "," ;
94+
95+ for ( int i = 1 ; i < lines . Length - 1 ; i ++ )
96+ {
97+ string [ ] lineSplit = lines [ i ] . Split ( new string [ ] { ":" } , StringSplitOptions . None ) ;
98+ lineSplit [ 0 ] = lineSplit [ 0 ] . Replace ( " \" " , "" ) ;
99+ lineSplit [ 0 ] = lineSplit [ 0 ] . Replace ( "\" " , "" ) ;
100+ lineSplit [ 1 ] = lineSplit [ 1 ] . Substring ( 1 , lineSplit [ 1 ] . Length - 2 ) ;
101+
102+ // If it's a boolean
103+ if ( lineSplit [ 1 ] == "true" || lineSplit [ 1 ] == "false" )
104+ {
105+ AddBool ( lineSplit [ 0 ] , Convert . ToBoolean ( lineSplit [ 1 ] ) ) ;
106+ }
107+
108+ // If it's an int
109+ if ( int . TryParse ( lineSplit [ 1 ] , out int num ) )
110+ {
111+ AddInt ( lineSplit [ 0 ] , num ) ;
112+ }
113+
114+ // If it's a float
115+ if ( lineSplit [ 1 ] . Contains ( "." ) && float . TryParse ( lineSplit [ 1 ] , out float fl ) )
116+ {
117+ AddFloat ( lineSplit [ 0 ] , fl ) ;
118+ }
119+
120+ // If it's a string
121+ if ( lineSplit [ 1 ] . StartsWith ( "\" " ) )
122+ {
123+ lineSplit [ 1 ] = lineSplit [ 1 ] . Replace ( "\" " , "" ) ;
124+ AddString ( lineSplit [ 0 ] , lineSplit [ 1 ] ) ;
125+ }
126+ }
127+ }
128+
129+ public void WriteToFile ( )
130+ {
131+ File . WriteAllText ( AssetDatabase . GetAssetPath ( jsonFile ) , ToString ( ) ) ;
132+ EditorUtility . SetDirty ( jsonFile ) ;
133+ }
134+
135+ #region Add Functions
136+
137+ public void AddBool ( string newItemName , bool newItemValue )
138+ {
139+ boolList . Add ( new JSONBoolean ( newItemName , newItemValue ) ) ;
140+ }
141+
142+ public void AddInt ( string newItemName , int newItemValue )
143+ {
144+ intList . Add ( new JSONInteger ( newItemName , newItemValue ) ) ;
145+ }
146+
147+ public void AddFloat ( string newItemName , float newItemValue )
148+ {
149+ floatList . Add ( new JSONFloat ( newItemName , newItemValue ) ) ;
150+ }
151+
152+ public void AddString ( string newItemName , string newItemValue )
153+ {
154+ stringList . Add ( new JSONString ( newItemName , newItemValue ) ) ;
155+ }
156+
157+ #endregion Add Functions
158+
159+ #region Get Funcitons
160+
161+ public bool GetBool ( string variableName )
162+ {
163+ bool found = false ;
164+ bool ret = false ;
165+
166+ for ( int i = 0 ; i < intList . Count ; i ++ )
167+ {
168+ if ( intList [ i ] . name == variableName )
169+ {
170+ found = true ;
171+ ret = boolList [ i ] . value ;
172+ }
173+ }
174+
175+ if ( found )
176+ {
177+ return ret ;
178+ }
179+ else
180+ {
181+ Debug . LogError ( $ "ERROR: No variable names { variableName } found! returning 'false' instead") ;
182+ return false ;
183+ }
184+ }
185+
186+ public int GetInt ( string variableName )
187+ {
188+ bool found = false ;
189+ int ret = 0 ;
190+
191+ for ( int i = 0 ; i < intList . Count ; i ++ )
192+ {
193+ if ( intList [ i ] . name == variableName )
194+ {
195+ found = true ;
196+ ret = intList [ i ] . value ;
197+ }
198+ }
199+
200+ if ( found )
201+ {
202+ return ret ;
203+ }
204+ else
205+ {
206+ Debug . LogError ( $ "ERROR: No variable names { variableName } found! returning '0' instead") ;
207+ return 0 ;
208+ }
209+ }
210+
211+ public float GetFloat ( string variableName )
212+ {
213+ bool found = false ;
214+ float ret = 0.0f ;
215+
216+ for ( int i = 0 ; i < intList . Count ; i ++ )
217+ {
218+ if ( intList [ i ] . name == variableName )
219+ {
220+ found = true ;
221+ ret = floatList [ i ] . value ;
222+ }
223+ }
224+
225+ if ( found )
226+ {
227+ return ret ;
228+ }
229+ else
230+ {
231+ Debug . LogError ( $ "ERROR: No variable names '{ variableName } ' found! returning '0.0f' instead") ;
232+ return 0.0f ;
233+ }
234+ }
235+
236+ public string GetString ( string variableName )
237+ {
238+ bool found = false ;
239+ string ret = null ;
240+
241+ for ( int i = 0 ; i < intList . Count ; i ++ )
242+ {
243+ if ( intList [ i ] . name == variableName )
244+ {
245+ found = true ;
246+ ret = stringList [ i ] . value ;
247+ }
248+ }
249+
250+ if ( found )
251+ {
252+ return ret ;
253+ }
254+ else
255+ {
256+ Debug . LogError ( $ "ERROR: No variable names '{ variableName } ' found! returning NULL instead") ;
257+ return null ;
258+ }
259+ }
260+
261+ #endregion Get Functions
262+ }
263+
264+ #region JSON Variables
265+
266+ [ System . Serializable ]
267+ public class JSONBoolean
268+ {
269+ public string name ;
270+ public bool value ;
271+
272+ public JSONBoolean ( string name , bool value )
273+ {
274+ this . name = name ;
275+ this . value = value ;
276+ }
277+ }
278+
279+ [ System . Serializable ]
280+ public class JSONInteger
281+ {
282+ public string name ;
283+ public int value ;
284+
285+ public JSONInteger ( string name , int value )
286+ {
287+ this . name = name ;
288+ this . value = value ;
289+ }
290+ }
291+
292+ [ System . Serializable ]
293+ public class JSONFloat
294+ {
295+ public string name ;
296+ public float value ;
297+
298+ public JSONFloat ( string name , float value )
299+ {
300+ this . name = name ;
301+ this . value = value ;
302+ }
303+ }
304+
305+ [ System . Serializable ]
306+ public class JSONString
307+ {
308+ public string name ;
309+ public string value ;
310+
311+ public JSONString ( string name , string value )
312+ {
313+ this . name = name ;
314+ this . value = value ;
315+ }
316+ }
317+
318+ #endregion JSON Variables
0 commit comments