@@ -12,7 +12,7 @@ namespace Battlehub.RTSaveLoad2
1212{
1313 public class PersistentObjectMapperWindow : EditorWindow
1414 {
15-
15+
1616 [ MenuItem ( "Tools/Runtime SaveLoad2/Persistent Objects Mapper" ) ]
1717 public static void ShowMenuItem ( )
1818 {
@@ -72,7 +72,7 @@ private bool IsNoneSelected
7272 {
7373 get { return m_selectedCount == 0 ; }
7474 }
75-
75+
7676 private int m_selectedCount ;
7777 private bool [ ] m_isUOTypeSelected ;
7878 private bool [ ] m_isUOTypeExpanded ;
@@ -93,11 +93,11 @@ private void Awake()
9393 List < string > assemblyNames = new List < string > { "All" } ;
9494 List < Assembly > assemblies = new List < Assembly > ( ) { null } ;
9595
96- for ( int i = 0 ; i < m_assemblies . Length ; ++ i )
96+ for ( int i = 0 ; i < m_assemblies . Length ; ++ i )
9797 {
9898 Assembly assembly = m_assemblies [ i ] ;
9999 Type [ ] uoTypes = assembly . GetTypes ( ) . Where ( t => t . IsSubclassOf ( typeof ( UnityObject ) ) ) . ToArray ( ) ;
100- if ( uoTypes . Length > 0 )
100+ if ( uoTypes . Length > 0 )
101101 {
102102 assemblies . Add ( assembly ) ;
103103 assemblyNames . Add ( assembly . GetName ( ) . Name ) ;
@@ -130,10 +130,9 @@ private void Awake()
130130
131131 m_assemblies = assemblies . ToArray ( ) ;
132132 m_assemblyNames = assemblyNames . ToArray ( ) ;
133-
134133
135- //m_map = Resources.Load<EditorsMapStorage>(EditorsMapStorage.EditorsMapPrefabName );
136- //EditorsMap.LoadMap();
134+ LoadMappings ( ) ;
135+
137136 }
138137
139138 private void OnGUI ( )
@@ -163,24 +162,24 @@ private void OnGUI()
163162
164163 EditorGUI . BeginChangeCheck ( ) ;
165164
166- if ( IsAllSelected )
165+ if ( IsAllSelected )
167166 {
168167 GUILayout . Toggle ( true , "Select All" ) ;
169168 }
170- else if ( IsNoneSelected )
169+ else if ( IsNoneSelected )
171170 {
172171 GUILayout . Toggle ( false , "Select All" ) ;
173172 }
174173 else
175174 {
176175 GUILayout . Toggle ( false , "Select All" , "ToggleMixed" ) ;
177176 }
178-
179- if ( EditorGUI . EndChangeCheck ( ) )
177+
178+ if ( EditorGUI . EndChangeCheck ( ) )
180179 {
181180 if ( IsAllSelected )
182181 {
183- for ( int i = 0 ; i < m_isUOTypeSelected . Length ; ++ i )
182+ for ( int i = 0 ; i < m_isUOTypeSelected . Length ; ++ i )
184183 {
185184 m_isUOTypeSelected [ i ] = false ;
186185 ExpandOrCollapseType ( i ) ;
@@ -217,20 +216,41 @@ private void OnGUI()
217216
218217 EditorGUI . BeginChangeCheck ( ) ;
219218 GUILayout . Button ( "Create Persistent Objects" ) ;
220- if ( EditorGUI . EndChangeCheck ( ) )
219+ if ( EditorGUI . EndChangeCheck ( ) )
221220 {
222221 SaveMappings ( ) ;
223222 }
224223
225224 EditorGUILayout . EndHorizontal ( ) ;
226225 }
227226
227+ private void LoadMappings ( )
228+ {
229+ GameObject storageGO = ( GameObject ) AssetDatabase . LoadAssetAtPath ( ObjectMappingsStoragePath , typeof ( GameObject ) ) ;
230+ if ( storageGO != null )
231+ {
232+ PersistentObjectMapping [ ] mappings = storageGO . GetComponentsInChildren < PersistentObjectMapping > ( true ) ;
233+ for ( int i = 0 ; i < mappings . Length ; ++ i )
234+ {
235+ PersistentObjectMapping objectMapping = mappings [ i ] ;
236+ Type type = Type . GetType ( objectMapping . MappedNamespace + "." + objectMapping . MappedTypeName , false , true ) ;
237+ int typeIndex ;
238+ if ( type != null && m_typeToIndex . TryGetValue ( type , out typeIndex ) )
239+ {
240+ PersistentPropertyMapping [ ] pMappings = objectMapping . PropertyMappings ;
241+
242+
243+ }
244+ }
245+ }
246+ }
247+
228248 private void SaveMappings ( )
229249 {
230250 GameObject storageGO = new GameObject ( ) ;
231- for ( int typeIndex = 0 ; typeIndex < m_propertyMappings . GetLength ( 0 ) ; ++ typeIndex )
251+ for ( int typeIndex = 0 ; typeIndex < m_propertyMappings . GetLength ( 0 ) ; ++ typeIndex )
232252 {
233- if ( ! m_isUOTypeSelected [ typeIndex ] )
253+ if ( ! m_isUOTypeSelected [ typeIndex ] )
234254 {
235255 continue ;
236256 }
@@ -242,9 +262,9 @@ private void SaveMappings()
242262 PersistentObjectMapping objectMapping = typeStorageGO . AddComponent < PersistentObjectMapping > ( ) ;
243263
244264 List < PersistentPropertyMapping > selectedPropertyMappings = new List < PersistentPropertyMapping > ( ) ;
245- for ( int i = 0 ; i < propertyMappings . Length ; ++ i )
265+ for ( int i = 0 ; i < propertyMappings . Length ; ++ i )
246266 {
247- if ( ! m_isPropertyMappingsEnabled [ typeIndex ] [ i ] )
267+ if ( ! m_isPropertyMappingsEnabled [ typeIndex ] [ i ] )
248268 {
249269 continue ;
250270 }
@@ -266,7 +286,7 @@ private void SaveMappings()
266286 private int GetAncestorsCount ( Type type )
267287 {
268288 int count = 0 ;
269- while ( type . BaseType != typeof ( UnityObject ) )
289+ while ( type . BaseType != typeof ( UnityObject ) )
270290 {
271291 count ++ ;
272292 type = type . BaseType ;
@@ -287,7 +307,7 @@ private void DrawTypeEditor(int rootTypeIndex, int typeIndex, int indent = 1)
287307 EditorGUI . BeginChangeCheck ( ) ;
288308 m_isUOTypeSelected [ typeIndex ] = EditorGUILayout . Toggle ( m_isUOTypeSelected [ typeIndex ] , GUILayout . Width ( indent * 15 ) ) ;
289309 isSelectionChanged = EditorGUI . EndChangeCheck ( ) ;
290-
310+
291311 EditorGUI . BeginChangeCheck ( ) ;
292312 if ( indent == 1 )
293313 {
@@ -372,68 +392,133 @@ private void ExpandOrCollapseType(int typeIndex)
372392 }
373393 else
374394 {
375- CollapseType ( typeIndex ) ;
395+ // CollapseType(typeIndex);
376396 }
377397 }
378398
379- private void CollapseType ( int typeIndex )
380- {
381- m_propertyMappings [ typeIndex ] = null ;
382- m_isPropertyMappingsEnabled [ typeIndex ] = null ;
383- m_propertyMappingNames [ typeIndex ] = null ;
384- m_propertyMappingsSelection [ typeIndex ] = null ;
385- }
399+ // private void CollapseType(int typeIndex)
400+ // {
401+ // m_propertyMappings[typeIndex] = null;
402+ // m_isPropertyMappingsEnabled[typeIndex] = null;
403+ // m_propertyMappingNames[typeIndex] = null;
404+ // m_propertyMappingsSelection[typeIndex] = null;
405+ // }
386406
387407 private void ExpandType ( int typeIndex )
388408 {
389409 Type type = m_uoTypes [ typeIndex ] ;
410+ Dictionary < string , PersistentPropertyMapping > existingFieldMappings =
411+ m_propertyMappings [ typeIndex ] != null ?
412+ m_propertyMappings [ typeIndex ] . Where ( p => ! p . IsProperty ) . ToDictionary ( p => p . MappedType + " " + p . MappedName ) :
413+ new Dictionary < string , PersistentPropertyMapping > ( ) ;
414+
415+ Dictionary < string , PersistentPropertyMapping > existingPropertyMappings =
416+ m_propertyMappings [ typeIndex ] != null ?
417+ m_propertyMappings [ typeIndex ] . Where ( p => p . IsProperty ) . ToDictionary ( p => p . MappedType + " " + p . MappedName ) :
418+ new Dictionary < string , PersistentPropertyMapping > ( ) ;
419+
390420 List < PersistentPropertyMapping > pMappings = new List < PersistentPropertyMapping > ( ) ;
391421 List < bool > pMappingsEnabled = new List < bool > ( ) ;
392422 FieldInfo [ ] fields = type . GetFields ( ) ;
393423 for ( int f = 0 ; f < fields . Length ; ++ f )
394424 {
395425 FieldInfo fInfo = fields [ f ] ;
396- PersistentPropertyMapping pMapping = new PersistentPropertyMapping ( ) ;
397- pMapping . PersistentName = fInfo . Name ;
398- pMapping . PersistentType = fInfo . FieldType . FullName ;
399- pMapping . MappedName = fInfo . Name ;
400- pMapping . MappedType = fInfo . FieldType . FullName ;
401- pMapping . IsProperty = false ;
402- pMapping . IsMappedProperty = false ;
403426
404- pMapping . UseBuiltInCodeSnippet = false ;
405- pMapping . BuiltInCodeSnippet = null ;
427+ string key = fInfo . FieldType . FullName + " " + fInfo . Name ;
428+
429+ PersistentPropertyMapping pMapping ;
430+ if ( existingFieldMappings . TryGetValue ( key , out pMapping ) )
431+ {
432+ pMappingsEnabled . Add ( true ) ;
433+
434+ existingFieldMappings . Remove ( key ) ;
435+ }
436+ else
437+ {
438+ pMapping = new PersistentPropertyMapping ( ) ;
439+ pMapping . PersistentName = fInfo . Name ;
440+ pMapping . PersistentType = fInfo . FieldType . FullName ;
441+ pMapping . MappedName = fInfo . Name ;
442+ pMapping . MappedType = fInfo . FieldType . FullName ;
443+ pMapping . IsProperty = false ;
444+
445+ pMapping . UseBuiltInCodeSnippet = false ;
446+ pMapping . BuiltInCodeSnippet = null ;
447+
448+ pMappingsEnabled . Add ( false ) ;
449+ }
406450
407451 pMappings . Add ( pMapping ) ;
408- pMappingsEnabled . Add ( false ) ;
452+ }
453+
454+ {
455+ PersistentPropertyMapping [ ] deprecatedMappings = existingFieldMappings . Values . ToArray ( ) ;
456+ existingFieldMappings . Clear ( ) ;
457+ for ( int i = 0 ; i < deprecatedMappings . Length ; ++ i )
458+ {
459+ PersistentPropertyMapping deprecatedMapping = deprecatedMappings [ i ] ;
460+ deprecatedMapping . MappedName = null ;
461+ deprecatedMapping . MappedType = null ;
462+ pMappings . Add ( deprecatedMapping ) ;
463+ }
409464 }
410465
411466 PropertyInfo [ ] properties = type . GetProperties ( BindingFlags . Public | BindingFlags . Instance | BindingFlags . DeclaredOnly ) . Where ( p => p . GetGetMethod ( ) != null && p . GetSetMethod ( ) != null ) . ToArray ( ) ;
412467 for ( int p = 0 ; p < properties . Length ; ++ p )
413468 {
414469 PropertyInfo pInfo = properties [ p ] ;
415- PersistentPropertyMapping pMapping = new PersistentPropertyMapping ( ) ;
416- pMapping . PersistentName = pInfo . Name ; //property name of mapping
417- pMapping . PersistentType = pInfo . PropertyType . FullName ;
418- pMapping . MappedName = pInfo . Name ; //property name of unity type
419- pMapping . MappedType = pInfo . PropertyType . FullName ;
420- pMapping . IsProperty = true ;
421- pMapping . IsMappedProperty = true ;
422470
423- pMapping . UseBuiltInCodeSnippet = false ;
424- pMapping . BuiltInCodeSnippet = null ;
471+ string key = pInfo . PropertyType . FullName + " " + pInfo . Name ;
472+
473+ PersistentPropertyMapping pMapping ;
474+ if ( existingPropertyMappings . TryGetValue ( key , out pMapping ) )
475+ {
476+ pMappingsEnabled . Add ( true ) ;
477+
478+ existingPropertyMappings . Remove ( key ) ;
479+ }
480+ else
481+ {
482+ pMapping = new PersistentPropertyMapping ( ) ;
483+ pMapping . PersistentName = pInfo . Name ; //property name of mapping
484+ pMapping . PersistentType = pInfo . PropertyType . FullName ;
485+ pMapping . MappedName = pInfo . Name ; //property name of unity type
486+ pMapping . MappedType = pInfo . PropertyType . FullName ;
487+ pMapping . IsProperty = true ;
488+
489+ pMapping . UseBuiltInCodeSnippet = false ;
490+ pMapping . BuiltInCodeSnippet = null ;
491+
492+ pMappingsEnabled . Add ( false ) ;
493+ }
425494
426495 pMappings . Add ( pMapping ) ;
427- pMappingsEnabled . Add ( true ) ;
496+ }
497+
498+ {
499+ PersistentPropertyMapping [ ] deprecatedMappings = existingPropertyMappings . Values . ToArray ( ) ;
500+ existingFieldMappings . Clear ( ) ;
501+ for ( int i = 0 ; i < deprecatedMappings . Length ; ++ i )
502+ {
503+ PersistentPropertyMapping deprecatedMapping = deprecatedMappings [ i ] ;
504+ deprecatedMapping . MappedName = null ;
505+ deprecatedMapping . MappedType = null ;
506+ pMappings . Add ( deprecatedMapping ) ;
507+ }
428508 }
429509
430510 m_propertyMappings [ typeIndex ] = pMappings . ToArray ( ) ;
431511 m_isPropertyMappingsEnabled [ typeIndex ] = pMappingsEnabled . ToArray ( ) ;
432- m_propertyMappingNames [ typeIndex ] = pMappings . Select ( m => m . MappedName ) . ToArray ( ) ;
512+ m_propertyMappingNames [ typeIndex ] = pMappings . Where ( m => m . MappedName != null ) . Select ( m => m . MappedName ) . ToArray ( ) ;
433513 m_propertyMappingsSelection [ typeIndex ] = new int [ pMappings . Count ] ;
514+
515+ string [ ] mappedKeys = pMappings . Where ( m => m . MappedType != null && m . MappedName != null ) . Select ( m => m . MappedType + " " + m . MappedName ) . ToArray ( ) ;
516+
434517 for ( int i = 0 ; i < m_propertyMappingsSelection [ typeIndex ] . Length ; ++ i )
435518 {
436- m_propertyMappingsSelection [ typeIndex ] [ i ] = i ;
519+ PersistentPropertyMapping mapping = m_propertyMappings [ typeIndex ] [ i ] ;
520+
521+ m_propertyMappingsSelection [ typeIndex ] [ i ] = Array . IndexOf ( mappedKeys , mapping . MappedType + " " + mapping . MappedName ) ;
437522 }
438523 }
439524 }
0 commit comments