Skip to content
This repository has been archived by the owner on Nov 12, 2024. It is now read-only.

Commit

Permalink
- Fixes paths in windows
Browse files Browse the repository at this point in the history
- Allows column sorting
- Fixes windows paths
- Improved error reporting
  • Loading branch information
soywiz committed Sep 5, 2016
1 parent adeba43 commit 482ce36
Show file tree
Hide file tree
Showing 12 changed files with 933 additions and 827 deletions.
1 change: 1 addition & 0 deletions classes/production/vitaorganizer/currentVersion.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0.2
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 8 additions & 0 deletions ftp4j/src/it/sauronsoftware/ftp4j/FTPConnector.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketException;

/**
* This abstract class is the base for creating a connector. Connectors are used
Expand Down Expand Up @@ -117,6 +118,13 @@ public void setConnectionTimeout(int connectionTimeout) {
*/
public void setReadTimeout(int readTimeout) {
this.readTimeout = readTimeout;
if (connectingCommunicationChannelSocket != null) {
try {
connectingCommunicationChannelSocket.setSoTimeout(readTimeout);
} catch (SocketException e) {
e.printStackTrace();
}
}
}

/**
Expand Down
2 changes: 1 addition & 1 deletion lastVersion.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
0.2
0.3
https://github.com/soywiz/vitaorganizer
2 changes: 1 addition & 1 deletion resources/currentVersion.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.2
0.3
29 changes: 17 additions & 12 deletions src/com/soywiz/vitaorganizer/FileSize.kt
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
package com.soywiz.vitaorganizer

object FileSize {
val BYTES = 1L
val KB = 1024 * BYTES
val MB = 1024 * KB
val GB = 1024 * MB
val TB = 1024 * GB
class FileSize(val value: Long) : Comparable<FileSize> {
companion object {
private val BYTES = 1L
private val KB = 1024 * BYTES
private val MB = 1024 * KB
private val GB = 1024 * MB
private val TB = 1024 * GB

fun toString(size: Long): String {
if (size < KB) return "%.1f B".format(size.toDouble() / BYTES.toDouble())
if (size < MB) return "%.1f KB".format(size.toDouble() / KB.toDouble())
if (size < GB) return "%.1f MB".format(size.toDouble() / MB.toDouble())
if (size < TB) return "%.1f GB".format(size.toDouble() / GB.toDouble())
return "%.1f TB".format(size.toDouble() / TB.toDouble())
fun toString(size: Long): String {
if (size < KB) return "%.1f B".format(size.toDouble() / BYTES.toDouble())
if (size < MB) return "%.1f KB".format(size.toDouble() / KB.toDouble())
if (size < GB) return "%.1f MB".format(size.toDouble() / MB.toDouble())
if (size < TB) return "%.1f GB".format(size.toDouble() / GB.toDouble())
return "%.1f TB".format(size.toDouble() / TB.toDouble())
}
}

override fun compareTo(other: FileSize): Int = this.value.compareTo(other.value)
override fun toString() = FileSize.toString(value)
}
106 changes: 75 additions & 31 deletions src/com/soywiz/vitaorganizer/PsvitaDevice.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import it.sauronsoftware.ftp4j.FTPException
import it.sauronsoftware.ftp4j.FTPFile
import java.io.ByteArrayInputStream
import java.io.File
import java.io.FileNotFoundException
import java.io.IOException
import java.net.InetSocketAddress
import java.net.NetworkInterface
Expand Down Expand Up @@ -52,10 +53,26 @@ object PsvitaDevice {

val ftp = FTPClient().apply {
type = FTPClient.TYPE_BINARY
//autoNoopTimeout = 50000L
this.connector.setCloseTimeout(20)
this.connector.setReadTimeout(240) // PROM could take a lot of time!
this.connector.setConnectionTimeout(120)
}

fun setFtpPromoteTimeouts() {
//ftp.connector.setCloseTimeout(20)
//ftp.connector.setReadTimeout(240) // PROM could take a lot of time!
//ftp.connector.setConnectionTimeout(120)
}

fun resetFtpTimeouts() {
ftp.connector.setCloseTimeout(20)
ftp.connector.setReadTimeout(240) // PROM could take a lot of time!
ftp.connector.setConnectionTimeout(120)

//ftp.connector.setCloseTimeout(2)
//ftp.connector.setReadTimeout(2)
//ftp.connector.setConnectionTimeout(2)
}

init {
resetFtpTimeouts()
}

//init {
Expand Down Expand Up @@ -91,22 +108,34 @@ object PsvitaDevice {
fun getGameFolder(id: String) = "/ux0:/app/${File(id).name}"

fun downloadSmallFile(path: String): ByteArray {
try {
if (connectedFtp().fileSize(path) == 0L) {
return byteArrayOf()
}
} catch (e: Throwable) {
return byteArrayOf()
}

val file = File.createTempFile("vita", "download")
try {
connectedFtp().download(path, file)
return file.readBytes()
} catch (e: FTPException) {

e.printStackTrace()
} catch (e: Throwable) {
e.printStackTrace()
} finally {
//e.printStackTrace()
file.delete()
}
return byteArrayOf()
}

fun getParamSfo(id: String): ByteArray = downloadSmallFile("${getGameFolder(id)}/sce_sys/param.sfo")
fun getGameIcon(id: String): ByteArray = downloadSmallFile("${getGameFolder(id)}/sce_sys/icon0.png")
fun getGameIcon(id: String): ByteArray {
val result = downloadSmallFile("${getGameFolder(id)}/sce_sys/icon0.png")
return result
}
fun downloadEbootBin(id: String): ByteArray = downloadSmallFile("${getGameFolder(id)}/eboot.bin")

fun getParamSfoCached(id: String): ByteArray {
Expand All @@ -121,15 +150,15 @@ object PsvitaDevice {
return file.readBytes()
}


fun getFolderSize(path: String, folderSizeCache: HashMap<String, Long> = hashMapOf<String, Long>()): Long {
return folderSizeCache.getOrPut(path) {
var out = 0L
val ftp = connectedFtp()
try {
for (file in ftp.list(path)) {
//println("$path/${file.name}: ${file.size}")
if (file.type == FTPFile.TYPE_DIRECTORY) {
getFolderSize("$path/${file.name}", folderSizeCache)
out += getFolderSize("$path/${file.name}", folderSizeCache)
} else {
out += file.size
}
Expand Down Expand Up @@ -157,7 +186,8 @@ object PsvitaDevice {

val createDirectoryCache = hashSetOf<String>()

fun createDirectories(path: String, createDirectoryCache: HashSet<String> = PsvitaDevice.createDirectoryCache) {
fun createDirectories(_path: String, createDirectoryCache: HashSet<String> = PsvitaDevice.createDirectoryCache) {
val path = _path.replace('\\', '/')
val parent = File(path).parent
if (parent != "" && parent != null) {
createDirectories(parent, createDirectoryCache)
Expand Down Expand Up @@ -193,31 +223,37 @@ object PsvitaDevice {
status.totalSize = filteredEntries.map { it.size }.sum()

for (entry in filteredEntries) {
val vname = "$base/${entry.name}"
val directory = File(vname).parent
val normalizedName = entry.name.replace('\\', '/')
val vname = "$base/$normalizedName"
val directory = File(vname).parent.replace('\\', '/')
val startSize = status.currentSize
println("Writting $vname...")
if (!entry.isDirectory) {
createDirectories(directory)
connectedFtp().upload(vname, zip.getInputStream(entry), 0L, 0L, object : FTPDataTransferListener {
override fun started() {
}

override fun completed() {
updateStatus(status)
}

override fun aborted() {
}

override fun transferred(size: Int) {
status.currentSize += size
updateStatus(status)
}

override fun failed() {
}
})
try {
connectedFtp().upload(vname, zip.getInputStream(entry), 0L, 0L, object : FTPDataTransferListener {
override fun started() {
}

override fun completed() {
updateStatus(status)
}

override fun aborted() {
}

override fun transferred(size: Int) {
status.currentSize += size
updateStatus(status)
}

override fun failed() {
}
})
}catch (e: FTPException) {
e.printStackTrace()
throw FileNotFoundException("Can't upload file $vname")
}
}
status.currentSize = startSize + entry.size
status.currentFile++
Expand Down Expand Up @@ -257,11 +293,19 @@ object PsvitaDevice {
}

fun removeFile(path: String) {
connectedFtp().deleteFile(path)
try {
connectedFtp().deleteFile(path)
} catch (e: Throwable) {
println("Can't delete $path")
e.printStackTrace()
}
}

fun promoteVpk(vpkPath: String) {
setFtpPromoteTimeouts()
println("Promoting: 'PROM $vpkPath'")
connectedFtp().sendCustomCommand("PROM $vpkPath")
resetFtpTimeouts()
}

/*
Expand Down
5 changes: 5 additions & 0 deletions src/com/soywiz/vitaorganizer/StatusUpdater.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.soywiz.vitaorganizer

interface StatusUpdater {
fun updateStatus(status: String)
}
10 changes: 10 additions & 0 deletions src/com/soywiz/vitaorganizer/Test1.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.soywiz.vitaorganizer

import java.util.*

object Test1 {
@JvmStatic fun main(args: Array<String>) {
PsvitaDevice.setIp("192.168.137.55", 1337)
println(PsvitaDevice.getGameSize("PCSG00683"))
}
}
Loading

0 comments on commit 482ce36

Please sign in to comment.