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
33package com.jme3.shader;
34
35import com.jme3.asset.AssetKey;
36import com.jme3.export.InputCapsule;
37import com.jme3.export.JmeExporter;
38import com.jme3.export.JmeImporter;
39import com.jme3.export.OutputCapsule;
40import java.io.IOException;
41
42public class ShaderKey extends AssetKey<Shader> {
43
44    protected String fragName;
45    protected DefineList defines;
46    protected String language;
47
48    public ShaderKey(){
49    }
50
51    public ShaderKey(String vertName, String fragName, DefineList defines, String lang){
52        super(vertName);
53        this.fragName = fragName;
54        this.defines = defines;
55        this.language = lang;
56    }
57
58    @Override
59    public String toString(){
60        return "V="+name + " F=" + fragName + (defines != null ? defines : "");
61    }
62
63    @Override
64    public boolean equals(Object obj) {
65        if (obj == null){
66            return false;
67        }
68        if (getClass() != obj.getClass()){
69            return false;
70        }
71
72        final ShaderKey other = (ShaderKey) obj;
73        if (name.equals(other.name) && fragName.equals(other.fragName)){
74//            return true;
75            if (defines != null && other.defines != null)
76                return defines.getCompiled().equals(other.defines.getCompiled());
77            else if (defines != null || other.defines != null)
78                return false;
79            else
80                return true;
81        }
82        return false;
83    }
84
85    @Override
86    public int hashCode() {
87        int hash = 7;
88        hash = 41 * hash + name.hashCode();
89        hash = 41 * hash + fragName.hashCode();
90        hash = 41 * hash + (defines != null ? defines.getCompiled().hashCode() : 0);
91        return hash;
92    }
93
94    public DefineList getDefines() {
95        return defines;
96    }
97
98    public String getVertName(){
99        return name;
100    }
101
102    public String getFragName() {
103        return fragName;
104    }
105
106    public String getLanguage() {
107        return language;
108    }
109
110    @Override
111    public void write(JmeExporter ex) throws IOException{
112        super.write(ex);
113        OutputCapsule oc = ex.getCapsule(this);
114        oc.write(fragName, "fragment_name", null);
115        oc.write(language, "language", null);
116    }
117
118    @Override
119    public void read(JmeImporter im) throws IOException{
120        super.read(im);
121        InputCapsule ic = im.getCapsule(this);
122        fragName = ic.readString("fragment_name", null);
123        language = ic.readString("language", null);
124    }
125
126}
127