Skip to content

Commit

Permalink
small optimizations and fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Tarasa24 committed Nov 3, 2020
1 parent 52d1a40 commit 3907783
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 15 deletions.
28 changes: 19 additions & 9 deletions SCS_RPC2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,12 @@
#include "custom_types.h"
#include "handlers.h"

telemetry_state telemetry;
telemetry_state telemetry{};

SCSAPI_RESULT scs_telemetry_init(const scs_u32_t version, const scs_telemetry_init_params_t* const params)
{
memset(&telemetry, 0, sizeof(telemetry));

const scs_telemetry_init_params_v101_t* const version_params = static_cast<const scs_telemetry_init_params_v101_t*>(params);
game_log = version_params->common.log;

Expand All @@ -60,14 +62,22 @@ SCSAPI_RESULT scs_telemetry_init(const scs_u32_t version, const scs_telemetry_in

telemetry.cities = construct_tree(&telemetry);

version_params->register_for_event(SCS_TELEMETRY_EVENT_frame_start, handle_frame_start, &telemetry);
version_params->register_for_event(SCS_TELEMETRY_EVENT_paused, handle_pause_start, &telemetry);
version_params->register_for_event(SCS_TELEMETRY_EVENT_started, handle_pause_start, &telemetry);
version_params->register_for_channel(SCS_TELEMETRY_TRUCK_CHANNEL_world_placement, SCS_U32_NIL, SCS_VALUE_TYPE_dvector, SCS_TELEMETRY_CHANNEL_FLAG_no_value, handle_position, &telemetry);
version_params->register_for_channel(SCS_TELEMETRY_TRUCK_CHANNEL_speed, SCS_U32_NIL, SCS_VALUE_TYPE_float, SCS_TELEMETRY_CHANNEL_FLAG_no_value, handle_speed, &telemetry);
version_params->register_for_channel(SCS_TELEMETRY_TRUCK_CHANNEL_navigation_distance, SCS_U32_NIL, SCS_VALUE_TYPE_float, SCS_TELEMETRY_CHANNEL_FLAG_no_value, handle_distance, &telemetry);
version_params->register_for_channel(SCS_TELEMETRY_TRAILER_CHANNEL_connected, SCS_U32_NIL, SCS_VALUE_TYPE_bool, SCS_TELEMETRY_CHANNEL_FLAG_no_value, handle_trailer_connect, &telemetry);
version_params->register_for_event(SCS_TELEMETRY_EVENT_configuration, handle_configuration, &telemetry);
bool registered = (
version_params->register_for_event(SCS_TELEMETRY_EVENT_frame_start, handle_frame_start, &telemetry) == SCS_RESULT_ok &&
version_params->register_for_event(SCS_TELEMETRY_EVENT_paused, handle_pause_start, &telemetry) == SCS_RESULT_ok &&
version_params->register_for_event(SCS_TELEMETRY_EVENT_started, handle_pause_start, &telemetry) == SCS_RESULT_ok &&
version_params->register_for_channel(SCS_TELEMETRY_TRUCK_CHANNEL_world_placement, SCS_U32_NIL, SCS_VALUE_TYPE_dvector, SCS_TELEMETRY_CHANNEL_FLAG_no_value, handle_position, &telemetry) == SCS_RESULT_ok &&
version_params->register_for_channel(SCS_TELEMETRY_TRUCK_CHANNEL_speed, SCS_U32_NIL, SCS_VALUE_TYPE_float, SCS_TELEMETRY_CHANNEL_FLAG_no_value, handle_speed, &telemetry) == SCS_RESULT_ok &&
version_params->register_for_channel(SCS_TELEMETRY_TRUCK_CHANNEL_navigation_distance, SCS_U32_NIL, SCS_VALUE_TYPE_float, SCS_TELEMETRY_CHANNEL_FLAG_no_value, handle_distance, &telemetry) == SCS_RESULT_ok &&
version_params->register_for_channel(SCS_TELEMETRY_TRAILER_CHANNEL_connected, SCS_U32_NIL, SCS_VALUE_TYPE_bool, SCS_TELEMETRY_CHANNEL_FLAG_no_value, handle_trailer_connect, &telemetry) == SCS_RESULT_ok &&
version_params->register_for_event(SCS_TELEMETRY_EVENT_configuration, handle_configuration, &telemetry) == SCS_RESULT_ok
);

if (!registered)
{
log_ingame("Unable to register callbacks");
return SCS_RESULT_generic_error;
}

telemetry.start_timestamp = seconds_since_epoch();

Expand Down
2 changes: 1 addition & 1 deletion custom_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ typedef kdtree<double, 2> tree2d;
std::unique_ptr<discord::Core> discordCore;

long start_timestamp;
tree2d* cities{};
std::unique_ptr<tree2d> cities{};

bool paused = true;
int game;
Expand Down
12 changes: 9 additions & 3 deletions handlers.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ SCSAPI_VOID handle_frame_start(const scs_event_t UNUSED(event), const void* cons
if (telemetry->game == ETS2) small_text << telemetry->truck.brand << " " << telemetry->truck.name <<" (" << (int)(telemetry->speed * 3.6) << " km/h)";
else if (telemetry->game == ATS) small_text << telemetry->truck.brand << " " << telemetry->truck.name << " (" << (int)(telemetry->speed * 2.237) << " mph)";

set_activity(telemetry->paused ? "Paused" : on_job ? details.str() : u8"🚚 Free roaming 💨",
set_activity(!telemetry->position.avalible || telemetry->paused ? "Paused" : on_job ? details.str() : u8"🚚 Free roaming 💨",
!telemetry->position.avalible || telemetry->paused ? "" : state.str(),
small_image.str(), small_text.str(), telemetry);

Expand Down Expand Up @@ -88,8 +88,11 @@ SCSAPI_VOID handle_configuration(const scs_event_t event, const void* const even
std::string id = info->id;

if (id.compare(SCS_TELEMETRY_CONFIG_job) == 0) {
for (const scs_named_value_t* current = info->attributes; current->name; ++current) {
for (size_t i = 0; i < sizeof(info->attributes) / sizeof(scs_named_value_t); i++)
{
const scs_named_value_t* current = &info->attributes[i];
const std::string name = current->name;

if (name.compare(SCS_TELEMETRY_CONFIG_ATTRIBUTE_cargo) == 0) {
telemetry->cargo.name = current->value.value_string.value;
}
Expand All @@ -102,8 +105,11 @@ SCSAPI_VOID handle_configuration(const scs_event_t event, const void* const even
}
}
else if (id.compare(SCS_TELEMETRY_CONFIG_truck) == 0) {
for (const scs_named_value_t* current = info->attributes; current->name; ++current) {
for (size_t i = 0; i < sizeof(info->attributes) / sizeof(scs_named_value_t); i++)
{
const scs_named_value_t* current = &info->attributes[i];
const std::string name = current->name;

if (name.compare(SCS_TELEMETRY_CONFIG_ATTRIBUTE_brand) == 0) {
telemetry->truck.brand = current->value.value_string.value;
}
Expand Down
5 changes: 3 additions & 2 deletions helper_functions.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <string>
#include <chrono>
#include <vector>
#include <memory>

#include "cities_ets2.h"
#include "cities_ats.h"
Expand Down Expand Up @@ -38,7 +39,7 @@ void set_activity(std::string details, std::string state, std::string smallImage
telemetry->discordCore->ActivityManager().UpdateActivity(activity, [](discord::Result result) {});
}

tree2d* construct_tree(telemetry_state* telemetry) {
std::unique_ptr<tree2d> construct_tree(telemetry_state* telemetry) {
city* cities = NULL;
size_t length;

Expand Down Expand Up @@ -69,5 +70,5 @@ tree2d* construct_tree(telemetry_state* telemetry) {
points.push_back(p);
}

return new tree2d(std::begin(points), std::end(points));
return std::make_unique<tree2d>(std::begin(points), std::end(points));
}

0 comments on commit 3907783

Please sign in to comment.