//============================================================================= private static bool LoadItemsList() { if (isItemListLoaded) { return(true); } isItemListLoaded = true; StringReader reader = null; if (PlayerPrefs.HasKey(PreHelpers.GetLiveDataVersionString()) && false) { // Load locally string DocPath = PreHelpers.GetFileFolderPath(); string FilePath = DocPath + "Fairies.txt"; // Read file string Contents = null; try { StreamReader CurFile = new StreamReader(FilePath); Contents = CurFile.ReadToEnd(); // Close file CurFile.Close(); } catch { PlayerPrefsWrapper.DeleteKey(PreHelpers.GetLiveDataVersionString()); } reader = new StringReader(Contents); } else { // Load from resources string fullpath = "Fairies/Fairies"; TextAsset textAsset = (TextAsset)Resources.Load(fullpath, typeof(TextAsset)); if (textAsset == null) { Debug.Log(fullpath + " file not found."); return(false); } reader = new StringReader(textAsset.text); } // Create the fairies item list if one doesn't exist if (null == itemList) { itemList = new List <FairyItemData>(64); } // Clear the dictionary itemList.Clear(); string dataVal = string.Empty; string intext = string.Empty; // [ Fairy | Unlock Cost | Level 1 Cost | Level 2 Cost | Level 3 Cost ] int maxColumns = 5; int columnCount = 0; int itemCount = 0; // First parse through rows until we find the starting one // The 'fairies.txt' file has loads of info at the start that we can ignore as it's all excel settings int RowCount = 0; while (true) { intext = reader.ReadLine(); int Idx = intext.IndexOf("<Row"); if (Idx != -1) { RowCount++; } if (RowCount == 6) { break; } } // Start of text data, begin parsing the rows while (true) { int Idx, Idx2, Idx3, Idx4; bool isIdValid = false; columnCount = 0; // Read cell containing text code intext = reader.ReadLine(); // intext might look something like this now: // <Cell ss:StyleID="s32"><Data ss:Type="String">0001</Data></Cell> // Find the data in the cell - in this case it's "Item ID" Idx4 = intext.IndexOf("><"); if (Idx4 != -1) { Idx2 = intext.IndexOf(">", Idx4 + 2); if (Idx2 != -1) { Idx3 = intext.IndexOf("<", Idx2); if (Idx3 != -1) { // String is between Idx2 and Idx3 - this is the text 'ItemId' - "0001" dataVal = intext.Substring(Idx2 + 1, Idx3 - Idx2 - 1); itemList.Add(new FairyItemData()); UpdateItemData(itemCount, columnCount, dataVal); isIdValid = true; } } ++columnCount; } // If we've found an itemId continue reading the item data for this item if (isIdValid) { while (columnCount < maxColumns) { // Read cell containing text code intext = reader.ReadLine(); Idx4 = intext.IndexOf("><"); if (Idx4 != -1) { Idx2 = intext.IndexOf(">", Idx4 + 2); if (Idx2 != -1) { Idx3 = intext.IndexOf("<", Idx2); if (Idx3 != -1) { // String is between Idx2 and Idx3 - this is the text 'ItemId' - "0001" dataVal = intext.Substring(Idx2 + 1, Idx3 - Idx2 - 1); UpdateItemData(itemCount, columnCount, dataVal); } } } ++columnCount; } ++itemCount; } // Find the end of this row by looking for the start of the next row while (true) { intext = reader.ReadLine(); Idx = intext.IndexOf("<Row"); if (Idx != -1) { // Found end of row break; } else { Idx = intext.IndexOf("Table>"); if (Idx != -1) { // Found end of table //Debug.Log("Finished loading fairies items data list."); reader.Dispose(); // Order list by priority //itemList.Sort((x,y) => x.GetOrderPriority() - y.GetOrderPriority()); return(true); } } } } }
//============================================================================= private static bool LoadItemsList() { if (isItemListLoaded) { return(true); } isItemListLoaded = true; StringReader reader = null; if (PlayerPrefs.HasKey(PreHelpers.GetLiveDataVersionString()) && false) { // Load locally string DocPath = PreHelpers.GetFileFolderPath(); string FilePath = DocPath + "Cards.txt"; // Read file string Contents = null; try { StreamReader CurFile = new StreamReader(FilePath); Contents = CurFile.ReadToEnd(); // Close file CurFile.Close(); } catch { PlayerPrefsWrapper.DeleteKey(PreHelpers.GetLiveDataVersionString()); } reader = new StringReader(Contents); } else { // Load from resources string fullpath = "Cards/Cards"; TextAsset textAsset = (TextAsset)Resources.Load(fullpath, typeof(TextAsset)); if (textAsset == null) { Debug.Log(fullpath + " file not found."); return(false); } reader = new StringReader(textAsset.text); } // Create the TradingCard item list if one doesn't exist if (null == itemList) { itemList = new List <TradingCardSpreadsheetItem>(64); } // Clear the dictionary itemList.Clear(); string dataVal = string.Empty; string intext = string.Empty; // [ ItemID | Classification | Value | ValueScuffed | Rarity | Wait Time | Reveal Price | Page | Position | SmallGUI - 2D | LargeGUI - 2D ] int maxColumns = 11; int columnCount = 0; int itemCount = 0; // First parse through rows until we find the starting one // The 'TradingCarditems.txt' file has loads of info at the start that we can ignore as it's all excel settings int RowCount = 0; while (true) { intext = reader.ReadLine(); int Idx = intext.IndexOf("<Row"); if (Idx != -1) { RowCount++; } if (RowCount == 6) { break; } } // Start of text data, begin parsing the rows while (true) { int Idx, Idx2, Idx3, Idx4; bool isIdValid = false; columnCount = 0; // Read cell containing text code intext = reader.ReadLine(); // intext might look something like this now: // <Cell ss:StyleID="s32"><Data ss:Type="String">0001</Data></Cell> // Find the data in the cell - in this case it's "Item ID" Idx4 = intext.IndexOf("><"); if (Idx4 != -1) { Idx2 = intext.IndexOf(">", Idx4 + 2); if (Idx2 != -1) { Idx3 = intext.IndexOf("<", Idx2); if (Idx3 != -1) { // String is between Idx2 and Idx3 - this is the text 'ItemId' - "0001" dataVal = intext.Substring(Idx2 + 1, Idx3 - Idx2 - 1); itemList.Add(new TradingCardSpreadsheetItem()); UpdateItemData(itemCount, columnCount, dataVal); isIdValid = true; } } ++columnCount; } // If we've found an itemId continue reading the item data for this item if (isIdValid) { while (columnCount < maxColumns) { // Read cell containing text code intext = reader.ReadLine(); Idx4 = intext.IndexOf("><"); if (Idx4 != -1) { Idx2 = intext.IndexOf(">", Idx4 + 2); if (Idx2 != -1) { Idx3 = intext.IndexOf("<", Idx2); if (Idx3 != -1) { // String is between Idx2 and Idx3 - this is the text 'ItemId' - "0001" dataVal = intext.Substring(Idx2 + 1, Idx3 - Idx2 - 1); UpdateItemData(itemCount, columnCount, dataVal); } } } ++columnCount; } ++itemCount; } // Find the end of this row by looking for the start of the next row while (true) { intext = reader.ReadLine(); Idx = intext.IndexOf("<Row"); if (Idx != -1) { // Found end of row break; } else { Idx = intext.IndexOf("Table>"); if (Idx != -1) { // Found end of table //Debug.Log("Finished loading TradingCard items data list."); reader.Dispose(); // Order list by priority //itemList.Sort((x,y) => x.GetOrderPriority() - y.GetOrderPriority()); // Expand each item type into individual page sections int NumWinxPages = GetPageCount(eTradingCardClassification.WINX); int NumWildPages = GetPageCount(eTradingCardClassification.WILD); int NumStoryPages = GetPageCount(eTradingCardClassification.STORY); int NumStandardPages = GetPageCount(eTradingCardClassification.STANDARD); pageTypeOffsets[(int)eTradingCardClassification.WINX] = 0; pageTypeOffsets[(int)eTradingCardClassification.WILD] = NumWinxPages; pageTypeOffsets[(int)eTradingCardClassification.STORY] = NumWinxPages + NumWildPages; pageTypeOffsets[(int)eTradingCardClassification.STANDARD] = NumWinxPages + NumWildPages + NumStoryPages; OffsetPageCount(eTradingCardClassification.WINX, pageTypeOffsets[(int)eTradingCardClassification.WINX]); OffsetPageCount(eTradingCardClassification.WILD, pageTypeOffsets[(int)eTradingCardClassification.WILD]); OffsetPageCount(eTradingCardClassification.STORY, pageTypeOffsets[(int)eTradingCardClassification.STORY]); OffsetPageCount(eTradingCardClassification.STANDARD, pageTypeOffsets[(int)eTradingCardClassification.STANDARD]); return(true); } } } } }
//============================================================================= IEnumerator LoadDataAndSaveToPref(string dataKey, string dataFilename, string uid) { // Save data as a file string DocPath = PreHelpers.GetFileFolderPath(); string FilePath = DocPath + dataFilename + ".txt"; IRequest req = null; if (uid != null) { req = PlayerWS.GetPlayerData(dataKey, uid); } else { req = PlayerWS.RestorePlayerData(dataKey); } yield return(StartCoroutine(req)); if (req.Status != RequestStatus.STATUS_OK) { Debug.Log("Error downloading friend data for key: " + dataKey + " - " + req.Error); DataLoaded = false; // Remove existing file File.Delete(FilePath); } else { // Retrieve data as JSON Dictionary <string, object> OutDict = req.Result as Dictionary <string, object>; JSON DataOut = new JSON(); DataOut.fields = OutDict; string OutString = DataOut.serialized; // Is it a valid file? (contains JSON data and not just {}) if (OutString.Length > 8) { DataLoaded = true; } else { DataLoaded = false; } if (DataLoaded) { Debug.Log("Data downloaded ok for key: " + dataKey + " (" + OutString.Length + ")"); // Open/create file PlayerPrefs.SetString(dataFilename, OutString); } else { // Remove existing file PlayerPrefs.DeleteKey(dataFilename); Debug.Log("Data for key is empty: " + dataKey + " (" + OutString.Length + ")"); } } }
//============================================================================= IEnumerator GetLiveData(int Version, string URL) { WWW GameDataWWW = new WWW(URL); yield return(GameDataWWW); uint NewGameDataChecksum = 0; // Find checksum int ChecksumIndex = URL.IndexOf("wfs2_"); if (ChecksumIndex != -1) { try { NewGameDataChecksum = uint.Parse(URL.Substring(ChecksumIndex + 5, 8), System.Globalization.NumberStyles.HexNumber); } catch { NewGameDataChecksum = 0; } } if (String.IsNullOrEmpty(GameDataWWW.error) && (GameDataWWW.bytes != null)) { UnityEngine.Debug.Log("Updated Game Data OK"); // Make sure checksum matches uint Checksum = HelperChecksum.GetChecksum(GameDataWWW.bytes); if (Checksum == NewGameDataChecksum) { // Save file string DocPath = PreHelpers.GetFileFolderPath(); string FilePath = DocPath + "wfs2.zip"; // Write file FileStream fs = null; try { fs = new FileStream(FilePath, FileMode.Create); } catch { Debug.Log("GameData file creation exception: " + FilePath); } if (fs != null) { PlayerPrefs.SetInt("LiveDataVersion", Version); BinaryWriter CurFile = new BinaryWriter(fs); CurFile.Write(GameDataWWW.bytes); // Close file CurFile.Close(); fs.Close(); // Unzip into local data files //PreHelpers.LoadGameDataUpdate(); } } else { UnityEngine.Debug.Log("Game Data update checksum mismatch: " + Checksum.ToString("X") + " " + NewGameDataChecksum.ToString("X")); } } else { UnityEngine.Debug.Log("Game Data updated error: " + GameDataWWW.error); } }
//===================================================== void LoadLiveUpdate() { string DocPath = PreHelpers.GetFileFolderPath(); string FilePath = DocPath + "wfs2.zip"; if (System.IO.File.Exists(FilePath) == false) { return; } // Read file FileStream fs = null; try { fs = new FileStream(FilePath, FileMode.Open); } catch { Debug.Log("GameData file open exception: " + FilePath); } if (fs != null) { try { // Read zip file ZipFile zf = new ZipFile(fs); int numFiles = 0; if (zf.TestArchive(true) == false) { Debug.Log("Zip file failed integrity check!"); zf.IsStreamOwner = false; zf.Close(); fs.Close(); } else { foreach (ZipEntry zipEntry in zf) { // Ignore directories if (!zipEntry.IsFile) { continue; } String entryFileName = zipEntry.Name; // Skip .DS_Store files if (entryFileName.Contains("DS_Store")) { continue; } Debug.Log("Unpacking zip file entry: " + entryFileName); byte[] buffer = new byte[4096]; // 4K is optimum Stream zipStream = zf.GetInputStream(zipEntry); // Manipulate the output filename here as desired. string fullZipToPath = PreHelpers.GetFileFolderPath() + Path.GetFileName(entryFileName); // Unzip file in buffered chunks. This is just as fast as unpacking to a buffer the full size // of the file, but does not waste memory. // The "using" will close the stream even if an exception occurs. using (FileStream streamWriter = System.IO.File.Create(fullZipToPath)) { StreamUtils.Copy(zipStream, streamWriter, buffer); } numFiles++; } zf.IsStreamOwner = false; zf.Close(); fs.Close(); // If we've unpacked the local files (at least 13) then start using local spreadsheets data Debug.Log("Zip updated with " + numFiles + " files (needs 13)"); if (numFiles >= 13) { PlayerPrefs.SetInt(PreHelpers.GetLiveDataVersionString(), 1); } // Reload managers TextManager.Reload(); SettingsManager.Reload(); TradingCardItemsManager.Reload(); ClothingItemsManager.Reload(); FairyItemsManager.Reload(); WildMagicItemsManager.Reload(); NPCItemsManager.Reload(); } } catch { Debug.Log("Zip file error!"); } // Remove zip file Debug.Log("Removing zip file"); System.IO.File.Delete(FilePath); } }
//============================================================================= static bool LoadLanguage() { string language_type = PreHelpers.GetLanguageCode(); Debug.Log("Loaded localisations for language code: '" + language_type + "'"); if (m_bLanguageLoaded == true) { return(true); } m_bLanguageLoaded = true; StringReader reader = null; if (PlayerPrefs.HasKey(PreHelpers.GetLiveDataVersionString()) && false) { // Load locally string DocPath = PreHelpers.GetFileFolderPath(); string FilePath = DocPath + "Localisations_" + language_type.ToUpper() + ".txt"; // Read file string Contents = null; try { StreamReader CurFile = new StreamReader(FilePath); Contents = CurFile.ReadToEnd(); // Close file CurFile.Dispose(); } catch { PlayerPrefs.DeleteKey(PreHelpers.GetLiveDataVersionString()); } reader = new StringReader(Contents); } else { // Load from resources string fullpath = "Languages/Localisations_" + language_type.ToUpper(); TextAsset textAsset = (TextAsset)Resources.Load(fullpath, typeof(TextAsset)); if (textAsset == null) { Debug.Log(fullpath + " file not found."); return(false); } reader = new StringReader(textAsset.text); } // create the text hash table if one doesn't exist if (textTable == null) { textTable = new Dictionary <string, string>(); } // clear the dictionary textTable.Clear(); string key = string.Empty; string val = string.Empty; string multival = string.Empty; string intext = string.Empty; // First parse through rows until we find the starting one // The 'localisations.txt' file has loads of info at the start that we can ignore as it's all excel settings int RowCount = 0; while (true) { intext = reader.ReadLine(); int Idx = intext.IndexOf("<Row"); if (Idx != -1) { RowCount++; } if (RowCount == 7) { break; } } // Here's an example of a 'row' from the localisations.txt file containing the various cells /* * <Row> * <Cell ss:StyleID="s32"><Data ss:Type="String">POPUP_CONFIRMATION</Data></Cell> * <Cell><Data ss:Type="Number">7</Data></Cell> * <Cell ss:StyleID="s32"><Data ss:Type="String">Confirm</Data></Cell> * <Cell ss:StyleID="s32"><Data ss:Type="Number">0</Data></Cell> * <Cell ss:StyleID="s32"><Data ss:Type="String">Conferma</Data></Cell> * <Cell ss:StyleID="s31"/> * <Cell ss:StyleID="s31"/> * <Cell ss:StyleID="s31"/> * </Row> */ // Start of text data, begin parsing the rows int Line = 0; while (true) { // Read cell containing text code intext = reader.ReadLine(); // intext might look something like this now: // <Cell ss:StyleID="s32"><Data ss:Type="String">POPUP_CONFIRMATION</Data></Cell> // Find the data in the cell - in this case it's "POPUP_CONFIRMATION" bool bValidKey = false; int Idx4 = intext.IndexOf("><"); if (Idx4 != -1) { int Idx2 = intext.IndexOf(">", Idx4 + 2); if (Idx2 != -1) { int Idx3 = intext.IndexOf("<", Idx2); if (Idx3 != -1) { // String is between Idx2 and Idx3 - this is the text 'key' - "POPUP_CONFIRMATION" key = intext.Substring(Idx2 + 1, Idx3 - Idx2 - 1); bValidKey = true; } } } // If we've found a key continue reading the translation text for this key if (bValidKey) { // Skip until correct language column is reached - each column is a line in the file int ColumnCount = 2; do { multival = reader.ReadLine(); ColumnCount++; } while(ColumnCount <= 2); intext = reader.ReadLine(); bool bValidVal = false; int Idx5 = intext.IndexOf("><"); if (Idx5 != -1) { int Idx6 = intext.IndexOf(">", Idx5 + 2); if (Idx6 != -1) { int Idx7 = intext.IndexOf("<", Idx6); if (Idx7 != -1) { // String is between Idx2 and Idx3 - this is the text 'val' val = intext.Substring(Idx6 + 1, Idx7 - Idx6 - 1); val = val.Replace("(newline)", "\n"); /* Below replacement is now done on server * // Convert newline characters and other special codes * val = val.Replace( " " , "\n" ); * val = val.Replace( "'" , "'" ); * val = val.Replace( """ , "\"" ); * val = val.Replace( "&" , "&" ); * * // Replace microsoft word special chars * val = val.Replace( "’" , "'" ); * val = val.Replace( "–" , "-" ); * val = val.Replace( "‘" , "'" ); * val = val.Replace( "“" , "\"" ); * val = val.Replace( "”" , "\"" ); */ bValidVal = true; } } } if (bValidVal) { //Debug.Log( "Valid Key: " + key + " (" + val + ")" ); try { textTable.Add(key, val); } catch { Debug.LogError("Duplicate key in localisations sheet: " + key + " " + val); } } else { Debug.Log("Invalid Value for key [" + key + "]"); } // Find the end of this row by looking for the start of the next row while (true) { multival = reader.ReadLine(); int Idx = multival.IndexOf("<Row"); if (Idx != -1) { // Found next row, break break; } // End of excel sheet? Idx = multival.IndexOf("Workbook>"); if (Idx != -1) { // Found end of workbook reader.Dispose(); return(true); } } } else { Debug.Log("Invalid Key! " + key); #if UNITY_EDITOR UnityEditor.EditorUtility.DisplayDialog("Error", "Key missing in localisations after:" + key + " - line:" + Line, "OK"); #endif break; } Line++; } reader.Dispose(); return(true); }