@@ -14,6 +14,7 @@ internal class modSync
1414 private static string Sync_ServerURL ;
1515 private static string Sync_SandstormToken ;
1616 private static string Sync_AccessKey ;
17+ private static int Sync_LastSuccessful ;
1718
1819 /// <summary>
1920 /// Loads the sync module: Schedules heartbeats
@@ -60,18 +61,28 @@ public static void Unload()
6061 public static string RemoteRekey ( string NewServerURL , string NewSandstormToken )
6162 {
6263 int SuccessCount = 0 ;
64+ Sync_LastSuccessful = 0 ;
65+ string OldServerURL = Sync_ServerURL ;
66+ string OldSandstormToken = Sync_SandstormToken ;
6367 Sync_ServerURL = NewServerURL ;
6468 Sync_SandstormToken = NewSandstormToken ;
6569 SuccessCount += modDatabase . UpdateConfig ( new modDatabase . Config { Key = "Sync_ServerURL" , Value = NewServerURL } ) ;
6670 SuccessCount += modDatabase . UpdateConfig ( new modDatabase . Config { Key = "Sync_SandstormToken" , Value = NewSandstormToken } ) ;
67- if ( SuccessCount == 2 )
71+ SendMessage ( "server" , "fetch" , "none" ) ;
72+ Thread . Sleep ( 2000 ) ;
73+ SuccessCount += Sync_LastSuccessful ;
74+ if ( SuccessCount == 3 )
6875 {
6976 modLogging . LogEvent ( "Sync remotely rekeyed" , EventLogEntryType . Warning , 6003 ) ;
7077 return "Remote rekey successful" ;
7178 }
7279 else
7380 {
74- modLogging . LogEvent ( "Sync was remotely rekeyed, but it was unsuccessful" , EventLogEntryType . Error , 6004 ) ;
81+ modLogging . LogEvent ( "Sync attempted a remote rekey, but it was unsuccessful" , EventLogEntryType . Error , 6004 ) ;
82+ Sync_ServerURL = OldServerURL ;
83+ Sync_SandstormToken = OldSandstormToken ;
84+ modDatabase . UpdateConfig ( new modDatabase . Config { Key = "Sync_ServerURL" , Value = OldServerURL } ) ;
85+ modDatabase . UpdateConfig ( new modDatabase . Config { Key = "Sync_SandstormToken" , Value = OldSandstormToken } ) ;
7586 return "Remote rekey failed" ;
7687 }
7788 }
@@ -85,7 +96,7 @@ public static string RemoteRekey(string NewServerURL, string NewSandstormToken)
8596 /// <param name="ExtendedMessage">(string) Contents of extended message data</param>
8697 public static async void SendMessage ( string Destination , string MessageType , string Message , string ExtendedMessage = "" )
8798 {
88- // TODO Only send messages if we are online
99+ // TODO: Only send messages if we are online
89100 try
90101 {
91102 HttpClient MessageClient = new HttpClient ( ) ;
@@ -103,10 +114,12 @@ public static async void SendMessage(string Destination, string MessageType, str
103114 if ( MessageResponse . IsSuccessStatusCode != true )
104115 {
105116 modLogging . LogEvent ( "Sync error:" + ( int ) MessageResponse . StatusCode + " " + MessageResponse . StatusCode , EventLogEntryType . Error , 6002 ) ;
117+ Sync_LastSuccessful = 0 ;
106118 }
107119 else
108120 {
109121 string ResponseContent = await MessageResponse . Content . ReadAsStringAsync ( ) ;
122+ Sync_LastSuccessful = 1 ;
110123 // VERBOSE: Uncomment to show JSON response from server
111124 // modLogging.LogEvent((int)MessageResponse.StatusCode + " " + MessageResponse.StatusCode + " " + ResponseContent, EventLogEntryType.Information);
112125
@@ -127,6 +140,7 @@ public static async void SendMessage(string Destination, string MessageType, str
127140 }
128141 catch ( Exception err )
129142 {
143+ Sync_LastSuccessful = 0 ;
130144 modLogging . LogEvent ( err . Message + "\n \n " + err . StackTrace , EventLogEntryType . Error , 6002 ) ;
131145 }
132146 }
0 commit comments