ReadCSV() public method

Read a single line of Comma-Separated Values from the file.
public ReadCSV ( ) : BetterList
return BetterList
Beispiel #1
0
    private static bool LoadCSV(byte[] _asset, string _keysPrefix)
    {
        ByteReader          byteReader = new ByteReader(_asset);
        BetterList <string> betterList = byteReader.ReadCSV();

        if (betterList.size < 2)
        {
            return(false);
        }
        betterList[0] = HeaderKey;
        if (!string.Equals(betterList[0], HeaderKey))
        {
            Debug.LogError(string.Concat(new string[]
            {
                "Invalid localization CSV file. The first value is expected to be '",
                HeaderKey,
                "', followed by language columns.\nInstead found '",
                betterList[0],
                "'"
            }));
            return(false);
        }


        while (betterList != null)
        {
            AddCSV(betterList, _keysPrefix);
            betterList = byteReader.ReadCSV();
        }
        return(true);
    }
Beispiel #2
0
    public static bool LoadCSV(TextAsset asset)
    {
        ByteReader          byteReader = new ByteReader(asset);
        BetterList <string> betterList = byteReader.ReadCSV();

        if (betterList.size < 2)
        {
            return(false);
        }
        betterList[0] = "KEY";
        if (!string.Equals(betterList[0], "KEY"))
        {
            Debug.LogError("Invalid localization CSV file. The first value is expected to be 'KEY', followed by language columns.\nInstead found '" + betterList[0] + "'", asset);
            return(false);
        }
        Localization.mLanguages = new string[betterList.size - 1];
        for (int i = 0; i < Localization.mLanguages.Length; i++)
        {
            Localization.mLanguages[i] = betterList[i + 1];
        }
        Localization.mDictionary.Clear();
        while (betterList != null)
        {
            Localization.AddCSV(betterList);
            betterList = byteReader.ReadCSV();
        }
        return(true);
    }
Beispiel #3
0
    public static bool LoadCSV(TextAsset asset)
    {
        ByteReader          byteReader = new ByteReader(asset);
        BetterList <string> betterList = byteReader.ReadCSV();

        if (betterList.size < 2)
        {
            return(false);
        }
        betterList[0] = "KEY";
        if (!string.Equals(betterList[0], "KEY"))
        {
            return(false);
        }
        Localization.knownLanguages = new string[betterList.size - 1];
        for (int i = 0; i < Localization.knownLanguages.Length; i++)
        {
            Localization.knownLanguages[i] = betterList[i + 1];
        }
        Localization.mDictionary.Clear();
        while (betterList != null)
        {
            Localization.AddCSV(betterList);
            betterList = byteReader.ReadCSV();
        }
        return(true);
    }
Beispiel #4
0
    private static bool LoadCSV(byte[] bytes)
    {
        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);

        _langIndex = 0;
        _langs     = new string[header.size];
        for (int i = 0; i < header.size; i++)
        {
            _langs[i] = header[i];
        }

        _langMap  = new Dictionary <string, string[]>();
        _langKeys = new List <string>();
        for (; ;)
        {
            BetterList <string> temp = reader.ReadCSV();
            if (temp == null || temp.size == 0)
            {
                break;
            }

            string key = temp[0];
            if (string.IsNullOrEmpty(key))
            {
                continue;
            }

            var fields = new string[_langs.Length];
            for (int i = 1; i < temp.size; i++)
            {
                fields[i - 1] = temp[i];
            }

            _langMap.Add(key, fields);
            _langKeys.Add(key);
        }

        return(true);
    }
Beispiel #5
0
        public Boolean TryLoadCSV(Byte[] bytes, Boolean merge)
        {
            if (bytes == null)
            {
                return(false);
            }

            ByteReader          byteReader = new ByteReader(bytes);
            BetterList <String> betterList = byteReader.ReadCSV();

            if (betterList.size < 2)
            {
                return(false);
            }

            betterList.RemoveAt(0);
            lock (_lock)
            {
                String[] newLanguages;
                Dictionary <String, Int32> languageIndices = PrepareLanguages(merge, betterList, out newLanguages);
                while (true)
                {
                    BetterList <string> newValues;
                    do
                    {
                        newValues = byteReader.ReadCSV();
                        if (newValues == null || newValues.size == 0)
                        {
                            goto label_33;
                        }
                    } while (string.IsNullOrEmpty(newValues[0]));

                    AddCSV(newValues, newLanguages, languageIndices);
                }

label_33:
                if (_merging || Localization.onLocalize == null)
                {
                    return(true);
                }

                _merging = true;

                Localization.OnLocalizeNotification localizeNotification = Localization.onLocalize;
                Localization.onLocalize = null;
                localizeNotification();
                Localization.onLocalize = localizeNotification;

                _merging = false;
                return(true);
            }
        }
Beispiel #6
0
    static void LoadDictionary(TextAsset asset)
    {
        var reader = new ByteReader(asset.bytes);
        var row    = reader.ReadCSV();

        if (row.size < 2)
        {
            Debug.LogError("Localization - There must be at least two columns in a valid CSV file. Columns: " + row.size);
            return;
        }

        if (row[0] != "KEY")
        {
            Debug.LogError("Localization - The first row must be KEY. First row: " + row[0]);
            return;
        }

        languages = new SystemLanguage[row.size - (row[row.size - 1] == "TAG" ? 2 : 1)];
        for (int i = 0; i < languages.Length; ++i)
        {
            languages[i] = row[i + 1].ToEnum <SystemLanguage>();
        }

        dictionary.Clear();

        while (row != null)
        {
            if (row.size > 1)
            {
                // Add a single line from a CSV file to the Localization list
                var temp = new string[row.size - 1];
                for (int i = 1; i < row.size; ++i)
                {
                    temp[i - 1] = row[i];
                }

                try
                {
                    dictionary.Add(row[0], temp);
                }
                catch (Exception e)
                {
                    Debug.LogError("Localization - Unable to add '" + row[0] + "' to the Localization dictionary: " + e.Message);
                }
            }
            row = reader.ReadCSV();
        }

        Debug.LogFormat("Localization - Loaded {0} languages", languages.Length);
    }
Beispiel #7
0
        public static void LoadLocalization(Object asset)
        {
            TextAsset textAsset = asset as TextAsset;

            if (textAsset == null)
            {
                Log("Asset called '{0}' is not a TextAsset as expected.", asset.name);
                return;
            }

            Log("Processing asset '{0}' as localization.", asset.name);

            ByteReader byteReader = new ByteReader(textAsset);

            string[] languages      = Trim(byteReader.ReadCSV().ToArray());
            string[] knownLanguages = Localization.knownLanguages;

            while (true)
            {
                BetterList <string> values = byteReader.ReadCSV();
                if (values == null)
                {
                    break;
                }

                string[] translations = new string[knownLanguages.Length];
                for (int i = 0; i < knownLanguages.Length; i++)
                {
                    string language = knownLanguages[i];
                    int    index    = System.Array.IndexOf(languages, language);
                    if (index == -1)
                    {
                        continue;
                    }

                    translations[i] = values[index].Trim();
                }

                var key = values[0];
                if (Localization.dictionary.ContainsKey(key))
                {
                    Localization.dictionary[key] = translations;
                }
                else
                {
                    Localization.dictionary.Add(key, translations);
                }
            }
        }
Beispiel #8
0
    /// <summary>
    /// Load the specified CSV file.
    /// </summary>

    static bool LoadCSV(byte[] bytes, TextAsset asset)
    {
        if (bytes == null)
        {
            return(false);
        }
        ByteReader reader = new ByteReader(bytes);

        // The first line should contain "KEY", followed by languages.
        BetterList <string> temp = reader.ReadCSV();

        // There must be at least two columns in a valid CSV file
        if (temp.size < 2)
        {
            return(false);
        }

        // The first entry must be 'KEY', capitalized
        temp[0] = "KEY";

#if !UNITY_3_5
        // Ensure that the first value is what we expect
        if (!string.Equals(temp[0], "KEY"))
        {
            Debug.LogError("Invalid localization CSV file. The first value is expected to be 'KEY', followed by language columns.\n" +
                           "Instead found '" + temp[0] + "'", asset);
            return(false);
        }
        else
#endif
        {
            mLanguages = new string[temp.size - 1];
            for (int i = 0; i < mLanguages.Length; ++i)
            {
                mLanguages[i] = temp[i + 1];
            }
        }

        mDictionary.Clear();

        // Read the entire CSV file into memory
        while (temp != null)
        {
            AddCSV(temp);
            temp = reader.ReadCSV();
        }
        return(true);
    }
Beispiel #9
0
 public bool LoadFromCsvFile(string fileName)
 {
     dataMap.Clear();
     try{
         ByteReader reader = ByteReader.Open(fileName);
         int        line   = 0;
         for (; ;)
         {
             BetterList <string> temp = reader.ReadCSV();
             if (temp == null || temp.size == 0)
             {
                 break;
             }
             if (line > 2)
             {
                 language newData = new language();
                 newData.ReadFrom(temp);
                 dataMap.Add(newData.Key, newData);
             }
             line++;
         }
     }catch (Exception e) {
         return(false);
     }
     return(dataMap.Count > 0);
 }
Beispiel #10
0
    /// <summary>
    /// Load the specified CSV file.
    /// </summary>

    static public bool LoadCSV(TextAsset asset)
    {
#if SHOW_REPORT
        mUsed.Clear();
#endif
        ByteReader reader = new ByteReader(asset);

        // The first line should contain "KEY", followed by languages.
        BetterList <string> temp = reader.ReadCSV();

        // There must be at least two columns in a valid CSV file
        if (temp.size < 2)
        {
            return(false);
        }

        // The first entry must be 'KEY', capitalized
        temp[0] = "KEY";

#if !UNITY_3_5
        // Ensure that the first value is what we expect
        if (!string.Equals(temp[0], "KEY"))
        {
            return(false);
        }
        else
#endif
        {
            knownLanguages = new string[temp.size - 1];
            for (int i = 0; i < knownLanguages.Length; ++i)
            {
                knownLanguages[i] = temp[i + 1];
            }
        }

        mDictionary.Clear();

        // Read the entire CSV file into memory
        while (temp != null)
        {
            AddCSV(temp);
            temp = reader.ReadCSV();
        }
        return(true);
    }
    static public bool LoadCSV(TextAsset asset)
    {
        ByteReader          reader = new ByteReader(asset);
        BetterList <string> temp   = reader.ReadCSV();

        if (temp.size < 2)
        {
            return(false);
        }
        temp[0] = "KEY";
        mDictionary.Clear();
        while (temp != null)
        {
            AddCSV(temp);
            temp = reader.ReadCSV();
        }

        return(true);
    }
Beispiel #12
0
        private static void LoadCSVLocalization(Object asset)
        {
            TextAsset textAsset = asset.Cast <TextAsset>();

            if (textAsset is null)
            {
                Logger.LogWarning("Asset called '{0}' is not a TextAsset as expected.", asset.name);
                return;
            }

            //Implementation.Log("Processing asset '{0}' as csv localization.", asset.name);

            ByteReader byteReader = new ByteReader(textAsset);

            string[] languages = ModAssetBundleManager.Trim(byteReader.ReadCSV().ToArray());

            while (true)
            {
                string[] values = byteReader.ReadCSV()?.ToArray();
                if (values is null || languages is null || values.Length == 0 || languages.Length == 0)
                {
                    break;
                }

                string locID = values[0];
                Dictionary <string, string> locDict = new Dictionary <string, string>();

                int maxIndex = System.Math.Min(values.Length, languages.Length);
                for (int j = 1; j < maxIndex; j++)
                {
                    if (!string.IsNullOrEmpty(values[j]) && !string.IsNullOrEmpty(languages[j]))
                    {
                        locDict.Add(languages[j], values[j]);
                    }
                }

                LoadLocalization(locID, locDict, USE_ENGLISH_AS_DEFAULT);
            }
        }
    /// <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> temp = reader.ReadCSV();

        // There must be at least two columns in a valid CSV file
        if (temp.size < 2)
        {
            return(false);
        }

        // Clear the dictionary
        if (!merge || temp.size - 1 != mLanguage.Length)
        {
            merge = false;
            mDictionary.Clear();
        }

        temp[0]    = "KEY";
        mLanguages = new string[temp.size - 1];
        for (int i = 0; i < mLanguages.Length; ++i)
        {
            mLanguages[i] = temp[i + 1];
        }

        // Read the entire CSV file into memory
        while (temp != null)
        {
            AddCSV(temp, !merge);
            temp = reader.ReadCSV();
        }
        return(true);
    }
Beispiel #14
0
 static public int ReadCSV(IntPtr l)
 {
     try {
         ByteReader self = (ByteReader)checkSelf(l);
         var        ret  = self.ReadCSV();
         pushValue(l, true);
         pushValue(l, ret);
         return(2);
     }
     catch (Exception e) {
         return(error(l, e));
     }
 }
Beispiel #15
0
        public LanguageMap()
        {
            Byte[] tableData = ReadEmbadedTable();

            ByteReader          reader = new ByteReader(tableData);
            BetterList <String> cells  = reader.ReadCSV();

            if (cells.size < 2 || cells[0] != LanguageKey)
            {
                throw new CsvParseException("Invalid localisation file.");
            }

            Int32 languageCount = cells.size - 1;
            Dictionary <Int32, String> cellLanguages = new Dictionary <Int32, String>(languageCount);

            _languages      = new Dictionary <String, SortedList <String, String> >(languageCount);
            _knownLanguages = new String[languageCount];

            for (Int32 i = 1; i < cells.size; i++)
            {
                String language = cells[i];
                cellLanguages.Add(i, language);

                _knownLanguages[i - 1] = language;

                SortedList <String, String> dic = new SortedList <String, String>();
                dic.Add(LanguageKey, language);

                _languages.Add(language, dic);
            }

            ReadText(reader, cellLanguages);

            _failbackLanguage = cellLanguages[1];
            _failback         = _languages[_failbackLanguage];
            _current          = _failback;

            LoadExternalText();

            SelectLanguage(LanguagePrefs.Key);
        }
Beispiel #16
0
        private void ReadText(ByteReader reader, Dictionary <Int32, String> cellLanguages)
        {
            while (reader.canRead)
            {
                BetterList <String> cells = reader.ReadCSV();
                if (cells == null || cells.size < 2)
                {
                    continue;
                }

                String key = cells[0];
                if (String.IsNullOrEmpty(key))
                {
                    continue;
                }

                for (Int32 i = 1; i < cells.size; i++)
                {
                    String value    = cells[i];
                    String language = cellLanguages[i];
                    StoreValue(language, key, value);
                }
            }
        }
Beispiel #17
0
	/// <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;
	}
Beispiel #18
0
    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(Localization.mLanguage))
        {
            Localization.localizationHasBeenSet = false;
        }
        if (!Localization.localizationHasBeenSet || (!merge && !Localization.mMerging) || Localization.mLanguages == null || Localization.mLanguages.Length == 0)
        {
            Localization.mDictionary.Clear();
            Localization.mLanguages = new string[betterList.size];
            if (!Localization.localizationHasBeenSet)
            {
                Localization.mLanguage = PlayerPrefs.GetString("Language", betterList[0]);
                Localization.localizationHasBeenSet = true;
            }
            for (int i = 0; i < betterList.size; i++)
            {
                Localization.mLanguages[i] = betterList[i];
                if (Localization.mLanguages[i] == Localization.mLanguage)
                {
                    Localization.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 (!Localization.HasLanguage(betterList[k]))
                {
                    int num = Localization.mLanguages.Length + 1;
                    Array.Resize <string>(ref Localization.mLanguages, num);
                    Localization.mLanguages[num - 1] = betterList[k];
                    Dictionary <string, string[]> dictionary = new Dictionary <string, string[]>();
                    foreach (KeyValuePair <string, string[]> current in Localization.mDictionary)
                    {
                        string[] value = current.Value;
                        Array.Resize <string>(ref value, num);
                        value[num - 1] = value[0];
                        dictionary.Add(current.Key, value);
                    }
                    Localization.mDictionary = dictionary;
                }
            }
        }
        Dictionary <string, int> dictionary2 = new Dictionary <string, int>();

        for (int l = 0; l < Localization.mLanguages.Length; l++)
        {
            dictionary2.Add(Localization.mLanguages[l], l);
        }
        while (true)
        {
            BetterList <string> betterList2 = byteReader.ReadCSV();
            if (betterList2 == null || betterList2.size == 0)
            {
                break;
            }
            if (!string.IsNullOrEmpty(betterList2[0]))
            {
                Localization.AddCSV(betterList2, array, dictionary2);
            }
        }
        if (!Localization.mMerging && Localization.onLocalize != null)
        {
            Localization.mMerging = true;
            Localization.OnLocalizeNotification onLocalizeNotification = Localization.onLocalize;
            Localization.onLocalize = null;
            onLocalizeNotification();
            Localization.onLocalize = onLocalizeNotification;
            Localization.mMerging   = false;
        }
        if (merge)
        {
            if (Localization.onLocalize != null)
            {
                Localization.onLocalize();
            }
            UIRoot.Broadcast("OnLocalize");
        }
        return(true);
    }
Beispiel #19
0
    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);
    }
Beispiel #20
0
 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;
     }
     if (!merge || betterList.size - 1 != Localization.mLanguage.Length)
     {
         merge = false;
         Localization.mDictionary.Clear();
     }
     betterList[0] = "KEY";
     Localization.mLanguages = new string[betterList.size - 1];
     for (int i = 0; i < Localization.mLanguages.Length; i++)
     {
         Localization.mLanguages[i] = betterList[i + 1];
     }
     while (betterList != null)
     {
         Localization.AddCSV(betterList, !merge);
         betterList = byteReader.ReadCSV();
     }
     return true;
 }
Beispiel #21
0
	/// <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> temp = reader.ReadCSV();

		// There must be at least two columns in a valid CSV file
		if (temp.size < 2) return false;

		// Clear the dictionary
		if (!merge || temp.size - 1 != mLanguage.Length)
		{
			merge = false;
			mDictionary.Clear();
		}

		temp[0] = "KEY";
		mLanguages = new string[temp.size - 1];
		for (int i = 0; i < mLanguages.Length; ++i)
			mLanguages[i] = temp[i + 1];

		// Read the entire CSV file into memory
		while (temp != null)
		{
			AddCSV(temp, !merge);
			temp = reader.ReadCSV();
		}
		return true;
	}
Beispiel #22
0
    private static void OnCSVLoad <T>(object CSVObject, Dictionary <int, T> entity)
    {
        List <string> tempTable = new List <string>();
        ByteReader    reader    = new ByteReader(CSVObject as TextAsset);
        //表头
        List <string> temp = reader.ReadCSV();

        for (int i = 0; i < temp.Count; i++)
        {
            tempTable.Add(temp[i]);
        }
        Type type = typeof(T);

        PropertyInfo[] piList = type.GetProperties();
        //表的第二行数据类型
        List <string> tempType = reader.ReadCSV();
        //表的第三行数据说明
        List <string> tempInfo = reader.ReadCSV();

        //行内容
        temp = reader.ReadCSV();
        int nIndex = 0;

        while (null != temp)
        {
            Type t           = typeof(T);
            T    entityValue = (T)Assembly.GetExecutingAssembly().CreateInstance(t.FullName, false);
            if (temp[0] == null)
            {
                Debug.LogError("csv表格中有空行");
            }

            int key = 0;
            int.TryParse(temp[0], out key);
            if (key == 0)
            {
                Debug.Log("CSV id:" + "csv表格:" + t.FullName + " 中有空行.");
            }

            entity.Add(key, entityValue);
            int colIndex = 0;
            for (int b = 0; b < piList.Length; b++)
            {
                try
                {
                    if (tempTable.Contains(piList[b].Name) && temp[colIndex] != "")
                    {
                        switch (piList[b].PropertyType.ToString())
                        {
                        case "System.String":
                            piList[b].SetValue(entityValue, Convert.ToString(temp[colIndex]), null);
                            break;

                        case "System.Int32":
                            piList[b].SetValue(entityValue, int.Parse(temp[colIndex]), null);
                            break;

                        case "System.Boolean":
                            piList[b].SetValue(entityValue, bool.Parse(temp[colIndex]), null);
                            break;

                        case "System.Single":
                            piList[b].SetValue(entityValue, float.Parse(temp[colIndex]), null);
                            break;

                        case "UnityEngine.Vector2":
                            string   value = temp[colIndex];
                            string[] vecs  = value.Split(';');
                            Vector2  vec   = new Vector2(float.Parse(vecs[0]), float.Parse(vecs[1]));
                            piList[b].SetValue(entityValue, vec, null);
                            break;

                        case "System.Collections.Generic.List`1[System.Int32]":
                            string[]   values    = temp[colIndex].Split(';');
                            List <int> valueList = new List <int>();
                            for (int j = 0; j < values.Length; j++)
                            {
                                if (values[j] != null && values[j] != "")
                                {
                                    valueList.Add(Convert.ToInt32(values[j]));
                                }
                            }
                            piList[b].SetValue(entityValue, valueList, null);
                            break;

                        case "System.Collections.Generic.List`1[System.String]":
                            string[]      strvalues    = temp[colIndex].Split(';');
                            List <string> strvalueList = new List <string>();

                            for (int j = 0; j < strvalues.Length; j++)
                            {
                                strvalueList.Add(strvalues[j]);
                            }
                            piList[b].SetValue(entityValue, strvalueList, null);
                            break;

                        case "System.Collections.Generic.List`1[System.Single]":
                            string[]     flvalues    = temp[colIndex].Split(';');
                            List <float> flvalueList = new List <float>();

                            for (int j = 0; j < flvalues.Length; j++)
                            {
                                flvalueList.Add(float.Parse(flvalues[j]));
                            }
                            piList[b].SetValue(entityValue, flvalueList, null);
                            break;

                        case "System.Collections.Generic.List`1[UnityEngine.Vector2]":
                            string[]       vecvalues    = temp[colIndex].Split(';');
                            List <Vector2> vecvalueList = new List <Vector2>();
                            for (int j = 0; j < vecvalues.Length; j++)
                            {
                                string[] xy = vecvalues[j].Split(':');
                                vecvalueList.Add(new Vector2(float.Parse(xy[0]), float.Parse(xy[1])));
                            }
                            piList[b].SetValue(entityValue, vecvalueList, null);
                            break;

                        default:
                            Debug.Log(t.FullName.ToString() + "表中" + temp[colIndex] + " 转换失败 类型:" + piList[b].PropertyType.ToString());
                            break;
                        }
                    }
                }
                catch (Exception e)
                {
                    Debug.LogError("**************" + "csv表格:" + t.FullName + "**************" + piList[b].Name + "**************" + colIndex + "==" + temp[colIndex]);
                }

                colIndex++;
            }
            nIndex++;
            temp = reader.ReadCSV();
        }
    }
Beispiel #23
0
    public static List <T> LoadImportData <T>(string fileName)
    {
#if UNITY_EDITOR
        object obj = Resources.Load("Data/" + fileName);
        if (obj == null)
        {
            obj = BundleManager.LoadTable(fileName);
            if (obj == null)
            {
                Debug.LogWarning("not found file :" + fileName);
                return(null);
            }
        }
#else
        object obj = BundleManager.LoadTable(fileName);
        if (obj == null)
        {
            obj = Resources.Load("Data/" + fileName);
            if (obj == null)
            {
                return(null);
            }
        }
#endif

        ByteReader          reader  = new ByteReader(GetBytes(obj));
        BetterList <string> keyList = reader.ReadCSV();

        while (keyList != null)
        {
            if (keyList[0].Contains("*"))
            {
                keyList.RemoveAt(0);
                break;
            }
            keyList = reader.ReadCSV();
        }
        List <T>    mDataList = new List <T>();
        int         nameLen   = keyList.size;
        FieldInfo[] fieldInfo = new FieldInfo[nameLen];
        for (int i = 0; i < nameLen; i++)
        {
            fieldInfo[i] = typeof(T).GetField(keyList[i]);
        }

        BetterList <string> temp;
        temp = reader.ReadCSV();
        while (temp != null)
        {
            if (string.IsNullOrEmpty(temp[0]) || !temp[0].Contains("*"))
            {
                temp = reader.ReadCSV();
                continue;
            }
            temp.RemoveAt(0);
            {
                T t = (default(T) == null) ? Activator.CreateInstance <T>() : default(T);
                for (int j = 0; j < temp.size; j++)
                {
                    object v = null;
                    string s = temp[j];

                    if (string.IsNullOrEmpty(s) || fieldInfo[j] == null)
                    {
                        continue;
                    }
                    if (fieldInfo[j].FieldType.IsEnum)
                    {
                        try
                        {
                            v = Enum.Parse(fieldInfo[j].FieldType, s);
                        }
                        catch (Exception ex)
                        {
                            Debug.Log("error datafile=" + fileName + " fieldinfo=" + fieldInfo[j].Name + " fieldinfoValue=" + s);
                        }
                    }
                    else
                    {
                        try
                        {
                            v = Convert.ChangeType(s, fieldInfo[j].FieldType);
                        }
                        catch (Exception ex)
                        {
                            Debug.Log("error datafile=" + fileName + " fieldinfo=" + fieldInfo[j].Name + " fieldinfoValue=" + s + "  ID " + temp[0]);
                        }
                    }
                    fieldInfo[j].SetValue(t, v);
                }
                mDataList.Add(t);
            }
            temp = reader.ReadCSV();
        }
        return(mDataList);
    }
	/// <summary>
	/// Load the specified CSV file.
	/// </summary>

	static public bool LoadCSV (TextAsset asset)
	{
#if SHOW_REPORT
		mUsed.Clear();
#endif
		ByteReader reader = new ByteReader(asset);

		// The first line should contain "KEY", followed by languages.
		BetterList<string> temp = reader.ReadCSV();

		// There must be at least two columns in a valid CSV file
		if (temp.size < 2) return false;

		// The first entry must be 'KEY', capitalized
		temp[0] = "KEY";

#if !UNITY_3_5
		// Ensure that the first value is what we expect
		if (!string.Equals(temp[0], "KEY"))
		{
			Debug.LogError("Invalid localization CSV file. The first value is expected to be 'KEY', followed by language columns.\n" +
				"Instead found '" + temp[0] + "'", asset);
			return false;
		}
		else
#endif
		{
			knownLanguages = new string[temp.size - 1];
			for (int i = 0; i < knownLanguages.Length; ++i)
				knownLanguages[i] = temp[i + 1];
		}

		mDictionary.Clear();

		// Read the entire CSV file into memory
		while (temp != null)
		{
			AddCSV(temp);
			temp = reader.ReadCSV();
		}
		return true;
	}
Beispiel #25
0
    /// <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);
    }
    public static bool LoadCSV(byte[] bytes)
    {
        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);

        _langIndex = 0;
        _langs     = new string[header.size];
        for (int i = 0; i < header.size; i++)
        {
            _langs[i] = header[i];
        }

        _langMap  = new Dictionary <string, string[]>();
        _langKeys = new List <string>();
        for (; ;)
        {
            BetterList <string> temp = reader.ReadCSV();
            if (temp == null || temp.size == 0)
            {
                break;
            }

            string key = temp[0];
            if (string.IsNullOrEmpty(key))
            {
                continue;
            }

            var fields = new string[_langs.Length];
            for (int i = 1; i < temp.size; i++)
            {
                try
                {
                    fields[i - 1] = temp[i];
                }
                catch (Exception)
                {
                    Debug.LogErrorFormat("当前key解析出错:{0}", key, string.Join("|", temp.ToArray()));
                    throw;
                }
            }

            if (_langMap.ContainsKey(key))
            {
                Debug.LogErrorFormat("存在重复ID,请检查配置表:<{0}> 行号:{1}", key, _langKeys.Count + 2);
            }
            else
            {
                _langMap.Add(key, fields);
                _langKeys.Add(key);
            }
        }

        UIRoot.BroadcastMessage("OnLocalize", SendMessageOptions.DontRequireReceiver);
        Debug.LogFormat("Load Localization csv success! langs:{0} key:{1}", _langs.Length, _langMap.Count);
        return(true);
    }