Ejemplo n.º 1
0
        public Dialog_ModSync_Window()
        {
            MSLog.Log("New window", MSLog.Level.All);
            try
            {
                CurrSyncState = CurrentSyncState.RequestStarted;
                ClientInternetConnectivity = InternetConnectivity.Unchecked;
                closeOnClickedOutside      = false;
                FetchRelevantMods();

                string userRequest = NetworkManager.GenerateServerRequestString(_relevantMods);
                _userRequestStr = Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(userRequest));
                NetworkManager.CheckForInternetConnectionAsync(
                    this.ClientInternetConnectivity, (InternetConnectivity clientInternetConnectivity) =>
                {
                    this.ClientInternetConnectivity = clientInternetConnectivity;
                    this.OnNetworkConnectionTestCompleted();
                });
            }
            catch (Exception e)
            {
                MSLog.Log("ERROR:", MSLog.Level.All);
                MSLog.Log(e.Message, MSLog.Level.All);
            }
        }
Ejemplo n.º 2
0
 public void OnNetworkConnectionTestCompleted()
 {
     // load data from server
     MSLog.Log("OnNetworkConnectionTestCompleted", MSLog.Level.All);
     if (ClientInternetConnectivity == InternetConnectivity.Online)
     {
         NetworkManager.CreateRequestHttpRequest(
             _userRequestStr,
             (CurrentSyncState syncState) => this.CurrSyncState = syncState,
             (string responseStr, bool success, string errorCode) =>
             this.OnRequestFromServerResponse(responseStr, success, errorCode));
     }
     else
     {
         CurrSyncState = CurrentSyncState.ClientOffline;
     }
 }
Ejemplo n.º 3
0
 private void OnSyncStateChanged()
 {
     Debug.WriteLine($"SyncStateChanged event triggered with state {CurrentSyncState.ToString()}");
     SyncStateChanged?.Invoke(this, CurrentSyncState);
 }
Ejemplo n.º 4
0
        public void OnRequestFromServerResponse(string responseStr, bool success, string errorCode = "")
        {
            MSLog.Log("OnRequestFromServerResponse, GOT: response:" + responseStr + "\nSuccess: " + success + "\nErrorCode: " + errorCode);
            if (!success)
            {
                // server not offline, this is a real error
                if (CurrSyncState != CurrentSyncState.ModSyncOffline)
                {
                    CurrSyncState = CurrentSyncState.ModSyncError;
                    _errorCode    = errorCode;
                }
            }
            else
            {
                // clear previous errors
                _errorCode = String.Empty;
                // empty response
                if (responseStr.Length == 0)
                {
                    CurrSyncState = CurrentSyncState.Done;
                    return;
                }
                string responseData;
                ModDetailsResponse[] responseDataObj;
                try
                {
                    responseData = Encoding.UTF8.GetString(Convert.FromBase64String(responseStr));
                }
                catch (Exception e)
                {
                    // Failed to parse BASE 64
                    MSLog.Log("Failed to parse BASE 64");
                    MSLog.Log(e.Message);
                    CurrSyncState = CurrentSyncState.ModSyncError;
                    _errorCode    = "151";
                    return;
                }
                try
                {
                    MSLog.Log("Response data BASE 64 set: " + responseData);
                    string[] rows = Regex.Split(responseData.Trim('\"'), "{%}");
                    if (rows.Length == 0)
                    {
                        CurrSyncState = CurrentSyncState.Done;
                        return;
                    }
                    MSLog.Log("Trying to parse data rows");
                    try
                    {
                        foreach (string row in rows)
                        {
                            string rowData = Encoding.UTF8.GetString(Convert.FromBase64String(row));
                            MSLog.Log("Raw row data: " + rowData);
                            ModDetailsResponse rowDataParsed = JsonUtility.FromJson <ModDetailsResponse>(rowData);
                            if (rowDataParsed != null)
                            {
                                MSLog.Log("MF:" + rowDataParsed.MF + " V:" + rowDataParsed.V + " S" +
                                          rowDataParsed.S + " SB" + rowDataParsed.SB);

                                var modData = _relevantMods.FirstOrDefault(x => x.ModDirName.ToUpper().Equals(rowDataParsed.MF.ToUpper()));
                                if (modData == null)
                                {
                                    continue;
                                }
                                modData.RemoteData                = new RemoteModData();
                                modData.IsModSyncMod              = true;
                                modData.RemoteData.IsModSyncMod   = true;
                                modData.RemoteData.Version        = rowDataParsed.V;
                                modData.RemoteData.DownloadUrl    = rowDataParsed.S;
                                modData.RemoteData.IsSaveBreaking = rowDataParsed.SB;
                                modData.ServerLoadedModData       = true;
                            }
                            else
                            {
                                MSLog.Log("Received invalid row, ignoring.");
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        MSLog.Log("Exception:");
                        MSLog.Log(e.Message);
                        CurrSyncState = CurrentSyncState.ModSyncError;
                        _errorCode    = "152";
                    }
                    MSLog.Log("Finished reading json");
                }
                catch (Exception e)
                {
                    // failed to parse JSON
                    MSLog.Log("Failed to parse JSON");
                    MSLog.Log(e.Message);
                    CurrSyncState = CurrentSyncState.ModSyncError;
                    _errorCode    = "153";
                    return;
                }
                //CurrSyncState = CurrentSyncState.RequestStarted;
                CurrSyncState = CurrentSyncState.Done;
            }
        }