# Lua Projects

### Prerequisites

* Lua 5.1 or higher
* LuaSocket library
* JSON library (dkjson or similar)
* SunLicense API credentials

### Integration Steps

#### 1. Create License Validator

```lua
local socket = require("socket")
local http = require("socket.http")
local ltn12 = require("ltn12")
local json = require("dkjson")

local SunLicenseValidator = {}
SunLicenseValidator.__index = SunLicenseValidator

function SunLicenseValidator.new(licenseKey, productId, version)
    local self = setmetatable({}, SunLicenseValidator)
    self.licenseKey = licenseKey
    self.productId = productId
    self.version = version or "1.0.0"
    self.apiUrl = "YOUR_API_URL/api/v1/validate"
    return self
end

function SunLicenseValidator:validate()
    local payload = {
        licenseKey = self.licenseKey,
        productId = self.productId,
        productVersion = self.version,
        hwid = self:getHWID() -- Optional
    }

    local jsonPayload = json.encode(payload)
    local response = {}

    local _, code = http.request{
        url = self.apiUrl,
        method = "POST",
        headers = {
            ["Content-Type"] = "application/json",
            ["Content-Length"] = #jsonPayload
        },
        source = ltn12.source.string(jsonPayload),
        sink = ltn12.sink.table(response)
    }

    if code ~= 200 then
        error("License validation failed with code: " .. tostring(code))
    end

    return true
end

function SunLicenseValidator:getHWID()
    -- Implement your HWID generation logic here
    return "YOUR-HWID"
end
```

#### 2. Basic Implementation

```lua
local function main()
    local validator = SunLicenseValidator.new(
        "YOUR-LICENSE-KEY",
        YOUR_PRODUCT_ID,
        "1.0.0"
    )

    local success, err = pcall(function()
        return validator:validate()
    end)

    if success then
        print("License is valid!")
        -- Continue with your application
    else
        print("License validation failed: " .. tostring(err))
        os.exit(1)
    end
end

main()
```

#### 3. Integration with LOVE2D (Game Framework)

```lua
function love.load()
    validator = SunLicenseValidator.new(
        "YOUR-LICENSE-KEY",
        YOUR_PRODUCT_ID
    )
    
    success, err = pcall(function()
        return validator:validate()
    end)
    
    if not success then
        error_message = err
        love.event.quit()
    end
end

function love.draw()
    if error_message then
        love.graphics.print("License Error: " .. error_message, 10, 10)
    end
end
```

### Best Practices

1. **Error Handling**
   * Use pcall for error handling
   * Implement proper logging
   * Handle network timeouts
2. **Security**
   * Secure storage of license key
   * Implement HTTPS communication
   * Handle sensitive data carefully
3. **Performance**
   * Cache validation results
   * Implement retry logic
   * Handle offline scenarios

### Common Issues

1. **Network Problems**
   * Implement timeout handling
   * Add retry logic
   * Handle connection errors
2. **SSL/HTTPS Issues**
   * Configure proper SSL certificates
   * Handle HTTPS requirements
   * Verify API endpoint security
3. **JSON Parsing**
   * Handle malformed responses
   * Validate response format
   * Proper error handling


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.sunlicense.hapangama.com/product-integrations/lua-projects.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
