void LicenseSystem::Initialize()
{

    FileSystem* filesystem = GetSubsystem<FileSystem>();
    String eulaConfirmedFilePath = filesystem->GetAppPreferencesDir("AtomicEditor", "License");
    eulaConfirmedFilePath = AddTrailingSlash(eulaConfirmedFilePath);
    eulaConfirmedFilePath += "EulaConfirmed";

    eulaAgreementConfirmed_ = filesystem->FileExists(eulaConfirmedFilePath);

    if (!LoadLicense() || !key_.Length() || !eulaAgreementConfirmed_)
    {
        ResetLicense();
        UIModalOps* ops = GetSubsystem<UIModalOps>();

        if (eulaAgreementConfirmed_)
            ops->ShowActivation();
        else
            ops->ShowEulaAgreement();
    }
    else
    {
        RequestServerVerification(key_);
    }
}
void LicenseSystem::Initialize()
{

    FileSystem* filesystem = GetSubsystem<FileSystem>();

    eulaAgreementConfirmed_ = filesystem->FileExists(eulaAgreementPath_);

    if (!eulaAgreementConfirmed_)
    {
        SendEvent(E_LICENSE_EULAREQUIRED);
        return;
    }
    else
    {
        SendEvent(E_LICENSE_EULAACCEPTED);
    }

    // TODO: Cleanup for MIT

    if (!LoadLicense() || !key_.Length())
    {
        ResetLicense();

        SendEvent(E_LICENSE_ACTIVATIONREQUIRED);
        return;
    }
    else
    {
        // RequestServerVerification(key_);
    }
}
LicenseSystem::LicenseSystem(Context* context) :
    Object(context)
    , eulaAgreementConfirmed_(false)

{
    FileSystem* filesystem = GetSubsystem<FileSystem>();

    licenseFilePath_ = filesystem->GetAppPreferencesDir("AtomicEditor", "License");
    licenseFilePath_ = AddTrailingSlash(licenseFilePath_);

    if (!filesystem->DirExists(licenseFilePath_))
    {
        Poco::File dirs(licenseFilePath_.CString());
        dirs.createDirectories();
    }

    licenseCachePath_ = licenseFilePath_;

    licenseCachePath_ += "AtomicLicenseCache";

    licenseFilePath_ += "AtomicLicense";

    eulaAgreementPath_ = filesystem->GetAppPreferencesDir("AtomicEditor", "License");
    eulaAgreementPath_ = AddTrailingSlash(eulaAgreementPath_);
    eulaAgreementPath_ += "EulaConfirmed";

    ResetLicense();
}
bool LicenseSystem::LoadLicense()
{

    ResetLicense();

    FileSystem* filesystem = GetSubsystem<FileSystem>();
    String licenseFilePath = filesystem->GetAppPreferencesDir("AtomicEditor", "License");
    licenseFilePath = AddTrailingSlash(licenseFilePath);
    licenseFilePath += "AtomicLicense";

    if (!filesystem->FileExists(licenseFilePath))
        return false;

    SharedPtr<File> file(new File(context_, licenseFilePath, FILE_READ));

    file->ReadInt(); // version

    String key = file->ReadString();
    if (!ValidateKey(key))
        return false;

    key_ = key;

    licenseWindows_ = file->ReadBool();
    licenseMac_ = file->ReadBool();
    licenseAndroid_ = file->ReadBool();
    licenseIOS_ = file->ReadBool();
    licenseHTML5_ = file->ReadBool();
    licenseModule3D_ = file->ReadBool();

    return true;
}
LicenseSystem::LicenseSystem(Context* context) :
    Object(context)
    , eulaAgreementConfirmed_(false)

{
    ResetLicense();

    SubscribeToEvent(E_EDITORSHUTDOWN, HANDLER(LicenseSystem, HandleEditorShutdown));
}
void LicenseSystem::HandleDeactivate(StringHash eventType, VariantMap& eventData)
{
    Editor* editor = GetSubsystem<Editor>();

    CurlRequest* request = (CurlRequest*) (eventData[CurlComplete::P_CURLREQUEST].GetPtr());

    if (deactivate_.NotNull())
    {
        assert(request == deactivate_);

        if (deactivate_->GetError().Length())
        {
            String msg;
            msg.AppendWithFormat("Unable to deactivate with server: %s", deactivate_->GetError().CString());
            editor->PostModalError("Deactivation Error", msg);
            LOGERROR(msg);
        }
        else
        {
            String response = request->GetResponse();
            if (response.StartsWith("AC_FAILED"))
            {
                String msg;
                msg.AppendWithFormat("Unable to deactivate with server: %s", response.CString());
                editor->PostModalError("Deactivation Error", msg);
                LOGERROR(msg);
            }
            else if (response.StartsWith("AC_NOTACTIVATED") || response.StartsWith("AC_SUCCESS"))
            {
                ResetLicense();
                RemoveLicense();

                UIModalOps* ops = GetSubsystem<UIModalOps>();
                ops->Hide();
                ops->ShowActivation();
            }

        }

        UnsubscribeFromEvents(deactivate_);
        deactivate_ = 0;
    }

}
void LicenseSystem::HandleDeactivate(StringHash eventType, VariantMap& eventData)
{
    CurlRequest* request = (CurlRequest*) (eventData[CurlComplete::P_CURLREQUEST].GetPtr());

    VariantMap eventDataOut;

    if (deactivate_.NotNull())
    {
        assert(request == deactivate_);

        if (deactivate_->GetError().Length())
        {
            String msg = "Deactivation Error:\n";
            msg.AppendWithFormat("Unable to deactivate with server: %s", deactivate_->GetError().CString());

            eventDataOut[LicenseDeactivationError::P_MESSAGE] = msg;
            SendEvent(E_LICENSE_DEACTIVATIONERROR, eventDataOut);
        }
        else
        {
            String response = request->GetResponse();
            if (response.StartsWith("AC_FAILED"))
            {
                String msg = "Deactivation Error:\n";
                msg.AppendWithFormat("Unable to deactivate with server: %s", response.CString());

                eventDataOut[LicenseDeactivationError::P_MESSAGE] = msg;
                SendEvent(E_LICENSE_DEACTIVATIONERROR, eventDataOut);
            }
            else if (response.StartsWith("AC_NOTACTIVATED") || response.StartsWith("AC_SUCCESS"))
            {
                ResetLicense();
                RemoveLicense();
                SendEvent(E_LICENSE_DEACTIVATIONSUCCESS);
            }

        }

        UnsubscribeFromEvents(deactivate_);
        deactivate_ = 0;
    }

}
void LicenseSystem::HandleVerification(StringHash eventType, VariantMap& eventData)
{

    CurlRequest* request = (CurlRequest*) (eventData[CurlComplete::P_CURLREQUEST].GetPtr());

    bool licenseError = false;
    bool resetLicense = false;

    if (serverVerification_.NotNull())
    {
        assert(request == serverVerification_);

        if (serverVerification_->GetError().Length())
        {
            ATOMIC_LOGERRORF("Unable to verify with server: %s", serverVerification_->GetError().CString());
        }
        else
        {
            LicenseParse parse;
            int code = ParseResponse(serverVerification_->GetResponse(), parse);

            if (code == 4)
            {
                // not activated
                resetLicense = true;
                licenseError = true;
            }
            else if (code == 2)
            {
                // something is wrong with the key
                resetLicense = true;
                licenseError = true;

            }
            else if (code == 3)
            {
                // something is wrong on the activation server
                licenseError = true;
            }
            else if (code == 1)
            {
                // exceeded code, should not happen here as we aren't activating
                resetLicense = true;
                licenseError = true;
            }
            else if (code == 0)
            {
                // we should raise an error if there is a mismatch between local and server keys
                // when the local says there are more enabled than server?
                // otherwise, they could be being added

                bool mismatch = false;

                if (parse.licenseWindows_ != licenseWindows_)
                    mismatch = true;

                if (parse.licenseMac_ != licenseMac_)
                    mismatch = true;

                if (parse.licenseWindows_ != licenseWindows_)
                    mismatch = true;

                if (parse.licenseAndroid_ != licenseAndroid_)
                    mismatch = true;

                if (parse.licenseIOS_ != licenseIOS_)
                    mismatch = true;

                if (parse.licenseHTML5_ != licenseHTML5_)
                    mismatch = true;

                if (parse.licenseModule3D_ != licenseModule3D_)
                    mismatch = true;

                if (mismatch)
                {
                    ATOMIC_LOGERROR("License Mismatch, reseting");
                    licenseWindows_ = parse.licenseWindows_;
                    licenseMac_ = parse.licenseMac_;
                    licenseAndroid_ = parse.licenseAndroid_;
                    licenseIOS_= parse.licenseIOS_;
                    licenseHTML5_= parse.licenseHTML5_;
                    licenseModule3D_= parse.licenseModule3D_;

                    SaveLicense();
                }

                CreateOrUpdateLicenseCache();

                SendEvent(E_LICENSE_SUCCESS);
            }

        }

        UnsubscribeFromEvents(serverVerification_);
        serverVerification_ = 0;
    }

    if (resetLicense)
    {
        RemoveLicense();
        ResetLicense();
    }

    if (licenseError)
    {
        ATOMIC_LOGINFO("There was an issue with the atomic-cli activation.  Please reactivate or contact [email protected] if this problem persists");
        SendEvent(E_LICENSE_ERROR);
    }

}
void LicenseSystem::HandleVerification(StringHash eventType, VariantMap& eventData)
{

    CurlRequest* request = (CurlRequest*) (eventData[CurlComplete::P_CURLREQUEST].GetPtr());

    if (serverVerification_.NotNull())
    {
        assert(request == serverVerification_);

        if (serverVerification_->GetError().Length())
        {
            LOGERRORF("Unable to verify with server: %s", serverVerification_->GetError().CString());
        }
        else
        {
            LicenseParse parse;
            int code = ParseResponse(serverVerification_->GetResponse(), parse);

            // Not activated
            if (code == 4)
            {

            }
            else if (code == 2)
            {
                // something is wrong with the key
                LOGERRORF("Error with product key");

                RemoveLicense();
                ResetLicense();
                UIModalOps* ops = GetSubsystem<UIModalOps>();
                ops->Hide();
                ops->ShowActivation();

            }
            else if (code == 3)
            {
                // something is wrong on the activation server
                key_ = "";
            }
            else if (code == 1)
            {
                // exceeded code, should not happen here as we aren't activating
                key_ = "";
            }
            else if (code == 0)
            {
                // we should raise an error if there is a mismatch between local and server keys
                // when the local says there are more enabled than server?
                // otherwise, they could be being added

                bool mismatch = false;

                if (parse.licenseWindows_ != licenseWindows_)
                    mismatch = true;

                if (parse.licenseMac_ != licenseMac_)
                    mismatch = true;

                if (parse.licenseWindows_ != licenseWindows_)
                    mismatch = true;

                if (parse.licenseAndroid_ != licenseAndroid_)
                    mismatch = true;

                if (parse.licenseIOS_ != licenseIOS_)
                    mismatch = true;

                if (parse.licenseHTML5_ != licenseHTML5_)
                    mismatch = true;

                if (parse.licenseModule3D_ != licenseModule3D_)
                    mismatch = true;

                if (mismatch)
                {
                    LOGERROR("License Mismatch, reseting");
                    licenseWindows_ = parse.licenseWindows_;
                    licenseMac_ = parse.licenseMac_;
                    licenseAndroid_ = parse.licenseAndroid_;
                    licenseIOS_= parse.licenseIOS_;
                    licenseHTML5_= parse.licenseHTML5_;
                    licenseModule3D_= parse.licenseModule3D_;

                    SaveLicense();
                }

                //if (!HasPlatformLicense())
                //{
                //    UIModalOps* ops = GetSubsystem<UIModalOps>();
                //    if (!ops->ModalActive())
                //        ops->ShowPlatformsInfo();

               // }

            }

        }

        UnsubscribeFromEvents(serverVerification_);
        serverVerification_ = 0;
    }

}