"Require" a lua package

seed.of.apricot

Chieftain
Joined
Mar 24, 2015
Messages
8
Currently I am planning to make a new mod that overwrites existing lua with my code and contains "require" to import a certain lua package.
But I'm not sure if it is allowed to import another file in my lua code because it may cause a security vulnerability.

e.g.
local hogehoge = require("fugafuga")

Does anyone have a suggestion about this idea?
 
Well, my goal is implement http post/get request to report and get various stats in a game.
I have already added a web api method to my server, and am now thinking how to implement the client-side code.
The other day I found there is a lua package called luasocket that enabled a http request, but I didn't know how to import the package into my project.
That's why I generalized the question and asked it here.

While waiting your response, I tried some code.
Firstly, according to this post, I overwrote the original lua code "UI/FrontEnd/MainMenu.lua" with adding the following code to the method "OnOptions()".

  1. function OnOptions()

  2. local socket = include("socket")
  3. client = socket.connect("google.com", 80)
  4. client:send("GET /robots.txt HTTP/1.0\r\n\r\n")
  5. client:close()
  6. UIManager:QueuePopup(Controls.Options, PopupPriority.Current);
  7. Close();
  8. end

When I enabled the mod and pressed "Options" in the main menu, the Firetuner2 log displayed something wrong.
They indicated that the problem was on the line 4, so I guessed including the package had been done successfully at first.
However, entering "print(socket)" returned nil in the console, which means the package was not loaded.

There are some network function in the original lua codes to communicate with other clients and steam servers,
thus I wonder luasocket or some other packages for network communication might be installed.

Is it possible to access those package within my code, or just let my mod contain the package and include it?
 
The system regards my previous post as a spam and I cannot edit it... This is the edited one.

Code:
function OnOptions()

    local socket = include("socket");
    client = socket.connect("google.com", 80);
    client:send("GET /robots.txt HTTP/1.0\r\n\r\n");
    while(true) do
      s, status, partial = client:receive(1024);
      print(s or partial);
      if (status == "closed") then
        break;
      end
    end
    client:close();
  
    UIManager:QueuePopup(Controls.Options, PopupPriority.Current);
    Close();
end
 
in civ6 and civ5 lua the include("Name.lua") function adds the contents of file Name.lua into the lua file that contains the "include" command. But the file Name.lua has to already have been added into the game's file system, either by an "Import" action in the modinfo file, or by being a file that the base-game already adds into the file system.

If there is no pre-existing file in the system called "socket.lua" then variable socket will return boolean nil. I am not sure when attempting to include the entire contents of a file into a variable whether you get an indexable table (if the file is essentially code that creates a table) or whether you merely get boolean true. I've never attempted to use "include" in the way you are doing.

But to be honest other than this I am still not sure I understand what you are trying to do. Maybe if @Gedemon takes a look at your thread he'll be able to give you some better advice on what it is you want to do.
 
Last edited:
AFAIK we can now "include" Lua files that are added in the VFS (and my workaround is no more needed), but "require" is still not possible (you need it to load modules that may include DLL), and I wouldn't expect it to be allowed at some point.
 
Thanks for your information @LeeS and @Gedemon. I understood that at least there is no way to include DLL into my mod.
So practically, to communicate something with my server, it is necessary to log some records in the Civ6 Client and then let another software read them and send http requests, is that correct?
 
Last edited:
Back
Top Bottom