aboutsummaryrefslogtreecommitdiff
path: root/games/openttd/files/extra-patch-save-passwords
blob: 3620a34f7de739f596bed5a1b36729197669633d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
--- src/network/network_func.h	2014-10-21 21:36:31.000000000 +0300
+++ src/network/network_func.h	2014-11-09 21:37:49.000000000 +0200
@@ -74,7 +74,8 @@
 bool NetworkServerStart();
 void NetworkServerNewCompany(const Company *company, NetworkClientInfo *ci);
 bool NetworkServerChangeClientName(ClientID client_id, const char *new_name);
-
+void NetworkSavePassword();
+void NetworkLoadPassword();
 
 void NetworkServerDoMove(ClientID client_id, CompanyID company_id);
 void NetworkServerSendRcon(ClientID client_id, TextColour colour_code, const char *string);
--- src/network/network_server.cpp	2014-10-21 21:36:31.000000000 +0300
+++ src/network/network_server.cpp	2014-11-09 21:37:49.000000000 +0200
@@ -32,7 +32,7 @@
 #include "../core/pool_func.hpp"
 #include <mutex>
 #include <condition_variable>
-
+#include "../fileio_func.h"
 #include "../safeguards.h"
 
 
@@ -500,6 +500,7 @@
 	/* Reset 'lag' counters */
 	this->last_frame = this->last_frame_server = _frame_counter;
 
+	DEBUG( net, 1, "requesting GAME password" );
 	Packet *p = new Packet(PACKET_SERVER_NEED_GAME_PASSWORD);
 	this->SendPacket(p);
 	return NETWORK_RECV_STATUS_OKAY;
@@ -1684,6 +1685,9 @@
 				IConsolePrintF(CC_DEFAULT, "Auto-removed protection from company #%d", c->index + 1);
 				_network_company_states[c->index].months_empty = 0;
 				NetworkServerUpdateCompanyPassworded(c->index, false);
+                                if (_settings_client.network.save_password) {
+                                        NetworkSavePassword( );
+                        	}
 			}
 			/* Is the company empty for autoclean_novehicles-months, and has no vehicles? */
 			if (_settings_client.network.autoclean_novehicles != 0 && _network_company_states[c->index].months_empty > _settings_client.network.autoclean_novehicles && vehicles_in_company[c->index] == 0) {
@@ -1782,6 +1786,9 @@
 
 	strecpy(_network_company_states[company_id].password, password, lastof(_network_company_states[company_id].password));
 	NetworkServerUpdateCompanyPassworded(company_id, !StrEmpty(_network_company_states[company_id].password));
+        if (_settings_client.network.save_password) {
+                NetworkSavePassword( );
+	}
 }
 
 /**
@@ -2206,4 +2213,47 @@
 	}
 }
 
+void NetworkSavePassword( )
+{
+	static FILE *file_pointer;
+	char password_file_name[80];
+
+	seprintf( password_file_name, lastof(password_file_name), "%u.pwd", _settings_game.game_creation.generation_seed );
+	DEBUG( net, 0, "Saving companies password to %s", password_file_name );
+	file_pointer = FioFOpenFile( password_file_name, "wb", SAVE_DIR );
+
+	if (file_pointer != NULL) {
+		for( CompanyID l_company = (CompanyID)0; l_company < MAX_COMPANIES; l_company++ ) {
+			if (NetworkCompanyIsPassworded(l_company)) {
+				fwrite( _network_company_states[l_company].password, strlen(_network_company_states[l_company].password), 1, file_pointer);
+			}
+			fwrite( "\n", 1, 1, file_pointer );
+		}
+		fclose(file_pointer);
+	}
+}
+
+void NetworkLoadPassword( )
+{
+	static FILE *file_pointer;
+	char password[NETWORK_PASSWORD_LENGTH];
+	char password_file_name[80];
+
+	seprintf( password_file_name, lastof(password_file_name), "%u.pwd", _settings_game.game_creation.generation_seed );
+	file_pointer = FioFOpenFile( password_file_name, "rb", SAVE_DIR );
+	if (file_pointer != NULL) {
+		DEBUG( net, 0, "Loading password from %s", password_file_name );
+		for( CompanyID l_company = (CompanyID)0; l_company < MAX_COMPANIES; l_company++ ) {
+			fgets( password, sizeof( password), file_pointer);
+			if (strlen(password)>1) {
+				fseek( file_pointer, 1L, SEEK_CUR );
+				strecpy(_network_company_states[l_company].password, password, lastof(_network_company_states[l_company].password));
+				NetworkServerUpdateCompanyPassworded(l_company, !StrEmpty(_network_company_states[l_company].password));
+			}
+		}
+	} else {
+		DEBUG( net, 0, "Password file %s not found", password_file_name );
+	}
+}
+
 #endif /* ENABLE_NETWORK */
--- src/openttd.cpp	2014-10-21 21:36:36.000000000 +0300
+++ src/openttd.cpp	2014-11-09 21:40:39.000000000 +0200
@@ -1111,6 +1111,10 @@
 #ifdef ENABLE_NETWORK
 				if (_network_server) {
 					seprintf(_network_game_info.map_name, lastof(_network_game_info.map_name), "%s (Loaded game)", _file_to_saveload.title);
+					// Try to load password
+					if ( _settings_client.network.save_password ) {
+						NetworkLoadPassword( );
+					}
 				}
 #endif /* ENABLE_NETWORK */
 			}
--- src/settings_type.h	2014-10-21 21:36:35.000000000 +0300
+++ src/settings_type.h	2014-11-09 21:37:49.000000000 +0200
@@ -266,6 +266,7 @@
 	char   last_host[NETWORK_HOSTNAME_LENGTH];            ///< IP address of the last joined server
 	uint16 last_port;                                     ///< port of the last joined server
 	bool   no_http_content_downloads;                     ///< do not do content downloads over HTTP
+	bool   save_password;                                 ///< If password file is used
 };
 
 /** Settings related to the creation of games. */
--- src/table/settings.ini	2014-10-21 21:36:21.000000000 +0300
+++ src/table/settings.ini	2014-11-09 21:37:49.000000000 +0200
@@ -3915,6 +3915,12 @@
 def      = false
 cat      = SC_EXPERT
 
+[SDTC_BOOL]
+ifdef   = ENABLE_NETWORK
+var     = network.save_password
+flags   = SLF_NOT_IN_SAVE | SLF_NO_NETWORK_SYNC
+def     = false
+
 ; Since the network code (CmdChangeSetting and friends) use the index in this array to decide
 ; which setting the server is talking about all conditional compilation of this array must be at the
 ; end. This isn't really the best solution, the settings the server can tell the client about should