Skip to content

Commit

Permalink
Introduce external modules
Browse files Browse the repository at this point in the history
  • Loading branch information
dfault-user committed Feb 13, 2024
1 parent 583c522 commit a84917c
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
local function Test(...)
return ...
end

return Test
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"ignoreUnknownInstances": true
}
27 changes: 21 additions & 6 deletions src/Spectre/Subsystems/ExampleService/init.lua
Original file line number Diff line number Diff line change
@@ -1,33 +1,48 @@
-- This is an example Spectre subsystem.
-- It contains the following: an example function, and an example command.
--

-- Do some variable initialization if you have to
local Players = game:GetService("Players")
local ServerScriptService = game:GetService("ServerScriptService")
local HttpService = game:GetService("HttpService")
local Spectre = ServerScriptService["Spectre"]
local Modules = {}
local Subsystems = {
LogService = require(Spectre["Subsystems"]["LogService"]),
}

-- Pull all Spectre modules (Also optional, but it's probably a good idea )
for i, v in pairs(Spectre["Modules"]:GetChildren()) do
Modules[`Spectre.{v.Name}`] = require(v)
end

-- Pull our own modules
for i, v in pairs(script["SpectreModules"]:GetChildren()) do
Modules[v.Name] = require(v)
end

-- The above is boilerplate, and is recommended for access to all Spectre modules. If you need to require any subsystems like the LogService at this point, do it now.
-- Subsystems required for operation
-- (This is optional if your service does not rely on any subsystems, but most services should subscribe to the LogService at least)
local Subsystems = {
LogService = require(Spectre["Subsystems"]["LogService"]),
}
-- Push to Spectre compartment in LogService
Subsystems.LogService:Push("Spectre",{
Origin = "ExampleService",
LoadedModules = Modules,
Ready = true
})

-- Initialize service table
local ExampleService = {}

-- Add method to service
function ExampleService:Test(...)
Modules.Output(`{script.Name}`, `Example fired: {...}`)
return ...
end

-- This is used to push an not-API API to Spectre for inclusion in _G.Spectre later
-- This is very much optional, all services will load without an API attached
ExampleService.API = {
Test = ExampleService.Test
}

return ExampleService -- All Spectre subsystems are ModuleScripts, and as such, need to return the subsystem to be valid
return ExampleService -- All services are modules
32 changes: 22 additions & 10 deletions src/Spectre/init.server.lua
Original file line number Diff line number Diff line change
Expand Up @@ -51,29 +51,41 @@ for i, v in pairs(Modules:GetChildren()) do
Spectre.Modules[v.Name] = require(v)
end

Modules = Spectre.Modules

-- Load Spectre Internal Commands
for i, v in pairs(Commands:GetChildren()) do
Spectre.Commands[v.Name] = require(v)
end

-- Load Spectre Subsystems
for i, v in pairs(Subsystems:GetChildren()) do
Spectre.Subsystems[v.Name] = require(v)
for i, Subsystem in pairs(Subsystems:GetChildren()) do
Spectre.Subsystems[Subsystem.Name] = require(Subsystem)
local HasAddtCommands
local Success, Result = pcall(v.FindFirstChildWhichIsA, v, "Folder", false)

if Success and Result then
HasAddtCommands = Result
local HasAddtModules

local AddtCommands = Modules.SafeFind(Subsystem, "SpectreCommands", "Folder")
local AddtModules = Modules.SafeFind(Subsystem, "SpectreModules", "Folder")

if AddtCommands then
for _,Command in pairs(AddtCommands:GetChildren()) do
Spectre.Commands[Command.Name] = require(Command)
end
end

if HasAddtCommands then
for i, v in pairs(v:FindFirstChild("SpectreCommands"):GetChildren()) do
Spectre.Commands[v.Name] = require(v)
if AddtModules then
for _,Module in pairs(AddtModules:GetChildren()) do
if Spectre.Modules[Module.Name] then
warn(`Attempting to deduplicate incoming module {Module.Name} from {Subsystem.Name}`)
Spectre.Modules[`{Module.Name}.{Subsystem.Name}`] = require(Module)
else
Spectre.Modules[Module.Name] = require(Module)
end
end
end

end

Modules = Spectre.Modules
Commands = Spectre.Commands
Subsystems = Spectre.Subsystems

Expand Down

0 comments on commit a84917c

Please sign in to comment.