Skip to content

Commit acb3104

Browse files
committed
Adapt for managed plugin private data locations.
1 parent 7f875a7 commit acb3104

File tree

4 files changed

+114
-8
lines changed

4 files changed

+114
-8
lines changed

CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -306,9 +306,9 @@ SET(SRC_S63
306306
src/s63chart.h
307307
src/mygeom63.h
308308
src/mygeom63.cpp
309-
#src/tri.c
310309
src/TexFont.cpp
311310
src/TexFont.h
311+
src/InstallDirs.cpp
312312
)
313313

314314
SET(SRC_CPL

src/InstallDirs.cpp

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
#include "InstallDirs.h"
2+
3+
#include <iostream>
4+
#include <fstream>
5+
#include <sstream>
6+
#include <unordered_map>
7+
#include <vector>
8+
9+
#include <wx/filename.h>
10+
#include <wx/log.h>
11+
12+
// FIXME: Missing includes in ocpn_plugin.h
13+
#include <wx/dcmemory.h>
14+
#include <wx/dialog.h>
15+
#include <wx/event.h>
16+
#include <wx/menuitem.h>
17+
#include <wx/gdicmn.h>
18+
#include <wx/platinfo.h>
19+
20+
#include "ocpn_plugin.h"
21+
22+
23+
typedef std::unordered_map<std::string, std::string> pathmap_t;
24+
25+
26+
std::vector<std::string> split(std::string s, char delimiter)
27+
{
28+
using namespace std;
29+
30+
vector<string> tokens;
31+
size_t start = s.find_first_not_of(delimiter);
32+
size_t end = start;
33+
while (start != string::npos) {
34+
end = s.find(delimiter, start);
35+
tokens.push_back(s.substr(start, end - start));
36+
start = s.find_first_not_of(delimiter, end);
37+
}
38+
return tokens;
39+
}
40+
41+
42+
inline bool exists(const std::string& name) {
43+
wxFileName fn(name.c_str());
44+
if (!fn.IsOk()) {
45+
return false;
46+
}
47+
return fn.FileExists();
48+
}
49+
50+
51+
std::string find_in_path(std::string binary)
52+
{
53+
using namespace std;
54+
55+
wxString wxPath;
56+
wxGetEnv("PATH", &wxPath);
57+
string path(wxPath.c_str());
58+
59+
auto const osSystemId = wxPlatformInfo::Get().GetOperatingSystemId();
60+
char delimiter = ':';
61+
if (osSystemId & wxOS_WINDOWS) {
62+
delimiter = ';';
63+
binary += ".exe";
64+
}
65+
vector<string> elements = split(path, delimiter);
66+
for (auto element: elements) {
67+
string filename = element + "/" + binary;
68+
if (exists(filename)) {
69+
return filename;
70+
}
71+
}
72+
return "";
73+
}

src/InstallDirs.h

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#include <string>
2+
#include <wx/string.h>
3+
4+
#if 0
5+
/**
6+
* Return binary directory used when installing plugin, without
7+
* trailing separator.
8+
*
9+
* - Parameter: name: Name of plugin.
10+
* - Return: Binary dir used when installing plugin or "" if not found.
11+
*/
12+
wxString getInstallationBindir(const char* name);
13+
#endif
14+
15+
/*
16+
* Look for binary with given basename (no extension) in PATH.
17+
*
18+
* @return path to binary if found, else 0.
19+
*
20+
*/
21+
std::string find_in_path(std::string binary);

src/s63_pi.cpp

+19-7
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
#include "json_defs.h"
4949
#include "jsonwriter.h"
5050
#include "jsonreader.h"
51+
#include "InstallDirs.h"
5152

5253
#ifdef __WXOSX__
5354
#include "GL/gl.h"
@@ -363,20 +364,31 @@ s63_pi::s63_pi(void *ppimgr)
363364
wxFileName fn_exe(GetOCPN_ExePath());
364365

365366
// Specify the location of the OCPNsenc helper.
366-
g_sencutil_bin = fn_exe.GetPath( wxPATH_GET_VOLUME | wxPATH_GET_SEPARATOR) + _T("OCPNsenc");
367-
367+
//g_sencutil_bin = fn_exe.GetPath( wxPATH_GET_VOLUME | wxPATH_GET_SEPARATOR) + _T("OCPNsenc");
368+
//g_sencutil_bin = GetPluginDataDir("s63_pi") + _T("/OCPNsenc");
369+
368370

369371
#ifdef __WXMSW__
370-
g_sencutil_bin = _T("\"") + fn_exe.GetPath( wxPATH_GET_VOLUME | wxPATH_GET_SEPARATOR) +
371-
_T("plugins\\s63_pi\\OCPNsenc.exe\"");
372+
//g_sencutil_bin = _T("\"") + fn_exe.GetPath( wxPATH_GET_VOLUME | wxPATH_GET_SEPARATOR) + _T("plugins\\s63_pi\\OCPNsenc.exe\"");
373+
g_sencutil_bin = GetPluginDataDir("s63_pi") + _T("\\OCPNsenc.exe");
374+
372375
#endif
373376

374377
#ifdef __WXOSX__
375-
fn_exe.RemoveLastDir();
376-
g_sencutil_bin = _T("\"") + fn_exe.GetPath( wxPATH_GET_VOLUME | wxPATH_GET_SEPARATOR) +
377-
_T("PlugIns/s63_pi/OCPNsenc\"");
378+
// fn_exe.RemoveLastDir();
379+
// g_sencutil_bin = _T("\"") + fn_exe.GetPath( wxPATH_GET_VOLUME | wxPATH_GET_SEPARATOR) + _T("PlugIns/s63_pi/OCPNsenc\"");
378380
#endif
379381

382+
if (!wxFileExists(g_sencutil_bin)) {
383+
std::string path(find_in_path("OCPNsenc"));
384+
if (path == "") {
385+
wxLogWarning("Cannot locate OCPNsenc binary in $PATH");
386+
}
387+
else {
388+
g_sencutil_bin = wxString(path.c_str());
389+
}
390+
}
391+
380392
g_bSENCutil_valid = false; // not confirmed yet
381393

382394

0 commit comments

Comments
 (0)