forked from mapod4d/mapod4d_updater_c2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
updater.gd
157 lines (128 loc) · 3.28 KB
/
updater.gd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# tool
# class_name
# extends
extends Control
## A brief description of your script.
##
## A more detailed description of the script.
##
## @tutorial: http://the/tutorial1/url.com
## @tutorial(Tutorial2): http://the/tutorial2/url.com
# ----- signals
# ----- enums
# ----- constants
const M4DVERSION = {
'v1': 2,
'v2': 0,
'v3': 0,
'v4': 3,
'p': "a",
'godot': {
'v1': 4,
'v2': 0,
'v3': 0,
'v4': 1,
'p': "rc"
}
}
const M4DNAME = "updater"
const WK_PATH = 'wk'
const UPDATES_PATH = WK_PATH + '/updates'
const UPDATER = "updater"
const LAUNCHER = "mapod4d_launcher"
const UPDATER_PATH = WK_PATH + "/" + UPDATER
const MULTIVSVR = "https://sv001.mapod4d.it"
# ----- exported variables
# ----- public variables
# ----- private variables
var _base_path = null
var _exe_ext = ""
# ----- onready variables
@onready var timer = $Timer
# ----- optional built-in virtual _init method
# ----- built-in virtual _ready method
# Called when the node enters the scene tree for the first time.
func _ready():
match OS.get_name():
"Windows", "UWP":
_exe_ext = ".exe"
"macOS":
pass
"Linux", "FreeBSD", "NetBSD", "OpenBSD", "BSD":
pass
"Android":
pass
"iOS":
pass
"Web":
pass
var updater_ready = false
if OS.has_feature('editor'):
_base_path = 'test/wk'
if OS.has_feature('standalone'):
_base_path = OS.get_executable_path().get_base_dir()
# print(_base_path)
var slice_list = _base_path.split("/")
if slice_list.size() > 1:
# print(slice_list)
slice_list.remove_at(slice_list.size()-1)
_base_path = "/".join(slice_list)
# print(_base_path)
var args = OS.get_cmdline_user_args()
if args.size() > 0:
if args[0] == "-m4dupdate":
updater_ready = true
if updater_ready == true:
# print("UPDATE")
await get_tree().create_timer(1).timeout
_do_launcher_update()
else:
get_tree().quit()
else:
get_tree().quit()
# ----- remaining built-in virtual methods
func _enter_tree():
var args = OS.get_cmdline_user_args()
# print(args)
if "-m4dver" in args:
_write_version()
get_tree().quit()
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(_delta):
pass # Replace with function body.
# ----- public methods
# ----- private methods
func _write_version():
# print(M4DVERSION)
var json_data = JSON.stringify(M4DVERSION)
var base_dir = OS.get_executable_path().get_base_dir()
if OS.has_feature('editor'):
base_dir = "test"
var file_name = base_dir + "/" + M4DNAME + ".json"
# print(file_name)
var file = FileAccess.open(file_name, FileAccess.WRITE)
if file != null:
file.store_string(json_data)
file.flush()
func _do_launcher_update():
if _base_path != null:
var dir = DirAccess.open(_base_path)
if dir != null:
var dw_launcher_path = UPDATES_PATH + "/" + "launcher"
# print(dw_launcher_path)
if dir.file_exists(dw_launcher_path):
var from_file_name = dw_launcher_path
var to_file_name = LAUNCHER + _exe_ext
var _error = dir.rename(from_file_name, to_file_name)
if dir.file_exists(LAUNCHER + _exe_ext):
OS.create_process(
_base_path + "/" + LAUNCHER + _exe_ext,
[])
get_tree().quit()
# print(_error)
else:
print("%s not found" % _base_path)
timer.timeout.connect(_timer_timeout)
timer.start()
func _timer_timeout():
get_tree().quit()