Skip to content

Commit

Permalink
proto/freetrack: implement more dll location controls
Browse files Browse the repository at this point in the history
  • Loading branch information
sthalik committed Aug 21, 2021
1 parent b615cc6 commit dae6a98
Show file tree
Hide file tree
Showing 8 changed files with 333 additions and 109 deletions.
183 changes: 126 additions & 57 deletions proto-ft/ftnoir_ftcontrols.ui
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,10 @@
<rect>
<x>0</x>
<y>0</y>
<width>508</width>
<height>232</height>
<width>533</width>
<height>326</height>
</rect>
</property>
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="minimumSize">
<size>
<width>0</width>
<height>0</height>
</size>
</property>
<property name="windowTitle">
<string>freetrack protocol settings</string>
</property>
Expand All @@ -42,6 +30,13 @@
<bool>false</bool>
</property>
<layout class="QGridLayout" name="gridLayout_2">
<item row="5" column="0">
<widget class="QDialogButtonBox" name="buttonBox">
<property name="standardButtons">
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
</property>
</widget>
</item>
<item row="0" column="0">
<widget class="QGroupBox" name="groupBox_3">
<property name="sizePolicy">
Expand Down Expand Up @@ -95,64 +90,138 @@
</layout>
</widget>
</item>
<item row="1" column="0">
<item row="1" column="0" rowspan="2">
<widget class="QGroupBox" name="groupBox_4">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="title">
<string>Repair NPClient location</string>
<string>Library location</string>
</property>
<property name="alignment">
<set>Qt::AlignJustify|Qt::AlignTop</set>
</property>
<property name="flat">
<bool>false</bool>
</property>
<layout class="QHBoxLayout" name="horizontalLayout_2">
<property name="spacing">
<number>9</number>
</property>
<item>
<widget class="QPushButton" name="bntLocateNPClient">
<property name="text">
<string>Locate DLL</string>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="label_10">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Minimum">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>Replace the registry entry if you want to use other software with the NPClient protocol and it doesn't work automatically.
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="0">
<widget class="QWidget" name="widget" native="true">
<layout class="QHBoxLayout" name="horizontalLayout">
<property name="topMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<item>
<widget class="QPushButton" name="bntLocateNPClient">
<property name="text">
<string>Locate DLL</string>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="label_10">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Maximum">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>Replace the registry entry if you want to use other software with the NPClient protocol and it doesn't work automatically.

Starting tracking will again overwrite the DLL locations.</string>
</property>
<property name="wordWrap">
<bool>true</bool>
</property>
</property>
<property name="wordWrap">
<bool>true</bool>
</property>
</widget>
</item>
</layout>
</widget>
</item>
<item row="1" column="0">
<widget class="QWidget" name="widget" native="true">
<layout class="QHBoxLayout" name="horizontalLayout_2">
<property name="spacing">
<number>6</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<item>
<widget class="QCheckBox" name="enable_custom_location">
<property name="toolTip">
<string>Useful for titles like Elite: Dangerous that require the library to reside in a specified location. Use this to avoid relocating your opentrack installation.</string>
</property>
<property name="text">
<string>Custom location</string>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="custom_location">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Maximum">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="set_custom_location">
<property name="sizePolicy">
<sizepolicy hsizetype="Maximum" vsizetype="Maximum">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>Browse...</string>
</property>
</widget>
</item>
</layout>
</widget>
</item>
<item row="2" column="0">
<widget class="QWidget" name="widget_2" native="true">
<layout class="QHBoxLayout" name="horizontalLayout_4">
<property name="spacing">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<item>
<widget class="QCheckBox" name="ephemeral_registry_entry">
<property name="toolTip">
<string>This is useful when you're only evaluating opentrack, and haven't yet decided to use it all the time.</string>
</property>
<property name="text">
<string>Clear location when tracking is stopped</string>
</property>
</widget>
</item>
</layout>
</widget>
</item>
</layout>
</widget>
</item>
<item row="2" column="0">
<widget class="QDialogButtonBox" name="buttonBox">
<property name="standardButtons">
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
</property>
</widget>
</item>
</layout>
</widget>
<tabstops>
<tabstop>cbxSelectInterface</tabstop>
<tabstop>bntLocateNPClient</tabstop>
<tabstop>enable_custom_location</tabstop>
<tabstop>custom_location</tabstop>
<tabstop>set_custom_location</tabstop>
<tabstop>ephemeral_registry_entry</tabstop>
</tabstops>
<resources>
<include location="ft-protocol.qrc"/>
<include location="ft-protocol.qrc"/>
Expand Down
73 changes: 45 additions & 28 deletions proto-ft/ftnoir_protocol_ft.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,23 @@

#include "ftnoir_protocol_ft.h"
#include "csv/csv.h"
#include <QDir>

#include <cstddef>
#include <cmath>
#include <windows.h>

freetrack::~freetrack()
{
dummyTrackIR.close();
//dummyTrackIR.kill(); dummyTrackIR.waitForFinished();
if (s.ephemeral_library_location)
{
QSettings settings_ft("Freetrack", "FreetrackClient");
QSettings settings_npclient("NaturalPoint", "NATURALPOINT\\NPClient Location");

settings_ft.setValue("Path", "");
settings_npclient.setValue("Path", "");
}
}

static_assert(sizeof(LONG) == sizeof(std::int32_t));
Expand Down Expand Up @@ -133,48 +142,56 @@ void freetrack::start_dummy() {
dummyTrackIR.start();
}

void freetrack::set_protocols(bool ft, bool npclient)
module_status freetrack::set_protocols()
{
static const QString program_dir = OPENTRACK_BASE_PATH + OPENTRACK_LIBRARY_PATH;

// Registry settings (in HK_USER)
QSettings settings_ft("Freetrack", "FreetrackClient");
QSettings settings_npclient("NaturalPoint", "NATURALPOINT\\NPClient Location");

if (ft)
settings_ft.setValue("Path", program_dir);
else
settings_ft.setValue("Path", "");
QString location = *s.custom_location_pathname;
const auto selection = *s.used_interface;

bool use_freetrack = ~selection & settings::enable_freetrack,
use_npclient = ~selection & settings::enable_npclient;

if (npclient)
settings_npclient.setValue("Path", program_dir);
if (!s.use_custom_location || s.custom_location_pathname->isEmpty() || !QDir{s.custom_location_pathname}.exists())
location = program_dir;
else
settings_npclient.setValue("Path", "");
{
bool copy = true;

if (use_npclient && !QFile{location + "/NPClient.dll"}.exists())
copy &= QFile::copy(program_dir + "/NPClient.dll", location + "/NPClient.dll");
if (use_npclient && !QFile{location + "/NPClient64.dll"}.exists())
copy &= QFile::copy(program_dir + "/NPClient64.dll", location + "/NPClient64.dll");
if (use_freetrack && !QFile{location + "/freetrackclient.dll"}.exists())
copy &= QFile::copy(program_dir + "/freetrackclient.dll", location + "/freetrackclient.dll");
if (use_freetrack && !QFile{location + "/freetrackclient64.dll"}.exists())
copy &= QFile::copy(program_dir + "/freetrackclient64.dll", location + "/freetrackclient64.dll");

if (!copy)
return {tr("Can't copy library to selected custom location '%1'").arg(s.custom_location_pathname)};
}

if (!location.endsWith('/'))
location += '/';
location.replace('\\', '/');

settings_ft.setValue("Path", use_freetrack ? location : "");
settings_npclient.setValue("Path", use_npclient ? location : "");

return {};
}

module_status freetrack::initialize()
{
if (!shm.success())
return error(tr("Can't load freetrack memory mapping"));

bool use_ft = false, use_npclient = false;

switch (s.intUsedInterface) {
case 0:
use_ft = true;
use_npclient = true;
break;
case 1:
use_ft = true;
break;
case 2:
use_npclient = true;
break;
default:
break;
}

set_protocols(use_ft, use_npclient);
if (auto ret = set_protocols(); !ret.is_ok())
return ret;

pMemData->data.DataID = 1;
pMemData->data.CamWidth = 100;
Expand All @@ -195,7 +212,7 @@ module_status freetrack::initialize()
store(pMemData->table_ints[k], 0);

// more games need the dummy executable than previously thought
if (use_npclient)
if (~s.used_interface & settings::enable_npclient)
start_dummy();

return status_ok();
Expand Down
16 changes: 10 additions & 6 deletions proto-ft/ftnoir_protocol_ft.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,12 @@
using namespace options;

struct settings : opts {
value<int> intUsedInterface;
settings() :
opts("proto-freetrack"),
intUsedInterface(b, "used-interfaces", 0)
{}
enum enable_status { enable_both, enable_freetrack, enable_npclient, };
value<int> used_interface{b, "used-interfaces", (int)enable_both};
value<bool> ephemeral_library_location{b, "ephemeral-library-location", false};
value<bool> use_custom_location{b, "use-custom-location", false};
value<QString> custom_location_pathname{b, "custom-library-location", {}};
settings() : opts("proto-freetrack") {}
};

class freetrack : TR, public IProtocol
Expand All @@ -58,7 +59,9 @@ class freetrack : TR, public IProtocol
void start_dummy();

public:
static void set_protocols(bool ft, bool npclient);
enum class status { starting, stopping };
[[nodiscard]] module_status set_protocols();
void clear_protocols();
};

class FTControls: public IProtocolDialog
Expand All @@ -75,6 +78,7 @@ private slots:
void selectDLL();
void doOK();
void doCancel();
void set_custom_location();
};

class freetrackDll : public Metadata
Expand Down
Loading

0 comments on commit dae6a98

Please sign in to comment.