@@ -17,15 +17,22 @@ public class AndroidUpgradeManager : MonoBehaviour
1717 public string LastInstalledVersion { get { return PlayerPrefs . GetString ( "LastInstalledVersion" , "0.0.0.0" ) ; } private set { PlayerPrefs . SetString ( "LastInstalledVersion" , value ) ; } }
1818 private IEnumerator Start ( )
1919 {
20- yield return new WaitForSeconds ( 1 ) ;
21- #if UNITY_ANDROID
20+ yield return new WaitForSecondsRealtime ( 1 ) ;
2221 // check if last installed version is different from current version on Player Settings
2322 if ( LastInstalledVersion != VersionInfo . DaggerfallUnityForAndroidVersion )
2423 {
2524 UpdateSettings ( ) ;
2625 LastInstalledVersion = VersionInfo . DaggerfallUnityForAndroidVersion ;
2726 }
28- #endif
27+ // check if initial-default-layout.json is outdated
28+ string initialDefaultLayoutPath = Path . Combine ( Paths . PersistentDataPath , "initial-default-layout.json" ) ;
29+ var layoutConfig = File . Exists ( initialDefaultLayoutPath ) ? TouchscreenLayoutConfiguration . ReadFromPath ( initialDefaultLayoutPath ) : null ;
30+ if ( layoutConfig == null || layoutConfig . buttons == null || layoutConfig . buttons . Count == 0
31+ || layoutConfig . buttons [ 0 ] . Version < TouchscreenButtonConfiguration . LatestVersion )
32+ {
33+ Debug . Log ( "AndroidUpgradeManager: Migrating touchscreen layout due to initial-default-layout.json" ) ;
34+ MigrateDefaultLayout ( ) ;
35+ }
2936 }
3037
3138 private void UpdateSettings ( )
@@ -38,10 +45,9 @@ private void UpdateSettings()
3845 bool isRC = versionSplit . Length > 4 ;
3946 Debug . Log ( $ "AndroidUpgradeManager: { first } { second } { third } { fourth } { isRC } ") ;
4047
41- // Handle layout migration for versions before 1.1.1.7
42- if ( first < 1 || ( first == 1 && second < 1 ) || ( first == 1 && second == 1 && third < 1 ) || ( first == 1 && second == 1 && third == 1 && fourth < 7 ) )
48+ if ( first < 1 || ( first == 1 && second < 1 ) || ( first == 1 && second == 1 && third < 1 ) || ( first == 1 && second == 1 && third == 1 && fourth < 7 ) )
4349 {
44- Debug . Log ( "AndroidUpgradeManager: Migrating touchscreen layout from pre-1.1.1.7 version" ) ;
50+ Debug . Log ( "AndroidUpgradeManager: Migrating touchscreen layout due to version" ) ;
4551 MigrateDefaultLayout ( ) ;
4652 }
4753
@@ -70,67 +76,70 @@ private void MigrateDefaultLayout()
7076 {
7177 string layoutsPath = TouchscreenLayoutsManager . LayoutsPath ;
7278 string defaultLayoutPath = Path . Combine ( layoutsPath , "default-layout" ) ;
73- string myLayout1Path = Path . Combine ( layoutsPath , "my-layout1" ) ;
74-
75- // Check if the user has a customized default-layout and my-layout1 doesn't already exist
76- if ( Directory . Exists ( defaultLayoutPath ) && ! Directory . Exists ( myLayout1Path ) )
79+ // Find the next available my-layoutN directory
80+ int layoutIndex = 1 ;
81+ string myLayoutNPath ;
82+ do
83+ {
84+ myLayoutNPath = Path . Combine ( layoutsPath , $ "my-layout{ layoutIndex } ") ;
85+ layoutIndex ++ ;
86+ } while ( Directory . Exists ( myLayoutNPath ) ) ;
87+ // Check if the user has a customized default-layout
88+ if ( Directory . Exists ( defaultLayoutPath ) )
7789 {
7890 try
7991 {
80- Debug . Log ( "AndroidUpgradeManager: Migrating default-layout to my-layout1" ) ;
81-
82- // Create the new directory
83- Directory . CreateDirectory ( myLayout1Path ) ;
84-
85- // Copy all files and subdirectories from default-layout to my-layout1
92+ // Create the new directory for the migrated layout
93+ Debug . Log ( $ "AndroidUpgradeManager: Migrating default-layout to { Path . GetFileName ( myLayoutNPath ) } ") ;
94+ Directory . CreateDirectory ( myLayoutNPath ) ;
95+
96+ // Copy all files and subdirectories from default-layout to my-layoutN
8697 foreach ( string dirPath in Directory . GetDirectories ( defaultLayoutPath , "*" , SearchOption . AllDirectories ) )
8798 {
88- Directory . CreateDirectory ( dirPath . Replace ( defaultLayoutPath , myLayout1Path ) ) ;
99+ Directory . CreateDirectory ( dirPath . Replace ( defaultLayoutPath , myLayoutNPath ) ) ;
89100 }
90-
91101 foreach ( string filePath in Directory . GetFiles ( defaultLayoutPath , "*.*" , SearchOption . AllDirectories ) )
92102 {
93- string newFilePath = filePath . Replace ( defaultLayoutPath , myLayout1Path ) ;
103+ string newFilePath = filePath . Replace ( defaultLayoutPath , myLayoutNPath ) ;
94104 File . Copy ( filePath , newFilePath , true ) ;
95105 }
96-
106+
97107 // Update the JSON file name and contents
98- string oldJsonPath = Path . Combine ( myLayout1Path , "default-layout.json" ) ;
99- string newJsonPath = Path . Combine ( myLayout1Path , "my-layout1.json" ) ;
100-
108+ string oldJsonPath = Path . Combine ( myLayoutNPath , "default-layout.json" ) ;
109+ string newJsonPath = Path . Combine ( myLayoutNPath , $ "my-layout{ layoutIndex - 1 } .json") ;
101110 if ( File . Exists ( oldJsonPath ) )
102111 {
103112 // Read the layout configuration and update its name
104113 var layoutConfig = TouchscreenLayoutConfiguration . ReadFromPath ( oldJsonPath ) ;
105114 if ( layoutConfig != null )
106115 {
107- layoutConfig . name = "my-layout1 " ;
116+ layoutConfig . name = $ "my-layout { layoutIndex - 1 } ";
108117 TouchscreenLayoutConfiguration . WriteToPath ( layoutConfig , newJsonPath ) ;
109118 File . Delete ( oldJsonPath ) ;
110-
119+
111120 // Set this as the selected layout and load it
112- TouchscreenLayoutsManager . LastSelectedLayout = "my-layout1 " ;
121+ TouchscreenLayoutsManager . LastSelectedLayout = $ "my-layout { layoutIndex - 1 } ";
113122 TouchscreenLayoutsManager . Instance . LoadLastSelectedOrDefaultLayout ( ) ;
114- Debug . Log ( "AndroidUpgradeManager: Successfully migrated default-layout to my-layout1 " ) ;
123+ Debug . Log ( $ "AndroidUpgradeManager: Successfully migrated default-layout to my-layout { layoutIndex - 1 } ") ;
115124 }
116125 else
117126 {
118127 Debug . LogWarning ( "AndroidUpgradeManager: Could not read layout configuration during migration" ) ;
119128 }
120129 }
121-
122- // Regenerate the default layout
130+
131+ // Regenerate the default layout so the user still has a default-layout
123132 TouchscreenLayoutsManager . Instance . RegenerateBrokenDefaultLayout ( "default-layout" , false ) ;
124133 }
125134 catch ( System . Exception e )
126135 {
127136 Debug . LogError ( $ "AndroidUpgradeManager: Error migrating layout: { e . Message } ") ;
128137 // If migration fails, make sure we don't leave things in a broken state
129- if ( Directory . Exists ( myLayout1Path ) )
138+ if ( Directory . Exists ( myLayoutNPath ) )
130139 {
131140 try
132141 {
133- Directory . Delete ( myLayout1Path , true ) ;
142+ Directory . Delete ( myLayoutNPath , true ) ;
134143 }
135144 catch ( System . Exception rollbackError )
136145 {
0 commit comments