@@ -22,132 +22,6 @@ public static class theInstanceManager
2222 // The Instances (Data)
2323 private static theInstance theInstance => CommonCore . theInstance ! ;
2424
25- public static void CheckSettings ( )
26- {
27- // Ensure AppData directory exists, with verbose logging and error handling.
28- try
29- {
30- if ( ! Directory . Exists ( CommonCore . AppDataPath ) )
31- {
32- AppDebug . Log ( "InstanceManager" , $ "AppData ({ CommonCore . AppDataPath } ) directory does not exist, creating it now.") ;
33- Directory . CreateDirectory ( CommonCore . AppDataPath ) ;
34- }
35-
36- if ( ! File . Exists ( lastKnownSettingsPath ) )
37- {
38- AppDebug . Log ( "InstanceManager" , $ "Settings file not found at { lastKnownSettingsPath } . Creating a new one with default values.") ;
39- theInstanceManager . SaveSettings ( ) ;
40- }
41- else
42- {
43- AppDebug . Log ( "InstanceManager" , $ "Settings file found at { lastKnownSettingsPath } . Loading settings.") ;
44- theInstanceManager . LoadSettings ( ) ;
45- }
46- }
47- catch ( Exception ex )
48- {
49- AppDebug . Log ( "InstanceManager" , $ "Error during settings check: { ex . Message } ") ;
50- throw ;
51- }
52- }
53- public static void LoadSettings ( bool external = false , string ? path = null )
54- {
55- string settingsPath = external && ! string . IsNullOrWhiteSpace ( path ) ? path : lastKnownSettingsPath ;
56- AppDebug . Log ( "InstanceManager" , $ "Loading settings from { ( external ? "external path" : "default path" ) } : { settingsPath } ") ;
57-
58- if ( ! File . Exists ( settingsPath ) )
59- {
60- AppDebug . Log ( "InstanceManager" , $ "Settings file not found at { settingsPath } . Using default settings.") ;
61- return ;
62- }
63-
64- try
65- {
66- var tempInstance = JsonSerializer . Deserialize < theInstance > ( File . ReadAllText ( settingsPath ) ) ;
67- if ( tempInstance == null ) return ;
68-
69- if ( ! external )
70- {
71- // Copy all properties from tempInstance to theInstance
72- foreach ( var prop in typeof ( theInstance ) . GetProperties ( ) )
73- {
74- if ( prop . CanRead && prop . CanWrite && prop . GetCustomAttributes ( typeof ( JsonIgnoreAttribute ) , true ) . Length == 0 )
75- {
76- var value = prop . GetValue ( tempInstance ) ;
77- prop . SetValue ( theInstance , value ) ;
78- }
79- }
80- theInstance . profileServerPath = tempInstance . profileServerPath != null ? Encoding . UTF8 . GetString ( Convert . FromBase64String ( tempInstance . profileServerPath ) ) : string . Empty ;
81-
82- theInstanceManager . GetServerVariables ( ) ;
83- }
84- else
85- {
86- theInstanceManager . GetServerVariables ( true , tempInstance ) ; // Get the variables from the tempInstance
87- }
88- }
89- catch ( Exception ex )
90- {
91- MessageBox . Show ( $ "Failed to load settings: { ex . Message } ", "Error" , MessageBoxButtons . OK , MessageBoxIcon . Error ) ;
92- AppDebug . Log ( "InstanceManager" , $ "Failed to load settings: { ex . Message } ") ;
93- }
94- }
95- public static void SaveSettings ( bool external = false , string ? path = null )
96- {
97- string settingsPath = external && ! string . IsNullOrWhiteSpace ( path ) ? path : lastKnownSettingsPath ;
98- try
99- {
100- var options = new JsonSerializerOptions
101- {
102- WriteIndented = true ,
103- DefaultIgnoreCondition = JsonIgnoreCondition . WhenWritingNull
104- } ;
105- theInstance tempInstance = new theInstance ( ) ;
106- foreach ( var prop in typeof ( theInstance ) . GetProperties ( ) )
107- {
108- if ( prop . CanRead && prop . CanWrite && prop . GetCustomAttributes ( typeof ( JsonIgnoreAttribute ) , true ) . Length == 0 )
109- {
110- var value = prop . GetValue ( theInstance ) ;
111- if ( value != null )
112- {
113- prop . SetValue ( tempInstance , value ) ;
114- }
115- }
116- }
117- // Only encode if not null or empty
118- if ( ! string . IsNullOrEmpty ( tempInstance . profileServerPath ) )
119- {
120- tempInstance . profileServerPath = Convert . ToBase64String ( Encoding . UTF8 . GetBytes ( tempInstance . profileServerPath ) ) ;
121- }
122- string json = JsonSerializer . Serialize ( tempInstance , options ) ;
123- File . WriteAllText ( settingsPath , json ) ;
124- }
125- catch ( Exception ex )
126- {
127- MessageBox . Show ( $ "Failed to save settings: { ex . Message } ", "Error" , MessageBoxButtons . OK , MessageBoxIcon . Error ) ;
128- AppDebug . Log ( "InstanceManager" , $ "Failed to save settings: { ex . Message } ") ;
129- }
130- }
131- public static void ExportSettings ( )
132- {
133- // Export the settings to a JSON file using a ShowFileDialog from CoreManager
134- string savePath = CommonCore . AppDataPath ;
135- string exportPath = Functions . ShowFileDialog ( true , "Server Settings (*.svset)|*.svset|All files (*.*)|*.*" , "Export Server Settings" , savePath , "exportedServerSettings.svset" ) ! ;
136- if ( ! string . IsNullOrEmpty ( exportPath ) )
137- {
138- theInstanceManager . SaveSettings ( true , exportPath ) ; // Save the settings to the specified path
139- }
140-
141- }
142- public static void ImportSettings ( )
143- {
144- string savePath = CommonCore . AppDataPath ;
145- string importPath = Functions . ShowFileDialog ( true , "Server Settings (*.svset)|*.svset|All files (*.*)|*.*" , "Import Server Settings" , savePath , "importServerSettings.svset" ) ! ;
146- if ( ! string . IsNullOrEmpty ( importPath ) )
147- {
148- theInstanceManager . LoadSettings ( true , importPath ) ;
149- }
150- }
15125 public static bool ValidateGameServerPath ( )
15226 {
15327 // Validate the profile server path
@@ -170,16 +44,6 @@ public static void GetServerVariables(bool import = false, theInstance updatedIn
17044 thisServer . StatsTab . functionEvent_GetStatSettings ( ( updatedInstance != null ? updatedInstance : null ! ) ) ;
17145
17246 }
173- public static void ValidateGameServerType ( string serverPath )
174- {
175- // Additional checks can be added here if necessary
176- if ( File . Exists ( Path . Combine ( serverPath , "EXP1.pff" ) ) )
177- {
178- thisInstance . profileServerType = 1 ; // BHDTS
179- return ;
180- }
181- thisInstance . profileServerType = 0 ; // BHD
182- }
18347 public static void UpdateGameServer ( )
18448 {
18549 // the following code will replace line by line until all variables have a "update" function in ServerMemory
@@ -392,35 +256,5 @@ public static void changeTeamGameMode(int currentMapType, int nextMapType)
392256
393257 }
394258
395- public static void HighlightDifferences ( )
396- {
397- // TO DO: Remove the need for HighlightDifferences in theInstanceManager.
398- }
399-
400- // Helper methods for highlighting
401- public static void HighlightTextBox ( TextBox tb , string value )
402- {
403- tb . BackColor = tb . Text == value ? SystemColors . Window : Color . LightYellow ;
404- }
405-
406- public static void HighlightComboBox ( ComboBox cb , string value )
407- {
408- cb . BackColor = cb . Text == value ? SystemColors . Window : Color . LightYellow ;
409- }
410-
411- public static void HighlightComboBoxIndex ( ComboBox cb , int value )
412- {
413- cb . BackColor = cb . SelectedIndex == value ? SystemColors . Window : Color . LightYellow ;
414- }
415-
416- public static void HighlightNumericUpDown ( NumericUpDown num , int value )
417- {
418- num . BackColor = ( int ) num . Value == value ? SystemColors . Window : Color . LightYellow ;
419- }
420-
421- public static void HighlightCheckBox ( CheckBox cb , bool value )
422- {
423- cb . BackColor = cb . Checked == value ? SystemColors . Control : Color . LightYellow ;
424- }
425259 }
426260}
0 commit comments