Exemplo n.º 1
0
        public TableDataBase()
        {
            var str = Resources.Load <TextAsset>(Path);

            if (str == null)
            {
                return;
            }
            using (var csv = new LW.CsvReader(new StringReader(str.text), true))
            {
                int           columnsCount = csv.FieldCount;
                List <string> fields       = new List <string>(columnsCount);
                fields.AddRange(csv.GetFieldHeaders());
                ReadHeaders(fields);
                while (csv.ReadNextRecord())
                {
                    fields.Clear();
                    for (int i = 0; i < columnsCount; ++i)
                    {
                        fields.Add(csv[i]);
                    }
                    ReadRow(fields);
                }
            }
        }
        static void ReadTable(string tableStr, LocalizationMap map)
        {
            using (var csv = new LW.CsvReader(new StringReader(tableStr), true, delimeter))
            {
                int columnsCount = csv.FieldCount;
                var headers      = csv.GetFieldHeaders();
                if (!headers.HasFieldNoBox("key", out int keyIndex))
                {
                    return;
                }

                Dictionary <int, LangCode> validKeys = new Dictionary <int, LangCode>(columnsCount);
                List <int> validKeysIds = new List <int>(columnsCount);
                for (int i = 0; i < columnsCount; ++i)
                {
                    if (i == keyIndex)
                    {
                        continue;
                    }
                    var codeStr = headers[i].ToLower();
                    if (!LocData.TryParseCode(codeStr, out var code))
                    {
                        continue;
                    }
                    validKeys[i] = code;
                    validKeysIds.Add(i);
                }

                int           keysCount = validKeysIds.Count;
                List <string> fields    = new List <string>(columnsCount);
                while (csv.ReadNextRecord())
                {
                    fields.Clear();
                    for (int i = 0; i < columnsCount; ++i)
                    {
                        fields.Add(csv[i]);
                    }
                    var trans = new Dictionary <LangCode, string>(keysCount);
                    for (int i = 0; i < keysCount; ++i)
                    {
                        int j = validKeysIds[i];
                        trans.Add(validKeys[j], fields[j]);
                    }
                    map.Add(fields[keyIndex], trans);
                }
            }
        }