Exemple #1
0
        public PapyrusAssemblyObject ParseScript(string file)
        {
            var obj = new PapyrusAssemblyObject();

            var scriptObject = Parse(file);

            var objName = scriptObject.Name;

            obj.Name = objName;

            foreach (var s in scriptObject.StateFunctions)
            {
                var state = new PapyrusAssemblyState();
                state.Name = s.Name;
                foreach (var f in s.Functions)
                {
                    var sourceFunction = f;

                    if (!string.IsNullOrEmpty(sourceFunction.StateName) && string.IsNullOrEmpty(state.Name))
                    {
                        state.Name = sourceFunction.StateName;
                    }

                    var function = new PapyrusAssemblyFunction();
                    function.Name        = functionNameResolver.Resolve(sourceFunction.Name);
                    function.ReturnType  = sourceFunction.ReturnType.VarType;
                    function.ReturnArray = sourceFunction.ReturnType.IsArray;
                    var inputNameIndex = 0;
                    foreach (var p in sourceFunction.Parameters)
                    {
                        function.Params.Add(new PapyrusAssemblyVariable(p.Name, p.VarType));

                        inputNameIndex++;
                    }

                    function.IsStatic = sourceFunction.IsGlobal;
                    function.IsNative = sourceFunction.IsNative;
                    function.IsEvent  = sourceFunction.IsEvent;
                    // Since we are only generating "dummy" classes,
                    // the function scope is not necessary at this point.
                    // var fs = sourceFunction.FunctionScope;

                    state.Functions.Add(function);
                }

                obj.States.Add(state);
            }

            // Ignore Non Overridable Functions for now.
            // We will need to handle these later to make sure that a user
            // does not override them. GetState, SetState are both common.
            //var nof = scriptObject.kNonOverridableFunctions;
            //var instanceVars = scriptObject.kInstanceVariables;
            //var propertyVars = scriptObject.kProperties;

            // If the script is extended by another script
            // The script will have a parent.
            if (scriptObject.Extends != null)
            {
                obj.ExtendsName = scriptObject.Extends;
            }

            foreach (var prop in scriptObject.Properties)
            {
                obj.PropertyTable.Add(new PapyrusAssemblyVariable(prop.Name, prop.VarType));
            }

            foreach (var field in scriptObject.InstanceVariables)
            {
                // Most likely an auto- property
                // And we have already added them above.
                if (field.Name.StartsWith("::"))
                {
                    continue;
                }
            }

            //      throw new NotImplementedException("Parsing from .psc/ Skyrim Papyrus Scripts are not yet supported.");

            //	TypeDefinition def = new TypeDefinition("","", TypeAttributes.);


            return(obj);
        }
Exemple #2
0
        public PapyrusAssemblyObject ParseAssembly(string file)
        {
            var inputScript          = File.ReadAllLines(file);
            var obj                  = new PapyrusAssemblyObject();
            var inVariableTable      = false;
            var inPropertyTable      = false;
            var inStateTable         = false;
            var inFunction           = false;
            var inFunctionLocalTable = false;
            var inFunctionParamTable = false;

            PapyrusAssemblyState    lastState    = null;
            PapyrusAssemblyFunction lastFunction = null;

            foreach (var line in inputScript)
            {
                var tLine = line.Replace("\t", "").Trim();
                if (tLine.Contains(";"))
                {
                    tLine = tLine.Split(';')[0].Trim();
                }

                if (tLine.StartsWith(".variableTable"))
                {
                    inVariableTable = true;
                }
                if (tLine.StartsWith(".endVariableTable"))
                {
                    inVariableTable = false;
                }

                if (tLine.StartsWith(".propertyTable"))
                {
                    inPropertyTable = true;
                }
                if (tLine.StartsWith(".endPropertyTable"))
                {
                    inPropertyTable = false;
                }

                if (tLine.StartsWith(".stateTable"))
                {
                    inStateTable = true;
                }
                if (tLine.StartsWith(".endStateTable"))
                {
                    inStateTable = false;
                }

                if (tLine.StartsWith(".paramTable"))
                {
                    inFunctionParamTable = true;
                }
                if (tLine.StartsWith(".endParamTable"))
                {
                    inFunctionParamTable = false;
                }

                if (tLine.StartsWith(".localTable"))
                {
                    inFunctionLocalTable = true;
                }
                if (tLine.StartsWith(".endLocalTable"))
                {
                    inFunctionLocalTable = false;
                }

                if (tLine.StartsWith(".object "))
                {
                    //			obj.Name = tLine.Split(' ')[1];
                    obj.Name = Path.GetFileNameWithoutExtension(file);


                    if (obj.Name.Contains("."))
                    {
                        obj.Name = obj.Name.Split('.')[0];
                    }

                    //string before = obj.Name;

                    obj.Name = assemblyNameResolver.Resolve(obj.Name);

                    //var theBefore = before;
                    //var theAfter = obj.Name;


                    if (tLine.Split(' ').Length > 2)
                    {
                        obj.ExtendsName = tLine.Split(' ')[2];
                    }
                    if (tLine.Contains("extends"))
                    {
                        obj.ExtendsName = tLine.Split(' ')[3];
                        // Parse(@"C:\The Elder Scrolls V Skyrim\Papyrus Compiler\" + tLine.Split(' ')[3] + ".disassemble.pas");
                    }
                }

                if (inVariableTable)
                {
                }
                else if (inPropertyTable)
                {
                }
                else if (inStateTable)
                {
                    if (tLine.StartsWith(".state") || tLine.StartsWith(".endState"))
                    {
                        if (tLine.StartsWith(".state"))
                        {
                            lastState = new PapyrusAssemblyState();
                        }
                        if (tLine.StartsWith(".endState"))
                        {
                            obj.States.Add(lastState);
                        }
                        continue;
                    }
                    if (tLine.StartsWith(".function "))
                    {
                        inFunction   = true;
                        lastFunction = new PapyrusAssemblyFunction();

                        if (tLine.Contains(" static"))
                        {
                            lastFunction.IsStatic = true;
                        }
                        lastFunction.Name = tLine.Split(' ')[1];
                    }
                    if (tLine.StartsWith(".endFunction") && inFunction)
                    {
                        inFunction = false;
                        lastState.Functions.Add(lastFunction);
                    }
                    if (inFunctionLocalTable && lastFunction != null)
                    {
                        if (tLine.StartsWith(".local "))
                        {
                            lastFunction.LocalTable.Add(new PapyrusAssemblyVariable(tLine.Split(' ')[1],
                                                                                    tLine.Split(' ')[2]));
                        }
                    }
                    if (inFunctionParamTable && lastFunction != null)
                    {
                        if (tLine.StartsWith(".param "))
                        {
                            lastFunction.Params.Add(new PapyrusAssemblyVariable(tLine.Split(' ')[1], tLine.Split(' ')[2]));
                        }
                    }
                    if (tLine.StartsWith(".return ") && lastFunction != null)
                    {
                        lastFunction.ReturnType = tLine.Split(' ')[1];
                    }
                }
            }
            return(obj);
        }