예제 #1
0
        public IEnumerable <PapyrusScriptVariable> ParseParameterList(string input, out bool wasFinished)
        {
            wasFinished = true;
            var p    = new List <PapyrusScriptVariable>();
            var vars = new[] { input };

            if (input.Contains(','))
            {
                vars = input.Split(',');
            }
            foreach (var v in vars)
            {
                if (!v.Contains(" ") || v.TrimSplit(" ").Length == 1)
                {
                    wasFinished = false;
                    continue;
                }
                var param = new PapyrusScriptVariable();
                var d     = v.TrimSplit(" ");
                param.VarType = d[0];
                param.Name    = d[1];
                param.Name    = param.Name.Replace(")", "");
                if (d.Length > 2 && v.Contains("="))
                {
                    // Default values
                    param.InitialValue    = v.TrimSplit("=").LastOrDefault();
                    param.InitialValue    = param.InitialValue.Replace(")", "");
                    param.HasInitialValue = true;
                }
                p.Add(param);
            }
            return(p);
        }
예제 #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);
                }
            }
        }