Exemplo n.º 1
0
        public static void      RequestVoucherPackages(this PublisherDatabase database, IPublisherAPI api, Action <RequestResponse <VoucherPackage[]> > onCompleted = null)
        {
            api.GetVoucherPackages(api.Session.publisher, (r, result) =>
            {
                RequestResponse <VoucherPackage[]> requestResponse = new RequestResponse <VoucherPackage[]>()
                {
                    context = database,
                    ok      = false,
                    error   = null,
                    result  = null
                };

                if (DataStructureExtension.CheckRequest(r, result, requestResponse) == true)
                {
                    VoucherPackagesContainer container = JsonUtility.FromJson <VoucherPackagesContainer>(result.Replace("],[", ",").Replace("[[", "[").Replace("]]", "]"));

                    if (Conf.DebugMode == Conf.DebugState.Verbose)
                    {
                        InternalNGDebug.Snapshot(container);
                    }

                    VoucherPackage[]        packages = new VoucherPackage[container.aaData.Length >> 1];

                    for (int i = 0, max = container.aaData.Length; i < max; i += 2)
                    {
                        packages[i >> 1] = new VoucherPackage()
                        {
                            packageId   = int.Parse(container.aaData[i]),
                            packageName = container.aaData[i + 1],
                        };
                    }

                    requestResponse.ok       = true;
                    requestResponse.result   = packages;
                    database.VoucherPackages = requestResponse.result;
                }

                if (onCompleted != null)
                {
                    onCompleted(requestResponse);
                }
            });
        }
Exemplo n.º 2
0
        public static void      RequestDeleteUnityPackage(this Version version, IPublisherAPI api, VersionDetailed.Package.Version.UnityPackage unityPackage, Action <RequestResponse <bool> > onCompleted = null)
        {
            api.DeleteUnityPackage(version.id, unityPackage, (r, result) =>
            {
                RequestResponse <bool> requestResponse = new RequestResponse <bool>()
                {
                    context = version,
                    ok      = false,
                    error   = null,
                    result  = false
                };

                if (DataStructureExtension.CheckRequest(r, result, requestResponse) == true)
                {
                    Response response = DataStructureExtension.CheckResponse(result, requestResponse);

                    if (Conf.DebugMode == Conf.DebugState.Verbose)
                    {
                        InternalNGDebug.Snapshot(response);
                    }

                    if (response != null && response.Succeeded == true)
                    {
                        var unityPackages = new List <VersionDetailed.Package.Version.UnityPackage>(version.detailed.package.version.unitypackages);

                        unityPackages.Remove(unityPackage);

                        version.detailed.package.version.unitypackages = unityPackages.ToArray();

                        requestResponse.ok     = true;
                        requestResponse.result = true;
                    }
                }

                if (onCompleted != null)
                {
                    onCompleted(requestResponse);
                }
            });
        }
Exemplo n.º 3
0
        public static void      RequestVetVersion(this Version version, IPublisherAPI api, VersionDetailed.Package.Version.UnityPackage unityPackage, IEnumerable <string> platforms, IEnumerable <string> unityVersions, IEnumerable <string> srpTypes, IEnumerable <string> dependencies, Action <RequestResponse <bool> > onCompleted = null)
        {
            api.VetVersion(version.id,
                           unityPackage.package_upload_id,
                           platforms,
                           unityVersions,
                           srpTypes,
                           dependencies,
                           (r, result) =>
            {
                RequestResponse <bool> requestResponse = new RequestResponse <bool>()
                {
                    context = version,
                    ok      = false,
                    error   = null,
                    result  = false
                };

                if (DataStructureExtension.CheckRequest(r, result, requestResponse) == true)
                {
                    Response response = DataStructureExtension.CheckResponse(result, requestResponse);

                    if (Conf.DebugMode == Conf.DebugState.Verbose)
                    {
                        InternalNGDebug.Snapshot(response);
                    }

                    if (response != null && response.Succeeded == true)
                    {
                        requestResponse.ok     = true;
                        requestResponse.result = true;
                    }
                }

                if (onCompleted != null)
                {
                    onCompleted(requestResponse);
                }
            });
        }
Exemplo n.º 4
0
#pragma warning restore 649

        public static void      RequestAllPackages(this PublisherDatabase database, IPublisherAPI api, Action <RequestResponse <Package[]> > onCompleted = null)
        {
            api.GetAllPackages((r, result) =>
            {
                RequestResponse <Package[]> requestResponse = new RequestResponse <Package[]>()
                {
                    context = database,
                    ok      = false,
                    error   = null,
                    result  = null
                };

                if (DataStructureExtension.CheckRequest(r, result, requestResponse) == true)
                {
                    Response response = DataStructureExtension.CheckResponse(result, requestResponse);

                    if (Conf.DebugMode == Conf.DebugState.Verbose)
                    {
                        InternalNGDebug.Snapshot(response);
                    }

                    if (response == null || response.Succeeded == true)
                    {
                        Packages packagesContainer = JsonUtility.FromJson <Packages>(result);
                        List <Package> packages    = new List <Package>(packagesContainer.packages);

                        packages.Sort((a, b) => a.name.CompareTo(b.name));

                        requestResponse.ok     = true;
                        requestResponse.result = packages.ToArray();
                        database.Packages      = requestResponse.result;
                    }
                }

                if (onCompleted != null)
                {
                    onCompleted(requestResponse);
                }
            });
        }
Exemplo n.º 5
0
        public static void      RequestSetPackage(this Version version, IPublisherAPI api, string versionName, string publishnotes, int categoryID, float price, Action <RequestResponse <bool> > onCompleted = null)
        {
            api.SetPackage(version.id,
                           versionName,
                           publishnotes,
                           categoryID,
                           price,
                           (r, result) =>
            {
                RequestResponse <bool> requestResponse = new RequestResponse <bool>()
                {
                    context = version,
                    ok      = false,
                    error   = null,
                    result  = false
                };

                if (DataStructureExtension.CheckRequest(r, result, requestResponse) == true)
                {
                    Response response = DataStructureExtension.CheckResponse(result, requestResponse);

                    if (Conf.DebugMode == Conf.DebugState.Verbose)
                    {
                        InternalNGDebug.Snapshot(response);
                    }

                    if (response != null && response.Succeeded == true)
                    {
                        requestResponse.ok     = true;
                        requestResponse.result = true;
                    }
                }

                if (onCompleted != null)
                {
                    onCompleted(requestResponse);
                }
            });
        }
Exemplo n.º 6
0
        public static void      RequestSetPackageMetadata(this Version version, IPublisherAPI api, VersionDetailed.Package.Version.Language language, string name, string description, string keywords, Action <RequestResponse <bool> > onCompleted = null)
        {
            api.SetPackageMetadata(version.id, language.languageCode,
                                   name,
                                   description,
                                   keywords,
                                   (r, result) =>
            {
                RequestResponse <bool> requestResponse = new RequestResponse <bool>()
                {
                    context = version,
                    ok      = false,
                    error   = null,
                    result  = false
                };

                if (DataStructureExtension.CheckRequest(r, result, requestResponse) == true)
                {
                    Response response = DataStructureExtension.CheckResponse(result, requestResponse);

                    if (Conf.DebugMode == Conf.DebugState.Verbose)
                    {
                        InternalNGDebug.Snapshot(response);
                    }

                    if (response != null && response.Succeeded == true)
                    {
                        requestResponse.ok     = true;
                        requestResponse.result = true;
                    }
                }

                if (onCompleted != null)
                {
                    onCompleted(requestResponse);
                }
            });
        }
Exemplo n.º 7
0
        public static void      RequestRatings(this Package package, IAssetStoreAPI api, Action <RequestResponse <Rating[]> > onCompleted = null)
        {
            api.GetRatings(package.id, (r, result) =>
            {
                RequestResponse <Rating[]> requestResponse = new RequestResponse <Rating[]>()
                {
                    context = package,
                    ok      = false,
                    error   = null,
                    result  = null
                };

                if (DataStructureExtension.CheckRequest(r, result, requestResponse) == true)
                {
                    PackageRatings packageRatings = JsonUtility.FromJson <PackageRatings>(result.Substring("[{\"data\":".Length - 2, result.Length - "[{\"data\":".Length - 1));

                    if (Conf.DebugMode == Conf.DebugState.Verbose)
                    {
                        InternalNGDebug.Snapshot(packageRatings.rating);
                    }

                    requestResponse.ok     = true;
                    requestResponse.result = packageRatings.rating;
                    package.Ratings        = requestResponse.result;
                }
                else
                {
                    package.Ratings = new Rating[0];
                }

                if (onCompleted != null)
                {
                    onCompleted(requestResponse);
                }
            });
        }
Exemplo n.º 8
0
        public static void      RequestGetPackageVersion(this PublisherDatabase database, IPublisherAPI api, Version version, Action <RequestResponse <VersionDetailed> > onCompleted = null)
        {
            api.GetPackageVersion(version.id, (r, result) =>
            {
                RequestResponse <VersionDetailed> requestResponse = new RequestResponse <VersionDetailed>()
                {
                    context = version,
                    ok      = false,
                    error   = null,
                    result  = null
                };

                if (DataStructureExtension.CheckRequest(r, result, requestResponse) == true)
                {
                    try
                    {
                        StringBuilder buffer = Utility.GetBuffer(result);

                        // Convert object "languages" to a JSON array.
                        int start = result.IndexOf("\"languages\":{");
                        Debug.AssertFormat(start != -1, "Token \"languages\" not found when requesting version \"{0}\".", version);

                        start += "\"languages\":{".Length - 1;

                        // Rely on brackets instead of sibling, in case Unity server changes the order.
                        int end = DataStructureExtension.DigestBracketScope(buffer, start) - 1;
                        Debug.AssertFormat(end < buffer.Length, "Closing token of \"languages\" not found when requesting version \"{0}\".", version);

                        buffer[start] = '[';
                        buffer[end]   = ']';

                        int i = start;

                        while (i < buffer.Length && buffer[i] != ']')
                        {
                            ++i;

                            int startBracket = i;

                            i             = DataStructureExtension.DigestString(buffer, i);
                            buffer[i + 1] = ',';
                            buffer.Remove(i + 2, 1);

                            buffer.Insert(startBracket, "{\"languageCode\":");

                            i = DataStructureExtension.DigestBracketScope(buffer, startBracket);
                        }

                        result = Utility.ReturnBuffer(buffer);
                    }
                    catch (Exception ex)
                    {
                        Debug.LogException(ex);
                    }

                    VersionDetailed versionDetail = JsonUtility.FromJson <VersionDetailed>(result);
                    var languages = new List <VersionDetailed.Package.Version.Language>(versionDetail.package.version.languages);

                    languages.Sort((a, b) => a.languageCode.CompareTo(b.languageCode));

                    versionDetail.package.version.languages = languages.ToArray();
                    version.detailed = versionDetail;

                    int index = database.versions.FindIndex(v => v.package.version.id == versionDetail.package.version.id);
                    if (index != -1)
                    {
                        database.versions[index] = versionDetail;
                    }
                    else
                    {
                        database.versions.Add(versionDetail);
                    }

                    requestResponse.ok     = true;
                    requestResponse.result = versionDetail;
                }

                if (onCompleted != null)
                {
                    onCompleted(requestResponse);
                }
            });
        }
Exemplo n.º 9
0
        public static void      RequestCreateDraft(this Version version, IPublisherAPI api, Action <RequestResponse <int> > onCompleted = null)
        {
            api.CreateDraft(version.id, (r, result) =>
            {
                RequestResponse <int> requestResponse = new RequestResponse <int>()
                {
                    context = version,
                    ok      = false,
                    error   = null,
                    result  = 0
                };

                if (DataStructureExtension.CheckRequest(r, result, requestResponse) == true)
                {
                    Response response = DataStructureExtension.CheckResponse(result, requestResponse);

                    if (Conf.DebugMode == Conf.DebugState.Verbose)
                    {
                        InternalNGDebug.Snapshot(response);
                    }

                    if (response != null)
                    {
                        if (response.Succeeded == true)
                        {
                            Package package = version.package;
                            Version model   = null;

                            // Pick a model, published if available. Even without, Unity might do it automatically on its side. Not sure though.
                            for (int i = 0, max = package.versions.Length; i < max; ++i)
                            {
                                Version v = package.versions[i];

                                if (model == null || v.status == Status.Published)
                                {
                                    model = v;
                                }
                            }

                            string modelJson = JsonUtility.ToJson(model);

                            model         = new Version();
                            model.package = package;

                            JsonUtility.FromJsonOverwrite(modelJson, model);

                            model.id     = response.id;
                            model.status = Status.Draft;

                            DateTime now = DateTime.Now;

                            model.created   = now.Year.ToString("0000") + "-" + now.Month.ToString("00") + "-" + now.Day.ToString("00") + " " + now.Hour.ToString("00") + ":" + now.Minute.ToString("00") + ":" + now.Second.ToString("00");
                            model.modified  = model.created;
                            model.published = string.Empty;

                            Array.Resize(ref package.versions, package.versions.Length + 1);
                            package.versions[package.versions.Length - 1] = model;

                            requestResponse.ok     = true;
                            requestResponse.result = response.id;
                        }
                    }
                }

                if (onCompleted != null)
                {
                    onCompleted(requestResponse);
                }
            });
        }
Exemplo n.º 10
0
        ///// <summary>Checks if the response's content is usable. Returns false if something went wrong.</summary>
        /// <param name="request"></param>
        /// <param name="result"></param>
        /// <returns></returns>
        private static bool     CheckRequest <T>(HttpWebResponse request, string result, RequestResponse <T> requestResponse)
        {
            if (Conf.DebugMode == Conf.DebugState.Verbose)
            {
                InternalNGDebug.Snapshot(request);
            }

            if (request == null || request.StatusCode != HttpStatusCode.OK)
            {
                requestResponse.error = result;
                return(false);
            }

            if (Conf.DebugMode == Conf.DebugState.Verbose)
            {
                //if (result[0] == '{' || result[0] == '[')
                //	NGDebug.LogJSON(result);
                //else
                Debug.Log(result);
            }

            return(true);
        }