protected static void Swap(ref Texture2D texture)
 {
     try
     {
         if (texture == null)
         {
             if (overrideData.OriginalTexture2D != null)
             {
                 Texture2DOverride.UnloadTexture2D(ref overrideData);
             }
         }
         else
         {
             if (overrideData.OriginalTexture2D != null)
             {
                 int instanceID = texture.GetInstanceID();
                 if (overrideData.InstanceID == instanceID)
                 {
                     texture = overrideData.OverrideTextureCache;
                     return;
                 }
                 Texture2DOverride.UnloadTexture2D(ref overrideData);
             }
             Texture2DOverride.LoadTexture2D(texture.name, texture, out overrideData);
             if (overrideData.OverrideTexture2D != null)
             {
                 if ((texture.width != overrideData.OverrideTexture2D.width) || (texture.height != overrideData.OverrideTexture2D.height))
                 {
                     texture.Resize(overrideData.OverrideTexture2D.width, overrideData.OverrideTexture2D.height);
                 }
                 texture.SetPixels32(overrideData.OverrideTexture2D.GetPixels32());
                 texture.Apply();
                 overrideData.OverrideTextureCache = texture;
             }
         }
     }
     catch (Exception exception)
     {
         IniSettings.Error("CursorOverride:\n" + exception.ToString());
     }
 }
 public static bool LoadDXT(byte[] ddsBytes, out Texture2D texture)
 {
     try
     {
         if (ddsBytes[4] != 0x7c)
         {
             throw new Exception("Invalid DDS DXTn texture. Unable to read");
         }
         TextureFormat format = (TextureFormat)0;
         if (((ddsBytes[0x54] == 0x44) && (ddsBytes[0x55] == 0x58)) && (ddsBytes[0x56] == 0x54))
         {
             if (ddsBytes[0x57] == 0x31)
             {
                 format = TextureFormat.DXT1;
             }
             else if (ddsBytes[0x57] == 0x35)
             {
                 format = TextureFormat.DXT5;
             }
         }
         if ((format != TextureFormat.DXT1) && (format != TextureFormat.DXT5))
         {
             throw new Exception("Invalid TextureFormat. Only DXT1 and DXT5 formats are supported by this method.");
         }
         int    height = (ddsBytes[13] * 0x100) + ddsBytes[12];
         int    width  = (ddsBytes[0x11] * 0x100) + ddsBytes[0x10];
         byte[] dst    = new byte[ddsBytes.Length - 0x80];
         Buffer.BlockCopy(ddsBytes, 0x80, dst, 0, ddsBytes.Length - 0x80);
         texture = new Texture2D(width, height, format, false);
         texture.LoadRawTextureData(dst);
         texture.Apply();
         return(true);
     }
     catch
     {
         texture = null;
         return(false);
     }
 }
Пример #3
0
    public static bool LoadPSD(byte[] psdBytes, out Texture2D texture)
    {
        bool flag;

        try
        {
            byte[] buffer;
            int    index = 0;
            if (psdBytes == null)
            {
                throw new ArgumentNullException("psdBytes");
            }
            byte[] bytes = Encoding.UTF8.GetBytes("8BPS");
            if (psdBytes.Length < bytes.Length)
            {
                throw new ArgumentOutOfRangeException();
            }
            for (int i = 0; i < bytes.Length; i++)
            {
                if (psdBytes[i] != bytes[i])
                {
                    throw new FormatException();
                }
            }
            index = Read(psdBytes, bytes.Length, 2, out buffer, true);
            if (BitConverter.ToInt16(buffer, 0) != 1)
            {
                throw new FormatException();
            }
            index += 6;
            index  = Read(psdBytes, index, 2, out buffer, true);
            short num2 = BitConverter.ToInt16(buffer, 0);
            if ((num2 != 3) && (num2 != 4))
            {
                throw new NotSupportedException();
            }
            index = Read(psdBytes, index, 4, out buffer, true);
            int height = BitConverter.ToInt32(buffer, 0);
            index = Read(psdBytes, index, 4, out buffer, true);
            int width = BitConverter.ToInt32(buffer, 0);
            index = Read(psdBytes, index, 2, out buffer, true);
            if (BitConverter.ToInt16(buffer, 0) != 8)
            {
                throw new NotSupportedException();
            }
            index = Read(psdBytes, index, 2, out buffer, true);
            short num5 = BitConverter.ToInt16(buffer, 0);
            index = Read(psdBytes, index, 4, out buffer, true);
            int num6 = BitConverter.ToInt32(buffer, 0);
            index += num6;
            index  = Read(psdBytes, index, 4, out buffer, true);
            int num7 = BitConverter.ToInt32(buffer, 0);
            index += num7;
            index  = Read(psdBytes, index, 4, out buffer, true);
            int num8 = BitConverter.ToInt32(buffer, 0);
            index += num8;
            index  = Read(psdBytes, index, 2, out buffer, true);
            short         num9   = BitConverter.ToInt16(buffer, 0);
            TextureFormat format = (TextureFormat)0;
            switch (num5)
            {
            case 0:
                throw new NotSupportedException();

            case 1:
                throw new NotSupportedException();

            case 2:
                throw new NotSupportedException();

            case 3:
                format = TextureFormat.RGBA32;
                break;

            case 4:
                throw new NotSupportedException();

            case 7:
                throw new NotSupportedException();

            case 8:
                throw new NotSupportedException();

            case 9:
                throw new NotSupportedException();

            default:
                throw new NotSupportedException();
            }
            switch (num9)
            {
            case 0:
                throw new NotSupportedException();

            case 1:
            {
                short[] numArray = new short[num2 * height];
                for (int j = 0; j < numArray.Length; j++)
                {
                    index       = Read(psdBytes, index, 2, out buffer, true);
                    numArray[j] = BitConverter.ToInt16(buffer, 0);
                }
                int       num11  = 0;
                int       num12  = 0;
                Color32[] colors = new Color32[width * height];
                for (int k = 0; k < numArray.Length; k++)
                {
                    if (num11 == 4)
                    {
                        throw new NotSupportedException();
                    }
                    index = Read(psdBytes, index, numArray[k], out buffer, false);
                    for (int m = 0; m < buffer.Length; m++)
                    {
                        sbyte num16 = (sbyte)buffer[m];
                        if (num16 != -128)
                        {
                            if (num16 < 0)
                            {
                                m++;
                                int num17 = 1 - num16;
                                for (int n = 0; n < num17; n++)
                                {
                                    Color32 color = colors[num12];
                                    switch (num11)
                                    {
                                    case 0:
                                        color.a = 0xff;
                                        color.r = buffer[m];
                                        break;

                                    case 1:
                                        color.g = buffer[m];
                                        break;

                                    case 2:
                                        color.b = buffer[m];
                                        break;

                                    case 3:
                                        color.a = buffer[m];
                                        break;
                                    }
                                    colors[num12++] = color;
                                }
                            }
                            else
                            {
                                int num19 = 1 + num16;
                                for (int num20 = 0; num20 < num19; num20++)
                                {
                                    Color32 color2 = colors[num12];
                                    switch (num11)
                                    {
                                    case 0:
                                        color2.a = 0xff;
                                        color2.r = buffer[++m];
                                        break;

                                    case 1:
                                        color2.g = buffer[++m];
                                        break;

                                    case 2:
                                        color2.b = buffer[++m];
                                        break;

                                    case 3:
                                        color2.a = buffer[++m];
                                        break;
                                    }
                                    colors[num12++] = color2;
                                }
                            }
                        }
                    }
                    if (num12 == colors.Length)
                    {
                        num11++;
                        num12 = 0;
                    }
                }
                texture = new Texture2D(width, height, format, false);
                texture.SetPixels32(colors);
                texture.Apply();
                return(true);
            }

            case 2:
                throw new NotSupportedException();

            case 3:
                throw new NotSupportedException();
            }
            throw new NotSupportedException();
        }
        catch
        {
            texture = null;
            flag    = false;
        }
        return(flag);
    }