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 
33 package com.jme3.shader;
34 
35 import com.jme3.export.*;
36 import java.io.IOException;
37 import java.util.Map;
38 import java.util.SortedMap;
39 import java.util.TreeMap;
40 
41 public class DefineList implements Savable {
42 
43     private final SortedMap<String, String> defines = new TreeMap<String, String>();
44     private String compiled = null;
45 
write(JmeExporter ex)46     public void write(JmeExporter ex) throws IOException{
47         OutputCapsule oc = ex.getCapsule(this);
48 
49         String[] keys = new String[defines.size()];
50         String[] vals = new String[defines.size()];
51 
52         int i = 0;
53         for (Map.Entry<String, String> define : defines.entrySet()){
54             keys[i] = define.getKey();
55             vals[i] = define.getValue();
56             i++;
57         }
58 
59         oc.write(keys, "keys", null);
60         oc.write(vals, "vals", null);
61 
62         // for compatability only with older versions
63         oc.write(compiled, "compiled", null);
64     }
65 
read(JmeImporter im)66     public void read(JmeImporter im) throws IOException{
67         InputCapsule ic = im.getCapsule(this);
68 
69         String[] keys = ic.readStringArray("keys", null);
70         String[] vals = ic.readStringArray("vals", null);
71         for (int i = 0; i < keys.length; i++){
72             defines.put(keys[i], vals[i]);
73         }
74 
75         compiled = ic.readString("compiled", null);
76     }
77 
clear()78     public void clear() {
79         defines.clear();
80         compiled = "";
81     }
82 
get(String key)83     public String get(String key){
84         // I do not see the point of forcing a rebuild on get()
85         // so I'm commenting it out. -pspeed
86         //compiled = null;
87         return defines.get(key);
88     }
89 
90 //    public void set(String key, String val){
91 //        compiled = null;
92 //        defines.put(key, val);
93 //    }
94 
set(String key, VarType type, Object val)95     public boolean set(String key, VarType type, Object val){
96         if (val == null){
97             defines.remove(key);
98             compiled = null;
99             return true;
100         }
101 
102         switch (type){
103             case Boolean:
104                 if ( ((Boolean) val).booleanValue() ) {
105                     // same literal, != should work
106                     if( defines.put(key, "1") != "1" ) {
107                         compiled = null;
108                         return true;
109                     }
110                 } else if (defines.containsKey(key)) {
111                     defines.remove(key);
112                     compiled = null;
113                     return true;
114                 }
115 
116                 break;
117             case Float:
118             case Int:
119                 String original = defines.put(key, val.toString());
120                 if (!val.equals(original)) {
121                     compiled = null;
122                     return true;
123                 }
124                 break;
125             default:
126                 // same literal, != should work
127                 if (defines.put(key, "1") != "1") {
128                     compiled = null;
129                     return true;
130                 }
131                 break;
132         }
133 
134         return false;
135     }
136 
remove(String key)137     public boolean remove(String key){
138         if (defines.remove(key) != null) {
139             compiled = null;
140             return true;
141         }
142 
143         return false;
144     }
145 
addFrom(DefineList other)146     public void addFrom(DefineList other){
147         if (other == null)
148             return;
149 
150         compiled = null;
151         defines.putAll(other.defines);
152     }
153 
getCompiled()154     public String getCompiled(){
155         if (compiled == null){
156             StringBuilder sb = new StringBuilder();
157             for (Map.Entry<String, String> entry : defines.entrySet()){
158                 sb.append("#define ").append(entry.getKey()).append(" ");
159                 sb.append(entry.getValue()).append('\n');
160             }
161             compiled = sb.toString();
162         }
163         return compiled;
164     }
165 
166     @Override
toString()167     public String toString(){
168         StringBuilder sb = new StringBuilder();
169         int i = 0;
170         for (Map.Entry<String, String> entry : defines.entrySet()) {
171             sb.append(entry.getKey());
172             if (i != defines.size() - 1)
173                 sb.append(", ");
174 
175             i++;
176         }
177         return sb.toString();
178     }
179 
180 }
181