Exemple #1
0
    public void ExecuteImport()
    {
        //create the folders
        foreach (C_RowData data in importData.rows)
        {
            string subFolder        = data.entries[importData.groupIndex];
            string importFolderPath = importFolder + "/" + subFolder;

            //test if group name is correct
            if (string.IsNullOrEmpty(subFolder))
            {
                importState = E_ImportState.Imported_Error;
                importInfo  = "'GroupName' can't be empty. Please reimport correct data set.";
                return;
            }

            //test if card name is correct
            if (string.IsNullOrEmpty(data.entries[importData.cardNameIndex]))
            {
                importState = E_ImportState.Imported_Error;
                importInfo  = "'CardName' can't be empty. Please reimport correct data set.";
                return;
            }

            if (importFolderPath.StartsWith(Application.dataPath))
            {
                data.importFolderPath = "Assets" + importFolderPath.Substring(Application.dataPath.Length);
                data.importFilePath   = data.importFolderPath + "/" + data.entries[importData.cardNameIndex] + ".prefab";
            }
            else
            {
                importInfo  = "The Path '" + importFolderPath + "' is not part of the actual project and can't be used.";
                importState = E_ImportState.Analyzed_Error;
                return;
            }

            //create directory if it doesn't exist yet
            if (!Directory.Exists(importFolderPath))
            {
                //if it doesn't exist, create it
                Directory.CreateDirectory(importFolderPath);
                Debug.Log("Create " + importFolderPath);
            }
        }

        //test if prefab exists
        foreach (C_RowData data in importData.rows)
        {
            string         styleName  = data.entries[importData.styleIndex];
            GameObject     cardPrefab = (GameObject)AssetDatabase.LoadAssetAtPath(data.importFilePath, typeof(GameObject));
            KingsCardStyle cardStyle  = styleDefinitions.GetStyle(styleName);

#if UNITY_2018_3_OR_NEWER
            //to get the nested prefabs working a slightly different approach is necessary
            //because actualization is done in between, calling a function would generate an ugly parameterlist
            //=> doing it in this place
            {
                GameObject instanceRoot = null;

                if (cardPrefab == null)
                {
                    if (cardStyle.prefab != null)
                    {
                        // make an instance of the prefab
                        instanceRoot = (GameObject)PrefabUtility.InstantiatePrefab(cardStyle.prefab);
                        Debug.Log("Creating card '" + data.entries[importData.cardNameIndex] + "' based on prefab '" + cardStyle.prefab.name + "' of the cardstyle '" + cardStyle.name + "'.");
                    }
                    else
                    {
                        Debug.LogError("Actualization/Creation of element failed. The prefab at the cardStyle '" + cardStyle.name + "' is 'null' (missing).");
                    }
                }
                else
                {
                    //an actual prefab exists
                    // make an instance of the already existing prefab
                    instanceRoot = (GameObject)PrefabUtility.InstantiatePrefab(cardPrefab);
                    Debug.Log("Actualizing the card prefab '" + cardPrefab.name + "', the linked prefab '" + cardStyle.prefab.name + "' of the cardstyle '" + cardStyle.name + "' is ignored because the prefab already exists.");
                }

                if (instanceRoot != null)
                {
                    // actualize the spawned object
                    ActualizeCard(instanceRoot, data, styleDefinitions, styleName);
                    // create prefab of the changed object
                    bool success = false;
                    UnityEngine.Object prefab = PrefabUtility.SaveAsPrefabAssetAndConnect(instanceRoot, data.importFilePath, InteractionMode.AutomatedAction, out success);

                    if (success == false)
                    {
                        Debug.LogError("The saving as prefab asset was unsuccessful.");
                    }

                    // destroy the spawned object
                    DestroyImmediate(instanceRoot);
                }
                else
                {
                    //some error messages if something failed with the instance Root
                    if (cardStyle.prefab == null)
                    {
                        Debug.LogError("Actualization/Creation of element failed. The prefab at the cardStyle is 'null' (missing).");
                    }
                    else
                    {
                        Debug.LogError("Actualization/Creation of prafab '" + cardStyle.prefab.name + "' failed.");
                    }
                }
            }
#else
            //import precedure for older unity versions

            //If the name doesn't exist, create the new Prefab
            if (cardPrefab == null)
            {
                cardPrefab = CreateNewCard(cardStyle.prefab, data.importFilePath);
            }

            ActualizeCard(cardPrefab, data, styleDefinitions, styleName);
#endif
        }

        importState = E_ImportState.Imported_OK;
        importInfo  = "Import done.\n" + DateTime.Now.ToShortTimeString();
    }
Exemple #2
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.";
    }