Skip to content

Commit 9f8c450

Browse files
committed
Big update and various things (not ready!)
1 parent 89516b4 commit 9f8c450

12 files changed

Lines changed: 337 additions & 48 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,3 +230,4 @@ $RECYCLE.BIN/
230230
# Windows shortcuts
231231
*.lnk
232232
/.vs/CodeStats/v15/Server/sqlite3
233+
/.vs/CodeStats/v16/Server/sqlite3

CodeStats.sln

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11

22
Microsoft Visual Studio Solution File, Format Version 12.00
3-
# Visual Studio 14
4-
VisualStudioVersion = 14.0.25420.1
3+
# Visual Studio 15
4+
VisualStudioVersion = 15.0.28307.421
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CodeStats", "CodeStats\CodeStats.csproj", "{E56F6E12-089C-40ED-BCFD-923E5FA121A1}"
77
EndProject
8+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CodeStatsUpdater", "CodeStatsUpdater\CodeStatsUpdater.csproj", "{A2A45792-7E19-4C34-9F30-7E7F9F007924}"
9+
EndProject
810
Global
911
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1012
Debug|Any CPU = Debug|Any CPU
@@ -21,8 +23,19 @@ Global
2123
{E56F6E12-089C-40ED-BCFD-923E5FA121A1}.Release|Any CPU.Build.0 = Release|Any CPU
2224
{E56F6E12-089C-40ED-BCFD-923E5FA121A1}.Release|x64.ActiveCfg = Release|x64
2325
{E56F6E12-089C-40ED-BCFD-923E5FA121A1}.Release|x64.Build.0 = Release|x64
26+
{A2A45792-7E19-4C34-9F30-7E7F9F007924}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
27+
{A2A45792-7E19-4C34-9F30-7E7F9F007924}.Debug|Any CPU.Build.0 = Debug|Any CPU
28+
{A2A45792-7E19-4C34-9F30-7E7F9F007924}.Debug|x64.ActiveCfg = Debug|Any CPU
29+
{A2A45792-7E19-4C34-9F30-7E7F9F007924}.Debug|x64.Build.0 = Debug|Any CPU
30+
{A2A45792-7E19-4C34-9F30-7E7F9F007924}.Release|Any CPU.ActiveCfg = Release|Any CPU
31+
{A2A45792-7E19-4C34-9F30-7E7F9F007924}.Release|Any CPU.Build.0 = Release|Any CPU
32+
{A2A45792-7E19-4C34-9F30-7E7F9F007924}.Release|x64.ActiveCfg = Release|Any CPU
33+
{A2A45792-7E19-4C34-9F30-7E7F9F007924}.Release|x64.Build.0 = Release|Any CPU
2434
EndGlobalSection
2535
GlobalSection(SolutionProperties) = preSolution
2636
HideSolutionNode = FALSE
2737
EndGlobalSection
38+
GlobalSection(ExtensibilityGlobals) = postSolution
39+
SolutionGuid = {266D351E-5EE6-4F7A-8E9B-91275C9E24A1}
40+
EndGlobalSection
2841
EndGlobal

CodeStats/CodeStats.csproj

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,17 +59,22 @@
5959
<ErrorReport>prompt</ErrorReport>
6060
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
6161
</PropertyGroup>
62+
<PropertyGroup>
63+
<ApplicationManifest>app.manifest</ApplicationManifest>
64+
</PropertyGroup>
6265
<ItemGroup>
6366
<Reference Include="System" />
6467
<Reference Include="System.Drawing" />
6568
<Reference Include="System.IO.Compression.FileSystem" />
69+
<Reference Include="System.Net.Http" />
6670
<Reference Include="System.Web.Extensions" />
6771
<Reference Include="System.Windows.Forms" />
6872
<Reference Include="System.Xml" />
6973
</ItemGroup>
7074
<ItemGroup>
7175
<Compile Include="ConfigFile.cs" />
7276
<Compile Include="Constants.cs" />
77+
<Compile Include="CustomMappingConfig.cs" />
7378
<Compile Include="Pulse.cs" />
7479
<Compile Include="Logger.cs" />
7580
<Compile Include="NativeMethods.cs" />
@@ -110,6 +115,7 @@
110115
</Compile>
111116
<Compile Include="PluginInfrastructure\UnmanagedExports.cs" />
112117
<Compile Include="RunProcess.cs" />
118+
<Compile Include="Updater.cs" />
113119
</ItemGroup>
114120
<ItemGroup>
115121
<EmbeddedResource Include="Forms\ApiKeyForm.resx">
@@ -129,6 +135,10 @@
129135
<EmbeddedResource Include="Resources\codestats-16.png" />
130136
<EmbeddedResource Include="Resources\extension_mapping.json" />
131137
</ItemGroup>
138+
<ItemGroup>
139+
<None Include="app.config" />
140+
<None Include="app.manifest" />
141+
</ItemGroup>
132142
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
133143
<Import Project="$(MSBuildProjectDirectory)\PluginInfrastructure\DllExport\NppPlugin.DllExport.targets" />
134144
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.

CodeStats/CodeStatsPackage.cs

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Drawing;
66
using System.IO;
77
using System.Net;
8+
using System.Net.Http;
89
using System.Reflection;
910
using System.Runtime.InteropServices;
1011
using System.Text;
@@ -74,6 +75,17 @@ internal static void CommandMenuInit()
7475

7576
private static void InitializeAsync()
7677
{
78+
if (System.Net.ServicePointManager.SecurityProtocol != 0)
79+
{
80+
// If the value is not set to 0 (SystemDefault), disable old protocols and make sure TLS 1.3, 1.2, 1.1 are enabled
81+
System.Net.ServicePointManager.SecurityProtocol &= ~SecurityProtocolType.Ssl3;
82+
System.Net.ServicePointManager.SecurityProtocol &= ~SecurityProtocolType.Tls;
83+
System.Net.ServicePointManager.SecurityProtocol |= (SecurityProtocolType)12288 | SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11;
84+
}
85+
//System.Net.ServicePointManager.SecurityProtocol = (SecurityProtocolType)0; // SystemDefault, we don't use this since it's supported only since .NET 4.7
86+
87+
//Updater.RenameTest();
88+
//Updater.SignatureVerificationTest();
7789
try
7890
{
7991
// Delete existing log file to save space
@@ -214,6 +226,11 @@ internal static void SetStatusBarDocType(string str)
214226

215227
public static void OnNotification(ScNotification notification)
216228
{
229+
if (nppStarted && notification.Header.Code == (uint)SciMsg.SCN_MODIFIED)
230+
{
231+
Logger.Debug("[notification] SCN_MODIFIED, notification code: 0x" + notification.Header.Code.ToString("x"));
232+
}
233+
217234
// TODO: NPPN_SHUTDOWN - save unpulsed to config (or do it in plugin unload sequence rather on bottom of this file)
218235
if (notification.Header.Code == (uint)NppMsg.NPPN_LANGCHANGED) // Does not seem to be triggered?
219236
{
@@ -261,7 +278,7 @@ public static void OnNotification(ScNotification notification)
261278
int SC_PERFORMED_USER_AND_SC_MOD_DELETETEXT = (int)SciMsg.SC_PERFORMED_USER | (int)SciMsg.SC_MOD_DELETETEXT;
262279
if (nppStarted && notification.Header.Code == (uint)SciMsg.SCN_MODIFIED && ((notification.ModificationType & SC_PERFORMED_USER_AND_SC_MOD_DELETETEXT) == SC_PERFORMED_USER_AND_SC_MOD_DELETETEXT))
263280
{
264-
Logger.Debug("[notification] SCN_MODIFIED #2");
281+
Logger.Debug("[notification] SCN_MODIFIED #2, notification code: 0x" + notification.Header.Code.ToString("x"));
265282
// Looks like we can use this to track deleted stuff once Notepad++ is started and ready
266283
// It doesn't trigger on file close either, unlike on open with SC_MOD_INSERTTEXT, so we only use this, and SCN_CHARADDED for inserts
267284
// It will skip Ctrl+V if it wasn't pasted on some existing text, but never mind, it is still counting the most we want
@@ -325,7 +342,7 @@ private static void ProcessPulses(object sender, ElapsedEventArgs e)
325342
});
326343
}
327344

328-
private static void ProcessPulses()
345+
private static async Task ProcessPulses()
329346
{
330347
if ( pulseQueue != null && ( (currentPulse != null && !currentPulse.isEmpty()) || !pulseQueue.IsEmpty ) && EnoughTimePassed(DateTime.Now) )
331348
{
@@ -371,7 +388,8 @@ private static void ProcessPulses()
371388
{
372389
string json = jsonSerializer.Serialize(result);
373390
Logger.Debug("Pulsing " + json);
374-
string HtmlResult = client.UploadString(URL, json);
391+
//string HtmlResult = client.UploadString(URL, json);
392+
string HtmlResult = await client.UploadStringTaskAsync(URL, json);
375393
_lastPulse = DateTime.Now;
376394
if (!HtmlResult.Contains(@"""ok""") && !HtmlResult.Contains(@"success"))
377395
{
@@ -392,8 +410,8 @@ private static void ProcessPulses()
392410
Logger.Error("Could not pulse (error 403). Please make sure you entered a valid API token in Code::Stats settings.", ex);
393411
if (!_hasAlreadyShownInvalidApiTokenMessage) // we want to inform user only once, and if they do not provide the token, let's not bomb him with error each time after they type something
394412
{
395-
MessageBox.Show("Could not pulse. Please make sure you entered a valid API token in Code::Stats settings.\nAll recorded XP from this session will be lost if you do not provide the correct API token!", "Code::Stats – error 403", MessageBoxButtons.OK, MessageBoxIcon.Error);
396413
_hasAlreadyShownInvalidApiTokenMessage = true;
414+
MessageBox.Show("Could not pulse. Please make sure you entered a valid API token in Code::Stats settings.\nAll recorded XP from this session will be lost if you do not provide the correct API token!", "Code::Stats – error 403", MessageBoxButtons.OK, MessageBoxIcon.Error);
397415
PromptApiKey();
398416
}
399417
}
@@ -601,7 +619,7 @@ private static void SettingsFormOnConfigSaved(object sender, EventArgs eventArgs
601619
GetSettings();
602620
}
603621

604-
private static void GetSettings()
622+
public static void GetSettings()
605623
{
606624
_CodeStatsConfigFile.Read();
607625
ApiKey = _CodeStatsConfigFile.ApiKey;
@@ -627,10 +645,14 @@ private static void SettingsPopup()
627645

628646
public static void ReportStats()
629647
{
630-
var client = new WebClient { Proxy = CodeStatsPackage.GetProxy() };
631-
client.Headers[HttpRequestHeader.UserAgent] = Constants.PluginUserAgent;
632-
string HtmlResult = client.DownloadString("https://p0358.net/codestats/report.php?pluginver=" + Constants.PluginVersion + "&cid=" + CodeStatsPackage.Guid + "&editorname=" + Constants.EditorName + "&editorver=" + Constants.EditorVersion + "&is64process=" + ProcessorArchitectureHelper.Is64BitProcess.ToString().ToLowerInvariant() + "&is64sys=" + ProcessorArchitectureHelper.Is64BitOperatingSystem.ToString().ToLowerInvariant()); // expected response: ok
633-
if (HtmlResult.Contains("ok")) _reportedStats = true;
648+
try
649+
{
650+
var client = new WebClient { Proxy = CodeStatsPackage.GetProxy() };
651+
client.Headers[HttpRequestHeader.UserAgent] = Constants.PluginUserAgent;
652+
string HtmlResult = client.DownloadString("https://p0358.net/codestats/report.php?pluginver=" + Constants.PluginVersion + "&cid=" + CodeStatsPackage.Guid + "&editorname=" + Constants.EditorName + "&editorver=" + Constants.EditorVersion + "&is64process=" + ProcessorArchitectureHelper.Is64BitProcess.ToString().ToLowerInvariant() + "&is64sys=" + ProcessorArchitectureHelper.Is64BitOperatingSystem.ToString().ToLowerInvariant()); // expected response: ok
653+
if (HtmlResult.Contains("ok")) _reportedStats = true;
654+
}
655+
finally { }
634656
}
635657

636658
private static string ToUnixEpoch(DateTime date)
@@ -715,8 +737,10 @@ internal static void RecursiveDelete(string folder)
715737

716738
internal static class CoreAssembly
717739
{
718-
static readonly Assembly Reference = typeof(CoreAssembly).Assembly;
740+
public static readonly Assembly Reference = typeof(CoreAssembly).Assembly;
719741
public static readonly Version Version = Reference.GetName().Version;
742+
// System.Reflection.Assembly.GetExecutingAssembly().Location
743+
public static readonly string Location = Reference.Location;
720744
}
721745

722746
internal static void PluginCleanUp()

CodeStats/CustomMappingConfig.cs

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
using Kbg.NppPluginNET.PluginInfrastructure;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
8+
namespace CodeStats
9+
{
10+
public class CustomMappingConfig
11+
{
12+
internal string ApiKey { get; set; }
13+
internal string ApiUrl { get; set; }
14+
internal string Proxy { get; set; }
15+
internal bool Stats { get; set; }
16+
internal string Guid { get; set; }
17+
internal bool Debug { get; set; }
18+
19+
private readonly string _configFilepath;
20+
21+
internal CustomMappingConfig()
22+
{
23+
_configFilepath = GetConfigFilePath();
24+
Read();
25+
}
26+
27+
internal void Read()
28+
{
29+
var ret = new StringBuilder(2083);
30+
31+
ApiKey = NativeMethods.GetPrivateProfileString("settings", "api_key", "", ret, 2083, _configFilepath) > 0
32+
? ret.ToString()
33+
: string.Empty;
34+
35+
ApiUrl = NativeMethods.GetPrivateProfileString("settings", "api_url", "", ret, 2083, _configFilepath) > 0
36+
? ret.ToString()
37+
: string.Empty;
38+
39+
Proxy = NativeMethods.GetPrivateProfileString("settings", "proxy", "", ret, 2083, _configFilepath) > 0
40+
? ret.ToString()
41+
: string.Empty;
42+
43+
if (NativeMethods.GetPrivateProfileString("settings", "stats", "true", ret, 2083, _configFilepath) > 0)
44+
{
45+
bool stats;
46+
if (bool.TryParse(ret.ToString(), out stats))
47+
Stats = stats;
48+
}
49+
else
50+
{
51+
Stats = true;
52+
}
53+
54+
this.Guid = NativeMethods.GetPrivateProfileString("settings", "guid", "", ret, 2083, _configFilepath) > 0
55+
? ret.ToString()
56+
: System.Guid.NewGuid().ToString();
57+
58+
// ReSharper disable once InvertIf
59+
if (NativeMethods.GetPrivateProfileString("settings", "debug", "", ret, 2083, _configFilepath) > 0)
60+
{
61+
bool debug;
62+
if (bool.TryParse(ret.ToString(), out debug))
63+
Debug = debug;
64+
}
65+
}
66+
67+
static string GetConfigFilePath()
68+
{
69+
//var homeFolder = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
70+
71+
// get path of plugin configuration
72+
StringBuilder sbIniFilePath = new StringBuilder(Win32.MAX_PATH);
73+
Win32.SendMessage(PluginBase.nppData._nppHandle, (uint)NppMsg.NPPM_GETPLUGINSCONFIGDIR, Win32.MAX_PATH, sbIniFilePath);
74+
string iniFilePath = sbIniFilePath.ToString();
75+
76+
return Path.Combine(iniFilePath, "CodeStats_custom_extension_mapping.ini");
77+
}
78+
}
79+
}

0 commit comments

Comments
 (0)