Home | History | Annotate | Download | only in system
      1 /*
      2  * Copyright (c) 2009-2010 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.system;
     33 
     34 import com.jme3.asset.AssetManager;
     35 import com.jme3.audio.AudioRenderer;
     36 import com.jme3.input.SoftTextDialogInput;
     37 import java.io.File;
     38 import java.io.InputStream;
     39 import java.net.URL;
     40 import java.util.logging.Logger;
     41 
     42 /**
     43  *
     44  * @author Kirill Vainer, normenhansen
     45  */
     46 public abstract class JmeSystemDelegate {
     47 
     48     protected final Logger logger = Logger.getLogger(JmeSystem.class.getName());
     49     protected boolean initialized = false;
     50     protected boolean lowPermissions = false;
     51     protected File storageFolder = null;
     52     protected SoftTextDialogInput softTextDialogInput = null;
     53 
     54     public synchronized File getStorageFolder() {
     55         if (lowPermissions) {
     56             throw new UnsupportedOperationException("File system access restricted");
     57         }
     58         if (storageFolder == null) {
     59             // Initialize storage folder
     60             storageFolder = new File(System.getProperty("user.home"), ".jme3");
     61             if (!storageFolder.exists()) {
     62                 storageFolder.mkdir();
     63             }
     64         }
     65         return storageFolder;
     66     }
     67 
     68     public String getFullName() {
     69         return JmeVersion.FULL_NAME;
     70     }
     71 
     72     public InputStream getResourceAsStream(String name) {
     73         return this.getClass().getResourceAsStream(name);
     74     }
     75 
     76     public URL getResource(String name) {
     77         return this.getClass().getResource(name);
     78     }
     79 
     80     public boolean trackDirectMemory() {
     81         return false;
     82     }
     83 
     84     public void setLowPermissions(boolean lowPerm) {
     85         lowPermissions = lowPerm;
     86     }
     87 
     88     public boolean isLowPermissions() {
     89         return lowPermissions;
     90     }
     91 
     92     public void setSoftTextDialogInput(SoftTextDialogInput input) {
     93         softTextDialogInput = input;
     94     }
     95     public SoftTextDialogInput getSoftTextDialogInput() {
     96         return softTextDialogInput;
     97     }
     98 
     99     public abstract AssetManager newAssetManager(URL configFile);
    100 
    101     public abstract AssetManager newAssetManager();
    102 
    103     public abstract boolean showSettingsDialog(AppSettings sourceSettings, boolean loadFromRegistry);
    104 
    105     private boolean is64Bit(String arch) {
    106         if (arch.equals("x86")) {
    107             return false;
    108         } else if (arch.equals("amd64")) {
    109             return true;
    110         } else if (arch.equals("x86_64")) {
    111             return true;
    112         } else if (arch.equals("ppc") || arch.equals("PowerPC")) {
    113             return false;
    114         } else if (arch.equals("ppc64")) {
    115             return true;
    116         } else if (arch.equals("i386") || arch.equals("i686")) {
    117             return false;
    118         } else if (arch.equals("universal")) {
    119             return false;
    120         } else {
    121             throw new UnsupportedOperationException("Unsupported architecture: " + arch);
    122         }
    123     }
    124 
    125     public Platform getPlatform() {
    126         String os = System.getProperty("os.name").toLowerCase();
    127         String arch = System.getProperty("os.arch").toLowerCase();
    128         boolean is64 = is64Bit(arch);
    129         if (os.contains("windows")) {
    130             return is64 ? Platform.Windows64 : Platform.Windows32;
    131         } else if (os.contains("linux") || os.contains("freebsd") || os.contains("sunos")) {
    132             return is64 ? Platform.Linux64 : Platform.Linux32;
    133         } else if (os.contains("mac os x") || os.contains("darwin")) {
    134             if (arch.startsWith("ppc")) {
    135                 return is64 ? Platform.MacOSX_PPC64 : Platform.MacOSX_PPC32;
    136             } else {
    137                 return is64 ? Platform.MacOSX64 : Platform.MacOSX32;
    138             }
    139         } else {
    140             throw new UnsupportedOperationException("The specified platform: " + os + " is not supported.");
    141         }
    142     }
    143 
    144     public abstract JmeContext newContext(AppSettings settings, JmeContext.Type contextType);
    145 
    146     public abstract AudioRenderer newAudioRenderer(AppSettings settings);
    147 
    148     public abstract void initialize(AppSettings settings);
    149 }
    150