Skip to content

Commit

Permalink
Splitter file handlers
Browse files Browse the repository at this point in the history
Better to let the mod launcher handle extensions and path matching
  • Loading branch information
Hampo committed Jun 27, 2024
1 parent ed038ae commit 1f51847
Show file tree
Hide file tree
Showing 16 changed files with 312 additions and 295 deletions.
20 changes: 19 additions & 1 deletion Randomiser/CustomFiles.ini
Original file line number Diff line number Diff line change
@@ -1,2 +1,20 @@
[PathHandlers]
*=Resources/ModuleHandler.lua
*=Resources/HandleGeneric.lua
*.mfk=Resources/HandleMFK.lua
*.con=Resources/HandleCON.lua
*.p3d=Resources/HandleP3D.lua

scripts\\missions\\level0?\\level.mfk=Resources/HandleLevelLoad.lua
scripts\\missions\\level0?\\leveli.mfk=Resources/HandleLevelInit.lua

scripts\\missions\\level0?\\m?sdl.mfk=Resources/HandleSundayDriveLoad.lua
scripts\\missions\\level0?\\m?sdi.mfk=Resources/HandleSundayDriveInit.lua

scripts\\missions\\level0?\\m?l.mfk=Resources/HandleMissionLoad.lua
scripts\\missions\\level0?\\m?i.mfk=Resources/HandleMissionInit.lua

scripts\\missions\\level0?\\sr?l.mfk=Resources/HandleStreetRaceLoad.lua
scripts\\missions\\level0?\\sr?i.mfk=Resources/HandleStreetRaceInit.lua

scripts\\missions\\level0?\\gr?l.mfk=Resources/HandleGambleRaceLoad.lua
scripts\\missions\\level0?\\gr?i.mfk=Resources/HandleGambleRaceInit.lua
28 changes: 28 additions & 0 deletions Randomiser/Resources/HandleCON.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
local Path = GetPath()
local GamePath

local CON

local isChanged = false
for moduleN=1,#Modules do
local module = Modules[moduleN]
local handlers = module.Handlers.CON

for handlerN=1,#handlers do
local handler = handlers[handlerN]

if WildcardMatch(Path, handler.Path, true, true) then
GamePath = GamePath or GetGamePath(Path)
CON = CON or MFKLexer.Lexer:Parse(ReadFile(GamePath))

print("ModuleHandler", "Running CON module: " .. module.Name)
local success, changed = pcall(handler.Callback, Path, CON)
assert(success, string.format("Error running CON handler from module \"%s\":\n%s", module.Name, changed))
isChanged = isChanged or changed
end
end
end

if isChanged then
CON:Output()
end
4 changes: 4 additions & 0 deletions Randomiser/Resources/HandleGambleRaceInit.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
if GambleRaceInit then
Output(GambleRaceInit)
GambleRaceInit = nil
end
28 changes: 28 additions & 0 deletions Randomiser/Resources/HandleGambleRaceLoad.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
local Path = GetPath()
local GamePath = GetGamePath(Path)

local GambleRaceLoad
local GambleRaceInit

local isChanged = false
for moduleN=1,#Modules do
local module = Modules[moduleN]
local handlers = module.Handlers.Race[CurrentLevel][4]

for handlerN=1,#handlers do
local handler = handlers[handlerN]

GambleRaceLoad = MissionLoad or MFKLexer.Lexer:Parse(ReadFile(GamePath))
GambleRaceInit = MissionInit or MFKLexer.Lexer:Parse(ReadFile(GamePath:sub(1, -6) .. "i.mfk"))

print("ModuleHandler", "Running gamble race module: " .. module.Name)
local success, changed = pcall(handler, CurrentLevel, 4, GambleRaceLoad, GambleRaceInit)
assert(success, string.format("Error running gamble race handler from module \"%s\":\n%s", module.Name, changed))
isChanged = isChanged or changed
end
end

if isChanged then
GambleRaceLoad:Output()
_G.GambleRaceInit = tostring(GambleRaceInit)
end
35 changes: 35 additions & 0 deletions Randomiser/Resources/HandleGeneric.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
if IsWriting() then
return
end

local Path = GetPath()
local GamePath = GetGamePath(Path)

local contents
local isChanged = false
for moduleN=1,#Modules do
local module = Modules[moduleN]
local handlers = module.Handlers.Generic

for handlerN=1,#handlers do
local handler = handlers[handlerN]

if WildcardMatch(Path, handler.Path, true, true) then
if Exists(GamePath, true, false) then
contents = contents or ReadFile(GamePath)
end

print("ModuleHandler", "Running generic module: " .. module.Name)
local success, changed, newContents = pcall(handler.Callback, contents)
assert(success, string.format("Error running generic handler from module \"%s\":\n%s", module.Name, changed))
if changed then
contents = newContents
isChanged = true
end
end
end
end

if isChanged then
Output(contents)
end
4 changes: 4 additions & 0 deletions Randomiser/Resources/HandleLevelInit.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
if LevelInit then
Output(LevelInit)
LevelInit = nil
end
29 changes: 29 additions & 0 deletions Randomiser/Resources/HandleLevelLoad.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
local Path = GetPath()
local GamePath = GetGamePath(Path)

CurrentLevel = tonumber(Path:match("level0(%d)"))
local LevelLoad
local LevelInit

local isChanged = false
for moduleN=1,#Modules do
local module = Modules[moduleN]
local handlers = module.Handlers.Level[CurrentLevel]

for handlerN=1,#handlers do
local handler = handlers[handlerN]

LevelLoad = LevelLoad or MFKLexer.Lexer:Parse(ReadFile(GamePath))
LevelInit = LevelInit or MFKLexer.Lexer:Parse(ReadFile(GamePath:sub(1, -5) .. "i.mfk"))

print("ModuleHandler", "Running level module: " .. module.Name)
local success, changed = pcall(handler, CurrentLevel, LevelLoad, LevelInit)
assert(success, string.format("Error running level handler from module \"%s\":\n%s", module.Name, changed))
isChanged = isChanged or changed
end
end

if isChanged then
LevelLoad:Output()
_G.LevelInit = tostring(LevelInit)
end
28 changes: 28 additions & 0 deletions Randomiser/Resources/HandleMFK.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
local Path = GetPath()
local GamePath

local MFK

local isChanged = false
for moduleN=1,#Modules do
local module = Modules[moduleN]
local handlers = module.Handlers.MFK

for handlerN=1,#handlers do
local handler = handlers[handlerN]

if WildcardMatch(Path, handler.Path, true, true) then
GamePath = GamePath or GetGamePath(Path)
MFK = MFK or MFKLexer.Lexer:Parse(ReadFile(GamePath))

print("ModuleHandler", "Running MFK module: " .. module.Name)
local success, changed = pcall(handler.Callback, Path, MFK)
assert(success, string.format("Error running MFK handler from module \"%s\":\n%s", module.Name, changed))
isChanged = isChanged or changed
end
end
end

if isChanged then
MFK:Output()
end
4 changes: 4 additions & 0 deletions Randomiser/Resources/HandleMissionInit.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
if MissionInit then
Output(MissionInit)
MissionInit = nil
end
34 changes: 34 additions & 0 deletions Randomiser/Resources/HandleMissionLoad.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
local Path = GetPath()
local GamePath = GetGamePath(Path)

CurrentMission = tonumber(Path:match("m(%d)l%.mfk"))
local MissionLoad
local MissionInit

local isChanged = false
for moduleN=1,#Modules do
local module = Modules[moduleN]
local handlers
if CurrentLevel == 1 then
handlers = module.Handlers.Mission[CurrentLevel][CurrentMission + 1]
else
handlers = module.Handlers.Mission[CurrentLevel][CurrentMission]
end

for handlerN=1,#handlers do
local handler = handlers[handlerN]

MissionLoad = MissionLoad or MFKLexer.Lexer:Parse(ReadFile(GamePath))
MissionInit = MissionInit or MFKLexer.Lexer:Parse(ReadFile(GamePath:sub(1, -6) .. "i.mfk"))

print("ModuleHandler", "Running mission module: " .. module.Name)
local success, changed = pcall(handler, CurrentLevel, CurrentMission, MissionLoad, MissionInit)
assert(success, string.format("Error running mission handler from module \"%s\":\n%s", module.Name, changed))
isChanged = isChanged or changed
end
end

if isChanged then
MissionLoad:Output()
_G.MissionInit = tostring(MissionInit)
end
28 changes: 28 additions & 0 deletions Randomiser/Resources/HandleP3D.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
local Path = GetPath()
local GamePath

local P3DFile

local isChanged = false
for moduleN=1,#Modules do
local module = Modules[moduleN]
local handlers = module.Handlers.P3D

for handlerN=1,#handlers do
local handler = handlers[handlerN]

if WildcardMatch(Path, handler.Path, true, true) then
GamePath = GamePath or GetGamePath(Path)
P3DFile = P3DFile or P3D.P3DFile(GamePath)

print("ModuleHandler", "Running P3D module: " .. module.Name)
local success, changed = pcall(handler.Callback, Path, P3DFile)
assert(success, string.format("Error running P3D handler from module \"%s\":\n%s", module.Name, changed))
isChanged = isChanged or changed
end
end
end

if isChanged then
P3DFile:Output()
end
4 changes: 4 additions & 0 deletions Randomiser/Resources/HandleStreetRaceInit.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
if StreetRaceInit then
Output(StreetRaceInit)
StreetRaceInit = nil
end
29 changes: 29 additions & 0 deletions Randomiser/Resources/HandleStreetRaceLoad.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
local Path = GetPath()
local GamePath = GetGamePath(Path)

CurrentStreetRace = tonumber(Path:match("sr(%d)l%.mfk"))
local StreetRaceLoad
local StreetRaceInit

local isChanged = false
for moduleN=1,#Modules do
local module = Modules[moduleN]
local handlers = module.Handlers.Race[CurrentLevel][CurrentStreetRace]

for handlerN=1,#handlers do
local handler = handlers[handlerN]

StreetRaceLoad = MissionLoad or MFKLexer.Lexer:Parse(ReadFile(GamePath))
StreetRaceInit = MissionInit or MFKLexer.Lexer:Parse(ReadFile(GamePath:sub(1, -6) .. "i.mfk"))

print("ModuleHandler", "Running street race module: " .. module.Name)
local success, changed = pcall(handler, CurrentLevel, CurrentStreetRace, StreetRaceLoad, StreetRaceInit)
assert(success, string.format("Error running street race handler from module \"%s\":\n%s", module.Name, changed))
isChanged = isChanged or changed
end
end

if isChanged then
StreetRaceLoad:Output()
_G.StreetRaceInit = tostring(StreetRaceInit)
end
4 changes: 4 additions & 0 deletions Randomiser/Resources/HandleSundayDriveInit.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
if SundayDriveInit then
Output(SundayDriveInit)
SundayDriveInit = nil
end
34 changes: 34 additions & 0 deletions Randomiser/Resources/HandleSundayDriveLoad.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
local Path = GetPath()
local GamePath = GetGamePath(Path)

CurrentSundayDrive = tonumber(Path:match("m(%d)sdl%.mfk"))
local SundayDriveLoad
local SundayDriveInit

local isChanged = false
for moduleN=1,#Modules do
local module = Modules[moduleN]
local handlers
if CurrentLevel == 1 then
handlers = module.Handlers.SundayDrive[CurrentLevel][CurrentSundayDrive + 1]
else
handlers = module.Handlers.SundayDrive[CurrentLevel][CurrentSundayDrive]
end

for handlerN=1,#handlers do
local handler = handlers[handlerN]

SundayDriveLoad = SundayDriveLoad or MFKLexer.Lexer:Parse(ReadFile(GamePath))
SundayDriveInit = SundayDriveInit or MFKLexer.Lexer:Parse(ReadFile(GamePath:sub(1, -6) .. "i.mfk"))

print("ModuleHandler", "Running sunday drive module: " .. module.Name)
local success, changed = pcall(handler, CurrentLevel, CurrentSundayDrive, SundayDriveLoad, SundayDriveInit)
assert(success, string.format("Error running sunday drive handler from module \"%s\":\n%s", module.Name, changed))
isChanged = isChanged or changed
end
end

if isChanged then
SundayDriveLoad:Output()
_G.SundayDriveInit = tostring(SundayDriveInit)
end
Loading

0 comments on commit 1f51847

Please sign in to comment.