public static void DumpMem(object b, DumpState D) { #if XBOX || SILVERLIGHT // todo: implement this - mjf Debug.Assert(false); #else //Console.WriteLine(b.GetType().ToString() + ":::" + b.ToString()); //uint size = (uint)Marshal.SizeOf(b); //D.status = D.writer(D.L, new CharPtr(b.ToString()), size, D.data); ///* int size = Marshal.SizeOf(b); IntPtr ptr = Marshal.AllocHGlobal(size); Marshal.StructureToPtr(b, ptr, false); byte[] bytes = new byte[size]; Marshal.Copy(ptr, bytes, 0, size); char[] ch = new char[bytes.Length]; for (int i = 0; i < bytes.Length; i++) ch[i] = (char)bytes[i]; CharPtr str = ch; DumpBlock(str, (uint)str.chars.Length, D); Marshal.Release(ptr); // */ #endif }
public static void DumpMem(object b, int n, DumpState D) { Array array = b as Array; Debug.Assert(array.Length == n); for (int i = 0; i < n; i++) DumpMem(array.GetValue(i), D); }
public static void DumpMem(object b, int n, DumpState D) { Array array = b as Array; // Fix for stripped debug info. Debug.Assert(array.Length == n || (D.strip != 0 && array.Length == 0)); for (int i = 0; i < n; i++) DumpMem(array.GetValue(i), D); }
private static void DumpBlock(CharPtr b, uint size, DumpState D) { if (D.status==0) { LuaUnlock(D.L); D.status=D.writer(D.L,b,size,D.data); LuaLock(D.L); } }
public override void Write(object o, HtmlWriter tag, DumpState state, RefDictionary refDict) { var val = o.ToString(); tag.Tag("kbd", t => { t.NewLineAfterSTag = false; t.WriteString(val); }); }
public static int LuaUDump(LuaState L, Proto f, lua_Writer w, object data, int strip) { DumpState D = new DumpState(); D.L=L; D.writer=w; D.data=data; D.strip=strip; D.status=0; DumpHeader(D); DumpFunction(f,null,D); return D.status; }
public static void DumpMem(object b, DumpState D) { #if XBOX || SILVERLIGHT // todo: implement this - mjf Debug.Assert(false); #else int size = Marshal.SizeOf(b); IntPtr ptr = Marshal.AllocHGlobal(size); Marshal.StructureToPtr(b, ptr, false); byte[] bytes = new byte[size]; Marshal.Copy(ptr, bytes, 0, size); char[] ch = new char[bytes.Length]; for (int i = 0; i < bytes.Length; i++) ch[i] = (char)bytes[i]; CharPtr str = ch; DumpBlock(str, (uint)str.chars.Length, D); Marshal.Release(ptr); #endif }
public static void DumpVar(object x, DumpState D) { DumpMem(x, D); }
private static void DumpInt(int x, DumpState D) { DumpVar(x,D); }
private static void DumpString(TString s, DumpState D) { if (s==null || GetStr(s)==null) { uint size=0; DumpVar(size,D); } else { uint size=s.tsv.len+1; /* include trailing '\0' */ DumpVar(size,D); DumpBlock(GetStr(s),size,D); } }
DumpCode(f,D); DumpConstants(f,D); DumpDebug(f,D); }
private static void DumpFunction(Proto f, TString p, DumpState D) { DumpString( ((f.source==p) || (D.strip!=0)) ? null : f.source, D); DumpInt(f.linedefined,D); DumpInt(f.lastlinedefined,D); DumpChar(f.nups,D); DumpChar(f.numparams,D); DumpChar(f.is_vararg,D); DumpChar(f.maxstacksize,D); DumpCode(f,D); DumpConstants(f,D); DumpDebug(f,D); }
private static void Run(string inWz, string outPath, WZVariant wzVar, bool initialEnc) { Console.WriteLine("Input .wz: {0}{1}Output .nx: {2}", Path.GetFullPath(inWz), Environment.NewLine, Path.GetFullPath(outPath)); Stopwatch swOperation = new Stopwatch(); Stopwatch fullTimer = new Stopwatch(); Action<string> reportDone = str => { Console.WriteLine("done. E{0} T{1}", swOperation.Elapsed, fullTimer.Elapsed); swOperation.Restart(); Console.Write(str); }; fullTimer.Start(); swOperation.Start(); Console.Write("Parsing input WZ... ".PadRight(31)); WZReadSelection rFlags = WZReadSelection.EagerParseImage | WZReadSelection.EagerParseStrings; if (!dumpImg) rFlags |= WZReadSelection.NeverParseCanvas; using (WZFile wzf = new WZFile(inWz, wzVar, initialEnc, rFlags)) using ( FileStream outFs = new FileStream(outPath, FileMode.Create, FileAccess.ReadWrite, FileShare.None)) using (BinaryWriter bw = new BinaryWriter(outFs)) { DumpState state = new DumpState(); reportDone("Writing header... ".PadRight(31)); bw.Write(PKG4); bw.Write(new byte[(4 + 8)*4]); reportDone("Writing nodes... ".PadRight(31)); outFs.EnsureMultiple(4); ulong nodeOffset = (ulong) bw.BaseStream.Position; List<WZObject> nodeLevel = new List<WZObject> {wzf.MainDirectory}; while (nodeLevel.Count > 0) WriteNodeLevel(ref nodeLevel, state, bw); ulong stringOffset; uint stringCount = (uint) state.Strings.Count; { reportDone("Writing string data...".PadRight(31)); Dictionary<uint, string> strings = state.Strings.ToDictionary(kvp => kvp.Value, kvp => kvp.Key); ulong[] offsets = new ulong[stringCount]; for (uint idx = 0; idx < stringCount; ++idx) { outFs.EnsureMultiple(2); offsets[idx] = (ulong) bw.BaseStream.Position; WriteString(strings[idx], bw); } outFs.EnsureMultiple(8); stringOffset = (ulong) bw.BaseStream.Position; for (uint idx = 0; idx < stringCount; ++idx) bw.Write(offsets[idx]); } ulong bitmapOffset = 0UL; uint bitmapCount = 0U; if (dumpImg) { reportDone("Writing canvas data...".PadRight(31)); bitmapCount = (uint) state.Canvases.Count; ulong[] offsets = new ulong[bitmapCount]; long cId = 0; foreach (WZCanvasProperty cNode in state.Canvases) { outFs.EnsureMultiple(8); offsets[cId++] = (ulong) bw.BaseStream.Position; WriteBitmap(cNode, bw); } outFs.EnsureMultiple(8); bitmapOffset = (ulong) bw.BaseStream.Position; for (uint idx = 0; idx < bitmapCount; ++idx) bw.Write(offsets[idx]); } ulong soundOffset = 0UL; uint soundCount = 0U; if (dumpSnd) { reportDone("Writing MP3 data... ".PadRight(31)); soundCount = (uint) state.MP3s.Count; ulong[] offsets = new ulong[soundCount]; long cId = 0; foreach (WZAudioProperty mNode in state.MP3s) { outFs.EnsureMultiple(8); offsets[cId++] = (ulong) bw.BaseStream.Position; WriteMP3(mNode, bw); } outFs.EnsureMultiple(8); soundOffset = (ulong) bw.BaseStream.Position; for (uint idx = 0; idx < soundCount; ++idx) bw.Write(offsets[idx]); } reportDone("Writing linked node data... ".PadRight(31)); byte[] uolReplace = new byte[16]; foreach (KeyValuePair<WZUOLProperty, Action<BinaryWriter, byte[]>> pair in state.UOLs) { WZObject result = pair.Key.FinalTarget; if (result == null) continue; bw.BaseStream.Position = (long) (nodeOffset + state.GetNodeID(result)*20 + 4); bw.BaseStream.Read(uolReplace, 0, 16); pair.Value(bw, uolReplace); } reportDone("Finalising... ".PadRight(31)); bw.Seek(4, SeekOrigin.Begin); bw.Write((uint) state.Nodes.Count); bw.Write(nodeOffset); bw.Write(stringCount); bw.Write(stringOffset); bw.Write(bitmapCount); bw.Write(bitmapOffset); bw.Write(soundCount); bw.Write(soundOffset); reportDone("Completed!"); } }
private static void DumpByte(int y, DumpState D) { lu_byte x = (lu_byte)y; DumpVar(x, D); }
public static void DumpMem(object b, DumpState D) { CharPtr str = Lua.object_to_charptr(b); DumpBlock(str, (uint)str.chars.Length, D); }
public static void DumpVar(object x, DumpState D) { DumpMem(x, D); }
private static void DumpCode(Proto f, DumpState D) { DumpVector(f.code, f.sizecode, D); }
} //FIXME: NOTE!!!if n == 1, use this version public static void DumpLiteral(string s, DumpState D) { DumpBlock(new CharPtr(s), (uint)((s.Length + 1) - 1 /*sizeof(char)*/), D); }
} //FIXME: here no need to use *sizeof(v[0]) public static void DumpVector(object v, DumpState D) { DumpBlock(v, D); } //FIXME: NOTE!!!if n == 1, use this version
/* ** All high-level dumps go through DumpVector; you can change it to ** change the endianness of the result */ public static void DumpVector(object v, uint n, DumpState D) { DumpBlock(v, n /* *sizeof((v)[0])*/, D); } //FIXME: here no need to use *sizeof(v[0])
private static void WriteNodeLevel(ref List<WZObject> nodeLevel, DumpState ds, BinaryWriter bw) { uint nextChildId = (uint) (ds.GetNextNodeID() + nodeLevel.Count); foreach (WZObject levelNode in nodeLevel) { if (levelNode is WZUOLProperty) WriteUOL((WZUOLProperty) levelNode, ds, bw); else WriteNode(levelNode, ds, bw, nextChildId); nextChildId += (uint) levelNode.ChildCount; } List<WZObject> @out = new List<WZObject>(); foreach (WZObject levelNode in nodeLevel.Where(n => n.ChildCount > 0)) @out.AddRange(levelNode.OrderBy(f => f.Name, StringComparer.Ordinal)); nodeLevel.Clear(); nodeLevel = @out; }
private static void DumpCode(Proto f, DumpState D) { DumpInt(f.sizecode, D); for (int i = 0; i < f.sizecode; i++) DumpInt((int)f.code[i], D); }
private static void WriteUOL(WZUOLProperty node, DumpState ds, BinaryWriter bw) { ds.AddNode(node); bw.Write(ds.AddString(node.Name)); ds.AddUOL(node, bw.BaseStream.Position); bw.Write(0L); bw.Write(0L); }
public static void DumpVar(object x, DumpState D) { DumpVector(x /*,1*/, D); }
private static void WriteNode(WZObject node, DumpState ds, BinaryWriter bw, uint nextChildID) { ds.AddNode(node); bw.Write(ds.AddString(node.Name)); bw.Write(nextChildID); bw.Write((ushort) node.ChildCount); ushort type; if (node is WZDirectory || node is WZImage || node is WZSubProperty || node is WZConvexProperty || node is WZNullProperty) type = 0; // no data; children only (8) else if (node is WZInt32Property || node is WZUInt16Property || node is WZInt64Property) type = 1; // int32 (4) else if (node is WZSingleProperty || node is WZDoubleProperty) type = 2; // Double (0) else if (node is WZStringProperty) type = 3; // String (4) else if (node is WZPointProperty) type = 4; // (0) else if (node is WZCanvasProperty) type = 5; // (4) else if (node is WZAudioProperty) type = 6; // (4) else throw new InvalidOperationException("Unhandled WZ node type [1]"); bw.Write(type); if (node is WZInt32Property) bw.Write((long) ((WZInt32Property) node).Value); else if (node is WZUInt16Property) bw.Write((long) ((WZUInt16Property) node).Value); else if (node is WZInt64Property) bw.Write(((WZInt64Property) node).Value); else if (node is WZSingleProperty) bw.Write((double) ((WZSingleProperty) node).Value); else if (node is WZDoubleProperty) bw.Write(((WZDoubleProperty) node).Value); else if (node is WZStringProperty) bw.Write(ds.AddString(((WZStringProperty) node).Value)); else if (node is WZPointProperty) { Point pNode = ((WZPointProperty) node).Value; bw.Write(pNode.X); bw.Write(pNode.Y); } else if (node is WZCanvasProperty) { WZCanvasProperty wzcp = (WZCanvasProperty) node; bw.Write(ds.AddCanvas(wzcp)); if (dumpImg) { bw.Write((ushort) wzcp.Value.Width); bw.Write((ushort) wzcp.Value.Height); wzcp.Dispose(); } else bw.Write(0); } else if (node is WZAudioProperty) { WZAudioProperty wzmp = (WZAudioProperty) node; bw.Write(ds.AddMP3(wzmp)); if (dumpSnd) { bw.Write((uint) wzmp.Value.Length); wzmp.Dispose(); } else bw.Write(0); } switch (type) { case 0: bw.Write(0L); break; case 3: bw.Write(0); break; } }
private static void DumpDebug(Proto f, DumpState D) { int i,n; n= (D.strip != 0) ? 0 : f.sizelineinfo; DumpVector(f.lineinfo, n, D); n= (D.strip != 0) ? 0 : f.sizelocvars; DumpInt(n,D); for (i=0; i<n; i++) { DumpString(f.locvars[i].varname,D); DumpInt(f.locvars[i].startpc,D); DumpInt(f.locvars[i].endpc,D); } n= (D.strip != 0) ? 0 : f.sizeupvalues; DumpInt(n,D); for (i=0; i<n; i++) DumpString(f.upvalues[i],D); }
public static void DumpMem(object b, DumpState D) { #if XBOX // todo: implement this - mjf Debug.Assert(false); #endif #if SILVERLIGHT // No support for Marshal.SizeOf in Silverlight, so we // have to manually set the size. Size values are from // Lua's 5.1 spec. // No support for Marshal.StructureToPtr in Silverlight, // let's use BitConverter instead! int size = 0; byte[] bytes; Type t = b.GetType(); if (t.Equals(typeof(UInt32))) { size = 4; bytes = BitConverter.GetBytes((uint)b); } else if (t.Equals(typeof(Int32))) { size = 4; bytes = BitConverter.GetBytes((int)b); } else if (t.Equals(typeof(Char))) { size = 1; bytes = new byte[1] { BitConverter.GetBytes((char)b)[0] }; } else if (t.Equals(typeof(Byte))) { size = 1; bytes = new byte[1] { (byte)b }; //bytes = BitConverter.GetBytes((byte)b); } else if (t.Equals(typeof(Double))) { size = 8; bytes = BitConverter.GetBytes((double)b); } else { throw new NotImplementedException("Invalid type: " + t.FullName); } #else int size = Marshal.SizeOf(b); IntPtr ptr = Marshal.AllocHGlobal(size); Marshal.StructureToPtr(b, ptr, false); byte[] bytes = new byte[size]; Marshal.Copy(ptr, bytes, 0, size); #endif char[] ch = new char[bytes.Length]; for (int i = 0; i < bytes.Length; i++) ch[i] = (char)bytes[i]; CharPtr str = ch; DumpBlock(str, (uint)str.chars.Length, D); #if !SILVERLIGHT Marshal.Release(ptr); #endif }
private static void DumpHeader(DumpState D) { CharPtr h = new char[LUAC_HEADERSIZE]; luaU_header(h); DumpBlock(h,LUAC_HEADERSIZE,D); }
private static void DumpInteger(lua_Integer x, DumpState D) { DumpVar(x, D); }
private static void DumpNumber(LuaNumberType x, DumpState D) { DumpVar(x,D); }
private static void DumpCode(Proto f,DumpState D) { DumpVector(f.code, f.sizecode, D); }
static void DumpVector(object b, int n, DumpState D) { DumpInt(n,D); DumpMem(b, n, D); }
private static void DumpNumber(LuaNumberType x, DumpState D) { DumpVar(x, D); }
public static void DumpMem(object b, DumpState D) { #if XBOX // todo: implement this - mjf Debug.Assert(false); #endif #if SILVERLIGHT // No support for Marshal.SizeOf in Silverlight, so we // have to manually set the size. Size values are from // Lua's 5.1 spec. // No support for Marshal.StructureToPtr in Silverlight, // let's use BitConverter instead! int size = 0; byte[] bytes; Type t = b.GetType(); if (t.Equals(typeof(UInt32))) { size = 4; bytes = BitConverter.GetBytes((uint)b); } else if (t.Equals(typeof(Int32))) { size = 4; bytes = BitConverter.GetBytes((int)b); } else if (t.Equals(typeof(Char))) { size = 1; bytes = new byte[1] { BitConverter.GetBytes((char)b)[0] }; } else if (t.Equals(typeof(Byte))) { size = 1; bytes = new byte[1] { (byte)b }; //bytes = BitConverter.GetBytes((byte)b); } else if (t.Equals(typeof(Double))) { size = 8; bytes = BitConverter.GetBytes((double)b); } else { throw new NotImplementedException("Invalid type: " + t.FullName); } #else int size = Marshal.SizeOf(b); IntPtr ptr = Marshal.AllocHGlobal(size); Marshal.StructureToPtr(b, ptr, false); byte[] bytes = new byte[size]; Marshal.Copy(ptr, bytes, 0, size); #endif char[] ch = new char[bytes.Length]; for (int i = 0; i < bytes.Length; i++) { ch[i] = (char)bytes[i]; } CharPtr str = ch; DumpBlock(str, (uint)str.chars.Length, D); #if !SILVERLIGHT Marshal.Release(ptr); #endif }
private static void DumpNumber(lua_Number x, DumpState D) { DumpMem(BitConverter.GetBytes(x), D); }
public static void DumpMem(byte[] b, DumpState D) { #if XBOX || SILVERLIGHT // todo: implement this - mjf Debug.Assert(false); #else char[] ch = new char[b.Length]; for (int i = 0; i < b.Length; i++) ch[i] = (char)b[i]; CharPtr str = ch; DumpBlock(str, (uint)str.chars.Length, D); #endif }
private static void DumpCode(Proto f, DumpState D) { DumpInt(f.sizecode, D); DumpVector(f.code, (uint)f.sizecode, D); }
private static void DumpChar(int y, DumpState D) { char x=(char)y; DumpVar(x,D); }
private static void DumpByte(int y, DumpState D) { byte[] x = new byte[1]; x[0] = (byte)y; DumpMem(x, D); }
private static void DumpConstants(Proto f, DumpState D) { int i,n=f.sizek; DumpInt(n,D); for (i=0; i<n; i++) { /*const*/ TValue o=f.k[i]; DumpChar(TType(o),D); switch (TType(o)) { case LUA_TNIL: break; case LUA_TBOOLEAN: DumpChar(BValue(o),D); break; case LUA_TNUMBER: DumpNumber(NValue(o),D); break; case LUA_TSTRING: DumpString(RawTSValue(o),D); break; default: LuaAssert(0); /* cannot happen */ break; } } n=f.sizep; DumpInt(n,D); for (i=0; i<n; i++) DumpFunction(f.p[i],f.source,D); }
private static void DumpInt(int x, DumpState D) { byte[] b = BitConverter.GetBytes((UInt32) x); if (!BitConverter.IsLittleEndian) Array.Reverse(b); DumpMem(b, D); }