Permalink
Browse files

Added support for _CLIENT and _SERVER directories in the builder.

  • Loading branch information...
sk89q committed Jun 10, 2013
1 parent 1cb9599 commit e21d6d5bf4651d04b8574234c0f47950d83b42e5
@@ -0,0 +1,41 @@
+/*
+ * SK's Minecraft Launcher
+ * Copyright (C) 2010, 2011 Albert Pham <http://www.sk89q.com>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+*/
+
+package com.sk89q.lpbuilder;
+
+import java.io.File;
+
+class RelativizedFile {
+
+ private final String path;
+ private final File file;
+
+ public RelativizedFile(String path, File file) {
+ this.path = path;
+ this.file = file;
+ }
+
+ public String getPath() {
+ return path;
+ }
+
+ public File getFile() {
+ return file;
+ }
+
+}
@@ -0,0 +1,36 @@
+/*
+ * SK's Minecraft Launcher
+ * Copyright (C) 2010, 2011 Albert Pham <http://www.sk89q.com>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+*/
+
+package com.sk89q.lpbuilder;
+
+public enum Role {
+
+ SERVER("_SERVER"),
+ CLIENT("_CLIENT");
+
+ private String dirName;
+
+ Role(String dirName) {
+ this.dirName = dirName;
+ }
+
+ public String getDirectoryName() {
+ return dirName;
+ }
+
+}
@@ -50,8 +50,8 @@
import com.sk89q.mclauncher.model.UpdateManifest;
import com.sk89q.mclauncher.util.BasicArgsParser;
import com.sk89q.mclauncher.util.BasicArgsParser.ArgsContext;
-import com.sk89q.mclauncher.util.SimpleLogFormatter;
import com.sk89q.mclauncher.util.LauncherUtils;
+import com.sk89q.mclauncher.util.SimpleLogFormatter;
/**
* Builds an update package for SKMCLauncher.
@@ -66,14 +66,15 @@
private final Map<String, ZipBucket> buckets = new HashMap<String, ZipBucket>();
private final FileSignatureBuilder versionBuilder = new FileSignatureBuilder();
+ private Role role = Role.CLIENT;
private UpdateManifest updateManifest;
private PackageManifest packageManifest;
private UpdateBuilderConfig config = new UpdateBuilderConfig();
private String updateFilename = "update.xml";
private String packageFilename = "package.xml";
/**
- * Create a new buidler with the given source directory and output directory.
+ * Create a new builder with the given source directory and output directory.
*
* @param updateDir the directory with the source files
* @param outputDir the output directory
@@ -88,6 +89,36 @@ public UpdateBuilder(File updateDir, File outputDir) {
setPackageManifest(new PackageManifest());
}
+ public Role getRole() {
+ return role;
+ }
+
+ public void setRole(Role role) {
+ this.role = role;
+ }
+
+ private boolean isForCurrentRole(File dir) {
+ if (role == null) {
+ return false;
+ }
+
+ return dir.getName().equals(role.getDirectoryName());
+ }
+
+ private boolean shouldRoleIgnore(File dir) {
+ for (Role role : Role.values()) {
+ if (this.role != null && role.equals(this.role)) {
+ continue;
+ }
+
+ if (role.getDirectoryName().equals(dir.getName())) {
+ return true;
+ }
+ }
+
+ return false;
+ }
+
public String getUpdateFilename() {
return updateFilename;
}
@@ -165,37 +196,54 @@ public void loadConfiguration(File file) throws FileNotFoundException, JAXBExcep
* @throws InterruptedException on interruption
*/
private void collectFiles() throws IOException, InterruptedException {
- collectFiles(updateDir);
+ collectFiles(updateDir, "", updateDir, null);
}
/**
* Collect all the files needed for this update in the given folder.
*
- * @param dir the directory
+ * @param baseDir the base directory
+ * @param relativeDir the relative directory
+ * @param dir the actual directory with files
+ * @param group file group to use, or null to create one
* @throws IOException on I/O error
* @throws InterruptedException on interruption
*/
- private void collectFiles(File dir) throws IOException, InterruptedException {
- String relative = getRelative(updateDir, dir);
-
+ private void collectFiles(
+ File baseDir, String relativeDir, File dir, FileGroup group)
+ throws IOException, InterruptedException {
logger.info("Collecting files in '" + dir.getAbsolutePath() + "'");
- FileGroup group = new FileGroup();
- group.setDest(relative);
- group.setSource(relative);
+ boolean addGroup = false;
+
+ if (group == null) {
+ group = new FileGroup();
+ group.setDest(relativeDir);
+ group.setSource(relativeDir);
+ addGroup = true;
+ }
for (File f : dir.listFiles()) {
if (Thread.interrupted()) {
throw new InterruptedException();
}
if (f.isDirectory()) {
- collectFiles(f);
+ if (shouldRoleIgnore(f)) {
+ continue;
+ } else if (isForCurrentRole(f)) {
+ collectFiles(baseDir, relativeDir, f, group);
+ } else {
+ collectFiles(baseDir,
+ LauncherUtils.joinUnixPath(relativeDir, f.getName()),
+ f, null);
+ }
} else {
- String fileRelative = getRelative(updateDir, f);
- logger.info("-> " + fileRelative);
+ String fileRelative = LauncherUtils.joinUnixPath(
+ relativeDir, f.getName());
+ logger.info("-> " + LauncherUtils.getRelative(baseDir, f));
- SingleFile singleFile = createSingleFile(f);
+ SingleFile singleFile = createSingleFile(fileRelative, f);
if (singleFile != null) {
singleFile.setSize(f.length());
singleFile.setVersion(versionBuilder.smartFromFile(f));
@@ -206,7 +254,7 @@ private void collectFiles(File dir) throws IOException, InterruptedException {
}
}
- if (group.getFiles().size() > 0) {
+ if (addGroup && group.getFiles().size() > 0) {
packageManifest.getFileGroups().add(group);
}
}
@@ -215,12 +263,11 @@ private void collectFiles(File dir) throws IOException, InterruptedException {
* Create the {@link SingleFile} for a given file, otherwise return null if the
* file needs to go into an archive.
*
- * @param path the path
+ * @param path the relative path
+ * @param file the file
* @return a package file, or null if it's not a {@link SingleFile}
*/
- private SingleFile createSingleFile(File file) {
- String path = getRelative(updateDir, file);
-
+ private SingleFile createSingleFile(String path, File file) {
SingleFile singleFile = new SingleFile();
String archiveName = null;
@@ -241,7 +288,7 @@ private SingleFile createSingleFile(File file) {
// We are .zipping this file up, so we do things differently
if (archiveName != null) {
- storeFileInArchive(archiveName, file, singleFile);
+ storeFileInArchive(path, archiveName, file, singleFile);
return null;
} else {
return singleFile;
@@ -252,18 +299,20 @@ private SingleFile createSingleFile(File file) {
* Store a file into an archive.
*
* @param archiveName the archive name
+ * @param path the relative path
* @param file the file
* @param packageFile file to inherit archive properties from
*/
- private void storeFileInArchive(String archiveName, File file, PackageFile packageFile) {
+ private void storeFileInArchive(
+ String archiveName, String path, File file, PackageFile packageFile) {
archiveName = archiveName.trim().toLowerCase();
ZipBucket bucket = buckets.get(archiveName);
if (bucket == null) {
bucket = new ZipBucket();
buckets.put(archiveName, bucket);
}
bucket.inheritGenericProperties(packageFile);
- bucket.queue(file);
+ bucket.queue(path, file);
}
/**
@@ -430,10 +479,6 @@ private static void copyFile(File sourceFile, File destFile) throws IOException
LauncherUtils.close(destination);
}
}
-
- private static String getRelative(File base, File path) {
- return base.toURI().relativize(path.toURI()).getPath();
- }
private static void checkArgSet(Object obj, String message) {
if (obj == null) {
@@ -33,10 +33,10 @@
class ZipBucket extends Archive {
- private final List<File> contents = new ArrayList<File>();
+ private final List<RelativizedFile> contents = new ArrayList<RelativizedFile>();
- void queue(File file) {
- contents.add(file);
+ void queue(String path, File file) {
+ contents.add(new RelativizedFile(path, file));
}
public void writeContents(FileSignatureBuilder builder, File baseDir, File target)
@@ -52,12 +52,11 @@ public void writeContents(FileSignatureBuilder builder, File baseDir, File targe
fos = new FileOutputStream(target);
zip = new ZipOutputStream(fos);
- for (File file : contents) {
- String relativePath = getRelative(baseDir, file);
- zip.putNextEntry(new ZipEntry(relativePath));
- list.add(relativePath, builder.fromFile(file));
+ for (RelativizedFile file : contents) {
+ zip.putNextEntry(new ZipEntry(file.getPath()));
+ list.add(file.getPath(), builder.fromFile(file.getFile()));
- fis = new FileInputStream(file);
+ fis = new FileInputStream(file.getFile());
int count;
while ((count = fis.read(buf)) > 0) {
zip.write(buf, 0, count);
@@ -74,8 +73,4 @@ public void writeContents(FileSignatureBuilder builder, File baseDir, File targe
}
}
- private static String getRelative(File base, File path) {
- return base.toURI().relativize(path.toURI()).getPath();
- }
-
}
@@ -376,4 +376,30 @@ public static void checkInterrupted() throws InterruptedException {
}
}
+ /**
+ * Get a relative path to a base directory.
+ *
+ * @param base the base directory
+ * @param path the path to make relative
+ * @return the relativized path
+ */
+ public static String getRelative(File base, File path) {
+ return base.toURI().relativize(path.toURI()).getPath();
+ }
+
+ /**
+ * Join two paths with a forward slash (/) in between (if needed).
+ *
+ * @param path path 1
+ * @param path2 path 2
+ * @return new path
+ */
+ public static String joinUnixPath(String path, String path2) {
+ if (!path.isEmpty()) {
+ return path + "/" + path2;
+ } else {
+ return path2;
+ }
+ }
+
}

0 comments on commit e21d6d5

Please sign in to comment.