Skip to content

Commit 4b56acb

Browse files
authored
Merge pull request #4 from Ward727a/v2
V2 merge
2 parents 7c1c9df + bbb4d2e commit 4b56acb

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+3409
-3009
lines changed
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
@tool
2+
extends _dt_common
3+
class_name _dt_backup
4+
5+
## Class that manage all the backup system
6+
7+
static var _INSTANCE: _dt_backup
8+
9+
static func delete():
10+
_INSTANCE = null
11+
12+
13+
var _folder: String
14+
var _max: int
15+
var _suffix: String
16+
17+
var _index: int = 0
18+
var _most_recent_path: String
19+
20+
static func get_instance() -> _dt_backup:
21+
22+
if !_INSTANCE || _dt_plugin.get_instance().get_dev_reset_instance() == "true":
23+
_INSTANCE = _dt_backup.new()
24+
25+
_INSTANCE.load_var()
26+
return _INSTANCE
27+
28+
# Init
29+
30+
func load_var():
31+
32+
var config = _dt_plugin.get_instance()
33+
34+
_folder = config.get_backup_path()
35+
_max = int(config.get_backup_max())
36+
_suffix = config.get_backup_suffix()
37+
38+
# Backend
39+
40+
func get_path():
41+
return _folder
42+
43+
func get_max():
44+
return _max
45+
46+
func make(path: String):
47+
48+
if _dt_plugin.get_instance().get_dev_stop_backup() == "true":
49+
WARNING("Can't make backup as the dev config 'stop_backup' is on true!")
50+
return
51+
52+
if !_check_path(path):
53+
ASSERT_ERROR("Error in the path, can't make a backup!")
54+
DEBUG(str("Path: ", path))
55+
return
56+
57+
var data = load(path)
58+
59+
var file_name = _get_file_name(path)
60+
61+
if file_name.is_empty():
62+
DEBUG(str("packedData: ", data, " file_name: ", file_name, " path: ",path))
63+
ASSERT_ERROR("File name is empty, cancelling backup")
64+
return
65+
66+
var index = _get_index(path)
67+
68+
var backup_file_path = str(_folder, "/", file_name, _suffix, index, _dt_plugin.get_instance().get_file_ext())
69+
70+
if !_has_dir():
71+
return
72+
73+
var err = ResourceSaver.save(data, backup_file_path)
74+
75+
if err != OK:
76+
DEBUG(str("packedData: ", data, " file_name: ", file_name, " file_path: ", backup_file_path, " path: ",path))
77+
ASSERT_ERROR("Couldn't create backup file!")
78+
else:
79+
print_rich("[color=lightgreen][DataTable] Created a backup for '",file_name,"' collection, path: ",backup_file_path)
80+
81+
func link(timer: Timer, path: String):
82+
83+
DEBUG("Link timer to backup system")
84+
85+
if timer.timeout.get_connections().has(make):
86+
DEBUG("Disconnect already connected func")
87+
timer.timeout.disconnect(make)
88+
89+
timer.timeout.connect(make.bind(path))
90+
timer.start()
91+
92+
93+
func _get_file_name(path: String)->String:
94+
95+
var file_name
96+
97+
if path.ends_with(".tableCollection.res"):
98+
var path_splitted = path.split(".tableCollection.res")
99+
path_splitted = path_splitted[0].split("/")
100+
file_name = path_splitted[path_splitted.size()-1]
101+
else:
102+
var path_splitted = path.split(".res")
103+
path_splitted = path_splitted[0].split("/")
104+
file_name = path_splitted[path_splitted.size()-1]
105+
106+
return file_name
107+
108+
func _check_path(path: String)->bool:
109+
110+
if path == null || path.is_empty():
111+
ASSERT_ERROR("Can't make a backup of an empty path")
112+
return false
113+
114+
if path.contains(_folder):
115+
ASSERT_ERROR("Can't make a backup of a backup file")
116+
return false
117+
118+
return true
119+
120+
func _get_index(path: String)->int:
121+
122+
_index += 1
123+
124+
if _index > _max:
125+
_index = 1
126+
return 1
127+
128+
if _most_recent_path != path:
129+
_index = 1
130+
_most_recent_path = path
131+
return 1
132+
133+
return (_index)
134+
135+
func _has_dir()->bool:
136+
var backup_dir = DirAccess.open("res://")
137+
138+
var dir_path = _folder.replace('res://', "")
139+
140+
if !backup_dir.dir_exists(dir_path):
141+
var make_err = backup_dir.make_dir(dir_path)
142+
143+
if make_err != OK:
144+
ASSERT_ERROR(str("The plugin couldn't create the '",dir_path,"' folder in the path: 'res://', the back-up system can't work without it!"))
145+
return false
146+
147+
return true

0 commit comments

Comments
 (0)