示例#1
0
        public static Asset Get(long assetId, string idPiece = "/asset/?ID=")
        {
            if (!assetCache.ContainsKey(assetId))
            {
                string appData       = Environment.GetEnvironmentVariable("AppData");
                string assetCacheDir = Path.Combine(appData, "Rbx2Source", "AssetCache");
                Directory.CreateDirectory(assetCacheDir);

                // Ping Roblox to figure out what this asset's cdn url is
                HttpWebRequest ping = WebRequest.CreateHttp("https://assetgame.roblox.com" + idPiece + assetId);
                ping.UserAgent         = "Roblox";
                ping.Method            = "HEAD";
                ping.AllowAutoRedirect = false;

                HttpWebResponse response   = (HttpWebResponse)ping.GetResponse();
                string          location   = response.GetResponseHeader("Location");
                string          identifier = location.Remove(0, 7).Replace(".rbxcdn.com/", "-") + "_64";
                string          cachedFile = Path.Combine(assetCacheDir, identifier);
                response.Close();

                Asset asset = null;
                if (File.Exists(cachedFile))
                {
                    string cachedContent = File.ReadAllText(cachedFile);
                    try
                    {
                        asset = JsonConvert.DeserializeObject <Asset>(cachedContent);
                        Rbx2Source.Print("Fetched pre-cached asset {0}", assetId);
                    }
                    catch
                    {
                        // Corrupted file?
                        if (File.Exists(cachedFile))
                        {
                            File.Delete(cachedFile);
                        }
                    }
                }

                if (asset == null)
                {
                    asset = new Asset();

                    WebClient http = new WebClient();
                    http.UseDefaultCredentials = true;
                    http.Headers.Set(HttpRequestHeader.UserAgent, "Roblox");
                    http.Proxy = null;
                    asset.Id   = assetId;

                    try
                    {
                        string productInfoJson = http.DownloadString("http://api.roblox.com/marketplace/productinfo?assetId=" + assetId);
                        asset.ProductInfo = JsonConvert.DeserializeObject <ProductInfo>(productInfoJson);
                        asset.ProductInfo.WindowsSafeName = FileUtility.MakeNameWindowsSafe(asset.ProductInfo.Name);
                        asset.AssetType = asset.ProductInfo.AssetTypeId;
                    }
                    catch
                    {
                        ProductInfo dummyInfo = new ProductInfo();
                        dummyInfo.Name            = "unknown_" + asset.Id;
                        dummyInfo.WindowsSafeName = dummyInfo.Name;
                        dummyInfo.AssetTypeId     = AssetType.Model;
                    }

                    asset.CdnUrl     = location;
                    asset.CdnCacheId = identifier;
                    asset.GetContent();
                    asset.Loaded = true;

                    string serialized = JsonConvert.SerializeObject(asset, Formatting.None, new JsonSerializerSettings());
                    try
                    {
                        File.WriteAllText(cachedFile, serialized);
                        Rbx2Source.Print("Precached AssetId {0}", assetId);
                    }
                    catch
                    {
                        // Oh well.
                        Rbx2Source.Print("Failed to cache AssetId {0}", assetId);
                    }
                }

                assetCache[assetId] = asset;
            }

            return(assetCache[assetId]);
        }
示例#2
0
        public static Asset Get(long assetId, string idPiece = "/asset/?ID=")
        {
            if (!assetCache.ContainsKey(assetId))
            {
                string appData = Environment.GetEnvironmentVariable("LocalAppData");

                string assetCacheDir = Path.Combine(appData, "Rbx2Source", "AssetCache");
                Directory.CreateDirectory(assetCacheDir);

                // Ping Roblox to figure out what this asset's cdn url is
                Uri            uri  = new Uri("https://assetdelivery.roblox.com/v1" + idPiece + assetId);
                HttpWebRequest ping = WebRequest.CreateHttp(uri);
                ping.UserAgent         = "RobloxStudio/WinInet";
                ping.AllowAutoRedirect = false;

                Asset asset = null;

                string location   = "";
                string identifier = "";
                string cachedFile = "";

                try
                {
                    using (var response = ping.GetResponse() as HttpWebResponse)
                    {
                        location   = response.GetResponseHeader("Location");
                        identifier = location.Remove(0, 8).Replace(".rbxcdn.com/", "-");
                        cachedFile = assetCacheDir + '\\' + identifier.Replace('/', '\\');

                        if (File.Exists(cachedFile))
                        {
                            string cachedContent = File.ReadAllText(cachedFile);

                            try
                            {
                                asset = JsonConvert.DeserializeObject <Asset>(cachedContent);

                                if (asset.Content.Length == 0)
                                {
                                    asset = null;
                                    throw new Exception();
                                }

                                Rbx2Source.Print("Fetched pre-cached asset {0}", assetId);
                            }
                            catch
                            {
                                // Corrupted file?
                                if (File.Exists(cachedFile))
                                {
                                    Rbx2Source.Print("Deleting corrupted file {0}", cachedFile);
                                    File.Delete(cachedFile);
                                }
                            }
                        }
                    }
                }
                catch
                {
                    Console.WriteLine("Failed to fetch {0}?", assetId);
                }

                if (asset == null)
                {
                    WebClient http = new WebClient()
                    {
                        UseDefaultCredentials = true,
                        Proxy = null
                    };

                    http.Headers.Set(HttpRequestHeader.UserAgent, "RobloxStudio/WinInet");
                    asset = new Asset()
                    {
                        Id = assetId
                    };

                    try
                    {
                        string productInfoJson = http.DownloadString("http://api.roblox.com/marketplace/productinfo?assetId=" + assetId);
                        asset.ProductInfo = JsonConvert.DeserializeObject <ProductInfo>(productInfoJson);
                        asset.ProductInfo.WindowsSafeName = FileUtility.MakeNameWindowsSafe(asset.ProductInfo.Name);
                        asset.AssetType = asset.ProductInfo.AssetTypeId;
                    }
                    catch
                    {
                        string name = "unknown_" + asset.Id;

                        ProductInfo dummyInfo = new ProductInfo()
                        {
                            Name            = name,
                            WindowsSafeName = name,
                            AssetTypeId     = AssetType.Model
                        };

                        asset.ProductInfo = dummyInfo;
                    }

                    asset.CdnUrl     = location;
                    asset.CdnCacheId = identifier;
                    asset.GetContent();
                    asset.Loaded = true;

                    string serialized = JsonConvert.SerializeObject(asset, Formatting.None);

                    try
                    {
                        File.WriteAllText(cachedFile, serialized);
                        Rbx2Source.Print("Precached AssetId {0}", assetId);
                    }
                    catch
                    {
                        // Oh well.
                        Rbx2Source.Print("Failed to cache AssetId {0}", assetId);
                    }

                    http.Dispose();
                }

                assetCache[assetId] = asset;
            }

            return(assetCache[assetId]);
        }