DC-SWAT Forum
Help with LUA scripting. - Версия для печати

+- DC-SWAT Forum (http://www.dc-swat.ru/forum)
+-- Форум: DreamShell (/forum-3.html)
+--- Форум: Programming (/forum-28.html)
+--- Тема: Help with LUA scripting. (/thread-4103.html)



Help with LUA scripting. - MastaG - 07.08.2024 12:31

Hi threre gang,

I'd like to offer my Dreamcast for helping out with some automated test cases.
For that I'd like DreamShell to download an iso file from a plain http server and run it at startup instead of showing the main menu.

So I'm trying to edit the DS/lua/startup.lua file:

Код:
-----------------------------------------
--                                     --
-- @name:     Startup script           --
-- @author:   SWAT                       --
-- @url:      http://www.dc-swat.ru    --
--                                     --
-----------------------------------------
--
-- Internal DreamShell lua functions:
--
--    OpenModule             Open module file and return ID
--    CloseModule            Close module by ID
--    GetModuleByName        Get module ID by module NAME
--
--    AddApp                 Add app by XML file, return app NAME
--    OpenApp                Open app by NAME (second argument for args)
--    CloseApp               Close app by NAME (second argument for change unload flag)
--
--    ShowConsole
--    HideConsole
--    SetDebugIO             Set the debug output (scif, dclsocket, fb, ds, sd). By default is ds.
--    Sleep                  Sleep in current thread (in ms)
--    MapleAttached          Check for attached maple device
--
--    Bit library:           bit.or, bit.and, bit.not, bit.xor
--    File system library:   lfs.chdir, lfs.currentdir, lfs.dir, lfs.mkdir, lfs.rmdir
--    
------------------------------------------

local DreamShell = {

    initialized = false,
    
    modules = {
        --"tolua",
        --"tolua_2plus",
        --"luaDS",            -- Depends: tolua
        --"luaKOS",           -- Depends: tolua
        --"luaSDL",           -- Depends: tolua
        --"luaGUI",           -- Depends: tolua
        --"luaMXML",          -- Depends: tolua
        --"luaSTD",           -- Depends: tolua
        --"sqlite3",
        --"luaSQL",           -- Depends: sqlite3
        "luaSocket",
        --"luaTask",
        --"bzip2",
        "minilzo",
        --"zip",              -- Depends: bzip2
        "http",
        --"httpd",
        --"telnetd",
        --"mongoose",
        --"ppp",
        --"mpg123",
        --"oggvorbis",
        --"adx",
        --"s3m",
        --"wav",
        --"xvid",
        --"SDL_mixer",        -- Depends: oggvorbis
        --"ffmpeg",           -- Depends: oggvorbis, mpg123, bzip2
        --"opengl",
        "isofs",            -- Depends: minilzo
        "isoldr"            -- Depends: isofs
        --"SDL_net",
        --"opkg",             -- Depends: minilzo
        --"aicaos",
        --"gumbo",
        --"ini",
        --"bflash",
        --"openssl",
        --"bitcoin",
        --"quirc",
        --"ftpd"
    },

    Initialize = function(self)

        os.execute("env USER Default");
        local path = os.getenv("PATH");

        print(os.getenv("HOST") .. " " .. os.getenv("VERSION") .. "\n");
        print(os.getenv("ARCH") .. ": " .. os.getenv("BOARD_ID") .. "\n");
        print("Date: " .. os.date() .. "\n");
        print("Base path: " .. path .. "\n");
        print("User: " .. os.getenv("USER") .. "\n");

        local emu = os.getenv("EMU");

        if emu ~= nil then
            print("Emulator: " .. emu .. "\n");
        end

        print("\n");

        if not MapleAttached("Keyboard") then
            table.insert(self.modules, "vkb");
        end

        table.foreach(self.modules, function(k, name)  
            print("DS_PROCESS: Loading module " .. name .. "...\n");
            if not OpenModule(path .. "/modules/" .. name .. ".klf") then
                print("DS_ERROR: Can't load module " .. path .. "/modules/" .. name .. ".klf\n");
            end
        end);

        self:InstallingApps(path .. "/apps");
        self.initialized = true;
        
        # Setup network
        os.execute("net --init");
        
        # disable FTP server for now
        --os.execute("ftpd -s -p 21 -d /");
        
        # try and download my file
        socket = require("socket")
        http = require("socket.http")
        ltn12 = require("ltn12")

        local file = ltn12.sink.file(io.open('/ide/GAMES/mygame/game.iso', 'w'))
        http.request {
                url = 'http://myserver/myfile.iso',
                sink = file,
        }
        
        # Run isoloader but I don't know how...
        OpenApp(os.getenv("APP"));
    end,

    InstallingApps = function(self, path)

        print("DS_PROCESS: Installing apps...\n");
        local name = nil;
        local list = {};

        for ent in lfs.dir(path) do
            if ent ~= nil and ent.name ~= ".." and ent.name ~= "." and ent.attr ~= 0 then
                table.insert(list, ent.name);
            end
        end

        table.sort(list, function(a, b) return a > b end);

        for index, directory in ipairs(list) do

            name = AddApp(path .. "/" .. directory .. "/app.xml");

            if not name then
                print("DS_ERROR: " .. directory .. "\n");
            else
                print("DS_OK: " .. name .. "\n");
            end
        end

        return true;
    end
};

if not DreamShell.initialized then
    DreamShell:Initialize();
end

As you can see I didn't got very far.

Any help would be greatly appreciated.


RE: Help with LUA scripting. - MastaG - 07.08.2024 13:09

I figured out the isoloader command:
Код:
isoldr -i -a -x 0x8c004000 -f /ide/GAMES/mygame/game.iso -o 1 -h 0

But it seems to hang on boot.


RE: Help with LUA scripting. - SWAT - 08.08.2024 05:57

Hang on network?
In general, there is some issues in KOS related to the simultaneous operation of AICA and BBA. In DS music plays at startup. Try to wait until it finishes playing or just turn it off by commenting this line: https://github.com/DC-SWAT/DreamShell/blob/master/src/main.c?ts=4#L217
In it not help, seems issues in lua http, I never test it.
You can also try the http module, it adds a /http to the file system through which you can work with normal file operations, for example: /http/192.168.1.2/file.iso
But again, all the network stuff is not very stable yet, I haven’t got around to it.
And there are issues in the KOS itself, which are clearly visible in the FTP server, in one direction the data goes very poorly, in the other direction it goes well.


RE: Help with LUA scripting. - MastaG - 08.08.2024 11:59

Then I need to build v4.0.0 from source, using the source code from: https://github.com/DC-SWAT/DreamShell/releases/tag/v4.0.0.Release
For the toolchain which compiler version did you use?
config.mk.10.5.0.sample config.mk.12.3.0.sample config.mk.14.0.1-dev.sample config.mk.9.3.0-legacy.sample config.mk.stable.sample
config.mk.11.4.0.sample config.mk.13.2.1-dev.sample config.mk.4.7.4-legacy.sample config.mk.9.5.0-winxp.sample

EDIT the binary DS_CORE of the v4.0.0 final release says: sh-elf-gcc (GCC) 13.2.0
So I'm going to use: config.mk.13.2.1-dev.sample

EDIT2:
Just need to read the README.md of the source code release.


RE: Help with LUA scripting. - MastaG - 08.08.2024 14:23

@SWAT
Would you happen to know if the iso loader was still working with this commit (and KOS commit?): https://github.com/DC-SWAT/DreamShell/commit/d7767d0d678e337d1bda154791534b0067422670
I want to build the last commit where both the iso loader and bios was working.
I think this would be: https://github.com/DC-SWAT/DreamShell/commit/83b39f4a9e331fd75e2077bd9ec3f27462df1e07

Because after this commit, you've updated ds/sdk/doc/KallistiOS.txt with a newer version and I think you said that it broke the bios and iso loader.

EDIT: Nope..
https://github.com/DC-SWAT/DreamShell/commit/83b39f4a9e331fd75e2077bd9ec3f27462df1e07 Seems too far ahead.
Quake3 reboots itself.


RE: Help with LUA scripting. - SWAT - 09.08.2024 06:11

Updating the KOS only affects the core and bootloader. It does not affect the ISO loader.
The ISO Loader is a bit broken at the moment, as some development is going on.
If you want a working version, use the release version.
If you want to develop something, just run other games that work. Most games work.
What is your goal?


RE: Help with LUA scripting. - MastaG - 09.08.2024 18:40

You're correct about the iso loader, in later versions it seems most games work fine.

Well my goal is to turn on the DC.
It should Download an iso file from a http server and then run the iso loader.

My first goal is to not have it show the DreamShell logo, but actually show what is going on by showing the conole.
I've edited main.c to read:
Код:
setenv("HOST", "DreamShell", 1);
        setenv("OS", getenv("HOST"), 1);
        setenv("USER", getenv("HOST"), 1);
        setenv("ARCH", hardware_sys_mode(&tmpi) == HW_TYPE_SET5 ? "Set5.xx" : "Dreamcast", 1);

        setenv("NET_IPV4", "0.0.0.0", 1);

        setenv("SDL_DEBUG", "0", 1);
        setenv("SDL_VIDEODRIVER", "dcvideo", 1);

        snd_stream_init();
        // Do not play startup audio
        // ds_sfx_play(DS_SFX_STARTUP);

        InitVideoHardware();
        // Do not show bootlogo
        // ShowLogo();

        vmu_draw_string(getenv("HOST"));
        dbglog(DBG_INFO, "Initializing DreamShell Core...\n");

        SetConsoleDebug(1);
        // Show the console
        ShowConsole();

        setenv("HOME", getenv("PATH"), 1);
        setenv("$PATH", getenv("PATH"), 1);
        setenv("LUA_PATH", getenv("PATH"), 1);
        setenv("LUA_CPATH", getenv("PATH"), 1);
        setenv("PWD", fs_getwd(), 1);
        setenv("APP", (settings->app[0] != 0 ? settings->app : "Main"), 1);

Without any edits to startup.lua.

But it hangs now..


RE: Help with LUA scripting. - MastaG - 09.08.2024 19:28

Okay so I got a bit further.
In src/main.c I've only commented out the startup sound.
It seems ShowLogo(); is required after InitVideoHardware();


Now in lua/startup.lua:

I've only added network init and trying to start gta3:
Код:
        self:InstallingApps(path .. "/apps");
        self.initialized = true;
        os.execute("net --init");
        isoldr -i -a -x 0x8c004000 -f /ide/GTA3/regta3dc.ds.iso -o 1 -h 0
        OpenApp(os.getenv("APP"));

Also the following modules are enabled: http, minilzo, isofs and isoldr.

But the isoldr command right before the OpenApp seems to hang the console.


RE: Help with LUA scripting. - MastaG - 09.08.2024 19:44

Okay, got the iso to auto start Big Grin
Код:
        self:InstallingApps(path .. "/apps");
        self.initialized = true;
        os.execute("net --init");
        OpenApp(os.getenv("APP"));
        os.execute("isoldr -i -a -x 0x8c000100 -f /ide/GTA3/regta3dc.ds.iso -o 1 -h 0");

Now I only need to copy the new iso from the http server before it executes the isoloader.


RE: Help with LUA scripting. - MastaG - 12.08.2024 11:56

I'm still awaiting my serial cable so I can actually see what is going on.
But according to "the inernet", it should be something like this:
Код:
...
        self:InstallingApps(path .. "/apps");
        self.initialized = true;
        
        -- Networking
        os.execute("net --init");
        
        -- Trying to download a file
        local ltn12 = require("ltn12");
        ltn12.pump.all(
            ltn12.source.file(assert(io.open("/http/xxx/regta3dc.ds.iso", "rb"))),
            ltn12.sink.file(assert(io.open("/ide/GTA3/regta3dc.ds.iso", "wb")))
        );
        
        OpenApp(os.getenv("APP"));
        -- Run GTA3
        os.execute("isoldr -i -a -x 0x8c000100 -f /ide/GTA3/regta3dc.ds.iso -o 1 -h 0");
        os.execute("console --show");
    end,
...

Unfortunately it seems to hang here.
Any ideas @SWAT ?


RE: Help with LUA scripting. - SWAT - 13.08.2024 06:51

Network issue, HTTP module issue, Lua issue Smile
I don't know and I haven't tested it like that.
Network capabilities in DS have still received little attention.
What I tested well is ping and ftp server module. They work, but ftp server shows that in KallistiOS there is a issue with the network in one direction, from PC to DC.
You can get data from the DC well, but you can only send a small amount to the DC in a reasonable amount of time. A large size will make it infinite.