| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using System.IO; |
| | 4 | | using System.Linq; |
| | 5 | | using DCL.Helpers; |
| | 6 | | using UnityEngine; |
| | 7 | | using UnityEngine.Networking; |
| | 8 | |
|
| | 9 | | namespace DCL.ABConverter |
| | 10 | | { |
| | 11 | | public static class Client |
| | 12 | | { |
| | 13 | | private const string COLLECTIONS_FETCH_URL = "https://peer.decentraland.org/lambdas/collections"; |
| | 14 | | private const string WEARABLES_FETCH_URL = "https://peer.decentraland.org/lambdas/collections/wearables?"; |
| | 15 | |
|
| | 16 | | public class Settings |
| | 17 | | { |
| | 18 | | /// <summary> |
| | 19 | | /// if set to true, when conversion finishes, the working folder containing all downloaded assets will be de |
| | 20 | | /// </summary> |
| | 21 | | public bool deleteDownloadPathAfterFinished = false; |
| | 22 | |
|
| | 23 | | /// <summary> |
| | 24 | | /// If set to true, Asset Bundles will not be built at all, and only the asset dump will be performed. |
| | 25 | | /// </summary> |
| | 26 | | public bool dumpOnly = false; |
| | 27 | |
|
| | 28 | | /// <summary> |
| | 29 | | /// If set to true, Asset Bundle output folder will be checked, and existing bundles in that folder will be |
| | 30 | | /// the conversion process. |
| | 31 | | /// </summary> |
| | 32 | | public bool skipAlreadyBuiltBundles = false; |
| | 33 | |
|
| | 34 | | /// <summary> |
| | 35 | | /// If set to true, the GLTF _Downloads folder and the Asset Bundles folder will be deleted at the beginning |
| | 36 | | /// </summary> |
| 0 | 37 | | public bool clearDirectoriesOnStart = true; |
| | 38 | |
|
| | 39 | | /// <summary> |
| | 40 | | /// Log verbosity. |
| | 41 | | /// </summary> |
| | 42 | | public bool verbose = false; |
| | 43 | |
|
| | 44 | | /// <summary> |
| | 45 | | /// Output folder for asset bundles, by default, they will be stored in Assets/../AssetBundles. |
| | 46 | | /// </summary> |
| 0 | 47 | | public string finalAssetBundlePath = Config.ASSET_BUNDLES_PATH_ROOT + Path.DirectorySeparatorChar; |
| | 48 | |
|
| | 49 | | /// <summary> |
| | 50 | | /// Target top level domain. This will define the content server url used for the conversion (org, zone, etc |
| | 51 | | /// </summary> |
| 0 | 52 | | public ContentServerUtils.ApiTLD tld = ContentServerUtils.ApiTLD.ORG; |
| | 53 | |
|
| | 54 | | /// <summary> |
| | 55 | | /// Raw baseUrl using for asset dumping. |
| | 56 | | /// </summary> |
| | 57 | | public string baseUrl; |
| | 58 | |
|
| 0 | 59 | | public Settings Clone() { return this.MemberwiseClone() as Settings; } |
| | 60 | |
|
| 0 | 61 | | public Settings(ContentServerUtils.ApiTLD tld = ContentServerUtils.ApiTLD.ORG) |
| | 62 | | { |
| 0 | 63 | | this.tld = tld; |
| 0 | 64 | | this.baseUrl = ContentServerUtils.GetContentAPIUrlBase(tld); |
| 0 | 65 | | } |
| | 66 | | } |
| | 67 | |
|
| 0 | 68 | | private static Logger log = new Logger("ABConverter.Client"); |
| | 69 | | public static Environment env; |
| | 70 | |
|
| | 71 | | public static Environment EnsureEnvironment() |
| | 72 | | { |
| 0 | 73 | | if (env == null) |
| 0 | 74 | | env = Environment.CreateWithDefaultImplementations(); |
| | 75 | |
|
| 0 | 76 | | return env; |
| | 77 | | } |
| | 78 | |
|
| | 79 | | public static string BuildAllWearableCollectionsURL() |
| | 80 | | { |
| 0 | 81 | | UnityWebRequest w = UnityWebRequest.Get(COLLECTIONS_FETCH_URL); |
| 0 | 82 | | w.SendWebRequest(); |
| | 83 | |
|
| 0 | 84 | | while (!w.isDone) { } |
| | 85 | |
|
| 0 | 86 | | if (!w.WebRequestSucceded()) |
| | 87 | | { |
| 0 | 88 | | Debug.LogError($"Request error! Wearable collections at '{COLLECTIONS_FETCH_URL}' couldn't be fetched! - |
| 0 | 89 | | return null; |
| | 90 | | } |
| | 91 | |
|
| 0 | 92 | | var collectionsApiData = JsonUtility.FromJson<WearableCollectionsAPIData>(w.downloadHandler.text); |
| | 93 | |
|
| 0 | 94 | | string finalUrl = WEARABLES_FETCH_URL; |
| 0 | 95 | | finalUrl += "collectionId=" + collectionsApiData.collections[0].id; |
| 0 | 96 | | for (int i = 1; i < collectionsApiData.collections.Length; i++) |
| | 97 | | { |
| 0 | 98 | | finalUrl += "&collectionId=" + collectionsApiData.collections[i].id; |
| | 99 | | } |
| | 100 | |
|
| 0 | 101 | | return finalUrl; |
| | 102 | | } |
| | 103 | |
|
| | 104 | | /// <summary> |
| | 105 | | /// Batch-mode entry point |
| | 106 | | /// </summary> |
| | 107 | | public static void ExportSceneToAssetBundles() |
| | 108 | | { |
| | 109 | | //NOTE(Brian): This should make the logs cleaner |
| 0 | 110 | | Application.SetStackTraceLogType(LogType.Log, StackTraceLogType.None); |
| 0 | 111 | | Application.SetStackTraceLogType(LogType.Warning, StackTraceLogType.None); |
| 0 | 112 | | Application.SetStackTraceLogType(LogType.Error, StackTraceLogType.Full); |
| 0 | 113 | | Application.SetStackTraceLogType(LogType.Exception, StackTraceLogType.Full); |
| 0 | 114 | | Application.SetStackTraceLogType(LogType.Assert, StackTraceLogType.Full); |
| | 115 | |
|
| 0 | 116 | | EnsureEnvironment(); |
| 0 | 117 | | ExportSceneToAssetBundles(System.Environment.GetCommandLineArgs()); |
| 0 | 118 | | } |
| | 119 | |
|
| | 120 | | /// <summary> |
| | 121 | | /// Start the conversion process with the given commandLineArgs. |
| | 122 | | /// </summary> |
| | 123 | | /// <param name="commandLineArgs">An array with the command line arguments.</param> |
| | 124 | | /// <exception cref="ArgumentException">When an invalid argument is passed</exception> |
| | 125 | | public static void ExportSceneToAssetBundles(string[] commandLineArgs) |
| | 126 | | { |
| 0 | 127 | | Settings settings = new Settings(); |
| | 128 | | try |
| | 129 | | { |
| 0 | 130 | | if (Utils.ParseOption(commandLineArgs, Config.CLI_SET_CUSTOM_OUTPUT_ROOT_PATH, 1, out string[] outputPat |
| | 131 | | { |
| 0 | 132 | | settings.finalAssetBundlePath = outputPath[0] + "/"; |
| | 133 | | } |
| | 134 | |
|
| 0 | 135 | | if (Utils.ParseOption(commandLineArgs, Config.CLI_SET_CUSTOM_BASE_URL, 1, out string[] customBaseUrl)) |
| 0 | 136 | | settings.baseUrl = customBaseUrl[0]; |
| | 137 | |
|
| 0 | 138 | | if (Utils.ParseOption(commandLineArgs, Config.CLI_VERBOSE, 0, out _)) |
| 0 | 139 | | settings.verbose = true; |
| | 140 | |
|
| 0 | 141 | | if (Utils.ParseOption(commandLineArgs, Config.CLI_ALWAYS_BUILD_SYNTAX, 0, out _)) |
| 0 | 142 | | settings.skipAlreadyBuiltBundles = false; |
| | 143 | |
|
| 0 | 144 | | if (Utils.ParseOption(commandLineArgs, Config.CLI_KEEP_BUNDLES_SYNTAX, 0, out _)) |
| 0 | 145 | | settings.deleteDownloadPathAfterFinished = false; |
| | 146 | |
|
| 0 | 147 | | if (Utils.ParseOption(commandLineArgs, Config.CLI_BUILD_SCENE_SYNTAX, 1, out string[] sceneCid)) |
| | 148 | | { |
| 0 | 149 | | if (sceneCid == null || string.IsNullOrEmpty(sceneCid[0])) |
| | 150 | | { |
| 0 | 151 | | throw new ArgumentException("Invalid sceneCid argument! Please use -sceneCid <id> to establish t |
| | 152 | | } |
| | 153 | |
|
| 0 | 154 | | DumpScene(sceneCid[0], settings); |
| 0 | 155 | | return; |
| | 156 | | } |
| | 157 | |
|
| 0 | 158 | | if (Utils.ParseOption(commandLineArgs, Config.CLI_BUILD_PARCELS_RANGE_SYNTAX, 4, out string[] xywh)) |
| | 159 | | { |
| 0 | 160 | | if (xywh == null) |
| | 161 | | { |
| 0 | 162 | | throw new ArgumentException("Invalid parcelsXYWH argument! Please use -parcelsXYWH x y w h to es |
| | 163 | | } |
| | 164 | |
|
| | 165 | | int x, y, w, h; |
| 0 | 166 | | bool parseSuccess = false; |
| | 167 | |
|
| 0 | 168 | | parseSuccess |= int.TryParse(xywh[0], out x); |
| 0 | 169 | | parseSuccess |= int.TryParse(xywh[1], out y); |
| 0 | 170 | | parseSuccess |= int.TryParse(xywh[2], out w); |
| 0 | 171 | | parseSuccess |= int.TryParse(xywh[3], out h); |
| | 172 | |
|
| 0 | 173 | | if (!parseSuccess) |
| | 174 | | { |
| 0 | 175 | | throw new ArgumentException("Invalid parcelsXYWH argument! Please use -parcelsXYWH x y w h to es |
| | 176 | | } |
| | 177 | |
|
| 0 | 178 | | if (w > 10 || h > 10 || w < 0 || h < 0) |
| | 179 | | { |
| 0 | 180 | | throw new ArgumentException("Invalid parcelsXYWH argument! Please don't use negative width/heigh |
| | 181 | | } |
| | 182 | |
|
| 0 | 183 | | DumpArea(new Vector2Int(x, y), new Vector2Int(w, h), settings); |
| 0 | 184 | | return; |
| | 185 | | } |
| | 186 | |
|
| 0 | 187 | | throw new ArgumentException("Invalid arguments! You must pass -parcelsXYWH or -sceneCid for dump to work |
| | 188 | | } |
| 0 | 189 | | catch (Exception e) |
| | 190 | | { |
| 0 | 191 | | log.Error(e.Message); |
| 0 | 192 | | } |
| 0 | 193 | | } |
| | 194 | |
|
| | 195 | | /// <summary> |
| | 196 | | /// This will start the asset bundle conversion for a given scene list, given a scene cids list. |
| | 197 | | /// </summary> |
| | 198 | | /// <param name="sceneCidsList">The cid list for the scenes to gather from the catalyst's content server</param> |
| | 199 | | /// <param name="settings">Any conversion settings object, if its null, a new one will be created</param> |
| | 200 | | /// <returns>A state context object useful for tracking the conversion progress</returns> |
| | 201 | | public static Core.State ConvertScenesToAssetBundles(List<string> sceneCidsList, Settings settings = null) |
| | 202 | | { |
| 0 | 203 | | if (sceneCidsList == null || sceneCidsList.Count == 0) |
| | 204 | | { |
| 0 | 205 | | log.Error("Scene list is null or count == 0! Maybe this sector lacks scenes or content requests failed?" |
| 0 | 206 | | return new Core.State() { lastErrorCode = Core.ErrorCodes.SCENE_LIST_NULL }; |
| | 207 | | } |
| | 208 | |
|
| 0 | 209 | | log.Info($"Building {sceneCidsList.Count} scenes..."); |
| | 210 | |
|
| 0 | 211 | | List<ContentServerUtils.MappingPair> rawContents = new List<ContentServerUtils.MappingPair>(); |
| | 212 | |
|
| 0 | 213 | | EnsureEnvironment(); |
| | 214 | |
|
| 0 | 215 | | if (settings == null) |
| 0 | 216 | | settings = new Settings(); |
| | 217 | |
|
| 0 | 218 | | foreach (var sceneCid in sceneCidsList) |
| | 219 | | { |
| 0 | 220 | | ContentServerUtils.MappingsAPIData parcelInfoApiData = ABConverter.Utils.GetSceneMappingsData(env.webReq |
| 0 | 221 | | rawContents.AddRange(parcelInfoApiData.data[0].content.contents); |
| | 222 | | } |
| | 223 | |
|
| 0 | 224 | | var core = new ABConverter.Core(env, settings); |
| 0 | 225 | | core.Convert(rawContents.ToArray()); |
| | 226 | |
|
| 0 | 227 | | return core.state; |
| | 228 | | } |
| | 229 | |
|
| | 230 | | /// <summary> |
| | 231 | | /// This will start the asset bundle conversion for a single asset |
| | 232 | | /// </summary> |
| | 233 | | /// <param name="assetHash">The asset's content server hash</param> |
| | 234 | | /// <param name="assetFilename">The asset's content server file name</param> |
| | 235 | | /// <param name="sceneCid">The asset scene ID</param> |
| | 236 | | /// <param name="settings">Any conversion settings object, if its null, a new one will be created</param> |
| | 237 | | /// <returns>A state context object useful for tracking the conversion progress</returns> |
| | 238 | | public static Core.State ConvertAssetToAssetBundle(string assetHash, string assetFilename, string sceneCid, Sett |
| | 239 | | { |
| 0 | 240 | | if (string.IsNullOrEmpty(assetHash)) |
| | 241 | | { |
| 0 | 242 | | log.Error("Missing asset hash for ConvertAssetToAssetBundle()"); |
| 0 | 243 | | return new Core.State() { lastErrorCode = Core.ErrorCodes.UNDEFINED }; |
| | 244 | | } |
| | 245 | |
|
| 0 | 246 | | if (string.IsNullOrEmpty(assetFilename)) |
| | 247 | | { |
| 0 | 248 | | log.Error("Missing asset file name for ConvertAssetToAssetBundle()"); |
| 0 | 249 | | return new Core.State() { lastErrorCode = Core.ErrorCodes.UNDEFINED }; |
| | 250 | | } |
| | 251 | |
|
| 0 | 252 | | log.Info($"Building {assetHash} asset..."); |
| | 253 | |
|
| 0 | 254 | | EnsureEnvironment(); |
| | 255 | |
|
| 0 | 256 | | if (settings == null) |
| | 257 | | { |
| 0 | 258 | | settings = new Settings() |
| | 259 | | { |
| | 260 | | skipAlreadyBuiltBundles = false |
| | 261 | | }; |
| | 262 | | } |
| | 263 | |
|
| 0 | 264 | | var core = new ABConverter.Core(env, settings); |
| | 265 | |
|
| 0 | 266 | | List<ContentServerUtils.MappingPair> rawContents = new List<ContentServerUtils.MappingPair>(); |
| 0 | 267 | | rawContents.Add(new ContentServerUtils.MappingPair |
| | 268 | | { |
| | 269 | | file = assetFilename, |
| | 270 | | hash = assetHash |
| | 271 | | }); |
| | 272 | |
|
| | 273 | | // If the asset is a GLTF we add the dependencies to the rawContents to be downloaded |
| 0 | 274 | | if (assetFilename.ToLower().EndsWith(".glb") || assetFilename.ToLower().EndsWith(".gltf")) |
| | 275 | | { |
| 0 | 276 | | core.GetAssetDependenciesMappingPairs(assetHash, assetFilename, sceneCid, ref rawContents); |
| | 277 | | } |
| | 278 | |
|
| 0 | 279 | | core.Convert(rawContents.ToArray(), null); |
| | 280 | |
|
| 0 | 281 | | return core.state; |
| | 282 | | } |
| | 283 | |
|
| | 284 | | /// <summary> |
| | 285 | | /// Dump a world area given coords and size. The zone is a rectangle with a center pivot. |
| | 286 | | /// </summary> |
| | 287 | | /// <param name="coords">Coords as parcel coordinates</param> |
| | 288 | | /// <param name="size">Size as radius</param> |
| | 289 | | /// <param name="settings">Conversion settings</param> |
| | 290 | | /// <returns>A state context object useful for tracking the conversion progress</returns> |
| | 291 | | public static Core.State DumpArea(Vector2Int coords, Vector2Int size, Settings settings = null) |
| | 292 | | { |
| 0 | 293 | | EnsureEnvironment(); |
| | 294 | |
|
| 0 | 295 | | if (settings == null) |
| 0 | 296 | | settings = new Settings(); |
| | 297 | |
|
| 0 | 298 | | HashSet<string> sceneCids = ABConverter.Utils.GetSceneCids(env.webRequest, settings.tld, coords, size); |
| 0 | 299 | | List<string> sceneCidsList = sceneCids.ToList(); |
| 0 | 300 | | return ConvertScenesToAssetBundles(sceneCidsList, settings); |
| | 301 | | } |
| | 302 | |
|
| | 303 | | /// <summary> |
| | 304 | | /// Dump a world area given a parcel coords array. |
| | 305 | | /// </summary> |
| | 306 | | /// <param name="coords">A list with the parcels coordinates wanted to be converted</param> |
| | 307 | | /// <param name="settings">Conversion settings</param> |
| | 308 | | /// <returns>A state context object useful for tracking the conversion progress</returns> |
| | 309 | | public static Core.State DumpArea(List<Vector2Int> coords, Settings settings = null) |
| | 310 | | { |
| 0 | 311 | | EnsureEnvironment(); |
| | 312 | |
|
| 0 | 313 | | if (settings == null) |
| 0 | 314 | | settings = new Settings(); |
| | 315 | |
|
| 0 | 316 | | HashSet<string> sceneCids = Utils.GetScenesCids(env.webRequest, settings.tld, coords); |
| | 317 | |
|
| 0 | 318 | | List<string> sceneCidsList = sceneCids.ToList(); |
| 0 | 319 | | return ConvertScenesToAssetBundles(sceneCidsList, settings); |
| | 320 | | } |
| | 321 | |
|
| | 322 | | /// <summary> |
| | 323 | | /// Dump a single world scene given a scene cid. |
| | 324 | | /// </summary> |
| | 325 | | /// <param name="cid">The scene cid in the multi-hash format (i.e. Qm...etc)</param> |
| | 326 | | /// <param name="settings">Conversion settings</param> |
| | 327 | | /// <returns>A state context object useful for tracking the conversion progress</returns> |
| | 328 | | public static Core.State DumpScene(string cid, Settings settings = null) |
| | 329 | | { |
| 0 | 330 | | EnsureEnvironment(); |
| | 331 | |
|
| 0 | 332 | | if (settings == null) |
| 0 | 333 | | settings = new Settings(); |
| | 334 | |
|
| 0 | 335 | | return ConvertScenesToAssetBundles(new List<string> { cid }, settings); |
| | 336 | | } |
| | 337 | |
|
| | 338 | | /// <summary> |
| | 339 | | /// Dump a single asset (and its dependencies) given an asset hash and scene Cid |
| | 340 | | /// </summary> |
| | 341 | | /// <param name="assetHash">The asset's content server hash</param> |
| | 342 | | /// <param name="assetFilename">The asset's content server file name</param> |
| | 343 | | /// <param name="sceneCid">The asset scene ID</param> |
| | 344 | | /// <param name="settings">Conversion settings</param> |
| | 345 | | /// <returns>A state context object useful for tracking the conversion progress</returns> |
| | 346 | | public static Core.State DumpAsset(string assetHash, string assetFilename, string sceneCid, Settings settings = |
| | 347 | | { |
| 0 | 348 | | EnsureEnvironment(); |
| | 349 | |
|
| 0 | 350 | | if (settings == null) |
| 0 | 351 | | settings = new Settings(); |
| | 352 | |
|
| 0 | 353 | | return ConvertAssetToAssetBundle(assetHash, assetFilename, sceneCid, settings); |
| | 354 | | } |
| | 355 | |
|
| | 356 | | /// <summary> |
| | 357 | | /// Dump all bodyshape wearables normally, including their imported skeleton |
| | 358 | | /// </summary> |
| | 359 | | public static void DumpAllBodiesWearables() |
| | 360 | | { |
| 0 | 361 | | EnsureEnvironment(); |
| | 362 | |
|
| 0 | 363 | | List<WearableItem> avatarItemList = GetWearableItems(BuildAllWearableCollectionsURL()) |
| 0 | 364 | | .Where(x => x.data.category == WearableLiterals.Categories.BODY_SHAPE) |
| | 365 | | .ToList(); |
| | 366 | |
|
| 0 | 367 | | Queue<WearableItem> itemQueue = new Queue<WearableItem>(avatarItemList); |
| 0 | 368 | | var settings = new Settings(); |
| 0 | 369 | | settings.skipAlreadyBuiltBundles = false; |
| 0 | 370 | | settings.deleteDownloadPathAfterFinished = false; |
| 0 | 371 | | settings.clearDirectoriesOnStart = false; |
| 0 | 372 | | var abConverterCoreController = new ABConverter.Core(ABConverter.Environment.CreateWithDefaultImplementation |
| | 373 | |
|
| 0 | 374 | | abConverterCoreController.InitializeDirectoryPaths(true); |
| 0 | 375 | | DumpWearableQueue(abConverterCoreController, itemQueue, GLTFImporter_OnBodyWearableLoad); |
| 0 | 376 | | } |
| | 377 | |
|
| | 378 | | /// <summary> |
| | 379 | | /// Dump all non-bodyshape wearables, optimized to remove the skeleton for the wearables ABs since that is |
| | 380 | | /// only needed for the body shapes (and the WearablesController sets it up for non-bodyshapes in runtime) |
| | 381 | | /// </summary> |
| | 382 | | public static void DumpAllNonBodiesWearables() |
| | 383 | | { |
| 0 | 384 | | EnsureEnvironment(); |
| | 385 | |
|
| | 386 | | // For debugging purposes we can intercept this item list with LinQ for specific wearables |
| 0 | 387 | | List<WearableItem> avatarItemList = GetWearableItems(BuildAllWearableCollectionsURL()) |
| 0 | 388 | | .Where(x => x.data.category != WearableLiterals.Categories.BODY_SHAPE) |
| | 389 | | .ToList(); |
| | 390 | |
|
| 0 | 391 | | Queue<WearableItem> itemQueue = new Queue<WearableItem>(avatarItemList); |
| 0 | 392 | | var settings = new Settings(); |
| 0 | 393 | | settings.skipAlreadyBuiltBundles = false; |
| 0 | 394 | | settings.deleteDownloadPathAfterFinished = false; |
| 0 | 395 | | settings.clearDirectoriesOnStart = false; |
| 0 | 396 | | var abConverterCoreController = new ABConverter.Core(ABConverter.Environment.CreateWithDefaultImplementation |
| | 397 | |
|
| 0 | 398 | | abConverterCoreController.InitializeDirectoryPaths(true); |
| 0 | 399 | | DumpWearableQueue(abConverterCoreController, itemQueue, GLTFImporter_OnNonBodyWearableLoad); |
| 0 | 400 | | } |
| | 401 | |
|
| | 402 | | /// <summary> |
| | 403 | | /// Given a list of WearableItems, each one is downloaded along with its dependencies and converted to ABs recur |
| | 404 | | /// (to avoid mixing same-name dependencies between wearables) |
| | 405 | | /// </summary> |
| | 406 | | /// <param name="abConverterCoreController">an instance of the ABCore</param> |
| | 407 | | /// <param name="items">an already-populated list of WearableItems</param> |
| | 408 | | /// <param name="OnWearableLoad">an action to be bind to the OnWearableLoad event on each wearable</param> |
| | 409 | | private static void DumpWearableQueue(ABConverter.Core abConverterCoreController, Queue<WearableItem> items, Sys |
| | 410 | | { |
| | 411 | | // We toggle the core's ABs generation off so that we execute that conversion here when there is no more ite |
| 0 | 412 | | abConverterCoreController.generateAssetBundles = false; |
| | 413 | |
|
| 0 | 414 | | if (items.Count == 0) |
| | 415 | | { |
| 0 | 416 | | abConverterCoreController.ConvertDumpedAssets(); |
| | 417 | |
|
| 0 | 418 | | return; |
| | 419 | | } |
| | 420 | |
|
| 0 | 421 | | Debug.Log("Building wearables... items left... " + items.Count); |
| | 422 | |
|
| 0 | 423 | | var pairs = ExtractMappingPairs(new List<WearableItem>() { items.Dequeue() }); |
| | 424 | |
|
| 0 | 425 | | UnityGLTF.GLTFImporter.OnGLTFWillLoad += OnWearableLoad; |
| | 426 | |
|
| 0 | 427 | | abConverterCoreController.Convert(pairs.ToArray(), |
| | 428 | | (err) => |
| | 429 | | { |
| 0 | 430 | | UnityGLTF.GLTFImporter.OnGLTFWillLoad -= OnWearableLoad; |
| 0 | 431 | | abConverterCoreController.CleanupWorkingFolders(); |
| 0 | 432 | | DumpWearableQueue(abConverterCoreController, items, OnWearableLoad); |
| 0 | 433 | | }); |
| 0 | 434 | | } |
| | 435 | |
|
| | 436 | | /// <summary> |
| | 437 | | /// Given a list of WearableItems, extracts and returns a list of MappingPairs |
| | 438 | | /// </summary> |
| | 439 | | /// <param name="wearableItems">A list of already-populated WearableItems</param> |
| | 440 | | /// <returns>A list of the extracted Wearables MappingPairs</returns> |
| | 441 | | private static List<ContentServerUtils.MappingPair> ExtractMappingPairs(List<WearableItem> wearableItems) |
| | 442 | | { |
| 0 | 443 | | var result = new List<ContentServerUtils.MappingPair>(); |
| | 444 | |
|
| 0 | 445 | | foreach (var wearable in wearableItems) |
| | 446 | | { |
| 0 | 447 | | foreach (var representation in wearable.data.representations) |
| | 448 | | { |
| 0 | 449 | | foreach (var datum in representation.contents) |
| | 450 | | { |
| 0 | 451 | | result.Add(new ContentServerUtils.MappingPair() { file = datum.key, hash = datum.hash }); |
| | 452 | | } |
| | 453 | | } |
| | 454 | | } |
| | 455 | |
|
| 0 | 456 | | return result; |
| | 457 | | } |
| | 458 | |
|
| | 459 | | /// <summary> |
| | 460 | | /// Given a base-url to fetch wearables collections, returns a list of all the WearableItems |
| | 461 | | /// </summary> |
| | 462 | | /// <param name="url">base-url to fetch the wearables collections</param> |
| | 463 | | /// <returns>A list of all the WearableItems found</returns> |
| | 464 | | private static List<WearableItem> GetWearableItems(string url) |
| | 465 | | { |
| 0 | 466 | | UnityWebRequest w = UnityWebRequest.Get(url); |
| 0 | 467 | | w.SendWebRequest(); |
| | 468 | |
|
| 0 | 469 | | while (!w.isDone) { } |
| | 470 | |
|
| 0 | 471 | | if (!w.WebRequestSucceded()) |
| | 472 | | { |
| 0 | 473 | | Debug.LogError($"Request error! Wearable at '{url}' couldn't be fetched! -- {w.error}"); |
| 0 | 474 | | return null; |
| | 475 | | } |
| | 476 | |
|
| 0 | 477 | | var wearablesApiData = JsonUtility.FromJson<WearablesAPIData>(w.downloadHandler.text); |
| 0 | 478 | | var resultList = wearablesApiData.GetWearableItems(); |
| | 479 | |
|
| | 480 | | // Since the wearables deployments response returns only a batch of elements, we need to fetch all the |
| | 481 | | // batches sequentially |
| 0 | 482 | | if (!string.IsNullOrEmpty(wearablesApiData.pagination.next)) |
| | 483 | | { |
| 0 | 484 | | var nextPageResults = GetWearableItems(WEARABLES_FETCH_URL + wearablesApiData.pagination.next); |
| 0 | 485 | | resultList.AddRange(nextPageResults); |
| | 486 | | } |
| | 487 | |
|
| 0 | 488 | | return resultList; |
| | 489 | | } |
| | 490 | |
|
| | 491 | | private static void GLTFImporter_OnNonBodyWearableLoad(UnityGLTF.GLTFSceneImporter obj) |
| | 492 | | { |
| 0 | 493 | | obj.importSkeleton = false; |
| 0 | 494 | | obj.maxTextureSize = 512; |
| 0 | 495 | | } |
| | 496 | |
|
| | 497 | | private static void GLTFImporter_OnBodyWearableLoad(UnityGLTF.GLTFSceneImporter obj) |
| | 498 | | { |
| 0 | 499 | | obj.importSkeleton = true; |
| 0 | 500 | | obj.maxTextureSize = 512; |
| 0 | 501 | | } |
| | 502 | | } |
| | 503 | | } |