Skip to content
This repository has been archived by the owner on Jul 31, 2023. It is now read-only.

Commit

Permalink
feat: Add cmake support
Browse files Browse the repository at this point in the history
Now it is possible to compile the project under OS X. However,
additional work is required.

Signed-off-by: Alexander Kurbatov <alkurbatov@virtuozzo.com>
  • Loading branch information
Alexander Kurbatov committed Jan 12, 2020
1 parent 4455ab6 commit 07b4877
Show file tree
Hide file tree
Showing 9 changed files with 156 additions and 121 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
build/
18 changes: 18 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
cmake_minimum_required(VERSION 3.1)

project(Observer)

# Specify output directories.
set(EXECUTABLE_OUTPUT_PATH "${PROJECT_BINARY_DIR}/bin")

# Build with c++14 support, required by sc2api.
set(CMAKE_CXX_STANDARD 14)

# Disable building of examples in the cpp-sc2 submodule.
set(BUILD_API_EXAMPLES OFF CACHE INTERNAL "" FORCE)

# Disable building of tests in the cpp-sc2 submodule.
set(BUILD_API_TESTS OFF CACHE INTERNAL "" FORCE)

add_subdirectory("cpp-sc2")
add_subdirectory("src")
41 changes: 41 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
add_executable(Observer)

target_sources(Observer
PRIVATE
CameraModule.h
CameraModule.cpp
Observer.cpp
Timer.hpp
Tools.h
$<$<PLATFORM_ID:Windows>:ToolsWindows.cpp>
$<$<PLATFORM_ID:Darwin>:ToolsUnix.cpp>
)

include_directories(SYSTEM
${PROJECT_SOURCE_DIR}/cpp-sc2/include
${PROJECT_SOURCE_DIR}/cpp-sc2/contrib/protobuf/src
${PROJECT_BINARY_DIR}/cpp-sc2/generated
"."
)

if (MSVC)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W4 /WX-")
elseif (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Weverything \
-Wno-c++98-compat -Wno-c++98-compat-pedantic -Wno-padded \
-Wno-switch-enum -Wno-weak-vtables -Wno-exit-time-destructors \
-Wno-float-equal -Wno-global-constructors"
)
endif ()

set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -DDEBUG")

link_directories(${PROJECT_BINARY_DIR}/cpp-sc2/bin)

target_link_libraries(Observer
sc2api sc2utils
)

if (APPLE)
target_link_libraries(Observer "-framework Carbon")
endif ()
1 change: 1 addition & 0 deletions src/CameraModule.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "CameraModule.h"
#include <iostream>
#include "sc2api/sc2_api.h"
#include "sc2api/sc2_unit_filters.h"
#include "sc2api/sc2_proto_interface.h"

// Radius to detect groups of army units
Expand Down
122 changes: 7 additions & 115 deletions src/Observer.cpp
Original file line number Diff line number Diff line change
@@ -1,108 +1,15 @@
#include "CameraModule.h"
#include "Timer.hpp"
#include "Tools.h"

#include "sc2api/sc2_api.h"
#include "sc2api/sc2_gametypes.h"
#include "sc2utils/sc2_manage_process.h"
#include "sc2utils/sc2_arg_parser.h"

#include <iostream>
#include <windows.h>
#include <vector>

#include "CameraModule.h"
#include "Timer.hpp"
#include "sc2api/sc2_gametypes.h"

bool loadReplayPaths(std::string & name, std::vector<std::string> & v)
{
if (name.find("2Replay") != std::string::npos)
{
v.push_back(name);
return false;
}

std::string pattern(name);
pattern.append("\\*");
WIN32_FIND_DATAA data;
HANDLE hFind;
if ((hFind = FindFirstFileA(pattern.c_str(), &data)) != INVALID_HANDLE_VALUE)
{
do
{
if (data.dwFileAttributes != 16lu)
{
const std::string fileName = name + "/" + data.cFileName;
if (std::find(v.begin(), v.end(), fileName) == v.end())
{
v.push_back(fileName);
}
}
}
while (FindNextFileA(hFind, &data) != 0);
FindClose(hFind);
}
return true;
}

void pressDKey()
{
auto hwnd = FindWindow(NULL, TEXT("StarCraft II"));
if (hwnd != 0)
{
SetForegroundWindow(hwnd);
SetFocus(hwnd);
SetActiveWindow(hwnd);
INPUT ip;
ip.type = INPUT_KEYBOARD;
ip.ki.wScan = 0; // hardware scan code for key
ip.ki.time = 0;
ip.ki.dwExtraInfo = 0;

// Press the "D" key
ip.ki.wVk = 0x44; // virtual-key code for the "D" key
ip.ki.dwFlags = 0; // 0 for key press
SendInput(1, &ip, sizeof(INPUT));

// Release the "D" key.
ip.ki.dwFlags = KEYEVENTF_KEYUP; // KEYEVENTF_KEYUP for key release
SendInput(1, &ip, sizeof(INPUT));
}
}

void pressAltEnter()
{
auto hwnd = FindWindow(NULL, TEXT("StarCraft II"));
if (hwnd != 0)
{
SetForegroundWindow(hwnd);
SetFocus(hwnd);
SetActiveWindow(hwnd);
INPUT ip;
ip.type = INPUT_KEYBOARD;
ip.ki.wScan = 0; // hardware scan code for key
ip.ki.time = 0;
ip.ki.dwExtraInfo = 0;

// Press keys
ip.ki.dwFlags = 0; // 0 for key press

// Press the "alt" key
ip.ki.wVk = 0x12; // virtual-key code for the "alt" key
SendInput(1, &ip, sizeof(INPUT));

// Press the "enter" key
ip.ki.wVk = 0x0D; // virtual-key code for the "alt" key
SendInput(1, &ip, sizeof(INPUT));

// Release keys
ip.ki.dwFlags = KEYEVENTF_KEYUP; // KEYEVENTF_KEYUP for key release

// Release the "alt" key
ip.ki.wVk = 0x12;
SendInput(1, &ip, sizeof(INPUT));

// Release the "enter" key
ip.ki.wVk = 0x0D;
SendInput(1, &ip, sizeof(INPUT));
}
}

class Replay : public sc2::ReplayObserver
{
Expand Down Expand Up @@ -238,7 +145,7 @@ int main(int argc, char* argv[])
{
std::cout << "If you provide a directory please do not end the path with an '\'" << std::endl;
}
Sleep(30000);
sc2::SleepFor(30000);
continue;
}
const std::string replayFile = replayFiles[replayIndex];
Expand All @@ -254,22 +161,7 @@ int main(int argc, char* argv[])
std::cout << "Please provide the replay path as command line argument." << std::endl;
return 1;
}
if (!FindWindow(NULL, TEXT("StarCraft II")))
{
// Update once so that the game launches
coordinator.Update();
// Wait for launch and then Full screen mode
int counter = 0;
while (!FindWindow(NULL, TEXT("StarCraft II")) && counter < 10)
{
++counter;
sc2::SleepFor(500);
}
//if (counter < 10)
//{
// pressAltEnter();
//}
}
// FIXME(alkurbatov): Launch in fullscreen mode!
while (coordinator.Update() && !coordinator.AllGamesEnded())
{
}
Expand Down
14 changes: 8 additions & 6 deletions src/Timer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,20 @@
#include <thread>
#include <chrono>

#ifdef WIN32 // Windows system specific
#ifdef _WIN32 // Windows system specific
#include <windows.h>
#else // Unix based system specific
#include <sys/time.h>
#endif

#include <iostream>

class Timer
{
double startTimeInMicroSec; // starting time in micro-second
double endTimeInMicroSec; // ending time in micro-second
int stopped; // stop flag
#ifdef WIN32
#ifdef _WIN32
LARGE_INTEGER frequency; // ticks per second
LARGE_INTEGER startCount; //
LARGE_INTEGER endCount; //
Expand All @@ -28,7 +30,7 @@ class Timer

Timer()
{
#ifdef WIN32
#ifdef _WIN32
QueryPerformanceFrequency(&frequency);
startCount.QuadPart = 0;
endCount.QuadPart = 0;
Expand All @@ -50,7 +52,7 @@ class Timer
{
stopped = 0; // reset stop flag

#ifdef WIN32
#ifdef _WIN32
QueryPerformanceCounter(&startCount);
#else
gettimeofday(&startCount, NULL);
Expand All @@ -61,7 +63,7 @@ class Timer
{
stopped = 1; // set timer stopped flag

#ifdef WIN32
#ifdef _WIN32
QueryPerformanceCounter(&endCount);
#else
gettimeofday(&endCount, NULL);
Expand All @@ -70,7 +72,7 @@ class Timer

double getElapsedTimeInMicroSec()
{
#ifdef WIN32
#ifdef _WIN32
if (!stopped)
QueryPerformanceCounter(&endCount);

Expand Down
9 changes: 9 additions & 0 deletions src/Tools.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#pragma once

#include <string>
#include <vector>


bool loadReplayPaths(std::string & name, std::vector<std::string> & v);

void pressDKey();
11 changes: 11 additions & 0 deletions src/ToolsUnix.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include "Tools.h"


bool loadReplayPaths(std::string &, std::vector<std::string> &)
{
return true;
}

void pressDKey()
{
}
60 changes: 60 additions & 0 deletions src/ToolsWindows.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#include "Tools.h"

#include <windows.h>


bool loadReplayPaths(std::string & name, std::vector<std::string> & v)
{
if (name.find("2Replay") != std::string::npos)
{
v.push_back(name);
return false;
}

std::string pattern(name);
pattern.append("\\*");
WIN32_FIND_DATAA data;
HANDLE hFind;
if ((hFind = FindFirstFileA(pattern.c_str(), &data)) != INVALID_HANDLE_VALUE)
{
do
{
if (data.dwFileAttributes != 16lu)
{
const std::string fileName = name + "/" + data.cFileName;
if (std::find(v.begin(), v.end(), fileName) == v.end())
{
v.push_back(fileName);
}
}
}
while (FindNextFileA(hFind, &data) != 0);
FindClose(hFind);
}
return true;
}

void pressDKey()
{
auto hwnd = FindWindow(NULL, TEXT("StarCraft II"));
if (hwnd != 0)
{
SetForegroundWindow(hwnd);
SetFocus(hwnd);
SetActiveWindow(hwnd);
INPUT ip;
ip.type = INPUT_KEYBOARD;
ip.ki.wScan = 0; // hardware scan code for key
ip.ki.time = 0;
ip.ki.dwExtraInfo = 0;

// Press the "D" key
ip.ki.wVk = 0x44; // virtual-key code for the "D" key
ip.ki.dwFlags = 0; // 0 for key press
SendInput(1, &ip, sizeof(INPUT));

// Release the "D" key.
ip.ki.dwFlags = KEYEVENTF_KEYUP; // KEYEVENTF_KEYUP for key release
SendInput(1, &ip, sizeof(INPUT));
}
}

0 comments on commit 07b4877

Please sign in to comment.