Skip to content

Commit 7d85118

Browse files
authored
Merge pull request #5 from sussy-drip/master
Overhauling argument behavior
2 parents ed08a89 + 4853dfd commit 7d85118

6 files changed

Lines changed: 119 additions & 17 deletions

File tree

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,5 +39,7 @@
3939
.vs
4040
Debug
4141
Release
42+
vcpkg
43+
.vscode
4244
vcpkg_installed
4345
*.user

README.md

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,29 +5,58 @@ A diablo 2 1.13C installation is required, but this API should work for all vers
55

66
## Installation
77

8-
Download & install vcpkg https://github.com/microsoft/vcpkg (don't forget to add to path)
8+
First install [vcpkg](https://github.com/microsoft/vcpkg)
99

10-
Dependencies should now be installed and included for you when building from source.
10+
### Windows
11+
```
12+
git clone https://github.com/microsoft/vcpkg
13+
.\vcpkg\bootstrap-vcpkg.bat
14+
```
15+
16+
### Unix
17+
```
18+
git clone https://github.com/microsoft/vcpkg
19+
.\vcpkg\bootstrap-vcpkg.sh
20+
```
21+
22+
The binary will be located here `.\vcpkg\vcpkg`, make sure to include the absolute path to this in your system path environment variable and restart your shell. Once done setup a vcpkg integration by running:
23+
24+
```
25+
vcpkg integrate install
26+
```
27+
28+
If you are using Visual Studio to build this project, vcpkg dependencies will be automatically installed and included to this repository. If not, you will need to manually fetch and include the following packages:
1129

12-
If required, manually install and include these dependences:
1330
```
1431
vcpkg install boost-uuid
1532
vcpkg install restinio
1633
vcpkg install json-dto
1734
```
1835

36+
You should now be able to build d2mapapi.sln
37+
1938
## Usage
2039

21-
d2mapapi.exe DIABLO2_PATH \[ADDRESS_OVERRIDE\]
40+
Syntax:
41+
```
42+
d2mapapi.exe {pathToDiablo2} [optionalArguments]
43+
```
44+
45+
#### Optional Arguments
46+
* -i or --ip
47+
specify an IP address to use instead of localhost. ex: `-i 192.168.0.1` OR `--ip=192.168.0.1`";
48+
* -p or --port
49+
specify n port address to use instead of the default 8080. ex: `-p 8080` OR `--port=8080`";
50+
* -h or --help
51+
display usage information
2252

23-
Examples:
53+
#### Examples
2454
```
2555
d2mapapi.exe "C:\Diablo II1.13c"
26-
d2mapapi.exe "C:\Diablo II1.13c" "0.0.0.0"
56+
d2mapapi.exe "C:\Diablo II1.13c" --ip=0.0.0.0 --port=88
57+
d2mapapi.exe "C:\Diablo II1.13c" --port=80
2758
```
2859

29-
Starts the webserver
30-
3160
## Running in Docker
3261

3362
This requires [Docker Desktop](https://www.docker.com/products/docker-desktop)
@@ -43,7 +72,7 @@ WORKDIR /app
4372
# d2mapapi should be in the same folder as your dockerfile
4473
COPY ./d2mapapi .
4574
EXPOSE 8080
46-
CMD ["wine", "d2mapapi.exe", "/app/game", "0.0.0.0"]
75+
CMD ["wine", "d2mapapi.exe", "/app/game", "--ip=0.0.0.0"]
4776
```
4877

4978
[Download the latest release](https://github.com/jcageman/d2mapapi/releases)

d2mapapi/Helpers.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#include <iostream>
2+
#include <string>
3+
#include <regex>
4+
5+
std::string extractArg(std::string arg) {
6+
std::regex rgx("--\\w+=(.+)");
7+
std::smatch match;
8+
if (std::regex_search(arg, match, rgx)) {
9+
return match[1];
10+
}
11+
else {
12+
std::cerr << "Failed to extract value in argument: " + arg;
13+
return "";
14+
}
15+
}
16+
17+
void printHelp() {
18+
std::cerr << "Usage: program.exe {diablo2dir} [optionalArguments]\n";
19+
std::cerr << "\t-i or --ip\n\t\tspecify an IP address to use instead of localhost.\n\t\tex: -i 192.168.0.1 OR --ip=192.168.0.1\n";
20+
std::cerr << "\t-p or --port\n\t\tspecify an port address to use instead of the default 8080.\n\t\tex: -p 8080 OR --port=8080\n";
21+
std::cerr << "\t-h or --help\n\t\tprints this usage information\n";
22+
std::cerr << "Example: program.exe \"C:\\Diablo II1.13c\"\nor\nExample: program.exe \"C:\\Diablo II1.13c\" -i 0.0.0.0 -p 8888";
23+
}

d2mapapi/Helpers.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#pragma once
2+
#include <string>
3+
4+
void printHelp();
5+
std::string extractArg(std::string arg);

d2mapapi/d2mapapi.vcxproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,14 @@
2121
<ClCompile Include="NewSessionDto.cpp" />
2222
<ClCompile Include="Session.cpp" />
2323
<ClCompile Include="SessionDto.cpp" />
24+
<ClCompile Include="Helpers.cpp" />
2425
</ItemGroup>
2526
<ItemGroup>
2627
<ClInclude Include="CollisionMap.h" />
2728
<ClInclude Include="D2Map.h" />
2829
<ClInclude Include="D2Ptrs.h" />
2930
<ClInclude Include="D2Structs.h" />
31+
<ClInclude Include="Helpers.h" />
3032
<ClInclude Include="MapDto.h" />
3133
<ClInclude Include="MapRequestHandler.h" />
3234
<ClInclude Include="Offset.h" />

d2mapapi/main.cpp

Lines changed: 49 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,21 @@
44

55
#include "D2Map.h"
66
#include "ServerHandler.h"
7-
8-
7+
#include "Helpers.h"
98

109
int main( int argc, const char* argv[] ) {
10+
int i;
1111

1212
// Check command line arguments.
1313
if (argc < 2)
1414
{
15-
std::cerr << "Usage: program.exe {diablo2dir} [addressOverride]\n";
16-
std::cerr << "Example: program.exe \"C:\\Diablo II1.13c\"\nor\nExample: program.exe \"C:\\Diablo II1.13c\" 0.0.0.0";
15+
printHelp();
1716
return 1;
1817
}
1918

2019
using namespace std::chrono;
2120
try
2221
{
23-
2422
init( argv[1] );
2523

2624
using traits_t =
@@ -30,14 +28,57 @@ int main( int argc, const char* argv[] ) {
3028
router_t >;
3129

3230
std::string address = "localhost";
31+
std::uint16_t port = 8080;
32+
33+
// handle args in this loop:
34+
for(i=2; i<argc; ++i)
35+
{
36+
std::string tmpArg = argv[i];
37+
std::string argVal;
38+
39+
if (tmpArg == "-h" || tmpArg == "--help")
40+
{
41+
printHelp();
42+
return 0;
43+
}
44+
45+
if (tmpArg == "-i")
46+
address = argv[i + 1];
47+
48+
if (tmpArg == "-p")
49+
{
50+
std::string tmpPort = argv[i + 1];
51+
port = std::stoi(tmpPort);
52+
}
53+
54+
if (tmpArg.find("--ip") != std::string::npos)
55+
{
56+
argVal = extractArg(tmpArg);
57+
if (argVal == "") {
58+
return 1;
59+
}
60+
else {
61+
address = argVal;
62+
}
63+
}
64+
3365

34-
if (argc == 3) {
35-
address = argv[2];
66+
if (tmpArg.find("--port") != std::string::npos)
67+
{
68+
argVal = extractArg(tmpArg);
69+
if (argVal == "") {
70+
return 1;
71+
}
72+
else {
73+
port = std::stoi(extractArg(tmpArg));
74+
}
75+
}
3676
}
3777

3878
restinio::run(
3979
restinio::on_this_thread< traits_t >()
4080
.address( address )
81+
.port( port )
4182
.request_handler( map::create_server_handler() )
4283
.read_next_http_message_timelimit( 10s )
4384
.write_http_response_timelimit( 1s )
@@ -50,4 +91,4 @@ int main( int argc, const char* argv[] ) {
5091
}
5192

5293
return 0;
53-
}
94+
}

0 commit comments

Comments
 (0)