Skip to content
This repository has been archived by the owner on Jun 22, 2021. It is now read-only.

Commit

Permalink
Update Checker
Browse files Browse the repository at this point in the history
Added new update checker to notify of new releases
  • Loading branch information
Ciaran Fisher committed Sep 25, 2015
1 parent 3b6f8dd commit f257dba
Show file tree
Hide file tree
Showing 5 changed files with 192 additions and 32 deletions.
197 changes: 168 additions & 29 deletions Plugin/Plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@
#include <iphlpapi.h>
#include <sstream>
#include <assert.h>
#include <windows.h>
#include <wininet.h>

#pragma comment(lib, "Ws2_32.lib")
#pragma comment(lib, "Wininet")

//239.255.50.10
//5050
Expand All @@ -33,7 +36,7 @@ static SimpleRadio::Plugin plugin;
namespace SimpleRadio
{
const char* Plugin::NAME = "DCS-SimpleRadio";
const char* Plugin::VERSION = "1.1.4";
const char* Plugin::VERSION = "1.1.5";
const char* Plugin::AUTHOR = "Ciribob - GitHub.com/ciribob";
const char* Plugin::DESCRIPTION = "DCS-SimpleRadio ";
const char* Plugin::COMMAND_KEYWORD = "sr";
Expand Down Expand Up @@ -63,6 +66,8 @@ namespace SimpleRadio

void Plugin::start()
{
//start update check

this->listening = true;

//read registry key
Expand All @@ -71,8 +76,12 @@ namespace SimpleRadio
this->acceptor = thread(&Plugin::UDPListener, this);

this->udpCommandListener = thread(&Plugin::UDPCommandListener, this);

checkForUpdate();
}



LPCWSTR Plugin::getConfigPath()
{
char configPath[512];
Expand Down Expand Up @@ -133,6 +142,11 @@ namespace SimpleRadio
{
this->udpCommandListener.join();
}

if (this->updateThread.joinable())
{
this->updateThread.join();
}
}

void Plugin::setTeamSpeakFunctions(TS3Functions functions)
Expand Down Expand Up @@ -605,23 +619,6 @@ namespace SimpleRadio
// Failed to get talkFlag value, assume not talking
}

if (isTalking)
{
/*if (this->currentRadio == receiver)
{
for (int i = 0; i < sampleCount; ++i)
{
samples[i] = 0;
}
return;
}*/
}
else
{

}

//check both updates are valid
if (this->myClientData.isCurrent() && talkingClient.isCurrent())
{
Expand All @@ -636,7 +633,6 @@ namespace SimpleRadio

// std::ostringstream oss;
//oss << "Receiving On: " <<myRadio.frequency << " From "<<sendingRadio.frequency;

// this->teamspeak.printMessageToCurrentTab(oss.str().c_str());

if (myRadio.frequency == sendingRadio.frequency
Expand Down Expand Up @@ -704,10 +700,6 @@ namespace SimpleRadio

if (!canReceive)
{
/*for (int i = 0; i < sampleCount; ++i)
{
samples[i] = samples[i] * getVolume();
}*/

for (int i = 0; i < sampleCount; i++)
{
Expand All @@ -728,12 +720,20 @@ namespace SimpleRadio
for (int i = 0; i < sampleCount; i++)
{
for (int j = 0; j < channels; j++)

samples[i * channels + j] = samples[i * channels + j] * (myRadio.volume);
}
}
}


void Plugin::checkForUpdate()
{
//start update thread
if (this->updateThread.joinable())
{
//finish old thread
this->updateThread.join();
}
this->updateThread = thread(&Plugin::UpdateCheckThread, this);
}

int Plugin::recvfromTimeOutUDP(SOCKET socket, long sec, long usec)
Expand Down Expand Up @@ -810,7 +810,7 @@ namespace SimpleRadio

addr.sin_addr.s_addr = htonl(INADDR_ANY);

ReceivingSocket = mksocket(&addr,!this->switchToUnicast);
ReceivingSocket = mksocket(&addr, !this->switchToUnicast);

/* use setsockopt() to request that the kernel join a multicast group */

Expand Down Expand Up @@ -901,6 +901,91 @@ namespace SimpleRadio
// Back to the system
}

void Plugin::UpdateCheckThread()
{

DWORD r = 0;
if (!InternetGetConnectedState(&r, 0))
return;
if (r & INTERNET_CONNECTION_OFFLINE)
return;

HINTERNET httpInit = InternetOpen(L"DCS-SimpleRadio-Updater", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
HINTERNET httpConnection = InternetConnect(httpInit, L"github.com", INTERNET_DEFAULT_HTTPS_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
//dont follow githubs redirect as the redirect gives us the latest version
HINTERNET httpRequest = HttpOpenRequest(httpConnection, NULL, L"/ciribob/DCS-SimpleRadio/releases/latest", NULL, NULL, NULL, INTERNET_FLAG_SECURE | INTERNET_FLAG_NO_AUTO_REDIRECT, 0);

if (HttpSendRequest(httpRequest, NULL, 0, NULL, 0))
{
DWORD bufferSize = 512;
LPVOID locationBuffer = new char[bufferSize];

//while (InternetReadFile(File, &ch, 1, &dwBytes))
if (HttpQueryInfo(httpRequest, HTTP_QUERY_LOCATION, locationBuffer, &bufferSize, NULL))
{
//its a 2 Byte CHAR! so char[0]=h and char[1]=0 !!
WCHAR *locationStr = reinterpret_cast<WCHAR *>(locationBuffer);
locationStr[bufferSize] = 0; //add terminator

//convert to widestring
std::wstring ws(locationStr);
//convert to normal string
std::string str(ws.begin(), ws.end());

//get the latest version number
int index = str.find_last_of('/');

std::string version = str.substr(index + 1);
std::string currentVersion(VERSION);

if (currentVersion == version)
{
char buffer[256] = { 0 };
sprintf_s(buffer, 256, "You are using the latest version of DCS-SimpleRadio: %s", VERSION);

this->teamspeak.printMessageToCurrentTab(buffer);
//std::cout << "OK! Same Version";
}
else
{
char buffer[256] = { 0 };
sprintf_s(buffer, 256, "Update to DCS-SimpleRadio Available. Latest Version %s - Download at https://github.com/ciribob/DCS-SimpleRadio/releases/latest", VERSION);

this->teamspeak.printMessageToCurrentTab(buffer);
//display alert box
int result = MessageBox(
NULL,
(LPCWSTR)L"Update to DCS-SimpleRadio Available\nDo you want to download it now?",
(LPCWSTR)L"Update Available",
MB_ICONWARNING | MB_YESNO
);

switch (result)
{
case IDYES:
// launch browser
ShellExecute(NULL, L"open", L"https://github.com/ciribob/DCS-SimpleRadio/releases/latest",
NULL, NULL, SW_SHOWNORMAL);
break;
case IDNO:

break;
default:

break;
}

//std::cout << "Newer version available";
}
}

delete[] locationBuffer;
}

InternetCloseHandle(httpRequest);
InternetCloseHandle(httpConnection);
InternetCloseHandle(httpInit);
}
/*
Determine if we should send a metadata update to the TS3 Server
*/
Expand Down Expand Up @@ -1011,7 +1096,7 @@ namespace SimpleRadio
{
addr.sin_port = htons(5061);
}

addr.sin_addr.s_addr = htonl(INADDR_ANY);

ReceivingSocket = mksocket(&addr, !this->switchToUnicast);
Expand Down Expand Up @@ -1060,7 +1145,7 @@ namespace SimpleRadio
case 1:
this->teamSpeakControlledClientData.radio[updateCommand.radio].frequency += updateCommand.freq;
break;
case 2:
case 2:
this->teamSpeakControlledClientData.radio[updateCommand.radio].volume = updateCommand.volume;
break;
case 3:
Expand All @@ -1071,7 +1156,7 @@ namespace SimpleRadio

}
}

}


Expand Down Expand Up @@ -1305,4 +1390,58 @@ void ts3plugin_initHotkeys(struct PluginHotkey*** hotkeys) {
}
void ts3plugin_onHotkeyEvent(const char* keyword) {
plugin.onHotKeyEvent(keyword);
}





/*
* Initialize plugin menus.
* This function is called after ts3plugin_init and ts3plugin_registerPluginID. A pluginID is required for plugin menus to work.
* Both ts3plugin_registerPluginID and ts3plugin_freeMemory must be implemented to use menus.
* If plugin menus are not used by a plugin, do not implement this function or return NULL.
*/
void ts3plugin_initMenus(struct PluginMenuItem*** menuItems, char** menuIcon) {
/*
* Create the menus
* There are three types of menu items:
* - PLUGIN_MENU_TYPE_CLIENT: Client context menu
* - PLUGIN_MENU_TYPE_CHANNEL: Channel context menu
* - PLUGIN_MENU_TYPE_GLOBAL: "Plugins" menu in menu bar of main window
*
* Menu IDs are used to identify the menu item when ts3plugin_onMenuItemEvent is called
*
* The menu text is required, max length is 128 characters
*
* The icon is optional, max length is 128 characters. When not using icons, just pass an empty string.
* Icons are loaded from a subdirectory in the TeamSpeak client plugins folder. The subdirectory must be named like the
* plugin filename, without dll/so/dylib suffix
* e.g. for "test_plugin.dll", icon "1.png" is loaded from <TeamSpeak 3 Client install dir>\plugins\test_plugin\1.png
*/


BEGIN_CREATE_MENUS(1)
CREATE_MENU_ITEM(PLUGIN_MENU_TYPE_GLOBAL, 1, "Check For Update", "");
END_CREATE_MENUS;


/* All memory allocated in this function will be automatically released by the TeamSpeak client later by calling ts3plugin_freeMemory */
}

void ts3plugin_onMenuItemEvent(uint64 serverConnectionHandlerID, enum PluginMenuType type, int menuItemID, uint64 selectedItemID) {
switch (type) {
case PLUGIN_MENU_TYPE_GLOBAL:
/* Global menu item was triggered. selectedItemID is unused and set to zero. */
switch (menuItemID) {
case 1:
plugin.checkForUpdate();
break;
default:
break;
}
break;
default:
break;
}
}
21 changes: 21 additions & 0 deletions Plugin/Plugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ namespace SimpleRadio

static void processMessage(const char* message);

void checkForUpdate();

private:
char* pluginId;

Expand All @@ -70,6 +72,8 @@ namespace SimpleRadio

std::thread udpCommandListener;

std::thread updateThread;

bool allowNonPlayers;

bool switchToUnicast;
Expand All @@ -84,6 +88,8 @@ namespace SimpleRadio

void UDPListener();

void UpdateCheckThread();

bool shouldSendUpdate(ClientMetaData & clientMetaData);

void processUDPUpdate(ClientMetaData & clientMetaData);
Expand Down Expand Up @@ -220,6 +226,21 @@ extern "C"
DLL_EXPORT void ts3plugin_onHotkeyEvent(const char* keyword);
DLL_EXPORT void ts3plugin_onHotkeyRecordedEvent(const char* keyword, const char* key);
DLL_EXPORT void ts3plugin_onClientDisplayNameChanged(uint64 serverConnectionHandlerID, anyID clientID, const char* displayName, const char* uniqueClientIdentifier);

/* Helper function to create a menu item */
static struct PluginMenuItem* createMenuItem(enum PluginMenuType type, int id, const char* text, const char* icon) {
struct PluginMenuItem* menuItem = (struct PluginMenuItem*)malloc(sizeof(struct PluginMenuItem));
menuItem->type = type;
menuItem->id = id;
strcpy_s(menuItem->text, PLUGIN_MENU_BUFSZ, text);
strcpy_s(menuItem->icon, PLUGIN_MENU_BUFSZ, icon);
return menuItem;
}

/* Some makros to make the code to create menu items a bit more readable */
#define BEGIN_CREATE_MENUS(x) const size_t sz = x + 1; size_t n = 0; *menuItems = (struct PluginMenuItem**)malloc(sizeof(struct PluginMenuItem*) * sz);
#define CREATE_MENU_ITEM(a, b, c, d) (*menuItems)[n++] = createMenuItem(a, b, c, d);
#define END_CREATE_MENUS (*menuItems)[n++] = NULL; assert(n == sz);
}

#endif
Binary file modified Plugin/Plugin.rc
Binary file not shown.
4 changes: 2 additions & 2 deletions RadioGui/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.1.4.0")]
[assembly: AssemblyFileVersion("1.1.4.0")]
[assembly: AssemblyVersion("1.1.5.0")]
[assembly: AssemblyFileVersion("1.1.5.0")]
2 changes: 1 addition & 1 deletion Scripts/DCS-SimpleRadio/SimpleRadioInit.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
-- Version 1.1.4
-- Version 1.1.5
SR = {}

SR.unicast = false -- if you've setup DCS Correctly and the plugin isn't talking to DCS,
Expand Down

0 comments on commit f257dba

Please sign in to comment.