Ejemplo n.º 1
0
JavaStaticMethod JavaClass::GetStaticMethod(TString Name, TString Signature) {
    jmethodID method = FEnv->GetStaticMethodID(FClass, PlatformString(Name), PlatformString(Signature));

    if (method == NULL || FEnv->ExceptionCheck() == JNI_TRUE) {
        Messages& messages = Messages::GetInstance();
        TString message = messages.GetMessage(METHOD_NOT_FOUND);
        message = PlatformString::Format(message, Name.data(), FClassName.data());
        throw JavaException(FEnv, message);
    }

    return JavaStaticMethod(FEnv, FClass, method);
}
std::list<TString> GenericPlatform::LoadFromFile(TString FileName) {
    std::list<TString> result;

    if (FilePath::FileExists(FileName) == true) {
        std::wifstream stream(FileName.data());

#ifdef WINDOWS
        const std::locale empty_locale = std::locale::empty();
#endif //WINDOWS
#ifdef POSIX
        const std::locale empty_locale = std::locale::classic();
#endif //POSIX
#if defined(WINDOWS)
        const std::locale utf8_locale = std::locale(empty_locale, new std::codecvt_utf8<wchar_t>());
        stream.imbue(utf8_locale);
#endif //WINDOWS

        if (stream.is_open() == true) {
            while (stream.eof() == false) {
                std::wstring line;
                std::getline(stream, line);

                // # at the first character will comment out the line.
                if (line.empty() == false && line[0] != '#') {
                    result.push_back(PlatformString(line).toString());
                }
            }
        }
    }

    return result;
}
void GenericPlatform::SaveToFile(TString FileName, std::list<TString> Contents, bool ownerOnly) {
    TString path = FilePath::ExtractFilePath(FileName);

    if (FilePath::DirectoryExists(path) == false) {
        FilePath::CreateDirectory(path, ownerOnly);
    }

    std::wofstream stream(FileName.data());

    FilePath::ChangePermissions(FileName.data(), ownerOnly);

#ifdef WINDOWS
    const std::locale empty_locale = std::locale::empty();
#endif //WINDOWS
#ifdef POSIX
    const std::locale empty_locale = std::locale::classic();
#endif //POSIX
#if defined(WINDOWS)
    const std::locale utf8_locale = std::locale(empty_locale, new std::codecvt_utf8<wchar_t>());
    stream.imbue(utf8_locale);
#endif //WINDOWS || MAC

    if (stream.is_open() == true) {
        for (std::list<TString>::const_iterator iterator = Contents.begin(); iterator != Contents.end(); iterator++) {
            TString line = *iterator;
            stream << PlatformString(line).toUnicodeString() << std::endl;
        }
    }
}
Ejemplo n.º 4
0
TString JavaException::CreateExceptionMessage(JNIEnv* Env, jthrowable Exception,
    jmethodID GetCauseMethod, jmethodID GetStackTraceMethod, jmethodID ThrowableToTStringMethod,
    jmethodID FrameToTStringMethod) {

    TString result;
    jobjectArray frames = (jobjectArray)Env->CallObjectMethod(Exception, GetStackTraceMethod);

    // Append Throwable.toTString().
    if (0 != frames) {
        jstring jstr = (jstring)Env->CallObjectMethod(Exception, ThrowableToTStringMethod);
        const char* str = Env->GetStringUTFChars(jstr, 0);
        result += PlatformString(str).toPlatformString();
        Env->ReleaseStringUTFChars(jstr, str);
        Env->DeleteLocalRef(jstr);
    }

    // Append stack trace if one exists.
    if (Env->GetArrayLength(frames) > 0) {
        jsize i = 0;

        for (i = 0; i < Env->GetArrayLength(frames); i++) {
            // Get the string from the next frame and append it to
            // the error message.
            jobject frame = Env->GetObjectArrayElement(frames, i);
            jstring obj = (jstring)Env->CallObjectMethod(frame, FrameToTStringMethod);
            const char* str = Env->GetStringUTFChars(obj, 0);
            result += _T("\n  ");
            result += PlatformString(str).toPlatformString();
            Env->ReleaseStringUTFChars(obj, str);
            Env->DeleteLocalRef(obj);
            Env->DeleteLocalRef(frame);
        }
    }

    // If Exception has a cause then append the stack trace messages.
    if (0 != frames) {
        jthrowable cause = (jthrowable)Env->CallObjectMethod(Exception, GetCauseMethod);

        if (cause != NULL) {
            result += CreateExceptionMessage(Env, cause, GetCauseMethod,
                GetStackTraceMethod, ThrowableToTStringMethod,
                FrameToTStringMethod);
        }
    }

    return result;
}
Ejemplo n.º 5
0
TPlatformNumber StringToPercentageOfNumber(TString Value, TPlatformNumber Number) {
    TPlatformNumber result = 0;
    size_t percentage = atoi(PlatformString(Value.c_str()));

    if (percentage > 0 && Number > 0) {
        result = Number * percentage / 100;
    }

    return result;
}
Ejemplo n.º 6
0
JavaStringArray::JavaStringArray(JNIEnv *Env, std::list<TString> Items) {
    FEnv = Env;
    Initialize(Items.size());
    unsigned int index = 0;

    for (std::list<TString>::const_iterator iterator = Items.begin(); iterator != Items.end(); iterator++) {
        TString item = *iterator;
        SetValue(index, PlatformString(item).toJString(FEnv));
        index++;
    }
}
FileSystemStringToString::FileSystemStringToString(const TCHAR* value) {
    bool release = false;
    PlatformString lvalue = PlatformString(value);
    Platform& platform = Platform::GetInstance();
    TCHAR* buffer = platform.ConvertFileSystemStringToString(lvalue, release);
    FData = buffer;
    
    if (buffer != NULL && release == true) {
        delete[] buffer;
    }
}
Ejemplo n.º 8
0
JavaClass::JavaClass(JNIEnv *Env, TString Name) {
    FEnv = Env;
    FClassName = Name;
    FClass = FEnv->FindClass(PlatformString(FClassName));

    if (FClass == NULL || FEnv->ExceptionCheck() == JNI_TRUE) {
        Messages& messages = Messages::GetInstance();
        TString message = messages.GetMessage(CLASS_NOT_FOUND);
        message = PlatformString::Format(message, FClassName.data());
        throw JavaException(FEnv, message);
    }
}
    JavaVMOption* ToJavaOptions() {
        FOptions = new JavaVMOption[FItems.size()];
        memset(FOptions, 0, sizeof(JavaVMOption) * FItems.size());
        Macros& macros = Macros::GetInstance();
        unsigned int index = 0;

        for (std::list<JavaOptionItem>::const_iterator iterator = FItems.begin();
                iterator != FItems.end(); iterator++) {
            TString key = iterator->name;
            TString value = iterator->value;
            TString option = Helpers::NameValueToString(key, value);
            option = macros.ExpandMacros(option);
#ifdef DEBUG
            printf("%s\n", PlatformString(option).c_str());
#endif //DEBUG
            FOptions[index].optionString = PlatformString::duplicate(PlatformString(option).c_str());
            FOptions[index].extraInfo = iterator->extraInfo;
            index++;
        }

        return FOptions;
    }
Ejemplo n.º 10
0
void JavaStringArray::Initialize(size_t Size) {
    JavaClass jstringClass(FEnv, _T("java/lang/String"));

    if (FEnv->ExceptionCheck() == JNI_TRUE) {
        Messages& messages = Messages::GetInstance();
        TString message = messages.GetMessage(CLASS_NOT_FOUND);
        message = PlatformString::Format(message, _T("String"));
        throw JavaException(FEnv, message.data());
    }

    jstring str = PlatformString("").toJString(FEnv);
    FData = (jobjectArray)FEnv->NewObjectArray((jsize)Size, jstringClass, str);

    if (FEnv->ExceptionCheck() == JNI_TRUE) {
        throw JavaException(FEnv, _T("Error"));
    }
}
Ejemplo n.º 11
0
    bool start_launcher(int argc, TCHAR* argv[]) {
        bool result = false;
        bool parentProcess = true;

        // Platform must be initialize first.
        Platform& platform = Platform::GetInstance();

        try {
            for (int index = 0; index < argc; index++) {
                TString argument = argv[index];

                if (argument == _T("-Xappcds:generatecache")) {
                    platform.SetAppCDSState(cdsGenCache);
                }
                else if (argument == _T("-Xappcds:off")) {
                    platform.SetAppCDSState(cdsDisabled);
                }
                else if (argument == _T("-Xapp:child")) {
                    parentProcess = false;
                }
#ifdef DEBUG
//TODO There appears to be a compiler bug on Mac overloading ShowResponseMessage. Investigate.
                else if (argument == _T("-nativedebug")) {
                    if (platform.ShowResponseMessage(_T("Test"),
                                                     TString(_T("Would you like to debug?\n\nProcessID: ")) +
                                                     PlatformString(platform.GetProcessID()).toString()) == mrOK) {
                        while (platform.IsNativeDebuggerPresent() == false) {
                        }
                    }
                }
#endif //DEBUG
            }

            // Package must be initialized after Platform is fully initialized.
            Package& package = Package::GetInstance();
            Macros::Initialize();
            package.SetCommandLineArguments(argc, argv);
            platform.SetCurrentDirectory(package.GetPackageAppDirectory());

            switch (platform.GetAppCDSState()) {
                case cdsDisabled:
                case cdsUninitialized:
                case cdsEnabled: {
                    break;
                }

                case cdsGenCache: {
                        TString cacheDirectory = package.GetAppCDSCacheDirectory();

                        if (FilePath::DirectoryExists(cacheDirectory) == false) {
                            FilePath::CreateDirectory(cacheDirectory, true);
                        }
                        else {
                            TString cacheFileName = package.GetAppCDSCacheFileName();

                            if (FilePath::FileExists(cacheFileName) == true) {
                                FilePath::DeleteFile(cacheFileName);
                            }
                        }

                        break;
                    }

                case cdsAuto: {
                    TString cacheFileName = package.GetAppCDSCacheFileName();

                    if (parentProcess == true && FilePath::FileExists(cacheFileName) == false) {
                        AutoFreePtr<Process> process = platform.CreateProcess();
                        std::vector<TString> args;
                        args.push_back(_T("-Xappcds:generatecache"));
                        args.push_back(_T("-Xapp:child"));
                        process->Execute(platform.GetModuleFileName(), args, true);

                        if (FilePath::FileExists(cacheFileName) == false) {
                            // Cache does not exist after trying to generate it,
                            // so run without cache.
                            platform.SetAppCDSState(cdsDisabled);
                            package.Clear();
                            package.Initialize();
                        }
                    }

                    break;
                }
            }

            // Validation
            {
                switch (platform.GetAppCDSState()) {
                    case cdsDisabled:
                    case cdsGenCache: {
                        // Do nothing.
                        break;
                    }

                    case cdsEnabled:
                    case cdsAuto: {
                            TString cacheFileName = package.GetAppCDSCacheFileName();

                            if (FilePath::FileExists(cacheFileName) == false) {
                                Messages& messages = Messages::GetInstance();
                                TString message = PlatformString::Format(messages.GetMessage(APPCDS_CACHE_FILE_NOT_FOUND), cacheFileName.data());
                                throw FileNotFoundException(message);
                            }
                            break;
                        }

                    case cdsUninitialized: {
                        throw Exception(_T("Internal Error"));
                }
            }
            }

            // Run App
            result = RunVM();
        }
        catch (FileNotFoundException &e) {
            platform.ShowMessage(e.GetMessage());
        }

        return result;
    }
bool JavaVirtualMachine::StartJVM() {
    Platform& platform = Platform::GetInstance();
    Package& package = Package::GetInstance();

    TString classpath = package.GetClassPath();

    JavaOptions options;
    options.AppendValue(_T("-Djava.class.path"), classpath);
    options.AppendValue(_T("-Djava.library.path"), package.GetPackageAppDirectory() + FilePath::PathSeparator() + package.GetPackageLauncherDirectory());
    options.AppendValue(_T("-Djava.launcher.path"), package.GetPackageLauncherDirectory());
    options.AppendValue(_T("-Dapp.preferences.id"), package.GetAppID());
    options.AppendValues(package.GetJVMArgs());
    options.AppendValues(RemoveTrailingEquals(package.GetJVMUserArgs()));

    TString maxHeapSizeOption;
    TString minHeapSizeOption;

    if (package.GetMemoryState() == PackageBootFields::msAuto) {
        TPlatformNumber memorySize = package.GetMemorySize();
        TString memory = PlatformString((size_t)memorySize).toString() + _T("m");
        maxHeapSizeOption = TString(_T("-Xmx")) + memory;
        options.AppendValue(maxHeapSizeOption, _T(""));

        if (memorySize > 256)
            minHeapSizeOption = _T("-Xms256m");
        else
            minHeapSizeOption = _T("-Xms") + memory;

        options.AppendValue(minHeapSizeOption, _T(""));
    }

    TString mainClassName = package.GetMainClassName();

    if (mainClassName.empty() == true) {
        Messages& messages = Messages::GetInstance();
        platform.ShowError(messages.GetMessage(NO_MAIN_CLASS_SPECIFIED));
        return false;
    }

    JavaLibrary javaLibrary(package.GetJVMLibraryFileName());

#ifndef USE_JLI_LAUNCH
    if (package.HasSplashScreen() == true) {
        options.AppendValue(TString(_T("-splash:")) + package.GetSplashScreenFileName(), _T(""));
    }

    // Set up the VM init args
    JavaVMInitArgs jvmArgs;
    memset(&jvmArgs, 0, sizeof(JavaVMInitArgs));
    jvmArgs.version = JNI_VERSION_1_6;
    jvmArgs.options = options.ToJavaOptions();
    jvmArgs.nOptions = (jint)options.GetCount();
    jvmArgs.ignoreUnrecognized = JNI_TRUE;

    if (javaLibrary.JavaVMCreate(&FJvm, &FEnv, &jvmArgs) == true) {
        try {
            JavaClass mainClass(FEnv, Helpers::ConvertIdToJavaPath(mainClassName));
            JavaStaticMethod mainMethod = mainClass.GetStaticMethod(_T("main"), _T("([Ljava/lang/String;)V"));
            std::list<TString> appargs = package.GetArgs();
            JavaStringArray largs(FEnv, appargs);

            package.FreeBootFields();

            mainMethod.CallVoidMethod(1, largs.GetData());
            return true;
        }
        catch (JavaException& exception) {
            platform.ShowError(PlatformString(exception.what()).toString());
            return false;
        }
    }

    return false;
}
Ejemplo n.º 13
0
Archivo: main.cpp Proyecto: CCJY/coliru
 PlatformString operator + (const PlatformString &s) {
     return PlatformString(this->value + s.value);
 };
Ejemplo n.º 14
0
Archivo: main.cpp Proyecto: CCJY/coliru
 PlatformString operator + (char const* s) {
     return PlatformString(this->value + String(s));
 };
Ejemplo n.º 15
0
Archivo: main.cpp Proyecto: CCJY/coliru
PlatformString DoThing(int someNum) {
    PlatformString result = PlatformString("asdf ") + PlatformString(someNum) + " " + PlatformString(123.4567f) + " " + PlatformString(123.4567) + " " + PlatformString(true);
    return result;
}
StringToFileSystemString::StringToFileSystemString(const TString &value) {
    FRelease = false;
    PlatformString lvalue = PlatformString(value);
    Platform& platform = Platform::GetInstance();
    FData = platform.ConvertStringToFileSystemString(lvalue, FRelease);
}