Forums

OverviewFelgo 2 Support (Qt 5) › Externalize license key

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #17948

    Mike

    So, obviously I don’t want my keys to be in source control…

    I’m thinking to set the key as an environment variable for the build process, allowing me to rotate keys and rebuild easily.  If I do that…

    1. Is a context property the best way to consume it in my main.qml file?
    2. Do I even need to have licenseKey in my QML or can I set it in my main.cpp when initializing the engine?

    #2 would be my preference.  Any examples or pointers to relevant article appreciated.  Thanks!

     

    #17951

    Marcin

    Yeah, was thinking about something similar, but in this scenario(small Indie game/app and the plugins we use), I don’t see many cases where I would have separate set of credentials per environment.
    Often you see dev mode flags in plugins rather than sandbox set of credentials.
    In something like web development, where your code lands with developers who shouldn’t have production access to your db etc, it definitely makes a lot of sense.
    Here, I am not sure.
    In the end your code lands on somebody else’s machine which you don’t control.
    Probably in small, Indie team is not that big deal.
    But if there are any plans to allow separating keys from your source code, and it would be supported in some way by Felgo, I would also be interested.

    #17953

    Mike

    Agree, Marcin.

    It’s not elegant, but here is what I decided to do for now.

    I have a little script that spits out a very simple header

    #!/bin/bash
    
    if [ -z $LICENSE_KEY ]; then
    cat <<EOM
    No license key exported, try something like...
    
    LICENSE_KEY='PUT_KEY_HERE' $0
    
    Get your key from https://felgo.com/developers/license/
    EOM
    exit 1
    fi
    
    cat <<EOF >> some/dir/license.h
    #ifndef LICENSE_H
    #define LICENSE_H
    
    #define LICENSE_KEY "${LICENSE_KEY}"
    
    #endif // LICENSE_H
    EOF
    

    That will create license.h that looks like this

    #ifndef LICENSE_H
    #define LICENSE_H
    
    #define LICENSE_KEY "15E83736..."
    
    #endif // LICENSE_H

    Then include and use to set a context property

    // add these includes
    #include <QQmlContext>
    #include "license.h"
    
    int main(int argc, char *argv[])
    {
        QApplication app(argc, argv);
        VPApplication vplay;
        vplay.setPreservePlatformFonts(true);
        QQmlApplicationEngine engine;
        vplay.initialize(&engine);
        vplay.setMainQmlFileName(QStringLiteral("qml/Main.qml"));
        vplay.setMainQmlFileName(QStringLiteral("qrc:/qml/Main.qml"));
    
        // add key as property
        engine.rootContext()->setContextProperty("appLicenseKey", LICENSE_KEY);
    
        engine.load(QUrl(vplay.mainQmlFileName()));
        return app.exec();
    }
    

    Your app now just uses the context property

    App {
    
        id: app
        licenseKey: appLicenseKey
    
    }

    I specifically added license.h to my .gitignore so it never accidentally gets committed to GitHub.  That’s it, ugly but simple, and my key never leaves my dev env…well except in the compiled binary, but that’s a different problem to solve 🙂

    #17957

    Günther
    Felgo Team

    Hi!

    Felgo does not specifically cover using different key settings in a project. Your solutions generally looks good Mike: Using a context property is a simple way of passing the key from your main.cpp to the QML.

    For Felgo projects, we also often combine the different key constants used in QML into a singleton QML type. For example, the Qt World Summit demo holds all settings like license key, game network configuration, etc … in qml/common/AppSettings.qml. This singleton type makes the constants available with a simple import in QML. If you want to create a singleton, please don’t miss to also add a qmldir file with the correct settings. (You can see the demo project for reference)

    When working with multiple setting-properties, you can also create and register a QML type with C++, instead of passing a context property. You can find more information here: Integrating QML and C++

    For build configuration and variants, you can also have a look at the qmake system, which allows you to configure your build in the *.pro configuration of your project.

    Best,
    Günther

Viewing 4 posts - 1 through 4 (of 4 total)

RSS feed for this thread

You must be logged in to reply to this topic.

Voted #1 for:

  • Easiest to learn
  • Most time saving
  • Best support

Develop Cross-Platform Apps and Games 50% Faster!

  • Voted the best supported, most time-saving and easiest to learn cross-platform development tool
  • Based on the Qt framework, with native performance and appearance on all platforms including iOS and Android
  • Offers a variety of plugins to monetize, analyze and engage users
FREE!
create apps
create games
cross platform
native performance
3rd party services
game network
multiplayer
level editor
easiest to learn
biggest time saving
best support