Return file data . lua in PHP in array form

Asked

Viewed 105 times

0

I need to access some data stored in a file .lua in PHP.

This is the contents of the archive meu_arquivo.lua:

tbl = {
    [22004] = {
        unidentifiedDisplayName = "Sapato",
        unidentifiedResourceName = "»÷µé",
        unidentifiedDescriptionName = {
            "Item não identificado. Pode ser identificado com uma [Lupa]."
        },
        identifiedDisplayName = "Botas Temporais DES",
        identifiedResourceName = "½Ã°£ÀǼÕÀçÁÖºÎÃ÷",
        identifiedDescriptionName = {
            "Botas incríveis que devem ter sido criadas há muito tempo, mas nunca foram usadas.",
            "^0000ffDEX +35.^000000",
            "Tipo: ^777777Calçado^000000",
            "Defesa: ^77777725^000000",
            "Peso: ^777777350^000000",
            "Nível necessário: ^77777799^000000",
            "Classes: ^777777Todas as classes^000000"
        },
        slotCount = 1,
        ClassNum = 0
    },
    [22005] = {
        unidentifiedDisplayName = "Sapato",
        unidentifiedResourceName = "»÷µé",
        unidentifiedDescriptionName = {
            "Item não identificado. Pode ser identificado com uma [Lupa]."
        },
        identifiedDisplayName = "Botas Temporais SOR",
        identifiedResourceName = "½Ã°£ÀÇÇà¿îºÎÃ÷",
        identifiedDescriptionName = {
            "Botas incríveis que devem ter sido criadas há muito tempo, mas nunca foram usadas.",
            "^0000ffLUK +35.^000000",
            "^0000ff+20 de Crítico.^000000",
            "Tipo: ^777777Calçado^000000",
            "Defesa: ^77777725^000000",
            "Peso: ^777777350^000000",
            "Nível necessário: ^77777799^000000",
            "Classes: ^777777Todas as classes^000000"
        },
        slotCount = 1,
        ClassNum = 0
    }
}

Example: I want to return the value of tbl > 22004 > unidentifiedDisplayName that would be Sapatoas if it were an array(array) proper to PHP:

<?= $meu_arquivo['tbl']['22004']['unidentifiedDisplayName']; // mostra "Sapato" ?>

I don’t know the syntax/language of this file to point my research more accurately. My only reference is that the file is read via MOON by the original application (Game).

As much as it bears a resemblance to JSON, the function json_decode didn’t work for this file.

Any help is welcome!

1 answer

1


One solution is to use the Lua extension for PHP: http://php.net/manual/en/class.lua.php

Follow an example using your file .lua as an input:

<?php
$lua = new Lua('./file.lua');
var_dump($lua->tbl[22004]['unidentifiedDisplayName']);

Produces the following output:

string(6) "Sapato"

I’ll also leave here an example of installing the moon extension in PHP (of course depending on your production environment the instructions may vary, but as I had to quickly have an environment to run the proposed solution, I thought it might be useful to share)

FROM php:latest

RUN apt-get -y update \
 && apt-get -y install lua5.2 liblua5.2-0 liblua5.2-dev \
 && cp /usr/include/lua5.2/lua.h /usr/include/ \
 && ln -s /usr/include/lua*.*/ /usr/include/lua \
 && cp /usr/lib/x86_64-linux-gnu/liblua5.2.a /usr/lib/liblua.a \
 && cp /usr/lib/x86_64-linux-gnu/liblua5.2.so /usr/lib/liblua.so \
 && pecl install lua-2.0.2 \
 && docker-php-ext-enable lua
  • 1

    Dude, it worked great! Only thing I didn’t like was the need to tinker with PHP extensions/components (PEAR/PECL). It will make it a little difficult for hosting to add this. But it’s already worth the joinha!

  • 1

    Glad I could help! maybe this Composer package is better for you then: https://github.com/koesie10/LuaSerializer (Capacity is limited to serialization only, but at least you do not need to install any extension)

  • So, I ended up creating a class using regexp to do the service I need. It wasn’t generic: it’s very specific to my file. But it turned out to be all right. It’s very functional. But anyway, this package is 10 and totally dynamic. I’ll try it too!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.