Skip to content

Commit 3974fef

Browse files
authored
Merge pull request #28 from cuberite/ScenarioRelativeRedirect
Scenario relative paths
2 parents 8374925 + b9057b2 commit 3974fef

6 files changed

Lines changed: 90 additions & 10 deletions

File tree

APIImpl/File.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ return
3333
assert(a_DstFileName ~= "")
3434
local isSuccess, msg = utils.copyFile(a_SrcFileName, a_DstFileName)
3535
if not(isSuccess) then
36-
a_Simulator.logger.debug("cFile:Copy failed: %s", msg)
36+
a_Simulator.logger:debug("cFile:Copy failed: %s", msg)
3737
end
3838
return isSuccess
3939
end,

Options.lua

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ The options object contains the following fields:
2323
pluginFiles - array-table of filenames for all plugin files to check. Each item already includes pluginPath
2424
pluginPath - path to the plugin's files
2525
scenarioFileName - filename to load as scenario file
26+
scenarioPath - path to the scenario file (used for relative FS redirection)
2627
shouldClearObjects - bool specifying whether API objects should be cleared after each callback (thus detecting potential use-after-callback)
2728
shouldGCObjects - bool specifying whether API objects should be GC-ed after each callback (thus detecting storage-after-callback)
2829
prefillSymbolPatterns - array-table of patterns used to fill the sandbox of the plugin
@@ -145,6 +146,12 @@ local optionProcessor =
145146
)
146147
end
147148
a_Options.scenarioFileName = a_Args[a_Idx + 1]
149+
local idxLastSlash = string.find(a_Options.scenarioFileName, "/[^/]*$")
150+
if (idxLastSlash) then
151+
a_Options.scenarioPath = string.sub(a_Options.scenarioFileName, 1, idxLastSlash)
152+
else
153+
a_Options.scenarioPath = ""
154+
end
148155
if not(a_Options.scenarioFileName) then
149156
error(string.format("Invalid option \"-s\" (%d), expected a scenario filename following it.", a_Idx))
150157
end

ScenarioFiles.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,15 @@ Note that the scenario needs to explicitly specify when to load / initialize the
3232
The following actions can be used in a scenario:
3333

3434
## redirect
35-
Adds redirection for files and folders. Any Cuberite API that takes a file or folder first checks if there's any redirection for the path. If the beginning of a path to be used matches any of the redirects given, the appropriate part of the path will be replaced. For example, consider a redirect of `["a/b/"] = "c/d/"`, the following table sums up the redirection performed:
35+
Adds redirection for files and folders. Any Cuberite API that takes a file or folder first checks if there's any redirection for the path. If the beginning of a path to be used matches any of the redirects given, the appropriate part of the path will be replaced. The redirected paths are relative to the scenario file's folder. For example, consider a redirect of `["a/b/"] = "c/d/"` and the scenario file being used is `tests/scenario.lua`, the following table sums up the redirection performed:
3636

3737
Path used by plugin | Path actually used | Notes
3838
--------------------|--------------------|------
3939
a.txt | a.txt | Partial matches are ignored
4040
a/b.txt | a/b.txt | Not matching the full redirect path
41-
a/b/z.txt | c/d/z.txt | Redirect performed
41+
a/b/z.txt | tests/c/d/z.txt | Redirect performed
4242
c/a/b/z.txt | c/a/b/z.txt | Not matching the *beginning* of the path
43-
c/../a/b/z.txt | c/d/z.txt | Redirect takes into account relative paths
43+
e/../a/b/z.txt | tests/c/d/z.txt | Redirect takes into account collapsed relative paths
4444
c/d/../a/b/z.txt | c/a/b/z.txt | Relative path collapsed, but not matching the *beginning* of the path
4545

4646
Takes a dictionary table which maps old paths to new paths as its parameter. Note that paths always use a slash, even on Windows! Also note that most paths cannot be used in the simple Lua table key format, but will need to use the brackets instead:
@@ -98,7 +98,7 @@ b b a
9898
b b b
9999
```
100100

101-
Takes a dictionary table as its parameter. The `playerName` value specifies the player to impersonate for the commands. The `choices` specifies an array of strings that are used for the parameters. The 'maxLen' specifies the maximum number of parameters, the optional `minLen` (default: 0) specifies the minimum number of parameters.
101+
Takes a dictionary table as its parameter. The `playerName` value specifies the player to impersonate for the commands. The `choices` specifies an array of strings that are used for the parameters. The 'maxLen' specifies the maximum number of parameters, the optional `minLen` (default: 0) specifies the minimum number of parameters.
102102

103103
If, at the time the action is executed, the specified player is not connected, the Checker aborts with an error message.
104104

Simulator.lua

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1382,7 +1382,9 @@ function Simulator:redirectPath(a_Path)
13821382
-- If there is a matching entry in the redirects, use it:
13831383
local match = self.redirects[a_Path]
13841384
if (match) then
1385-
return match
1385+
local res = self.options.scenarioPath .. match
1386+
self.logger:trace(string.format("Redirecting \"%s\" to \"%s\".", a_Path, res))
1387+
return res
13861388
end
13871389

13881390
-- If there is a full foldername match, use it:
@@ -1391,13 +1393,17 @@ function Simulator:redirectPath(a_Path)
13911393
while (true) do
13921394
idx = a_Path:find("/", idx + 1) -- find the next slash
13931395
if not(idx) then
1396+
-- No redirection match
13941397
return a_Path
13951398
end
13961399
local match = self.redirects[a_Path:sub(1, idx)] -- check the path up to the current slash for redirects:
13971400
if (match) then
1398-
return match .. a_Path.sub(idx + 1)
1401+
local res = self.options.scenarioPath .. match .. a_Path.sub(idx + 1)
1402+
self.logger:trace(string.format("Redirecting \"%s\" to \"%s\".", a_Path, res))
1403+
return res
13991404
end
14001405
end
1406+
assert(false, "Should never get here, the above is an infinite loop with return points")
14011407
end
14021408

14031409

tests/Gallery/FuzzCommands.cfg

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
2+
-- This is an example configuration file. It uses Lua syntax to describe individual galleries and other settings
3+
-- This file will be copied over to $/Server/Galleries.example.cfg to give the server admins a skeleton to work on
4+
5+
6+
7+
8+
9+
Galleries =
10+
{
11+
{
12+
-- Name of the gallery, as used in the commands
13+
Name = "first";
14+
15+
-- Name of the world for which the gallery is defined.
16+
WorldName = "world";
17+
18+
-- Dimensions of the gallery
19+
MinX = 100;
20+
MinZ = 100;
21+
MaxX = 200;
22+
MaxZ = 10000;
23+
24+
-- The areas don't have a template, use the current world's contents instead:
25+
AreaSizeX = 16,
26+
AreaSizeZ = 16,
27+
28+
-- Edge of each area that is "public", i. e. non-buildable even by area's owner.
29+
-- This is so that the AreaTemplate may include e. g. sidewalks that will be preserved
30+
-- Setting this to N means that N blocks from each area's edge are protected, i. e. sidewalk of width 2 * N
31+
AreaEdge = 2;
32+
33+
-- Strategy used for allocating the areas in the gallery.
34+
-- Represents the direction and the order in which areas are allocated out of the gallery
35+
-- "x+z+" means first increment X, then (when MaxX is reached) increment z and start over with x from MinX.
36+
-- Other possible values: "x-z+", "x+z-", "x-z-", "z+x+", "z+x-", "z-x+", "z-x-".
37+
FillStrategy = "x+z+";
38+
39+
-- The biome to use for the gallery areas
40+
-- Only used when AreaTemplate is valid.
41+
-- If not set, no biome is forced (the world generator provides the default biome)
42+
Biome = "Jungle";
43+
};
44+
};
45+
46+
47+
48+
49+
50+
Config =
51+
{
52+
-- The prefix, under which all the plugin's commands are registered.
53+
-- All commands use this prefix, space, and an additional verb, like "list" or "claim"
54+
-- CommandPrefix = "/gal";
55+
56+
-- Allow building outside galleries for regular users
57+
-- When set to false, only users with the "gallery.admin.buildoutside" permission can build outside the galleries
58+
AllowBuildOutsideGalleries = false;
59+
60+
-- WorldEdit usage outside the galleries
61+
-- When set to false, only players with the "gallery.admin.worldeditoutside" permission can use WE outside the galleries
62+
AllowWEOutsideGalleries = false;
63+
};
64+
65+
66+
67+

tests/Gallery/FuzzCommands.lua

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ scenario
1111
name = "Fuzz all commands",
1212
redirect -- Redirect files / folders
1313
{
14-
["Galleries.cfg"] = "tests/Gallery/FuzzCommands.cfg",
15-
["Galleries.example.cfg"] = "tests/Gallery/FuzzCommands.example.cfg",
16-
["Galleries.sqlite"] = "tests/Gallery/FuzzCommands.sqlite",
14+
["Galleries.cfg"] = "FuzzCommands.cfg",
15+
["Galleries.example.cfg"] = "FuzzCommands.example.cfg",
16+
["Galleries.sqlite"] = "FuzzCommands.sqlite",
1717
},
1818
world -- Create a world
1919
{

0 commit comments

Comments
 (0)