Пример #1
0
 public static void Save(BPComponent blueprint, FileInfo blueprintFile)
 {
     using (StreamWriter writer = new StreamWriter(blueprintFile.OpenWrite(), Encoding.UTF8))
     {
         SaveLuaValue(blueprint.Get(""), writer, "	");
     }
 }
Пример #2
0
    public static void Load(GameObject gameObject, FileInfo bpFile)
    {
        BPComponent bpComponent = gameObject.GetComponent <BPComponent>();

        using (StreamReader reader = new StreamReader(bpFile.OpenRead(), Encoding.UTF8))
        {
            Stack <TableBuilder> tableStack = new Stack <TableBuilder>();

            StringBuilder input = new StringBuilder();
            while (reader.Peek() >= 0)
            {
                char c = (char)reader.Read();
                input.Append(c);

                // Skip over whitespace.
                if (char.IsWhiteSpace(c))
                {
                    while (reader.Peek() >= 0)
                    {
                        char next = (char)reader.Peek();
                        if (char.IsWhiteSpace(next))
                        {
                            reader.Read();
                        }
                        else
                        {
                            break;
                        }
                    }
                }

                c = input[input.Length - 1];
                // Equals denotes the end of an identifier.
                if (c == '=')
                {
                    input.Remove(input.Length - 1, 1);
                    string id = input.ToString();
                    id = id.Trim();
                    input.Remove(0, input.Length);
                    if (id.Length > 0)
                    {
                        tableStack.Peek().PushID(id);
                    }
                }
                // Number signs and -- indicate a comment.
                else if (c == '#')
                {
                    input.Remove(input.Length - 1, 1);
                    reader.ReadLine();
                }
                else if (c == '-')
                {
                    if (reader.Peek() == '-')
                    {
                        input.Remove(input.Length - 1, 1);
                        reader.ReadLine();
                    }
                }
                // Quotes indicate the beginning of a string.
                else if (c == '\"')
                {
                    input.Remove(input.Length - 1, 1);
                    string val = ReadString(reader, '\"');
                    if (val == null)
                    {
                        Debug.LogError("String parse error.");
                        return;
                    }
                    TableBuilder builder = tableStack.Peek();
                    EncounteredString(builder.table, builder.PopID(), val);
                }
                else if (c == '\'')
                {
                    input.Remove(input.Length - 1, 1);
                    string val = ReadString(reader, '\'');
                    if (val == null)
                    {
                        Debug.LogError("String parse error.");
                        return;
                    }
                    TableBuilder builder = tableStack.Peek();
                    EncounteredString(builder.table, builder.PopID(), val);
                }
                // Curly brace indicates a table.
                else if (c == '{')
                {
                    input.Remove(input.Length - 1, 1);
                    string tableName = input.ToString();
                    tableName = tableName.Trim();
                    input.Remove(0, input.Length);
                    if (tableName.Length == 0)
                    {
                        tableName = null;
                    }

                    StartTable(tableStack, tableName);
                }
                else if (c == '}')
                {
                    input.Remove(input.Length - 1, 1);
                    LuaValue root = EndTable(tableStack);
                    if (root != null)
                    {
                        bpComponent.SetRoot(root);
                        break;
                    }
                }
                // Whitespace or comma denotes the end of a token.
                else if (char.IsWhiteSpace(c) || c == ',')
                {
                    input.Remove(input.Length - 1, 1);
                    string val = input.ToString();
                    val = val.Trim();
                    input.Remove(0, input.Length);

                    if (val.Length > 0)
                    {
                        if (val == "true")
                        {
                            TableBuilder builder = tableStack.Peek();
                            EncounteredBool(builder.table, builder.PopID(), true);
                        }
                        else if (val == "false")
                        {
                            TableBuilder builder = tableStack.Peek();
                            EncounteredBool(builder.table, builder.PopID(), false);
                        }
                        else if (val == "nil")
                        {
                            TableBuilder builder = tableStack.Peek();
                            EncounteredNil(builder.table, builder.PopID());
                        }
                        else
                        {
                            float f = 0.0f;
                            if (float.TryParse(val, out f))
                            {
                                TableBuilder builder = tableStack.Peek();
                                EncounteredFloat(builder.table, builder.PopID(), f);
                            }
                            else if (c != ',')
                            {
                                input.Append(val);
                            }
                            else
                            {
                                Debug.LogError("Token parse error: " + val + " " + c);
                                //input.Append(val);
                            }
                        }
                    }
                }
            }
        }
    }
Пример #3
0
    System.Collections.IEnumerator LoadCoroutine()
    {
        // Verify that the directory exists and grab its files.
        string        directoryPath = FAGameComponent.Instance.MakeAbsolutePath("/units/" + _unitID);
        DirectoryInfo directory     = new DirectoryInfo(directoryPath);

        if (!directory.Exists)
        {
            Debug.LogError(directoryPath + " does not exist.");
            yield break;
        }
        IEnumerable <FileInfo> directoryFiles = directory.GetFiles();

        // Load the BP, so we know what mesh, animations, and textures to load and what shaders to load.
        FileInfo blueprintFile = directoryFiles.FirstOrDefault(x => x.Name.EndsWith("_unit.bp", StringComparison.CurrentCultureIgnoreCase));

        if (blueprintFile == null || !blueprintFile.Exists)
        {
            Debug.LogError(directoryPath + " does not contain a unit blueprint.");
            yield break;
        }
        BPImporter.Load(gameObject, blueprintFile);
        _blueprint = GetComponent <BPComponent>();
        yield return(null);

        // Save the BP to test the BP exporter.
        FileInfo blueprintFileExport = new FileInfo(directoryPath + "/exportTest.bp");

        BPExporter.Save(_blueprint, blueprintFileExport);
        yield return(null);

        // Get the LOD0 files.
        LODFiles lod0 = GetLODFiles(0, directoryFiles);

        if (lod0.mesh == null)
        {
            Debug.LogError(directoryPath + " does not contain an LOD0.scm file.");
            yield break;
        }

        // Load the mesh.
        SCMImporter.Load(gameObject, lod0.mesh);

        // Apply the blueprint's scale.
        LuaValue scaleVal = _blueprint.Get("Display.UniformScale");
        float    scale    = (scaleVal == null || scaleVal.LVT != LuaValueType.LVT_Float) ? 1.0f : scaleVal.FloatValue;

        transform.root.localScale = new Vector3(scale, scale, scale);
        yield return(null);

        // Iteratively load the DDS files.
        for (DDSImporter.LoadStages i = DDSImporter.LoadStages.Init; i < DDSImporter.LoadStages.Count; ++i)
        {
            bool success = DDSImporter.LoadStage(i, gameObject, lod0);
            yield return(null);

            if (!success)
            {
                break;
            }
        }

        // Load the SCAs referenced in the blueprint.
        List <FileInfo> scaFiles = GetSCAFiles();

        foreach (FileInfo scaFile in scaFiles)
        {
            SCAImporter.Load(gameObject, scaFile);
            yield return(null);
        }
        Animation animComponent = gameObject.GetComponentInParent <Animation>();

        /*if (animComponent.GetClipCount() > 0)
         * {
         *      Debug.Log("Playing animation " + scaFiles[0].Name);
         *      animComponent.clip = animComponent.GetClip(scaFiles[0].Name);
         *      animComponent.Play(scaFiles[0].Name);
         * }*/
    }