55
66namespace ImprovedLandmarks
77{
8+ public class WorldData
9+ {
10+ public List < CustomLandmark > landmarks = new List < CustomLandmark > ( ) ;
11+ public List < int > labeledObjects = new List < int > ( ) ;
12+ }
13+
814 public static class LandmarkManager
915 {
10- static Dictionary < string , List < CustomLandmark > > _allLandmarks = new Dictionary < string , List < CustomLandmark > > ( ) ;
16+ public static List < int > GetLabeledObjects ( string world )
17+ {
18+ if ( string . IsNullOrEmpty ( world ) ) world = "default" ;
19+ if ( ! _worlds . ContainsKey ( world ) ) return new List < int > ( ) ;
20+ return new List < int > ( _worlds [ world ] . labeledObjects ) ;
21+ }
22+
23+ public static void SetLabeledObjects ( string world , IEnumerable < int > objects )
24+ {
25+ if ( string . IsNullOrEmpty ( world ) ) world = "default" ;
26+ if ( ! _worlds . ContainsKey ( world ) ) _worlds [ world ] = new WorldData ( ) ;
27+ _worlds [ world ] . labeledObjects = new List < int > ( objects ) ;
28+ SaveAll ( ) ;
29+ }
30+ static Dictionary < string , WorldData > _worlds = new Dictionary < string , WorldData > ( ) ;
1131 static string _currentWorld = "" ;
1232
1333 public static List < CustomLandmark > Landmarks
@@ -16,16 +36,17 @@ public static List<CustomLandmark> Landmarks
1636 {
1737 if ( string . IsNullOrEmpty ( _currentWorld ) )
1838 return new List < CustomLandmark > ( ) ;
19- if ( ! _allLandmarks . ContainsKey ( _currentWorld ) )
20- _allLandmarks [ _currentWorld ] = new List < CustomLandmark > ( ) ;
21- return _allLandmarks [ _currentWorld ] ;
39+ if ( ! _worlds . ContainsKey ( _currentWorld ) )
40+ _worlds [ _currentWorld ] = new WorldData ( ) ;
41+ return _worlds [ _currentWorld ] . landmarks ;
2242 }
2343 }
2444
2545 public static string CurrentWorld => _currentWorld ;
2646
2747 static string _savePath ;
2848 static string _configPath ;
49+ static string _objectLabelsPath ;
2950
3051 public static void Load ( string modFolder )
3152 {
@@ -36,51 +57,73 @@ public static void Load(string modFolder)
3657 return ;
3758
3859 string json = File . ReadAllText ( _savePath ) ;
39-
60+ bool upgraded = false ;
4061 try
4162 {
42- var loaded = JsonConvert . DeserializeObject < Dictionary < string , List < CustomLandmark > > > ( json ) ;
63+ var loaded = JsonConvert . DeserializeObject < Dictionary < string , WorldData > > ( json ) ;
4364 if ( loaded != null )
4465 {
45- _allLandmarks = loaded ;
46- return ;
66+ _worlds = loaded ;
67+ upgraded = true ;
4768 }
4869 }
4970 catch { }
5071
51- try
72+ if ( ! upgraded )
5273 {
53- var legacy = JsonConvert . DeserializeObject < List < CustomLandmark > > ( json ) ;
54- if ( legacy != null && legacy . Count > 0 )
74+ try
5575 {
56- _allLandmarks [ "default" ] = legacy ;
57- SaveAll ( ) ;
76+ var loaded = JsonConvert . DeserializeObject < Dictionary < string , List < CustomLandmark > > > ( json ) ;
77+ if ( loaded != null )
78+ {
79+ foreach ( var kv in loaded )
80+ {
81+ if ( ! _worlds . ContainsKey ( kv . Key ) )
82+ _worlds [ kv . Key ] = new WorldData ( ) ;
83+ _worlds [ kv . Key ] . landmarks = kv . Value ;
84+ }
85+ upgraded = true ;
86+ SaveAll ( ) ;
87+ }
5888 }
89+ catch { }
90+ }
91+
92+ if ( ! upgraded )
93+ {
94+ try
95+ {
96+ var legacy = JsonConvert . DeserializeObject < List < CustomLandmark > > ( json ) ;
97+ if ( legacy != null && legacy . Count > 0 )
98+ {
99+ _worlds [ "default" ] = new WorldData { landmarks = legacy } ;
100+ SaveAll ( ) ;
101+ }
102+ }
103+ catch { }
59104 }
60- catch { }
61105 }
62106
63107 public static void SetCurrentWorld ( string worldName )
64108 {
65109 _currentWorld = string . IsNullOrEmpty ( worldName ) ? "default" : worldName ;
66- if ( ! _allLandmarks . ContainsKey ( _currentWorld ) )
67- _allLandmarks [ _currentWorld ] = new List < CustomLandmark > ( ) ;
110+ if ( ! _worlds . ContainsKey ( _currentWorld ) )
111+ _worlds [ _currentWorld ] = new WorldData ( ) ;
68112 }
69113
70114 static void SaveAll ( )
71115 {
72116 if ( _savePath == null )
73117 return ;
74118
75- // Remove empty or keyless entries before saving
76119 var toRemove = new List < string > ( ) ;
77- foreach ( var kv in _allLandmarks )
78- if ( string . IsNullOrEmpty ( kv . Key ) || kv . Value . Count == 0 )
120+ foreach ( var kv in _worlds )
121+ if ( string . IsNullOrEmpty ( kv . Key ) || ( kv . Value . landmarks . Count == 0 && kv . Value . labeledObjects . Count == 0 ) )
79122 toRemove . Add ( kv . Key ) ;
80123 foreach ( var key in toRemove )
81- _allLandmarks . Remove ( key ) ;
124+ _worlds . Remove ( key ) ;
82125
83- string json = JsonConvert . SerializeObject ( _allLandmarks , Formatting . Indented ) ;
126+ string json = JsonConvert . SerializeObject ( _worlds , Formatting . Indented ) ;
84127 File . WriteAllText ( _savePath , json ) ;
85128 }
86129
@@ -90,18 +133,19 @@ public static void RemoveWorld(string worldName)
90133 {
91134 if ( string . IsNullOrEmpty ( worldName ) )
92135 return ;
93- _allLandmarks . Remove ( worldName ) ;
136+ _worlds . Remove ( worldName ) ;
94137 SaveAll ( ) ;
95138 }
96139
97140 public static void RenameWorld ( string oldName , string newName )
98141 {
99142 if ( string . IsNullOrEmpty ( oldName ) || string . IsNullOrEmpty ( newName ) )
100143 return ;
101- if ( ! _allLandmarks . ContainsKey ( oldName ) )
102- return ;
103- _allLandmarks [ newName ] = _allLandmarks [ oldName ] ;
104- _allLandmarks . Remove ( oldName ) ;
144+ if ( _worlds . ContainsKey ( oldName ) )
145+ {
146+ _worlds [ newName ] = _worlds [ oldName ] ;
147+ _worlds . Remove ( oldName ) ;
148+ }
105149 if ( _currentWorld == oldName )
106150 _currentWorld = newName ;
107151 SaveAll ( ) ;
@@ -144,6 +188,24 @@ public static Vector2 LoadGuiPosition(Vector2 defaultPos)
144188
145189 public static bool ConfigExists => _configPath != null && File . Exists ( _configPath ) ;
146190
191+ public static bool IsObjectLabeled ( int branch )
192+ {
193+ if ( string . IsNullOrEmpty ( _currentWorld ) ) return false ;
194+ if ( ! _worlds . TryGetValue ( _currentWorld , out var data ) ) return false ;
195+ return data . labeledObjects . Contains ( branch ) ;
196+ }
197+
198+ public static void ToggleObjectLabel ( int branch )
199+ {
200+ if ( string . IsNullOrEmpty ( _currentWorld ) ) return ;
201+ if ( ! _worlds . ContainsKey ( _currentWorld ) )
202+ _worlds [ _currentWorld ] = new WorldData ( ) ;
203+ var list = _worlds [ _currentWorld ] . labeledObjects ;
204+ if ( ! list . Remove ( branch ) )
205+ list . Add ( branch ) ;
206+ SaveAll ( ) ;
207+ }
208+
147209 public static void SaveGuiPosition ( Vector2 pos )
148210 {
149211 if ( _configPath == null ) return ;
0 commit comments