/// <summary> /// Load the specified CSV file. /// </summary> static bool LoadCSV (byte[] bytes, TextAsset asset, bool merge = false) { if (bytes == null) return false; ByteReader reader = new ByteReader(bytes); // The first line should contain "KEY", followed by languages. BetterList<string> header = reader.ReadCSV(); // There must be at least two columns in a valid CSV file if (header.size < 2) return false; header.RemoveAt(0); string[] languagesToAdd = null; if (string.IsNullOrEmpty(mLanguage)) localizationHasBeenSet = false; // Clear the dictionary if (!localizationHasBeenSet || (!merge && !mMerging) || mLanguages == null || mLanguages.Length == 0) { mDictionary.Clear(); mLanguages = new string[header.size]; if (!localizationHasBeenSet) { mLanguage = PlayerPrefs.GetString("Language", header[0]); localizationHasBeenSet = true; } for (int i = 0; i < header.size; ++i) { mLanguages[i] = header[i]; if (mLanguages[i] == mLanguage) mLanguageIndex = i; } } else { languagesToAdd = new string[header.size]; for (int i = 0; i < header.size; ++i) languagesToAdd[i] = header[i]; // Automatically resize the existing languages and add the new language to the mix for (int i = 0; i < header.size; ++i) { if (!HasLanguage(header[i])) { int newSize = mLanguages.Length + 1; #if UNITY_FLASH string[] temp = new string[newSize]; for (int b = 0, bmax = arr.Length; b < bmax; ++b) temp[b] = mLanguages[b]; mLanguages = temp; #else System.Array.Resize(ref mLanguages, newSize); #endif mLanguages[newSize - 1] = header[i]; Dictionary<string, string[]> newDict = new Dictionary<string, string[]>(); foreach (KeyValuePair<string, string[]> pair in mDictionary) { string[] arr = pair.Value; #if UNITY_FLASH temp = new string[newSize]; for (int b = 0, bmax = arr.Length; b < bmax; ++b) temp[b] = arr[b]; arr = temp; #else System.Array.Resize(ref arr, newSize); #endif arr[newSize - 1] = arr[0]; newDict.Add(pair.Key, arr); } mDictionary = newDict; } } } Dictionary<string, int> languageIndices = new Dictionary<string, int>(); for (int i = 0; i < mLanguages.Length; ++i) languageIndices.Add(mLanguages[i], i); // Read the entire CSV file into memory for (;;) { BetterList<string> temp = reader.ReadCSV(); if (temp == null || temp.size == 0) break; if (string.IsNullOrEmpty(temp[0])) continue; AddCSV(temp, languagesToAdd, languageIndices); } if (!mMerging && onLocalize != null) { mMerging = true; OnLocalizeNotification note = onLocalize; onLocalize = null; note(); onLocalize = note; mMerging = false; } return true; }
/// <summary> /// Load the specified CSV file. /// </summary> static bool LoadCSV(byte[] bytes, TextAsset asset, bool merge = false) { if (bytes == null) { return(false); } ByteReader reader = new ByteReader(bytes); // The first line should contain "KEY", followed by languages. BetterList <string> header = reader.ReadCSV(); // There must be at least two columns in a valid CSV file if (header.size < 2) { return(false); } header.RemoveAt(0); string[] languagesToAdd = null; if (string.IsNullOrEmpty(mLanguage)) { localizationHasBeenSet = false; } // Clear the dictionary if (!localizationHasBeenSet || (!merge && !mMerging) || mLanguages == null || mLanguages.Length == 0) { mDictionary.Clear(); mLanguages = new string[header.size]; if (!localizationHasBeenSet) { mLanguage = PlayerPrefs.GetString("Language", header[0]); localizationHasBeenSet = true; } for (int i = 0; i < header.size; ++i) { mLanguages[i] = header[i]; if (mLanguages[i] == mLanguage) { mLanguageIndex = i; } } } else { languagesToAdd = new string[header.size]; for (int i = 0; i < header.size; ++i) { languagesToAdd[i] = header[i]; } // Automatically resize the existing languages and add the new language to the mix for (int i = 0; i < header.size; ++i) { if (!HasLanguage(header[i])) { int newSize = mLanguages.Length + 1; #if UNITY_FLASH string[] temp = new string[newSize]; for (int b = 0, bmax = arr.Length; b < bmax; ++b) { temp[b] = mLanguages[b]; } mLanguages = temp; #else System.Array.Resize(ref mLanguages, newSize); #endif mLanguages[newSize - 1] = header[i]; Dictionary <string, string[]> newDict = new Dictionary <string, string[]>(); foreach (KeyValuePair <string, string[]> pair in mDictionary) { string[] arr = pair.Value; #if UNITY_FLASH temp = new string[newSize]; for (int b = 0, bmax = arr.Length; b < bmax; ++b) { temp[b] = arr[b]; } arr = temp; #else System.Array.Resize(ref arr, newSize); #endif arr[newSize - 1] = arr[0]; newDict.Add(pair.Key, arr); } mDictionary = newDict; } } } Dictionary <string, int> languageIndices = new Dictionary <string, int>(); for (int i = 0; i < mLanguages.Length; ++i) { languageIndices.Add(mLanguages[i], i); } // Read the entire CSV file into memory for (;;) { BetterList <string> temp = reader.ReadCSV(); if (temp == null || temp.size == 0) { break; } if (string.IsNullOrEmpty(temp[0])) { continue; } AddCSV(temp, languagesToAdd, languageIndices); } if (!mMerging && onLocalize != null) { mMerging = true; OnLocalizeNotification note = onLocalize; onLocalize = null; note(); onLocalize = note; mMerging = false; } return(true); }
private static bool LoadCSV(byte[] bytes, TextAsset asset, bool merge = false) { if (bytes == null) { return(false); } ByteReader byteReader = new ByteReader(bytes); BetterList <string> betterList = byteReader.ReadCSV(); if (betterList.size < 2) { return(false); } betterList.RemoveAt(0); string[] array = null; if (string.IsNullOrEmpty(mLanguage)) { localizationHasBeenSet = false; } if (!localizationHasBeenSet || (!merge && !mMerging) || mLanguages == null || mLanguages.Length == 0) { mDictionary.Clear(); mLanguages = new string[betterList.size]; if (!localizationHasBeenSet) { mLanguage = PlayerPrefs.GetString("Language", betterList[0]); localizationHasBeenSet = true; } for (int i = 0; i < betterList.size; i++) { mLanguages[i] = betterList[i]; if (mLanguages[i] == mLanguage) { mLanguageIndex = i; } } } else { array = new string[betterList.size]; for (int j = 0; j < betterList.size; j++) { array[j] = betterList[j]; } for (int k = 0; k < betterList.size; k++) { if (!HasLanguage(betterList[k])) { int num = mLanguages.Length + 1; Array.Resize(ref mLanguages, num); mLanguages[num - 1] = betterList[k]; Dictionary <string, string[]> dictionary = new Dictionary <string, string[]>(); foreach (KeyValuePair <string, string[]> item in mDictionary) { string[] array2 = item.Value; Array.Resize(ref array2, num); array2[num - 1] = array2[0]; dictionary.Add(item.Key, array2); } mDictionary = dictionary; } } } Dictionary <string, int> dictionary2 = new Dictionary <string, int>(); for (int l = 0; l < mLanguages.Length; l++) { dictionary2.Add(mLanguages[l], l); } while (true) { BetterList <string> betterList2 = byteReader.ReadCSV(); if (betterList2 == null || betterList2.size == 0) { break; } if (!string.IsNullOrEmpty(betterList2[0])) { AddCSV(betterList2, array, dictionary2); } } if (!mMerging && onLocalize != null) { mMerging = true; OnLocalizeNotification onLocalizeNotification = onLocalize; onLocalize = null; onLocalizeNotification(); onLocalize = onLocalizeNotification; mMerging = false; } return(true); }