public static void Init(string TemplateId, Action <bool, string> OnEditorResult)
        {
            CurrentTemplateId = TemplateId;

            var config  = PaymentsManager.CurrentConfig;
            var address = Resources.Load <PaymentsAppSettings>("PaymentsAppSettings").ServiceURL;

            /// Download target template.
            HTTPEditor.SendGet(address + "/GetApp/" + config.AppId + "/" + TemplateId, (isSuccess, responseBody) => {
                if (!isSuccess)
                {
                    OnEditorResult?.Invoke(false, "HTTP Error");
                    return;
                }
                else
                {
                    CurrentTemplate = JsonUtility.FromJson <PaymentTemplate>(responseBody);

                    if (CurrentTemplate == null)
                    {
                        OnEditorResult?.Invoke(true, "Template data is null.");
                        return;
                    }

                    OnEditorResult?.Invoke(true, "Template received.");

                    // Create window.
                    var window = (TemplateEditor)GetWindow(typeof(TemplateEditor));
                    window.Show();
                }
            });
        }
Пример #2
0
        public static void Push(Action <bool> OnPushed = null)
        {
            var address = Resources.Load <PaymentsAppSettings>("PaymentsAppSettings").ServiceURL;

            HTTPEditor.SendPost(address + "/SetApp/" + CurrentConfig.AppId, JsonUtility.ToJson(CurrentConfig),
                                (isSuccess, responseBody) => {
                if (!isSuccess)
                {
                    lastMessage = "HTTP Error.";
                    OnPushed?.Invoke(false);
                    return;
                }

                var response = JsonUtility.FromJson <GeneralResponse>(responseBody);
                if (response == null || response.error > 0)
                {
                    appIdDoesntExist = true;
                    lastMessage      = response != null ? response.message : "HTTP Error";
                    OnPushed?.Invoke(false);
                }
                else
                {
                    appIdDoesntExist = false;
                    lastMessage      = response.message;
                    OnPushed?.Invoke(true);
                }
            });
        }
        private void UpdateTemplate()
        {
            Debug.Log("Updating template " + CurrentTemplateId);

            var config  = PaymentsManager.CurrentConfig;
            var address = Resources.Load <PaymentsAppSettings>("PaymentsAppSettings").ServiceURL;

            var templateData = JsonUtility.ToJson(CurrentTemplate, true);

            HTTPEditor.SendPost(address + "/SetApp/" + config.AppId + "/" + CurrentTemplateId, templateData, (isSuccess, responseData) => {
                if (!isSuccess)
                {
                    Debug.LogError("Failed to update template.");
                }
                else
                {
                    Debug.Log(responseData);
                }
            });
        }
Пример #4
0
        private void OnGUI()
        {
            GUILayout.Label("Templates");

            GUILayout.Label(lastMessage);

            if (newTemplateCreator)
            {
                if (duplicationList.Length > 0)
                {
                    GUILayout.Label("You can select a template from the currents to duplicate.");
                    templateIndex = EditorGUILayout.Popup(templateIndex, duplicationList);
                }

                newTemplateName = GUILayout.TextField(newTemplateName);
                if (GUILayout.Button("Create template"))
                {
                    newTemplateCreator = false;
                    // Send this new template to the service.

                    var address = Resources.Load <PaymentsAppSettings>("PaymentsAppSettings").ServiceURL;

                    if (templateIndex >= 1)
                    { // duplication is required.
                        if (templateIndex < duplicationList.Length)
                        {
                            // First download the target template.

                            HTTPEditor.SendGet(address + "/GetApp/" + paymentsConfig.AppId + "/" + duplicationList[templateIndex],
                                               (isError, responseBody) =>
                            {
                                Debug.Log(responseBody);

                                // Save the new template.
                                HTTPEditor.SendPost(address + "/SetApp/" + paymentsConfig.AppId + "/" + newTemplateName, responseBody,
                                                    (_isSuccess, _responseBody) =>
                                {
                                    if (!!!_isSuccess)
                                    {
                                        lastMessage = "HTTP Error";
                                    }
                                    else
                                    {
                                        RefreshWithResponse(_responseBody);
                                    }
                                });
                            });
                        }
                    }
                    else
                    {
                        var body = "{ }";
                        // Create empty template.
                        // Save the new template.
                        HTTPEditor.SendPost(address + "/SetApp/" + paymentsConfig.AppId + "/" + newTemplateName, body,
                                            (_isSuccess, _responseBody) =>
                        {
                            Debug.Log(_responseBody);

                            if (!!!_isSuccess)
                            {
                                lastMessage = "HTTP Error";
                            }
                            else
                            {
                                RefreshWithResponse(_responseBody);
                            }
                        });
                    }
                }
                return;
            }

            if (paymentsConfig.IsUpdateAvailable)
            {
                // Show update button.
                if (GUILayout.Button("Save config."))
                {
                    paymentsConfig.IsUpdateAvailable = false;

                    File.WriteAllText(PaymentsManager.ConfigPath, JsonUtility.ToJson(paymentsConfig, true));

                    // Load asset database again.
                    AssetDatabase.Refresh();
                }
            }

            // Template selection enabled.
            if (paymentsConfig.Templates != null && paymentsConfig.Templates.Length > 0)
            {
                templateIndex = EditorGUILayout.Popup(templateIndex, paymentsConfig.Templates);
            }
            else
            {
                GUILayout.Label("No templates on this app yet.");
            }

            if (templateIndex >= 0)
            {
                // template options.
                if (GUILayout.Button("Edit Template"))
                {
                    TemplateEditor.Init(paymentsConfig.Templates[templateIndex], (isSuccess, Message) => {
                        lastMessage = "Edit Template Result  => " + isSuccess + " Message: " + Message;
                        Debug.Log(lastMessage);
                    });
                }

                if (GUILayout.Button("Set as default"))
                {
                    if (EditorUtility.DisplayDialog("Beware!", "Are you sure you want to assign this template as defauşt?", "Yes", "Cancel"))
                    {
                        paymentsConfig.Template = paymentsConfig.Templates[templateIndex];
                        // Update on server.
                        PaymentsManager.Push((isPushed) => {
                            lastMessage = "Pushed => " + isPushed;
                            Debug.Log(lastMessage);
                        });
                    }
                }

                if (GUILayout.Button("DeleteTemplate"))
                {
                    if (EditorUtility.DisplayDialog("Beware!", "You are about to remove this template. Cannot be undo.", "Go ahead", "Cancel"))
                    {
                        var address = Resources.Load <PaymentsAppSettings>("PaymentsAppSettings").ServiceURL;

                        HTTPEditor.SendGet(address + "/RemoveTemplate/" + paymentsConfig.AppId + "/" + paymentsConfig.Templates[templateIndex],
                                           (isSuccess, _responseBody) => {
                            lastMessage = _responseBody;
                            Debug.Log(lastMessage);
                            if (isSuccess)
                            {
                                RefreshWithResponse(_responseBody);
                            }
                        }
                                           );
                    }
                }
            }

            GUILayout.Space(20);
            if (GUILayout.Button("Create new template"))
            {
                newTemplateName = "NewTemplate";
                if (paymentsConfig.Templates != null && paymentsConfig.Templates.ToList().Find(x => x.Equals(newTemplateName)) != null)
                {
                    int i = 1;
                    while (i < 99)
                    {
                        var targetName = newTemplateName + "(" + i + ")";
                        if (paymentsConfig.Templates.ToList().Find(x => x.Equals(targetName)) == null)
                        {
                            newTemplateName = targetName;
                            break;
                        }

                        i++;
                    }
                }

                if (paymentsConfig.Templates == null || paymentsConfig.Templates.Length == 0)
                {
                    duplicationList = new string[0];
                }
                else
                {
                    var tList = paymentsConfig.Templates.ToList();
                    tList.Insert(0, "Select a template.");
                    duplicationList = tList.ToArray();
                }


                newTemplateCreator = true;
            }
        }
Пример #5
0
        void OnGUI()
        {
            if (CurrentConfig == null)
            {
                Init(); // Code refresh probably. Redo.
                return;
            }

            GUILayout.Label("Base Settings", EditorStyles.boldLabel);
            CurrentConfig._AppId = EditorGUILayout.TextField("AppId", CurrentConfig._AppId);

            if (!dataPulled)
            {
                // pull the latest.
                if (GUILayout.Button("Pull the latest config"))
                {
                    Pull();
                }
            }
            else
            {
                // pull the latest.
                if (GUILayout.Button("Push latest config."))
                {
                    // HTTPRequester Download.
                }
            }

            GUILayout.Label(lastMessage);

            if (appIdDoesntExist)
            {
                if (GUILayout.Button("Create new app with this ID"))
                {
                    var address = Resources.Load <PaymentsAppSettings>("PaymentsAppSettings").ServiceURL;

                    var appId = CurrentConfig.AppId;
                    CurrentConfig       = new PaymentsConfig();
                    CurrentConfig.AppId = appId;

                    HTTPEditor.SendPost(address + "/SetApp/" + CurrentConfig.AppId, JsonUtility.ToJson(CurrentConfig),
                                        (isSuccess, responseBody) => {
                        if (!isSuccess)
                        {
                            lastMessage = "HTTP Error.";
                            return;
                        }

                        var response = JsonUtility.FromJson <GeneralResponse>(responseBody);
                        if (response == null || response.error > 0)
                        {
                            appIdDoesntExist = true;

                            lastMessage = response != null ? response.message : "HTTP Error";
                        }
                        else
                        {
                            appIdDoesntExist = false;
                            lastMessage      = response.message;
                        }
                    });
                }
            }

            if (templatesEnabled)
            {
                if (GUILayout.Button("Open templates window"))
                {
                    TemplateManager.Init(CurrentConfig);
                }
            }
        }
Пример #6
0
        public static void Pull(Action <bool> OnPulled = null)
        {
            lastMessage = "Pulling appId => " + CurrentConfig.AppId;

            // HTTPRequester Download.
            var address = Resources.Load <PaymentsAppSettings>("PaymentsAppSettings").ServiceURL;

            HTTPEditor.SendGet(address + "/GetApp/" + CurrentConfig.AppId, (isSuccess, responseBody) => {
                if (!isSuccess)
                {
                    Debug.Log("[Payments] HTTP Error.");
                    return;
                }

                Debug.Log(responseBody);
                var response = JsonUtility.FromJson <GeneralResponse>(responseBody);

                if (response == null)
                {
                    lastMessage = "[Payments] Data is broken.";
                    OnPulled?.Invoke(false);
                }
                else
                {
                    lastMessage = "Pulled appId => " + CurrentConfig.AppId;

                    switch (response.error)
                    {
                    case 0:     // App is exists with templates.
                        templatesEnabled = true;
                        appIdDoesntExist = false;

                        CurrentConfig = JsonUtility.FromJson <PaymentsConfig>(responseBody);

                        // [KNOWN ISSUE]
                        // Service returns the templates list with the extension. Because template files could be a different file type.
                        // Server never checks the templates if they are proper json or not. They could be images also.
                        // But in this JSON case, im putting an easy fix here by removing the extension.
                        if (CurrentConfig.Templates != null)
                        {
                            int length = CurrentConfig.Templates.Length;
                            for (int i = 0; i < length; i++)
                            {
                                int textLength             = CurrentConfig.Templates[i].Length;
                                CurrentConfig.Templates[i] = Path.GetFileNameWithoutExtension(CurrentConfig.Templates[i]);
                            }
                        }

                        File.WriteAllText(ConfigPath, JsonUtility.ToJson(CurrentConfig));     // Update payments config on resources.
                        AssetDatabase.Refresh();

                        OnPulled?.Invoke(true);

                        if (!string.IsNullOrEmpty(CurrentConfig.Template) && CurrentConfig.Templates.ToList().Find(x => x.Equals(CurrentConfig.Template)) != null)
                        {
                            // Pull default template.
                            /// Download target template.
                            HTTPEditor.SendGet(address + "/GetApp/" + CurrentConfig.AppId + "/" + CurrentConfig.Template, (_isSuccess, _responseBody) => {
                                if (!_isSuccess)
                                {
                                    return;
                                }
                                else
                                {
                                    // Default template Pulled.
                                    Debug.Log("[Payments] Default template pulled.");
                                    File.WriteAllText(Application.dataPath + "/Resources/defaultPaymentsTemplate.json", _responseBody);
                                    AssetDatabase.Refresh();
                                }
                            });
                        }

                        break;

                    default:
                        appIdDoesntExist = true; templatesEnabled = false;
                        lastMessage      = "AppId doesnt exist on the server.";
                        OnPulled?.Invoke(false);
                        break;
                    }
                }
            });
        }