private void ReadBuffer()
        {
            if (this.phase == ReadPhase.HasReachedEndOfInput)
            {
                throw new InvalidOperationException("Attempting to read buffer after end of input has been reached.");
            }

            char[] buffer = this.buffer;
            int    trailingCount;

            if (buffer == null)
            {
                // Create buffer for reading input.
                buffer        = this.buffer = new char[2048];
                trailingCount = 0;
            }
            else
            {
                // Move any trailing unused characters to start of buffer.
                trailingCount = this.bufferSize - this.bufferPos;
                for (int i = 0; i < trailingCount; ++i)
                {
                    buffer[i] = buffer[this.bufferPos + i];
                }

                // Zero-out any excess characters.
                if (this.phase == ReadPhase.HasReachedEndOfStream)
                {
                    for (int i = trailingCount; i < buffer.Length; ++i)
                    {
                        buffer[i] = default(char);
                    }

                    this.phase = ReadPhase.HasReachedEndOfInput;
                }
            }

            if (this.phase == ReadPhase.ReadingStream)
            {
                this.bufferSize = trailingCount + this.jsonReader.ReadBlock(buffer, trailingCount, buffer.Length - trailingCount);

                // Has end of input stream been reached?
                if (this.bufferSize < buffer.Length)
                {
                    this.phase = ReadPhase.HasReachedEndOfStream;

                    // Zero-out any excess characters.
                    for (int i = this.bufferSize; i < buffer.Length; ++i)
                    {
                        buffer[i] = default(char);
                    }
                }
            }

            if (!this.initLineEnding)
            {
                // Use either '\r' or '\n' to quickly detect line endings for maintaining
                // line number and position. Initially we do not know which type of line
                // ending characters are being used. If a file contains mixed styles then
                // line number and position feedback will be inaccurate when syntax
                // errors are encountered.
                for (int i = 0; i < buffer.Length - 1; ++i)
                {
                    char c = buffer[i];
                    if (c == '\r' || c == '\n')
                    {
                        this.initLineEnding = true;
                        this.lineEnding     = c;
                        if (c == '\r' && buffer[i + 1] == '\n')
                        {
                            this.lineEndingLength = 2;
                        }
                        break;
                    }
                }
            }

            this.bufferPos = 0;
        }
Esempio n. 2
0
        //めんどいからある程度ファイルフォーマット決め打ち
        public static ItemBuildData ReadBuildFile(string filePath)
        {
            ItemBuildData data = new ItemBuildData();

            try{
                ReadPhase        phase          = ReadPhase.Header;
                ItemBuildSection currentSection = null;

                foreach (string temp in File.ReadAllLines(filePath))
                {
                    string line = temp.Trim();
                    if (line.StartsWith(COMMENT) || line == "")
                    {
                        continue;   //コメント行
                    }

                    //Console.WriteLine(line);

                    switch (phase)
                    {
                    case ReadPhase.Header:
                        data.FileHeader = line;
                        phase           = ReadPhase.HeaderAfter;
                        break;

                    case ReadPhase.HeaderAfter:
                        if (line == "{")
                        {
                            phase = ReadPhase.ItemsHeaderSection;
                        }
                        break;

                    case ReadPhase.ItemsHeaderSection:
                        if (line == "{")
                        {
                            phase = ReadPhase.SectionHeader;
                        }
                        else if (line.ToLower() != "items")
                        {
                            data.AddItemsHeader(line);
                        }
                        break;

                    case ReadPhase.SectionHeader:
                        if (line == "}")
                        {
                            //アイテムセクションの終了
                            phase = ReadPhase.Finalize;
                            continue;
                        }

                        currentSection = new ItemBuildSection();
                        currentSection.SectionLabel = line.Replace("\"", "");       //"外して渡す
                        phase = ReadPhase.SectionContent;
                        break;

                    case ReadPhase.SectionContent:
                        if (line == "{")
                        {
                            continue;       //Section開始括弧 次
                        }

                        //セクション終了 データへ登録し次のヘッダ探しへ
                        if (line == "}")
                        {
                            data.Sections.Add(currentSection);
                            phase = ReadPhase.SectionHeader;
                            continue;
                        }

                        ItemData item = ItemData.FromItemBuildFileLine(line);
                        if (item != null)
                        {
                            currentSection.Items.Add(item);
                        }
                        break;

                    case ReadPhase.Finalize:
                        //特になんもしない
                        break;
                    }
                }
                return(data);
            }
            catch {
                MessageBox.Show("読み込み失敗。", "エラー");
            }
            return(null);
        }
        //#if UNITY_ANDROID || UNITY_EDITOR
        //        string fullpath = resourceLanguagesFiles + file + ".po";
        //        TextAsset textAsset = Resources.Load<TextAsset>(resourceLanguagesFiles + "pt_BR.po");
        //        Debug.Log(textAsset);
        #region NOT CHANGE
        private void LoadFile(Languages lang)
        {
            ReadPhase phase = ReadPhase.None;

            if (lang == defaultLang)
            {
                //if (textTable != null)
                textTable = new Dictionary <string, string[]>();
                textTable.Clear();
                language = defaultLang;
                phase    = ReadPhase.None;
                OnChangeLanguage(this);
                initialized = true;
                return;
            }
            if (Array.IndexOf <Languages>(avariableLanguages, lang) >= 0 && lang != defaultLang)
            {
                //possui o arquivo, e não é a linguagem padrão então TRADUZIR
                initialized = false;// INDICA que não completou a tradução
#if UNITY_ANDROID || UNITY_IOS
                string file      = lang.ToString().Replace("-", "_") + ".po";
                string textAsset = Resources.Load <TextAsset>(resourceLanguagesFiles + file).text;
#else
                string fullpath  = GetPoFile(lang);
                string textAsset = File.ReadAllText(fullpath, System.Text.Encoding.UTF8);
#endif

                //Debug.Log(fullpath);

                //Debug.Log(textAsset);
                if (textAsset != null)
                {
                    //Debug.LogWarning("[LanguageManager] loading: " + fullpath);
                    if (textTable == null)
                    {
                        textTable = new Dictionary <string, string[]>();
                    }
                    textTable.Clear();
                    StringReader reader = new StringReader(textAsset);
                    string       key    = null;
                    string       plural = null;
                    string       val    = null;
                    int          number = 0;
                    string[]     valp   = new string[1];
                    string       line;
                    while ((line = reader.ReadLine()) != null)
                    {
                        //Debug.Log("Linha: " + line);
                        if (line.StartsWith("\"Plural-Forms: nplurals="))
                        {
                            nplural = System.Int32.Parse(line.Substring(line.IndexOf("nplurals=") + 9, line.IndexOf(";") - (line.IndexOf("nplurals=") + 9)));
                            fplural = line.Substring(line.IndexOf("plural=") + 7, line.IndexOf(";", line.IndexOf("plural=")) - (line.IndexOf("plural=") + 7));
                        }
                        else if (line.StartsWith("\""))
                        {
                            // é continuação do texto
                            //Debug.Log("Continuação: " + line);
                            if (phase != ReadPhase.None)
                            {
                                switch (phase)
                                {
                                case ReadPhase.Singular:
                                    key += line.Substring(1, line.Length - 2);
                                    break;

                                case ReadPhase.Plural:
                                    plural += line.Substring(1, line.Length - 2);
                                    break;

                                case ReadPhase.TSingular:
                                    val += line.Substring(1, line.Length - 2);
                                    break;

                                case ReadPhase.TPlural:
                                    valp[number] += line.Substring(1, line.Length - 2);
                                    break;

                                default:
                                    phase = ReadPhase.None;
                                    break;
                                }
                            }
                        }
                        else if (line.StartsWith("msgid \""))
                        {
                            phase = ReadPhase.Singular;
                            key   = line.Substring(7, line.Length - 8);
                            //Debug.Log("Key: "+key);
                        }
                        else if (line.StartsWith("msgid_plural \""))
                        {
                            phase  = ReadPhase.Plural;
                            plural = line.Substring(14, line.Length - 15);
                            //Debug.Log("Achei o plural: "+plural);
                        }
                        else if (line.StartsWith("msgstr \""))
                        {
                            phase = ReadPhase.TSingular;
                            val   = line.Substring(8, line.Length - 9);
                            //Debug.Log("Valor: "+val);
                        }
                        else if (line.StartsWith("msgstr["))
                        {
                            phase = ReadPhase.TPlural;
                            string sub = line.Substring(7, 1);
                            if (System.Int32.TryParse(sub.Trim(), out number))
                            {
                                if (valp == null)
                                {
                                    //Debug.Log("Valp é nulo: "+ nplural);
                                    valp = new string[nplural];
                                    //Debug.Log("guarda no numero: " + number);
                                    valp[number] = line.Substring(line.IndexOf("\"") + 1, line.Length - (line.IndexOf("\"") + 2));
                                    // Debug.Log("No numero: " + number + " tem o valor: "+ valp[number]);
                                }
                                else
                                {
                                    // Debug.Log("Valp NAO nulo");
                                    valp[number] = line.Substring(line.IndexOf("\"") + 1, line.Length - (line.IndexOf("\"") + 2));
                                    // Debug.Log("No numero: " + number + " tem o valor: " + valp[number]);
                                }
                            }
                        }
                        else
                        {
                            AddLocation(key, val, plural, valp);
                            plural = key = val = null;
                            valp   = null;
                        }
                    }
                    AddLocation(key, val, plural, valp);
                    reader.Close();
                }
                else
                {
                    Debug.LogWarning("[LanguageManager] " + lang.ToString() + " file not found.");
                    return;
                }
                initialized = true;
            }
        }