private static bool ValidateJsonFileExist(ServerProperties properties)
        {
            CredentialList credentialList = FindCredentialsAsset();

            Credentials credentials =
                properties.IsProduction ? credentialList.prodCredentials : credentialList.devCredentials;

            string credentialsJson = JsonConvert.SerializeObject(credentials, Formatting.Indented,
                                                                 new JsonSerializerSettings {
                ContractResolver = new ShouldSerializeContractResolver()
            });

            var filePathTmp = Path.Combine(Application.temporaryCachePath, GetJsonFilename(properties));

            try
            {
                File.WriteAllText(filePathTmp, credentialsJson.Replace("\\\\", "\\"));
            }
            catch (Exception e)
            {
                Debug.LogError(e.Message);
                return(false);
            }

            return(true);
        }
        private static async Task <bool> ValidateConnectionToFirestore(ServerProperties properties)
        {
            string         collectionName = "Test_firestore_collection";
            string         docName        = "Test_firestore_document";
            CredentialList credentialList = FindCredentialsAsset();

            FirestoreDb db = FirestoreDb.Create(GetProjectId(properties, credentialList));

            // Create a document with a random ID in the "Test_firestore_collection" collection.
            CollectionReference collection = db.Collection(collectionName);

            DocumentReference document =
                await collection.AddAsync(new { Name = docName });

            // A DocumentReference doesn't contain the data - it's just a path.
            // Let's fetch the current document.
            DocumentSnapshot snapshot = await document.GetSnapshotAsync();

            string receivedDocName = snapshot.GetValue <string>("Name");

            if (receivedDocName != docName)
            {
                await document.DeleteAsync();

                Debug.LogError($"Could not write a test document to firebase");
                return(false);
            }

            await document.DeleteAsync();

            return(true);
        }
        public static bool CreateServerPropertiesFile(ServerProperties properties)
        {
            CredentialList credentialList = FindCredentialsAsset();

            Credentials credentials =
                properties.IsProduction ? credentialList.prodCredentials : credentialList.devCredentials;

            string credentialsJson = JsonConvert.SerializeObject(credentials, Formatting.Indented,
                                                                 new JsonSerializerSettings {
                ContractResolver = new ShouldSerializeContractResolver()
            });

            string serverSettings = $"host.local={properties.IsLocal}{System.Environment.NewLine}" +
                                    $"host.production={properties.IsProduction}{System.Environment.NewLine}" +
                                    $"host.prod.credentials={JSON_FILENAME_PROD}{System.Environment.NewLine}" +
                                    $"host.prod.projectId={GetProjectId(true, credentialList)}{System.Environment.NewLine}" +
                                    $"host.dev.credentials={JSON_FILENAME_DEV}{System.Environment.NewLine}" +
                                    $"host.dev.projectId={GetProjectId(false, credentialList)}";

            var filePathServerCredentials = Path.Combine(Application.dataPath, "Server~/src/main/resources", GetJsonFilename(properties));
            var filePathServerSettings    = Path.Combine(Application.dataPath, "Server~/src/main/resources", SERVER_SETTINGS_FILENAME);

            try
            {
                File.WriteAllText(filePathServerCredentials, credentialsJson.Replace("\\\\", "\\"));
                File.WriteAllText(filePathServerSettings, serverSettings);
            }
            catch (Exception e)
            {
                Debug.LogError(e.Message);
                return(false);
            }

            return(true);
        }
        public static async Task <bool> Validate(ServerProperties properties)
        {
            if (!ValidateCredentialsAssetExist())
            {
                return(false);
            }

            if (!ValidateCredentialsNotEmpty())
            {
                return(false);
            }

            if (!ValidateJsonFileExist(properties))
            {
                return(false);
            }

            if (!ValidateEnvironmentVariableIsSet(properties))
            {
                return(false);
            }

            bool conn = await ValidateConnectionToFirestore(properties);

            if (!conn)
            {
                return(false);
            }

            return(true);
        }
        private static bool ValidateEnvironmentVariableIsSet(ServerProperties properties)
        {
            Environment.SetEnvironmentVariable(GOOGLE_CREDENTIAL_VAR_NAME, Path.Combine(Application.temporaryCachePath, GetJsonFilename(properties)));

            if (String.IsNullOrEmpty(Environment.GetEnvironmentVariable(GOOGLE_CREDENTIAL_VAR_NAME)))
            {
                Debug.LogError($"Env. var {GOOGLE_CREDENTIAL_VAR_NAME} could not be found");
                return(false);
            }
            return(true);
        }
 public static string GetProjectId(ServerProperties properties, CredentialList credentialList)
 {
     return(GetProjectId(properties.IsProduction, credentialList));
 }
 private static string GetJsonFilename(ServerProperties properties)
 {
     return(properties.IsProduction ? JSON_FILENAME_PROD : JSON_FILENAME_DEV);
 }