Skip to content

Commit

Permalink
Script downloading fixes, additional squirrel handlers added.
Browse files Browse the repository at this point in the history
  • Loading branch information
pnill committed Jun 3, 2020
1 parent 0b2478b commit 37bd2ff
Show file tree
Hide file tree
Showing 9 changed files with 444 additions and 57 deletions.
51 changes: 46 additions & 5 deletions xlive/H2MOD.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ tc_simulation_unit_entity_definition_creation_decode pc_simulation_unit_entity_d
bool __stdcall c_simulation_unit_entity_definition_creation_decode(void *thisptr, int creation_data_size, void* creation_data, bitstream* stream)
{
//LOG_TRACE_GAME_N("c_simulation_unit_entity_definition_creation_decode()\r\nthisptr: %08X, creation_data_size: %i, creation_data: %08X, packet: %08X", thisptr, creation_data_size, creation_data, packet);

if (stream->data_decode_bool("object-permutation-exists"))
{
//LOG_TRACE_GAME_N("c_simulation_unit_entity_decode - object-permutation-exists packet: %08X, *packet: %08X", packet, *(int*)packet);
Expand Down Expand Up @@ -533,6 +533,8 @@ void H2MOD::set_unit_biped(Player::Biped biped_type, int playerIndex)
PlayerIterator playersIt;
if (playerIndex >= 0 && playerIndex < 16)
playersIt.get_data_at_index(playerIndex)->properties.profile.player_character_type = biped_type;

*(BYTE*)(((BYTE*)h2mod->GetAddress(0x977104) + 0x20CC * playerIndex) + 8) = (BYTE)biped_type;
}

BYTE H2MOD::get_unit_team_index(datum unit_datum_index)
Expand Down Expand Up @@ -574,7 +576,7 @@ void H2MOD::set_unit_speed_patch(bool hackit) {
}
else
{
memcpy(patch_addr, origBytes, 8);
WriteBytes((DWORD)patch_addr, origBytes, 8);
}

}
Expand Down Expand Up @@ -667,6 +669,25 @@ void H2MOD::disable_sound(int sound)
}
}

void H2MOD::custom_sound_play(const char* soundName, int delay)
{
auto playSound = [=]()
{
//std::unique_lock<std::mutex> lck(h2mod->sound_mutex);
std::chrono::high_resolution_clock::time_point timePoint = std::chrono::high_resolution_clock::now() + std::chrono::milliseconds(delay);

LOG_TRACE_GAME("[H2MOD-SoundQueue] - attempting to play sound {0} - delaying {1} miliseconds first", soundName, delay);

if (delay > 0)
std::this_thread::sleep_until(timePoint);

PlaySoundA(soundName, NULL, SND_FILENAME | SND_NODEFAULT);
};

if (!h2mod->Server)
std::thread(playSound).detach();
}

void H2MOD::custom_sound_play(const wchar_t* soundName, int delay)
{
auto playSound = [=]()
Expand Down Expand Up @@ -1573,7 +1594,27 @@ int __cdecl LoadSaveGameHook(int a1, SaveStackStruct *a2, char a3)

wsprintf((wchar_t*)((BYTE*)a2->OutBuffer + 4), var_name_mod);
*(BYTE*)((BYTE*)a2->OutBuffer + 0x44) = variant_engine[engine_name];


short flags = *(short*)((BYTE*)a2->OutBuffer + 0x48);
*(short*)((BYTE*)a2->OutBuffer + 0x48) ^= (-(unsigned long)ScriptEngine::g_sqTeamGame ^ flags) & (1UL << variant_flag_bitfield::_game_engine_teams_bit);
*(short*)((BYTE*)a2->OutBuffer + 0x48) ^= (-(unsigned long)ScriptEngine::g_sqMotionSensor ^ flags) & (1UL << variant_flag_bitfield::_game_engine_motion_sensor_bit);
*(short*)((BYTE*)a2->OutBuffer + 0x48) ^= (-(unsigned long)ScriptEngine::g_sgAlwaysInvisible ^ flags) & (1UL << variant_flag_bitfield::_game_engine_always_invisible_bit);
*(short*)((BYTE*)a2->OutBuffer + 0x48) ^= (-(unsigned long)ScriptEngine::g_sgRoundSwitchResetsMap ^ flags) & (1UL << variant_flag_bitfield::_game_engine_round_switch_resets_map_bit);
*(short*)((BYTE*)a2->OutBuffer + 0x48) ^= (-(unsigned long)ScriptEngine::g_sgObservers ^ flags) & (1UL << variant_flag_bitfield::_game_engine_observers_bit);
*(short*)((BYTE*)a2->OutBuffer + 0x48) ^= (-(unsigned long)ScriptEngine::g_sgAllowChangingTeams ^ flags) & (1UL << variant_flag_bitfield::_game_engine_changing_teams_bit);
*(short*)((BYTE*)a2->OutBuffer + 0x48) ^= (-(unsigned long)ScriptEngine::g_sgFriendlyFire ^ flags) & (1UL << variant_flag_bitfield::_game_engine_friendly_fire_bit);
*(short*)((BYTE*)a2->OutBuffer + 0x48) ^= (-(unsigned long)ScriptEngine::g_sgOvershieldsOnMap ^ flags) & (1UL << variant_flag_bitfield::_game_engine_overshields_on_map_bit);
*(short*)((BYTE*)a2->OutBuffer + 0x48) ^= (-(unsigned long)ScriptEngine::g_sgInvisibilityOnMap ^ flags) & (1UL << variant_flag_bitfield::_game_engine_invisiblity_on_map_bit);
*(short*)((BYTE*)a2->OutBuffer + 0x48) ^= (-(unsigned long)ScriptEngine::g_sgGrenadesOnMap ^ flags) & (1UL << variant_flag_bitfield::_game_engine_grenades_on_map_bit);
*(short*)((BYTE*)a2->OutBuffer + 0x48) ^= (-(unsigned long)ScriptEngine::g_sgStartingGrenades ^ flags) & (1UL << variant_flag_bitfield::_game_engine_starting_grenades_bit);
*(short*)((BYTE*)a2->OutBuffer + 0x48) ^= (-(unsigned long)ScriptEngine::g_sgExtraDamage ^ flags) & (1UL << variant_flag_bitfield::_game_engine_extra_damage_bit);
*(short*)((BYTE*)a2->OutBuffer + 0x48) ^= (-(unsigned long)ScriptEngine::g_sgDamageResistant ^ flags) & (1UL << variant_flag_bitfield::_game_engine_extra_damage_bit);
*(short*)((BYTE*)a2->OutBuffer + 0x48) ^= (-(unsigned long)ScriptEngine::g_sgForceEvenTeams ^ flags) & (1UL << variant_flag_bitfield::_game_engine_force_even_teams_bit);
*(short*)((BYTE*)a2->OutBuffer + 0x48) ^= (-(unsigned long)ScriptEngine::g_sgRoundSetting1Round ^ flags) & (1UL << variant_flag_bitfield::_game_engine_round_setting_1_round);


*(short*)((BYTE*)a2->OutBuffer + 0x54) = ScriptEngine::g_sqRoundTime;

return 1;
}

Expand Down Expand Up @@ -1705,6 +1746,7 @@ void H2MOD::ApplyHooks() {

// bellow hooks applied to specific executables
if (this->Server == false) {
HMODULE h = GetModuleHandleA("DiscordHook.dll");

//DWORD random_memory = malloc()
LOG_TRACE_GAME("Applying client hooks...");
Expand Down Expand Up @@ -1788,8 +1830,7 @@ void H2MOD::ApplyHooks() {

//Hooked to fix custom map images.
Codecave(GetAddress(0x593F0), load_map_data_for_display,0);



//Initialise_tag_loader();
}
else {
Expand Down
21 changes: 21 additions & 0 deletions xlive/H2MOD.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,25 @@ enum game_life_cycle : int
life_cycle_matchmaking
};

enum variant_flag_bitfield : BYTE
{
_game_engine_teams_bit = 0,
_game_engine_motion_sensor_bit,
_game_engine_always_invisible_bit,
_game_engine_round_switch_resets_map_bit,
_game_engine_tie_resolution_bit,
_game_engine_observers_bit,
_game_engine_changing_teams_bit,
_game_engine_friendly_fire_bit,
_game_engine_overshields_on_map_bit,
_game_engine_invisiblity_on_map_bit,
_game_engine_grenades_on_map_bit,
_game_engine_starting_grenades_bit,
_game_engine_extra_damage_bit,
_game_engine_damage_resistant_bit,
_game_engine_force_even_teams_bit,
_game_engine_round_setting_1_round
};
int __cdecl call_object_try_and_get_with_type(datum object_datum_index, int object_type);
int __cdecl call_unit_reset_equipment(datum unit_datum_index);
bool __cdecl call_add_object_to_sync(datum gamestate_object_datum);
Expand Down Expand Up @@ -87,6 +106,8 @@ class H2MOD
void set_player_unit_grenades_count(int playerIndex, BYTE type, BYTE count, bool resetEquipment);
void disable_sound(int sound);
void custom_sound_play(const wchar_t* soundName, int delay);
void custom_sound_play(const char* soundName, int delay);

void toggle_weapon_pickup();
void toggle_weapon_pickup(bool bEnable);
void leave_session();
Expand Down
2 changes: 1 addition & 1 deletion xlive/H2MOD/GUI/GUI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ extern void handleHotkeyInput(WPARAM lpMsg);
// #5030: XLivePreTranslateMessage
BOOL WINAPI XLivePreTranslateMessage(const LPMSG lpMsg)
{
if ((GetAsyncKeyState(lpMsg->wParam) & 0x8000) && (lpMsg->message == WM_KEYDOWN || lpMsg->message == WM_SYSKEYDOWN))
if ((GetKeyState(lpMsg->wParam) & 0x8000) && (lpMsg->message == WM_KEYDOWN || lpMsg->message == WM_SYSKEYDOWN))
{
// hotkeys
handleHotkeyInput(lpMsg->wParam);
Expand Down
13 changes: 9 additions & 4 deletions xlive/H2MOD/Modules/Networking/CustomPackets/CustomPackets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ void register_custom_packets(void* network_messages)
register_packet_impl(network_messages, request_sq_script, "request-sq-script", 0, sizeof(s_request_script), sizeof(s_request_script),
(void*)sqScriptDownloader::encode_sq_request_packet, (void*)sqScriptDownloader::decode_sq_request_packet, NULL);

register_packet_impl(network_messages, send_sq_script, "send-sq-script", 0, 4, 65535, (void*)sqScriptDownloader::encode_sq_send_packet,(void*)sqScriptDownloader::decode_sq_send_packet, NULL);
register_packet_impl(network_messages, send_sq_script, "send-sq-script", 1, 4, 0xFFFFF, (void*)sqScriptDownloader::encode_sq_send_packet,(void*)sqScriptDownloader::decode_sq_send_packet, NULL);


}
Expand Down Expand Up @@ -223,11 +223,13 @@ void __stdcall handle_channel_message_hook(void *thisx, int network_channel_inde
size_t data_size = sizeof(s_send_script) + scriptData.size() + 1;
s_send_script *data = (s_send_script*)malloc(data_size);
ZeroMemory(data, sizeof(s_send_script) + scriptData.size());
data->script_size = scriptData.size();
data->script_size = scriptData.size() +1;
strcpy(&data->script_data[0], scriptData.c_str());
data->script_data[scriptData.size() + 1] = 0;

observer->sendNetworkMessage(session->unk_index, observer_channel->observer_index, false, send_sq_script, data_size, data);

free(data);
}

}
Expand Down Expand Up @@ -284,13 +286,16 @@ void __stdcall handle_channel_message_hook(void *thisx, int network_channel_inde
return;
}

case send_sq_script:
case send_sq_script: // received script from server
{
if (*(int*)network_channel + 0x54 == 5)
if (*(int*)(network_channel + 0x54) == 5)
{
s_send_script* received_data = (s_send_script*)packet;
LOG_INFO_FUNC("[H2MOD-CustomPackets] script_size: {}", received_data->script_size);
LOG_INFO_FUNC("[H2MOD-CustomPackets] script: {}",(char*)&received_data->script_data);

sqScriptDownloader::sq_process_script((char*)&received_data->script_data);

}
return;
}
Expand Down
1 change: 0 additions & 1 deletion xlive/H2MOD/Modules/Networking/Networking.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ bool __cdecl parameters_update_decode(bitstream* stream, int a2, int a3)
int script_id = 0;
bool ret = pparameters_update_decode(stream, a2, a3);
bool script_loaded = stream->data_decode_bool("sqScript-loaded");

if(script_loaded == true)
script_id = stream->data_decode_integer("sqScript-id", 32);

Expand Down
24 changes: 16 additions & 8 deletions xlive/H2MOD/Modules/Scripting/ScriptDownloader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@
#include "ScriptDownloader.h"
#include "H2MOD/Modules/Networking/Networking.h"
#include "H2MOD/Modules/Networking/NetworkObserver/NetworkObserver.h"
#include "H2MOD.h"
#include "xliveless.h"
#include <iostream>
#include <limits>

#undef max // windows.h defines max.

bool g_sqScriptDownloaded = false;
bool g_sqScriptDownloaded = true;

namespace sqScriptDownloader {

Expand All @@ -35,8 +36,8 @@ namespace sqScriptDownloader {
{
s_request_script data;
XUserGetXUID(0, &data.user_identifier);

network_observer* observer = session->network_observer_ptr;

peer_observer_channel* observer_channel = NetworkSession::getPeerObserverChannel(session->session_host_peer_index);

if (observer_channel->field_1)
Expand Down Expand Up @@ -68,18 +69,19 @@ namespace sqScriptDownloader {
void sq_process_script(char* script_data)
{
// process script data received
ScriptEngine::sqLoadScriptFromString(script_data);
}

void encode_sq_request_packet(bitstream* stream, int a2, s_request_script* data)
{
LOG_INFO_FUNC(" user_identifier: {}", data->user_identifier);
stream->data_encode_integer("user-identifier", data->user_identifier, 64);
stream->data_encode_bits("user-identifier", &data->user_identifier, 64);
}

bool decode_sq_request_packet(bitstream* stream, int a2, s_request_script* data)
{
data->user_identifier = stream->data_decode_integer("user-identifier", 64);
stream->data_decode_bits("user-identifier", (int)&data->user_identifier, 64);

LOG_INFO_FUNC(" user_idenitfier: {}", data->user_identifier);

return stream->packet_is_valid() == false;
Expand All @@ -97,10 +99,16 @@ namespace sqScriptDownloader {
bool decode_sq_send_packet(bitstream* stream, int a2, s_send_script* data)
{
data->script_size = stream->data_decode_integer("sqScriptSize", 32);
stream->data_decode_bits("sqScriptData", (int)&data->script_data, data->script_size * CHAR_BIT);

if (stream->packet_is_valid())
LOG_CRITICAL_FUNC("stream->packet_is_valid()");


if (data->script_size < 0 || data->script_size > 0xFFFF || a2 != data->script_size + 4)
LOG_CRITICAL_FUNC("data->script_size < 0 || data->script_size > 0xFFFF || a2 != data->script_size + 4");

LOG_INFO_FUNC("data->script_size: {}", data->script_size);
LOG_INFO_FUNC("data->data: {}", (char*)&data->script_data);
stream->data_decode_bits("sqScriptData", (int)&data->script_data, data->script_size * CHAR_BIT);

g_sqScriptDownloaded = true;

return stream->packet_is_valid() == false;
Expand Down
1 change: 1 addition & 0 deletions xlive/H2MOD/Modules/Scripting/ScriptDownloader.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ namespace sqScriptDownloader {
void encode_sq_send_packet(bitstream* stream, int a2, s_send_script* data);
bool decode_sq_send_packet(bitstream* stream, int a2, s_send_script* data);
void send_sq_request_packet();
void sq_process_script(char* script_data);
std::string sq_send_script_fill_data();
}
Loading

0 comments on commit 37bd2ff

Please sign in to comment.