Esempio n. 1
0
        public PapyrusScriptObject Parse(string file)
        {
            var output = new PapyrusScriptObject();

            output.Name = Path.GetFileNameWithoutExtension(file);

            var source = File.ReadAllText(file);


            while (source.Contains(";/") && source.Contains("/;"))
            {
                var start = source.IndexOf(";/");
                var end   = source.IndexOf("/;");
                source = source.Remove(start, 1 + end - start);
            }

            while (source.Contains('{') && source.Contains('}'))
            {
                var start = source.IndexOf('{');
                var end   = source.IndexOf('}');
                source = source.Remove(start, 1 + end - start);
            }

            source = source.Replace('\t', ' ').Trim();

            var sourceLines = source.Split('\n');

            ParseScript(ref output, sourceLines);

            return(output);
        }
Esempio n. 2
0
        public void ParseScript(ref PapyrusScriptObject output, string[] sourceLines)
        {
            var insideFunction = false;
            var insideState    = false;

            PapyrusScriptFunction lastFunction = null;

            var lastState = new PapyrusScriptStateFunction();

            output.StateFunctions.Add(lastState);

            var lastParametersFinished = true;

            for (var i = 0; i < sourceLines.Length; i++)
            {
                sourceLines[i] = sourceLines[i].Trim();
                if (sourceLines[i].StartsWith(";") || string.IsNullOrEmpty(sourceLines[i]) || sourceLines[i].Length < 3)
                {
                    continue;
                }

                if (sourceLines[i].Contains(";"))
                {
                    sourceLines[i] = sourceLines[i].Remove(sourceLines[i].IndexOf(';'));
                }

                var l                = sourceLines[i].ToLower();
                var u                = sourceLines[i];
                var dataSplits       = l.TrimSplit(" ");
                var dataSplitsNormal = u.TrimSplit(" ");

                // We won't handle EndProperty just yet.
                // Since we are not exposing any properties to the DLL atm
                if (l.StartsWith("endproperty"))
                {
                    continue;
                }

                if (dataSplits.Contains("import"))
                {
                    output.Imports.Add(dataSplits[1]);
                    continue;
                }

                if (l.StartsWith("scriptname "))
                {
                    var data = sourceLines[i].TrimSplit(" ");

                    if (data.Contains("extends"))
                    {
                        var e = data.IndexOf("extends");
                        output.Extends = data[e + 1];
                    }

                    output.Name          = data[data.IndexOf("scriptname") + 1];
                    output.IsConditional = data.Contains("conditional");
                    output.IsHidden      = data.Contains("hidden");
                    continue;
                }

                if ((l.StartsWith("state ") || l.StartsWith("auto state ")) && !insideFunction)
                {
                    var values = l.TrimSplit(" ");
                    lastState = new PapyrusScriptStateFunction();
                    if (values.Contains("auto"))
                    {
                        lastState.IsAuto = true;
                        lastState.Name   = values[2].Trim();
                    }
                    else
                    {
                        lastState.Name = values[1];
                    }
                    output.StateFunctions.Add(lastState);

                    insideState = true;
                    continue;
                }

                if (l.StartsWith("endstate") && insideState)
                {
                    insideState = false;
                    continue;
                }

                if (!lastParametersFinished && insideFunction)
                {
                    var param = ParseParameterList(u, out lastParametersFinished);

                    lastFunction.Parameters.AddRange(param);

                    if (dataSplits.Contains("global"))
                    {
                        lastFunction.IsGlobal = true;
                    }

                    if (dataSplits.Contains("native"))
                    {
                        lastState.Functions.Add(lastFunction);
                        insideFunction = false;
                    }

                    continue;
                }

                if (l.StartsWith("event ") || l.Contains(" event ") || l.StartsWith("function ") ||
                    l.Contains(" function ") && l.Contains("(") && l.Contains(")"))
                {
                    lastFunction            = new PapyrusScriptFunction();
                    lastFunction.ReturnType = new PapyrusScriptVariable
                    {
                        VarType = "void"
                    };

                    var parameters = GetParameters(sourceLines[i], out lastParametersFinished);

                    if (l.Contains("function"))
                    {
                        var fi = dataSplitsNormal.IndexOf("function");
                        lastFunction.Name = dataSplitsNormal[fi + 1].Split('(').FirstOrDefault();
                        if (fi > 0)
                        {
                            var val = dataSplitsNormal.FirstOrDefault();
                            lastFunction.ReturnType = new PapyrusScriptVariable
                            {
                                VarType = val,
                                IsArray = val.Contains("[]")
                            };
                        }
                    }
                    else if (l.Contains("event"))
                    {
                        lastFunction.IsEvent = true;
                        lastFunction.Name    =
                            dataSplitsNormal[dataSplitsNormal.IndexOf("event") + 1].Split('(').FirstOrDefault();
                    }

                    if (dataSplits.Contains("global"))
                    {
                        lastFunction.IsGlobal = true;
                    }

                    lastFunction.Parameters.AddRange(parameters);

                    insideFunction = true;
                    if (dataSplits.Contains("native"))
                    {
                        lastFunction.IsNative = true;

                        lastState.Functions.Add(lastFunction);
                        insideFunction = false;
                    }
                    continue;
                }

                if (insideFunction && (l.StartsWith("endfunction") || l.StartsWith("endevent")))
                {
                    lastState.Functions.Add(lastFunction);
                    insideFunction = false;
                    continue;
                }


                if (!insideFunction)
                {
                    var prop = new PapyrusScriptVariable();
                    prop.VarType       = dataSplits[0];
                    prop.IsArray       = prop.VarType.Contains("[]");
                    prop.IsConditional = dataSplits.Contains("conditional");
                    prop.IsProperty    = dataSplits.Contains("property");
                    prop.IsAuto        = dataSplits.Contains("auto");
                    prop.Name          = prop.IsProperty ? dataSplitsNormal[2] : dataSplitsNormal[1];

                    if (dataSplits.Contains("="))
                    {
                        prop.InitialValue    = dataSplitsNormal.LastOrDefault().Trim();
                        prop.HasInitialValue = true;
                    }

                    if (prop.Name.Contains("="))
                    {
                        var val = prop.Name.TrimSplit("=");
                        prop.Name            = val.FirstOrDefault();
                        prop.InitialValue    = val.LastOrDefault();
                        prop.HasInitialValue = true;
                    }

                    output.Properties.Add(prop);
                }
            }
        }