|
| 1 | +/* |
| 2 | + * Copyright (c) 2009-2026 jMonkeyEngine |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * Redistribution and use in source and binary forms, with or without |
| 6 | + * modification, are permitted provided that the following conditions are |
| 7 | + * met: |
| 8 | + * |
| 9 | + * * Redistributions of source code must retain the above copyright |
| 10 | + * notice, this list of conditions and the following disclaimer. |
| 11 | + * |
| 12 | + * * Redistributions in binary form must reproduce the above copyright |
| 13 | + * notice, this list of conditions and the following disclaimer in the |
| 14 | + * documentation and/or other materials provided with the distribution. |
| 15 | + * |
| 16 | + * * Neither the name of 'jMonkeyEngine' nor the names of its contributors |
| 17 | + * may be used to endorse or promote products derived from this software |
| 18 | + * without specific prior written permission. |
| 19 | + * |
| 20 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 | + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
| 22 | + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 23 | + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
| 24 | + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 25 | + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 26 | + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 27 | + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 28 | + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 29 | + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 30 | + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | + */ |
| 32 | +package com.jme3.asset.plugins; |
| 33 | + |
| 34 | +import com.jme3.asset.AssetInfo; |
| 35 | +import com.jme3.asset.AssetKey; |
| 36 | +import com.jme3.asset.AssetLoadException; |
| 37 | +import com.jme3.asset.AssetLocator; |
| 38 | +import com.jme3.asset.AssetManager; |
| 39 | +import org.ngengine.libjglios.core.LibJGLIOSBundleBridge; |
| 40 | + |
| 41 | +import java.io.File; |
| 42 | +import java.io.FileInputStream; |
| 43 | +import java.io.FileNotFoundException; |
| 44 | +import java.io.IOException; |
| 45 | +import java.io.InputStream; |
| 46 | + |
| 47 | +/** |
| 48 | + * Locates assets inside the iOS application bundle resource directory. |
| 49 | + */ |
| 50 | +public class IosBundleLocator implements AssetLocator { |
| 51 | + |
| 52 | + private File root; |
| 53 | + |
| 54 | + @Override |
| 55 | + public void setRootPath(String rootPath) { |
| 56 | + String resourcePath = LibJGLIOSBundleBridge.resourcePath(); |
| 57 | + if (resourcePath == null || resourcePath.isEmpty()) { |
| 58 | + throw new AssetLoadException("Unable to resolve iOS bundle resource path"); |
| 59 | + } |
| 60 | + |
| 61 | + String relativeRoot = normalizeRootPath(rootPath); |
| 62 | + try { |
| 63 | + root = new File(resourcePath, relativeRoot).getCanonicalFile(); |
| 64 | + if (!root.isDirectory()) { |
| 65 | + throw new IllegalArgumentException("Given iOS bundle root path \"" + root + "\" is not a directory"); |
| 66 | + } |
| 67 | + } catch (IOException ex) { |
| 68 | + throw new AssetLoadException("iOS bundle root path is invalid", ex); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + private static String normalizeRootPath(String rootPath) { |
| 73 | + if (rootPath == null || rootPath.isEmpty() || rootPath.equals("/") || rootPath.equals(".")) { |
| 74 | + return ""; |
| 75 | + } |
| 76 | + |
| 77 | + String normalized = rootPath.replace('\\', '/'); |
| 78 | + while (normalized.startsWith("/")) { |
| 79 | + normalized = normalized.substring(1); |
| 80 | + } |
| 81 | + return normalized; |
| 82 | + } |
| 83 | + |
| 84 | + @Override |
| 85 | + public AssetInfo locate(AssetManager manager, AssetKey key) { |
| 86 | + String name = key.getName(); |
| 87 | + if (name.startsWith("/")) { |
| 88 | + name = name.substring(1); |
| 89 | + } |
| 90 | + |
| 91 | + File file = new File(root, name); |
| 92 | + if (!file.exists() || !file.isFile()) { |
| 93 | + return null; |
| 94 | + } |
| 95 | + |
| 96 | + try { |
| 97 | + File canonicalFile = file.getCanonicalFile(); |
| 98 | + if (!isInsideRoot(canonicalFile)) { |
| 99 | + return null; |
| 100 | + } |
| 101 | + return new BundleAssetInfo(manager, key, canonicalFile); |
| 102 | + } catch (IOException ex) { |
| 103 | + throw new AssetLoadException("Failed to resolve iOS bundle asset path " + file, ex); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + private boolean isInsideRoot(File file) { |
| 108 | + String rootPath = root.getPath(); |
| 109 | + String filePath = file.getPath(); |
| 110 | + return filePath.equals(rootPath) || filePath.startsWith(rootPath + File.separator); |
| 111 | + } |
| 112 | + |
| 113 | + private static final class BundleAssetInfo extends AssetInfo { |
| 114 | + |
| 115 | + private final File file; |
| 116 | + |
| 117 | + private BundleAssetInfo(AssetManager manager, AssetKey key, File file) { |
| 118 | + super(manager, key); |
| 119 | + this.file = file; |
| 120 | + } |
| 121 | + |
| 122 | + @Override |
| 123 | + public InputStream openStream() { |
| 124 | + try { |
| 125 | + return new FileInputStream(file); |
| 126 | + } catch (FileNotFoundException ex) { |
| 127 | + throw new AssetLoadException("Failed to open iOS bundle asset: " + file, ex); |
| 128 | + } |
| 129 | + } |
| 130 | + } |
| 131 | +} |
0 commit comments