Example #1
0
        /// <summary>
        /// Part the of the uploading that can happen in the background
        /// on another thread
        /// </summary>
        private void BackgroundJob(
            List <BackendFile> files, string backendHash, bool verbose
            )
        {
            /*
             * WARNING: You run in another thread, be aware of what you touch!
             */

            // check server reachability
            if (!Http.UrlReachable(apiUrl.Index()))
            {
                Debug.LogError(
                    $"Unisave server at '{apiUrl.Index()}' is not reachable.\n"
                    + "If you want to work offline, you can go to "
                    + "Window/Unisave/Preferences and disable automatic "
                    + "backend uploading."
                    );
                return;
            }

            // send all file paths, hashes and global hash to the server
            // and initiate the upload
            JsonObject startResponse = Http.Post(
                apiUrl.BackendUpload_Start(),
                new JsonObject()
                .Add("game_token", preferences.GameToken)
                .Add("editor_key", preferences.EditorKey)

                .Add("backend_hash", backendHash)
                .Add("framework_version", FrameworkMeta.Version)
                .Add(
                    "backend_folder_path",
                    "Assets/" + preferences.BackendFolder
                    )
                .Add("files", new JsonArray(
                         files.Select(f => (JsonValue) new JsonObject()
                                      .Add("path", f.Path)
                                      .Add("hash", f.Hash)
                                      ).ToArray()
                         ))
                );

            // finish upload if requested
            if (startResponse["upload_has_finished"].AsBoolean)
            {
                if (verbose)
                {
                    Debug.Log(
                        "[Unisave] Backend upload done, this backend " +
                        "has already been uploaded."
                        );
                }

                return;
            }

            var filePathsToUpload = new HashSet <string>(
                startResponse["files_to_upload"]
                .AsJsonArray
                .Select(x => x.AsString)
                );

            // filter out files that needn't be uploaded
            IEnumerable <BackendFile> filteredFiles = files.Where(
                f => filePathsToUpload.Contains(f.Path)
                );

            // send individual files the server has asked for
            foreach (var file in filteredFiles)
            {
                Http.Post(
                    apiUrl.BackendUpload_File(),
                    new JsonObject()
                    .Add("game_token", preferences.GameToken)
                    .Add("editor_key", preferences.EditorKey)

                    .Add("backend_hash", backendHash)
                    .Add("file", new JsonObject()
                         .Add("path", file.Path)
                         .Add("hash", file.Hash)
                         .Add("file_type", file.FileType)
                         .Add(
                             "content",
                             Convert.ToBase64String(
                                 file.ContentForUpload())
                             )
                         )
                    );

                if (verbose)
                {
                    Debug.Log($"Uploaded '{file.Path}'");
                }
            }

            // finish the upload
            JsonObject finishResponse = Http.Post(
                apiUrl.BackendUpload_Finish(),
                new JsonObject()
                .Add("game_token", preferences.GameToken)
                .Add("editor_key", preferences.EditorKey)

                .Add("backend_hash", backendHash)
                );

            if (verbose)
            {
                Debug.Log(
                    "[Unisave] Backend upload done, starting server compilation..."
                    );
            }

            // print result of the compilation
            if (!finishResponse["compiler_success"].AsBoolean)
            {
                Debug.LogError(
                    "[Unisave] Server compile error:\n" +
                    finishResponse["compiler_output"].AsString
                    );
            }
            else
            {
                if (verbose)
                {
                    Debug.Log("[Unisave] Server compilation done.");
                }
            }
        }