mirror of
https://github.com/OMGeeky/ATCS.git
synced 2026-01-17 17:03:29 +01:00
reformat all code
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
package com.gpl.rpg.atcontentstudio.utils;
|
||||
|
||||
import java.awt.Desktop;
|
||||
import com.gpl.rpg.atcontentstudio.model.Workspace;
|
||||
|
||||
import java.awt.*;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
@@ -8,101 +10,99 @@ import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
|
||||
import com.gpl.rpg.atcontentstudio.model.Workspace;
|
||||
|
||||
public class DesktopIntegration {
|
||||
|
||||
public static void openTmxMap(File f) {
|
||||
if (Workspace.activeWorkspace.settings.useSystemDefaultMapEditor.getCurrentValue()) {
|
||||
try {
|
||||
Desktop.getDesktop().open(f);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
} else {
|
||||
try {
|
||||
Runtime.getRuntime().exec(tokenize(Workspace.activeWorkspace.settings.mapEditorCommand.getCurrentValue()+" \""+f.getAbsolutePath()+"\""));
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void openImage(File f) {
|
||||
if (Workspace.activeWorkspace.settings.useSystemDefaultImageViewer.getCurrentValue()) {
|
||||
try {
|
||||
Desktop.getDesktop().open(f);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
} else if (Workspace.activeWorkspace.settings.useSystemDefaultImageEditor.getCurrentValue()) {
|
||||
try {
|
||||
Desktop.getDesktop().edit(f);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
} else {
|
||||
try {
|
||||
Runtime.getRuntime().exec(tokenize(Workspace.activeWorkspace.settings.imageEditorCommand.getCurrentValue()+" \""+f.getAbsolutePath()+"\""));
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
public static enum OSType {
|
||||
Windows, MacOS, NIX, Other
|
||||
}
|
||||
|
||||
public static OSType detectedOS = detectOS();
|
||||
|
||||
private static OSType detectOS() {
|
||||
String os = System.getProperty("os.name", "generic").toLowerCase(Locale.ENGLISH);
|
||||
if ((os.indexOf("mac") >= 0) || (os.indexOf("darwin") >= 0)) return OSType.MacOS;
|
||||
if (os.indexOf("win") >= 0) return OSType.Windows;
|
||||
if ((os.indexOf("nux") >= 0) || (os.indexOf("nix") >= 0) || (os.indexOf("aix") >= 0) || (os.indexOf("sunos") >= 0) || (os.indexOf("solaris") >= 0)) return OSType.NIX;
|
||||
return OSType.Other;
|
||||
}
|
||||
|
||||
|
||||
private static List<Character> quotes = Arrays.asList(new Character[]{'\'', '"'});
|
||||
private static List<Character> delims = Arrays.asList(new Character[]{' ', '\r', '\n', '\t'});
|
||||
|
||||
private static String[] tokenize(String command) {
|
||||
List<String> tokens = new ArrayList<String>();
|
||||
boolean inQuote = false;
|
||||
char usedQuote = '\0';
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
for (char c : command.toCharArray()) {
|
||||
if (inQuote) {
|
||||
if (c == usedQuote) {
|
||||
inQuote = false;
|
||||
continue;
|
||||
} else {
|
||||
sb.append(c);
|
||||
}
|
||||
} else {
|
||||
if (quotes.contains(c)) {
|
||||
inQuote = true;
|
||||
usedQuote = c;
|
||||
} else if (delims.contains(c)) {
|
||||
if (sb.length() > 0) {
|
||||
tokens.add(sb.toString());
|
||||
sb = new StringBuilder();
|
||||
}
|
||||
} else {
|
||||
sb.append(c);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (sb.length() > 0) {
|
||||
tokens.add(sb.toString());
|
||||
}
|
||||
return tokens.toArray(new String[tokens.size()]);
|
||||
}
|
||||
|
||||
|
||||
public static void openTmxMap(File f) {
|
||||
if (Workspace.activeWorkspace.settings.useSystemDefaultMapEditor.getCurrentValue()) {
|
||||
try {
|
||||
Desktop.getDesktop().open(f);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
} else {
|
||||
try {
|
||||
Runtime.getRuntime().exec(tokenize(Workspace.activeWorkspace.settings.mapEditorCommand.getCurrentValue() + " \"" + f.getAbsolutePath() + "\""));
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void openImage(File f) {
|
||||
if (Workspace.activeWorkspace.settings.useSystemDefaultImageViewer.getCurrentValue()) {
|
||||
try {
|
||||
Desktop.getDesktop().open(f);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
} else if (Workspace.activeWorkspace.settings.useSystemDefaultImageEditor.getCurrentValue()) {
|
||||
try {
|
||||
Desktop.getDesktop().edit(f);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
} else {
|
||||
try {
|
||||
Runtime.getRuntime().exec(tokenize(Workspace.activeWorkspace.settings.imageEditorCommand.getCurrentValue() + " \"" + f.getAbsolutePath() + "\""));
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static enum OSType {
|
||||
Windows, MacOS, NIX, Other
|
||||
}
|
||||
|
||||
public static OSType detectedOS = detectOS();
|
||||
|
||||
private static OSType detectOS() {
|
||||
String os = System.getProperty("os.name", "generic").toLowerCase(Locale.ENGLISH);
|
||||
if ((os.indexOf("mac") >= 0) || (os.indexOf("darwin") >= 0)) return OSType.MacOS;
|
||||
if (os.indexOf("win") >= 0) return OSType.Windows;
|
||||
if ((os.indexOf("nux") >= 0) || (os.indexOf("nix") >= 0) || (os.indexOf("aix") >= 0) || (os.indexOf("sunos") >= 0) || (os.indexOf("solaris") >= 0))
|
||||
return OSType.NIX;
|
||||
return OSType.Other;
|
||||
}
|
||||
|
||||
|
||||
private static List<Character> quotes = Arrays.asList(new Character[]{'\'', '"'});
|
||||
private static List<Character> delims = Arrays.asList(new Character[]{' ', '\r', '\n', '\t'});
|
||||
|
||||
private static String[] tokenize(String command) {
|
||||
List<String> tokens = new ArrayList<String>();
|
||||
boolean inQuote = false;
|
||||
char usedQuote = '\0';
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
for (char c : command.toCharArray()) {
|
||||
if (inQuote) {
|
||||
if (c == usedQuote) {
|
||||
inQuote = false;
|
||||
continue;
|
||||
} else {
|
||||
sb.append(c);
|
||||
}
|
||||
} else {
|
||||
if (quotes.contains(c)) {
|
||||
inQuote = true;
|
||||
usedQuote = c;
|
||||
} else if (delims.contains(c)) {
|
||||
if (sb.length() > 0) {
|
||||
tokens.add(sb.toString());
|
||||
sb = new StringBuilder();
|
||||
}
|
||||
} else {
|
||||
sb.append(c);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (sb.length() > 0) {
|
||||
tokens.add(sb.toString());
|
||||
}
|
||||
return tokens.toArray(new String[tokens.size()]);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -1,15 +1,6 @@
|
||||
package com.gpl.rpg.atcontentstudio.utils;
|
||||
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.BufferedOutputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.io.*;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
@@ -19,206 +10,209 @@ import java.util.zip.ZipOutputStream;
|
||||
|
||||
public class FileUtils {
|
||||
|
||||
public static void deleteDir(File dir) {
|
||||
if (dir.exists()) {
|
||||
for (File f : dir.listFiles()) {
|
||||
if (f.isDirectory()) {
|
||||
deleteDir(f);
|
||||
} else {
|
||||
f.delete();
|
||||
}
|
||||
}
|
||||
dir.delete();
|
||||
}
|
||||
}
|
||||
|
||||
public static void copyFile(File sourceLocation , File targetLocation) {
|
||||
try {
|
||||
InputStream in = new FileInputStream(sourceLocation);
|
||||
OutputStream out = new FileOutputStream(targetLocation);
|
||||
public static void deleteDir(File dir) {
|
||||
if (dir.exists()) {
|
||||
for (File f : dir.listFiles()) {
|
||||
if (f.isDirectory()) {
|
||||
deleteDir(f);
|
||||
} else {
|
||||
f.delete();
|
||||
}
|
||||
}
|
||||
dir.delete();
|
||||
}
|
||||
}
|
||||
|
||||
// Copy the bits from instream to outstream
|
||||
byte[] buf = new byte[1024];
|
||||
int len;
|
||||
try {
|
||||
while ((len = in.read(buf)) > 0) {
|
||||
out.write(buf, 0, len);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
// TODO Auto-generated catch block
|
||||
} finally {
|
||||
try {
|
||||
in.close();
|
||||
} catch (IOException e) {
|
||||
// TODO Auto-generated catch block
|
||||
}
|
||||
try {
|
||||
out.close();
|
||||
} catch (IOException e) {
|
||||
// TODO Auto-generated catch block
|
||||
}
|
||||
}
|
||||
} catch (FileNotFoundException e1) {
|
||||
// TODO Auto-generated catch block
|
||||
}
|
||||
}
|
||||
|
||||
private static final int BUFFER = 2048;
|
||||
public static void writeToZip(File folder, File target) {
|
||||
try {
|
||||
FileOutputStream dest = new FileOutputStream(target);
|
||||
ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(dest));
|
||||
zipDir(folder, "", out);
|
||||
out.flush();
|
||||
out.close();
|
||||
} catch (Exception e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
public static void copyFile(File sourceLocation, File targetLocation) {
|
||||
try {
|
||||
InputStream in = new FileInputStream(sourceLocation);
|
||||
OutputStream out = new FileOutputStream(targetLocation);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* cp sourceFolder/* targetFolder/
|
||||
* @param sourceFolder
|
||||
* @param targetFolder
|
||||
*/
|
||||
public static void copyOver(File sourceFolder, File targetFolder) {
|
||||
if (!sourceFolder.isDirectory() || !targetFolder.isDirectory()) return;
|
||||
for (File f : sourceFolder.listFiles()) {
|
||||
if (Files.isSymbolicLink(f.toPath())) {
|
||||
//Skip symlinks
|
||||
continue;
|
||||
} else if (f.isDirectory()) {
|
||||
File dest = new File(targetFolder, f.getName());
|
||||
if (!dest.exists()) {
|
||||
dest.mkdir();
|
||||
}
|
||||
copyOver(f, dest);
|
||||
} else {
|
||||
copyFile(f, new File(targetFolder, f.getName()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void zipDir(File dir, String prefix, ZipOutputStream zos) {
|
||||
if (prefix != "") {
|
||||
prefix = prefix + File.separator;
|
||||
}
|
||||
for (File f : dir.listFiles()) {
|
||||
if (f.isDirectory()) {
|
||||
zipDir(f, prefix+f.getName(), zos);
|
||||
} else {
|
||||
FileInputStream fis;
|
||||
try {
|
||||
fis = new FileInputStream(f);
|
||||
BufferedInputStream origin = new BufferedInputStream(fis, BUFFER);
|
||||
ZipEntry entry = new ZipEntry(prefix+f.getName());
|
||||
try {
|
||||
zos.putNextEntry(entry);
|
||||
int count;
|
||||
byte data[] = new byte[BUFFER];
|
||||
while ((count = origin.read(data, 0, BUFFER)) != -1) {
|
||||
zos.write(data, 0, count);
|
||||
zos.flush();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
try {
|
||||
origin.close();
|
||||
} catch (IOException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
} catch (FileNotFoundException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean makeSymlink(File targetFile, File linkFile) {
|
||||
Path target = Paths.get(targetFile.getAbsolutePath());
|
||||
Path link = Paths.get(linkFile.getAbsolutePath());
|
||||
if (!Files.exists(link)) {
|
||||
try {
|
||||
Files.createSymbolicLink(link, target);
|
||||
} catch (Exception e) {
|
||||
System.err.println("Failed to create symbolic link to target \""+targetFile.getAbsolutePath()+"\" as \""+linkFile.getAbsolutePath()+"\" the java.nio way:");
|
||||
e.printStackTrace();
|
||||
switch (DesktopIntegration.detectedOS) {
|
||||
case Windows:
|
||||
System.err.println("Trying the Windows way with mklink");
|
||||
try {
|
||||
Runtime.getRuntime().exec("cmd.exe /C mklink "+(targetFile.isDirectory() ? "/J " : "")+"\""+linkFile.getAbsolutePath()+"\" \""+targetFile.getAbsolutePath()+"\"");
|
||||
} catch (IOException e1) {
|
||||
e1.printStackTrace();
|
||||
}
|
||||
if (!linkFile.exists()) {
|
||||
System.err.println("Attempting UAC elevation through VBS script.");
|
||||
runWithUac("cmd.exe /C mklink "+(targetFile.isDirectory() ? "/J " : "")+"\""+linkFile.getAbsolutePath()+"\" \""+targetFile.getAbsolutePath()+"\"", 3, linkFile);
|
||||
}
|
||||
break;
|
||||
case MacOS:
|
||||
case NIX:
|
||||
case Other:
|
||||
System.err.println("Trying the unix way with ln -s");
|
||||
try {
|
||||
Runtime.getRuntime().exec("ln -s "+targetFile.getAbsolutePath()+" "+linkFile.getAbsolutePath());
|
||||
} catch (IOException e1) {
|
||||
e1.printStackTrace();
|
||||
}
|
||||
break;
|
||||
default:
|
||||
System.out.println("Unrecognized OS. Please contact ATCS dev.");
|
||||
break;
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!Files.exists(link)) {
|
||||
System.err.println("Failed to create link \""+linkFile.getAbsolutePath()+"\" targetting \""+targetFile.getAbsolutePath()+"\"");
|
||||
System.err.println("You can try running ATCS with administrative privileges once, or create the symbolic link manually.");
|
||||
}
|
||||
return true;
|
||||
}
|
||||
// Copy the bits from instream to outstream
|
||||
byte[] buf = new byte[1024];
|
||||
int len;
|
||||
try {
|
||||
while ((len = in.read(buf)) > 0) {
|
||||
out.write(buf, 0, len);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
// TODO Auto-generated catch block
|
||||
} finally {
|
||||
try {
|
||||
in.close();
|
||||
} catch (IOException e) {
|
||||
// TODO Auto-generated catch block
|
||||
}
|
||||
try {
|
||||
out.close();
|
||||
} catch (IOException e) {
|
||||
// TODO Auto-generated catch block
|
||||
}
|
||||
}
|
||||
} catch (FileNotFoundException e1) {
|
||||
// TODO Auto-generated catch block
|
||||
}
|
||||
}
|
||||
|
||||
private static final int BUFFER = 2048;
|
||||
|
||||
public static void writeToZip(File folder, File target) {
|
||||
try {
|
||||
FileOutputStream dest = new FileOutputStream(target);
|
||||
ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(dest));
|
||||
zipDir(folder, "", out);
|
||||
out.flush();
|
||||
out.close();
|
||||
} catch (Exception e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* cp sourceFolder/* targetFolder/
|
||||
*
|
||||
* @param sourceFolder
|
||||
* @param targetFolder
|
||||
*/
|
||||
public static void copyOver(File sourceFolder, File targetFolder) {
|
||||
if (!sourceFolder.isDirectory() || !targetFolder.isDirectory()) return;
|
||||
for (File f : sourceFolder.listFiles()) {
|
||||
if (Files.isSymbolicLink(f.toPath())) {
|
||||
//Skip symlinks
|
||||
continue;
|
||||
} else if (f.isDirectory()) {
|
||||
File dest = new File(targetFolder, f.getName());
|
||||
if (!dest.exists()) {
|
||||
dest.mkdir();
|
||||
}
|
||||
copyOver(f, dest);
|
||||
} else {
|
||||
copyFile(f, new File(targetFolder, f.getName()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void zipDir(File dir, String prefix, ZipOutputStream zos) {
|
||||
if (prefix != "") {
|
||||
prefix = prefix + File.separator;
|
||||
}
|
||||
for (File f : dir.listFiles()) {
|
||||
if (f.isDirectory()) {
|
||||
zipDir(f, prefix + f.getName(), zos);
|
||||
} else {
|
||||
FileInputStream fis;
|
||||
try {
|
||||
fis = new FileInputStream(f);
|
||||
BufferedInputStream origin = new BufferedInputStream(fis, BUFFER);
|
||||
ZipEntry entry = new ZipEntry(prefix + f.getName());
|
||||
try {
|
||||
zos.putNextEntry(entry);
|
||||
int count;
|
||||
byte data[] = new byte[BUFFER];
|
||||
while ((count = origin.read(data, 0, BUFFER)) != -1) {
|
||||
zos.write(data, 0, count);
|
||||
zos.flush();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
try {
|
||||
origin.close();
|
||||
} catch (IOException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
} catch (FileNotFoundException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean makeSymlink(File targetFile, File linkFile) {
|
||||
Path target = Paths.get(targetFile.getAbsolutePath());
|
||||
Path link = Paths.get(linkFile.getAbsolutePath());
|
||||
if (!Files.exists(link)) {
|
||||
try {
|
||||
Files.createSymbolicLink(link, target);
|
||||
} catch (Exception e) {
|
||||
System.err.println("Failed to create symbolic link to target \"" + targetFile.getAbsolutePath() + "\" as \"" + linkFile.getAbsolutePath() + "\" the java.nio way:");
|
||||
e.printStackTrace();
|
||||
switch (DesktopIntegration.detectedOS) {
|
||||
case Windows:
|
||||
System.err.println("Trying the Windows way with mklink");
|
||||
try {
|
||||
Runtime.getRuntime().exec("cmd.exe /C mklink " + (targetFile.isDirectory() ? "/J " : "") + "\"" + linkFile.getAbsolutePath() + "\" \"" + targetFile.getAbsolutePath() + "\"");
|
||||
} catch (IOException e1) {
|
||||
e1.printStackTrace();
|
||||
}
|
||||
if (!linkFile.exists()) {
|
||||
System.err.println("Attempting UAC elevation through VBS script.");
|
||||
runWithUac("cmd.exe /C mklink " + (targetFile.isDirectory() ? "/J " : "") + "\"" + linkFile.getAbsolutePath() + "\" \"" + targetFile.getAbsolutePath() + "\"", 3, linkFile);
|
||||
}
|
||||
break;
|
||||
case MacOS:
|
||||
case NIX:
|
||||
case Other:
|
||||
System.err.println("Trying the unix way with ln -s");
|
||||
try {
|
||||
Runtime.getRuntime().exec("ln -s " + targetFile.getAbsolutePath() + " " + linkFile.getAbsolutePath());
|
||||
} catch (IOException e1) {
|
||||
e1.printStackTrace();
|
||||
}
|
||||
break;
|
||||
default:
|
||||
System.out.println("Unrecognized OS. Please contact ATCS dev.");
|
||||
break;
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!Files.exists(link)) {
|
||||
System.err.println("Failed to create link \"" + linkFile.getAbsolutePath() + "\" targetting \"" + targetFile.getAbsolutePath() + "\"");
|
||||
System.err.println("You can try running ATCS with administrative privileges once, or create the symbolic link manually.");
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public static File backupFile(File f) {
|
||||
try {
|
||||
Path returned = Files.copy(Paths.get(f.getAbsolutePath()), Paths.get(f.getAbsolutePath() + ".bak"), StandardCopyOption.REPLACE_EXISTING);
|
||||
return returned.toFile();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
static final String uacBatName = "ATCS_elevateWithUac.bat";
|
||||
|
||||
public static void runWithUac(String command, int tries, File checkExists) {
|
||||
File tmpFolder = new File(System.getProperty("java.io.tmpdir"));
|
||||
File batFile = new File(tmpFolder, uacBatName);
|
||||
batFile.deleteOnExit();
|
||||
FileWriter writer;
|
||||
try {
|
||||
writer = new FileWriter(batFile, false);
|
||||
writer.write(
|
||||
"@echo Set objShell = CreateObject(\"Shell.Application\") > %temp%\\sudo.tmp.vbs\r\n"
|
||||
+ "@echo args = Right(\"%*\", (Len(\"%*\") - Len(\"%1\"))) >> %temp%\\sudo.tmp.vbs\r\n"
|
||||
+ "@echo objShell.ShellExecute \"%1\", args, \"\", \"runas\" >> %temp%\\sudo.tmp.vbs\r\n"
|
||||
+ "@cscript %temp%\\sudo.tmp.vbs\r\n"
|
||||
+ "del /f %temp%\\sudo.tmp.vbs\r\n");
|
||||
writer.close();
|
||||
while (!checkExists.exists() && tries-- > 0) {
|
||||
Runtime.getRuntime().exec(new String[]{"cmd.exe", "/C", batFile.getAbsolutePath() + " " + command});
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static File backupFile(File f) {
|
||||
try {
|
||||
Path returned = Files.copy(Paths.get(f.getAbsolutePath()), Paths.get(f.getAbsolutePath()+".bak"), StandardCopyOption.REPLACE_EXISTING);
|
||||
return returned.toFile();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
static final String uacBatName = "ATCS_elevateWithUac.bat";
|
||||
public static void runWithUac(String command, int tries, File checkExists) {
|
||||
File tmpFolder = new File(System.getProperty("java.io.tmpdir"));
|
||||
File batFile = new File(tmpFolder, uacBatName);
|
||||
batFile.deleteOnExit();
|
||||
FileWriter writer;
|
||||
try {
|
||||
writer = new FileWriter(batFile, false);
|
||||
writer.write(
|
||||
"@echo Set objShell = CreateObject(\"Shell.Application\") > %temp%\\sudo.tmp.vbs\r\n"
|
||||
+ "@echo args = Right(\"%*\", (Len(\"%*\") - Len(\"%1\"))) >> %temp%\\sudo.tmp.vbs\r\n"
|
||||
+ "@echo objShell.ShellExecute \"%1\", args, \"\", \"runas\" >> %temp%\\sudo.tmp.vbs\r\n"
|
||||
+ "@cscript %temp%\\sudo.tmp.vbs\r\n"
|
||||
+ "del /f %temp%\\sudo.tmp.vbs\r\n");
|
||||
writer.close();
|
||||
while (!checkExists.exists() && tries-- > 0) {
|
||||
Runtime.getRuntime().exec(new String[]{"cmd.exe","/C", batFile.getAbsolutePath()+" "+command});
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,30 +1,30 @@
|
||||
package com.gpl.rpg.atcontentstudio.utils;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import com.zackehh.siphash.SipHash;
|
||||
import com.zackehh.siphash.SipHashResult;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class HashUtils {
|
||||
|
||||
private static final Map<String, SipHash> HASHER_CACHE = new LinkedHashMap<String, SipHash>();
|
||||
|
||||
static String siphash(String key, byte[] data) {
|
||||
SipHash hasher = HASHER_CACHE.get(key);
|
||||
if (hasher == null) {
|
||||
hasher= new SipHash("Weblate Sip Hash".getBytes());
|
||||
HASHER_CACHE.put(key, hasher);
|
||||
}
|
||||
|
||||
if (data != null) {
|
||||
SipHashResult result = hasher.hash(data);
|
||||
return result.getHex();
|
||||
}
|
||||
|
||||
|
||||
return null;
|
||||
|
||||
}
|
||||
|
||||
private static final Map<String, SipHash> HASHER_CACHE = new LinkedHashMap<String, SipHash>();
|
||||
|
||||
static String siphash(String key, byte[] data) {
|
||||
SipHash hasher = HASHER_CACHE.get(key);
|
||||
if (hasher == null) {
|
||||
hasher = new SipHash("Weblate Sip Hash".getBytes());
|
||||
HASHER_CACHE.put(key, hasher);
|
||||
}
|
||||
|
||||
if (data != null) {
|
||||
SipHashResult result = hasher.hash(data);
|
||||
return result.getHex();
|
||||
}
|
||||
|
||||
|
||||
return null;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -5,30 +5,30 @@ import java.awt.image.WritableRaster;
|
||||
|
||||
public final class SpriteUtils {
|
||||
|
||||
/**
|
||||
* Check if the image is empty (transparent )
|
||||
*
|
||||
* @param img The image to check
|
||||
* @return true if the image is empty
|
||||
*/
|
||||
public static boolean checkIsImageEmpty(BufferedImage img) {
|
||||
int width = img.getWidth(null);
|
||||
int height = img.getHeight(null);
|
||||
WritableRaster raster = img.getAlphaRaster();
|
||||
if (raster == null) {
|
||||
return false;
|
||||
}
|
||||
for (int x = 0; x < width; x++) {
|
||||
for (int y = 0; y < height; y++) {
|
||||
//get pixel alpha value
|
||||
int alpha = raster.getSample(x, y, 0);
|
||||
//if alpha is not 0 then the pixel is not transparent
|
||||
if (alpha != 0) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
//no non-transparent pixel found
|
||||
return true;
|
||||
}
|
||||
/**
|
||||
* Check if the image is empty (transparent )
|
||||
*
|
||||
* @param img The image to check
|
||||
* @return true if the image is empty
|
||||
*/
|
||||
public static boolean checkIsImageEmpty(BufferedImage img) {
|
||||
int width = img.getWidth(null);
|
||||
int height = img.getHeight(null);
|
||||
WritableRaster raster = img.getAlphaRaster();
|
||||
if (raster == null) {
|
||||
return false;
|
||||
}
|
||||
for (int x = 0; x < width; x++) {
|
||||
for (int y = 0; y < height; y++) {
|
||||
//get pixel alpha value
|
||||
int alpha = raster.getSample(x, y, 0);
|
||||
//if alpha is not 0 then the pixel is not transparent
|
||||
if (alpha != 0) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
//no non-transparent pixel found
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,18 +2,19 @@ package com.gpl.rpg.atcontentstudio.utils;
|
||||
|
||||
public class TextUtils {
|
||||
public static String wordWrap(String in, int length) {
|
||||
if(in == null || length <= 0) return in;
|
||||
if (in == null || length <= 0) return in;
|
||||
final String newline = "\n";
|
||||
//:: Trim
|
||||
while(in.length() > 0 && (in.charAt(0) == '\t' || in.charAt(0) == ' ')) in = in.substring(1);
|
||||
while (in.length() > 0 && (in.charAt(0) == '\t' || in.charAt(0) == ' ')) in = in.substring(1);
|
||||
//:: If Small Enough Already, Return Original
|
||||
if(in.length() < length) return in;
|
||||
if (in.length() < length) return in;
|
||||
//:: If Next length Contains Newline, Split There
|
||||
if(in.substring(0, length).contains(newline)) return in.substring(0, in.indexOf(newline)).trim() + newline + wordWrap(in.substring(in.indexOf("\n") + 1), length);
|
||||
if (in.substring(0, length).contains(newline))
|
||||
return in.substring(0, in.indexOf(newline)).trim() + newline + wordWrap(in.substring(in.indexOf("\n") + 1), length);
|
||||
//:: Otherwise, Split Along Nearest Previous Space/Tab/Dash
|
||||
int spaceIndex = Math.max(Math.max( in.lastIndexOf(" ", length), in.lastIndexOf("\t", length)), in.lastIndexOf("-", length));
|
||||
int spaceIndex = Math.max(Math.max(in.lastIndexOf(" ", length), in.lastIndexOf("\t", length)), in.lastIndexOf("-", length));
|
||||
//:: If No Nearest Space, Split At length
|
||||
if(spaceIndex == -1) spaceIndex = length;
|
||||
if (spaceIndex == -1) spaceIndex = length;
|
||||
//:: Split
|
||||
return in.substring(0, spaceIndex).trim() + newline + wordWrap(in.substring(spaceIndex), length);
|
||||
}
|
||||
|
||||
@@ -3,13 +3,16 @@ package com.gpl.rpg.atcontentstudio.utils;
|
||||
import com.gpl.rpg.atcontentstudio.ATContentStudio;
|
||||
import com.gpl.rpg.atcontentstudio.model.GameDataElement;
|
||||
import com.gpl.rpg.atcontentstudio.ui.CollapsiblePanel;
|
||||
import com.gpl.rpg.atcontentstudio.ui.OrderedListenerListModel;
|
||||
import com.gpl.rpg.atcontentstudio.ui.DefaultIcons;
|
||||
import com.gpl.rpg.atcontentstudio.ui.FieldUpdateListener;
|
||||
import com.gpl.rpg.atcontentstudio.ui.OrderedListenerListModel;
|
||||
import com.jidesoft.swing.JideBoxLayout;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.event.*;
|
||||
import java.awt.event.KeyAdapter;
|
||||
import java.awt.event.KeyEvent;
|
||||
import java.awt.event.MouseAdapter;
|
||||
import java.awt.event.MouseEvent;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
public class UiUtils {
|
||||
@@ -70,7 +73,7 @@ public class UiUtils {
|
||||
|
||||
addRemoveAndAddButtons(listener, itemsListModel, selectedItemReset, selectedItem, tempSupplier, createBtn, itemsList, listButtonsPane, deleteBtn);
|
||||
|
||||
if(withMoveButtons) {
|
||||
if (withMoveButtons) {
|
||||
addMoveButtonListeners(listener, itemsListModel, selectedItem, moveUpBtn, itemsList, listButtonsPane, moveDownBtn);
|
||||
}
|
||||
listButtonsPane.add(new JPanel(), JideBoxLayout.VARY);
|
||||
@@ -85,7 +88,8 @@ public class UiUtils {
|
||||
return new CollapsibleItemListCreation<E>() {
|
||||
{
|
||||
collapsiblePanel = itemsPane;
|
||||
list = itemsList;}
|
||||
list = itemsList;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@@ -140,7 +144,7 @@ public class UiUtils {
|
||||
GameDataElement referencedObj = getReferencedObj.doIt(selectedValue);
|
||||
if (referencedObj != null) {
|
||||
ATContentStudio.frame.openEditor(referencedObj);
|
||||
ATContentStudio.frame.selectInTree( referencedObj);
|
||||
ATContentStudio.frame.selectInTree(referencedObj);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,106 +1,105 @@
|
||||
package com.gpl.rpg.atcontentstudio.utils;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
|
||||
import com.gpl.rpg.atcontentstudio.model.Workspace;
|
||||
import com.gpl.rpg.atcontentstudio.utils.WeblateIntegration.WeblateTranslationUnit.Status;
|
||||
import org.jsoup.Jsoup;
|
||||
import org.jsoup.nodes.Document;
|
||||
import org.jsoup.nodes.Element;
|
||||
import org.jsoup.select.Elements;
|
||||
|
||||
import com.gpl.rpg.atcontentstudio.model.Workspace;
|
||||
import com.gpl.rpg.atcontentstudio.utils.WeblateIntegration.WeblateTranslationUnit.Status;
|
||||
import java.io.IOException;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
|
||||
public class WeblateIntegration {
|
||||
|
||||
static final String WEBLATE_SIPASH_KEY = "Weblate Sip Hash";
|
||||
static final String WEBLATE_SIPASH_KEY = "Weblate Sip Hash";
|
||||
|
||||
public static String weblateHash(String str, String ctx) {
|
||||
|
||||
byte[] data = null;
|
||||
|
||||
if (str != null) {
|
||||
byte[] strBytes;
|
||||
try {
|
||||
strBytes = str.getBytes("UTF-8");
|
||||
byte[] ctxBytes = ctx.getBytes("UTF-8");
|
||||
data = new byte[strBytes.length + ctxBytes.length];
|
||||
System.arraycopy(strBytes, 0, data, 0, strBytes.length);
|
||||
System.arraycopy(ctxBytes, 0, data, strBytes.length, ctxBytes.length);
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
} else {
|
||||
try {
|
||||
data = ctx.getBytes("UTF-8");
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
return HashUtils.siphash(WEBLATE_SIPASH_KEY, data);
|
||||
}
|
||||
public static String weblateHash(String str, String ctx) {
|
||||
|
||||
public static String getWeblateLabelURI(String text) {
|
||||
return "https://hosted.weblate.org/translate/andors-trail/game-content/"+Workspace.activeWorkspace.settings.translatorLanguage.getCurrentValue()+"/?checksum="+weblateHash(text, "");
|
||||
}
|
||||
byte[] data = null;
|
||||
|
||||
public static class WeblateTranslationUnit {
|
||||
public enum Status {
|
||||
notAllowed, error, absent, notTranslated, warning, fuzzy, done
|
||||
}
|
||||
if (str != null) {
|
||||
byte[] strBytes;
|
||||
try {
|
||||
strBytes = str.getBytes("UTF-8");
|
||||
byte[] ctxBytes = ctx.getBytes("UTF-8");
|
||||
data = new byte[strBytes.length + ctxBytes.length];
|
||||
System.arraycopy(strBytes, 0, data, 0, strBytes.length);
|
||||
System.arraycopy(ctxBytes, 0, data, strBytes.length, ctxBytes.length);
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
public Status status;
|
||||
public String translatedText;
|
||||
}
|
||||
} else {
|
||||
try {
|
||||
data = ctx.getBytes("UTF-8");
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public static WeblateTranslationUnit getTranslationUnit(String text) {
|
||||
WeblateTranslationUnit unit = new WeblateTranslationUnit();
|
||||
if (!Workspace.activeWorkspace.settings.useInternet.getCurrentValue()) {
|
||||
unit.status = Status.notAllowed;
|
||||
unit.translatedText = "Allow internet connection in the workspace settings to get translation status";
|
||||
} else if (Workspace.activeWorkspace.settings.translatorLanguage == null) {
|
||||
unit.status = Status.notAllowed;
|
||||
unit.translatedText = "Select a target language in the workspace settings to get translation status";
|
||||
} else {
|
||||
unit.status = Status.absent;
|
||||
unit.translatedText = "Cannot find this on weblate";
|
||||
String hash = weblateHash(text, "");
|
||||
try {
|
||||
Document wlDoc = Jsoup.connect(getWeblateLabelURI(text)).get();
|
||||
Element textArea = wlDoc.getElementById("id_"+hash+"_0");
|
||||
if (textArea != null) {
|
||||
String trans = textArea.text();
|
||||
if (trans != null) {
|
||||
unit.translatedText = trans.trim();
|
||||
if (unit.translatedText.isEmpty()) {
|
||||
unit.translatedText = "Not yet translated";
|
||||
unit.status = Status.notTranslated;
|
||||
} else {
|
||||
unit.status = Status.done;
|
||||
}
|
||||
}
|
||||
Element fuzzyBox = wlDoc.getElementById("id_"+hash+"_fuzzy");
|
||||
if (fuzzyBox != null && fuzzyBox.hasAttr("checked")) {
|
||||
if ("checked".equals(fuzzyBox.attr("checked"))) {
|
||||
unit.status = Status.fuzzy;
|
||||
}
|
||||
} else {
|
||||
Elements dangerZone = wlDoc.getElementsByAttributeValue("class", "panel panel-danger");
|
||||
if (dangerZone != null && !dangerZone.isEmpty()) {
|
||||
unit.status = Status.warning;
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (IOException e) {
|
||||
unit.status = Status.error;
|
||||
unit.translatedText = "Cannot connect to weblate: "+e.getMessage();
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
return unit;
|
||||
}
|
||||
return HashUtils.siphash(WEBLATE_SIPASH_KEY, data);
|
||||
}
|
||||
|
||||
public static String getWeblateLabelURI(String text) {
|
||||
return "https://hosted.weblate.org/translate/andors-trail/game-content/" + Workspace.activeWorkspace.settings.translatorLanguage.getCurrentValue() + "/?checksum=" + weblateHash(text, "");
|
||||
}
|
||||
|
||||
public static class WeblateTranslationUnit {
|
||||
public enum Status {
|
||||
notAllowed, error, absent, notTranslated, warning, fuzzy, done
|
||||
}
|
||||
|
||||
public Status status;
|
||||
public String translatedText;
|
||||
}
|
||||
|
||||
public static WeblateTranslationUnit getTranslationUnit(String text) {
|
||||
WeblateTranslationUnit unit = new WeblateTranslationUnit();
|
||||
if (!Workspace.activeWorkspace.settings.useInternet.getCurrentValue()) {
|
||||
unit.status = Status.notAllowed;
|
||||
unit.translatedText = "Allow internet connection in the workspace settings to get translation status";
|
||||
} else if (Workspace.activeWorkspace.settings.translatorLanguage == null) {
|
||||
unit.status = Status.notAllowed;
|
||||
unit.translatedText = "Select a target language in the workspace settings to get translation status";
|
||||
} else {
|
||||
unit.status = Status.absent;
|
||||
unit.translatedText = "Cannot find this on weblate";
|
||||
String hash = weblateHash(text, "");
|
||||
try {
|
||||
Document wlDoc = Jsoup.connect(getWeblateLabelURI(text)).get();
|
||||
Element textArea = wlDoc.getElementById("id_" + hash + "_0");
|
||||
if (textArea != null) {
|
||||
String trans = textArea.text();
|
||||
if (trans != null) {
|
||||
unit.translatedText = trans.trim();
|
||||
if (unit.translatedText.isEmpty()) {
|
||||
unit.translatedText = "Not yet translated";
|
||||
unit.status = Status.notTranslated;
|
||||
} else {
|
||||
unit.status = Status.done;
|
||||
}
|
||||
}
|
||||
Element fuzzyBox = wlDoc.getElementById("id_" + hash + "_fuzzy");
|
||||
if (fuzzyBox != null && fuzzyBox.hasAttr("checked")) {
|
||||
if ("checked".equals(fuzzyBox.attr("checked"))) {
|
||||
unit.status = Status.fuzzy;
|
||||
}
|
||||
} else {
|
||||
Elements dangerZone = wlDoc.getElementsByAttributeValue("class", "panel panel-danger");
|
||||
if (dangerZone != null && !dangerZone.isEmpty()) {
|
||||
unit.status = Status.warning;
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (IOException e) {
|
||||
unit.status = Status.error;
|
||||
unit.translatedText = "Cannot connect to weblate: " + e.getMessage();
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
return unit;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user