Search in sources :

Example 1 with VersionInfo

use of com.github.mob41.osumer.updater.VersionInfo in project osumer by mob41.

the class Installer method getInstalledVersion.

public static VersionInfo getInstalledVersion() {
    if (!Osumer.isWindows()) {
        return null;
    }
    if (!isInstalled()) {
        return null;
    }
    File file = new File(winPath + "\\" + verInfoFile);
    try {
        FileInputStream in = new FileInputStream(file);
        ByteArrayOutputStream buffer = new ByteArrayOutputStream();
        int read;
        byte[] data = new byte[1024];
        while ((read = in.read(data, 0, data.length)) != -1) {
            buffer.write(data, 0, read);
        }
        buffer.flush();
        in.close();
        String str = new String(Base64.decodeBase64(data), StandardCharsets.UTF_8);
        JSONObject json = null;
        try {
            json = new JSONObject(str);
        } catch (JSONException e) {
            return null;
        }
        String ver = json.isNull("version") ? null : json.getString("version");
        String branch = json.isNull("branch") ? null : json.getString("branch");
        int build = json.isNull("build") ? -1 : json.getInt("build");
        if (ver == null || branch == null || build == -1) {
            return null;
        }
        return new VersionInfo(ver, branch, build);
    } catch (IOException e) {
        return null;
    }
}
Also used : VersionInfo(com.github.mob41.osumer.updater.VersionInfo) JSONObject(org.json.JSONObject) JSONException(org.json.JSONException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 2 with VersionInfo

use of com.github.mob41.osumer.updater.VersionInfo in project osumer by mob41.

the class Updater method getPerVersionPerBranchLatestVersion.

public UpdateInfo getPerVersionPerBranchLatestVersion() throws WithDumpException {
    final String thisVersion = Osumer.OSUMER_VERSION;
    final String buildBranch = Osumer.OSUMER_BRANCH;
    final int buildNum = Osumer.OSUMER_BUILD_NUM;
    final int updateSource = config.getUpdateSource();
    JSONObject json = getVersions();
    if (json.isNull("sources")) {
        throw new WithDumpException(VERSION_LIST, "Create JSONObject", "JSONObject validating \"sources\" parameter", "Convert source integer to string", "Structure invalid, missing \"sources\" parameter", false);
    }
    // sources -> snapshot (updateSource) -> 1.0.0 (version) -> (array:
    // index0) 1 (build-number)
    JSONObject sourcesJson = json.getJSONObject("sources");
    JSONArray buildsArr;
    String sourceKey = null;
    switch(updateSource) {
        case UPDATE_SOURCE_SNAPSHOT:
            sourceKey = SOURCE_SNAPSHOT;
            break;
        case UPDATE_SOURCE_BETA:
            sourceKey = SOURCE_BETA;
            break;
        case UPDATE_SOURCE_STABLE:
            sourceKey = SOURCE_STABLE;
            break;
        default:
            throw new InvalidSourceIntegerException(json.toString(5), "JSONObject validating \"sources\" parameter", "Convert source integer to string", "Validate sources' JSONObject and set variable", updateSource);
    }
    if (sourcesJson.isNull(sourceKey)) {
        throw new NoSuchSourceException(json.toString(5), "Convert source integer to string", "Validate sources' JSONObject and set variable", "Validate versions' JSONObject and set variable", sourceKey);
    }
    JSONObject versionsJson = sourcesJson.getJSONObject(sourceKey);
    if (versionsJson.isNull(thisVersion)) {
        throw new NoSuchVersionException(json.toString(5), "Validate sources' JSONObject and set variable", "Validate versions' JSONObject and set variable", "Validate builds JSONArray and set variable", sourceKey, thisVersion);
    }
    buildsArr = versionsJson.getJSONArray(thisVersion);
    if (buildsArr.length() < buildNum) {
        throw new NoSuchBuildNumberException(json.toString(5), "Validate versions' JSONObject and set variable", "Validate builds JSONArray and set variable", "Get latest build number from JSONArray", buildNum);
    }
    int latest = buildsArr.length();
    JSONObject verJson = buildsArr.getJSONObject(latest - 1);
    if ((verJson.isNull("ended") || !verJson.getBoolean("ended")) && (!buildBranch.equals(sourceKey) || latest != buildNum)) {
        String webLink = verJson.isNull("web_link") ? null : verJson.getString("web_link");
        String exeLink = verJson.isNull("exe_link") ? null : verJson.getString("exe_link");
        String jarLink = verJson.isNull("jar_link") ? null : verJson.getString("jar_link");
        String desc = verJson.isNull("desc") ? null : verJson.getString("desc");
        return new UpdateInfo(desc, thisVersion, updateSource, latest, webLink, exeLink, jarLink, false, false);
    }
    // As the version is ended, we are finding a new version here
    Iterator<String> it = versionsJson.keys();
    String key;
    String upgradeNode = null;
    while (it.hasNext()) {
        key = it.next();
        switch(compareVersion(thisVersion, key)) {
            case -2:
                break;
            case -1:
                if ((upgradeNode != null && compareVersion(upgradeNode, key) == -1) || upgradeNode == null) {
                    upgradeNode = key;
                }
                break;
            case 0:
                break;
            case 1:
                break;
        }
    }
    JSONArray upgradeNodeArr = null;
    int upgradedBuildNum = -1;
    if (upgradeNode != null) {
        upgradeNodeArr = versionsJson.getJSONArray(upgradeNode);
        upgradedBuildNum = upgradeNodeArr.length();
    }
    if (upgradedBuildNum == 0) {
        throw new NoBuildsForVersionException(json.toString(5), "Get Upgrade Node", "Validate build info JSON", "Return VersionInfo");
    }
    if (upgradeNode != null && upgradedBuildNum != -1) {
        JSONObject upgradedVerJson = upgradeNodeArr.getJSONObject(upgradedBuildNum - 1);
        String webLink = upgradedVerJson.isNull("web_link") ? null : upgradedVerJson.getString("web_link");
        String exeLink = upgradedVerJson.isNull("exe_link") ? null : upgradedVerJson.getString("exe_link");
        String jarLink = upgradedVerJson.isNull("jar_link") ? null : upgradedVerJson.getString("jar_link");
        String desc = upgradedVerJson.isNull("desc") ? null : upgradedVerJson.getString("desc");
        return new UpdateInfo(desc, upgradeNode, updateSource, upgradedBuildNum, webLink, exeLink, jarLink, false, true);
    } else {
        String webLink = verJson.isNull("web_link") ? null : verJson.getString("web_link");
        String exeLink = verJson.isNull("exe_link") ? null : verJson.getString("exe_link");
        String jarLink = verJson.isNull("jar_link") ? null : verJson.getString("jar_link");
        String desc = verJson.isNull("desc") ? null : verJson.getString("desc");
        return new UpdateInfo(desc, thisVersion, updateSource, buildNum, webLink, exeLink, jarLink, true, false);
    }
}
Also used : InvalidSourceIntegerException(com.github.mob41.osumer.exceptions.InvalidSourceIntegerException) NoSuchVersionException(com.github.mob41.osumer.exceptions.NoSuchVersionException) WithDumpException(com.github.mob41.osumer.debug.WithDumpException) JSONObject(org.json.JSONObject) NoSuchBuildNumberException(com.github.mob41.osumer.exceptions.NoSuchBuildNumberException) NoSuchSourceException(com.github.mob41.osumer.exceptions.NoSuchSourceException) JSONArray(org.json.JSONArray) NoBuildsForVersionException(com.github.mob41.osumer.exceptions.NoBuildsForVersionException)

Example 3 with VersionInfo

use of com.github.mob41.osumer.updater.VersionInfo in project osumer by mob41.

the class Updater method getLatestVersion.

public UpdateInfo getLatestVersion() throws WithDumpException {
    final int updateSource = config.getUpdateSource();
    JSONObject json = getVersions();
    if (json.isNull("sources")) {
        throw new WithDumpException(VERSION_LIST, "Create JSONObject", "JSONObject validating \"sources\" parameter", "Convert source integer to string", "Structure invalid, missing \"sources\" parameter", false);
    }
    // sources -> snapshot (updateSource) -> 1.0.0 (version) -> (array: index0) 1 (build-number)
    JSONObject sourcesJson = json.getJSONObject("sources");
    JSONArray buildsArr;
    String sourceKey = null;
    switch(updateSource) {
        case UPDATE_SOURCE_SNAPSHOT:
            sourceKey = SOURCE_SNAPSHOT;
            break;
        case UPDATE_SOURCE_BETA:
            sourceKey = SOURCE_BETA;
            break;
        case UPDATE_SOURCE_STABLE:
            sourceKey = SOURCE_STABLE;
            break;
        default:
            throw new InvalidSourceIntegerException(json.toString(5), "JSONObject validating \"sources\" parameter", "Convert source integer to string", "Validate sources' JSONObject and set variable", updateSource);
    }
    if (sourcesJson.isNull(sourceKey)) {
        throw new NoSuchSourceException(json.toString(5), "Convert source integer to string", "Validate sources' JSONObject and set variable", "Validate versions' JSONObject and set variable", sourceKey);
    }
    JSONObject versionsJson = sourcesJson.getJSONObject(sourceKey);
    VersionInfo thisInfo = new VersionInfo(Osumer.OSUMER_VERSION, Osumer.OSUMER_BRANCH, Osumer.OSUMER_BUILD_NUM);
    Iterator<String> it = versionsJson.keys();
    String last = null;
    String key;
    while (it.hasNext()) {
        key = it.next();
        if (last != null && compareVersion(key, last) != 1) {
            continue;
        } else {
            last = key;
        }
    }
    if (last == null) {
        return null;
    }
    buildsArr = versionsJson.getJSONArray(last);
    int latest = buildsArr.length();
    JSONObject verJson = buildsArr.getJSONObject(latest - 1);
    String webLink = verJson.isNull("web_link") ? null : verJson.getString("web_link");
    String exeLink = verJson.isNull("exe_link") ? null : verJson.getString("exe_link");
    String jarLink = verJson.isNull("jar_link") ? null : verJson.getString("jar_link");
    String desc = verJson.isNull("desc") ? null : verJson.getString("desc");
    boolean isThisVersion = thisInfo != null && last.equals(thisInfo.getVersion()) && latest == thisInfo.getBuildNum() && getBranchStr(updateSource).equals(thisInfo.getBranch());
    if (thisInfo != null) {
        System.out.println(last + " to " + thisInfo.getVersion());
        System.out.println(latest + " to " + thisInfo.getBuildNum());
        System.out.println(getBranchStr(updateSource) + " to " + thisInfo.getBranch());
    } else {
        System.out.println("ThisInfoNull");
    }
    System.out.println("IsThisVer: " + isThisVersion);
    return new UpdateInfo(desc, last, updateSource, latest, webLink, exeLink, jarLink, isThisVersion, !isThisVersion);
}
Also used : InvalidSourceIntegerException(com.github.mob41.osumer.exceptions.InvalidSourceIntegerException) VersionInfo(com.github.mob41.osumer.updater.VersionInfo) WithDumpException(com.github.mob41.osumer.debug.WithDumpException) JSONObject(org.json.JSONObject) NoSuchSourceException(com.github.mob41.osumer.exceptions.NoSuchSourceException) JSONArray(org.json.JSONArray)

Aggregations

JSONObject (org.json.JSONObject)3 WithDumpException (com.github.mob41.osumer.debug.WithDumpException)2 InvalidSourceIntegerException (com.github.mob41.osumer.exceptions.InvalidSourceIntegerException)2 NoSuchSourceException (com.github.mob41.osumer.exceptions.NoSuchSourceException)2 VersionInfo (com.github.mob41.osumer.updater.VersionInfo)2 JSONArray (org.json.JSONArray)2 NoBuildsForVersionException (com.github.mob41.osumer.exceptions.NoBuildsForVersionException)1 NoSuchBuildNumberException (com.github.mob41.osumer.exceptions.NoSuchBuildNumberException)1 NoSuchVersionException (com.github.mob41.osumer.exceptions.NoSuchVersionException)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 JSONException (org.json.JSONException)1