public static StringToPaddedByteArray ( string s, int maxlength ) : byte[] | ||
s | string | |
maxlength | int | |
return | byte[] |
public unsafe static int WriteMap(FileInfo fi, Map map) { byte[] map3_signature = new byte[] { (byte)'V', (byte)'3', (byte)'M', (byte)'A', (byte)'P', (byte)0 }; if (map == null) { return(-1); } string vspname = Helper.GetRelativePath(map.FileOnDisk.Directory.FullName, map.vsp.FileOnDisk.FullName); if (vspname == null) { throw new Exception("Unable to resolve VSP path."); } fi = new FileInfo(fi.Name); if (fi.Exists) { fi.Delete(); } FileStream fs = fi.OpenWrite(); MemoryStream ms_outbuf = new MemoryStream(); BinaryWriter bw_o = new BinaryWriter(ms_outbuf); MemoryStream ms_outbuf2 = new MemoryStream(); BinaryWriter bw = new BinaryWriter(ms_outbuf2); bw_o.Write(map3_signature); bw_o.Write(Global.VERSIONINFO.MAPVERSION); bw.Write(Helper.StringToPaddedByteArray(map.FormalName, 256)); bw.Write(Helper.StringToPaddedByteArray(vspname, 256)); bw.Write(Helper.StringToPaddedByteArray(map.MusicFileName, 256)); bw.Write(Helper.StringToPaddedByteArray(map.RenderString, 256)); bw.Write(Helper.StringToPaddedByteArray(map.AutoExecEvent, 256)); bw.Write((short)map.PlayerStartX); bw.Write((short)map.PlayerStartY); int special_count = 0; foreach (MapLayer ml in map.Layers) { if (ml.type != LayerType.Tile) { special_count++; } } bw.Write(map.Layers.Count - special_count); foreach (MapLayer ml in map.Layers) { if (ml.type != LayerType.Tile) { continue; } bw.Write(Helper.StringToPaddedByteArray(ml.name, 256)); bw.Write(ml.parallaxInfo.MultipleX); bw.Write(ml.parallaxInfo.MultipleY); bw.Write((short)ml.Width); bw.Write((short)ml.Height); bw.Write((byte)ml.Translucency); fixed(short *ptr = ml.Data) { byte[] zdata = ZLIB.Encode((byte *)ptr, ml.Data.Length * 2); bw.Write(ml.Data.Length * 2); bw.Write(zdata.Length); bw.Write(zdata); } } MapLayer zl = map.ZoneLayer, ol = map.ObsLayer; byte[] obsdata = new byte[ol.Data.Length]; for (int j = 0; j < ol.Data.Length; j++) { obsdata[j] = (byte)ol.Data[j]; fixed(byte *ptr = obsdata) { byte[] zdata = ZLIB.Encode(ptr, obsdata.Length); bw.Write(obsdata.Length); bw.Write(zdata.Length); bw.Write(zdata); } fixed(short *ptr = zl.Data) { byte[] zdata = ZLIB.Encode((byte *)ptr, zl.Data.Length * 2); bw.Write(zl.Data.Length * 2); bw.Write(zdata.Length); bw.Write(zdata); } bw.Write(map.Zones.Count); foreach (MapZone mz in map.Zones) { bw.Write(Helper.StringToPaddedByteArray(mz.Name, 256)); bw.Write(Helper.StringToPaddedByteArray(mz.PlayerScript, 256)); // bw.Write(Helper.StringToPaddedByteArray(mz.EntityScript,256)); bw.Write((byte)mz.Rate); bw.Write((byte)mz.Delay); bw.Write((byte)mz.AdjAct); } bw.Write(map.Entities.Count); foreach (MapEntity me in map.Entities) { bw.Write((short)me.TileX); bw.Write((short)me.TileY); bw.Write((byte)me.Facing); bw.Write((byte)me.ObeyObstruction); bw.Write((byte)me.IsObstruction); bw.Write((byte)me.AutoFace); bw.Write((short)me.Speed); bw.Write((byte)me.ActivationMode); bw.Write((byte)me.MoveType); bw.Write((short)me.WanderRectangle.x0); bw.Write((short)me.WanderRectangle.y0); bw.Write((short)me.WanderRectangle.x1); bw.Write((short)me.WanderRectangle.y1); bw.Write((short)me.WanderDelay); bw.Write((int)0);//expand bw.Write(Helper.StringToPaddedByteArray(me.MoveScript, 256)); bw.Write(Helper.StringToPaddedByteArray(me.ChrName, 256)); bw.Write(Helper.StringToPaddedByteArray(me.Description, 256)); bw.Write(Helper.StringToPaddedByteArray(me.onActivate, 256)); } bw_o.Write((int)14 + (int)ms_outbuf2.Length); bw.Close(); bw_o.Close(); bw = new BinaryWriter(fs); bw.Write(ms_outbuf.ToArray()); bw.Write(ms_outbuf2.ToArray()); //write number of compiled vc functions bw.Write((int)0); bw.Close(); return(0); }
public unsafe static int WriteVsp(FileInfo fi, Vsp24 vsp) { if (fi.Exists) { fi.Delete(); } FileStream fs = fi.OpenWrite(); BinaryWriter bw = new BinaryWriter(fs); vsp.FileOnDisk = fi; bw.Write(VSP_SIGNATURE); bw.Write(VSP_VERSION); bw.Write(VSP_TILESIZE); bw.Write(VSP_FORMAT); bw.Write(vsp.Tiles.Count); bw.Write(VSP_COMPRESSION); // build byte array of all tiles in our vsp MemoryStream ms_tiles = new MemoryStream(); BinaryWriter bw_tiles = new BinaryWriter(ms_tiles); foreach (Vsp24Tile tile in vsp.Tiles) { for (int y = 0; y < 16; y++) { for (int x = 0; x < 16; x++) { int p = tile.Image.GetPixel(x, y); bw_tiles.Write((byte)((p & 0x00FF0000) >> 16)); bw_tiles.Write((byte)((p & 0x0000FF00) >> 8)); bw_tiles.Write((byte)((p & 0x000000FF))); } } } bw_tiles.Close(); byte[] zdata = ZLIB.Encode(ms_tiles.ToArray()); bw.Write((int)16 * 16 * 3 * vsp.Tiles.Count); bw.Write(zdata.Length); bw.Write(zdata); bw.Write(vsp.Animations.Count); foreach (VspAnimation va in vsp.Animations) { bw.Write(Helper.StringToPaddedByteArray(va.Name, 256)); bw.Write(va.Start); bw.Write(va.End); bw.Write(va.Delay); bw.Write(va.Mode); } bw.Write(vsp.ObstructionTiles.Count); byte[] odata = new byte[vsp.ObstructionTiles.Count * 256]; for (int i = 0; i < vsp.ObstructionTiles.Count; i++) { int[] pixels = ((VspObstructionTile)vsp.ObstructionTiles[i]).Image.GetArray(); for (int j = 0; j < 256; j++) { odata[i * 256 + j] = (byte)pixels[j]; } } byte[] ozdata = ZLIB.Encode(odata); bw.Write((int)odata.Length); bw.Write((int)ozdata.Length); bw.Write(ozdata); Global.pluginManager.tilesetSave(fs); bw.Close(); return(0); }