1
+ -- advtrains compat
2
+ -- saves a snapshot of the advtrains-data with the `/mapsync_save_advtrains` command
3
+ -- loads the snapshot if available on startup, defaults to the worldfolder if no snapshot found
4
+
5
+ -- sanity checks
6
+ assert (type (advtrains .load_version_4 ) == " function" )
7
+ assert (type (advtrains .ndb .save_callback ) == " function" )
8
+ assert (type (advtrains .ndb .load_callback ) == " function" )
9
+ assert (type (advtrains .read_component ) == " function" )
10
+ assert (type (advtrains .save_component ) == " function" )
11
+ assert (type (advtrains .save ) == " function" )
12
+ assert (type (serialize_lib .load_atomic ) == " function" )
13
+ assert (type (serialize_lib .save_atomic_multiple ) == " function" )
14
+ assert (type (advtrains .fpath ) == " string" )
15
+
16
+ -- local old_advtrains_read_component = advtrains.read_component
17
+ function advtrains .read_component (name )
18
+ assert (name == " version" )
19
+ -- currently supported version
20
+ return 4
21
+ end
22
+
23
+ -- local old_advtrains_save_component = advtrains.save_component
24
+ function advtrains .save_component ()
25
+ -- no-op
26
+ end
27
+
28
+ -- load from data- or world-path
29
+ local old_load_atomic = serialize_lib .load_atomic
30
+ function serialize_lib .load_atomic (filename , load_callback )
31
+ local relpath = string.sub (filename , # advtrains .fpath + 2 )
32
+ local timestamp_storage_key = " advtrains_timestamp_" .. relpath
33
+
34
+ -- check world- and snapshot-timestamp
35
+ local snapshot_timestamp = mapsync .load_data (" advtrains_timestamp" ) or 0
36
+ local world_timestamp = mapsync .storage :get_int (timestamp_storage_key )
37
+ -- snapshot exists and has a different timestamp
38
+ local load_snapshot = snapshot_timestamp ~= world_timestamp
39
+
40
+ local data_file = mapsync .get_data_file (" advtrains_" .. relpath )
41
+ if data_file and load_snapshot then
42
+ -- apply snapshot timestamp to world
43
+ mapsync .storage :set_int (timestamp_storage_key , snapshot_timestamp )
44
+ -- data-snapshot available, load it
45
+ local data_path = mapsync .get_data_file_path (" advtrains_" .. relpath )
46
+ minetest .log (" action" , " [mapsync] loading advtrains data from '" .. data_path ..
47
+ " ' snapshot_timestamp: " .. snapshot_timestamp ..
48
+ " world_timestamp: " .. world_timestamp )
49
+ return old_load_atomic (data_path , load_callback )
50
+ else
51
+ -- no data snapshot available, load default from world-folder
52
+ return old_load_atomic (filename , load_callback )
53
+ end
54
+ end
55
+
56
+ local advtrains_parts = {" atlatc.ls" , " interlocking.ls" , " core.ls" , " lines.ls" , " ndb4.ls" }
57
+
58
+ local function copy_advtrains_files ()
59
+ if not mapsync .get_data_backend () then
60
+ return false , " no data-backend configured"
61
+ end
62
+
63
+ -- copy files to data-directory
64
+ local count = 0
65
+ for _ , part in ipairs (advtrains_parts ) do
66
+ local path = advtrains .fpath .. " _" .. part
67
+ local src = io.open (path , " rb" )
68
+ if not src then
69
+ return false , " open failed for '" .. path .. " '"
70
+ end
71
+
72
+ local dst = mapsync .get_data_file (" advtrains_" .. part , " wb" )
73
+ local data = src :read (" *all" )
74
+ count = count + # data
75
+ dst :write (data )
76
+ dst :close ()
77
+ src :close ()
78
+ end
79
+ -- write timestamp
80
+ mapsync .save_data (" advtrains_timestamp" , os.time ())
81
+
82
+ minetest .log (" action" , " [mapsync] saved " .. count .. " bytes of advtrains data" )
83
+ return count
84
+ end
85
+
86
+ local old_advtrains_save = advtrains .save
87
+ function advtrains .save (remove_players_from_wagons )
88
+ -- save advtrains state
89
+ old_advtrains_save (remove_players_from_wagons )
90
+
91
+ if mapsync .autosave then
92
+ -- save to data-directory
93
+ copy_advtrains_files ()
94
+ end
95
+ end
96
+
97
+ minetest .register_chatcommand (" mapsync_save_advtrains" , {
98
+ privs = { mapsync = true },
99
+ func = function ()
100
+ -- save advtrains data first
101
+ old_advtrains_save ()
102
+
103
+ local count , err = copy_advtrains_files ()
104
+ if err then
105
+ return false , err
106
+ end
107
+
108
+ return true , " saved " .. count .. " bytes of advtrains data"
109
+ end
110
+ })
0 commit comments