public static Texture2D Load(string MainTexture, string AltTexture = null)
        {
            //return null;
            tagVTFHEADER VTF_Header = new tagVTFHEADER();
            bool         m_Mipmaps  = true;
            string       Path       = string.Empty;

            if (File.Exists(System.IO.Path.Combine(ConfigLoader._PakPath, ConfigLoader.LevelName + "_pakFile/materials/" + MainTexture + ".vtf")))
            {
                Path = System.IO.Path.Combine(ConfigLoader._PakPath, ConfigLoader.LevelName + "_pakFile/materials/" + MainTexture + ".vtf");
            }
            else
            {
                for (int i = 0; i < ConfigLoader.ModFolders.Length; i++)
                {
                    if (File.Exists(ConfigLoader.GamePath + "/" + ConfigLoader.ModFolders[i] + "/materials/" + MainTexture + ".vtf"))
                    {
                        Path = ConfigLoader.GamePath + "/" + ConfigLoader.ModFolders[i] + "/materials/" + MainTexture + ".vtf";
                    }
                }
            }

            if (string.IsNullOrEmpty(Path))
            {
                if (AltTexture != null)
                {
                    return(Load(AltTexture));
                }

                Debug.Log(String.Format("{0}: File not found", MainTexture + ".vtf"));
                return(Load("debug/debugempty", null));
                //return null;
            }

            MemUtils VTFFileReader = new MemUtils(File.OpenRead(Path));

            VTFFileReader.ReadType(ref VTF_Header);

            if (VTF_Header.Signature != 0x00465456)
            {
                Debug.Log(String.Format("{0}: File signature does not match 'VTF'", MainTexture + ".vtf"));
                return(null);
            }

            int[] UiBytesPerPixels =
            {
                4, 4, 3, 3, 2, 1,
                2, 1, 1, 3, 3, 4,
                4, 1, 1, 1, 4, 2,
                2, 2, 1, 2, 2, 4,
                8, 8, 4
            };

            int           ImageSize = VTF_Header.Width * VTF_Header.Height * UiBytesPerPixels[(int)VTF_Header.HighResImageFormat];
            TextureFormat InternalFormat;

            switch (VTF_Header.HighResImageFormat)
            {
            case VTFImageFormat.IMAGE_FORMAT_DXT1:
            case VTFImageFormat.IMAGE_FORMAT_DXT1_ONEBITALPHA:
                ImageSize      = ((VTF_Header.Width + 3) / 4) * ((VTF_Header.Height + 3) / 4) * 8;
                InternalFormat = TextureFormat.DXT1;
                break;

            case VTFImageFormat.IMAGE_FORMAT_DXT3:
            case VTFImageFormat.IMAGE_FORMAT_DXT5:
                ImageSize      = ((VTF_Header.Width + 3) / 4) * ((VTF_Header.Height + 3) / 4) * 16;
                InternalFormat = TextureFormat.DXT5;
                break;

            case VTFImageFormat.IMAGE_FORMAT_RGB888:
            case VTFImageFormat.IMAGE_FORMAT_RGB888_BLUESCREEN:
            case VTFImageFormat.IMAGE_FORMAT_BGR888:
            case VTFImageFormat.IMAGE_FORMAT_BGR888_BLUESCREEN:
                InternalFormat = TextureFormat.RGB24;
                break;

            case VTFImageFormat.IMAGE_FORMAT_RGBA8888:
                InternalFormat = TextureFormat.RGBA32;
                break;

            case VTFImageFormat.IMAGE_FORMAT_ARGB8888:
                InternalFormat = TextureFormat.ARGB32;
                break;

            case VTFImageFormat.IMAGE_FORMAT_BGRA8888:
            case VTFImageFormat.IMAGE_FORMAT_BGRX8888:
                InternalFormat = TextureFormat.BGRA32;
                break;

            /*case VTFImageFormat.IMAGE_FORMAT_UV88:
             *                      InternalFormat = TextureFormat.RG16;
             *                      break;*/


            default:
                Debug.Log(String.Format("{0}: Unsupported format: {1}", MainTexture + ".vtf", VTF_Header.HighResImageFormat));
                VTFFileReader.Close();
                return(null);
            }

            Frames = new Texture2D[VTF_Header.Frames];

            for (Int32 i = 0; i < VTF_Header.Frames; i++)
            {
                Texture2D VTF_Texture = new Texture2D(VTF_Header.Width, VTF_Header.Height, InternalFormat, false);

                VTFFileReader.BaseStream.Seek(VTFFileReader.BaseStream.Length - ImageSize * (VTF_Header.Frames - i), SeekOrigin.Begin);
                Byte[] VTFFile = VTFFileReader.ReadBytes(ImageSize);
                if (VTF_Header.HighResImageFormat == VTFImageFormat.IMAGE_FORMAT_BGR888 || VTF_Header.HighResImageFormat == VTFImageFormat.IMAGE_FORMAT_BGR888_BLUESCREEN)
                {
                    for (Int32 j = 0; j < VTFFile.Length - 1; j += 3)
                    {
                        Byte PixelX = VTFFile[j];
                        VTFFile[j]     = VTFFile[j + 2];
                        VTFFile[j + 2] = PixelX;
                    }
                }

                VTF_Texture.LoadRawTextureData(VTFFile);

                switch (VTF_Header.HighResImageFormat)
                {
                case VTFImageFormat.IMAGE_FORMAT_DXT1:
                case VTFImageFormat.IMAGE_FORMAT_DXT1_ONEBITALPHA:
                    Frames[i] = new Texture2D(VTF_Header.Width, VTF_Header.Height, TextureFormat.RGB24, true);
                    break;

                case VTFImageFormat.IMAGE_FORMAT_DXT3:
                case VTFImageFormat.IMAGE_FORMAT_DXT5:
                    Frames[i] = new Texture2D(VTF_Header.Width, VTF_Header.Height, TextureFormat.RGBA32, true);
                    break;

                default:
                    Frames[i] = new Texture2D(VTF_Header.Width, VTF_Header.Height, InternalFormat, true);
                    break;
                }

                if ((VTF_Header.Flags & (VTFImageFlags.TEXTUREFLAGS_CLAMPS | VTFImageFlags.TEXTUREFLAGS_CLAMPT)) != 0)
                {
                    Frames[i].wrapMode = TextureWrapMode.Clamp;
                }

                if (m_Mipmaps)
                {
                    Color32[] pixels = Frames[i].GetPixels32();

                    switch (VTF_Header.HighResImageFormat)
                    {
                    case VTFImageFormat.IMAGE_FORMAT_DXT1:
                    case VTFImageFormat.IMAGE_FORMAT_DXT1_ONEBITALPHA:
                        Frames[i] = new Texture2D(VTF_Header.Width, VTF_Header.Height, TextureFormat.RGB24, true);
                        break;

                    case VTFImageFormat.IMAGE_FORMAT_DXT3:
                    case VTFImageFormat.IMAGE_FORMAT_DXT5:
                        Frames[i] = new Texture2D(VTF_Header.Width, VTF_Header.Height, TextureFormat.RGBA32, true);
                        break;

                    default:
                        Frames[i] = new Texture2D(VTF_Header.Width, VTF_Header.Height, InternalFormat, true);
                        break;
                    }

                    Frames[i].SetPixels32(pixels);
                }

                Frames[i].SetPixels32(VTF_Texture.GetPixels32());
                Frames[i].Apply();

                if (m_Mipmaps)
                {
                    Frames[i].Compress(true);
                }

                Frames[i].Compress(true);
                UnityEngine.Object.DestroyImmediate(VTF_Texture);
            }
            VTFFileReader.BaseStream.Dispose();
            Frames[0].name = Path;

            return(Frames[0]);
        }
        public static Transform Load(String ModelName)
        {
            Clear();
            //return new GameObject(ModelName).transform;
            if (ModelsInRAM == null)
            {
                ModelsInRAM = new Dictionary <string, Transform>();
            }

            String OpenPath = String.Empty;

            ModelName = ModelName
                        .Replace(".mdl", "")
                        .Replace("models/", "");

            if (ModelsInRAM.ContainsKey(ModelName))
            {
                return(UnityEngine.Object.Instantiate(ModelsInRAM[ModelName]));
            }

            if (File.Exists(Path.Combine(ConfigLoader._PakPath, ConfigLoader.LevelName + "_pakFile/models/" + ModelName + ".mdl")))
            {
                OpenPath = Path.Combine(ConfigLoader._PakPath, ConfigLoader.LevelName + "_pakFile/models/" + ModelName);
            }
            else
            {
                for (Int32 i = 0; i < ConfigLoader.ModFolders.Length; i++)
                {
                    if (File.Exists(ConfigLoader.GamePath + "/" + ConfigLoader.ModFolders[i] + "/models/" + ModelName + ".mdl"))
                    {
                        OpenPath = ConfigLoader.GamePath + "/" + ConfigLoader.ModFolders[i] + "/models/" + ModelName;
                    }
                }
            }

            ModelObject = new GameObject(ModelName);

            if (!File.Exists(OpenPath + ".mdl"))
            {
                Debug.Log(String.Format("{0}: File not found", ModelName + ".mdl"));
                return(Load("error"));
                //return ModelObject.transform;
            }

            if (!File.Exists(OpenPath + ".vvd"))
            {
                Debug.Log(String.Format("{0}: File not found", ModelName + ".vvd"));
                return(ModelObject.transform);
            }

            if (!File.Exists(OpenPath + ".dx90.vtx"))
            {
                Debug.Log(String.Format("{0}: File not found", ModelName + ".dx90.vtx"));
                return(ModelObject.transform);
            }

            try
            {
                ModelFileLoader = new MemUtils(File.OpenRead(OpenPath + ".mdl"));
                ModelFileLoader.ReadType(ref MDL_Header);
                ParseMdlFile();

                ModelFileLoader = new MemUtils(File.OpenRead(OpenPath + ".vvd"));
                ModelFileLoader.ReadType(ref VVD_Header);
                ParseVvdFile();

                ModelFileLoader = new MemUtils(File.OpenRead(OpenPath + ".dx90.vtx"));
                ModelFileLoader.ReadType(ref VTX_Header);
                ParseVtxFile();
            }
            catch (Exception ErrorInfo)
            {
                Debug.LogError(ErrorInfo.ToString());
                return(ModelObject.transform);
            }

            if (ConfigLoader.DrawArmature)
            {
                BonesInfo = ModelObject.AddComponent <MDLArmatureInfo>();
                DrawArmature();
            }

            ModelsInRAM.Add(ModelName, ModelObject.transform);
            ModelFileLoader.BaseStream.Dispose();
            return(ModelObject.transform);
        }
        public static void Load(string BSPName)
        {
            if (!File.Exists((ConfigLoader.GamePath + "/" + ConfigLoader.ModFolders[0] + "/maps/" + BSPName + ".bsp")))
            {
                throw new FileNotFoundException(String.Format("Map file ({0}) wasn't found in the ({1}) mod-folder. Check weather a path is valid.", BSPName, ConfigLoader.ModFolders[0]));
            }

            BSPFileReader = new MemUtils(File.OpenRead(ConfigLoader.GamePath + "/" + ConfigLoader.ModFolders[0] + "/maps/" + BSPName + ".bsp"));
            BSPFileReader.ReadType(ref BSP_Header);

            if (BSP_Header.Ident != 0x50534256)
            {
                throw new FileLoadException(String.Format("{0}: File signature does not match 'VBSP'", BSPName));
            }

            if (BSP_Header.Version < 19 || BSP_Header.Version > 21)
            {
                throw new FileLoadException(String.Format("{0}: BSP version ({1}) isn't supported", BSPName, BSP_Header.Version));
            }

            if (BSP_Header.Lumps[0].FileOfs == 0)
            {
                Debug.Log("Found Left 4 Dead 2 header");
                for (Int32 i = 0; i < BSP_Header.Lumps.Length; i++)
                {
                    BSP_Header.Lumps[i].FileOfs = BSP_Header.Lumps[i].FileLen;
                    BSP_Header.Lumps[i].FileLen = BSP_Header.Lumps[i].Version;
                }
            }

            if (BSP_Header.Lumps[58].FileLen / 56 <= 0)
            {
                BSP_Faces = new dface_t[BSP_Header.Lumps[7].FileLen / 56];
                BSPFileReader.ReadArray(ref BSP_Faces, BSP_Header.Lumps[7].FileOfs);
            }
            else
            {
                BSP_Faces = new dface_t[BSP_Header.Lumps[58].FileLen / 56];
                BSPFileReader.ReadArray(ref BSP_Faces, BSP_Header.Lumps[58].FileOfs);
            }

            BSP_Models = new dmodel_t[BSP_Header.Lumps[14].FileLen / 48];
            BSPFileReader.ReadArray(ref BSP_Models, BSP_Header.Lumps[14].FileOfs);

            BSP_Overlay = new doverlay_t[BSP_Header.Lumps[45].FileLen / 352];
            BSPFileReader.ReadArray(ref BSP_Overlay, BSP_Header.Lumps[45].FileOfs);

            BSP_DispInfo = new ddispinfo_t[BSP_Header.Lumps[26].FileLen / 176];
            BSPFileReader.ReadArray(ref BSP_DispInfo, BSP_Header.Lumps[26].FileOfs);

            BSP_DispVerts = new dDispVert[BSP_Header.Lumps[33].FileLen / 20];
            BSPFileReader.ReadArray(ref BSP_DispVerts, BSP_Header.Lumps[33].FileOfs);

            BSP_TexInfo = new texinfo_t[BSP_Header.Lumps[18].FileLen / 12];
            BSPFileReader.ReadArray(ref BSP_TexInfo, BSP_Header.Lumps[18].FileOfs);

            BSP_TexInfo = new texinfo_t[BSP_Header.Lumps[6].FileLen / 72];
            BSPFileReader.ReadArray(ref BSP_TexInfo, BSP_Header.Lumps[6].FileOfs);

            BSP_TexData = new dtexdata_t[BSP_Header.Lumps[2].FileLen / 32];
            BSPFileReader.ReadArray(ref BSP_TexData, BSP_Header.Lumps[2].FileOfs);

            BSP_TextureStringData = new String[BSP_Header.Lumps[44].FileLen / 4];

            Int32[] BSP_TextureStringTable = new Int32[BSP_Header.Lumps[44].FileLen / 4];
            BSPFileReader.ReadArray(ref BSP_TextureStringTable, BSP_Header.Lumps[44].FileOfs);

            for (Int32 i = 0; i < BSP_TextureStringTable.Length; i++)
            {
                BSP_TextureStringData[i] = BSPFileReader.ReadNullTerminatedString(BSP_Header.Lumps[43].FileOfs + BSP_TextureStringTable[i]);
            }

            BSP_Edges = new dedge_t[BSP_Header.Lumps[12].FileLen / 4];
            BSPFileReader.ReadArray(ref BSP_Edges, BSP_Header.Lumps[12].FileOfs);

            BSPFileReader.BaseStream.Seek(BSP_Header.Lumps[3].FileOfs, SeekOrigin.Begin);
            BSP_Vertices = new Vector3[BSP_Header.Lumps[3].FileLen / 12];

            for (Int32 i = 0; i < BSP_Vertices.Length; i++)
            {
                BSP_Vertices[i] = BSPFileReader.ReadVector3D(true) * ConfigLoader.WorldScale;
            }

            BSP_Surfedges = new Int32[BSP_Header.Lumps[13].FileLen / 4];
            BSPFileReader.ReadArray(ref BSP_Surfedges, BSP_Header.Lumps[13].FileOfs);

            BSP_WorldSpawn = new GameObject(BSPName);
            BSP_Brushes    = new List <GameObject>();

            //Create new lightmap list
            ConfigLoader.lightmapsData     = new List <LightmapData>();
            LightmapSettings.lightmapsMode = LightmapsMode.NonDirectional;

            LoadEntities();
            LoadStaticProps();

            BSPFileReader.BaseStream.Dispose();//.Close();
        }