Splitting plugin in files
#1
I am trying to break my plugin into files, IE SqlLite.lua, Commands.lua, etc... So I don't have this massive single huge file with everything in it.

The problem is that I don't understand how the different files are loaded by the cuberite server, IE do i have to use like a main.lua file that tells the cuberite server all of my files, or since the files live in the plugin folder they are all loaded?

I been looking at all of the multiple file plugins and the connection on how this works is eluding me at this time.

If someone could post me some language on how to search for the answer it would be great, or if you just want to tell me that is cool too.

Thank you for your time and efforts on helping this.
Reply
Thanks given by:
#2
Just did some testing (also from working with plugins for a bit).

Any .lua files in the top level folder will be able to communicate. I ASSUME that they are all being compiled as if is they were one file, so they can share function calls and variables.

However, anything inside of nested folders does not seem to automatically get loaded in. You SHOULD be able to use require. I see no reason at all why it wouldn't work, but I've seen weirder things in the past.
Reply
Thanks given by: chrobione
#3
Cuberite loads all Lua files in the plugin's folder, and initializes them in alphabetical order, except for the Info.lua file, which gets initialized always as the last one.
Note that the sort order is case sensitive, so on platforms with case-preserving filesystems (almost all todays platforms, including Windows) it makes a difference if you name your file A.lua or a.lua.
Reply
Thanks given by: chrobione
#4
Also note that if you break stuff into files, all "local" declarations will not be shared between the files, while all global (default) declarations will
Reply
Thanks given by:
#5
(12-18-2015, 04:17 PM)Zee1234 Wrote: Just did some testing (also from working with plugins for a bit).

Any .lua files in the top level folder will be able to communicate. I ASSUME that they are all being compiled as if is they were one file, so they can share function calls and variables.

However, anything inside of nested folders does not seem to automatically get loaded in. You SHOULD be able to use require. I see no reason at all why it wouldn't work, but I've seen weirder things in the past.


Thank you for information and thank you for the research material to read very helpful indeed.

chrobi
Reply
Thanks given by:
#6
(12-18-2015, 05:01 PM)xoft Wrote: Cuberite loads all Lua files in the plugin's folder, and initializes them in alphabetical order, except for the Info.lua file, which gets initialized always as the last one.
Note that the sort order is case sensitive, so on platforms with case-preserving filesystems (almost all todays platforms, including Windows) it makes a difference if you name your file A.lua or a.lua.

Thank you for this information. I will make sure that my naming conventions will follow suit with need for my loading orders.

I really appreciate everyone's help with this.

Chrobi
Reply
Thanks given by:
#7
One more note. Assume you have this format:

--#Main.lua
require "subfolder/filename.lua"

function Initialize(a_Plugin)
  --stuff

  function_from_filename()
  --stuff
end
--#subfolder/filename.lua
function function_from_filename()
  --stuff
  local_function_in_filename()
end

local function local_function_in_filename()
  --stuff
end

This apparently doesn't work. You have to define the local function above the first function. Although, this is probably a "Lua" thing, not a "Cuberite" thing.
Reply
Thanks given by:
#8
The "require" method would be really good to enforce a good structure to the plugins. Why doesn't the server load the files that way?
Reply
Thanks given by:
#9
It' also possible to use dofile to load a lua file. I have this code, that loads all lua files in the folder from the array folders. This also allows you to structure your files. The code has to be placed outside of a function, in a lua file in your main plugin folder.

function LoadLuaFiles()
	local folders =  { "/code", "/code/classes", "/code/commands" }
	local localFolder = cPluginManager:GetCurrentPlugin():GetLocalFolder()

	for _, folder in pairs(folders) do
		local files = cFile:GetFolderContents(localFolder .. "/" .. folder)
		for _, file in pairs(files) do
			if (string.sub(file, #file -3, #file) == ".lua") then
				dofile(localFolder .. folder .. "/" .. file)
			end
		end
	end
end
LoadLuaFiles()
Reply
Thanks given by:
#10
@Seadragon91 that depends. If the files in the subfolders contain command handlers then that means the Info.lua file won't have them. Initialize is called after all files in the root of the plugin are loaded. It's the reason why I have the code to load the subfolders in WorldEdit outside the Initialize function: https://github.com/cuberite/WorldEdit/bl...ua#L24-L36

That makes me think, can't we exclude the Info.lua file, and let the InfoReg file load it? Then it's loaded only when needed.
Reply
Thanks given by:




Users browsing this thread: 4 Guest(s)