public static BytesToString ( byte ba ) : string | ||
ba | byte | |
return | string |
/// <summary> /// /// </summary> /// <param name="fi"></param> /// <param name="map"></param> public static Map ReadMap2(FileInfo fi) { FileStream fs = fi.OpenRead(); BinaryReader br = new BinaryReader(fs); byte[] Buffer = br.ReadBytes((int)fi.Length); br.Close(); Map map = new Map(); map.FileOnDisk = fi; MemoryStream ms = new MemoryStream(Buffer); br = new BinaryReader(ms); if (Buffer[0] != 77 || Buffer[1] != 65 || Buffer[2] != 80 || Buffer[3] != 249 || Buffer[4] != 53 || Buffer[5] != 0) { Errors.Error("InputOutput", "File is not a VERGE2 Map"); return(null); } // 6 bytes signature // 4 bytes unknown?? // --- // advance 10 bytes br.ReadBytes(10); // 60 bytes VSP FileName FileInfo vspfile = new FileInfo(Helper.BytesToString(br.ReadBytes(60))); if (!vspfile.Exists) { // TODO: give the option to create a vsp in this case Errors.Error("InputOutput", "VSP was not found. New VSP created."); map.vsp = new Vsp24(); } else { map.vsp = ReadVsp(vspfile.FullName); } // 60 bytes music filename map.MusicFileName = Helper.BytesToString(br.ReadBytes(60)); // 20 bytes render string string dontcare = Helper.BytesToString(br.ReadBytes(20)); // 4 bytes, start position map.PlayerStartX = (int)br.ReadInt16(); map.PlayerStartY = (int)br.ReadInt16(); // 51 bytes, unknown br.ReadBytes(51); // 1 byte, number of layers int layerCount = (int)br.ReadByte(); for (int i = 0; i < layerCount; i++) { // 12 bytes per layer discriptor MapLayer ml = new MapLayer(map); ml.type = LayerType.Tile; ml.name = "Layer " + i.ToString(); int mx, dx, my, dy; mx = (int)br.ReadByte(); dx = (int)br.ReadByte(); my = (int)br.ReadByte(); dy = (int)br.ReadByte(); double dmx, dmy; dmx = 1.0 * mx / dx; dmy = 1.0 * my / dy; ml.parallaxInfo.MultipleX = dmx; ml.parallaxInfo.MultipleY = dmy; /* * ml.parallaxInfo.MultiplyX = (int)br.ReadByte(); * ml.parallaxInfo.DivideX = (int)br.ReadByte(); * ml.parallaxInfo.MultiplyY = (int)br.ReadByte(); * ml.parallaxInfo.DivideY = (int)br.ReadByte(); */ int w = (int)br.ReadInt16(); int h = (int)br.ReadInt16(); ml.size(w, h); ml.Translucency = (int)br.ReadByte(); ml.HLine = (int)br.ReadByte(); // padding br.ReadInt16(); map.Layers.Add(ml); } for (int i = 0; i < layerCount; i++) { int rleLength = br.ReadInt32(); ushort[] layerdata = InputOutput.DecompressData16(Helper.BytesToWords(br.ReadBytes(rleLength))); MapLayer ml = (MapLayer)map.Layers[i]; ml.Data = new short[ml.Width * ml.Height]; for (int j = 0; j < ml.Width * ml.Height; j++) { ml.Data[j] = (short)layerdata[j]; } } int obsDataLength = br.ReadInt32(); byte[] obsdata = InputOutput.DecompressData8(br.ReadBytes(obsDataLength)); map.ObsLayer = new MapLayer(map); map.ObsLayer.type = LayerType.Obs; map.ObsLayer.size(((MapLayer)map.Layers[0]).Width, ((MapLayer)map.Layers[0]).Height); map.ObsLayer.name = "Obstructions"; map.ObsLayer.Data = new short[map.ObsLayer.Width * map.ObsLayer.Height]; for (int i = 0; i < map.ObsLayer.Width * map.ObsLayer.Height; i++) { map.ObsLayer.Data[i] = obsdata[i]; } int zoneDataLength = br.ReadInt32(); byte[] zonedata = InputOutput.DecompressData8(br.ReadBytes(zoneDataLength)); map.ZoneLayer = new MapLayer(map); map.ZoneLayer.type = LayerType.Zone; map.ZoneLayer.size(((MapLayer)map.Layers[0]).Width, ((MapLayer)map.Layers[0]).Height); map.ZoneLayer.name = "Zones"; map.ZoneLayer.Data = new short[map.ZoneLayer.Width * map.ZoneLayer.Height]; for (int i = 0; i < map.ZoneLayer.Width * map.ZoneLayer.Height; i++) map.ZoneLayer.Data[i] = zonedata[i]; }
public unsafe static Map ReadMap3(FileInfo fi) { byte[] m3s = new byte[] { (byte)'V', (byte)'3', (byte)'M', (byte)'A', (byte)'P', (byte)0 }; FileStream fs = fi.OpenRead(); BinaryReader br = new BinaryReader(fs); Map map = new Map(); map.FileOnDisk = fi; // Directory.SetCurrentDirectory(map.FileOnDisk.Directory.FullName); byte[] sig = br.ReadBytes(m3s.Length); for (int i = 0; i < m3s.Length; i++) { if (sig[i] != m3s[i]) { return(null); } } int version = br.ReadInt32(); int vcofs = br.ReadInt32(); byte[] formal = br.ReadBytes(256); byte[] vspname = br.ReadBytes(256); byte[] musicname = br.ReadBytes(256); byte[] renderstring = br.ReadBytes(256); byte[] aexec = br.ReadBytes(256); string vspf = Helper.BytesToString(vspname); FileInfo vspfile; try { vspfile = new FileInfo(vspf); } catch (ArgumentException) { throw new Exception("VSP file is inaccessible. Requested file was '" + vspf + "'"); } if (!vspfile.Exists) { System.Windows.Forms.DialogResult dr = System.Windows.Forms.MessageBox.Show("Unable to load requested VSP file. Create a blank vsp?", "Load Error", System.Windows.Forms.MessageBoxButtons.YesNoCancel, System.Windows.Forms.MessageBoxIcon.Information); if (dr == System.Windows.Forms.DialogResult.OK) { map.vsp = new Vsp24(); } else if (dr == System.Windows.Forms.DialogResult.Cancel) { return(null); } else { return(null); } } else { map.vsp = ReadVsp(vspfile.FullName); } map.FormalName = Helper.BytesToString(formal); map.MusicFileName = Helper.BytesToString(musicname); //map.RenderString = Helper.BytesToString(renderstring); string rs = Helper.BytesToString(renderstring); map.AutoExecEvent = Helper.BytesToString(aexec); map.PlayerStartX = br.ReadInt16(); map.PlayerStartY = br.ReadInt16(); int layercount = br.ReadInt32(); for (int i = 0; i < layercount; i++) { MapLayer ml = new MapLayer(map); ml.name = Helper.BytesToString(br.ReadBytes(256)); ml.type = LayerType.Tile; ml.parallaxInfo = new ParallaxInfo(); ml.parallaxInfo.MultipleX = br.ReadDouble(); ml.parallaxInfo.MultipleY = br.ReadDouble(); int w = br.ReadInt16(); int h = br.ReadInt16(); ml.size(w, h); ml.Translucency = br.ReadByte(); int len = br.ReadInt32(); int zlen = br.ReadInt32(); ml.Data = ZLIB.DecodeShorts(br.ReadBytes(zlen), len); map.Layers.Add(ml); } MapLayer ol = new MapLayer(map); ol.type = LayerType.Obs; ol.name = "Obstructions"; ol.size(((MapLayer)map.Layers[0]).Width, ((MapLayer)map.Layers[0]).Height); int ol_lenn = br.ReadInt32(); int ol_len = br.ReadInt32(); byte[] obsdata = ZLIB.Decode(br.ReadBytes(ol_len), ol_lenn); for (int i = 0; i < obsdata.Length; i++) { ol.Data[i] = obsdata[i]; } map.ObsLayer = ol; int zl_lenn = br.ReadInt32(); int zl_len = br.ReadInt32(); MapLayer zl = new MapLayer(map); zl.type = LayerType.Zone; zl.name = "Zones"; zl.size(((MapLayer)map.Layers[0]).Width, ((MapLayer)map.Layers[0]).Height); zl.Data = ZLIB.DecodeShorts(br.ReadBytes(zl_len), zl_lenn); map.ZoneLayer = zl; MapLayer el = new MapLayer(map); el.type = LayerType.Entity; el.name = "Entities"; map.EntLayer = el; map.Layers.Add(el); MapLayerSpecial rl = new MapLayerSpecial(map); rl.type = LayerType.Special_Retrace; map.Layers.Add(rl); map.Layers.Add(ol); map.Layers.Add(zl); int zonecount = br.ReadInt32(); for (int i = 0; i < zonecount; i++) { MapZone mz = new MapZone(); byte[] zname = br.ReadBytes(256); byte[] pscript = br.ReadBytes(256); // byte[] escript = br.ReadBytes(256); mz.Name = Helper.BytesToString(zname); mz.PlayerScript = Helper.BytesToString(pscript); // mz.EntityScript = Helper.BytesToString(escript); mz.Rate = br.ReadByte(); mz.Delay = br.ReadByte(); mz.AdjAct = br.ReadByte(); mz.ID = i; map.Zones.Add(mz); } if (zonecount == 0) { MapZone mz = new MapZone(); mz.ID = 0; mz.Name = "NULL_ZONE"; map.Zones.Add(mz); } int entcount = br.ReadInt32(); for (int i = 0; i < entcount; i++) { MapEntity me = new MapEntity(); me.TileX = br.ReadInt16(); me.TileY = br.ReadInt16(); me.Facing = br.ReadByte(); me.ObeyObstruction = br.ReadByte(); me.IsObstruction = br.ReadByte(); me.AutoFace = br.ReadByte(); me.Speed = br.ReadInt16(); me.ActivationMode = br.ReadByte(); me.MoveType = br.ReadByte(); me.WanderRectangle.x0 = br.ReadInt16(); me.WanderRectangle.y0 = br.ReadInt16(); me.WanderRectangle.x1 = br.ReadInt16(); me.WanderRectangle.y1 = br.ReadInt16(); me.WanderDelay = br.ReadInt16(); int expand = br.ReadInt32(); me.MoveScript = Helper.BytesToString(br.ReadBytes(256)); me.ChrName = Helper.BytesToString(br.ReadBytes(256)); me.Description = Helper.BytesToString(br.ReadBytes(256)); me.onActivate = Helper.BytesToString(br.ReadBytes(256)); me.ID = map.Entities.Add(me); } br.Close(); string rs2 = ""; if (version == 1) { foreach (char c in rs) { if (rs2.Length != 0) { rs2 += ","; } rs2 += c; } map.RenderString = rs2; } else { map.RenderString = rs; } return(map); }
public unsafe static Vsp24 ReadVsp24(FileInfo fi) { //Errors.Error("loading vsp"); Vsp24 vsp = new Vsp24(); vsp.FileOnDisk = fi; FileStream fs = fi.OpenRead(); BinaryReader br = new BinaryReader(fs); int sig = br.ReadInt32(); int version = br.ReadInt32(); if (sig != VSP_SIGNATURE || version != VSP_VERSION) { return(null); } // assume 16 int tilesize = br.ReadInt32(); // assume 24-bit int format = br.ReadInt32(); int tilecount = br.ReadInt32(); // assume zlib int compression = br.ReadInt32(); int datalen = br.ReadInt32(); int zdatalen = br.ReadInt32(); byte[] zdata = br.ReadBytes(zdatalen); byte[] data = ZLIB.Decode(zdata, datalen); for (int tile = 0; tile < tilecount; tile++) { int[] px = new int[256]; for (int i = 0; i < 256; i++) { int idx = tile * 256 + i; int c = unchecked ((int)0xFF000000); c = c | (data[idx * 3] << 16); c = c | (data[idx * 3 + 1] << 8); c = c | data[idx * 3 + 2]; px[i] = c; } vsp.Tiles.Add(new Vsp24Tile(vsp, new pr2.BufferImage(16, 16, px))); } int animcount = br.ReadInt32(); for (int i = 0; i < animcount; i++) { VspAnimation va = new VspAnimation(); va.Name = Helper.BytesToString(br.ReadBytes(256)); va.Start = br.ReadInt32(); va.End = br.ReadInt32(); va.Delay = br.ReadInt32(); va.Mode = br.ReadInt32(); va.ID = i; vsp.Animations.Add(va); } int obscount = br.ReadInt32(); int od_len = br.ReadInt32(); int od_zlen = br.ReadInt32(); byte[] zd = br.ReadBytes(od_zlen); byte[] od = ZLIB.Decode(zd, od_len); for (int i = 0; i < obscount; i++) { int[] tile = new int[256]; for (int j = 0; j < 256; j++) { tile[j] = od[i * 256 + j]; } vsp.ObstructionTiles.Add(new VspObstructionTile(vsp, tile)); } br.Close(); return(vsp); }