1+ using System . Collections . Generic ;
2+ using System . IO ;
3+ using Newtonsoft . Json ;
4+
5+ namespace LethalDataAPI
6+ {
7+ public class LethalData
8+ {
9+ private static string dir = "" ;
10+
11+ public LethalData ( string modName )
12+ {
13+ string _dir = dir ;
14+
15+ dir = Path . Combine ( _dir , modName ) ;
16+
17+ if ( ! Directory . Exists ( dir ) )
18+ {
19+ Directory . CreateDirectory ( dir ) ;
20+ }
21+ }
22+
23+ public static void Save < T > ( string key , T value )
24+ {
25+ string currentSave = Plugin . currentSave ;
26+ string _dir = Path . Combine ( dir , currentSave + ".json" ) ;
27+
28+ Dictionary < string , object > currentData ;
29+
30+ if ( File . Exists ( _dir ) )
31+ {
32+ string jsonContent = File . ReadAllText ( _dir ) ;
33+ currentData = JsonConvert . DeserializeObject < Dictionary < string , object > > ( jsonContent ) ;
34+ }
35+ else
36+ {
37+ currentData = new Dictionary < string , object > ( ) ;
38+ using ( FileStream fs = File . Create ( _dir ) ) { }
39+ }
40+
41+ currentData [ key ] = value ;
42+
43+ string jsonData = JsonConvert . SerializeObject ( currentData , Formatting . Indented ) ;
44+ File . WriteAllText ( _dir , jsonData ) ;
45+ }
46+
47+ public static T ? Load < T > ( string key )
48+ {
49+ string currentSave = Plugin . currentSave ;
50+ string _dir = Path . Combine ( dir , currentSave + ".json" ) ;
51+
52+ Dictionary < string , object > currentData = new Dictionary < string , object > ( ) ;
53+
54+ if ( File . Exists ( _dir ) )
55+ {
56+ string jsonContent = File . ReadAllText ( _dir ) ;
57+ currentData = JsonConvert . DeserializeObject < Dictionary < string , object > > ( jsonContent ) ;
58+ }
59+ else
60+ {
61+ using ( FileStream fs = File . Create ( _dir ) ) { } ;
62+ }
63+
64+ if ( currentData . TryGetValue ( key , out var value ) )
65+ {
66+ return ( T ? ) value ;
67+ }
68+
69+ return ( T ? ) ( object ) null ;
70+ }
71+ }
72+ }
0 commit comments