示例#1
0
        public void Import_Google_FromCache()
        {
            if (GoogleUpdateFrequency == eGoogleUpdateFrequency.Never)
            {
                return;
            }

            if (!I2Utils.IsPlaying())
            {
                return;
            }

            string PlayerPrefName = GetSourcePlayerPrefName();
            string I2SavedData    = PersistentStorage.LoadFile(PersistentStorage.eFileType.Persistent, "I2Source_" + PlayerPrefName + ".loc", false);

            if (string.IsNullOrEmpty(I2SavedData))
            {
                return;
            }

            if (I2SavedData.StartsWith("[i2e]", StringComparison.Ordinal))
            {
                I2SavedData = StringObfucator.Decode(I2SavedData.Substring(5, I2SavedData.Length - 5));
            }

            //--[ Compare with current version ]-----
            bool   shouldUpdate            = false;
            string savedSpreadsheetVersion = Google_LastUpdatedVersion;

            if (PersistentStorage.HasSetting("I2SourceVersion_" + PlayerPrefName))
            {
                savedSpreadsheetVersion = PersistentStorage.GetSetting_String("I2SourceVersion_" + PlayerPrefName, Google_LastUpdatedVersion);
//				Debug.Log (Google_LastUpdatedVersion + " - " + savedSpreadsheetVersion);
                shouldUpdate = IsNewerVersion(Google_LastUpdatedVersion, savedSpreadsheetVersion);
            }

            if (!shouldUpdate)
            {
                PersistentStorage.DeleteFile(PersistentStorage.eFileType.Persistent, "I2Source_" + PlayerPrefName + ".loc", false);
                PersistentStorage.DeleteSetting("I2SourceVersion_" + PlayerPrefName);
                return;
            }

            if (savedSpreadsheetVersion.Length > 19)             // Check for corruption from previous versions
            {
                savedSpreadsheetVersion = string.Empty;
            }
            Google_LastUpdatedVersion = savedSpreadsheetVersion;

            //Debug.Log ("[I2Loc] Using Saved (PlayerPref) data in 'I2Source_"+PlayerPrefName+"'" );
            Import_Google_Result(I2SavedData, eSpreadsheetUpdateMode.Replace);
        }
示例#2
0
        public string Import_Google_Result(string JsonString, eSpreadsheetUpdateMode UpdateMode, bool saveInPlayerPrefs = false)
        {
            try
            {
                string ErrorMsg = string.Empty;
                if (string.IsNullOrEmpty(JsonString) || JsonString == "\"\"")
                {
                    return(ErrorMsg);
                }

                int idxV  = JsonString.IndexOf("version=", StringComparison.Ordinal);
                int idxSV = JsonString.IndexOf("script_version=", StringComparison.Ordinal);
                if (idxV < 0 || idxSV < 0)
                {
                    return("Invalid Response from Google, Most likely the WebService needs to be updated");
                }

                idxV  += "version=".Length;
                idxSV += "script_version=".Length;

                string newSpreadsheetVersion = JsonString.Substring(idxV, JsonString.IndexOf(",", idxV, StringComparison.Ordinal) - idxV);
                var    scriptVersion         = int.Parse(JsonString.Substring(idxSV, JsonString.IndexOf(",", idxSV, StringComparison.Ordinal) - idxSV));

                if (newSpreadsheetVersion.Length > 19) // Check for corruption
                {
                    newSpreadsheetVersion = string.Empty;
                }

                if (scriptVersion != LocalizationManager.GetRequiredWebServiceVersion())
                {
                    return("The current Google WebService is not supported.\nPlease, delete the WebService from the Google Drive and Install the latest version.");
                }

                //Debug.Log (Google_LastUpdatedVersion + " - " + newSpreadsheetVersion);
                if (saveInPlayerPrefs && !IsNewerVersion(Google_LastUpdatedVersion, newSpreadsheetVersion))
#if UNITY_EDITOR
                { return(""); }
#else
                { return("LanguageSource is up-to-date"); }
#endif

                if (saveInPlayerPrefs)
                {
                    string PlayerPrefName = GetSourcePlayerPrefName();
                    PersistentStorage.SaveFile(PersistentStorage.eFileType.Persistent, "I2Source_" + PlayerPrefName, "[i2e]" + StringObfucator.Encode(JsonString) + ".loc");
                    PersistentStorage.SetSetting_String("I2SourceVersion_" + PlayerPrefName, newSpreadsheetVersion);
                    PersistentStorage.ForceSaveSettings();
                }
                Google_LastUpdatedVersion = newSpreadsheetVersion;

                if (UpdateMode == eSpreadsheetUpdateMode.Replace)
                {
                    ClearAllData();
                }

                int CSVstartIdx = JsonString.IndexOf("[i2category]", StringComparison.Ordinal);
                while (CSVstartIdx > 0)
                {
                    CSVstartIdx += "[i2category]".Length;
                    int    endCat   = JsonString.IndexOf("[/i2category]", CSVstartIdx, StringComparison.Ordinal);
                    string category = JsonString.Substring(CSVstartIdx, endCat - CSVstartIdx);
                    endCat += "[/i2category]".Length;

                    int    endCSV = JsonString.IndexOf("[/i2csv]", endCat, StringComparison.Ordinal);
                    string csv    = JsonString.Substring(endCat, endCSV - endCat);

                    CSVstartIdx = JsonString.IndexOf("[i2category]", endCSV, StringComparison.Ordinal);

                    Import_I2CSV(category, csv, UpdateMode);

                    // Only the first CSV should clear the Data
                    if (UpdateMode == eSpreadsheetUpdateMode.Replace)
                    {
                        UpdateMode = eSpreadsheetUpdateMode.Merge;
                    }
                }

                if (I2Utils.IsPlaying())
                {
                    SaveLanguages(true);
                }

#if UNITY_EDITOR
                if (!string.IsNullOrEmpty(ErrorMsg))
                {
                    UnityEditor.EditorUtility.SetDirty(this);
                }
#endif
                return(ErrorMsg);
            }
            catch (System.Exception e)
            {
                Debug.LogWarning(e);
                return(e.ToString());
            }
        }