Skip to content

Commit d625bc9

Browse files
committed
WindowStateEditor
1 parent 110fde5 commit d625bc9

File tree

4 files changed

+139
-87
lines changed

4 files changed

+139
-87
lines changed

NotepadStateLibrary/NotepadStateLibrary.sln

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WindowsNotepadParser-Minima
1515
EndProject
1616
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ValidationTests", "ValidationTests\ValidationTests.csproj", "{532A36A6-70FC-4F80-BED0-7FEDDA8DA9B4}"
1717
EndProject
18+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WindowStateEditor", "WindowStateEditor\WindowStateEditor.csproj", "{7128ED92-E8E0-4C3F-B683-780CCE0FBA20}"
19+
EndProject
1820
Global
1921
GlobalSection(SolutionConfigurationPlatforms) = preSolution
2022
Debug|Any CPU = Debug|Any CPU
@@ -45,6 +47,10 @@ Global
4547
{532A36A6-70FC-4F80-BED0-7FEDDA8DA9B4}.Debug|Any CPU.Build.0 = Debug|Any CPU
4648
{532A36A6-70FC-4F80-BED0-7FEDDA8DA9B4}.Release|Any CPU.ActiveCfg = Release|Any CPU
4749
{532A36A6-70FC-4F80-BED0-7FEDDA8DA9B4}.Release|Any CPU.Build.0 = Release|Any CPU
50+
{7128ED92-E8E0-4C3F-B683-780CCE0FBA20}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
51+
{7128ED92-E8E0-4C3F-B683-780CCE0FBA20}.Debug|Any CPU.Build.0 = Debug|Any CPU
52+
{7128ED92-E8E0-4C3F-B683-780CCE0FBA20}.Release|Any CPU.ActiveCfg = Release|Any CPU
53+
{7128ED92-E8E0-4C3F-B683-780CCE0FBA20}.Release|Any CPU.Build.0 = Release|Any CPU
4854
EndGlobalSection
4955
GlobalSection(SolutionProperties) = preSolution
5056
HideSolutionNode = FALSE

NotepadStateLibrary/NotepadStateLibrary/NPWindowState.cs

Lines changed: 14 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -94,93 +94,8 @@ public NPWindowState(byte[] bytes, string fileName)
9494
/// <param name="NewActiveTab">Integer of the active tab. 0 based index.</param>
9595
public void ChangeActiveTab(int NewActiveTab)
9696
{
97-
using (MemoryStream stream = new MemoryStream(bytes))
98-
{
99-
using (MemoryStream outStream = new MemoryStream())
100-
{
101-
using (BinaryReader reader = new BinaryReader(stream))
102-
{
103-
using (BinaryWriter writer = new BinaryWriter(outStream))
104-
{
105-
var hdr = reader.ReadBytes(2);
106-
writer.Write(hdr);
107-
string hdrType = Encoding.ASCII.GetString(hdr);
108-
if (hdrType == "NP")
109-
{
110-
CRC32Check c = new CRC32Check();
111-
112-
reader.ReadLEB128Unsigned();
113-
writer.Write(SequenceNumber.WriteLEB128Unsigned());
114-
c.AddBytes(SequenceNumber);
115-
116-
reader.ReadLEB128Unsigned();
117-
writer.Write(BytesToCRC);
118-
c.AddBytes(BytesToCRC);
119-
120-
121-
var delim = reader.ReadBytes(1);
122-
writer.Write(delim);
123-
c.AddBytes(delim);
124-
125-
reader.ReadLEB128Unsigned();
126-
writer.Write(NumberTabs.WriteLEB128Unsigned());
127-
c.AddBytes(NumberTabs);
128-
129-
for (int x = 0; x < (int)NumberTabs; x++)
130-
{
131-
var chunk = reader.ReadBytes(16);
132-
writer.Write(chunk);
133-
c.AddBytes(chunk);
134-
}
135-
136-
reader.ReadLEB128Unsigned(); //Active Tab
137-
ActiveTab = (ulong)NewActiveTab;
138-
writer.Write(ActiveTab.WriteLEB128Unsigned());
139-
c.AddBytes(ActiveTab);
140-
141-
var tlc1 = reader.ReadUInt32();
142-
writer.Write(tlc1);
143-
c.AddBytes(tlc1);
144-
var tlc2 = reader.ReadUInt32();
145-
writer.Write(tlc2);
146-
c.AddBytes(tlc2);
147-
148-
TopLeftCoords = new WindowXY((int)tlc1, (int)tlc2);
149-
150-
151-
var brc3 = reader.ReadUInt32();
152-
writer.Write(brc3);
153-
c.AddBytes(brc3);
154-
var brc4 = reader.ReadUInt32();
155-
writer.Write(brc4);
156-
c.AddBytes(brc4);
157-
158-
BottomRightCoords = new WindowXY((int)brc3, (int)brc4);
159-
160-
161-
var wsc5 = reader.ReadUInt32();
162-
writer.Write(wsc5);
163-
c.AddBytes(wsc5);
164-
var wsc6 = reader.ReadUInt32();
165-
writer.Write(wsc6);
166-
c.AddBytes(wsc6);
167-
168-
WindowSize = new WindowXY((int)wsc5, (int)wsc6);
169-
170-
171-
var delim2 = reader.ReadBytes(1);
172-
writer.Write(delim2);
173-
c.AddBytes(delim2);
174-
175-
reader.ReadBytes(4);
176-
CRC32Stored = c.CRC32;
177-
writer.Write(CRC32Stored);
178-
CRC32Calculated = c.CRC32;
179-
}
180-
}
181-
}
182-
}
183-
}
97+
this.ActiveTab = (ulong)NewActiveTab;
98+
Save();
18499
}
185100

186101
/// <summary>
@@ -195,6 +110,18 @@ public void WriteTabList(List<byte[]> newTabs)
195110
Save();
196111
}
197112

113+
public void WriteTabList(List<Guid> newTabs)
114+
{
115+
NumberTabs = (ulong) newTabs.Count;
116+
Tabs = new List<byte[]>();
117+
foreach(Guid g in newTabs)
118+
{
119+
Tabs.Add(g.ToByteArray());
120+
}
121+
122+
Save();
123+
}
124+
198125
/// <summary>
199126
///
200127
/// </summary>
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
// See https://aka.ms/new-console-template for more information
2+
using NotepadStateLibrary;
3+
using CommandLine;
4+
using System.Diagnostics;
5+
using System.Formats.Asn1;
6+
using System.Globalization;
7+
using System.Text;
8+
using System.ComponentModel;
9+
10+
11+
Parser.Default.ParseArguments<Options>(args)
12+
.WithParsed(options =>
13+
{
14+
string windowStateLocation = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), @"Packages\Microsoft.WindowsNotepad_8wekyb3d8bbwe\LocalState\WindowState");
15+
List<Guid> guidList = new List<Guid>();
16+
17+
18+
if (!string.IsNullOrWhiteSpace(options.windowStateLocation))
19+
{
20+
windowStateLocation = options.windowStateLocation;
21+
}
22+
23+
foreach (var stringGuid in options.guidTabs ?? new List<string>())
24+
{
25+
if (Guid.TryParse(stringGuid, out Guid guid))
26+
{
27+
guidList.Add(guid);
28+
Console.WriteLine("Add GUID: {0}", stringGuid);
29+
}
30+
else
31+
{
32+
Console.WriteLine("Invalid GUID: {0}", stringGuid);
33+
}
34+
}
35+
36+
Console.WriteLine("********** Starting **********");
37+
38+
foreach (var path in Directory.EnumerateFiles(windowStateLocation, "*.bin"))
39+
{
40+
byte[] o = new byte[0];
41+
42+
using (FileStream fileStream = new FileStream(path, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite))
43+
{
44+
byte[] data = new byte[fileStream.Length];
45+
fileStream.Read(data);
46+
47+
if (data.Length > 0)
48+
{
49+
Console.WriteLine("Processing WindowState - {0}", Path.GetFileName(path));
50+
NPWindowState np = new NPWindowState(data, Path.GetFileName(path));
51+
52+
if (guidList.Count > 0)
53+
{
54+
np.WriteTabList(guidList);
55+
56+
np.ChangeActiveTab(options.activeTab);
57+
58+
}
59+
60+
61+
62+
o = np.bytes;
63+
}
64+
}
65+
File.WriteAllBytes(path, o);
66+
}
67+
68+
Console.WriteLine("********** Finished **********");
69+
})
70+
.WithNotParsed(errors =>
71+
{
72+
foreach (var error in errors)
73+
{
74+
if (error is HelpRequestedError || error is VersionRequestedError)
75+
{
76+
77+
}
78+
else
79+
{
80+
Console.WriteLine($"Error: {error}");
81+
}
82+
}
83+
});
84+
85+
86+
public class Options
87+
{
88+
[Option('w', "windowstatelocation", Required = false, HelpText = "Window State Folder Location. Default value is the system location.")]
89+
public string windowStateLocation { get; set; }
90+
91+
[Option('t', "tabs", Required = true, HelpText = "Space seperated GUIDs to write to Window State File.")]
92+
public IEnumerable<string> guidTabs { get; set; }
93+
94+
[Option('a', "activeTab", Required = false, HelpText = "0 based index of the active tab. Default value is 0", Default = 0)]
95+
public int activeTab { get; set; }
96+
}
97+
98+
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
<Authors>ogmini</Authors>
9+
<Version>0.0.1</Version>
10+
<PackageProjectUrl>https://github.com/ogmini/Notepad-State-Library</PackageProjectUrl>
11+
</PropertyGroup>
12+
13+
<ItemGroup>
14+
<PackageReference Include="CommandLineParser" Version="2.9.1" />
15+
</ItemGroup>
16+
17+
<ItemGroup>
18+
<ProjectReference Include="..\NotepadStateLibrary\NotepadStateLibrary.csproj" />
19+
</ItemGroup>
20+
21+
</Project>

0 commit comments

Comments
 (0)