gdx-studio

#libgdx#java#desktop

git clone https://git.pyrossh.dev/gdx-studio

An IDE for creating Games using libgdx and Java supported on all platforms Android, iOS, Desktop


src/gdxstudio/Export.java
a9b2a64 1
package gdxstudio;
a62d533 2
import java.io.BufferedReader;
a62d533 3
import java.io.File;
a62d533 4
import java.io.FileInputStream;
a62d533 5
import java.io.FileOutputStream;
a62d533 6
import java.io.FileReader;
a62d533 7
import java.io.IOException;
a62d533 8
import java.io.InputStream;
a62d533 9
import java.io.InputStreamReader;
a62d533 10
import java.nio.file.Files;
a62d533 11
import java.util.ArrayList;
a62d533 12
import java.util.Enumeration;
a62d533 13
import java.util.List;
a62d533 14
import java.util.jar.Attributes;
a62d533 15
import java.util.jar.JarEntry;
a62d533 16
import java.util.jar.JarOutputStream;
a62d533 17
import java.util.jar.Manifest;
a62d533 18
import java.util.zip.ZipEntry;
a62d533 19
import java.util.zip.ZipFile;
a62d533 20
a62d533 21
import javax.swing.JOptionPane;
a62d533 22
a9b2a64 23
import scene2d.Scene;
a9b2a64 24
a62d533 25
import com.badlogic.gdx.utils.ArrayMap;
a62d533 26
a62d533 27
public class Export {
a62d533 28
	
a62d533 29
	private static final String[] frameWorkClasses = {
a9b2a64 30
		"scene2d", "scene3d",
a62d533 31
		
a62d533 32
		"com/badlogic", "com/esotericsoftware", "gdx",
a62d533 33
		
a62d533 34
		"com/jcraft", "javazoom", "org/lwjgl", "sfd.ser",
a62d533 35
		
a62d533 36
		"lwjgl", "OpenAL", "openal",
a62d533 37
	};
a62d533 38
	
a62d533 39
	private static final String[] excludeClasses = {
a9b2a64 40
		"org/fife", "org/eclipse" , "com/threerings", "gdxstudio", "tools", "web/laf",
a9b2a64 41
		"android", "antlr"
a62d533 42
	};
a62d533 43
	
a62d533 44
	private static final String eclipse_project =
a62d533 45
	"<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
a62d533 46
	+"<projectDescription>"
a62d533 47
	+	"<name>$$$</name>"
a62d533 48
	+		"<comment></comment>"
a62d533 49
	+	"<projects>"
a62d533 50
	+	"</projects>"
a62d533 51
	+	"<buildSpec>"
a62d533 52
	+	"<buildCommand>"
a62d533 53
	+		"<name>org.eclipse.jdt.core.javabuilder</name>"
a62d533 54
	+		"<arguments>"
a62d533 55
	+		"</arguments>"
a62d533 56
	+	"</buildCommand>"
a62d533 57
	+	"</buildSpec>"
a62d533 58
	+	"<natures>"
a62d533 59
	+	"<nature>org.eclipse.jdt.core.javanature</nature>"
a62d533 60
	+	"</natures>"
a62d533 61
	+"</projectDescription>";
a62d533 62
	
a62d533 63
	/*private static final String eclipse_cp =
a62d533 64
	"<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
a62d533 65
	+"<classpath>"
a62d533 66
		<classpathentry kind="src" path="atlas"/>"
a62d533 67
		<classpathentry kind="src" path="font"/>
a62d533 68
		<classpathentry kind="src" path="map"/>
a62d533 69
		<classpathentry kind="src" path="model"/>
a62d533 70
		<classpathentry kind="src" path="music"/>
a62d533 71
		<classpathentry kind="src" path="particle"/>
a62d533 72
		<classpathentry kind="src" path="skin"/>
a62d533 73
		<classpathentry kind="src" path="sound"/>
a62d533 74
		<classpathentry kind="src" path="source"/>
a62d533 75
		<classpathentry kind="src" path="splash"/>
a62d533 76
		<classpathentry kind="src" path="actor"/>
a62d533 77
		<classpathentry kind="src" path="asset"/>
a62d533 78
		<classpathentry kind="src" path="config"/>
a62d533 79
		<classpathentry kind="src" path="scene"/>
a62d533 80
		<classpathentry kind="src" path="icon"/>
a62d533 81
		<classpathentry kind="lib" path="pathtogdxsutiod.jar"/>
a62d533 82
		<classpathentry kind="output" path="bin"/>
a62d533 83
		<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
a62d533 84
	+"</classpath>";*/
a62d533 85
a62d533 86
	
a62d533 87
	public static void createProject(String foldername){
a9b2a64 88
		GdxStudio.log("Creating Project: "+Content.getProject());
a62d533 89
		Content.setProject(foldername);
a62d533 90
		new File(Content.getProject()+"atlas").mkdir();
a62d533 91
		new File(Content.getProject()+"font").mkdir();
a62d533 92
		new File(Content.getProject()+"map").mkdir();
a62d533 93
		new File(Content.getProject()+"model").mkdir();
a62d533 94
		new File(Content.getProject()+"music").mkdir();
a62d533 95
		new File(Content.getProject()+"sound").mkdir();
a62d533 96
		new File(Content.getProject()+"source").mkdir();
a62d533 97
		new File(Content.getProject()+"skin").mkdir();
a62d533 98
		new File(Content.getProject()+"pack").mkdir();
a62d533 99
		new File(Content.getProject()+"particle").mkdir();
a62d533 100
		new File(Content.getProject()+"META-INF").mkdir();
a62d533 101
		ArrayMap<String, String> map = new ArrayMap<String, String>();
a62d533 102
		map.put("Title", "");
a62d533 103
		map.put("TargetSize", "");
a62d533 104
		map.put("ScreenSize", "");
a62d533 105
		map.put("AudioBufferCount", "");
a62d533 106
    	map.put("Resize", "");
a62d533 107
    	map.put("ForceExit", "");
a62d533 108
    	map.put("FullScreen", "");
a62d533 109
    	map.put("UseGL20", "");
a62d533 110
    	map.put("VSync", "");
a62d533 111
    	map.put("DisableAudio", "");
a62d533 112
    	map.put("KeepAspectRatio", "");
a62d533 113
    	map.put("ShowFPS", "");
a62d533 114
    	map.put("LoggingEnabled", "");
a62d533 115
    	map.put("Version", "");
a62d533 116
    	map.put("Target", "");
a62d533 117
    	File prjFile = new File(Content.getProject());
a9b2a64 118
		Export.writeFile("config", Scene.json.toJson(map, ArrayMap.class));
a62d533 119
		Export.writeFile(".project", eclipse_project.replace("$$$", prjFile.getName()));
a62d533 120
		Frame.setEnabledProject();
a62d533 121
		createJar();
a62d533 122
	}
a62d533 123
	
a62d533 124
	public static void openProject(String foldername){
a9b2a64 125
		GdxStudio.log("Opening Project: "+foldername);
a62d533 126
		Content.setProject(foldername);
a62d533 127
		Frame.setEnabledProject();
a62d533 128
	}
a62d533 129
	
a62d533 130
	public static void writeFile(String filename, String data){
a62d533 131
		if(!Content.projectExists())
a62d533 132
			return;
a9b2a64 133
		GdxStudio.log("Writing File: "+filename);
a62d533 134
		File file = new File(Content.getProject()+filename);
a62d533 135
		try {
a62d533 136
			file.createNewFile();
a62d533 137
			FileOutputStream fos = new FileOutputStream(file);
a62d533 138
			fos.write(data.getBytes());
a62d533 139
			fos.close();
a62d533 140
		} catch (IOException e) {
a62d533 141
			JOptionPane.showMessageDialog(null, "Couldn't Save File: "+filename, "Error", 
a62d533 142
					JOptionPane.ERROR_MESSAGE);
a62d533 143
			e.printStackTrace();
a62d533 144
		}
a62d533 145
	}
a62d533 146
	
a62d533 147
	public static String readFile(String filename){
a62d533 148
		if(!Content.projectExists())
a62d533 149
			return "";
a9b2a64 150
		GdxStudio.log("Reading File: "+filename);
a62d533 151
		StringBuffer sb = new StringBuffer();
a62d533 152
		File file = new File(Content.getProject()+filename);
a62d533 153
		if(!file.exists())
a62d533 154
			return "";
a62d533 155
		BufferedReader br = null;
a62d533 156
		try {
a62d533 157
			FileReader fr = new FileReader(file);
a62d533 158
			br = new BufferedReader(fr);
a62d533 159
			String line;
a62d533 160
            while ((line=br.readLine())!=null){
a62d533 161
            	sb.append(line);
a62d533 162
            	sb.append(System.lineSeparator());;
a62d533 163
            }
a62d533 164
            br.close();
a62d533 165
			fr.close();
a62d533 166
		} catch (IOException e) {
a62d533 167
			JOptionPane.showMessageDialog(null, "Couldn't Read File: "+filename, "Error", 
a62d533 168
					JOptionPane.ERROR_MESSAGE);
a62d533 169
			e.printStackTrace();
a62d533 170
		}
a62d533 171
		return sb.toString();
a62d533 172
	}
a62d533 173
	
a62d533 174
	public static String readFileFromClassPath(String filename){
a62d533 175
		StringBuffer sb = new StringBuffer();
a62d533 176
		try {
a62d533 177
			BufferedReader br = new BufferedReader(new InputStreamReader(
a62d533 178
					Export.class.getClassLoader().getResourceAsStream(filename)));
a62d533 179
			for (int c = br.read(); c != -1; c = br.read()) 
a62d533 180
				sb.append((char)c);
a62d533 181
		} catch (IOException e) {
a62d533 182
			e.printStackTrace();
a62d533 183
		}
a62d533 184
		return sb.toString();
a62d533 185
	}
a62d533 186
	
a62d533 187
	public static ArrayList<String> listFiles(String foldername){
a62d533 188
		if(!Content.projectExists())
a62d533 189
			return null;
a62d533 190
		ArrayList<String> list = new ArrayList<String>();
a62d533 191
		File folder = new File(Content.getProject()+foldername);
a62d533 192
		if(folder.exists())
a62d533 193
			for (String member : folder.list())
a62d533 194
				list.add(member);
a62d533 195
		return list;
a62d533 196
	}
a62d533 197
	
a62d533 198
	public static void deleteFile(String filename){
a62d533 199
		if(!Content.projectExists())
a62d533 200
			return;
a9b2a64 201
		GdxStudio.log("Deleting File: "+filename);
a62d533 202
		try {
a62d533 203
			Files.delete(new File(Content.getProject()+filename).toPath());
a62d533 204
		} catch (IOException e) {
a62d533 205
			e.printStackTrace();
a62d533 206
		}
a62d533 207
	}
a62d533 208
	
a62d533 209
	static File prjFile;
a62d533 210
	static List<File> fileList = new ArrayList<File>();
a62d533 211
	static Manifest manifest = new Manifest();
a62d533 212
	static{
a62d533 213
		manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0");
a62d533 214
		manifest.getMainAttributes().put(Attributes.Name.CLASS_PATH, ". ");
a9b2a64 215
		manifest.getMainAttributes().put(Attributes.Name.MAIN_CLASS, "scene2d.desktop.Main");
a62d533 216
		manifest.getMainAttributes().put(Attributes.Name.IMPLEMENTATION_VENDOR, "pyros2097");
a62d533 217
	}
a62d533 218
a62d533 219
	public static void createJar(){
a62d533 220
		if(!Content.projectExists())
a62d533 221
			return;
a62d533 222
		String filename = Export.class.getProtectionDomain().getCodeSource().getLocation().getFile();
a62d533 223
		if(!filename.endsWith(".jar"))
a62d533 224
			return;
a9b2a64 225
		GdxStudio.log("Exporting Project: "+Content.getProject());
a62d533 226
		fileList.clear();
a62d533 227
		prjFile = new File(Content.getProject());
a62d533 228
		generateFileList(prjFile);
a62d533 229
		byte[] buffer = new byte[1024];
a62d533 230
		try{
a62d533 231
			FileOutputStream fos = new FileOutputStream(Content.getProject()+prjFile.getName()+".jar");
a62d533 232
			JarOutputStream jos = new JarOutputStream(fos, manifest);
a62d533 233
			JarEntry je;
a62d533 234
			for(File file : fileList){
a62d533 235
				if(file.getParentFile().equals(prjFile) || file.getParentFile().getName().equals("bin"))
a62d533 236
					je = new JarEntry(file.getName());
a62d533 237
				else
a62d533 238
					je = new JarEntry(file.getParentFile().getName()+"/"+file.getName());
a62d533 239
				jos.putNextEntry(je);
a62d533 240
				FileInputStream in = new FileInputStream(file);
a62d533 241
				int len;
a62d533 242
				while ((len = in.read(buffer)) > 0) {
a62d533 243
					jos.write(buffer, 0, len);
a62d533 244
				}
a62d533 245
				in.close();
a62d533 246
				jos.closeEntry();
a62d533 247
			}
a62d533 248
			addLibrary(jos);
a62d533 249
			jos.close();
a62d533 250
		}catch(IOException ex){
a62d533 251
			ex.printStackTrace();   
a62d533 252
		}
a62d533 253
		JOptionPane.showMessageDialog(null, "Exported: "+prjFile.getName()
a62d533 254
				+" to "+Content.getProject()+prjFile.getName()+".jar", "Export", 
a62d533 255
				JOptionPane.INFORMATION_MESSAGE);
a62d533 256
	}
a62d533 257
a62d533 258
	private static void generateFileList(File node){
a62d533 259
		if(node.isFile()){
a9b2a64 260
			if(!node.getName().endsWith(".java") && !node.getName().equals(prjFile.getName()+".jar") 
a9b2a64 261
				 && !node.getName().equals(prjFile.getName()+".apk")){
a62d533 262
				fileList.add(node);
a62d533 263
			}
a62d533 264
		}
a62d533 265
		else{
a9b2a64 266
			for(String filename : node.list()){
a9b2a64 267
				if(!filename.equals("pack"))
a9b2a64 268
					generateFileList(new File(node, filename));
a9b2a64 269
			}
a62d533 270
		}
a62d533 271
	}
a62d533 272
	
a62d533 273
	private static void addLibrary(JarOutputStream jos){
a62d533 274
		String filename = Export.class.getProtectionDomain().getCodeSource().getLocation().getFile();
a62d533 275
		if(!filename.endsWith(".jar"))
a62d533 276
			return;
a62d533 277
		ZipFile jf = null;
a62d533 278
		byte[] buffer = new byte[512];
a62d533 279
		try {
a62d533 280
			jf = new ZipFile(filename);
a62d533 281
			Enumeration<? extends ZipEntry> e = jf.entries();
a62d533 282
			while (e.hasMoreElements()) 
a62d533 283
			{
a62d533 284
				ZipEntry je = (ZipEntry) e.nextElement();
a62d533 285
				String entryName = je.getName();
a62d533 286
				for(String name: frameWorkClasses){
a62d533 287
					if(!entryName.contains(excludeClasses[0]) && !entryName.contains(excludeClasses[1]) 
a62d533 288
						&& !entryName.contains(excludeClasses[2]) && !entryName.contains(excludeClasses[3]) 
a62d533 289
						&& !entryName.contains(excludeClasses[4]) && !entryName.contains(excludeClasses[5])
a9b2a64 290
						&& !entryName.contains(excludeClasses[6]) && !entryName.contains(excludeClasses[7])
a62d533 291
						&& entryName.contains(name)) {
a9b2a64 292
							//GdxStudio.log(entryName);
a62d533 293
							ZipEntry newEntry = new ZipEntry(entryName); 
a62d533 294
							jos.putNextEntry(newEntry);
a62d533 295
							InputStream in = jf.getInputStream(je);
a62d533 296
					        while (0 < in.available()){
a62d533 297
					            int read = in.read(buffer);
a62d533 298
					            jos.write(buffer,0,read);
a62d533 299
					        }
a62d533 300
					        in.close();
a62d533 301
							jos.closeEntry();
a62d533 302
							break;
a62d533 303
						}
a62d533 304
				}
a62d533 305
			}
a62d533 306
		} catch (IOException e1) {
a62d533 307
			e1.printStackTrace();
a62d533 308
		}
a62d533 309
		finally{
a62d533 310
			try {
a62d533 311
				if(jf != null)
a62d533 312
					jf.close();
a62d533 313
			} catch (IOException e) {
a62d533 314
				e.printStackTrace();
a62d533 315
			}
a62d533 316
		}
a62d533 317
	}
a62d533 318
	
a62d533 319
	public static void createDex(){
a62d533 320
		File prjFile = new File(Content.getProject());
a62d533 321
		String name = prjFile.getName();
a62d533 322
		String buildArgs = "--no-strict --output="+Content.getProject()+name+".apk "+Content.getProject()+"/bin";
a62d533 323
		try {
a62d533 324
			com.android.dx.command.dexer.Main.main(buildArgs.split(" "));
a62d533 325
		} catch (IOException e) {
a62d533 326
			e.printStackTrace();
a62d533 327
		}
a62d533 328
		JOptionPane.showMessageDialog(null, "Exported: "+name+" to "+Content.getProject()+name+".apk",
a62d533 329
				"Export", JOptionPane.INFORMATION_MESSAGE);
a62d533 330
	}
a62d533 331
	
a62d533 332
}