Skip to content

Commit

Permalink
Version 1.0 feature-ready
Browse files Browse the repository at this point in the history
  • Loading branch information
Martomate committed Aug 23, 2019
1 parent aa2c935 commit 055a40c
Show file tree
Hide file tree
Showing 13 changed files with 154 additions and 117 deletions.
17 changes: 14 additions & 3 deletions GameVersion.gd
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,22 @@ var id: String
var name: String
var release_date: String
var download_url: String
var download_size: int
var file_to_run: String

func _init(id: String, name: String, release_date: String, download_url: String, download_size: int):
func _init(id: String, name: String, release_date: String, download_url: String, file_to_run: String):
self.id = id
self.name = name
self.release_date = release_date
self.download_url = download_url
self.download_size = download_size
self.file_to_run = file_to_run

func asDict():
var dict = {}

dict["id"] = id
dict["name"] = name
dict["release_date"] = release_date
dict["url"] = download_url
dict["file_to_run"] = file_to_run

return dict
Binary file removed Hexacraft Launcher.exe
Binary file not shown.
Binary file removed Hexacraft Launcher.pck
Binary file not shown.
Binary file removed Hexacraft Launcher_original.exe
Binary file not shown.
24 changes: 0 additions & 24 deletions VersionList.gd

This file was deleted.

4 changes: 0 additions & 4 deletions VersionListItem.gd

This file was deleted.

19 changes: 0 additions & 19 deletions VersionListItem.tscn

This file was deleted.

30 changes: 0 additions & 30 deletions Versions.gd

This file was deleted.

131 changes: 106 additions & 25 deletions Window.gd
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
extends Control

const MARTO_LIB_VERSION = "1.0"
var libFilePath

var currentVersion = "latest-version"

var launcher_data_path: String = "user://launcher_data.json"

var versions: Dictionary = {}

func _ready():
libFilePath = str(OS.get_user_data_dir(), "/libs/martolib_", MARTO_LIB_VERSION, ".jar")

var file = File.new()
if file.file_exists(launcher_data_path):
file.open(launcher_data_path, File.READ)
var json = JSON.parse(file.get_as_text())
loadVersionsDict(json["versions"])
var fileContents = file.get_as_text()
var json = JSON.parse(fileContents)
currentVersion = json.result["current-version"]
loadVersionsDict(json.result["versions"])
else:
$CenterPlay/MarginPlay/Play.hide()

Expand All @@ -20,33 +27,59 @@ func _ready():
func saveLauncherData():
var file = File.new()
file.open("user://launcher_data.json", File.WRITE)
var jsonStr = JSON.print({"versions": versions.values()})
var versionList = []
for v in versions.keys():
if v != "latest-version":
versionList.append(versions[v].asDict());
var jsonStr = JSON.print({
"current-version": currentVersion,
"versions": versionList
})
file.store_string(jsonStr)

func _on_Play_pressed():
saveLauncherData()
if ensureVersionIsDownloaded(currentVersion):
startGame(currentVersion)

func startGame(versionID):
print("success!")
var version = versions[versionID]
var dir = str(OS.get_user_data_dir(), "/versions/", version.name)

var executable = '/bin/sh'
var flag = '-c'
if OS.get_name() == 'Windows':
executable = 'CMD.exe'
flag = '/C'

OS.execute(executable, [flag, str('cd ', dir, ' && java -jar \"', version.file_to_run, "\"")], false)
get_tree().quit()

func ensureVersionIsDownloaded(versionID):
var version = versions[versionID]
var dir = Directory.new()
print(dir.open("user://"))
dir.open("user://")

var base = "user://versions/" + version.name

if !dir.dir_exists(base):
dir.make_dir(base)
var error = dir.make_dir_recursive(base)
if error != OK:
print(str("Could not make dir. Error: ", error))

$VersionDownloader.download_file = "user://tmp.zip"

$CenterPlay/MarginPlay/Progress.value = 0

$VersionDownloader.download_file = "user://temp/tmp.zip"
$VersionDownloader.request(version.download_url)
$CenterPlay/MarginPlay/Play.hide()
$CenterPlay/MarginPlay/Progress.show()
$CenterPlay/MarginPlay/Progress.max_value = version.download_size
$DownloadProgress.start()
print("Requesting " + version.download_url)

var requestStatus = $VersionDownloader.request(version.download_url)
if requestStatus != OK:
print("Request failed with status: " + requestStatus)
else:
print("Requesting " + version.download_url)
return false
else:
return true
Expand All @@ -61,17 +94,18 @@ func _on_Settings_pressed():

func _on_VersionListRequest_request_completed(result, response_code, headers, body):
if result == HTTPRequest.RESULT_SUCCESS:
loadVersionsDict(JSON.parse(body.get_string_from_utf8()))
loadVersionsDict(JSON.parse(body.get_string_from_utf8()).result)
$CenterPlay/MarginPlay/Play.show()
saveLauncherData()
else:
print(str("Could not download latest version list. Result was: ", result))

func loadVersionsDict(p: JSONParseResult):
if typeof(p.result) == TYPE_ARRAY:
for v in p.result:
var id = v["id"]
versions[id] = GameVersion.new(id, v["name"], v["release_date"], v["url"], v["fileSize"])
print("Could not download latest version list. Result was: ", result, ", response_code", response_code)

func loadVersionsDict(p: Array):
if typeof(p) == TYPE_ARRAY:
versions.clear()
for v in p:
var version = GameVersion.new(v["id"], v["name"], v["release_date"], v["url"], v["file_to_run"])
versions[version.id] = version

var sortedVersions = Array(versions.values())
sortedVersions.sort_custom(GameVersionSorter, "sort")
Expand All @@ -80,26 +114,73 @@ func loadVersionsDict(p: JSONParseResult):
versionSelector.clear()

versions["latest-version"] = sortedVersions[0]
versionSelector.add_item(str("Latest version (", sortedVersions[0].name, ")"))
versionSelector.add_item(str("Latest (", sortedVersions[0].name, ")"))
versionSelector.set_item_metadata(0, "latest-version")
if (currentVersion == "latest-version"):
versionSelector.select(0)

for v in sortedVersions:
versionSelector.add_item(v.name)
versionSelector.set_item_metadata(versionSelector.get_item_count() - 1, v.id)

versionSelector.select(0)
if (currentVersion == v.id):
versionSelector.select(versionSelector.get_item_count() - 1)

else:
print("unexpected results")
print("ERROR: The retrieved game versions were not in an array")

class GameVersionSorter:
static func sort(a, b):
return a.release_date > b.release_date
return a.name > b.name


func _on_VersionDownloader_request_completed(result, response_code, headers, body):
startGame(currentVersion)
if result != HTTPRequest.RESULT_SUCCESS:
print("Failed to downloadload version. Result: ", result, ", response_code: ", response_code)
$DownloadProgress.stop()
updateVersionDownloadProgress()

ensureMartoLibIsDownloaded()

func ensureMartoLibIsDownloaded():
var libDir = Directory.new()
libDir.open("user://")
if libDir.file_exists(libFilePath):
unzipAndStartGame()
else:
print("Downloading MartoLib")
libDir.make_dir("libs")
$MartoLibDownloader.download_file = libFilePath
$MartoLibDownloader.request("https://www.martomate.com/api/libs/martolib")

func _on_DownloadProgress_timeout():
$CenterPlay/MarginPlay/Progress.value = $VersionDownloader.get_downloaded_bytes()
updateVersionDownloadProgress()

func updateVersionDownloadProgress():
var maxBytes = $VersionDownloader.get_body_size()
if maxBytes != -1:
var downloadedBytes = $VersionDownloader.get_downloaded_bytes()
$CenterPlay/MarginPlay/Progress.max_value = maxBytes
$CenterPlay/MarginPlay/Progress.value = downloadedBytes


func _on_MartoLibDownloader_request_completed(result, response_code, headers, body):
if result != HTTPRequest.RESULT_SUCCESS:
print("Failed to downloadload MartoLib. Result: ", result, ", response_code: ", response_code)
unzipAndStartGame()

func unzipAndStartGame():
unzipDownloadedVersionFile()

$CenterPlay/MarginPlay/Play.show()
$CenterPlay/MarginPlay/Progress.hide()
startGame(currentVersion)

func unzipDownloadedVersionFile():
var filePath = str(OS.get_user_data_dir(), "/tmp.zip")
var output = []
OS.execute("java", ["-jar", libFilePath, "unzip", filePath, str(OS.get_user_data_dir(), "/versions/", versions[currentVersion].name)], true, output)
if !output.empty() && output[0] != "":
print("Failed to unzip version file. Error: ", output)

Directory.new().remove(filePath)

15 changes: 9 additions & 6 deletions Window.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ outline_size = 2
outline_color = Color( 0, 0, 0, 1 )
font_data = ExtResource( 4 )

[sub_resource type="Theme" id=3]
[sub_resource type="Theme" id=2]
Button/colors/font_color = Color( 0.878431, 0.878431, 0.878431, 1 )
Button/colors/font_color_disabled = Color( 0.9, 0.9, 0.9, 0.2 )
Button/colors/font_color_hover = Color( 0.941176, 0.941176, 0.941176, 1 )
Expand All @@ -25,7 +25,7 @@ Button/styles/hover = ExtResource( 5 )
Button/styles/normal = ExtResource( 5 )
Button/styles/pressed = ExtResource( 5 )

[sub_resource type="DynamicFont" id=2]
[sub_resource type="DynamicFont" id=3]
size = 32
font_data = ExtResource( 4 )

Expand Down Expand Up @@ -71,8 +71,8 @@ margin_right = 128.0
margin_bottom = 64.0
rect_min_size = Vector2( 128, 64 )
size_flags_horizontal = 0
theme = SubResource( 3 )
custom_fonts/font = SubResource( 2 )
theme = SubResource( 2 )
custom_fonts/font = SubResource( 3 )
text = "Play"

[node name="Progress" type="ProgressBar" parent="CenterPlay/MarginPlay"]
Expand All @@ -97,7 +97,7 @@ margin_right = 712.0
margin_bottom = 29.0

[node name="Versions" type="OptionButton" parent="MarginContainer/HBoxContainer"]
margin_right = 215.0
margin_right = 242.0
margin_bottom = 29.0
size_flags_horizontal = 0
text = "Latest release (0.8)"
Expand All @@ -106,7 +106,7 @@ items = [ "Latest release (0.8)", null, false, -1, null ]
selected = 0

[node name="Filler" type="Label" parent="MarginContainer/HBoxContainer"]
margin_left = 219.0
margin_left = 246.0
margin_top = 3.0
margin_right = 604.0
margin_bottom = 26.0
Expand All @@ -123,13 +123,16 @@ flat = true
[node name="VersionListRequest" type="HTTPRequest" parent="."]

[node name="VersionDownloader" type="HTTPRequest" parent="."]
use_threads = true

[node name="DownloadProgress" type="Timer" parent="."]
wait_time = 0.1

[node name="MartoLibDownloader" type="HTTPRequest" parent="."]
[connection signal="pressed" from="CenterPlay/MarginPlay/Play" to="." method="_on_Play_pressed"]
[connection signal="item_selected" from="MarginContainer/HBoxContainer/Versions" to="." method="_on_Versions_item_selected"]
[connection signal="pressed" from="MarginContainer/HBoxContainer/Settings" to="." method="_on_Settings_pressed"]
[connection signal="request_completed" from="VersionListRequest" to="." method="_on_VersionListRequest_request_completed"]
[connection signal="request_completed" from="VersionDownloader" to="." method="_on_VersionDownloader_request_completed"]
[connection signal="timeout" from="DownloadProgress" to="." method="_on_DownloadProgress_timeout"]
[connection signal="request_completed" from="MartoLibDownloader" to="." method="_on_MartoLibDownloader_request_completed"]
1 change: 0 additions & 1 deletion default_env.tres
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,3 @@
[resource]
background_mode = 2
background_sky = SubResource( 1 )

2 changes: 1 addition & 1 deletion export_presets.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ custom_features=""
export_filter="all_resources"
include_filter=""
exclude_filter=""
export_path=""
export_path="/home/martin/git/HexacraftLauncher/HexacraftLauncherLinux.x86_64"
patch_list=PoolStringArray( )
script_export_mode=1
script_encryption_key=""
Expand Down
Loading

0 comments on commit 055a40c

Please sign in to comment.