Ejemplo n.º 1
0
        /// <summary>Generates a debug-friendly string of a web request.</summary>
        public static string GetRequestInfo(UnityWebRequest webRequest, string userIdString)
        {
            if (webRequest == null)
            {
                return("NULL_WEB_REQUEST");
            }

            // check user string
            if (userIdString == null)
            {
                userIdString = "[NOT RECORDED]";
            }

            // build string
            var requestString = new System.Text.StringBuilder();

            requestString.Append("URL: ");
            requestString.Append(webRequest.url);
            requestString.Append(" (");
            requestString.Append(webRequest.method.ToUpper());
            requestString.AppendLine(")");

            requestString.Append("User: "******"Headers:");
            foreach (string headerKey in APIClient.MODIO_REQUEST_HEADER_KEYS)
            {
                string headerValue = webRequest.GetRequestHeader(headerKey);
                if (headerValue != null)
                {
                    requestString.Append("  ");
                    requestString.Append(headerKey);
                    requestString.Append('=');

                    if (headerKey.ToUpper() == "AUTHORIZATION")
                    {
                        if (headerValue != null &&
                            headerValue.StartsWith("Bearer ") &&
                            headerValue.Length > 8)
                        {
                            requestString.Append("Bearer [OAUTH_TOKEN]");
                        }
                        else // NULL
                        {
                            requestString.Append(headerValue);
                        }
                    }
                    else
                    {
                        requestString.Append(headerValue);
                    }

                    requestString.AppendLine();
                }
            }

            // add uploaded data
            var uploadHandler = webRequest.uploadHandler;

            if (uploadHandler != null)
            {
                List <API.StringValueParameter> stringFields = null;
                List <API.BinaryDataParameter>  binaryFields = null;

                string contentType = webRequest.GetRequestHeader("content-type");
                if (contentType.ToLower() == "application/x-www-form-urlencoded")
                {
                    DebugUtilities.ParseURLEncodedFormData(uploadHandler.data,
                                                           out stringFields);
                }
                else if (contentType.Contains("multipart/form-data"))
                {
                    DebugUtilities.ParseMultipartFormData(uploadHandler.data,
                                                          out stringFields,
                                                          out binaryFields);
                }
                else
                {
                    Debug.Log("[mod.io] Unable to parse upload data for content-type \'"
                              + contentType + "\'");
                }

                // add string fields
                if (stringFields != null)
                {
                    requestString.AppendLine("String Fields:");

                    int countInsertIndex = requestString.Length - 1;
                    int count            = 0;

                    foreach (var svp in stringFields)
                    {
                        requestString.Append("  ");
                        requestString.Append(svp.key);
                        requestString.Append('=');
                        requestString.Append(svp.value);
                        requestString.AppendLine();
                        ++count;
                    }

                    requestString.Insert(countInsertIndex, " [" + count.ToString() + "]");
                }

                // add binary fields
                if (binaryFields != null)
                {
                    requestString.AppendLine("Binary Fields:");

                    int countInsertIndex = requestString.Length - 1;
                    int count            = 0;

                    foreach (var bdp in binaryFields)
                    {
                        requestString.Append("  ");
                        requestString.Append(bdp.key);
                        requestString.Append('=');
                        requestString.Append(bdp.fileName);
                        requestString.Append(" (");
                        requestString.Append(bdp.contents == null
                                             ? "NULL_DATA"
                                             : ValueFormatting.ByteCount(bdp.contents.Length, null));
                        requestString.Append(")");
                        requestString.AppendLine();
                        ++count;
                    }

                    requestString.Insert(countInsertIndex, " [" + count.ToString() + "]");
                }
            }

            return(requestString.ToString());
        }
Ejemplo n.º 2
0
        // ---------[ 2019 ]---------
        /// <summary>Moves the data from the UserAuthenticationData and ModManager caches to UserAccountManagement.</summary>
        private static void Update_2_0_to_2_1_UserData()
        {
            Debug.Log("[mod.io] Attempting 2.0->2.1 UserData update.");

            // check if the file already exists
            byte[] fileData = null;

            UserDataStorage.InitializeForUser(null, () => {});
            UserDataStorage.ReadFile(LocalUser.FILENAME, (success, data) => fileData = data);

            if (fileData != null && fileData.Length > 0)
            {
                Debug.Log("[mod.io] Aborting UserData update. FileExists: \'"
                          + LocalUser.FILENAME + "\' ["
                          + ValueFormatting.ByteCount(fileData.Length, null) + "]");
            }

            // update
            GenericJSONObject dataWrapper;
            LocalUser         userData = new LocalUser();
            string            filePath = null;

            // - copy enabled/subbed -
            filePath = ModManager.PERSISTENTDATA_FILEPATH;

            if (IOUtilities.TryReadJsonObjectFile(filePath, out dataWrapper))
            {
                int[] modIds = null;

                if (DataUpdater.TryGetArrayField(dataWrapper,
                                                 "subscribedModIds",
                                                 out modIds))
                {
                    userData.subscribedModIds = new List <int>(modIds);
                }

                if (DataUpdater.TryGetArrayField(dataWrapper,
                                                 "enabledModIds",
                                                 out modIds))
                {
                    userData.enabledModIds = new List <int>(modIds);
                }
            }

            // - copy queued subs/unsubs -
            filePath = IOUtilities.CombinePath(CacheClient.cacheDirectory,
                                               ModIO.UI.ModBrowser.MANIFEST_FILENAME);

            if (IOUtilities.TryReadJsonObjectFile(filePath, out dataWrapper))
            {
                List <int> modIds = null;

                if (DataUpdater.TryGetArrayField(dataWrapper,
                                                 "queuedSubscribes",
                                                 out modIds))
                {
                    userData.queuedSubscribes = new List <int>(modIds);
                }

                if (DataUpdater.TryGetArrayField(dataWrapper,
                                                 "queuedUnsubscribes",
                                                 out modIds))
                {
                    userData.queuedUnsubscribes = new List <int>(modIds);
                }
            }

            // - copy UAD -
            filePath = UserAuthenticationData.FILE_LOCATION;

            if (IOUtilities.TryReadJsonObjectFile(filePath, out dataWrapper))
            {
                // user profile
                int userId = UserProfile.NULL_ID;
                if (dataWrapper.data.ContainsKey("userId"))
                {
                    userId = (int)dataWrapper.data["userId"];
                }

                userData.profile = null;
                if (userId != UserProfile.NULL_ID)
                {
                    userData.profile = CacheClient.LoadUserProfile(userId);
                }

                // token data
                if (dataWrapper.data.ContainsKey("token"))
                {
                    userData.oAuthToken = (string)dataWrapper.data["token"];
                }
                if (dataWrapper.data.ContainsKey("wasTokenRejected"))
                {
                    userData.wasTokenRejected = (bool)dataWrapper.data["wasTokenRejected"];
                }

                // NOTE(@jackson): External Authentication is no longer saved to disk and is thus ignored.

                IOUtilities.DeleteFile(filePath);
            }

            // - set and save -
            LocalUser.instance = userData;
            LocalUser.isLoaded = true;
            LocalUser.Save();

            Debug.Log("[mod.io] UserData updated completed.");
        }
Ejemplo n.º 3
0
        // ---------[ FUNCTIONALITY ]---------
        /// <summary>Formats a value as a display string.</summary>
        public static string FormatValue(object value, Method method, string toStringParameter)
        {
            string displayString = string.Empty;

            if (string.IsNullOrEmpty(toStringParameter))
            {
                // Default value for ToString() in most cases is (G)eneral
                toStringParameter = "G";
            }

            switch (method)
            {
            case Method.ByteCount:
            {
                Int64 bytes = 0;

                if (value != null)
                {
                    bytes = (Int64)value;
                }

                displayString = ValueFormatting.ByteCount(bytes, toStringParameter);
            }
            break;

            case Method.AbbreviatedNumber:
            {
                int number = 0;

                if (value != null)
                {
                    number = (int)value;
                }

                displayString = ValueFormatting.AbbreviateInteger(number, toStringParameter);
            }
            break;

            case Method.TimeStampAsDateTime:
            {
                if (value == null)
                {
                    displayString = "--";
                }
                else
                {
                    displayString = ServerTimeStamp.ToLocalDateTime((int)value).ToString(toStringParameter);
                }
            }
            break;

            case Method.Percentage:
            {
                if (value == null)
                {
                    displayString = "--%";
                }
                else
                {
                    displayString = ((float)value * 100.0f).ToString(toStringParameter) + "%";
                }
            }
            break;

            case Method.SecondsAsTime:
            {
                int seconds = 0;

                if (value != null)
                {
                    seconds = (int)value;
                }

                displayString = ValueFormatting.SecondsAsTime(seconds);
            }
            break;

            default:
            {
                displayString = null;

                if (value != null &&
                    !string.IsNullOrEmpty(toStringParameter))
                {
                    if (value is float)
                    {
                        displayString = ((float)value).ToString(toStringParameter);
                    }
                    else if (value is int)
                    {
                        displayString = ((int)value).ToString(toStringParameter);
                    }
                    else if (value is Int64)
                    {
                        displayString = ((Int64)value).ToString(toStringParameter);
                    }
                }

                if (displayString == null)
                {
                    if (value != null)
                    {
                        displayString = value.ToString();
                    }
                    else
                    {
                        displayString = string.Empty;
                    }
                }
            }
            break;
            }
            return(displayString);
        }