예제 #1
0
        private static void DumpPackage(string packageName, string gameDir, string outDir, bool scriptSource)
        {
            using (Stream s = new FileStream(packageName, FileMode.Open, FileAccess.Read, FileShare.Read))
            {
                var package = UE3PackageReader.ReadPackage(new BinaryReader(s, Encoding.ASCII), gameDir);
                foreach (var export in package.TopLevelExports)
                {
                    if (export.ClassName == "Class")
                    {
                        Directory.CreateDirectory(outDir);
                        if (scriptSource)
                        {
                            var scriptText = export.Children.SingleOrDefault(e => e.ObjectName == "ScriptText");
                            if (scriptText != null)
                            {
                                var text = (string) scriptText.ReadInstance();
                                DumpOutput(export, outDir, text);
                                continue;
                            }
                        }
                        var instance = (UnClass)export.ReadInstance();
                        var builder = new TextBuilder();
                        instance.Decompile(builder);
                        DumpOutput(export, outDir, builder.ToString());
                    }
                }

                ProblemRegistry.LogProblems(Path.Combine(outDir, "unhood.log"));
            }
        }
예제 #2
0
        public override void Decompile(TextBuilder result)
        {
            result.Indent().Append("state ").Append(_self.ObjectName).NewLine();
            result.Indent().Append("{").NewLine();
            result.PushIndent();
            DecompileChildren(result, false);
            result.PopIndent();

            StatementList bytecode = ReadBytecode();
            DecompileBytecode(bytecode, result, true);

            result.Indent().Append("}").NewLine().NewLine();
        }
예제 #3
0
 public override void Decompile(TextBuilder result)
 {
     result.Indent().Append("struct ");
     if (Native) result.Append("native ");
     if (Transient) result.Append("transient ");
     result.Append(_self.ObjectName);
     if (_super != null)
     {
         result.Append(" extends ").Append(_super.ObjectName);
     }
     result.NewLine();
     result.Append("{").NewLine();
     result.PushIndent();
     DecompileChildren(result, true);
     result.PopIndent();
     result.Indent().Append("}").NewLine().NewLine();
 }
예제 #4
0
 public virtual void Decompile(TextBuilder result)
 {
     result.Indent();
     result.Append("var");
     if (_flags.HasFlag("Edit"))
     {
         result.Append("(");
         if (_category != null && _category.Name != _export.Parent.ObjectName)
         {
             result.Append(_category.Name);
         }
         result.Append(")");
     }
     _flags.Except("Edit", "Net").Each(f => result.Append(" " + f.ToLower()));
     result.Append(" ").Append(GetPropertyType()).Append(" ");
     result.Append(_export.ObjectName);
     if (_arraySize != 1)
     {
         result.Append("[").Append(_arraySize).Append("]");
     }
     result.Append(";\n\n");
 }
예제 #5
0
        public override void Decompile(TextBuilder result)
        {
            result.Append("class ").Append(_self.ObjectName);
            if (_super != null)
            {
                result.Append(" extends ").Append(_super.ObjectName);
            }
            if (_outerInstance != null && _outerInstance.ObjectName != "Object")
            {
                result.NewLine().Append("    within ").Append(_outerInstance.ObjectName);
            }
            if (_hideCategories.Count > 0)
            {
                result.NewLine().Append("    hidecategories(").Append(string.Join(",", _hideCategories.ToArray())).Append(")");
            }
            if (_interfaces.Count > 0)
            {
                var intfNames = _interfaces.ConvertAll(e => e.ObjectName).ToArray();
                result.NewLine().Append("    implements(").Append(string.Join(",", intfNames)).Append(")");
            }
            if (_config != "None")
            {
                result.NewLine().Append("    config(").Append(_config).Append(")");
            }
            _flags.Except("Compiled", "Parsed", "Config", "Localized").Each(f => result.NewLine().Append("    ").Append(f.ToLower()));
            result.Append(";").NewLine().NewLine();
            DecompileChildren(result, false);

            var statementList = ReadBytecode();

            if (statementList.Count > 0)
            {
                DecompileReplicationBlock(result, statementList);
            }
            if (_defaults != null)
            {
                DecompileDefaultProperties(result);
            }
        }
예제 #6
0
 public override void Decompile(TextBuilder result)
 {
     result.Indent().Append("struct ");
     if (Native)
     {
         result.Append("native ");
     }
     if (Transient)
     {
         result.Append("transient ");
     }
     result.Append(_self.ObjectName);
     if (_super != null)
     {
         result.Append(" extends ").Append(_super.ObjectName);
     }
     result.NewLine();
     result.Append("{").NewLine();
     result.PushIndent();
     DecompileChildren(result, true);
     result.PopIndent();
     result.Indent().Append("}").NewLine().NewLine();
 }
예제 #7
0
 private void DecompileReplicationBlock(TextBuilder result, StatementList statementList)
 {
     result.Append("replication\n{\n").PushIndent();
     for (int i = 0; i < statementList.Count; i++)
     {
         List <String> names = FindReplicatedProperties(statementList [i].StartOffset);
         if (names.Count > 0)
         {
             result.Indent().Append("if (").Append(statementList[i].Token.ToString()).Append(")").NewLine();
             result.Indent().Append("    ");
             foreach (string name in names)
             {
                 result.Append(name);
                 if (name != names.Last())
                 {
                     result.Append(", ");
                 }
             }
             result.Append(";").NewLine().NewLine();
         }
     }
     result.Append("}").NewLine().NewLine().PopIndent();
 }
예제 #8
0
 protected void DecompileBytecode(StatementList statements, TextBuilder result, bool createControlStatements)
 {
     var labelTableStatement = statements.Find(s => s.Token is LabelTableToken);
     var labelTable = labelTableStatement != null ? (LabelTableToken) labelTableStatement.Token : null;
     result.HasErrors = statements.HasErrors();
     if (createControlStatements)
     {
         try
         {
             statements.CreateControlStatements();
         }
         catch (Exception)
         {
             ProblemRegistry.RegisterIncompleteControlFlow(this);
         }
         if (statements.IsIncompleteControlFlow())
         {
             ProblemRegistry.RegisterIncompleteControlFlow(this);
         }
         statements.RemoveRedundantReturns();
     }
     statements.Print(result, labelTable, !createControlStatements);
 }
예제 #9
0
        protected void DecompileBytecode(StatementList statements, TextBuilder result, bool createControlStatements)
        {
            var labelTableStatement = statements.Find(s => s.Token is LabelTableToken);
            var labelTable          = labelTableStatement != null ? (LabelTableToken)labelTableStatement.Token : null;

            result.HasErrors = statements.HasErrors();
            if (createControlStatements)
            {
                try
                {
                    statements.CreateControlStatements();
                }
                catch (Exception)
                {
                    ProblemRegistry.RegisterIncompleteControlFlow(this);
                }
                if (statements.IsIncompleteControlFlow())
                {
                    ProblemRegistry.RegisterIncompleteControlFlow(this);
                }
                statements.RemoveRedundantReturns();
            }
            statements.Print(result, labelTable, !createControlStatements);
        }
예제 #10
0
        protected void DecompileChildren(TextBuilder result, bool reverse)
        {
            var collection = reverse ? _self.Children.Reverse() : _self.Children;

            foreach (UnExport export in collection)
            {
                try
                {
                    object instance = export.ReadInstance();
                    if (instance is Decompilable)
                    {
                        ((Decompilable)instance).Decompile(result);
                    }
                    else
                    {
                        result.Append("// ").Append(export.ToString()).Append("\n");
                    }
                }
                catch (Exception e)
                {
                    result.Append("//!!! Error decompiling " + export.ObjectName + ": " + e.Message);
                }
            }
        }
예제 #11
0
 public override void Print(TextBuilder result, LabelTableToken labelTable, bool offset)
 {
     result.Indent().Append(_text).Append(";").NewLine();
 }
예제 #12
0
 protected abstract void PrintHead(TextBuilder result);
예제 #13
0
 public void Print(TextBuilder result, LabelTableToken labelTable, bool showStartOffset)
 {
     result.PushIndent();
     _statements.ForEach(s => s.Print(result, labelTable, showStartOffset));
     result.PopIndent();
 }
예제 #14
0
 public virtual void Decompile(TextBuilder result)
 {
     result.Indent();
     result.Append("var");
     if (_flags.HasFlag("Edit"))
     {
         result.Append("(");
         if (_category != null && _category.Name != _export.Parent.ObjectName)
             result.Append(_category.Name);
         result.Append(")");
     }
     _flags.Except("Edit", "Net").Each(f => result.Append(" " + f.ToLower()));
     result.Append(" ").Append(GetPropertyType()).Append(" ");
     result.Append(_export.ObjectName);
     if (_arraySize != 1)
         result.Append("[").Append(_arraySize).Append("]");
     result.Append(";\n\n");
 }
예제 #15
0
 protected abstract void PrintHead(TextBuilder result);
예제 #16
0
 protected override void PrintHead(TextBuilder result)
 {
     result.Append("switch (").Append(Token.ToString()).Append(")");
 }
예제 #17
0
 public abstract void Decompile(TextBuilder result);
예제 #18
0
 protected override void PrintHead(TextBuilder result)
 {
     var foreachToken = (ForeachToken)Token;
     result.Append("foreach ").Append(foreachToken.Expr.ToString());
     if (foreachToken.IteratorExpr != null)
     {
         result.Append("(").Append(foreachToken.IteratorExpr.ToString()).Append(")");
     }
 }
예제 #19
0
 private void DecompileReplicationBlock(TextBuilder result, StatementList statementList)
 {
     result.Append("replication\n{\n").PushIndent();
     for(int i=0; i<statementList.Count; i++)
     {
         List<String> names = FindReplicatedProperties(statementList [i].StartOffset);
         if (names.Count > 0)
         {
             result.Indent().Append("if (").Append(statementList[i].Token.ToString()).Append(")").NewLine();
             result.Indent().Append("    ");
             foreach (string name in names)
             {
                 result.Append(name);
                 if (name != names.Last()) result.Append(", ");
             }
             result.Append(";").NewLine().NewLine();
         }
     }
     result.Append("}").NewLine().NewLine().PopIndent();
 }
예제 #20
0
 protected void DecompileChildren(TextBuilder result, bool reverse)
 {
     var collection = reverse ? _self.Children.Reverse() : _self.Children;
     foreach (UnExport export in collection)
     {
         try
         {
             object instance = export.ReadInstance();
             if (instance is Decompilable)
             {
                 ((Decompilable)instance).Decompile(result);
             }
             else
             {
                 result.Append("// ").Append(export.ToString()).Append("\n");
             }
         }
         catch (Exception e)
         {
             result.Append("//!!! Error decompiling " + export.ObjectName + ": " + e.Message);
         }
     }
 }
예제 #21
0
 private void DecompileDefaultProperties(TextBuilder result)
 {
     result.Append("defaultproperties\n{\n").PushIndent();
     var defaultsExport = _defaults.Resolve();
     UnPropertyList propertyList = Package.ReadPropertyList(defaultsExport, this);
     foreach(UnProperty prop in propertyList.Properties)
     {
         var name = prop.Name;
         if (name.StartsWith("__") && name.EndsWith("__Delegate"))
         {
             name = name.Substring(2, name.Length - 2 - 10);
         }
         if (prop.Value is UnPropertyArray)
         {
             var array = (UnPropertyArray) prop.Value;
             for(int i=0; i<array.Count; i++)
             {
                 result.Indent().Append(name).Append("(").Append(i).Append(")=")
                     .Append(ValueToString(array [i], array.ElementType)).NewLine();
             }
         }
         else
         {
             result.Indent().Append(name).Append("=").Append(ValueToString(prop.Value, prop.Type)).NewLine();
         }
     }
     foreach(UnExport export in defaultsExport.Children)
     {
         result.Indent().Append("// child object " + export.ObjectName + " of type " + export.ClassName).NewLine();
     }
     result.Append("}").NewLine().PopIndent();
 }
예제 #22
0
        public override void Decompile(TextBuilder result)
        {
            result.Append("class ").Append(_self.ObjectName);
            if (_super != null)
                result.Append(" extends ").Append(_super.ObjectName);
            if (_outerInstance != null && _outerInstance.ObjectName != "Object")
            {
                result.NewLine().Append("    within ").Append(_outerInstance.ObjectName);
            }
            if (_hideCategories.Count > 0)
            {
                result.NewLine().Append("    hidecategories(").Append(string.Join(",", _hideCategories.ToArray())).Append(")");
            }
            if (_interfaces.Count > 0)
            {
                var intfNames = _interfaces.ConvertAll(e => e.ObjectName).ToArray();
                result.NewLine().Append("    implements(").Append(string.Join(",", intfNames)).Append(")");
            }
            if (_config != "None")
            {
                result.NewLine().Append("    config(").Append(_config).Append(")");
            }
            _flags.Except("Compiled", "Parsed", "Config", "Localized").Each(f => result.NewLine().Append("    ").Append(f.ToLower()));
            result.Append(";").NewLine().NewLine();
            DecompileChildren(result, false);

            var statementList = ReadBytecode();
            if (statementList.Count > 0)
            {
                DecompileReplicationBlock(result, statementList);
            }
            if (_defaults != null)
            {
                DecompileDefaultProperties(result);
            }
        }
예제 #23
0
 public override void Print(TextBuilder result, LabelTableToken labelTable, bool offset)
 {
     result.Indent().Append(_text).Append(";").NewLine();
 }
예제 #24
0
 public override void Print(TextBuilder result, LabelTableToken labelTable, bool showStartOffsets)
 {
     PrintLabel(labelTable, result);
     result.Indent();
     PrintHead(result);
     result.Append("\n");
     result.Indent().Append("{\n");
     PrintChildren(result, labelTable, showStartOffsets);
     result.Indent().Append("}");
     //            result.Append("   // ").Append(_endOffset.ToString());
     result.NewLine();
 }
예제 #25
0
 protected override void PrintHead(TextBuilder result)
 {
     result.Append("while (").Append(Token.ToString()).Append(")");
 }
예제 #26
0
 protected virtual void PrintChildren(TextBuilder result, LabelTableToken labelTable, bool showStartOffsets)
 {
     _children.Print(result, labelTable, showStartOffsets);
 }
예제 #27
0
 protected void PrintLabel(LabelTableToken labelTable, TextBuilder result)
 {
     if (labelTable != null)
     {
         var label = labelTable.GetLabel(StartOffset);
         if (label != null)
         {
             result.Append(label).Append(":").NewLine();
         }
     }
 }
예제 #28
0
 protected override void PrintHead(TextBuilder result)
 {
     result.Append("else");
 }
예제 #29
0
 protected override void PrintHead(TextBuilder result)
 {
     result.Append("else");
 }
예제 #30
0
 protected override void PrintChildren(TextBuilder result, LabelTableToken labelTable, bool showStartOffsets)
 {
     result.PushIndent();
     bool indented = false;
     for (int i = 0; i < Children.Count; i++)
     {
         var stmt = Children[i];
         if (stmt.Token is CaseToken || stmt.Token is DefaultToken)
         {
             if (indented)
             {
                 result.PopIndent();
                 indented = false;
             }
             result.Indent().Append(stmt.Token.ToString()).Append(":").NewLine();
         }
         else
         {
             if (!indented)
             {
                 indented = true;
                 result.PushIndent();
             }
             stmt.Print(result, labelTable, showStartOffsets);
         }
     }
     if (indented) result.PopIndent();
     result.PopIndent();
 }
예제 #31
0
파일: UnConst.cs 프로젝트: yole/unhood
 public void Decompile(TextBuilder result)
 {
     result.Append("const ").Append(_export.ObjectName).Append(" = ").Append(_value).Append(";").NewLine();
 }
예제 #32
0
 public virtual void Print(TextBuilder result, LabelTableToken labelTable, bool showStartOffset)
 {
     if (Token.ToString() == "") return;
     PrintLabel(labelTable, result);
     result.Indent();
     if (showStartOffset)
     {
         result.Append("/* ").Append(StartOffset).Append(" */ ");
     }
     result.Append(Token.ToString(), StartOffset, EndOffset).Append(";");
     result.Append("\n");
 }
예제 #33
0
 public void Decompile(TextBuilder result)
 {
     result.Append("enum ").Append(_export.ObjectName).Append("\n{\n");
     _values.GetRange(0, _values.Count - 1).ForEach(v => result.Append("    ").Append(v).Append(",\n"));
     result.Append("};\n\n");
 }
예제 #34
0
        public void Decompile(TextBuilder result, bool createControlStatements)
        {
            result.Indent();
            if (Native)
            {
                result.Append("native");
                if (_nativeIndex > 0)
                    result.Append("(").Append(_nativeIndex).Append(")");
                result.Append(" ");
            }

            _flags.Except("Native", "Event", "Delegate", "Defined", "Public", "HasDefaults", "HasOutParms").Each(f => result.Append(f.ToLower() + " "));

            if (HasFlag("Event"))
                result.Append("event ");
            else if (HasFlag("Delegate"))
                result.Append("delegate ");
            else
                result.Append("function ");
            string type = GetReturnType();
            if (type != null)
            {
                result.Append(type).Append(" ");
            }
            result.Append(_self.ObjectName).Append("(");
            int paramCount = 0;
            var locals = new List<UnClassProperty>();

            var statements = ReadBytecode();
            foreach (UnExport export in _self.Children.Reverse())
            {
                object instance = export.ReadInstance();
                if (instance is UnClassProperty)
                {
                    var prop = (UnClassProperty)instance;
                    if (prop.Parm)
                    {
                        if (!prop.ReturnParm)
                        {
                            if (paramCount > 0)
                                result.Append(", ");

                            prop.Flags.Except("Parm").Each(f => result.Append(f.ToLower() + " "));
                            result.Append(prop.GetPropertyType()).Append(" ").Append(export.ObjectName);
                            if (prop.OptionalParm && statements.Count > 0)
                            {
                                if (statements[0].Token is NothingToken)
                                    statements.RemoveRange(0, 1);
                                else if (statements [0].Token is DefaultParamValueToken)
                                {
                                    result.Append(" = ").Append(statements[0].Token.ToString());
                                    statements.RemoveRange(0, 1);
                                }
                            }
                            paramCount++;
                        }
                    }
                    else
                    {
                        locals.Add(prop);
                    }
                }
            }
            result.Append(")");
            if (HasFlag("Defined"))
            {
                result.NewLine().Indent().Append("{").NewLine();
                result.PushIndent();
                foreach (var local in locals)
                {
                    result.Indent().Append("local ").Append(local.GetPropertyType()).Append(" ").Append(local.Name).Append(";").NewLine();
                }
                result.PopIndent();   // will be pushed again in DecompileBytecode()
                DecompileBytecode(statements, result, createControlStatements);
                result.Indent().Append("}").NewLine().NewLine();
            }
            else
            {
                result.Append(";").NewLine().NewLine();
            }
        }
예제 #35
0
 public void Decompile(TextBuilder result)
 {
     result.Append("enum ").Append(_export.ObjectName).Append("\n{\n");
     _values.GetRange(0, _values.Count-1).ForEach(v => result.Append("    ").Append(v).Append(",\n"));
     result.Append("};\n\n");
 }
예제 #36
0
 public override void Decompile(TextBuilder result)
 {
     if (_export.ObjectName.StartsWith("__") && _export.ObjectName.EndsWith("__Delegate"))
     {
         // skip synthetic property
         return;
     }
     base.Decompile(result);
 }
예제 #37
0
파일: UnFunction.cs 프로젝트: yole/unhood
        public void Decompile(TextBuilder result, bool createControlStatements)
        {
            result.Indent();
            if (Native)
            {
                result.Append("native");
                if (_nativeIndex > 0)
                {
                    result.Append("(").Append(_nativeIndex).Append(")");
                }
                result.Append(" ");
            }

            _flags.Except("Native", "Event", "Delegate", "Defined", "Public", "HasDefaults", "HasOutParms").Each(f => result.Append(f.ToLower() + " "));

            if (HasFlag("Event"))
            {
                result.Append("event ");
            }
            else if (HasFlag("Delegate"))
            {
                result.Append("delegate ");
            }
            else
            {
                result.Append("function ");
            }
            string type = GetReturnType();

            if (type != null)
            {
                result.Append(type).Append(" ");
            }
            result.Append(_self.ObjectName).Append("(");
            int paramCount = 0;
            var locals     = new List <UnClassProperty>();

            var statements = ReadBytecode();

            foreach (UnExport export in _self.Children.Reverse())
            {
                object instance = export.ReadInstance();
                if (instance is UnClassProperty)
                {
                    var prop = (UnClassProperty)instance;
                    if (prop.Parm)
                    {
                        if (!prop.ReturnParm)
                        {
                            if (paramCount > 0)
                            {
                                result.Append(", ");
                            }

                            prop.Flags.Except("Parm").Each(f => result.Append(f.ToLower() + " "));
                            result.Append(prop.GetPropertyType()).Append(" ").Append(export.ObjectName);
                            if (prop.OptionalParm && statements.Count > 0)
                            {
                                if (statements[0].Token is NothingToken)
                                {
                                    statements.RemoveRange(0, 1);
                                }
                                else if (statements [0].Token is DefaultParamValueToken)
                                {
                                    result.Append(" = ").Append(statements[0].Token.ToString());
                                    statements.RemoveRange(0, 1);
                                }
                            }
                            paramCount++;
                        }
                    }
                    else
                    {
                        locals.Add(prop);
                    }
                }
            }
            result.Append(")");
            if (HasFlag("Defined"))
            {
                result.NewLine().Indent().Append("{").NewLine();
                result.PushIndent();
                foreach (var local in locals)
                {
                    result.Indent().Append("local ").Append(local.GetPropertyType()).Append(" ").Append(local.Name).Append(";").NewLine();
                }
                result.PopIndent();   // will be pushed again in DecompileBytecode()
                DecompileBytecode(statements, result, createControlStatements);
                result.Indent().Append("}").NewLine().NewLine();
            }
            else
            {
                result.Append(";").NewLine().NewLine();
            }
        }
예제 #38
0
 public override void Decompile(TextBuilder result)
 {
     Decompile(result, true);
 }
예제 #39
0
 public void Print(TextBuilder result, LabelTableToken labelTable, bool showStartOffset)
 {
     result.PushIndent();
     _statements.ForEach(s => s.Print(result, labelTable, showStartOffset));
     result.PopIndent();
 }
예제 #40
0
 public abstract void Decompile(TextBuilder result);
예제 #41
0
 protected virtual void PrintChildren(TextBuilder result, LabelTableToken labelTable, bool showStartOffsets)
 {
     _children.Print(result, labelTable, showStartOffsets);
 }
예제 #42
0
파일: UnFunction.cs 프로젝트: yole/unhood
 public override void Decompile(TextBuilder result)
 {
     Decompile(result, true);
 }
예제 #43
0
 private static string PrintText(StatementListBuilder builder)
 {
     builder.List.CreateControlStatements();
     builder.List.RemoveRedundantReturns();
     var result = new TextBuilder();
     builder.List.Print(result, null, false);
     return result.ToString();
 }