Skip to content

Commit

Permalink
add save/load settings
Browse files Browse the repository at this point in the history
  • Loading branch information
RUEEE committed Sep 14, 2023
1 parent c51e0b2 commit 086aae8
Show file tree
Hide file tree
Showing 9 changed files with 159 additions and 51 deletions.
91 changes: 74 additions & 17 deletions Connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -444,14 +444,16 @@ void ConnectLoop()
P2PConnection::P2PConnection():
port_listen_Host(10800),port_send_Guest(10801),port_sendto(c_no_port), delay_compensation(3),
is_host(true), is_ipv6(false),pack_index(0),is_blocking(false),
connect_state(ConnectState::No_Connection),is_addr_guest_sendto_set(false)
connect_state(ConnectState::No_Connection),is_addr_sendto_set(false)
{
// GetTime(&last_init_value);
memset(&addr_self6, 0, sizeof(addr_self6));
memset(&addr_other6, 0, sizeof(addr_other6));
memset(&addr_self4, 0, sizeof(addr_self4));
memset(&addr_other4, 0, sizeof(addr_other4));
memset(&address_sendto, 0, sizeof(address_sendto));
socket_udp = INVALID_SOCKET;

LoadSettings();
}


Expand All @@ -468,14 +470,14 @@ bool P2PConnection::SetUpConnect_Guest()
socket_udp = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
res_udp=bind(socket_udp, (SOCKADDR*)&(addr_self6), sizeof(addr_self6));

LogInfo(std::format("ipv6 Guest,to {} port: {}", this->addr_sendto,this->port_sendto));
LogInfo(std::format("ipv6 Guest,to {} port: {}", this->ip_sendto,this->port_sendto));
}else {//ipv4

addr_self4 = { AF_INET, htons(port_send_Guest) };
socket_udp = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
res_udp = bind(socket_udp, (SOCKADDR*)&(addr_self4), sizeof(addr_self4));

LogInfo(std::format("ipv4 Guest,to {} port: {}", this->addr_sendto, this->port_sendto));
LogInfo(std::format("ipv4 Guest,to {} port: {}", this->ip_sendto, this->port_sendto));
}
if (res_udp != 0 || socket_udp ==INVALID_SOCKET) {
LogError(std::format("fail to create udp socket : {}", WSAGetLastError()));
Expand All @@ -485,6 +487,7 @@ bool P2PConnection::SetUpConnect_Guest()
SetSocketBlocking(socket_udp);
LogInfo(std::format("set up connect"));
connect_state = ConnectState::Guest_Requesting;
SaveSettings();
return true;
}

Expand Down Expand Up @@ -515,6 +518,7 @@ bool P2PConnection::SetUpConnect_Host(bool iis_ipv6)
SetSocketBlocking(socket_udp);
LogInfo(std::format("set up connect"));
connect_state = ConnectState::Host_Listening;
SaveSettings();
return true;
}

Expand Down Expand Up @@ -636,35 +640,88 @@ int P2PConnection::SendUDPPack(Data_StatePack data)
return l_nLen;
}

bool P2PConnection::SetGuestSocketSetting(std::string host_ipaddress, int port_sendto_Guest, bool iis_ipv6)

void P2PConnection::LoadSettings()
{
char buffer[256];
PushCurrentDictionary(L"%appdata%\\ShanghaiAlice\\th19");

GetPrivateProfileStringA("connect_setting", "addr_sendto", "", buffer, sizeof(buffer), c_setting_file);
memcpy(address_sendto, buffer, sizeof(buffer));
SetGuestSocketSetting();

GetPrivateProfileStringA("connect_setting", "delay", "3", buffer, sizeof(buffer), c_setting_file);
delay_compensation=s_atoi(buffer,3);

GetPrivateProfileStringA("connect_setting", "port_for_guest", "10801", buffer, sizeof(buffer), c_setting_file);
port_send_Guest = s_atoi(buffer, 10801);
if (port_send_Guest < 0 || port_send_Guest>65535)
port_send_Guest = 10801;

GetPrivateProfileStringA("connect_setting", "port_for_host", "10800", buffer, sizeof(buffer), c_setting_file);
port_listen_Host = s_atoi(buffer, 10800);
if (port_listen_Host < 0 || port_listen_Host>65535)
port_listen_Host = 10800;
PopCurrentDictionary();

SaveSettings();
}

void P2PConnection::SaveSettings()
{
PushCurrentDictionary(L"%appdata%\\ShanghaiAlice\\th19");

WritePrivateProfileStringA("connect_setting", "addr_sendto", address_sendto, c_setting_file);

std::string buf="";
buf = std::format("{}", delay_compensation);
WritePrivateProfileStringA("connect_setting", "delay", buf.c_str(), c_setting_file);

buf = std::format("{}", port_send_Guest);
WritePrivateProfileStringA("connect_setting", "port_for_guest", buf.c_str(), c_setting_file);

buf = std::format("{}", port_listen_Host);
WritePrivateProfileStringA("connect_setting", "port_for_host", buf.c_str(), c_setting_file);

PopCurrentDictionary();
}

bool P2PConnection::SetGuestSocketSetting()
{
is_addr_guest_sendto_set = false;
if (port_sendto_Guest > 65535 || port_sendto_Guest < 0)
is_addr_sendto_set = false;

auto [ip_get, port_get, is_ipv6_get]=split_addr_and_port(std::string(address_sendto));
is_ipv6 = is_ipv6_get;

if (port_get > 65535 || port_get < 0){
port_sendto = c_no_port;
return false;
is_ipv6 = iis_ipv6;
}else{
port_sendto = port_get;
}

if (is_ipv6) {
addr_other6 = { AF_INET6, htons(port_sendto_Guest) };
if (inet_pton(AF_INET6, host_ipaddress.c_str(), &(addr_other6.sin6_addr)) != 1) {
addr_other6 = { AF_INET6, htons(port_sendto) };
if (inet_pton(AF_INET6, ip_get.c_str(), &(addr_other6.sin6_addr)) != 1) {
LogError("Wrong IP addr");
return false;
}
char buf[256] = { 0 };
inet_ntop(AF_INET6, &addr_other6.sin6_addr, buf, sizeof(buf));
addr_sendto = buf;
port_sendto = port_sendto_Guest;
ip_sendto = buf;
}else {//ipv4
addr_other4 = { AF_INET, htons(port_sendto_Guest) };
if (inet_pton(AF_INET, host_ipaddress.c_str(), &(addr_other4.sin_addr)) != 1) {
addr_other4 = { AF_INET, htons(port_sendto) };
if (inet_pton(AF_INET, ip_get.c_str(), &(addr_other4.sin_addr)) != 1) {
LogError("Wrong IP addr");
return false;
}
char buf[256] = { 0 };
inet_ntop(AF_INET, &addr_other4.sin_addr, buf, sizeof(buf));
addr_sendto = buf;
port_sendto = port_sendto_Guest;
ip_sendto = buf;
}

is_addr_guest_sendto_set = true;
is_addr_sendto_set = true;
return true;
}


Expand Down
14 changes: 9 additions & 5 deletions Connection.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ enum ConnectState

struct P2PConnection
{
static constexpr char const * c_setting_file = ".\\p2vp_settings.ini";
static constexpr int c_max_time_retry_timeout = 32;
static constexpr int c_time_ms_retry_sync = 2000;

Expand All @@ -71,8 +72,10 @@ struct P2PConnection
int port_listen_Host;
int port_send_Guest;

bool is_addr_guest_sendto_set;
std::string addr_sendto;
bool is_addr_sendto_set;
char address_sendto[256];

std::string ip_sendto;
int port_sendto;

bool is_blocking;
Expand All @@ -94,18 +97,19 @@ struct P2PConnection
bool SetUpConnect_Guest();
bool SetUpConnect_Host(bool iis_ipv6);


void EndConnect();


static bool WSAStartUp();

Pack CreateEmptyPack();
int RcvUDPPack();
int SendUDPPack(Data_KeyState data);
int SendUDPPack(Data_NAK_KeyState data);
int SendUDPPack(Data_StatePack data);
bool SetGuestSocketSetting(std::string host_ipaddress, int port_sendto_Guest,bool iis_ipv6);
bool SetGuestSocketSetting();

void LoadSettings();
void SaveSettings();
};


Expand Down
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,12 @@ Click show cmd will show the cmd console (it may cause lag when logging)

**since i only have keyboard, the gamepad control might be buggy(**


## Updates

1.04(23/09/14)

automatically save/load settings to/from ```%appdata%/ShanghaiAlice/th19/p2vp_settings.ini```

1.03 (23/09/13)

​ thcrap support, game without ALSR support
Expand Down
37 changes: 18 additions & 19 deletions UI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,41 +50,40 @@ void SetUI(IDirect3DDevice9* device)
ImGui::SetNextWindowPos(ImVec2(0.0f, 0.0f));

ImGui::SetNextWindowCollapsed(is_collapse);

const char* wind_capital = "###wind";
if (g_connection.connect_state == ConnectState::No_Connection)
{
ImGui::Begin("No Connection###wind",0,ImGuiWindowFlags_::ImGuiWindowFlags_NoMove | ImGuiWindowFlags_::ImGuiWindowFlags_NoResize);
wind_capital = "No Connection###wind";
}else if (g_connection.connect_state == ConnectState::Connected){
if(g_is_synced)
ImGui::Begin("PVP###wind", 0, ImGuiWindowFlags_::ImGuiWindowFlags_NoMove | ImGuiWindowFlags_::ImGuiWindowFlags_NoResize);
wind_capital = "PVP###wind";
else
ImGui::Begin("Desync Probably###wind", 0, ImGuiWindowFlags_::ImGuiWindowFlags_NoMove | ImGuiWindowFlags_::ImGuiWindowFlags_NoResize);
wind_capital = "Desync Probably###wind";
}else{
if(g_connection.is_ipv6)
ImGui::Begin("(ipv6) Waiting for 2P###wind", 0, ImGuiWindowFlags_::ImGuiWindowFlags_NoMove | ImGuiWindowFlags_::ImGuiWindowFlags_NoResize);
wind_capital = "(ipv6) Waiting for 2P###wind";
else
ImGui::Begin("(ipv4) Waiting for 2P###wind", 0, ImGuiWindowFlags_::ImGuiWindowFlags_NoMove | ImGuiWindowFlags_::ImGuiWindowFlags_NoResize);
wind_capital = "(ipv4) Waiting for 2P###wind";
}
ImGui::Begin(wind_capital, 0, ImGuiWindowFlags_::ImGuiWindowFlags_NoMove | ImGuiWindowFlags_::ImGuiWindowFlags_NoResize);

if (g_connection.connect_state == ConnectState::No_Connection){
g_is_focus_ui = ImGui::IsWindowFocused();
}else{
g_is_focus_ui = false;
}


is_collapse=ImGui::IsWindowCollapsed();




static int delay = g_connection.delay_compensation;
int delay = g_connection.delay_compensation;
ImGui::SetNextItemWidth(100.0f);
if (ImGui::InputInt("delay", &delay, 1, 5))
{
delay = std::clamp(delay, 1, 180);
if (g_connection.connect_state == ConnectState::No_Connection) {
g_connection.delay_compensation = delay;
}
else {
} else {
delay = g_connection.delay_compensation;
}
}
Expand All @@ -105,14 +104,14 @@ void SetUI(IDirect3DDevice9* device)
ImGui::Separator();

ImGui::SetNextItemWidth(240.0f);
static char address[256];

if (ImGui::InputText("host IP", address, sizeof(address), g_connection.connect_state==ConnectState::No_Connection ? ImGuiInputTextFlags_::ImGuiInputTextFlags_None : ImGuiInputTextFlags_::ImGuiInputTextFlags_ReadOnly)){
auto [addr,port,is_ipv6] = get_addr_and_port(std::string(address));
g_connection.SetGuestSocketSetting(addr, port, is_ipv6);
if (ImGui::InputText("host IP", g_connection.address_sendto, sizeof(g_connection.address_sendto),
g_connection.connect_state==ConnectState::No_Connection ? ImGuiInputTextFlags_::ImGuiInputTextFlags_AutoSelectAll : ImGuiInputTextFlags_::ImGuiInputTextFlags_ReadOnly
)){
g_connection.SetGuestSocketSetting();
}
if (g_connection.is_addr_guest_sendto_set){
ImGui::LabelText(" ###Gip", "%s: %s, port: %d", g_connection.is_ipv6 ? "ipv6" : "ipv4", g_connection.addr_sendto.c_str(), g_connection.port_sendto);
if (g_connection.is_addr_sendto_set){
ImGui::LabelText(" ###Gip", "%s: %s, port: %d", g_connection.is_ipv6 ? "ipv6" : "ipv4", g_connection.ip_sendto.c_str(), g_connection.port_sendto);
}else{
ImGui::LabelText(" ###Gip", "invalid ip address");
}
Expand Down Expand Up @@ -182,7 +181,7 @@ void SetUI(IDirect3DDevice9* device)
ImGui::End();
}

ImGui::TextColored(ImVec4(0.3f, 0.7f, 0.6f, 1.0f), "ver 1.03");
ImGui::TextColored(ImVec4(0.3f, 0.7f, 0.6f, 1.0f), "ver 1.04");
ImGui::End();


Expand Down
36 changes: 34 additions & 2 deletions Utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ DWORD GetAddress(DWORD Addr_noALSR)

void InitUtils()
{

QueryPerformanceFrequency(&(g_time_freq));
GetTime(&g_start_time);
}
Expand Down Expand Up @@ -113,6 +112,21 @@ bool test_is_ipv6(const std::string& addr)
return count>=2;//ipv4 only have 1 :
}

int s_atoi(const char* str, int default_int)
{
int ret = default_int;
try {
ret = std::atoi(str);
}
catch (std::invalid_argument const& ex) {
ret = default_int;
}
catch (std::out_of_range const& ex) {
ret = default_int;
}
return ret;
}

int s_stoi(const std::string& str,int default_int)
{
int ret= default_int;
Expand All @@ -127,9 +141,27 @@ int s_stoi(const std::string& str,int default_int)
}
return ret;
}
#include <stack>
std::stack<std::wstring> g_dict;
void PushCurrentDictionary(LPCWSTR new_dictionary)
{
WCHAR buffer[MAX_PATH] = { 0 };
GetCurrentDirectoryW(MAX_PATH, buffer);
g_dict.push(std::wstring(buffer));


ExpandEnvironmentStringsW(new_dictionary,buffer,MAX_PATH);
SetCurrentDirectoryW(buffer);
}

void PopCurrentDictionary()
{
SetCurrentDirectoryW(g_dict.top().c_str());
g_dict.pop();
}


std::tuple<std::string, int, bool> get_addr_and_port(const std::string& addr)
std::tuple<std::string, int, bool> split_addr_and_port(const std::string& addr)
{
if (addr.size() == 0)
return std::make_tuple("", c_no_port, false);
Expand Down
9 changes: 8 additions & 1 deletion Utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,15 @@ ImVec2 GetClientFromStage(ImVec2 stage, ImVec2 client_sz, bool is_1P);
DWORD GetAddress(DWORD Addr_noALSR);

constexpr int c_no_port = -1;

int s_atoi(const char* str, int default_int);
int s_stoi(const std::string& str, int default_int);

void PushCurrentDictionary(LPCWSTR new_dictionary);
void PopCurrentDictionary();

bool test_is_ipv6(const std::string& addr);
std::tuple<std::string, int, bool> get_addr_and_port(const std::string& addr);
std::tuple<std::string, int, bool> split_addr_and_port(const std::string& addr);

#define VALUED(x) (*(DWORD*)(x))
#define VALUEF(x) (*(float*)(x))
Expand Down
4 changes: 1 addition & 3 deletions dllmain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,7 @@ BOOL APIENTRY DllMain(HMODULE hModule,
{
case DLL_PROCESS_ATTACH:
#ifndef THCRAP
ImGui::CreateContext();
ImGui::StyleColorsLight();
InjectAll();
LoadDll();
#endif
break;
case DLL_THREAD_ATTACH:
Expand Down
Loading

0 comments on commit 086aae8

Please sign in to comment.