Skip to content

Commit

Permalink
made newgroundsrequest return an error if http status code wasn't OK/…
Browse files Browse the repository at this point in the history
…result was null.

medals can now have non filename characters in them without crashing the medal import.
Open getting started page on first time enabling the plugin.
  • Loading branch information
jefvel committed May 13, 2024
1 parent 1df7be6 commit 5c4166e
Show file tree
Hide file tree
Showing 12 changed files with 81 additions and 30 deletions.
3 changes: 3 additions & 0 deletions addons/newgrounds/data/newgrounds_ids.gd
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,18 @@ class_name NewgroundsIds

enum ScoreboardId {
None = 0,
BestSoup = 13693,

}

enum MedalId {
None = 0,
FallFlaTaaa = 78662,

}

class MedalIdsToResource:
const medals = {
78662: "res://addons/newgrounds/imported_medals/fall_fla_taaa.tres",

}
1 change: 1 addition & 0 deletions addons/newgrounds/examples/ExampleSaveData.gd
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ extends Control
@onready var text_edit = $TextEdit

var test_text = ""

func _ready():
NGCloudSave.on_cloudsave_loaded.connect(_on_loaded)

Expand Down
11 changes: 11 additions & 0 deletions addons/newgrounds/examples/example_root_cloudsave_data.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
extends Node
var hehe = randi_range(0, 1500)
func _ready() -> void:
add_to_group("CloudSave")

func _cloud_save():
print("Saving data")
return {
"hehe": hehe
}
pass
2 changes: 1 addition & 1 deletion addons/newgrounds/examples/newgrounds_debug.gd
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ func on_saves_synced():
print("Saves have been synced")

func test():

pass

@onready var user_name = $Profile/UserName
Expand Down Expand Up @@ -92,6 +91,7 @@ func _on_get_slot_data_pressed():
func _slot_loaded(data):
print("Loaded data slot with data %s" % data)


func _on_clear_slot_pressed():
await NG.cloudsave_clear_slot(1)
get_tree().reload_current_scene()
Expand Down
3 changes: 0 additions & 3 deletions addons/newgrounds/examples/newgrounds_debug.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,6 @@ offset_left = 422.0
offset_top = 57.0
offset_right = 688.0
offset_bottom = 422.0
scoreboard_id = 13398

[node name="ExampleMedalList" parent="." instance=ExtResource("4_lqp0p")]
offset_left = 885.0
Expand All @@ -194,5 +193,3 @@ script = ExtResource("7_tu0rn")
[connection signal="pressed" from="Get Slot Data" to="." method="_on_get_slot_data_pressed"]
[connection signal="pressed" from="Clear Slot" to="." method="_on_clear_slot_pressed"]
[connection signal="pressed" from="Profile" to="." method="_on_profile_pressed"]
[connection signal="on_signed_in" from="NewgroundsSessionWatcher" to="." method="_on_newgrounds_session_watcher_on_signed_in"]
[connection signal="on_signed_out" from="NewgroundsSessionWatcher" to="." method="_on_newgrounds_session_watcher_on_signed_out"]
7 changes: 3 additions & 4 deletions addons/newgrounds/newgrounds_medal_unlocker.gd
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,18 @@ class_name NewgroundsMedalUnlocker extends Node

var unlocked: bool = false;

signal on_unlock(medal:MedalResource);
signal on_unlocked(medal:MedalResource);

func unlock():
if unlocked:
return
NG.medal_unlock(medal_id)
pass

func _ready():
func _ready() -> void:
var medal = NG.get_medal_resource(medal_id)
if medal and medal.unlocked:
unlocked = medal.unlocked;

NG.on_medals_loaded.connect(_medals_loaded);
NG.on_medal_unlocked.connect(_on_medal_unlocked);

Expand All @@ -31,6 +30,6 @@ func _medals_loaded(m: Array[MedalResource]):

func _on_medal_unlocked(id:int):
if id == medal_id:
on_unlock.emit(NG.get_medal_resource(id))
on_unlocked.emit(NG.get_medal_resource(id))
unlocked = true;

2 changes: 1 addition & 1 deletion addons/newgrounds/plugin.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
name="Newgrounds.io (Godot 4)"
description="Cross platform Newgrounds Medals, Scoreboards, and Cloud saves"
author="jefvel"
version="1.0.1"
version="1.0.2"
script="plugin.gd"
36 changes: 30 additions & 6 deletions addons/newgrounds/plugin.gd
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,18 @@ func _enter_tree():
add_project_setting(C.IMPORTED_MEDALS_LOCATION_PROPERTY, C.MEDALS_PATH, TYPE_STRING, PROPERTY_HINT_DIR, "", false)

add_tool_menu_item(C.IMPORT_ACTION_NAME, _import_newgrounds)
add_tool_menu_item(C.OPEN_NEWGROUNDS_HELP, _open_docs)

ProjectSettings.set_setting("global_group/CloudSave", "Newgrounds Cloudsave")

if !FileAccess.file_exists(C.IDS_FILE_PATH):
_write_template_file([], [])

if !ProjectSettings.has_setting("newgrounds.io/opened_getting_started_page"):
ProjectSettings.set_setting("newgrounds.io/opened_getting_started_page", true)
ProjectSettings.set_as_internal("newgrounds.io/opened_getting_started_page", true)
_open_docs()

if !ProjectSettings.has_setting("autoload/%s" % C.NEWGROUNDS_AUTOLOAD_NAME):
add_autoload_singleton(C.NEWGROUNDS_AUTOLOAD_NAME, C.NEWGROUNDS_AUTOLOAD_NODE)
if !ProjectSettings.has_setting("autoload/%s" % C.CLOUDSAVE_AUTOLOAD_NAME):
Expand All @@ -21,10 +29,15 @@ func _enter_tree():
ProjectSettings.save()


func _open_docs():
OS.shell_open("https://github.com/jefvel/newgrounds-godot-4/wiki/Quick-Start")
pass

func _exit_tree():
remove_autoload_singleton(C.NEWGROUNDS_AUTOLOAD_NAME)
remove_autoload_singleton(C.CLOUDSAVE_AUTOLOAD_NAME)
remove_tool_menu_item(C.IMPORT_ACTION_NAME)
remove_tool_menu_item(C.OPEN_NEWGROUNDS_HELP)
pass

var loadedMedals = false;
Expand Down Expand Up @@ -54,7 +67,7 @@ func _import_newgrounds():
add_child(e);
e.init(app_id, aes_key);
e.create("ScoreBoard.getBoards", null)
e.on_success.connect(scoreboards_get)
e.on_response.connect(scoreboards_get)
boardRequest = e;

if medalRequest and is_instance_valid(medalRequest):
Expand All @@ -64,26 +77,37 @@ func _import_newgrounds():
add_child(e);
e.init(app_id, aes_key);
e.create("Medal.getList", null)
e.on_success.connect(medals_get)
e.on_response.connect(medals_get)
medalRequest = e;
pass

func scoreboards_get(b):
func scoreboards_get(res:NewgroundsResponse):
if (res.error):
printerr("Could not get scoreboards: %s" % res.error_message)
return
var b = res.data;
scoreboardList = b.scoreboards;
loadedScoreboards = true
_write_ids()

const NewgroundsImage = preload("res://addons/newgrounds/scripts/newgrounds_image.gd")

func medals_get(medals):
func medals_get(res: NewgroundsResponse):
if (res.error):
printerr("Could not get medals: %s" % res.error_message)
return
var data = res.data;
var medal_dir = ProjectSettings.get_setting(C.IMPORTED_MEDALS_LOCATION_PROPERTY)
if !FileAccess.file_exists(medal_dir):
DirAccess.make_dir_absolute(medal_dir)

var used_names = {}
const medal_names = preload("res://addons/newgrounds/data/newgrounds_ids.gd").MedalIdsToResource.medals
for m in medals.medals:
var medal_name = m.name.to_snake_case();
const not_allowed = [':',"'",'/,','\\','?','*','"','|', '%','<','>']
for m in data.medals:
var medal_name: String = m.name.to_snake_case();
for c in not_allowed:
medal_name = medal_name.replace(c, "")
if (used_names.has(medal_name)):
used_names[medal_name] = used_names[medal_name] + 1
medal_name += "_%s" % used_names[medal_name]
Expand Down
8 changes: 4 additions & 4 deletions addons/newgrounds/scripts/newgrounds.gd
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,6 @@ var signed_in : bool = false;
## Emitted when starting session/signing in/signing out
signal on_session_change(session: NewgroundsSession)

## Emitted when savedata has been uploaded/downloaded,
## and offline scoreboards & medals have been synced
signal on_saves_synced()

signal on_signed_in();
signal on_signed_out();

Expand All @@ -37,6 +33,10 @@ signal on_medals_loaded(medals:Array[MedalResource]);
signal on_medal_unlocked(medal_id:int);
signal on_medal_score_get(score:int);

## Emitted when savedata has been uploaded/downloaded,
## and offline scoreboards & medals have been synced
signal on_saves_synced()

var save_slots:Dictionary = {};
signal on_cloudsave_slots_loaded();
signal on_cloudsave_slot_loaded(slot: int);
Expand Down
1 change: 1 addition & 0 deletions addons/newgrounds/scripts/newgrounds_consts.gd
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const SCOREBOARD_HINT_TEXT_PROPERTY = "newgrounds.io/scoreboard_hint_string";
const MEDAL_HINT_TEXT_PROPERTY = "newgrounds.io/medal_hint_string";

const IMPORT_ACTION_NAME = "Import Newgrounds Medals & Scoreboards"
const OPEN_NEWGROUNDS_HELP = "Open Newgrounds Addon Help Page"
const NEWGROUNDS_IDS_TEMPLATE = "res://addons/newgrounds/data/newgrounds_ids_template.txt"
const IDS_FILE_PATH = "res://addons/newgrounds/data/newgrounds_ids.gd"
const MEDALS_PATH = "res://addons/newgrounds/imported_medals"
26 changes: 16 additions & 10 deletions addons/newgrounds/scripts/newgrounds_request.gd
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ func create(component, parameters, result_field = "", encrypt = true) -> HTTPReq

var input_parameters = {
"app_id": app_id,
# "debug": true,
"call": call,
# "debug": true,
}

if (session.id):
Expand Down Expand Up @@ -114,27 +114,24 @@ func custom_request(url: String):
_custom_call = true;
request(url)

func _request_completed(result, response_code, headers, body):
func _request_completed(result: int, response_code: int, headers: PackedStringArray, body: PackedByteArray):
queue_free()


pending = false;
var resp = NewgroundsResponse.new()


if result != RESULT_SUCCESS:
resp.error = ERR_FAILED_REQUEST
resp.error_message = "Could not fulfill request."
resp.data = null;
on_response.emit(resp);
on_error.emit(resp.error_message);
return
pass

var body_string = body.get_string_from_utf8()

if _custom_call:
if response_code == 200:
if response_code >= 200 and response_code < 300:
resp.data = body_string
on_success.emit(body_string)
on_response.emit(resp)
Expand All @@ -144,12 +141,22 @@ func _request_completed(result, response_code, headers, body):
on_response.emit(resp)
on_error.emit(body_string);
return




var res = JSON.parse_string(body_string)
# res null, response_code 405 not allowed when under maintenance
if res == null and response_code < 200 or response_code > 299:
resp.error = ERR_FAILED_REQUEST
resp.error_message = "Request error code %s" % response_code
if response_code == 405:
resp.error_message += ". Newgrounds might be under maintenance."
resp.data = null;
on_response.emit(resp);
on_error.emit(resp.error_message)
return

if !res.success:
on_error.emit(res.error)

resp.error = FAILED
resp.error_message = res.error
on_response.emit(resp)
Expand All @@ -158,7 +165,6 @@ func _request_completed(result, response_code, headers, body):
var d = res.result.data;
if !d.success:
on_error.emit(d.error)

resp.error = d.error.code if d.error.code != 0 else ERR_FAILED_REQUEST
resp.error_message = d.error.message
on_response.emit(resp)
Expand Down
11 changes: 10 additions & 1 deletion project.godot
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,26 @@ config_version=5

config/name="newgrounds.io"
run/main_scene="res://example_scene.tscn"
config/features=PackedStringArray("4.2", "GL Compatibility")
config/features=PackedStringArray("4.3", "GL Compatibility")

[autoload]

NG="*res://addons/newgrounds/scripts/newgrounds.tscn"
NGCloudSave="*res://addons/newgrounds/scripts/newgrounds_cloudsave.gd"
SaveData="*res://addons/newgrounds/examples/example_root_cloudsave_data.gd"

[editor_plugins]

enabled=PackedStringArray("res://addons/newgrounds/plugin.cfg")

[global_group]

CloudSave="Newgrounds Cloudsave"

[newgrounds.io]

opened_getting_started_page=true

[rendering]

renderer/rendering_method="gl_compatibility"
Expand Down

0 comments on commit 5c4166e

Please sign in to comment.