Ejemplo n.º 1
0
    public void AnalyzeImportData()
    {
        importData = null;//This is intentional.

        //Open the text asset
        if (importFile == null)
        {
            importState = E_ImportState.Analyzed_Error;
            importInfo  = "File for importing is not defined.";
            return;
        }

        if (styleDefinitions == null)
        {
            importState = E_ImportState.Analyzed_Error;
            importInfo  = "Style file for importing is not defined.";
            return;
        }

        importData = new C_RawImportData();

        fs = getFS(); //use the configured fieldseparator

        mDebug("Fieldseparator: " + fs);
        mDebug("Input file: " + importFile.text);

        //Sometimes unity is not very nice, therefore using an text read workaround for importFile.text, if it is encoded with "illegal" characters:
        byte[] importBytes = importFile.bytes;
        for (int i = 0; i < importBytes.Length; i++)
        {
            if ((importBytes[i] & 0x80) > 0)
            {
                Debug.LogWarning("Import data could contain illegal character: 0x" + System.Convert.ToInt32(importBytes[i]).ToString("X") + ". Please check the resulting text and consider storing the import file in UTF8 encoding. If character is illegal is will be replaced by '�'. The corresponding row will be marked '�!', if no row was marked this was a false alarm.");
            }
        }
        string importText = Encoding.UTF8.GetString(importBytes);

        //split file correctly for \r\n and \n
        importLines = importText.Replace("\r\n", "\n").Split('\n');

        for (int i = 0; i < importLines.Length; i++)
        {
            mDebug("Line " + i.ToString() + ": '" + importLines[i] + "'");
        }

        importData.headers = importUnescape(importLines[0]);
        importData.targets = new string[importData.headers.Length];
        for (int i = 0; i < importData.headers.Length; i++)
        {
            //string[] hSplit;
            mDebug("importdata.headers " + i.ToString() + ": " + importData.headers[i]);

            if (importData.headers[i].Contains('.'))
            {
                importData.targets[i] = importData.headers[i].Split('.')[0];
                importData.headers[i] = importData.headers[i].Split('.')[1];
            }
        }

        for (int i = 1; i < importLines.Length; i++)
        {
            if (string.IsNullOrEmpty(importLines[i]) == false)
            {
                C_RowData col = new C_RowData();

                if (importLines[i].Contains('\uFFFD'))
                {
                    col.rowError = true;
                    col.rowInfo += "�! ";
                }

                col.entries = importUnescape(importLines[i]);
                importData.rows.Add(col);
            }
            else
            {
                Debug.Log("ImExCards: empty line removed.");
            }
        }

        //search through the import file and get the number of different styles
        //get the column
        importData.styleIndex    = -1;
        importData.groupIndex    = -1;
        importData.cardNameIndex = -1;
        for (int i = 0; i < importData.headers.Length; i++)
        {
            if (importData.headers[i] == "StyleName")
            {
                importData.styleIndex = i;
            }
            if (importData.headers[i] == "CardName")
            {
                importData.cardNameIndex = i;
            }
            if (importData.headers[i] == "GroupName")
            {
                importData.groupIndex = i;
            }

            mDebug("Header col. " + i.ToString() + ": " + importData.headers[i]);
        }



        //error if no found
        if (importData.styleIndex == -1 || importData.cardNameIndex == -1 || importData.groupIndex == -1)
        {
            importInfo = "";
            if (importData.styleIndex == -1)
            {
                importInfo += "Could not find 'StyleName' coloumn within the data.\n";
            }
            if (importData.cardNameIndex == -1)
            {
                importInfo += "Could not find 'CardName' coloumn within the data.\n";
            }
            if (importData.groupIndex == -1)
            {
                importInfo += "Could not find 'GroupName' coloumn within the data.\n";
            }

            importState = E_ImportState.Analyzed_Error;
            return;
        }

        //clean the group-and cardName of invalid characters
        foreach (C_RowData row in importData.rows)
        {
            string s = row.entries[importData.cardNameIndex];
            foreach (var c in Path.GetInvalidFileNameChars())
            {
                s = s.Replace(c, '-');
            }
            row.entries[importData.cardNameIndex] = s;

            s = row.entries[importData.groupIndex];
            foreach (var c in Path.GetInvalidFileNameChars())
            {
                s = s.Replace(c, '-');
            }
            row.entries[importData.groupIndex] = s;
        }


        //make a dictionary out of the styles
        Dictionary <string, string> importStyles = new Dictionary <string, string>();
        int nrOfStyles = 0;

        foreach (C_RowData row in importData.rows)
        {
            string key = row.entries[importData.styleIndex];
            if (!importStyles.ContainsKey(key))
            {
                importStyles.Add(key, "irrelevant");
                nrOfStyles++;
            }
        }

        //test each import row if card style exists
        string missingStyles         = "";
        int    numberOfMissingStyles = 0;
        int    rowCnt = 1;

        foreach (C_RowData row in importData.rows)
        {
            string style = row.entries[importData.styleIndex];
            if (styleDefinitions.HasStyle(style))
            {
                row.rowInfo += "Style OK.";
            }
            else
            {
                row.rowInfo += "Style Error.";
                row.rowError = true;

                if (!missingStyles.Contains(" " + style + ";"))
                {
                    numberOfMissingStyles++;
                    missingStyles += " " + style + ";";
                }
            }

            rowCnt++;
        }
        if (numberOfMissingStyles > 0)
        {
            importState = E_ImportState.Analyzed_Error;
            importInfo  = "Missing " + numberOfMissingStyles.ToString() + " card styles: " + missingStyles;
            return;
        }

        string styleDefinitionErrors = styleDefinitions.GetCardStyleDefinitionErrors();

        if (styleDefinitionErrors != "")
        {
            importInfo  = styleDefinitionErrors;
            importState = E_ImportState.Imported_Error;
            return;
        }

        importState = E_ImportState.Analyzed_OK;
        importInfo  = "OK.";
    }