Example #1
0
        string CodeToSingleLine(File.Code c, string context, bool keep_newline = false)
        {
            string code = null;

            if (c.Size == 0)
            {
                Debug.WriteLine("Code '" + c.Name + "' has no data but is regestered?");
            }
            if (c.Size > 0)
            {
                BlockToCode output = CreateOutput(context);
                GetScriptWriter(output).WriteCode(c);
                code = output.ToString();
                Debug.Assert(!string.IsNullOrWhiteSpace(code));
                if (!keep_newline)
                {
                    code = regex_newline.Replace(code, ";");
                    // replace all double/tripple commas and puts a space next to any statements so its slightly easyer to read in a line
                    code = regex_commas.Replace(code, "; ");
                    code = code.Trim();
                }
                return(code);
            }
            return(null);
        }
Example #2
0
 public void WriteCode(File.Code code)
 {
     if (code == null)
     {
         throw new ArgumentNullException("code");
     }
     WriteCode(code, DecompileCode(code));
 }
Example #3
0
 public virtual void Write(File.Code code)
 {
     using (var output = new BlockToCode(new ErrorContext(code.Name), this))
     {
         new GameMaker.Writer(output).WriteCode(code);
         Write(output.ToString());
     }
 }
Example #4
0
        static void DoFileWrite(string path, File.Code s)
        {
            if (s.Block == null)
            {
                return;                  // do nothing if we have no code
            }
            string filename = Path.ChangeExtension(Path.Combine(path, s.Name), "js");

            using (ResourceFormater fmt = new ResourceFormater(filename)) fmt.Write(s);
        }
Example #5
0
 public void WriteCode(File.Code code, ILBlock block)
 {
     if (code == null)
     {
         throw new ArgumentNullException("code");
     }
     if (block == null)
     {
         throw new ArgumentNullException("block");
     }
     output.Write(block);
 }
Example #6
0
        ILBlock DecompileCode(File.Code codeData)
        {
            ILBlock block = codeData.Block;

            if (block == null)
            {
                output.Error("Code '{0}' empty, but used here", codeData.Name);
            }
            else if (!codeUsed.ContainsKey(codeData.Name) || !codeUsed.TryAdd(codeData.Name, true))  // check if already done
            {
                AddBlockToLocals(block);
            }
            // block.ClearAndSetAllParents();
            return(block);
        }
Example #7
0
        public void Write(File.Script script)
        {
            ILBlock block = script.Block;

            if (block == null)
            {
                output.Error("Code '{0}' empty, but used here for script", script.Name);
                return; // error
            }
            else
            {
                File.Code codeData = script.Code;
                if (block == null)
                {
                    output.Error("Code '{0}' empty, but used here", codeData.Name);
                }
                else if (!codeUsed.ContainsKey(codeData.Name) || !codeUsed.TryAdd(codeData.Name, true)) // check if already done
                {
                    AddBlockToLocals(block);
                }
            }
            CheckAllVars();
            int arguments = 0;

            foreach (var e in block.GetSelfAndChildrenRecursive <ILExpression>(x => x.Code == GMCode.Var))
            {
                ILVariable v     = e.Operand as ILVariable;
                Match      match = Context.ScriptArgRegex.Match(v.Name);
                if (match != null && match.Success)
                {
                    int arg = int.Parse(match.Groups[1].Value) + 1; // we want the count
                    if (arg > arguments)
                    {
                        arguments = arg;
                    }
                }
            }
            ScriptInfo oi = new ScriptInfo();

            oi.Locals        = locals.ToDictionary(x => x.Key, z => z.Value);
            oi.Script        = script;
            oi.ArgumentCount = arguments;
            oi.Block         = block;
            WriteScript(oi);
        }
Example #8
0
        public ObjectInfo BuildEventInfo(File.GObject obj)
        {
            List <EventInfo> infos = new List <EventInfo>();

            if (obj.SpriteIndex > -1)
            {
                spritesUsed.TryAdd(obj.SpriteIndex, File.Sprites[obj.SpriteIndex].Name);
            }
            if (obj.Parent > -1)
            {
                objectsUsed.TryAdd(obj.Parent, File.Objects[obj.Parent].Name);
            }
            // seperating the compiling time for all the tasks didn't make it faster humm
            for (int i = 0; i < obj.Events.Length; i++)
            {
                if (obj.Events[i] == null)
                {
                    continue;
                }
                EventInfo einfo = new EventInfo();
                ConcurrentBag <ActionInfo> actions = new ConcurrentBag <ActionInfo>();
                //  var actions = einfo.Actions;
                infos.Add(einfo);
                einfo.Type = i;
                if (Context.doThreads)
                {
                    Parallel.ForEach(obj.Events[i], e => // This too much?:P
                    {
                        Parallel.ForEach(e.Actions, a =>
                        {
                            File.Code codeData = File.Codes[a.CodeOffset];
                            ILBlock block      = DecompileCode(codeData);
                            ActionInfo info    = new ActionInfo()
                            {
                                Method = block, Name = Context.EventToString(i, e.SubType), SubType = e.SubType, Type = i
                            };
                            actions.Add(info);
                        });
                    });
                }
                else
                {
                    foreach (var e in obj.Events[i])
                    {
                        foreach (var a in e.Actions)
                        {
                            File.Code  codeData = File.Codes[a.CodeOffset];
                            ILBlock    block    = DecompileCode(codeData);
                            ActionInfo info     = new ActionInfo()
                            {
                                Method = block, Name = Context.EventToString(i, e.SubType), SubType = e.SubType, Type = i
                            };
                            actions.Add(info);
                        }
                    }
                }

                einfo.Actions = actions.OrderBy(x => x.Type).ToList();
            }
            CheckAllVars();
            ObjectInfo oi = new ObjectInfo();

            oi.Events = infos;
            oi.Object = obj;
            oi.Locals = locals.ToDictionary(x => x.Key, z => z.Value);

            return(oi);
        }
Example #9
0
 public static string QuickCodeToLine(File.Code code, string context, bool keep_newline = false)
 {
     return(new AllWriter().CodeToSingleLine(code, context, keep_newline));
 }
Example #10
0
 public static string QuickCodeToLine(File.Code code, string context)
 {
     return(new AllWriter().CodeToSingleLine(code, context));
 }
Example #11
0
        static void DoFileWrite(string path, File.Code s)
        {
            string filename = Path.ChangeExtension(Path.Combine(path, s.Name), "js");

            using (ResourceFormater fmt = new ResourceFormater(filename)) fmt.Write(s);
        }
Example #12
0
        public List <ILNode> Build(File.Code code, ErrorContext error = null)
        {
            if (code == null)
            {
                throw new ArgumentNullException("code");
            }
            Stream stream = code.Data;

            if (stream == null)
            {
                throw new ArgumentNullException("code.Data");
            }
            if (!stream.CanRead)
            {
                throw new ArgumentException("Must be readable", "code_stream");
            }
            if (!stream.CanSeek)
            {
                throw new ArgumentException("Must be seekable", "code_stream");
            }
            if (stream.Length == 0)
            {
                return(new List <ILNode>());                   // empty stream
            }
            StartingOffset  = code.CodePosition;
            Error           = error ?? new ErrorContext(code.Name);
            labels          = new Dictionary <int, ILLabel>(); // cause they are all the same
            stream.Position = 0;
            r         = new BinaryReader(stream);
            CurrentPC = 0;
            List <ILNode>         list           = new List <ILNode>();
            Dictionary <int, int> pcToExpressoin = new Dictionary <int, int>();

            Start(list);
            while (stream.Position < stream.Length)
            {
                CurrentPC  = (int)stream.Position / 4;
                CurrentRaw = r.ReadUInt32();
                ILExpression e = CreateExpression(list);
                if (e != null)
                {
                    /*
                     * // hack here cause of issues
                     * if (e.Code == GMCode.Conv)
                     * {
                     *  var prev = list.Last.Value as ILExpression;
                     *  Debug.Assert(prev.Code != GMCode.Pop);
                     *  prev.ILRanges.Add(new ILRange(CurrentPC, CurrentPC));
                     *  prev.Types = e.Types; // don't add it
                     * }
                     * else
                     */
                    pcToExpressoin.Add(CurrentPC, list.Count);
                    list.Add(e);
                }
            }
            CurrentPC  = (int)stream.Position / 4;
            CurrentRaw = 0;
            if (labelExists(CurrentPC)) // this is in case we do have a jump but we need to exit clean
            {
                pcToExpressoin.Add(CurrentPC, list.Count);
                list.Add(CreateExpression(GMCode.Exit, null)); // make sure we got an exit as the last code
                // we HAVE to have an exit
            }
            else
            {
                ILExpression last = list.Last() as ILExpression;
                if (last.Code != GMCode.Ret && last.Code != GMCode.Exit)
                {
                    pcToExpressoin.Add(CurrentPC, list.Count);
                    list.Add(CreateExpression(GMCode.Exit, null)); // make sure we got an exit as the last code
                }
            }

            foreach (var l in labels)
            {
                int n;
                if (pcToExpressoin.TryGetValue(l.Key, out n))
                {
                    list[n].UserData = l.Value;
                }
            }
            var rlist = new List <ILNode>();

            for (int i = 0; i < list.Count; i++)
            {
                ILExpression e = list[i] as ILExpression;
                if (e != null)
                {
                    if (e.UserData != null)
                    {
                        rlist.Add(e.UserData as ILLabel); e.UserData = null;
                    }
                    if (e.Code == GMCode.Conv)
                    {
                        ILExpression ne = list[i + 1] as ILExpression;
                        ne.ILRanges.AddRange(e.ILRanges);
                        ne.Types = e.Types;
                        continue; // skip
                    }
                }
                rlist.Add(list[i]);
            }
            Finish(rlist);

            return(rlist);
        }