public static Palette LoadPalette(string filename, int offset = 0x36) { MemoryStream ms = ResourceManager.OpenRead(filename); if (ms == null) { Core.Abort("Couldn't load \"{0}\"", filename); return(null); } BinaryReader msb = new BinaryReader(ms); ms.Seek(offset, SeekOrigin.Begin); uint[] colors = new uint[256]; for (int i = 0; i < 256; i++) { colors[i] = msb.ReadUInt32(); } msb.Close(); return(new Palette(colors)); }
public static AudioClip LoadWAV(string filename) { // uses code from http://stackoverflow.com/questions/8754111/how-to-read-the-data-in-a-wav-file-to-an-array MemoryStream ms = ResourceManager.OpenRead(filename); if (ms == null) { Core.Abort("Couldn't load \"{0}\"", filename); return null; } byte[] wav = new byte[ms.Length]; ms.Read(wav, 0, (int)ms.Length); ms.Close(); int channels = wav[22]; int pos = 12; try { while (!(wav[pos] == 100 && wav[pos + 1] == 97 && wav[pos + 2] == 116 && wav[pos + 3] == 97)) { pos += 4; int chunkSize = wav[pos] + wav[pos + 1] * 256 + wav[pos + 2] * 65536 + wav[pos + 3] * 16777216; pos += 4 + chunkSize; } } catch (IndexOutOfRangeException) { Core.Abort("Couldn't load \"{0}\" (invalid WAV)", filename); return null; } pos += 8; int samples = (wav.Length - pos) / 2; // 2 bytes per sample (16 bit sound mono) if (channels == 2) samples /= 2; float[] fsamples = new float[samples*channels]; int i = 0; while (pos < wav.Length) { fsamples[i] = MakeFloatFromShort(wav[pos], wav[pos + 1]); pos += 2; i++; if (channels == 2) { fsamples[i] = MakeFloatFromShort(wav[pos], wav[pos + 1]); pos += 2; i++; } } // allods wav files are 22k AudioClip clip = AudioClip.Create(filename, samples, channels, 22050, false); clip.SetData(fsamples, 0); return clip; }
// public interface public static Texture2D LoadPalette(string filename, uint offset = 0x36) { MemoryStream ms = ResourceManager.OpenRead(filename); if (ms == null) { Core.Abort("Couldn't load \"{0}\"", filename); return(null); } BinaryReader br = new BinaryReader(ms); ms.Seek(offset, SeekOrigin.Begin); Texture2D texture = LoadPaletteFromStream(br); br.Close(); return(texture); }
public Registry(string filename) { try { MemoryStream ms = ResourceManager.OpenRead(filename); if (ms == null) { Core.Abort("Couldn't load \"{0}\"", filename); return; } BinaryReader msb = new BinaryReader(ms); if (msb.ReadUInt32() != RegistrySignature) { ms.Close(); Core.Abort("Couldn't load \"{0}\" (not a registry file)", filename); return; } uint root_offset = msb.ReadUInt32(); uint root_size = msb.ReadUInt32(); uint reg_flags = msb.ReadUInt32(); uint reg_eatsize = msb.ReadUInt32(); uint reg_junk = msb.ReadUInt32(); Root = new RegistryNode(); Root.Type = RegistryNodeType.Directory; Root.Name = ""; Root.Children = new RegistryNode[root_size]; if (!TreeTraverse(ms, msb, Root, root_offset, root_offset + root_size, 0x1C + 0x20 * reg_eatsize)) { ms.Close(); Core.Abort("Couldn't load \"{0}\" (invalid registry file)", filename); return; } ms.Close(); } catch (IOException) { Core.Abort("Couldn't load \"{0}\"", filename); } }
internal static object LoadObjectFromStream(Type t, BinaryReader br) { if (t.IsAssignableFrom(typeof(string))) { return(Core.ReadSmallString(br)); } if (t.IsAssignableFrom(typeof(int))) { return(br.ReadInt32()); } if (t.IsAssignableFrom(typeof(uint))) { return(br.ReadUInt32()); } if (t.IsAssignableFrom(typeof(short))) { return(br.ReadInt16()); } if (t.IsAssignableFrom(typeof(ushort))) { return(br.ReadUInt16()); } if (t.IsAssignableFrom(typeof(sbyte))) { return(br.ReadSByte()); } if (t.IsAssignableFrom(typeof(byte))) { return(br.ReadByte()); } if (t == typeof(float)) // I'm not sure, but float is assignable from double right? and we need to differ. { return((float)br.ReadDouble()); } if (t == typeof(double)) { return(br.ReadDouble()); } Core.Abort("Unknown type {0} in data.bin serialization!", t.Name); return(null); }
public Font(string filename, int spacing, int line_height, int space_width) { Spacing = spacing; LineHeight = line_height; string[] fns = filename.Split('.'); string fnn = string.Join(".", fns, 0, fns.Length - 1); // first, load the data file. MemoryStream ms = ResourceManager.OpenRead(fnn + ".dat"); if (ms == null) { Core.Abort("Couldn't load \"{0}\" as data file for \"{1}\"", fnn + ".dat", filename); return; } int count = (int)ms.Length / 4; BinaryReader br = new BinaryReader(ms); for (int i = 0; i < 224; i++) { if (i < count) { Widths[i] = br.ReadInt32(); } else { Widths[i] = 0; } } br.Close(); Widths[0] = space_width; CombinedTexture = Images.LoadSprite(filename); CombinedMaterial = new Material(MainCamera.MainShaderPaletted); CombinedMaterial.mainTexture = CombinedTexture.Atlas; }
public void LoadFromFile(string filename) { MemoryStream ms = ResourceManager.OpenRead("world/data/data.bin"); if (ms == null) { Core.Abort("Couldn't load \"world/data/data.bin\"!"); return; } ms.Position = 0; BinaryReader br = new BinaryReader(ms); try { LoadFromStream(br); } finally { br.Close(); } }
public static TextureList LoadSprite(string filename) { string[] extf = filename.Split('.'); string ext = extf[extf.Length - 1].ToLower(); if (ext == "16a") { return(Load16A(filename)); } else if (ext == "256") { return(Load256(filename)); } else if (ext == "16") { return(Load16(filename)); } else { Core.Abort("Couldn't guess the sprite type of \"{0}\"", filename); return(null); } }
public static AllodsSprite LoadSprite(string filename) { string[] filename_split = filename.Split(new char[] { '.' }); string ext = filename_split[filename_split.Length - 1].ToLower(); if (ext == "16a") { return(Load16A(filename)); } else if (ext == "256") { return(Load256(filename)); } else if (ext == "16") { return(Load16(filename)); } else { Core.Abort("Couldn't load \"{0}\" (unknown extension)", filename); return(null); } }
public Font(string filename, int spacing, int line_height, int space_width) { Spacing = spacing; LineHeight = line_height; string[] fns = filename.Split('.'); string fnn = string.Join(".", fns, 0, fns.Length - 1); // first, load the data file. MemoryStream ms_dat = ResourceManager.OpenRead(fnn + ".dat"); if (ms_dat == null) { Core.Abort("Couldn't load \"{0}\" as data file for \"{1}\"", fnn + ".dat", filename); return; } int count = (int)ms_dat.Length / 4; BinaryReader msb_dat = new BinaryReader(ms_dat); for (int i = 0; i < 224; i++) { if (i < count) { Widths[i] = msb_dat.ReadInt32(); } else { Widths[i] = 0; } } msb_dat.Close(); TextureList tlF = Images.LoadSprite(filename); CellX = 0; CellY = 0; for (int i = 0; i < tlF.Textures.Count; i++) { if (tlF.Textures[i].Width > CellX) { CellX = tlF.Textures[i].Width; } if (tlF.Textures[i].Height > CellY) { CellY = tlF.Textures[i].Height; } } int tex_w = CellX * 16; int tex_h = CellY * 16; uint[] tex_pixels = new uint[tex_w * tex_h]; unsafe { fixed(uint *opixels_fixed = tex_pixels) { for (int i = 0; i < tlF.Textures.Count; i++) { int cX = CellX * (i % 16); int cY = CellY * (i / 16); uint *opixels = opixels_fixed + cX + cY * tex_w; fixed(uint *ipixels_fixed = tlF.Textures[i].Pixels) { uint *ipixels = ipixels_fixed; for (int y = 0; y < tlF.Textures[i].Height; y++) { for (int x = 0; x < tlF.Textures[i].Width; x++) { *opixels++ = *ipixels++; } opixels += tex_w - CellX; } } } } } Widths[0] = space_width; CombinedTexture = new Texture(tex_w, tex_h, tex_pixels); }
public static void InitClasses() { if (ClassesLoaded) { return; } ClassesLoaded = true; MemoryStream ms = ResourceManager.OpenRead("world/data/itemname.pkt"); if (ms == null) { Core.Abort("Couldn't load \"world/data/itemname.pkt\"!"); return; } ms.Position = 3; BinaryReader br = new BinaryReader(ms); try { uint item_count = br.ReadUInt32(); ms.Position += 2; for (uint i = 0; i < item_count; i++) { ItemClass cls = new ItemClass(); cls.ItemID = br.ReadUInt16(); cls.ServerName = Locale.ItemServ[(int)i]; cls.VisualName = Locale.ItemName[(int)i]; ms.Position += 2; // some unknown info (usually 0x01 0x00) byte flags = br.ReadByte(); if (flags == 2 || flags == 6) // usablefighter { cls.UsableFighter = true; } if (flags == 4 || flags == 6) // usablemage { cls.UsableMage = true; } byte count_mods = br.ReadByte(); ms.Position += 2; // size of modifier array in bytes (i.e. 0x0B) + id of price (0x01) cls.Price = br.ReadUInt32(); count_mods--; for (byte j = 0; j < count_mods; j++) { ItemEffect effect = new ItemEffect(); byte r_effect = br.ReadByte(); sbyte r_value = br.ReadSByte(); effect.Type1 = (ItemEffect.Effects)r_effect; effect.Value1 = r_value; cls.Effects.Add(effect); } // done reading. init auxiliary fields. int materialId = (cls.ItemID & 0xF000) >> 12; int slotId = (cls.ItemID & 0x0F00) >> 8; int classId = (cls.ItemID & 0x0070) >> 5; int optionId = (cls.ItemID & 0x001F); if (materialId == 0 && slotId == 14) // 0x0E## { cls.IsMagic = true; cls.MagicID = (cls.ItemID & 0xFF) - 1; optionId = cls.MagicID + 1; classId = 0; materialId = 0; } else { cls.IsMagic = false; cls.MagicID = -1; } cls.IsSpecial = false; cls.Material = TemplateLoader.GetMaterialById(materialId); cls.Class = TemplateLoader.GetClassById(classId); cls.Option = TemplateLoader.GetOptionByIdAndSlot(optionId, slotId); string imageNameBase = string.Format("{0:D2}{1:D2}{2}{3:D2}", materialId, slotId, classId, optionId); cls.File_Pack = new ItemPackFile("graphics/inventory/" + imageNameBase + ".16a"); if (cls.UsableFighter) { cls.File_BodyMF1 = new ItemFile("graphics/equipment/mfighter/primary/" + imageNameBase + ".256"); cls.File_BodyFF1 = new ItemFile("graphics/equipment/ffighter/primary/" + imageNameBase + ".256"); cls.File_BodyMF2 = new ItemFile("graphics/equipment/mfighter/secondary/" + imageNameBase + ".256"); cls.File_BodyFF2 = new ItemFile("graphics/equipment/ffighter/secondary/" + imageNameBase + ".256"); } if (cls.UsableMage) { cls.File_BodyMM1 = new ItemFile("graphics/equipment/mmage/primary/" + imageNameBase + ".256"); cls.File_BodyFM1 = new ItemFile("graphics/equipment/fmage/primary/" + imageNameBase + ".256"); cls.File_BodyMM2 = new ItemFile("graphics/equipment/mmage/secondary/" + imageNameBase + ".256"); cls.File_BodyFM2 = new ItemFile("graphics/equipment/fmage/secondary/" + imageNameBase + ".256"); } Classes.Add(cls); } // create a fake item with ID=0xFFFF // "gold" ItemClass gcls = new ItemClass(); gcls.ServerName = "Gold"; gcls.VisualName = "Gold"; gcls.Price = 1; gcls.ItemID = 0xFFFF; gcls.IsMagic = false; gcls.MagicID = -1; gcls.IsSpecial = false; gcls.IsMoney = true; gcls.UsableFighter = true; gcls.UsableMage = true; gcls.File_Pack = new ItemPackFile("graphics/interface/money/money.16a"); Classes.Add(gcls); } finally { ms.Close(); } }
private static Texture LoadImage(string filename, uint colormask, bool has_colormask) { MemoryStream ms = ResourceManager.OpenRead(filename); if (ms == null) { Core.Abort("Couldn't load \"{0}\"", filename); return(null); } Image image = Image.FromStream(ms); ms.Close(); Bitmap bitmap = new Bitmap(image); uint[] pixels = new uint[bitmap.Width * bitmap.Height]; int w = bitmap.Width; int h = bitmap.Height; BitmapData bmd = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb); try { unsafe { fixed(uint *opixels_fixed = pixels) { uint *opixels = opixels_fixed; uint *ipixels = (uint *)bmd.Scan0; if (!has_colormask) { for (int i = 0; i < bitmap.Height * bitmap.Width; i++) { *opixels++ = *ipixels++; } } else { for (int i = 0; i < bitmap.Height * bitmap.Width; i++) { uint opixel = *opixels++; if ((opixel & 0x00F0F0F0) == (colormask & 0x00F0F0F0)) { *ipixels++ = opixel; } else { *ipixels++ = 0; } } } } } } finally { bitmap.UnlockBits(bmd); bitmap.Dispose(); image.Dispose(); } return(new Texture(w, h, pixels)); }
public static AllodsSprite Load16(string filename) { MemoryStream ms = ResourceManager.OpenRead(filename); if (ms == null) { Core.Abort("Couldn't load \"{0}\"", filename); return(null); } BinaryReader br = new BinaryReader(ms); ms.Position = ms.Length - 4; int count = br.ReadInt32() & 0x7FFFFFFF; ms.Position = 0; AllodsSprite sprite = new AllodsSprite(); // read palette sprite.OwnPalette = null; // no such thing as the palette in .16 file Texture2D[] frames = new Texture2D[count]; int oldCount = count; for (int i = 0; i < count; i++) { uint w = br.ReadUInt32(); uint h = br.ReadUInt32(); uint ds = br.ReadUInt32(); long cpos = ms.Position; if (w > 512 || h > 512 || ds > 1000000) { Debug.LogFormat("Invalid sprite \"{0}\": NULL frame #{1}", filename, i); i--; count--; continue; } Color[] colors = new Color[w * h]; for (int j = 0; j < colors.Length; j++) { colors[j].g = 0; } int ix = 0; int iy = 0; int ids = (int)ds; while (ids > 0) { ushort ipx = br.ReadByte(); ipx |= (ushort)(ipx << 8); ipx &= 0xC03F; ids -= 1; if ((ipx & 0xC000) > 0) { if ((ipx & 0xC000) == 0x4000) { ipx &= 0x3F; SpriteAddIXIY(ref ix, ref iy, w, ipx * w); } else { ipx &= 0x3F; SpriteAddIXIY(ref ix, ref iy, w, ipx); } } else { ipx &= 0x3F; byte[] bytes = new byte[ipx]; for (int j = 0; j < ipx; j++) { bytes[j] = br.ReadByte(); } for (int j = 0; j < ipx; j++) { uint alpha1 = (bytes[j] & 0x0Fu) | ((bytes[j] & 0x0Fu) << 4); colors[iy * w + ix] = new Color(1, (float)alpha1 / 255, 0, 0); SpriteAddIXIY(ref ix, ref iy, w, 1); if (j != ipx - 1 || (bytes[bytes.Length - 1] & 0xF0) > 0) { uint alpha2 = (bytes[j] & 0xF0u) | ((bytes[j] & 0xF0u) >> 4); colors[iy * w + ix] = new Color(1, (float)alpha2 / 255, 0, 0); SpriteAddIXIY(ref ix, ref iy, w, 1); } } ids -= ipx; } } // add tex here Texture2D texture = new Texture2D((int)w, (int)h, TextureFormat.RGHalf, false); // too large, but meh. texture.filterMode = FilterMode.Point; texture.SetPixels(colors); //texture.Apply(false); frames[i] = texture; ms.Position = cpos + ds; } br.Close(); if (oldCount != count) { Array.Resize(ref frames, count); } sprite.Atlas = new Texture2D(0, 0, TextureFormat.RGHalf, false); sprite.AtlasRects = sprite.Atlas.PackTextures(frames, 0); sprite.Atlas.filterMode = FilterMode.Point; if (sprite.AtlasRects == null) { Core.Abort("Couldn't pack sprite \"{0}\"", filename); return(null); } for (int i = 0; i < frames.Length; i++) { GameObject.DestroyImmediate(frames[i]); } sprite.Sprites = new Sprite[sprite.AtlasRects.Length]; for (int i = 0; i < sprite.AtlasRects.Length; i++) { sprite.Sprites[i] = Sprite.Create(sprite.Atlas, new Rect(sprite.AtlasRects[i].x * sprite.Atlas.width, sprite.AtlasRects[i].y * sprite.Atlas.height, sprite.AtlasRects[i].width * sprite.Atlas.width, sprite.AtlasRects[i].height * sprite.Atlas.height), new Vector2(0, 0)); } return(sprite); }
public static AllodsSpriteSeparate Load256Separate(string filename, bool hasOwnPalette = true) { MemoryStream ms = ResourceManager.OpenRead(filename); if (ms == null) { Core.Abort("Couldn't load \"{0}\"", filename); return(null); } BinaryReader br = new BinaryReader(ms); ms.Position = ms.Length - 4; int count = br.ReadInt32() & 0x7FFFFFFF; ms.Position = 0; AllodsSpriteSeparate sprite = new AllodsSpriteSeparate(); // read palette if (hasOwnPalette) { sprite.OwnPalette = LoadPaletteFromStream(br); } else { sprite.OwnPalette = null; } AllodsSpriteSeparate.Frame[] frames = new AllodsSpriteSeparate.Frame[count]; int oldCount = count; for (int i = 0; i < count; i++) { uint w = br.ReadUInt32(); uint h = br.ReadUInt32(); uint ds = br.ReadUInt32(); long cpos = ms.Position; if (w > 512 || h > 512 || ds > 1000000) { //Core.Abort("Invalid sprite \"{0}\": NULL frame #{1}", filename, i); Debug.LogFormat("Invalid sprite \"{0}\": NULL frame #{1}", filename, i); i--; count--; continue; } Color[] colors = new Color[w * h]; for (int j = 0; j < colors.Length; j++) { colors[j].g = 0; } int ix = 0; int iy = 0; int ids = (int)ds; while (ids > 0) { ushort ipx = br.ReadByte(); ipx |= (ushort)(ipx << 8); ipx &= 0xC03F; ids--; if ((ipx & 0xC000) > 0) { if ((ipx & 0xC000) == 0x4000) { ipx &= 0x3F; SpriteAddIXIY(ref ix, ref iy, w, ipx * w); } else { ipx &= 0x3F; SpriteAddIXIY(ref ix, ref iy, w, ipx); } } else { ipx &= 0x3F; for (int j = 0; j < ipx; j++) { byte ss = br.ReadByte(); //uint px = (ss << 16) | (ss << 8) | (ss) | 0xFF000000; colors[iy * w + ix] = new Color((float)ss / 255f, 1, 0, 0); SpriteAddIXIY(ref ix, ref iy, w, 1); } ids -= ipx; } } // add tex here //Texture2D texture = new Texture2D((int)w, (int)h, TextureFormat.RGHalf, false); // too large, but meh. //texture.filterMode = FilterMode.Point; //texture.SetPixels(colors); //texture.Apply(false); frames[i] = new AllodsSpriteSeparate.Frame(sprite); frames[i].Width = (int)w; frames[i].Height = (int)h; frames[i]._Colors = colors; frames[i]._Texture = null; ms.Position = cpos + ds; } br.Close(); if (oldCount != count) { Array.Resize(ref frames, count); } sprite.Frames = frames; return(sprite); }
private static Texture2D LoadImage(string filename, ImageType type, uint colormask, bool has_colormask) { MemoryStream ms = ResourceManager.OpenRead(filename); if (ms == null) { Core.Abort("Couldn't load \"{0}\"", filename); return(null); } Texture2D texture; if (type == ImageType.Unity) { texture = new Texture2D(0, 0); texture.filterMode = FilterMode.Point; byte[] imageData = new byte[ms.Length]; ms.Read(imageData, 0, (int)ms.Length); texture.LoadImage(imageData); ms.Close(); if (has_colormask) { //colormask &= 0xF0F0F0; Color32[] colors = texture.GetPixels32(); for (int i = 0; i < colors.Length; i++) { uint pixel = ((uint)colors[i].r << 16) | ((uint)colors[i].g << 8) | colors[i].b | ((uint)colors[i].a << 24); if (/*(pixel & 0xF0F0F0) == colormask*/ (pixel & 0xFFFFFF) == colormask) { colors[i].a = 0; } } texture.SetPixels32(colors); texture.Apply(false); } } else { // load bmp manually. BinaryReader br = new BinaryReader(ms); try { ushort bfh_signature = br.ReadUInt16(); if (bfh_signature != 0x4D42) // BM { Core.Abort("\"{0}\" is not a valid BMP image", filename); return(null); } ms.Position = 0x0A; uint bfh_pixeldata = br.ReadUInt32(); //Debug.Log(String.Format("pixel data at {0}", bfh_pixeldata)); uint bi_version = br.ReadUInt32(); if (bi_version == 12) { Core.Abort("\"{0}\": CORE BMP images not supported", filename); return(null); } if (bi_version != 40) { Core.Abort("\"{0}\": version {1} is not supported", filename, bi_version); return(null); } int bi_width = br.ReadInt32(); int bi_height = br.ReadInt32(); br.BaseStream.Position += 2; // short bi_planes = br.ReadInt16(); short bi_bitcount = br.ReadInt16(); br.BaseStream.Position += 4; // uint bi_compression = br.ReadUInt32(); br.BaseStream.Position += 4; // uint bi_sizeimage = br.ReadUInt32(); br.BaseStream.Position += 4; // int bi_xpelspermeter = br.ReadInt32(); br.BaseStream.Position += 4; // int bi_ypelspermeter = br.ReadInt32(); br.BaseStream.Position += 4; // uint bi_clrused = br.ReadUInt32(); br.BaseStream.Position += 4; // uint bi_clrimportant = br.ReadUInt32(); //Debug.Log(String.Format("pixel data at {0}, {1}x{2}, bits: {3}", bfh_pixeldata, bi_width, bi_height, bi_bitcount)); texture = new Texture2D(bi_width, bi_height, TextureFormat.ARGB32, false); texture.filterMode = FilterMode.Point; Color32[] colors = new Color32[bi_width * bi_height]; if (bi_bitcount == 24) // read RGB { int i = 0; for (int y = bi_height - 1; y >= 0; y--) { ms.Position = bfh_pixeldata + bi_width * y * 3; for (int x = 0; x < bi_width; x++) { byte b = br.ReadByte(); byte g = br.ReadByte(); byte r = br.ReadByte(); colors[i++] = new Color32(r, g, b, 255); } } } else if (bi_bitcount == 32) // read RGBA { int i = 0; for (int y = bi_height - 1; y >= 0; y--) { ms.Position = bfh_pixeldata + bi_width * y * 4; for (int x = 0; x < bi_width; x++) { byte b = br.ReadByte(); byte g = br.ReadByte(); byte r = br.ReadByte(); br.BaseStream.Position += 1; // byte a = br.ReadByte(); colors[i++] = new Color32(r, g, b, 255); } } } else if (bi_bitcount == 8) { // also read palette // current offset (after header) = 0x36 Color32[] colormap = new Color32[256]; for (int i = 0; i < 256; i++) { byte b = br.ReadByte(); byte g = br.ReadByte(); byte r = br.ReadByte(); br.BaseStream.Position += 1; // byte a = br.ReadByte(); colormap[i] = new Color32(r, g, b, 255); // ignore alpha value here, it doesn't make sense } int j = 0; for (int y = bi_height - 1; y >= 0; y--) { ms.Position = bfh_pixeldata + bi_width * y; for (int x = 0; x < bi_width; x++) { colors[j++] = colormap[br.ReadByte()]; } } } else { Core.Abort("\"{0}\": bad bit count: {1}", filename, bi_bitcount); return(null); } texture.SetPixels32(colors); if (has_colormask) { //colormask &= 0xF0F0F0; for (int i = 0; i < colors.Length; i++) { uint pixel = ((uint)colors[i].r << 16) | ((uint)colors[i].g << 8) | colors[i].b | ((uint)colors[i].a << 24); if (/*(pixel & 0xF0F0F0) == colormask*/ (pixel & 0xFFFFFF) == colormask) { colors[i].a = 0; } } texture.SetPixels32(colors); } texture.Apply(false); } finally { br.Close(); } } return(texture); }
public static TextureList Load16A(string filename) { MemoryStream ms = ResourceManager.OpenRead(filename); if (ms == null) { Core.Abort("Couldn't load \"{0}\"", filename); return(null); } BinaryReader msb = new BinaryReader(ms); ms.Position = ms.Length - 4; int count = msb.ReadInt32() & 0x7FFFFFFF; ms.Position = 0; uint[] colors = new uint[256]; for (int i = 0; i < 256; i++) { colors[i] = msb.ReadUInt32(); } Palette palette = new Palette(colors); List <Texture> textures = new List <Texture>(); for (int i = 0; i < count; i++) { uint w = msb.ReadUInt32(); uint h = msb.ReadUInt32(); uint ds = msb.ReadUInt32(); long cpos = ms.Position; if (w == 0 || h == 0 || ds == 0) { Core.Abort("Invalid sprite \"{0}\": NULL frame #{1}", filename, i); return(null); } uint[] pixels = new uint[w * h]; int ix = 0; int iy = 0; int ids = (int)ds; while (ids > 0) { ushort ipx = msb.ReadUInt16(); ipx &= 0xC03F; ids -= 2; if ((ipx & 0xC000) > 0) { if ((ipx & 0xC000) == 0x4000) { ipx &= 0x3F; SpriteAddIXIY(ref ix, ref iy, w, ipx * w); } else { ipx &= 0x3F; SpriteAddIXIY(ref ix, ref iy, w, ipx); } } else { ipx &= 0x3F; for (int j = 0; j < ipx; j++) { uint ss = msb.ReadUInt16(); uint alpha = (((ss & 0xFF00) >> 9) & 0x0F) + (((ss & 0xFF00) >> 5) & 0xF0); uint idx = ((ss & 0xFF00) >> 1) + ((ss & 0x00FF) >> 1); idx &= 0xFF; alpha &= 0xFF; uint px = (idx << 16) | (idx << 8) | (idx) | (alpha << 24); pixels[iy * w + ix] = px; SpriteAddIXIY(ref ix, ref iy, w, 1); } ids -= ipx * 2; } } textures.Add(new Texture((int)w, (int)h, pixels)); ms.Position = cpos + ds; } msb.Close(); return(new TextureList(palette, textures)); }
public static TextureList Load16(string filename) { MemoryStream ms = ResourceManager.OpenRead(filename); if (ms == null) { Core.Abort("Couldn't load \"{0}\"", filename); return(null); } BinaryReader msb = new BinaryReader(ms); ms.Position = ms.Length - 4; int count = msb.ReadInt32() & 0x7FFFFFFF; ms.Position = 0; Palette palette = null; List <Texture> textures = new List <Texture>(); for (int i = 0; i < count; i++) { uint w = msb.ReadUInt32(); uint h = msb.ReadUInt32(); uint ds = msb.ReadUInt32(); long cpos = ms.Position; if (w == 0 || h == 0 || ds == 0) { Core.Abort("Invalid sprite \"{0}\": NULL frame #{1}", filename, i); return(null); } uint[] pixels = new uint[w * h]; int ix = 0; int iy = 0; int ids = (int)ds; while (ids > 0) { ushort ipx = msb.ReadByte(); ipx |= (ushort)(ipx << 8); ipx &= 0xC03F; ids -= 1; if ((ipx & 0xC000) > 0) { if ((ipx & 0xC000) == 0x4000) { ipx &= 0x3F; SpriteAddIXIY(ref ix, ref iy, w, ipx * w); } else { ipx &= 0x3F; SpriteAddIXIY(ref ix, ref iy, w, ipx); } } else { ipx &= 0x3F; byte[] bytes = new byte[ipx]; for (int j = 0; j < ipx; j++) { bytes[j] = msb.ReadByte(); } for (int j = 0; j < ipx; j++) { uint alpha1 = (bytes[j] & 0x0Fu) | ((bytes[j] & 0x0Fu) << 4); uint px1 = 0x00FFFFFF | (alpha1 << 24); pixels[iy * w + ix] = px1; SpriteAddIXIY(ref ix, ref iy, w, 1); if (j != ipx - 1 || (bytes[bytes.Length - 1] & 0xF0) > 0) { uint alpha2 = (bytes[j] & 0xF0u) | ((bytes[j] & 0xF0u) >> 4); uint px2 = 0x00FFFFFF | (alpha2 << 24); pixels[iy * w + ix] = px2; SpriteAddIXIY(ref ix, ref iy, w, 1); } } ids -= ipx; } } textures.Add(new Texture((int)w, (int)h, pixels)); ms.Position = cpos + ds; } msb.Close(); return(new TextureList(palette, textures)); }
public Shader(string filename) { FileName = filename; MemoryStream ms = ResourceManager.OpenRead(filename); if (ms == null) { Core.Abort("Couldn't load \"{0}\"", filename); return; } string code_vertex = ""; string code_fragment = ""; bool in_vertex = false; bool in_fragment = false; List <string> includes = new List <string>(); List <string> lines = new List <string>(); StreamReader sr = new StreamReader(ms); while (!sr.EndOfStream) { lines.Add(sr.ReadLine()); } sr.Close(); string last_file = filename; for (int i = 0; i < lines.Count; i++) { string line = lines[i].Trim(); if (line.StartsWith("#shader ") || line.StartsWith("#include ") || line.StartsWith("#returnto ")) { string[] linep = line.Split(new char[] { ' ' }, 2, StringSplitOptions.RemoveEmptyEntries); if (linep.Length < 2) { continue; } if (linep[0] == "#shader") { string shader_type = linep[1].ToLower(); if (shader_type == "vertex") { in_vertex = true; in_fragment = false; } else if (shader_type == "fragment") { in_vertex = false; in_fragment = true; } else { Core.Abort("Unknown shader type \"{0}\" in \"{1}\"", shader_type, last_file); return; } continue; } else if (linep[0] == "#returnto") { string ret_filename = linep[1]; last_file = ret_filename; continue; } else if (linep[0] == "#include") { string inc_filename = linep[1]; MemoryStream msi = ResourceManager.OpenRead(inc_filename); if (msi == null) { Core.Abort("Couldn't load \"{0}\" (included from \"{1}\")", inc_filename, last_file); return; } int inumlines = i + 1; sr = new StreamReader(msi); while (!sr.EndOfStream) { lines.Insert(inumlines++, sr.ReadLine()); } sr.Close(); lines.Insert(inumlines++, "#returnto " + last_file); last_file = inc_filename; sr.Close(); continue; } } if (in_vertex) { code_vertex += line + "\n"; } if (in_fragment) { code_fragment += line + "\n"; } } if (code_fragment.Length <= 0) { Core.Abort("No fragment shader found in \"{0}\"", filename); return; } if (code_vertex.Length <= 0) { Core.Abort("No vertex shader found in \"{0}\"", filename); return; } CodeVertex = code_vertex; CodeFragment = code_fragment; }
private void Compile() { if (ContextVersion != AllodsWindow.ContextVersion) { int shader_vx = GL.CreateShader(ShaderType.VertexShader); GL.ShaderSource(shader_vx, CodeVertex); GL.CompileShader(shader_vx); // check if it compiled int shader_vx_success = 0; GL.GetShader(shader_vx, ShaderParameter.CompileStatus, out shader_vx_success); if (shader_vx_success == 0) { string log = GL.GetShaderInfoLog(shader_vx); GL.DeleteShader(shader_vx); Console.WriteLine(" ! Compilation failed:\n{0}\n", log); Core.Abort("Failed to compile vertex shader from \"{0}\"\nError log:\n{1}", FileName, log); return; } int shader_fr = GL.CreateShader(ShaderType.FragmentShader); GL.ShaderSource(shader_fr, CodeFragment); GL.CompileShader(shader_fr); // check if it compiled int shader_fr_success = 0; GL.GetShader(shader_fr, ShaderParameter.CompileStatus, out shader_fr_success); if (shader_fr_success == 0) { string log = GL.GetShaderInfoLog(shader_fr); GL.DeleteShader(shader_fr); GL.DeleteShader(shader_vx); Console.WriteLine(" ! Compilation failed:\n{0}\n", log); Core.Abort("Failed to compile fragment shader from \"{0}\"\nError log:\n{1}", FileName, log); return; } ShaderGL = GL.CreateProgram(); GL.AttachShader(ShaderGL, shader_vx); GL.AttachShader(ShaderGL, shader_fr); GL.LinkProgram(ShaderGL); int program_success = 0; GL.GetProgram(ShaderGL, GetProgramParameterName.LinkStatus, out program_success); if (program_success == 0) { string log = GL.GetProgramInfoLog(ShaderGL); GL.DeleteProgram(ShaderGL); GL.DeleteShader(shader_fr); GL.DeleteShader(shader_vx); ShaderGL = 0; Console.WriteLine(" ! Linking failed:\n{0}\n", log); Core.Abort("Failed to link shader program from \"{0}\"\nError log:\n{1}", FileName, log); return; } GL.DetachShader(ShaderGL, shader_fr); GL.DeleteShader(shader_fr); GL.DetachShader(ShaderGL, shader_vx); GL.DeleteShader(shader_vx); ContextVersion = AllodsWindow.ContextVersion; } }